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>
28 (declare-function notmuch-show-insert-bodypart "notmuch-show" (msg part depth &optional hide))
32 (defgroup notmuch-wash nil
33 "Cleaning up messages for display."
36 (defcustom notmuch-wash-signature-regexp "^\\(-- ?\\|_+\\)$"
37 "Pattern to match a line that separates content from signature."
41 (defcustom notmuch-wash-citation-regexp "\\(^[[:space:]]*>.*\n\\)+"
42 "Pattern to match citation lines."
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."
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."
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."
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."
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."
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."
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."
94 (defcustom notmuch-wash-signature-lines-max 12
95 "Maximum length of signature that will be hidden by default."
99 (defcustom notmuch-wash-citation-lines-prefix 3
100 "Always show at least this many lines from the start of a citation.
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."
107 :group 'notmuch-wash)
109 (defcustom notmuch-wash-citation-lines-suffix 3
110 "Always show at least this many lines from the end of a citation.
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."
117 :group 'notmuch-wash)
119 (defcustom notmuch-wash-wrap-lines-length nil
120 "Wrap line after at most this many characters.
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
126 :type '(choice (const :tag "window width" nil)
127 (integer :tag "number of characters"))
128 :group 'notmuch-wash)
130 (defface notmuch-wash-toggle-button
131 '((t (:inherit font-lock-comment-face)))
132 "Face used for buttons toggling the visibility of washed away
135 :group 'notmuch-faces)
137 (defface notmuch-wash-cited-text
138 '((t (:inherit message-cited-text)))
139 "Face used for cited text."
141 :group 'notmuch-faces)
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))
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))))))
162 (define-button-type 'notmuch-wash-button-invisibility-toggle-type
163 'action 'notmuch-wash-toggle-invisible-action
165 'face 'notmuch-wash-toggle-button
166 :supertype 'notmuch-button-type)
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)
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)
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)
180 (defun notmuch-wash-region-isearch-show (overlay)
181 (notmuch-wash-toggle-invisible-action
182 (overlay-get overlay 'notmuch-wash-button)))
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)))
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
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."
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.
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)
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))
222 (overlay-put overlay 'notmuch-wash-button button))))))
224 (defun notmuch-wash-excerpt-citations (msg depth)
225 "Excerpt citations and up to one signature."
226 (goto-char (point-min))
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
244 (goto-char cite-start)
245 (forward-line notmuch-wash-citation-lines-prefix)
246 (let ((hidden-start (point-marker)))
248 (forward-line (- notmuch-wash-citation-lines-suffix))
249 (notmuch-wash-region-to-button
250 msg hidden-start (point-marker)
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
269 (defun notmuch-wash-elide-blank-lines (msg depth)
270 "Elide leading, trailing and successive blank lines."
272 ;; Algorithm derived from `article-strip-multiple-blank-lines' in
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))
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)))
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)))
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))))
297 (defun notmuch-wash-tidy-citations (msg depth)
298 "Improve the display of cited regions of a message.
300 Perform several transformations on the message body:
302 - Remove lines of repeated citation leaders with no other
304 - Remove citation leaders standing alone before a block of cited
306 - Remove citation trailers standing alone after a block of cited
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"))
314 ;; Remove citation leaders standing alone before a block of cited
316 (goto-char (point-min))
317 (while (re-search-forward "\\(\n\\|^[^>].*\\)\n\\(^>[> ]*\n\\)" nil t)
318 (replace-match "\\1\n"))
320 ;; Remove citation trailers standing alone after a block of cited
322 (goto-char (point-min))
323 (while (re-search-forward "\\(^>[> ]*\n\\)\\(^$\\|^[^>].*\\)" nil t)
324 (replace-match "\\2")))
328 (defun notmuch-wash-wrap-long-lines (msg depth)
329 "Wrap long lines in the message.
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."
337 (let* ((coolj-wrap-follows-window-size nil)
338 (limit (if (numberp notmuch-wash-wrap-lines-length)
339 (min notmuch-wash-wrap-lines-length
342 (fill-column (- limit
344 ;; 2 to avoid poor interaction with
347 (coolj-wrap-region (point-min) (point-max))))
353 (defvar diff-file-header-re) ; From `diff-mode.el'.
355 (defun notmuch-wash-subject-to-filename (subject &optional maxlen)
356 "Convert a mail SUBJECT into a filename.
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.
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)))
372 (defun notmuch-wash-subject-to-patch-sequence-number (subject)
373 "Convert a patch mail SUBJECT into a patch sequence number.
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."
378 "^ *\\(\\[[^]]*\\] *\\)*\\[[^]]*?\\([0-9]+\\)/[0-9]+[^]]*\\].*"
380 (string-to-number (substring subject (match-beginning 2) (match-end 2)))))
382 (defun notmuch-wash-subject-to-patch-filename (subject)
383 "Convert a patch mail SUBJECT into a filename.
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)))
393 (defun notmuch-wash-convert-inline-patch-to-part (msg depth)
394 "Convert an inline patch into a fake 'text/x-diff' attachment.
396 Given that this function guesses whether a buffer includes a
397 patch and then guesses the extent of the patch, there is scope
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))
406 (goto-char patch-start)
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)))
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
421 (plist-get msg :headers) :Subject))))
422 (delete-region (point-min) (point-max))
423 (notmuch-show-insert-bodypart nil part depth)))))
427 (provide 'notmuch-wash)
429 ;;; notmuch-wash.el ends here