--- /dev/null
+Return-Path: <awg@lagos.xvx.ca>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id A1B4F429E55\r
+ for <notmuch@notmuchmail.org>; Wed, 15 Feb 2012 19:12:52 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+ autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id Zp5RvT7I2PaN for <notmuch@notmuchmail.org>;\r
+ Wed, 15 Feb 2012 19:12:51 -0800 (PST)\r
+Received: from smtp-out-04.shaw.ca (smtp-out-04.shaw.ca [64.59.134.12])\r
+ by olra.theworths.org (Postfix) with ESMTP id 9D505429E4D\r
+ for <notmuch@notmuchmail.org>; Wed, 15 Feb 2012 19:12:47 -0800 (PST)\r
+Received: from lb7f8hsrpno-svcs.dcs.int.inet (HELO pd7ml3no-ssvc.prod.shaw.ca)\r
+ ([10.0.144.222])\r
+ by pd5mo1no-svcs.prod.shaw.ca with ESMTP; 15 Feb 2012 20:12:47 -0700\r
+X-Cloudmark-SP-Filtered: true\r
+X-Cloudmark-SP-Result: v=1.1 cv=aDUJ/pRHNXkohnfhaDKKve0FfU8uPxX8npdo6G126bI=\r
+ c=1 sm=1\r
+ a=bULfHVm_DoMA:10 a=BLceEmwcHowA:10 a=yQp6g8lIsgqumF79BAsFDg==:17\r
+ a=IRZdaMhHRr9h3ODv8asA:9 a=HpAAvcLHHh0Zw7uRqdWCyQ==:117\r
+Received: from unknown (HELO lagos.xvx.ca) ([96.52.216.56])\r
+ by pd7ml3no-dmz.prod.shaw.ca with ESMTP; 15 Feb 2012 20:12:47 -0700\r
+Received: by lagos.xvx.ca (Postfix, from userid 1000)\r
+ id 121DC8004EDC; Wed, 15 Feb 2012 20:12:46 -0700 (MST)\r
+From: Adam Wolfe Gordon <awg+notmuch@xvx.ca>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v5.2 5/7] emacs: Factor out useful functions into notmuch-lib\r
+Date: Wed, 15 Feb 2012 20:12:35 -0700\r
+Message-Id: <1329361957-28493-6-git-send-email-awg+notmuch@xvx.ca>\r
+X-Mailer: git-send-email 1.7.5.4\r
+In-Reply-To: <1329361957-28493-1-git-send-email-awg+notmuch@xvx.ca>\r
+References: <1329361957-28493-1-git-send-email-awg+notmuch@xvx.ca>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Thu, 16 Feb 2012 03:12:53 -0000\r
+\r
+Move a few functions related to handling multipart/alternative parts\r
+into notmuch-lib.el, so they can be used by future reply code.\r
+---\r
+ emacs/notmuch-lib.el | 33 +++++++++++++++++++++++++++++++++\r
+ emacs/notmuch-show.el | 24 ++----------------------\r
+ 2 files changed, 35 insertions(+), 22 deletions(-)\r
+\r
+diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el\r
+index d315f76..7e3f110 100644\r
+--- a/emacs/notmuch-lib.el\r
++++ b/emacs/notmuch-lib.el\r
+@@ -21,6 +21,8 @@\r
+ \r
+ ;; This is an part of an emacs-based interface to the notmuch mail system.\r
+ \r
++(eval-when-compile (require 'cl))\r
++\r
+ (defvar notmuch-command "notmuch"\r
+ "Command to run the notmuch binary.")\r
+ \r
+@@ -173,6 +175,37 @@ the user hasn't set this variable with the old or new value."\r
+ (list 'when (< emacs-major-version 23)\r
+ form))\r
+ \r
++(defun notmuch-split-content-type (content-type)\r
++ "Split content/type into 'content' and 'type'"\r
++ (split-string content-type "/"))\r
++\r
++(defun notmuch-match-content-type (t1 t2)\r
++ "Return t if t1 and t2 are matching content types, taking wildcards into account"\r
++ (let ((st1 (notmuch-split-content-type t1))\r
++ (st2 (notmuch-split-content-type t2)))\r
++ (if (or (string= (cadr st1) "*")\r
++ (string= (cadr st2) "*"))\r
++ (string= (car st1) (car st2))\r
++ (string= t1 t2))))\r
++\r
++(defvar notmuch-multipart/alternative-discouraged\r
++ '(\r
++ ;; Avoid HTML parts.\r
++ "text/html"\r
++ ;; multipart/related usually contain a text/html part and some associated graphics.\r
++ "multipart/related"\r
++ ))\r
++\r
++(defun notmuch-multipart/alternative-choose (types)\r
++ "Return a list of preferred types from the given list of types"\r
++ ;; Based on `mm-preferred-alternative-precedence'.\r
++ (let ((seq types))\r
++ (dolist (pref (reverse notmuch-multipart/alternative-discouraged))\r
++ (dolist (elem (copy-sequence seq))\r
++ (when (string-match pref elem)\r
++ (setq seq (nconc (delete elem seq) (list elem))))))\r
++ seq))\r
++\r
+ ;; Compatibility functions for versions of emacs before emacs 23.\r
+ ;;\r
+ ;; Both functions here were copied from emacs 23 with the following copyright:\r
+diff --git a/emacs/notmuch-show.el b/emacs/notmuch-show.el\r
+index 43408d9..90cdd38 100644\r
+--- a/emacs/notmuch-show.el\r
++++ b/emacs/notmuch-show.el\r
+@@ -513,30 +513,13 @@ current buffer, if possible."\r
+ (mm-display-part handle)\r
+ t))))))\r
+ \r
+-(defvar notmuch-show-multipart/alternative-discouraged\r
+- '(\r
+- ;; Avoid HTML parts.\r
+- "text/html"\r
+- ;; multipart/related usually contain a text/html part and some associated graphics.\r
+- "multipart/related"\r
+- ))\r
+-\r
+ (defun notmuch-show-multipart/*-to-list (part)\r
+ (mapcar (lambda (inner-part) (plist-get inner-part :content-type))\r
+ (plist-get part :content)))\r
+ \r
+-(defun notmuch-show-multipart/alternative-choose (types)\r
+- ;; Based on `mm-preferred-alternative-precedence'.\r
+- (let ((seq types))\r
+- (dolist (pref (reverse notmuch-show-multipart/alternative-discouraged))\r
+- (dolist (elem (copy-sequence seq))\r
+- (when (string-match pref elem)\r
+- (setq seq (nconc (delete elem seq) (list elem))))))\r
+- seq))\r
+-\r
+ (defun notmuch-show-insert-part-multipart/alternative (msg part content-type nth depth declared-type)\r
+ (notmuch-show-insert-part-header nth declared-type content-type nil)\r
+- (let ((chosen-type (car (notmuch-show-multipart/alternative-choose (notmuch-show-multipart/*-to-list part))))\r
++ (let ((chosen-type (car (notmuch-multipart/alternative-choose (notmuch-show-multipart/*-to-list part))))\r
+ (inner-parts (plist-get part :content))\r
+ (start (point)))\r
+ ;; This inserts all parts of the chosen type rather than just one,\r
+@@ -775,9 +758,6 @@ current buffer, if possible."\r
+ \r
+ ;; Functions for determining how to handle MIME parts.\r
+ \r
+-(defun notmuch-show-split-content-type (content-type)\r
+- (split-string content-type "/"))\r
+-\r
+ (defun notmuch-show-handlers-for (content-type)\r
+ "Return a list of content handlers for a part of type CONTENT-TYPE."\r
+ (let (result)\r
+@@ -788,7 +768,7 @@ current buffer, if possible."\r
+ (list (intern (concat "notmuch-show-insert-part-*/*"))\r
+ (intern (concat\r
+ "notmuch-show-insert-part-"\r
+- (car (notmuch-show-split-content-type content-type))\r
++ (car (notmuch-split-content-type content-type))\r
+ "/*"))\r
+ (intern (concat "notmuch-show-insert-part-" content-type))))\r
+ result))\r
+-- \r
+1.7.5.4\r
+\r