1 ;; notmuch-wash.el --- cleaning up message bodies
3 ;; Copyright © Carl Worth
4 ;; Copyright © David Edmondson
6 ;; This file is part of Notmuch.
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.
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.
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/>.
21 ;; Authors: Carl Worth <cworth@cworth.org>
22 ;; David Edmondson <dme@dme.org>
26 (declare-function notmuch-show-insert-bodypart "notmuch-show" (msg part depth))
30 (defvar notmuch-wash-signature-regexp
32 "Pattern to match a line that separates content from signature.")
34 (defvar notmuch-wash-citation-regexp
35 "\\(^[[:space:]]*>.*\n\\)+"
36 "Pattern to match citation lines.")
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.")
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")
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")
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")
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")
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")
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")
71 (defvar notmuch-wash-signature-lines-max 12
72 "Maximum length of signature that will be hidden by default.")
74 (defvar notmuch-wash-citation-lines-prefix 3
75 "Always show at least this many lines from the start of a citation.
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.")
82 (defvar notmuch-wash-citation-lines-suffix 3
83 "Always show at least this many lines from the end of a citation.
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.")
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))
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)
109 (define-button-type 'notmuch-wash-button-invisibility-toggle-type
110 'action 'notmuch-wash-toggle-invisible-action
112 'face 'font-lock-comment-face)
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)
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)
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)
126 (defun notmuch-wash-region-isearch-show (overlay)
127 (dolist (invis-spec (overlay-get overlay 'invisible))
128 (remove-from-invisibility-spec invis-spec)))
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)))
139 (defun notmuch-wash-region-to-button (msg beg end type &optional prefix)
140 "Auxiliary function to do the actual making of overlays and buttons
142 BEG and END are buffer locations. TYPE should a string, either
143 \"citation\" or \"signature\". Optional PREFIX is some arbitrary
144 text to insert before the button, probably for indentation. Note
145 that PREFIX should not include a newline."
147 ;; This uses some slightly tricky conversions between strings and
148 ;; symbols because of the way the button code works. Note that
149 ;; replacing intern-soft with make-symbol will cause this to fail,
150 ;; since the newly created symbol has no plist.
152 (let ((overlay (make-overlay beg end))
153 (message-invis-spec (plist-get msg :message-invis-spec))
154 (invis-spec (make-symbol (concat "notmuch-" type "-region")))
155 (button-type (intern-soft (concat "notmuch-wash-button-"
156 type "-toggle-type"))))
157 (add-to-invisibility-spec invis-spec)
158 (overlay-put overlay 'invisible (list invis-spec message-invis-spec))
159 (overlay-put overlay 'isearch-open-invisible #'notmuch-wash-region-isearch-show)
160 (overlay-put overlay 'priority 10)
161 (overlay-put overlay 'type type)
166 (insert-before-markers prefix))
167 (let ((button-beg (point)))
168 (insert-before-markers (notmuch-wash-button-label overlay) "\n")
169 (make-button button-beg (1- (point))
170 'invisibility-spec invis-spec
172 :type button-type)))))
174 (defun notmuch-wash-excerpt-citations (msg depth)
175 "Excerpt citations and up to one signature."
176 (goto-char (point-min))
178 (if (and (< (point) (point-max))
179 (re-search-forward notmuch-wash-original-regexp nil t))
180 (let* ((msg-start (match-beginning 0))
181 (msg-end (point-max))
182 (msg-lines (count-lines msg-start msg-end)))
183 (notmuch-wash-region-to-button
184 msg msg-start msg-end "original")))
185 (while (and (< (point) (point-max))
186 (re-search-forward notmuch-wash-citation-regexp nil t))
187 (let* ((cite-start (match-beginning 0))
188 (cite-end (match-end 0))
189 (cite-lines (count-lines cite-start cite-end)))
190 (overlay-put (make-overlay cite-start cite-end) 'face 'message-cited-text)
191 (when (> cite-lines (+ notmuch-wash-citation-lines-prefix
192 notmuch-wash-citation-lines-suffix
194 (goto-char cite-start)
195 (forward-line notmuch-wash-citation-lines-prefix)
196 (let ((hidden-start (point-marker)))
198 (forward-line (- notmuch-wash-citation-lines-suffix))
199 (notmuch-wash-region-to-button
200 msg hidden-start (point-marker)
202 (if (and (not (eobp))
203 (re-search-forward notmuch-wash-signature-regexp nil t))
204 (let* ((sig-start (match-beginning 0))
205 (sig-end (match-end 0))
206 (sig-lines (count-lines sig-start (point-max))))
207 (if (<= sig-lines notmuch-wash-signature-lines-max)
208 (let ((sig-start-marker (make-marker))
209 (sig-end-marker (make-marker)))
210 (set-marker sig-start-marker sig-start)
211 (set-marker sig-end-marker (point-max))
212 (overlay-put (make-overlay sig-start-marker sig-end-marker) 'face 'message-cited-text)
213 (notmuch-wash-region-to-button
214 msg sig-start-marker sig-end-marker
219 (defun notmuch-wash-elide-blank-lines (msg depth)
220 "Elide leading, trailing and successive blank lines."
222 ;; Algorithm derived from `article-strip-multiple-blank-lines' in
225 ;; Make all blank lines empty.
226 (goto-char (point-min))
227 (while (re-search-forward "^[[:space:]\t]+$" nil t)
228 (replace-match "" nil t))
230 ;; Replace multiple empty lines with a single empty line.
231 (goto-char (point-min))
232 (while (re-search-forward "^\n\\(\n+\\)" nil t)
233 (delete-region (match-beginning 1) (match-end 1)))
235 ;; Remove a leading blank line.
236 (goto-char (point-min))
237 (if (looking-at "\n")
238 (delete-region (match-beginning 0) (match-end 0)))
240 ;; Remove a trailing blank line.
241 (goto-char (point-max))
242 (if (looking-at "\n")
243 (delete-region (match-beginning 0) (match-end 0))))
247 (defun notmuch-wash-tidy-citations (msg depth)
248 "Improve the display of cited regions of a message.
250 Perform several transformations on the message body:
252 - Remove lines of repeated citation leaders with no other
254 - Remove citation leaders standing alone before a block of cited
256 - Remove citation trailers standing alone after a block of cited
259 ;; Remove lines of repeated citation leaders with no other content.
260 (goto-char (point-min))
261 (while (re-search-forward "\\(^>[> ]*\n\\)\\{2,\\}" nil t)
262 (replace-match "\\1"))
264 ;; Remove citation leaders standing alone before a block of cited
266 (goto-char (point-min))
267 (while (re-search-forward "\\(\n\\|^[^>].*\\)\n\\(^>[> ]*\n\\)" nil t)
268 (replace-match "\\1\n"))
270 ;; Remove citation trailers standing alone after a block of cited
272 (goto-char (point-min))
273 (while (re-search-forward "\\(^>[> ]*\n\\)\\(^$\\|^[^>].*\\)" nil t)
274 (replace-match "\\2")))
278 (defun notmuch-wash-wrap-long-lines (msg depth)
279 "Wrap any long lines in the message to the width of the window.
281 When doing so, maintaining citation leaders in the wrapped text."
283 (let ((coolj-wrap-follows-window-size nil)
284 (fill-column (- (window-width)
286 ;; 2 to avoid poor interaction with
289 (coolj-wrap-region (point-min) (point-max))))
295 (defvar diff-file-header-re) ; From `diff-mode.el'.
297 (defun notmuch-wash-subject-to-filename (subject &optional maxlen)
298 "Convert a mail SUBJECT into a filename.
300 The resulting filename is similar to the names generated by \"git
301 format-patch\", without the leading patch sequence number
302 \"0001-\" and \".patch\" extension. Any leading \"[PREFIX]\"
303 style strings are removed prior to conversion.
305 Optional argument MAXLEN is the maximum length of the resulting
306 filename, before trimming any trailing . and - characters."
307 (let* ((s (replace-regexp-in-string "^ *\\(\\[[^]]*\\] *\\)*" "" subject))
308 (s (replace-regexp-in-string "[^A-Za-z0-9._]+" "-" s))
309 (s (replace-regexp-in-string "\\.+" "." s))
310 (s (if maxlen (substring s 0 (min (length s) maxlen)) s))
311 (s (replace-regexp-in-string "[.-]*$" "" s)))
314 (defun notmuch-wash-subject-to-patch-sequence-number (subject)
315 "Convert a patch mail SUBJECT into a patch sequence number.
317 Return the patch sequence number N from the last \"[PATCH N/M]\"
318 style prefix in SUBJECT, or nil if such a prefix can't be found."
320 "^ *\\(\\[[^]]*\\] *\\)*\\[[^]]*?\\([0-9]+\\)/[0-9]+[^]]*\\].*"
322 (string-to-number (substring subject (match-beginning 2) (match-end 2)))))
324 (defun notmuch-wash-subject-to-patch-filename (subject)
325 "Convert a patch mail SUBJECT into a filename.
327 The resulting filename is similar to the names generated by \"git
328 format-patch\". If the patch mail was generated and sent using
329 \"git format-patch/send-email\", this should re-create the
330 original filename the sender had."
331 (format "%04d-%s.patch"
332 (or (notmuch-wash-subject-to-patch-sequence-number subject) 1)
333 (notmuch-wash-subject-to-filename subject 52)))
335 (defun notmuch-wash-convert-inline-patch-to-part (msg depth)
336 "Convert an inline patch into a fake 'text/x-diff' attachment.
338 Given that this function guesses whether a buffer includes a
339 patch and then guesses the extent of the patch, there is scope
342 (goto-char (point-min))
343 (when (re-search-forward diff-file-header-re nil t)
344 (beginning-of-line -1)
345 (let ((patch-start (point))
346 (patch-end (point-max))
348 (goto-char patch-start)
350 ;; Patch ends with signature.
351 (re-search-forward notmuch-wash-signature-regexp nil t)
352 ;; Patch ends with bugtraq comment.
353 (re-search-forward "^\\*\\*\\* " nil t))
354 (setq patch-end (match-beginning 0)))
356 (narrow-to-region patch-start patch-end)
357 (setq part (plist-put part :content-type "inline-patch-fake-part"))
358 (setq part (plist-put part :content (buffer-string)))
359 (setq part (plist-put part :id -1))
360 (setq part (plist-put part :filename
361 (notmuch-wash-subject-to-patch-filename
363 (plist-get msg :headers) :Subject))))
364 (delete-region (point-min) (point-max))
365 (notmuch-show-insert-bodypart nil part depth)))))
369 ;; Temporary workaround for Emacs bug #8721
370 ;; http://debbugs.gnu.org/cgi/bugreport.cgi?bug=8721
372 (defun notmuch-isearch-range-invisible (beg end)
373 "Same as `isearch-range-invisible' but with fixed Emacs bug #8721."
375 ;; Check that invisibility runs up to END.
378 (let (;; can-be-opened keeps track if we can open some overlays.
379 (can-be-opened (eq search-invisible 'open))
380 ;; the list of overlays that could be opened
382 (when (and can-be-opened isearch-hide-immediately)
383 (isearch-close-unnecessary-overlays beg end))
384 ;; If the following character is currently invisible,
385 ;; skip all characters with that same `invisible' property value.
386 ;; Do that over and over.
387 (while (and (< (point) end) (invisible-p (point)))
388 (if (invisible-p (get-text-property (point) 'invisible))
390 (goto-char (next-single-property-change (point) 'invisible
392 ;; if text is hidden by an `invisible' text property
393 ;; we cannot open it at all.
394 (setq can-be-opened nil))
396 (let ((overlays (overlays-at (point)))
401 (setq o (car overlays)
402 invis-prop (overlay-get o 'invisible))
403 (if (invisible-p invis-prop)
404 (if (overlay-get o 'isearch-open-invisible)
405 (setq ov-list (cons o ov-list))
406 ;; We found one overlay that cannot be
407 ;; opened, that means the whole chunk
409 (setq can-be-opened nil)))
410 (setq overlays (cdr overlays)))
412 ;; It makes sense to append to the open
413 ;; overlays list only if we know that this is
415 (setq crt-overlays (append ov-list crt-overlays)))))
416 (goto-char (next-overlay-change (point)))))
417 ;; See if invisibility reaches up thru END.
419 (if (and can-be-opened (consp crt-overlays))
421 (setq isearch-opened-overlays
422 (append isearch-opened-overlays crt-overlays))
423 (mapc 'isearch-open-overlay-temporary crt-overlays)
425 (setq isearch-hidden t)))))))
427 (defadvice isearch-range-invisible (around notmuch-isearch-range-invisible-advice activate)
428 "Call `notmuch-isearch-range-invisible' instead of the original
429 `isearch-range-invisible' when in `notmuch-show-mode' mode."
430 (if (eq major-mode 'notmuch-show-mode)
431 (setq ad-return-value (notmuch-isearch-range-invisible beg end))
436 (provide 'notmuch-wash)