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