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