[PATCH v6 2/3] emacs: quote MML tags in replies
[notmuch-archives.git] / ea / e40c1ca8b8d77365c4a59ab9589029bd61ae25
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 190AB429E5F\r
6         for <notmuch@notmuchmail.org>; Wed, 18 Jan 2012 00:00:32 -0800 (PST)\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 x4IGnAyRk7X4 for <notmuch@notmuchmail.org>;\r
16         Wed, 18 Jan 2012 00:00:27 -0800 (PST)\r
17 Received: from mail-we0-f181.google.com (mail-we0-f181.google.com\r
18         [74.125.82.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id 755CB429E27\r
21         for <notmuch@notmuchmail.org>; Wed, 18 Jan 2012 00:00:27 -0800 (PST)\r
22 Received: by werp13 with SMTP id p13so1165800wer.26\r
23         for <notmuch@notmuchmail.org>; Wed, 18 Jan 2012 00:00:26 -0800 (PST)\r
24 Received: by 10.216.131.91 with SMTP id l69mr5897649wei.28.1326873626115;\r
25         Wed, 18 Jan 2012 00:00:26 -0800 (PST)\r
26 Received: from hotblack-desiato.hh.sledj.net\r
27         (host81-149-164-25.in-addr.btopenworld.com. [81.149.164.25])\r
28         by mx.google.com with ESMTPS id bj10sm21031695wib.9.2012.01.18.00.00.23\r
29         (version=TLSv1/SSLv3 cipher=OTHER);\r
30         Wed, 18 Jan 2012 00:00:24 -0800 (PST)\r
31 Received: by hotblack-desiato.hh.sledj.net (Postfix, from userid 30000)\r
32         id 8AFDCA0397; Wed, 18 Jan 2012 08:00:22 +0000 (GMT)\r
33 From: David Edmondson <dme@dme.org>\r
34 To: notmuch@notmuchmail.org\r
35 Subject: =?UTF-8?q?=5BPATCH=5D=20emacs=3A=20Improved=20printing=20support=2E?=\r
36 Date: Wed, 18 Jan 2012 08:00:21 +0000\r
37 Message-Id: <1326873621-1701-1-git-send-email-dme@dme.org>\r
38 X-Mailer: git-send-email 1.7.7.3\r
39 In-Reply-To: <m2vcobbaol.fsf@gmail.com>\r
40 References: <m2vcobbaol.fsf@gmail.com>\r
41 MIME-Version: 1.0\r
42 Content-Type: text/plain; charset=UTF-8\r
43 Content-Transfer-Encoding: 8bit\r
44 X-BeenThere: notmuch@notmuchmail.org\r
45 X-Mailman-Version: 2.1.13\r
46 Precedence: list\r
47 List-Id: "Use and development of the notmuch mail system."\r
48         <notmuch.notmuchmail.org>\r
49 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
50         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
51 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
52 List-Post: <mailto:notmuch@notmuchmail.org>\r
53 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
54 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
55         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
56 X-List-Received-Date: Wed, 18 Jan 2012 08:00:32 -0000\r
57 \r
58 Add various functions to print notmuch messages and tie them together\r
59 with a simple frontend.\r
60 \r
61 Add a binding ('#') in `notmuch-show-mode' to print the current\r
62 message.\r
63 ---\r
64 \r
65 Updates based on Aaron's review - the comments were very useful in\r
66 improving things - thanks!\r
67 \r
68 Add a keybinding to `notmuch-show-mode'.\r
69 \r
70  emacs/Makefile.local   |    3 +-\r
71  emacs/notmuch-print.el |   85 ++++++++++++++++++++++++++++++++++++++++++++++++\r
72  emacs/notmuch-show.el  |   53 ++++++++++++++++++++++++++++++\r
73  3 files changed, 140 insertions(+), 1 deletions(-)\r
74  create mode 100644 emacs/notmuch-print.el\r
75 \r
76 diff --git a/emacs/Makefile.local b/emacs/Makefile.local\r
77 index 0c58b82..4fee0e8 100644\r
78 --- a/emacs/Makefile.local\r
79 +++ b/emacs/Makefile.local\r
80 @@ -13,7 +13,8 @@ emacs_sources := \\r
81         $(dir)/notmuch-maildir-fcc.el \\r
82         $(dir)/notmuch-message.el \\r
83         $(dir)/notmuch-crypto.el \\r
84 -       $(dir)/coolj.el\r
85 +       $(dir)/coolj.el \\r
86 +       $(dir)/notmuch-print.el\r
87  \r
88  emacs_images := \\r
89         $(srcdir)/$(dir)/notmuch-logo.png\r
90 diff --git a/emacs/notmuch-print.el b/emacs/notmuch-print.el\r
91 new file mode 100644\r
92 index 0000000..77f10bb\r
93 --- /dev/null\r
94 +++ b/emacs/notmuch-print.el\r
95 @@ -0,0 +1,85 @@\r
96 +;; notmuch-print.el --- printing messages from notmuch.\r
97 +;;\r
98 +;; Copyright © David Edmondson\r
99 +;;\r
100 +;; This file is part of Notmuch.\r
101 +;;\r
102 +;; Notmuch is free software: you can redistribute it and/or modify it\r
103 +;; under the terms of the GNU General Public License as published by\r
104 +;; the Free Software Foundation, either version 3 of the License, or\r
105 +;; (at your option) any later version.\r
106 +;;\r
107 +;; Notmuch is distributed in the hope that it will be useful, but\r
108 +;; WITHOUT ANY WARRANTY; without even the implied warranty of\r
109 +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
110 +;; General Public License for more details.\r
111 +;;\r
112 +;; You should have received a copy of the GNU General Public License\r
113 +;; along with Notmuch.  If not, see <http://www.gnu.org/licenses/>.\r
114 +;;\r
115 +;; Authors: David Edmondson <dme@dme.org>\r
116 +\r
117 +(defcustom notmuch-print-mechanism 'notmuch-print-lpr\r
118 +  "How should printing be done?"\r
119 +  :group 'notmuch\r
120 +  :type '(choice\r
121 +         (function :tag "Use lpr" notmuch-print-lpr)\r
122 +         (function :tag "Use ps-print" notmuch-print-ps-print)\r
123 +         (function :tag "Use ps-print then evince" notmuch-print-ps-print/evince)\r
124 +         (function :tag "Use muttprint" notmuch-print-muttprint)\r
125 +         (function :tag "Use muttprint then evince" notmuch-print-muttprint/evince)\r
126 +         (function :tag "Using a custom function")))\r
127 +\r
128 +;; Utility functions:\r
129 +\r
130 +(defun notmuch-print-run-evince (file)\r
131 +  "View FILE using 'evince'."\r
132 +  (start-process "evince" nil "evince" file))\r
133 +\r
134 +(defun notmuch-print-run-muttprint (&optional output)\r
135 +  "Pass the contents of the current buffer to 'muttprint'.\r
136 +\r
137 +Optional OUTPUT allows passing a list of flags to muttprint."\r
138 +  (apply #'call-process-region (point-min) (point-max)\r
139 +        ;; Reads from stdin.\r
140 +        "muttprint"\r
141 +        nil nil nil\r
142 +        ;; Show the tags.\r
143 +        "--printed-headers" "Date_To_From_CC_Newsgroups_*Subject*_/Tags/"\r
144 +        output))\r
145 +\r
146 +;; User-visible functions:\r
147 +\r
148 +(defun notmuch-print-lpr (msg)\r
149 +  "Print a message buffer using lpr."\r
150 +  (lpr-buffer))\r
151 +\r
152 +(defun notmuch-print-ps-print (msg)\r
153 +  "Print a message buffer using the ps-print package."\r
154 +  (let ((subject (plist-get (notmuch-show-get-prop :headers msg) :Subject)))\r
155 +    (rename-buffer subject t)\r
156 +    (ps-print-buffer)))\r
157 +\r
158 +(defun notmuch-print-ps-print/evince (msg)\r
159 +  "Preview a message buffer using ps-print and evince."\r
160 +  (let ((ps-file (make-temp-file "notmuch"))\r
161 +       (subject (plist-get (notmuch-show-get-prop :headers msg) :Subject)))\r
162 +    (rename-buffer subject t)\r
163 +    (ps-print-buffer ps-file)\r
164 +    (notmuch-print-run-evince ps-file)))\r
165 +\r
166 +(defun notmuch-print-muttprint (msg)\r
167 +  "Print a message using muttprint."\r
168 +  (notmuch-print-run-muttprint))\r
169\r
170 +(defun notmuch-print-muttprint/evince (msg)\r
171 +  "Preview a message buffer using muttprint and evince."\r
172 +  (let ((ps-file (make-temp-file "notmuch")))\r
173 +    (notmuch-print-run-muttprint (list "--printer" (concat "TO_FILE:" ps-file)))\r
174 +    (notmuch-print-run-evince ps-file)))\r
175 +\r
176 +(defun notmuch-print-message (msg)\r
177 +  "Print a message using the user-selected mechanism."\r
178 +  (funcall notmuch-print-mechanism msg))\r
179 +\r
180 +(provide 'notmuch-print)\r
181 diff --git a/emacs/notmuch-show.el b/emacs/notmuch-show.el\r
182 index 797f94b..db25565 100644\r
183 --- a/emacs/notmuch-show.el\r
184 +++ b/emacs/notmuch-show.el\r
185 @@ -34,6 +34,7 @@\r
186  (require 'notmuch-wash)\r
187  (require 'notmuch-mua)\r
188  (require 'notmuch-crypto)\r
189 +(require 'notmuch-print)\r
190  \r
191  (declare-function notmuch-call-notmuch-process "notmuch" (&rest args))\r
192  (declare-function notmuch-fontify-headers "notmuch" nil)\r
193 @@ -186,6 +187,52 @@ indentation."\r
194        mm-handle (> (notmuch-count-attachments mm-handle) 1))))\r
195    (message "Done"))\r
196  \r
197 +(defun notmuch-show-with-message-as-text (fn)\r
198 +  "Apply FN to a text representation of the current message.\r
199 +\r
200 +FN is called with one argument, the message properties. It should\r
201 +operation on the contents of the current buffer."\r
202 +\r
203 +  ;; Remake the header to ensure that all information is available.\r
204 +  (let* ((to (notmuch-show-get-to))\r
205 +        (cc (notmuch-show-get-cc))\r
206 +        (from (notmuch-show-get-from))\r
207 +        (subject (notmuch-show-get-subject))\r
208 +        (date (notmuch-show-get-date))\r
209 +        (tags (notmuch-show-get-tags))\r
210 +        (depth (notmuch-show-get-depth))\r
211 +\r
212 +        (header (concat\r
213 +                 "Subject: " subject "\n"\r
214 +                 "To: " to "\n"\r
215 +                 (if (not (string= cc ""))\r
216 +                     (concat "Cc: " cc "\n")\r
217 +                   "")\r
218 +                 "From: " from "\n"\r
219 +                 "Date: " date "\n"\r
220 +                 (if tags\r
221 +                     (concat "Tags: "\r
222 +                             (mapconcat #'identity tags ", ") "\n")\r
223 +                   "")))\r
224 +        (all (buffer-substring (notmuch-show-message-top)\r
225 +                               (notmuch-show-message-bottom)))\r
226 +\r
227 +        (props (notmuch-show-get-message-properties)))\r
228 +    (with-temp-buffer\r
229 +      (insert all)\r
230 +      (indent-rigidly (point-min) (point-max) (- depth))\r
231 +      ;; Remove the original header.\r
232 +      (goto-char (point-min))\r
233 +      (re-search-forward "^$" (point-max) nil)\r
234 +      (delete-region (point-min) (point))\r
235 +      (insert header)\r
236 +      (funcall fn props))))\r
237 +\r
238 +(defun notmuch-show-print-message ()\r
239 +  "Print the current message."\r
240 +  (interactive)\r
241 +  (notmuch-show-with-message-as-text 'notmuch-print-message))\r
242 +\r
243  (defun notmuch-show-fontify-header ()\r
244    (let ((face (cond\r
245                ((looking-at "[Tt]o:")\r
246 @@ -757,6 +804,8 @@ current buffer, if possible."\r
247        (overlay-put headers-overlay 'priority 10))\r
248      (overlay-put (make-overlay body-start body-end) 'invisible message-invis-spec)\r
249  \r
250 +    (plist-put msg :depth depth)\r
251 +\r
252      ;; Save the properties for this message. Currently this saves the\r
253      ;; entire message (augmented it with other stuff), which seems\r
254      ;; like overkill. We might save a reduced subset (for example, not\r
255 @@ -947,6 +996,7 @@ thread id.  If a prefix is given, crypto processing is toggled."\r
256         (define-key map " " 'notmuch-show-advance-and-archive)\r
257         (define-key map (kbd "M-RET") 'notmuch-show-open-or-close-all)\r
258         (define-key map (kbd "RET") 'notmuch-show-toggle-message)\r
259 +       (define-key map "#" 'notmuch-show-print-message)\r
260         map)\r
261        "Keymap for \"notmuch show\" buffers.")\r
262  (fset 'notmuch-show-mode-map notmuch-show-mode-map)\r
263 @@ -1107,6 +1157,9 @@ Some useful entries are:\r
264  (defun notmuch-show-get-to ()\r
265    (notmuch-show-get-header :To))\r
266  \r
267 +(defun notmuch-show-get-depth ()\r
268 +  (notmuch-show-get-prop :depth))\r
269 +\r
270  (defun notmuch-show-set-tags (tags)\r
271    "Set the tags of the current message."\r
272    (notmuch-show-set-prop :tags tags)\r
273 -- \r
274 1.7.7.3\r
275 \r