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 &optional hide))
30 (defgroup notmuch-wash nil
31 "Cleaning up messages for display."
34 (defvar notmuch-wash-signature-regexp
36 "Pattern to match a line that separates content from signature.")
38 (defvar notmuch-wash-citation-regexp
39 "\\(^[[:space:]]*>.*\n\\)+"
40 "Pattern to match citation lines.")
42 (defvar notmuch-wash-original-regexp "^\\(--+\s?[oO]riginal [mM]essage\s?--+\\)$"
43 "Pattern to match a line that separates original message from reply in top-posted message.")
45 (defvar notmuch-wash-button-signature-hidden-format
46 "[ %d-line signature. Click/Enter to show. ]"
47 "String used to construct button text for hidden signatures.
48 Can use up to one integer format parameter, i.e. %d")
50 (defvar notmuch-wash-button-signature-visible-format
51 "[ %d-line signature. Click/Enter to hide. ]"
52 "String used to construct button text for visible signatures.
53 Can use up to one integer format parameter, i.e. %d")
55 (defvar notmuch-wash-button-citation-hidden-format
56 "[ %d more citation lines. Click/Enter to show. ]"
57 "String used to construct button text for hidden citations.
58 Can use up to one integer format parameter, i.e. %d")
60 (defvar notmuch-wash-button-citation-visible-format
61 "[ %d more citation lines. Click/Enter to hide. ]"
62 "String used to construct button text for visible citations.
63 Can use up to one integer format parameter, i.e. %d")
65 (defvar notmuch-wash-button-original-hidden-format
66 "[ %d-line hidden original message. Click/Enter to show. ]"
67 "String used to construct button text for hidden citations.
68 Can use up to one integer format parameter, i.e. %d")
70 (defvar notmuch-wash-button-original-visible-format
71 "[ %d-line original message. Click/Enter to hide. ]"
72 "String used to construct button text for visible citations.
73 Can use up to one integer format parameter, i.e. %d")
75 (defvar notmuch-wash-signature-lines-max 12
76 "Maximum length of signature that will be hidden by default.")
78 (defvar notmuch-wash-citation-lines-prefix 3
79 "Always show at least this many lines from the start of a citation.
81 If there is one more line than the sum of
82 `notmuch-wash-citation-lines-prefix' and
83 `notmuch-wash-citation-lines-suffix', show that, otherwise
84 collapse the remaining lines into a button.")
86 (defvar notmuch-wash-citation-lines-suffix 3
87 "Always show at least this many lines from the end of a citation.
89 If there is one more line than the sum of
90 `notmuch-wash-citation-lines-prefix' and
91 `notmuch-wash-citation-lines-suffix', show that, otherwise
92 collapse the remaining lines into a button.")
94 (defvar notmuch-wash-wrap-lines-length nil
95 "Wrap line after at most this many characters.
97 If this is nil, lines in messages will be wrapped to fit in the
98 current window. If this is a number, lines will be wrapped after
99 this many characters or at the window width (whichever one is
102 (defface notmuch-wash-toggle-button
103 '((t (:inherit font-lock-comment-face)))
104 "Face used for buttons toggling the visibility of washed away
107 :group 'notmuch-faces)
109 (defface notmuch-wash-cited-text
110 '((t (:inherit message-cited-text)))
111 "Face used for cited text."
113 :group 'notmuch-faces)
115 (defun notmuch-wash-toggle-invisible-action (cite-button)
116 ;; Toggle overlay visibility
117 (let ((overlay (button-get cite-button 'overlay)))
118 (overlay-put overlay 'invisible (not (overlay-get overlay 'invisible))))
119 ;; Update button text
120 (let* ((new-start (button-start cite-button))
121 (overlay (button-get cite-button 'overlay))
122 (button-label (notmuch-wash-button-label overlay))
124 (properties (text-properties-at (point)))
125 (inhibit-read-only t))
126 (goto-char new-start)
127 (insert button-label)
128 (set-text-properties new-start (point) properties)
129 (let ((old-end (button-end cite-button)))
130 (move-overlay cite-button new-start (point))
131 (delete-region (point) old-end))
132 (goto-char (min old-point (1- (button-end cite-button))))))
134 (define-button-type 'notmuch-wash-button-invisibility-toggle-type
135 'action 'notmuch-wash-toggle-invisible-action
137 'face 'notmuch-wash-toggle-button
138 :supertype 'notmuch-button-type)
140 (define-button-type 'notmuch-wash-button-citation-toggle-type
141 'help-echo "mouse-1, RET: Show citation"
142 :supertype 'notmuch-wash-button-invisibility-toggle-type)
144 (define-button-type 'notmuch-wash-button-signature-toggle-type
145 'help-echo "mouse-1, RET: Show signature"
146 :supertype 'notmuch-wash-button-invisibility-toggle-type)
148 (define-button-type 'notmuch-wash-button-original-toggle-type
149 'help-echo "mouse-1, RET: Show original message"
150 :supertype 'notmuch-wash-button-invisibility-toggle-type)
152 (defun notmuch-wash-region-isearch-show (overlay)
153 (notmuch-wash-toggle-invisible-action
154 (overlay-get overlay 'notmuch-wash-button)))
156 (defun notmuch-wash-button-label (overlay)
157 (let* ((type (overlay-get overlay 'type))
158 (invis-spec (overlay-get overlay 'invisible))
159 (state (if (invisible-p invis-spec) "hidden" "visible"))
160 (label-format (symbol-value (intern-soft (concat "notmuch-wash-button-"
161 type "-" state "-format"))))
162 (lines-count (count-lines (overlay-start overlay) (overlay-end overlay))))
163 (format label-format lines-count)))
165 (defun notmuch-wash-region-to-button (msg beg end type &optional prefix)
166 "Auxiliary function to do the actual making of overlays and buttons
168 BEG and END are buffer locations. TYPE should a string, either
169 \"citation\" or \"signature\". Optional PREFIX is some arbitrary
170 text to insert before the button, probably for indentation. Note
171 that PREFIX should not include a newline."
173 ;; This uses some slightly tricky conversions between strings and
174 ;; symbols because of the way the button code works. Note that
175 ;; replacing intern-soft with make-symbol will cause this to fail,
176 ;; since the newly created symbol has no plist.
178 (let ((overlay (make-overlay beg end))
179 (button-type (intern-soft (concat "notmuch-wash-button-"
180 type "-toggle-type"))))
181 (overlay-put overlay 'invisible t)
182 (overlay-put overlay 'isearch-open-invisible #'notmuch-wash-region-isearch-show)
183 (overlay-put overlay 'type type)
188 (insert-before-markers prefix))
189 (let ((button-beg (point)))
190 (insert-before-markers (notmuch-wash-button-label overlay) "\n")
191 (let ((button (make-button button-beg (1- (point))
194 (overlay-put overlay 'notmuch-wash-button button))))))
196 (defun notmuch-wash-excerpt-citations (msg depth)
197 "Excerpt citations and up to one signature."
198 (goto-char (point-min))
200 (if (and (< (point) (point-max))
201 (re-search-forward notmuch-wash-original-regexp nil t))
202 (let* ((msg-start (match-beginning 0))
203 (msg-end (point-max))
204 (msg-lines (count-lines msg-start msg-end)))
205 (notmuch-wash-region-to-button
206 msg msg-start msg-end "original")))
207 (while (and (< (point) (point-max))
208 (re-search-forward notmuch-wash-citation-regexp nil t))
209 (let* ((cite-start (match-beginning 0))
210 (cite-end (match-end 0))
211 (cite-lines (count-lines cite-start cite-end)))
212 (overlay-put (make-overlay cite-start cite-end) 'face 'notmuch-wash-cited-text)
213 (when (> cite-lines (+ notmuch-wash-citation-lines-prefix
214 notmuch-wash-citation-lines-suffix
216 (goto-char cite-start)
217 (forward-line notmuch-wash-citation-lines-prefix)
218 (let ((hidden-start (point-marker)))
220 (forward-line (- notmuch-wash-citation-lines-suffix))
221 (notmuch-wash-region-to-button
222 msg hidden-start (point-marker)
224 (if (and (not (eobp))
225 (re-search-forward notmuch-wash-signature-regexp nil t))
226 (let* ((sig-start (match-beginning 0))
227 (sig-end (match-end 0))
228 (sig-lines (count-lines sig-start (point-max))))
229 (if (<= sig-lines notmuch-wash-signature-lines-max)
230 (let ((sig-start-marker (make-marker))
231 (sig-end-marker (make-marker)))
232 (set-marker sig-start-marker sig-start)
233 (set-marker sig-end-marker (point-max))
234 (overlay-put (make-overlay sig-start-marker sig-end-marker) 'face 'message-cited-text)
235 (notmuch-wash-region-to-button
236 msg sig-start-marker sig-end-marker
241 (defun notmuch-wash-elide-blank-lines (msg depth)
242 "Elide leading, trailing and successive blank lines."
244 ;; Algorithm derived from `article-strip-multiple-blank-lines' in
247 ;; Make all blank lines empty.
248 (goto-char (point-min))
249 (while (re-search-forward "^[[:space:]\t]+$" nil t)
250 (replace-match "" nil t))
252 ;; Replace multiple empty lines with a single empty line.
253 (goto-char (point-min))
254 (while (re-search-forward "^\n\\(\n+\\)" nil t)
255 (delete-region (match-beginning 1) (match-end 1)))
257 ;; Remove a leading blank line.
258 (goto-char (point-min))
259 (if (looking-at "\n")
260 (delete-region (match-beginning 0) (match-end 0)))
262 ;; Remove a trailing blank line.
263 (goto-char (point-max))
264 (if (looking-at "\n")
265 (delete-region (match-beginning 0) (match-end 0))))
269 (defun notmuch-wash-tidy-citations (msg depth)
270 "Improve the display of cited regions of a message.
272 Perform several transformations on the message body:
274 - Remove lines of repeated citation leaders with no other
276 - Remove citation leaders standing alone before a block of cited
278 - Remove citation trailers standing alone after a block of cited
281 ;; Remove lines of repeated citation leaders with no other content.
282 (goto-char (point-min))
283 (while (re-search-forward "\\(^>[> ]*\n\\)\\{2,\\}" nil t)
284 (replace-match "\\1"))
286 ;; Remove citation leaders standing alone before a block of cited
288 (goto-char (point-min))
289 (while (re-search-forward "\\(\n\\|^[^>].*\\)\n\\(^>[> ]*\n\\)" nil t)
290 (replace-match "\\1\n"))
292 ;; Remove citation trailers standing alone after a block of cited
294 (goto-char (point-min))
295 (while (re-search-forward "\\(^>[> ]*\n\\)\\(^$\\|^[^>].*\\)" nil t)
296 (replace-match "\\2")))
300 (defun notmuch-wash-wrap-long-lines (msg depth)
301 "Wrap long lines in the message.
303 If `notmuch-wash-wrap-lines-length' is a number, this will wrap
304 the message lines to the minimum of the width of the window or
305 its value. Otherwise, this function will wrap long lines in the
306 message at the window width. When doing so, citation leaders in
307 the wrapped text are maintained."
309 (let* ((coolj-wrap-follows-window-size nil)
310 (limit (if (numberp notmuch-wash-wrap-lines-length)
311 (min notmuch-wash-wrap-lines-length
314 (fill-column (- limit
316 ;; 2 to avoid poor interaction with
319 (coolj-wrap-region (point-min) (point-max))))
325 (defvar diff-file-header-re) ; From `diff-mode.el'.
327 (defun notmuch-wash-subject-to-filename (subject &optional maxlen)
328 "Convert a mail SUBJECT into a filename.
330 The resulting filename is similar to the names generated by \"git
331 format-patch\", without the leading patch sequence number
332 \"0001-\" and \".patch\" extension. Any leading \"[PREFIX]\"
333 style strings are removed prior to conversion.
335 Optional argument MAXLEN is the maximum length of the resulting
336 filename, before trimming any trailing . and - characters."
337 (let* ((s (replace-regexp-in-string "^ *\\(\\[[^]]*\\] *\\)*" "" subject))
338 (s (replace-regexp-in-string "[^A-Za-z0-9._]+" "-" s))
339 (s (replace-regexp-in-string "\\.+" "." s))
340 (s (if maxlen (substring s 0 (min (length s) maxlen)) s))
341 (s (replace-regexp-in-string "[.-]*$" "" s)))
344 (defun notmuch-wash-subject-to-patch-sequence-number (subject)
345 "Convert a patch mail SUBJECT into a patch sequence number.
347 Return the patch sequence number N from the last \"[PATCH N/M]\"
348 style prefix in SUBJECT, or nil if such a prefix can't be found."
350 "^ *\\(\\[[^]]*\\] *\\)*\\[[^]]*?\\([0-9]+\\)/[0-9]+[^]]*\\].*"
352 (string-to-number (substring subject (match-beginning 2) (match-end 2)))))
354 (defun notmuch-wash-subject-to-patch-filename (subject)
355 "Convert a patch mail SUBJECT into a filename.
357 The resulting filename is similar to the names generated by \"git
358 format-patch\". If the patch mail was generated and sent using
359 \"git format-patch/send-email\", this should re-create the
360 original filename the sender had."
361 (format "%04d-%s.patch"
362 (or (notmuch-wash-subject-to-patch-sequence-number subject) 1)
363 (notmuch-wash-subject-to-filename subject 52)))
365 (defun notmuch-wash-convert-inline-patch-to-part (msg depth)
366 "Convert an inline patch into a fake 'text/x-diff' attachment.
368 Given that this function guesses whether a buffer includes a
369 patch and then guesses the extent of the patch, there is scope
372 (goto-char (point-min))
373 (when (re-search-forward diff-file-header-re nil t)
374 (beginning-of-line -1)
375 (let ((patch-start (point))
376 (patch-end (point-max))
378 (goto-char patch-start)
380 ;; Patch ends with signature.
381 (re-search-forward notmuch-wash-signature-regexp nil t)
382 ;; Patch ends with bugtraq comment.
383 (re-search-forward "^\\*\\*\\* " nil t))
384 (setq patch-end (match-beginning 0)))
386 (narrow-to-region patch-start patch-end)
387 (setq part (plist-put part :content-type "inline patch"))
388 (setq part (plist-put part :content (buffer-string)))
389 (setq part (plist-put part :id -1))
390 (setq part (plist-put part :filename
391 (notmuch-wash-subject-to-patch-filename
393 (plist-get msg :headers) :Subject))))
394 (delete-region (point-min) (point-max))
395 (notmuch-show-insert-bodypart nil part depth)))))
399 (provide 'notmuch-wash)