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