--- /dev/null
+Return-Path: <amdragon@mit.edu>\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 1F765431FAE\r
+ for <notmuch@notmuchmail.org>; Sat, 24 Jan 2015 13:17:19 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0.138\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0.138 tagged_above=-999 required=5\r
+ tests=[DNS_FROM_AHBL_RHSBL=2.438, RCVD_IN_DNSWL_MED=-2.3]\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 MrD7Oc5j5wk6 for <notmuch@notmuchmail.org>;\r
+ Sat, 24 Jan 2015 13:17:15 -0800 (PST)\r
+Received: from dmz-mailsec-scanner-3.mit.edu (dmz-mailsec-scanner-3.mit.edu\r
+ [18.9.25.14])\r
+ (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id D8F83431FCB\r
+ for <notmuch@notmuchmail.org>; Sat, 24 Jan 2015 13:17:12 -0800 (PST)\r
+X-AuditID: 1209190e-f799e6d000000cfe-91-54c40bd6bd45\r
+Received: from mailhub-auth-2.mit.edu ( [18.7.62.36])\r
+ (using TLS with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+ (Client did not present a certificate)\r
+ by dmz-mailsec-scanner-3.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id 24.73.03326.6DB04C45; Sat, 24 Jan 2015 16:17:10 -0500 (EST)\r
+Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11])\r
+ by mailhub-auth-2.mit.edu (8.13.8/8.9.2) with ESMTP id t0OLH9fM000700; \r
+ Sat, 24 Jan 2015 16:17:09 -0500\r
+Received: from drake (216-15-114-40.c3-0.arl-ubr1.sbo-arl.ma.cable.rcn.com\r
+ [216.15.114.40]) (authenticated bits=0)\r
+ (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+ by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id t0OLH6P5007459\r
+ (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT);\r
+ Sat, 24 Jan 2015 16:17:07 -0500\r
+Received: from amthrax by drake with local (Exim 4.84)\r
+ (envelope-from <amdragon@mit.edu>)\r
+ id 1YF84n-0005Rf-Lu; Sat, 24 Jan 2015 16:17:05 -0500\r
+From: Austin Clements <amdragon@mit.edu>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 5/8] emacs: Support caching in\r
+ notmuch-get-bodypart-{binary, text}\r
+Date: Sat, 24 Jan 2015 16:17:00 -0500\r
+Message-Id: <1422134223-20739-6-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 2.1.3\r
+In-Reply-To: <1422134223-20739-1-git-send-email-amdragon@mit.edu>\r
+References: <1422134223-20739-1-git-send-email-amdragon@mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFrrFIsWRmVeSWpSXmKPExsUixG6nonuN+0iIwbeTchY3WrsZLfbd2cJk\r
+ sXouj8X1mzOZLd6snMfqwOqx6/lfJo+ds+6yexz+upDF49mqW8weWw69Zw5gjeKySUnNySxL\r
+ LdK3S+DKaD85kb1gvXLFl4cz2BoYF8h0MXJySAiYSGw5+oAJwhaTuHBvPVsXIxeHkMBiJon+\r
+ k9dYIJyNjBIvtqxkhnAuMklcPNDECOFMYpTomvWdEaSfTUBD4vetxWCzRASkJXbenc0KYjML\r
+ 1En8nXMerEZYIETi/8wPYHEWAVWJ7+uWs4PYvAIOEqfvPACKcwDdISexdZ03SJhTwFHixoZd\r
+ LCC2EFBJ9+dGtgmM/AsYGVYxyqbkVunmJmbmFKcm6xYnJ+blpRbpGuvlZpbopaaUbmIEBSGn\r
+ JN8Oxq8HlQ4xCnAwKvHwBjAeDhFiTSwrrsw9xCjJwaQkyrvqF1CILyk/pTIjsTgjvqg0J7X4\r
+ EKMEB7OSCO+FDUA53pTEyqrUonyYlDQHi5I476YffCFCAumJJanZqakFqUUwWRkODiUJXkZg\r
+ tAkJFqWmp1akZeaUIKSZODhBhvMADWcFqeEtLkjMLc5Mh8ifYlSUEuf9ygWUEABJZJTmwfXC\r
+ ksQrRnGgV4R560GqeIAJBq77FdBgJqDBBdsPgAwuSURISTUwKviE3OaU9oyvv1S34p2lZ9em\r
+ ygcuZ4WUxfX/Jy3sDHojkfFqzdZE0bWr8mbWMohuXacXvuZZZJzVe8ZX+x/mB8W0Rd1/k1Nz\r
+ embk7tArnsy83gxVvXszc6Y3pr2/n9z0dl6hg2XOrWNbvypb5X6UmFX5/E6AiuaLW2ru2w3u\r
+ CcZfV3faMFFbiaU4I9FQi7moOBEAXsVqY+0CAAA=\r
+Cc: tomi.ollila@iki.fi\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: Sat, 24 Jan 2015 21:17:19 -0000\r
+\r
+(The actual code change here is small, but requires re-indenting\r
+existing code.)\r
+---\r
+ emacs/notmuch-lib.el | 64 ++++++++++++++++++++++++++++++++--------------------\r
+ 1 file changed, 40 insertions(+), 24 deletions(-)\r
+\r
+diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el\r
+index 3154725..f8e5165 100644\r
+--- a/emacs/notmuch-lib.el\r
++++ b/emacs/notmuch-lib.el\r
+@@ -529,39 +529,53 @@ (defun notmuch-parts-filter-by-type (parts type)\r
+ (lambda (part) (notmuch-match-content-type (plist-get part :content-type) type))\r
+ parts))\r
+ \r
+-(defun notmuch-get-bodypart-binary (msg part process-crypto)\r
++(defun notmuch-get-bodypart-binary (msg part process-crypto &optional cache)\r
+ "Return the unprocessed content of PART in MSG as a unibyte string.\r
+ \r
+ This returns the \"raw\" content of the given part after content\r
+ transfer decoding, but with no further processing (see the\r
+ discussion of --format=raw in man notmuch-show). In particular,\r
+-this does no charset conversion."\r
+- (let ((args `("show" "--format=raw"\r
+- ,(format "--part=%d" (plist-get part :id))\r
+- ,@(when process-crypto '("--decrypt"))\r
+- ,(notmuch-id-to-query (plist-get msg :id)))))\r
+- (with-temp-buffer\r
+- ;; Emacs internally uses a UTF-8-like multibyte string\r
+- ;; representation by default (regardless of the coding system,\r
+- ;; which only affects how it goes from outside data to this\r
+- ;; internal representation). This *almost* never matters.\r
+- ;; Annoyingly, it does matter if we use this data in an image\r
+- ;; descriptor, since Emacs will use its internal data buffer\r
+- ;; directly and this multibyte representation corrupts binary\r
+- ;; image formats. Since the caller is asking for binary data, a\r
+- ;; unibyte string is a more appropriate representation anyway.\r
+- (set-buffer-multibyte nil)\r
+- (let ((coding-system-for-read 'no-conversion))\r
+- (apply #'call-process notmuch-command nil '(t nil) nil args)\r
+- (buffer-string)))))\r
+-\r
+-(defun notmuch-get-bodypart-text (msg part process-crypto)\r
++this does no charset conversion.\r
++\r
++If CACHE is non-nil, the content of this part will be saved in\r
++MSG (if it isn't already)."\r
++ (let ((data (plist-get part :binary-content)))\r
++ (when (not data)\r
++ (let ((args `("show" "--format=raw"\r
++ ,(format "--part=%d" (plist-get part :id))\r
++ ,@(when process-crypto '("--decrypt"))\r
++ ,(notmuch-id-to-query (plist-get msg :id)))))\r
++ (with-temp-buffer\r
++ ;; Emacs internally uses a UTF-8-like multibyte string\r
++ ;; representation by default (regardless of the coding\r
++ ;; system, which only affects how it goes from outside data\r
++ ;; to this internal representation). This *almost* never\r
++ ;; matters. Annoyingly, it does matter if we use this data\r
++ ;; in an image descriptor, since Emacs will use its internal\r
++ ;; data buffer directly and this multibyte representation\r
++ ;; corrupts binary image formats. Since the caller is\r
++ ;; asking for binary data, a unibyte string is a more\r
++ ;; appropriate representation anyway.\r
++ (set-buffer-multibyte nil)\r
++ (let ((coding-system-for-read 'no-conversion))\r
++ (apply #'call-process notmuch-command nil '(t nil) nil args)\r
++ (setq data (buffer-string)))))\r
++ (when cache\r
++ ;; Cheat. part is non-nil, and `plist-put' always modifies\r
++ ;; the list in place if it's non-nil.\r
++ (plist-put part :binary-content data)))\r
++ data))\r
++\r
++(defun notmuch-get-bodypart-text (msg part process-crypto &optional cache)\r
+ "Return the text content of PART in MSG.\r
+ \r
+ This returns the content of the given part as a multibyte Lisp\r
+ string after performing content transfer decoding and any\r
+ necessary charset decoding. It is an error to use this for\r
+-non-text/* parts."\r
++non-text/* parts.\r
++\r
++If CACHE is non-nil, the content of this part will be saved in\r
++MSG (if it isn't already)."\r
+ (let ((content (plist-get part :content)))\r
+ (when (not content)\r
+ ;; Use show --format=sexp to fetch decoded content\r
+@@ -572,7 +586,9 @@ (defun notmuch-get-bodypart-text (msg part process-crypto)\r
+ (npart (apply #'notmuch-call-notmuch-sexp args)))\r
+ (setq content (plist-get npart :content))\r
+ (when (not content)\r
+- (error "Internal error: No :content from %S" args))))\r
++ (error "Internal error: No :content from %S" args)))\r
++ (when cache\r
++ (plist-put part :content content)))\r
+ content))\r
+ \r
+ ;; Workaround: The call to `mm-display-part' below triggers a bug in\r
+-- \r
+2.1.3\r
+\r