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