Re: [feature request] emacs: use `notmuch insert` for FCC
[notmuch-archives.git] / 4f / ae6d407a55c04ab643d0d8b85d48bfc483b3a9
1 Return-Path: <dme@dme.org>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 190B8431FB6\r
6         for <notmuch@notmuchmail.org>; Tue,  7 Oct 2014 00:57:19 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -0.7\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
12         tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id nhCCKu3eRI+9 for <notmuch@notmuchmail.org>;\r
16         Tue,  7 Oct 2014 00:57:14 -0700 (PDT)\r
17 Received: from mail-wg0-f49.google.com (mail-wg0-f49.google.com\r
18  [74.125.82.49])        (using TLSv1 with cipher RC4-SHA (128/128 bits))        (No client\r
19  certificate requested) by olra.theworths.org (Postfix) with ESMTPS id\r
20  2B21E431FAE    for <notmuch@notmuchmail.org>; Tue,  7 Oct 2014 00:57:14 -0700\r
21  (PDT)\r
22 Received: by mail-wg0-f49.google.com with SMTP id x12so8538054wgg.20\r
23         for <notmuch@notmuchmail.org>; Tue, 07 Oct 2014 00:57:13 -0700 (PDT)\r
24 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
25         d=1e100.net; s=20130820;\r
26         h=x-gm-message-state:to:subject:in-reply-to:references:user-agent\r
27         :from:date:message-id:mime-version:content-type;\r
28         bh=F8Q2p2uCHjkXkNFu+8eMwVRPI9n0G/ButLRhBMNo6/w=;\r
29         b=Kwyd0QoSPFiOM4r7raPBMimZl0dKeVnYayvGtTKhc7LYGH0eNp1VRThsGz5GOPdJEI\r
30         TzkNCge6SqAXV44x9b45hhjWR7/VoSPDLr6StiG4ZBi4A0HQJucS0ECry0PAh7UOAXsj\r
31         GG3ng19M4GPwec9rgQvSXTyutF0Biff7dBGk6qnfkD7XqGF7g5mUJxpoq/+F16v9h/c1\r
32         POdF3o96SwKVQwgx2KAZUq1ccEAxFoS6OsqqIfZ/XWj9fOgDGA6ULB15UahjPWWrhf9J\r
33         lDizaDidVNcBZBDRiduj3JGplWh0KaRT71UXOawaO6ENE5SeswsfnxjVMpdX9wSJZczv\r
34         wOTg==\r
35 X-Gm-Message-State:\r
36  ALoCoQnAWptzOvjC3ngQ9uwy2UFb1mVFqxL/A3eGRPh7Zlr2//Ws90WQikRSNPJ+QAOcJnCvcov6\r
37 X-Received: by 10.194.119.9 with SMTP id kq9mr2617393wjb.25.1412668632947;\r
38         Tue, 07 Oct 2014 00:57:12 -0700 (PDT)\r
39 Received: from localhost ([2a01:348:1a2:1:bc6b:bac4:2954:d414])\r
40         by mx.google.com with ESMTPSA id\r
41         fs13sm13703833wic.19.2014.10.07.00.57.11 for <multiple recipients>\r
42         (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\r
43         Tue, 07 Oct 2014 00:57:12 -0700 (PDT)\r
44 To: notmuch@notmuchmail.org\r
45 Subject: Re: RFC: notmuch-cache.el: simple caching of MIME parts\r
46 In-Reply-To: <m2wq8cmi2c.fsf@fenchurch.hh.sledj.net>\r
47 References: <m2wq8cmi2c.fsf@fenchurch.hh.sledj.net>\r
48 User-Agent: Notmuch/0.18.1 (http://notmuchmail.org) Emacs/24.3.1\r
49         (x86_64-apple-darwin13.3.0)\r
50 From: David Edmondson <dme@dme.org>\r
51 Date: Tue, 07 Oct 2014 08:57:09 +0100\r
52 Message-ID: <m2tx3gmhwq.fsf@fenchurch.hh.sledj.net>\r
53 MIME-Version: 1.0\r
54 Content-Type: multipart/mixed; boundary="=-=-="\r
55 X-BeenThere: notmuch@notmuchmail.org\r
56 X-Mailman-Version: 2.1.13\r
57 Precedence: list\r
58 List-Id: "Use and development of the notmuch mail system."\r
59         <notmuch.notmuchmail.org>\r
60 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
61         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
62 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
63 List-Post: <mailto:notmuch@notmuchmail.org>\r
64 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
65 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
66         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
67 X-List-Received-Date: Tue, 07 Oct 2014 07:57:19 -0000\r
68 \r
69 --=-=-=\r
70 Content-Type: text/plain\r
71 \r
72 On Tue, Oct 07 2014, David Edmondson wrote:\r
73 > I've been using remote-notmuch over a 3G connection quite a lot\r
74 > recently. Dragging down the same MIME part more than once is annoying,\r
75 > so here is a very simple filesystem based cache of MIME parts for\r
76 > notmuch. It's integrated using defadvice for now, but a cleaner approach\r
77 > is obviously possible.\r
78 >\r
79 > No suggestion that this should be integrated - it's just a toy to prompt\r
80 > discussion.\r
81 \r
82 Bah. Attached the auto-save file :-p\r
83 \r
84 \r
85 --=-=-=\r
86 Content-Type: application/emacs-lisp\r
87 Content-Disposition: attachment; filename=notmuch-cache.el\r
88 Content-Transfer-Encoding: quoted-printable\r
89 \r
90 ;; notmuch-cache.el --- a simple local filesystem cache of MIME parts\r
91 ;;\r
92 ;; Copyright =C2=A9 David Edmondson\r
93 ;;\r
94 ;; This file is not part of Notmuch.\r
95 ;;\r
96 ;; Notmuch is free software: you can redistribute it and/or modify it\r
97 ;; under the terms of the GNU General Public License as published by\r
98 ;; the Free Software Foundation, either version 3 of the License, or\r
99 ;; (at your option) any later version.\r
100 ;;\r
101 ;; Notmuch is distributed in the hope that it will be useful, but\r
102 ;; WITHOUT ANY WARRANTY; without even the implied warranty of\r
103 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
104 ;; General Public License for more details.\r
105 ;;\r
106 ;; You should have received a copy of the GNU General Public License\r
107 ;; along with Notmuch.  If not, see <http://www.gnu.org/licenses/>.\r
108 ;;\r
109 ;; Authors: David Edmondson <dme@dme.org>\r
110 \r
111 ;; Things to consider:\r
112 ;;\r
113 ;; - Keep an in-memory cache of parts? What would be an appropriate\r
114 ;;   way to limit the size of that cache?\r
115 ;;\r
116 ;; - In a long thread with mostly non-matching messages (i.e. new\r
117 ;;   message in old thread), the default show output includes the text\r
118 ;;   of all of the non-matching parts. Given that we can now insert\r
119 ;;   previously unshown parts on the fly, maybe non-matching body\r
120 ;;   parts should not be included in the default output, and code\r
121 ;;   added to glue them in when a non-matching message is\r
122 ;;   opened. (Would this be simpler if we convert simple text/plain\r
123 ;;   messages to look like MIME messages with a single text/plain\r
124 ;;   part?)\r
125 ;;\r
126 ;; - There is no cache management functionality. Just delete the\r
127 ;;   directory and its' contents for now.\r
128 \r
129 ;; Stats:\r
130 \r
131 (defvar notmuch-cache-stats [0 0 0]\r
132   "Statistics about the notmuch part cache.\r
133 \r
134 This is an array with three elements:\r
135  - the number of requests to the cache,\r
136  - the number of reads satisified from the cache,\r
137  - the number of writes to the cache.")\r
138 \r
139 (defun notmuch-cache-stat-incr (nth)\r
140   (aset notmuch-cache-stats nth (+ 1 (aref notmuch-cache-stats nth))))\r
141 \r
142 ;; The cache:\r
143 \r
144 (defcustom notmuch-cache-directory "~/.notmuch-cache/parts"\r
145   "A directory used to store cached message parts."\r
146   :type 'directory)\r
147 \r
148 (defun notmuch-cache-filename (id part)\r
149   (expand-file-name (format "%s/%s/%d" notmuch-cache-directory id part)))\r
150 \r
151 (defun notmuch-cache-get (id part)\r
152   "Return a cached copy of ID's MIME PART, or nil."\r
153 \r
154   (notmuch-cache-stat-incr 0) ;; Request.\r
155 =20=20\r
156   (let ((filename (notmuch-cache-filename id part)))\r
157     (if (not (file-exists-p filename))\r
158         nil\r
159       (notmuch-cache-stat-incr 1) ;; Read.\r
160       (let ((coding-system-for-read 'no-conversion))\r
161         (with-temp-buffer\r
162           (insert-file-contents (notmuch-cache-filename id part))\r
163           (buffer-substring (point-min) (point-max)))))))\r
164 \r
165 (defun notmuch-cache-put (id part content)\r
166   "Cache a copy of ID's MIME PART using CONTENT."\r
167 \r
168   (let ((filename (notmuch-cache-filename id part)))\r
169     (unless (file-exists-p filename)\r
170       (notmuch-cache-stat-incr 2) ;; Write.\r
171       (make-directory (file-name-directory filename) t)\r
172       (let ((coding-system-for-write 'no-conversion))\r
173         (with-temp-buffer\r
174           (insert content)\r
175           (write-region (point-min) (point-max) (notmuch-cache-filename id part)))=\r
176 ))))\r
177 \r
178 ;; Integration:\r
179 \r
180 (defadvice notmuch-get-bodypart-internal (around notmuch-bodypart-cache act=\r
181 ivate)\r
182   "Use a local cache of notmuch body parts."\r
183 \r
184   (let ((content (notmuch-cache-get query part-number)))\r
185     (if content\r
186         (setq ad-return-value content)\r
187       ad-do-it\r
188       (when ad-return-value\r
189         (notmuch-cache-put query part-number ad-return-value)))))\r
190 \r
191 --=-=-=--\r