]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-wash.el
Merge branch 'release'
[notmuch] / emacs / notmuch-wash.el
1 ;;; notmuch-wash.el --- cleaning up message bodies
2 ;;
3 ;; Copyright © Carl Worth
4 ;; Copyright © David Edmondson
5 ;;
6 ;; This file is part of Notmuch.
7 ;;
8 ;; Notmuch is free software: you can redistribute it and/or modify it
9 ;; under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12 ;;
13 ;; Notmuch is distributed in the hope that it will be useful, but
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 ;; General Public License for more details.
17 ;;
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with Notmuch.  If not, see <https://www.gnu.org/licenses/>.
20 ;;
21 ;; Authors: Carl Worth <cworth@cworth.org>
22 ;;          David Edmondson <dme@dme.org>
23
24 ;;; Code:
25
26 (require 'coolj)
27
28 (declare-function notmuch-show-insert-bodypart "notmuch-show" (msg part depth &optional hide))
29 (defvar notmuch-show-indent-messages-width)
30
31 ;;
32
33 (defgroup notmuch-wash nil
34   "Cleaning up messages for display."
35   :group 'notmuch)
36
37 (defcustom notmuch-wash-signature-regexp "^\\(-- ?\\|_+\\)$"
38   "Pattern to match a line that separates content from signature."
39   :type 'regexp
40   :group 'notmuch-wash)
41
42 (defcustom notmuch-wash-citation-regexp "\\(^[[:space:]]*>.*\n\\)+"
43   "Pattern to match citation lines."
44   :type 'regexp
45   :group 'notmuch-wash)
46
47 (defcustom notmuch-wash-original-regexp "^\\(--+\s?[oO]riginal [mM]essage\s?--+\\)$"
48   "Pattern to match a line that separates original message from
49 reply in top-posted message."
50   :type 'regexp
51   :group 'notmuch-wash)
52
53 (defcustom notmuch-wash-button-signature-hidden-format
54   "[ %d-line signature. Click/Enter to show. ]"
55   "String used to construct button text for hidden signatures.
56 Can use up to one integer format parameter, i.e. %d."
57   :type 'string
58   :group 'notmuch-wash)
59
60 (defcustom notmuch-wash-button-signature-visible-format
61   "[ %d-line signature. Click/Enter to hide. ]"
62   "String used to construct button text for visible signatures.
63 Can use up to one integer format parameter, i.e. %d."
64   :type 'string
65   :group 'notmuch-wash)
66
67 (defcustom notmuch-wash-button-citation-hidden-format
68   "[ %d more citation lines. Click/Enter to show. ]"
69   "String used to construct button text for hidden citations.
70 Can use up to one integer format parameter, i.e. %d."
71   :type 'string
72   :group 'notmuch-wash)
73
74 (defcustom notmuch-wash-button-citation-visible-format
75   "[ %d more citation lines. Click/Enter to hide. ]"
76   "String used to construct button text for visible citations.
77 Can use up to one integer format parameter, i.e. %d."
78   :type 'string
79   :group 'notmuch-wash)
80
81 (defcustom notmuch-wash-button-original-hidden-format
82   "[ %d-line hidden original message. Click/Enter to show. ]"
83   "String used to construct button text for hidden citations.
84 Can use up to one integer format parameter, i.e. %d."
85   :type 'string
86   :group 'notmuch-wash)
87
88 (defcustom notmuch-wash-button-original-visible-format
89   "[ %d-line original message. Click/Enter to hide. ]"
90   "String used to construct button text for visible citations.
91 Can use up to one integer format parameter, i.e. %d."
92   :type 'string
93   :group 'notmuch-wash)
94
95 (defcustom notmuch-wash-signature-lines-max 12
96   "Maximum length of signature that will be hidden by default."
97   :type 'integer
98   :group 'notmuch-wash)
99
100 (defcustom notmuch-wash-citation-lines-prefix 3
101   "Always show at least this many lines from the start of a citation.
102
103 If there is one more line than the sum of
104 `notmuch-wash-citation-lines-prefix' and
105 `notmuch-wash-citation-lines-suffix', show that, otherwise
106 collapse the remaining lines into a button."
107   :type 'integer
108   :group 'notmuch-wash)
109
110 (defcustom notmuch-wash-citation-lines-suffix 3
111   "Always show at least this many lines from the end of a citation.
112
113 If there is one more line than the sum of
114 `notmuch-wash-citation-lines-prefix' and
115 `notmuch-wash-citation-lines-suffix', show that, otherwise
116 collapse the remaining lines into a button."
117   :type 'integer
118   :group 'notmuch-wash)
119
120 (defcustom notmuch-wash-wrap-lines-length nil
121   "Wrap line after at most this many characters.
122
123 If this is nil, lines in messages will be wrapped to fit in the
124 current window. If this is a number, lines will be wrapped after
125 this many characters (ignoring indentation due to thread depth)
126 or at the window width (whichever one is lower)."
127   :type '(choice (const :tag "window width" nil)
128                  (integer :tag "number of characters"))
129   :group 'notmuch-wash)
130
131 (defface notmuch-wash-toggle-button
132   '((t (:inherit font-lock-comment-face)))
133   "Face used for buttons toggling the visibility of washed away
134 message parts."
135   :group 'notmuch-wash
136   :group 'notmuch-faces)
137
138 (defface notmuch-wash-cited-text
139   '((t (:inherit message-cited-text)))
140   "Face used for cited text."
141   :group 'notmuch-wash
142   :group 'notmuch-faces)
143
144 (defun notmuch-wash-toggle-invisible-action (cite-button)
145   ;; Toggle overlay visibility
146   (let ((overlay (button-get cite-button 'overlay)))
147     (overlay-put overlay 'invisible (not (overlay-get overlay 'invisible))))
148   ;; Update button text
149   (let* ((new-start (button-start cite-button))
150          (overlay (button-get cite-button 'overlay))
151          (button-label (notmuch-wash-button-label overlay))
152          (old-point (point))
153          (properties (text-properties-at (point)))
154          (inhibit-read-only t))
155     (goto-char new-start)
156     (insert button-label)
157     (set-text-properties new-start (point) properties)
158     (let ((old-end (button-end cite-button)))
159       (move-overlay cite-button new-start (point))
160       (delete-region (point) old-end))
161     (goto-char (min old-point (1- (button-end cite-button))))))
162
163 (define-button-type 'notmuch-wash-button-invisibility-toggle-type
164   'action 'notmuch-wash-toggle-invisible-action
165   'follow-link t
166   'face 'notmuch-wash-toggle-button
167   :supertype 'notmuch-button-type)
168
169 (define-button-type 'notmuch-wash-button-citation-toggle-type
170   'help-echo "mouse-1, RET: Show citation"
171   :supertype 'notmuch-wash-button-invisibility-toggle-type)
172
173 (define-button-type 'notmuch-wash-button-signature-toggle-type
174   'help-echo "mouse-1, RET: Show signature"
175   :supertype 'notmuch-wash-button-invisibility-toggle-type)
176
177 (define-button-type 'notmuch-wash-button-original-toggle-type
178   'help-echo "mouse-1, RET: Show original message"
179   :supertype 'notmuch-wash-button-invisibility-toggle-type)
180
181 (defun notmuch-wash-region-isearch-show (overlay)
182   (notmuch-wash-toggle-invisible-action
183    (overlay-get overlay 'notmuch-wash-button)))
184
185 (defun notmuch-wash-button-label (overlay)
186   (let* ((type (overlay-get overlay 'type))
187          (invis-spec (overlay-get overlay 'invisible))
188          (state (if (invisible-p invis-spec) "hidden" "visible"))
189          (label-format (symbol-value (intern-soft (concat "notmuch-wash-button-"
190                                                           type "-" state "-format"))))
191          (lines-count (count-lines (overlay-start overlay) (overlay-end overlay))))
192     (format label-format lines-count)))
193
194 (defun notmuch-wash-region-to-button (msg beg end type &optional prefix)
195   "Auxiliary function to do the actual making of overlays and buttons
196
197 BEG and END are buffer locations. TYPE should a string, either
198 \"citation\" or \"signature\". Optional PREFIX is some arbitrary
199 text to insert before the button, probably for indentation.  Note
200 that PREFIX should not include a newline."
201
202   ;; This uses some slightly tricky conversions between strings and
203   ;; symbols because of the way the button code works. Note that
204   ;; replacing intern-soft with make-symbol will cause this to fail,
205   ;; since the newly created symbol has no plist.
206
207   (let ((overlay (make-overlay beg end))
208         (button-type (intern-soft (concat "notmuch-wash-button-"
209                                           type "-toggle-type"))))
210     (overlay-put overlay 'invisible t)
211     (overlay-put overlay 'isearch-open-invisible #'notmuch-wash-region-isearch-show)
212     (overlay-put overlay 'type type)
213     (goto-char (1+ end))
214     (save-excursion
215       (goto-char beg)
216       (if prefix
217           (insert-before-markers prefix))
218       (let ((button-beg (point)))
219         (insert-before-markers (notmuch-wash-button-label overlay) "\n")
220         (let ((button (make-button button-beg (1- (point))
221                                    'overlay overlay
222                                    :type button-type)))
223           (overlay-put overlay 'notmuch-wash-button button))))))
224
225 (defun notmuch-wash-excerpt-citations (msg depth)
226   "Excerpt citations and up to one signature."
227   (goto-char (point-min))
228   (beginning-of-line)
229   (if (and (< (point) (point-max))
230            (re-search-forward notmuch-wash-original-regexp nil t))
231       (let* ((msg-start (match-beginning 0))
232              (msg-end (point-max))
233              (msg-lines (count-lines msg-start msg-end)))
234         (notmuch-wash-region-to-button
235          msg msg-start msg-end "original")))
236   (while (and (< (point) (point-max))
237               (re-search-forward notmuch-wash-citation-regexp nil t))
238     (let* ((cite-start (match-beginning 0))
239            (cite-end (match-end 0))
240            (cite-lines (count-lines cite-start cite-end)))
241       (overlay-put (make-overlay cite-start cite-end) 'face 'notmuch-wash-cited-text)
242       (when (> cite-lines (+ notmuch-wash-citation-lines-prefix
243                              notmuch-wash-citation-lines-suffix
244                              1))
245         (goto-char cite-start)
246         (forward-line notmuch-wash-citation-lines-prefix)
247         (let ((hidden-start (point-marker)))
248           (goto-char cite-end)
249           (forward-line (- notmuch-wash-citation-lines-suffix))
250           (notmuch-wash-region-to-button
251            msg hidden-start (point-marker)
252            "citation")))))
253   (if (and (not (eobp))
254            (re-search-forward notmuch-wash-signature-regexp nil t))
255       (let* ((sig-start (match-beginning 0))
256              (sig-end (match-end 0))
257              (sig-lines (count-lines sig-start (point-max))))
258         (if (<= sig-lines notmuch-wash-signature-lines-max)
259             (let ((sig-start-marker (make-marker))
260                   (sig-end-marker (make-marker)))
261               (set-marker sig-start-marker sig-start)
262               (set-marker sig-end-marker (point-max))
263               (overlay-put (make-overlay sig-start-marker sig-end-marker) 'face 'message-cited-text)
264               (notmuch-wash-region-to-button
265                msg sig-start-marker sig-end-marker
266                "signature"))))))
267
268 ;;
269
270 (defun notmuch-wash-elide-blank-lines (msg depth)
271   "Elide leading, trailing and successive blank lines."
272
273   ;; Algorithm derived from `article-strip-multiple-blank-lines' in
274   ;; `gnus-art.el'.
275
276   ;; Make all blank lines empty.
277   (goto-char (point-min))
278   (while (re-search-forward "^[[:space:]\t]+$" nil t)
279     (replace-match "" nil t))
280
281   ;; Replace multiple empty lines with a single empty line.
282   (goto-char (point-min))
283   (while (re-search-forward "^\n\\(\n+\\)" nil t)
284     (delete-region (match-beginning 1) (match-end 1)))
285
286   ;; Remove a leading blank line.
287   (goto-char (point-min))
288   (if (looking-at "\n")
289       (delete-region (match-beginning 0) (match-end 0)))
290
291   ;; Remove a trailing blank line.
292   (goto-char (point-max))
293   (if (looking-at "\n")
294       (delete-region (match-beginning 0) (match-end 0))))
295
296 ;;
297
298 (defun notmuch-wash-tidy-citations (msg depth)
299   "Improve the display of cited regions of a message.
300
301 Perform several transformations on the message body:
302
303 - Remove lines of repeated citation leaders with no other
304   content,
305 - Remove citation leaders standing alone before a block of cited
306   text,
307 - Remove citation trailers standing alone after a block of cited
308   text."
309
310   ;; Remove lines of repeated citation leaders with no other content.
311   (goto-char (point-min))
312   (while (re-search-forward "\\(^>[> ]*\n\\)\\{2,\\}" nil t)
313     (replace-match "\\1"))
314
315   ;; Remove citation leaders standing alone before a block of cited
316   ;; text.
317   (goto-char (point-min))
318   (while (re-search-forward "\\(\n\\|^[^>].*\\)\n\\(^>[> ]*\n\\)" nil t)
319     (replace-match "\\1\n"))
320
321   ;; Remove citation trailers standing alone after a block of cited
322   ;; text.
323   (goto-char (point-min))
324   (while (re-search-forward "\\(^>[> ]*\n\\)\\(^$\\|^[^>].*\\)" nil t)
325     (replace-match "\\2")))
326
327 ;;
328
329 (defun notmuch-wash-wrap-long-lines (msg depth)
330   "Wrap long lines in the message.
331
332 If `notmuch-wash-wrap-lines-length' is a number, this will wrap
333 the message lines to the minimum of the width of the window or
334 its value. Otherwise, this function will wrap long lines in the
335 message at the window width. When doing so, citation leaders in
336 the wrapped text are maintained."
337
338   (let* ((coolj-wrap-follows-window-size nil)
339          (indent (* depth notmuch-show-indent-messages-width))
340          (limit (if (numberp notmuch-wash-wrap-lines-length)
341                     (min (+ notmuch-wash-wrap-lines-length indent)
342                          (window-width))
343                   (window-width)))
344          (fill-column (- limit
345                          indent
346                          ;; 2 to avoid poor interaction with
347                          ;; `word-wrap'.
348                          2)))
349     (coolj-wrap-region (point-min) (point-max))))
350
351 ;;
352
353 (require 'diff-mode)
354
355 (defvar diff-file-header-re) ; From `diff-mode.el'.
356
357 (defun notmuch-wash-subject-to-filename (subject &optional maxlen)
358   "Convert a mail SUBJECT into a filename.
359
360 The resulting filename is similar to the names generated by \"git
361 format-patch\", without the leading patch sequence number
362 \"0001-\" and \".patch\" extension. Any leading \"[PREFIX]\"
363 style strings are removed prior to conversion.
364
365 Optional argument MAXLEN is the maximum length of the resulting
366 filename, before trimming any trailing . and - characters."
367   (let* ((s (replace-regexp-in-string "^ *\\(\\[[^]]*\\] *\\)*" "" subject))
368          (s (replace-regexp-in-string "[^A-Za-z0-9._]+" "-" s))
369          (s (replace-regexp-in-string "\\.+" "." s))
370          (s (if maxlen (substring s 0 (min (length s) maxlen)) s))
371          (s (replace-regexp-in-string "[.-]*$" "" s)))
372     s))
373
374 (defun notmuch-wash-subject-to-patch-sequence-number (subject)
375   "Convert a patch mail SUBJECT into a patch sequence number.
376
377 Return the patch sequence number N from the last \"[PATCH N/M]\"
378 style prefix in SUBJECT, or nil if such a prefix can't be found."
379   (when (string-match
380          "^ *\\(\\[[^]]*\\] *\\)*\\[[^]]*?\\([0-9]+\\)/[0-9]+[^]]*\\].*"
381          subject)
382       (string-to-number (substring subject (match-beginning 2) (match-end 2)))))
383
384 (defun notmuch-wash-subject-to-patch-filename (subject)
385   "Convert a patch mail SUBJECT into a filename.
386
387 The resulting filename is similar to the names generated by \"git
388 format-patch\". If the patch mail was generated and sent using
389 \"git format-patch/send-email\", this should re-create the
390 original filename the sender had."
391   (format "%04d-%s.patch"
392           (or (notmuch-wash-subject-to-patch-sequence-number subject) 1)
393           (notmuch-wash-subject-to-filename subject 52)))
394
395 (defun notmuch-wash-convert-inline-patch-to-part (msg depth)
396   "Convert an inline patch into a fake 'text/x-diff' attachment.
397
398 Given that this function guesses whether a buffer includes a
399 patch and then guesses the extent of the patch, there is scope
400 for error."
401
402   (goto-char (point-min))
403   (when (re-search-forward diff-file-header-re nil t)
404     (beginning-of-line -1)
405     (let ((patch-start (point))
406           (patch-end (point-max))
407           part)
408       (goto-char patch-start)
409       (if (or
410            ;; Patch ends with signature.
411            (re-search-forward notmuch-wash-signature-regexp nil t)
412            ;; Patch ends with bugtraq comment.
413            (re-search-forward "^\\*\\*\\* " nil t))
414           (setq patch-end (match-beginning 0)))
415       (save-restriction
416         (narrow-to-region patch-start patch-end)
417         (setq part (plist-put part :content-type "inline patch"))
418         (setq part (plist-put part :content (buffer-string)))
419         (setq part (plist-put part :id -1))
420         (setq part (plist-put part :filename
421                               (notmuch-wash-subject-to-patch-filename
422                                (plist-get
423                                 (plist-get msg :headers) :Subject))))
424         (delete-region (point-min) (point-max))
425         (notmuch-show-insert-bodypart nil part depth)))))
426
427 ;;
428
429 (provide 'notmuch-wash)
430
431 ;;; notmuch-wash.el ends here