]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-wash.el
1f420b25a05d770556cd19e4e737f522b7e18098
[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 (defun notmuch-wash-toggle-invisible-action (cite-button)
91   (let ((invis-spec (button-get cite-button 'invisibility-spec)))
92     (if (invisible-p invis-spec)
93         (remove-from-invisibility-spec invis-spec)
94       (add-to-invisibility-spec invis-spec)))
95   (let* ((new-start (button-start cite-button))
96          (overlay (button-get cite-button 'overlay))
97          (button-label (notmuch-wash-button-label overlay))
98          (old-point (point))
99          (inhibit-read-only t))
100     (goto-char new-start)
101     (insert button-label)
102     (let ((old-end (button-end cite-button)))
103       (move-overlay cite-button new-start (point))
104       (delete-region (point) old-end))
105     (goto-char (min old-point (1- (button-end cite-button)))))
106   (force-window-update)
107   (redisplay t))
108
109 (define-button-type 'notmuch-wash-button-invisibility-toggle-type
110   'action 'notmuch-wash-toggle-invisible-action
111   'follow-link t
112   'face 'font-lock-comment-face)
113
114 (define-button-type 'notmuch-wash-button-citation-toggle-type
115   'help-echo "mouse-1, RET: Show citation"
116   :supertype 'notmuch-wash-button-invisibility-toggle-type)
117
118 (define-button-type 'notmuch-wash-button-signature-toggle-type
119   'help-echo "mouse-1, RET: Show signature"
120   :supertype 'notmuch-wash-button-invisibility-toggle-type)
121
122 (define-button-type 'notmuch-wash-button-original-toggle-type
123   'help-echo "mouse-1, RET: Show original message"
124   :supertype 'notmuch-wash-button-invisibility-toggle-type)
125
126 (defun notmuch-wash-region-isearch-show (overlay)
127   (dolist (invis-spec (overlay-get overlay 'invisible))
128     (remove-from-invisibility-spec invis-spec)))
129
130 (defun notmuch-wash-button-label (overlay)
131   (let* ((type (overlay-get overlay 'type))
132          (invis-spec (overlay-get overlay 'invisible))
133          (state (if (invisible-p invis-spec) "hidden" "visible"))
134          (label-format (symbol-value (intern-soft (concat "notmuch-wash-button-"
135                                                           type "-" state "-format"))))
136          (lines-count (count-lines (overlay-start overlay) (overlay-end overlay))))
137     (format label-format lines-count)))
138
139 (defun notmuch-wash-region-to-button (msg beg end type prefix)
140   "Auxiliary function to do the actual making of overlays and buttons
141
142 BEG and END are buffer locations. TYPE should a string, either
143 \"citation\" or \"signature\". PREFIX is some arbitrary text to
144 insert before the button, probably for indentation."
145
146   ;; This uses some slightly tricky conversions between strings and
147   ;; symbols because of the way the button code works. Note that
148   ;; replacing intern-soft with make-symbol will cause this to fail,
149   ;; since the newly created symbol has no plist.
150
151   (let ((overlay (make-overlay beg end))
152         (message-invis-spec (plist-get msg :message-invis-spec))
153         (invis-spec (make-symbol (concat "notmuch-" type "-region")))
154         (button-type (intern-soft (concat "notmuch-wash-button-"
155                                           type "-toggle-type"))))
156     (add-to-invisibility-spec invis-spec)
157     (overlay-put overlay 'invisible (list invis-spec message-invis-spec))
158     (overlay-put overlay 'isearch-open-invisible #'notmuch-wash-region-isearch-show)
159     (overlay-put overlay 'priority 10)
160     (overlay-put overlay 'type type)
161     (goto-char (1+ end))
162     (save-excursion
163       (goto-char (1- beg))
164       (insert prefix)
165       (insert-button (notmuch-wash-button-label overlay)
166                      'invisibility-spec invis-spec
167                      'overlay overlay
168                      :type button-type))))
169
170 (defun notmuch-wash-excerpt-citations (msg depth)
171   "Excerpt citations and up to one signature."
172   (goto-char (point-min))
173   (beginning-of-line)
174   (if (and (< (point) (point-max))
175            (re-search-forward notmuch-wash-original-regexp nil t))
176       (let* ((msg-start (match-beginning 0))
177              (msg-end (point-max))
178              (msg-lines (count-lines msg-start msg-end)))
179         (notmuch-wash-region-to-button
180          msg msg-start msg-end "original" "\n")))
181   (while (and (< (point) (point-max))
182               (re-search-forward notmuch-wash-citation-regexp nil t))
183     (let* ((cite-start (match-beginning 0))
184            (cite-end (match-end 0))
185            (cite-lines (count-lines cite-start cite-end)))
186       (overlay-put (make-overlay cite-start cite-end) 'face 'message-cited-text)
187       (when (> cite-lines (+ notmuch-wash-citation-lines-prefix
188                              notmuch-wash-citation-lines-suffix
189                              1))
190         (goto-char cite-start)
191         (forward-line notmuch-wash-citation-lines-prefix)
192         (let ((hidden-start (point-marker)))
193           (goto-char cite-end)
194           (forward-line (- notmuch-wash-citation-lines-suffix))
195           (notmuch-wash-region-to-button
196            msg hidden-start (point-marker)
197            "citation" "\n")))))
198   (if (and (not (eobp))
199            (re-search-forward notmuch-wash-signature-regexp nil t))
200       (let* ((sig-start (match-beginning 0))
201              (sig-end (match-end 0))
202              (sig-lines (count-lines sig-start (point-max))))
203         (if (<= sig-lines notmuch-wash-signature-lines-max)
204             (let ((sig-start-marker (make-marker))
205                   (sig-end-marker (make-marker)))
206               (set-marker sig-start-marker sig-start)
207               (set-marker sig-end-marker (point-max))
208               (overlay-put (make-overlay sig-start-marker sig-end-marker) 'face 'message-cited-text)
209               (notmuch-wash-region-to-button
210                msg sig-start-marker sig-end-marker
211                "signature" "\n"))))))
212
213 ;;
214
215 (defun notmuch-wash-elide-blank-lines (msg depth)
216   "Elide leading, trailing and successive blank lines."
217
218   ;; Algorithm derived from `article-strip-multiple-blank-lines' in
219   ;; `gnus-art.el'.
220
221   ;; Make all blank lines empty.
222   (goto-char (point-min))
223   (while (re-search-forward "^[[:space:]\t]+$" nil t)
224     (replace-match "" nil t))
225
226   ;; Replace multiple empty lines with a single empty line.
227   (goto-char (point-min))
228   (while (re-search-forward "^\n\\(\n+\\)" nil t)
229     (delete-region (match-beginning 1) (match-end 1)))
230
231   ;; Remove a leading blank line.
232   (goto-char (point-min))
233   (if (looking-at "\n")
234       (delete-region (match-beginning 0) (match-end 0)))
235
236   ;; Remove a trailing blank line.
237   (goto-char (point-max))
238   (if (looking-at "\n")
239       (delete-region (match-beginning 0) (match-end 0))))
240
241 ;;
242
243 (defun notmuch-wash-tidy-citations (msg depth)
244   "Improve the display of cited regions of a message.
245
246 Perform several transformations on the message body:
247
248 - Remove lines of repeated citation leaders with no other
249   content,
250 - Remove citation leaders standing alone before a block of cited
251   text,
252 - Remove citation trailers standing alone after a block of cited
253   text."
254
255   ;; Remove lines of repeated citation leaders with no other content.
256   (goto-char (point-min))
257   (while (re-search-forward "\\(^>[> ]*\n\\)\\{2,\\}" nil t)
258     (replace-match "\\1"))
259
260   ;; Remove citation leaders standing alone before a block of cited
261   ;; text.
262   (goto-char (point-min))
263   (while (re-search-forward "\\(\n\\|^[^>].*\\)\n\\(^>[> ]*\n\\)" nil t)
264     (replace-match "\\1\n"))
265
266   ;; Remove citation trailers standing alone after a block of cited
267   ;; text.
268   (goto-char (point-min))
269   (while (re-search-forward "\\(^>[> ]*\n\\)\\(^$\\|^[^>].*\\)" nil t)
270     (replace-match "\\2")))
271
272 ;;
273
274 (defun notmuch-wash-wrap-long-lines (msg depth)
275   "Wrap any long lines in the message to the width of the window.
276
277 When doing so, maintaining citation leaders in the wrapped text."
278
279   (let ((coolj-wrap-follows-window-size nil)
280         (fill-column (- (window-width)
281                         depth
282                         ;; 2 to avoid poor interaction with
283                         ;; `word-wrap'.
284                         2)))
285     (coolj-wrap-region (point-min) (point-max))))
286
287 ;;
288
289 (require 'diff-mode)
290
291 (defvar diff-file-header-re) ; From `diff-mode.el'.
292
293 (defun notmuch-wash-convert-inline-patch-to-part (msg depth)
294   "Convert an inline patch into a fake 'text/x-diff' attachment.
295
296 Given that this function guesses whether a buffer includes a
297 patch and then guesses the extent of the patch, there is scope
298 for error."
299
300   (goto-char (point-min))
301   (if (re-search-forward diff-file-header-re nil t)
302       (progn
303         (beginning-of-line -1)
304         (let ((patch-start (point))
305               (patch-end (point-max))
306               part)
307           (goto-char patch-start)
308           (if (or
309                ;; Patch ends with signature.
310                (re-search-forward notmuch-wash-signature-regexp nil t)
311                ;; Patch ends with bugtraq comment.
312                (re-search-forward "^\\*\\*\\* " nil t))
313               (setq patch-end (match-beginning 0)))
314           (save-restriction
315             (narrow-to-region patch-start patch-end)
316             (setq part (plist-put part :content-type "text/x-diff"))
317             (setq part (plist-put part :content (buffer-string)))
318             (setq part (plist-put part :id -1))
319             (setq part (plist-put part :filename "inline patch"))
320             (delete-region (point-min) (point-max))
321             (notmuch-show-insert-bodypart nil part depth))))))
322
323 ;;
324
325 ;; Temporary workaround for Emacs bug #8721
326 ;; http://debbugs.gnu.org/cgi/bugreport.cgi?bug=8721
327
328 (defun notmuch-isearch-range-invisible (beg end)
329   "Same as `isearch-range-invisible' but with fixed Emacs bug #8721."
330   (when (/= beg end)
331     ;; Check that invisibility runs up to END.
332     (save-excursion
333       (goto-char beg)
334       (let (;; can-be-opened keeps track if we can open some overlays.
335             (can-be-opened (eq search-invisible 'open))
336             ;; the list of overlays that could be opened
337             (crt-overlays nil))
338         (when (and can-be-opened isearch-hide-immediately)
339           (isearch-close-unnecessary-overlays beg end))
340         ;; If the following character is currently invisible,
341         ;; skip all characters with that same `invisible' property value.
342         ;; Do that over and over.
343         (while (and (< (point) end) (invisible-p (point)))
344           (if (invisible-p (get-text-property (point) 'invisible))
345               (progn
346                 (goto-char (next-single-property-change (point) 'invisible
347                                                         nil end))
348                 ;; if text is hidden by an `invisible' text property
349                 ;; we cannot open it at all.
350                 (setq can-be-opened nil))
351             (when can-be-opened
352               (let ((overlays (overlays-at (point)))
353                     ov-list
354                     o
355                     invis-prop)
356                 (while overlays
357                   (setq o (car overlays)
358                         invis-prop (overlay-get o 'invisible))
359                   (if (invisible-p invis-prop)
360                       (if (overlay-get o 'isearch-open-invisible)
361                           (setq ov-list (cons o ov-list))
362                         ;; We found one overlay that cannot be
363                         ;; opened, that means the whole chunk
364                         ;; cannot be opened.
365                         (setq can-be-opened nil)))
366                   (setq overlays (cdr overlays)))
367                 (if can-be-opened
368                     ;; It makes sense to append to the open
369                     ;; overlays list only if we know that this is
370                     ;; t.
371                     (setq crt-overlays (append ov-list crt-overlays)))))
372             (goto-char (next-overlay-change (point)))))
373         ;; See if invisibility reaches up thru END.
374         (if (>= (point) end)
375             (if (and can-be-opened (consp crt-overlays))
376                 (progn
377                   (setq isearch-opened-overlays
378                         (append isearch-opened-overlays crt-overlays))
379                   (mapc 'isearch-open-overlay-temporary crt-overlays)
380                   nil)
381               (setq isearch-hidden t)))))))
382
383 (defadvice isearch-range-invisible (around notmuch-isearch-range-invisible-advice activate)
384   "Call `notmuch-isearch-range-invisible' instead of the original
385 `isearch-range-invisible' when in `notmuch-show-mode' mode."
386   (if (eq major-mode 'notmuch-show-mode)
387       (setq ad-return-value (notmuch-isearch-range-invisible beg end))
388     ad-do-it))
389
390 ;;
391
392 (provide 'notmuch-wash)