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 <https://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))
29 (defvar notmuch-show-indent-messages-width)
33 (defgroup notmuch-wash nil
34 "Cleaning up messages for display."
37 (defcustom notmuch-wash-signature-regexp "^\\(-- ?\\|_+\\)$"
38 "Pattern to match a line that separates content from signature."
42 (defcustom notmuch-wash-citation-regexp "\\(^[[:space:]]*>.*\n\\)+"
43 "Pattern to match citation lines."
47 (defcustom notmuch-wash-original-regexp "^\\(--+\s?[oO]riginal [mM]essage\s?--+\\)$"
48 "Pattern to match a line that separates original message from
49 reply in top-posted message."
53 (defcustom notmuch-wash-button-signature-hidden-format
54 "[ %d-line signature. Click/Enter to show. ]"
55 "String used to construct button text for hidden signatures.
56 Can use up to one integer format parameter, i.e. %d."
60 (defcustom notmuch-wash-button-signature-visible-format
61 "[ %d-line signature. Click/Enter to hide. ]"
62 "String used to construct button text for visible signatures.
63 Can use up to one integer format parameter, i.e. %d."
67 (defcustom notmuch-wash-button-citation-hidden-format
68 "[ %d more citation lines. Click/Enter to show. ]"
69 "String used to construct button text for hidden citations.
70 Can use up to one integer format parameter, i.e. %d."
74 (defcustom notmuch-wash-button-citation-visible-format
75 "[ %d more citation lines. Click/Enter to hide. ]"
76 "String used to construct button text for visible citations.
77 Can use up to one integer format parameter, i.e. %d."
81 (defcustom notmuch-wash-button-original-hidden-format
82 "[ %d-line hidden original message. Click/Enter to show. ]"
83 "String used to construct button text for hidden citations.
84 Can use up to one integer format parameter, i.e. %d."
88 (defcustom notmuch-wash-button-original-visible-format
89 "[ %d-line original message. Click/Enter to hide. ]"
90 "String used to construct button text for visible citations.
91 Can use up to one integer format parameter, i.e. %d."
95 (defcustom notmuch-wash-signature-lines-max 12
96 "Maximum length of signature that will be hidden by default."
100 (defcustom notmuch-wash-citation-lines-prefix 3
101 "Always show at least this many lines from the start of a citation.
103 If there is one more line than the sum of
104 `notmuch-wash-citation-lines-prefix' and
105 `notmuch-wash-citation-lines-suffix', show that, otherwise
106 collapse the remaining lines into a button."
108 :group 'notmuch-wash)
110 (defcustom notmuch-wash-citation-lines-suffix 3
111 "Always show at least this many lines from the end of a citation.
113 If there is one more line than the sum of
114 `notmuch-wash-citation-lines-prefix' and
115 `notmuch-wash-citation-lines-suffix', show that, otherwise
116 collapse the remaining lines into a button."
118 :group 'notmuch-wash)
120 (defcustom notmuch-wash-wrap-lines-length nil
121 "Wrap line after at most this many characters.
123 If this is nil, lines in messages will be wrapped to fit in the
124 current window. If this is a number, lines will be wrapped after
125 this many characters (ignoring indentation due to thread depth)
126 or at the window width (whichever one is lower)."
127 :type '(choice (const :tag "window width" nil)
128 (integer :tag "number of characters"))
129 :group 'notmuch-wash)
131 (defface notmuch-wash-toggle-button
132 '((t (:inherit font-lock-comment-face)))
133 "Face used for buttons toggling the visibility of washed away
136 :group 'notmuch-faces)
138 (defface notmuch-wash-cited-text
139 '((t (:inherit message-cited-text)))
140 "Face used for cited text."
142 :group 'notmuch-faces)
144 (defun notmuch-wash-toggle-invisible-action (cite-button)
145 ;; Toggle overlay visibility
146 (let ((overlay (button-get cite-button 'overlay)))
147 (overlay-put overlay 'invisible (not (overlay-get overlay 'invisible))))
148 ;; Update button text
149 (let* ((new-start (button-start cite-button))
150 (overlay (button-get cite-button 'overlay))
151 (button-label (notmuch-wash-button-label overlay))
153 (properties (text-properties-at (point)))
154 (inhibit-read-only t))
155 (goto-char new-start)
156 (insert button-label)
157 (set-text-properties new-start (point) properties)
158 (let ((old-end (button-end cite-button)))
159 (move-overlay cite-button new-start (point))
160 (delete-region (point) old-end))
161 (goto-char (min old-point (1- (button-end cite-button))))))
163 (define-button-type 'notmuch-wash-button-invisibility-toggle-type
164 'action 'notmuch-wash-toggle-invisible-action
166 'face 'notmuch-wash-toggle-button
167 :supertype 'notmuch-button-type)
169 (define-button-type 'notmuch-wash-button-citation-toggle-type
170 'help-echo "mouse-1, RET: Show citation"
171 :supertype 'notmuch-wash-button-invisibility-toggle-type)
173 (define-button-type 'notmuch-wash-button-signature-toggle-type
174 'help-echo "mouse-1, RET: Show signature"
175 :supertype 'notmuch-wash-button-invisibility-toggle-type)
177 (define-button-type 'notmuch-wash-button-original-toggle-type
178 'help-echo "mouse-1, RET: Show original message"
179 :supertype 'notmuch-wash-button-invisibility-toggle-type)
181 (defun notmuch-wash-region-isearch-show (overlay)
182 (notmuch-wash-toggle-invisible-action
183 (overlay-get overlay 'notmuch-wash-button)))
185 (defun notmuch-wash-button-label (overlay)
186 (let* ((type (overlay-get overlay 'type))
187 (invis-spec (overlay-get overlay 'invisible))
188 (state (if (invisible-p invis-spec) "hidden" "visible"))
189 (label-format (symbol-value (intern-soft (concat "notmuch-wash-button-"
190 type "-" state "-format"))))
191 (lines-count (count-lines (overlay-start overlay) (overlay-end overlay))))
192 (format label-format lines-count)))
194 (defun notmuch-wash-region-to-button (msg beg end type &optional prefix)
195 "Auxiliary function to do the actual making of overlays and buttons
197 BEG and END are buffer locations. TYPE should a string, either
198 \"citation\" or \"signature\". Optional PREFIX is some arbitrary
199 text to insert before the button, probably for indentation. Note
200 that PREFIX should not include a newline."
202 ;; This uses some slightly tricky conversions between strings and
203 ;; symbols because of the way the button code works. Note that
204 ;; replacing intern-soft with make-symbol will cause this to fail,
205 ;; since the newly created symbol has no plist.
207 (let ((overlay (make-overlay beg end))
208 (button-type (intern-soft (concat "notmuch-wash-button-"
209 type "-toggle-type"))))
210 (overlay-put overlay 'invisible t)
211 (overlay-put overlay 'isearch-open-invisible #'notmuch-wash-region-isearch-show)
212 (overlay-put overlay 'type type)
217 (insert-before-markers prefix))
218 (let ((button-beg (point)))
219 (insert-before-markers (notmuch-wash-button-label overlay) "\n")
220 (let ((button (make-button button-beg (1- (point))
223 (overlay-put overlay 'notmuch-wash-button button))))))
225 (defun notmuch-wash-excerpt-citations (msg depth)
226 "Excerpt citations and up to one signature."
227 (goto-char (point-min))
229 (if (and (< (point) (point-max))
230 (re-search-forward notmuch-wash-original-regexp nil t))
231 (let* ((msg-start (match-beginning 0))
232 (msg-end (point-max))
233 (msg-lines (count-lines msg-start msg-end)))
234 (notmuch-wash-region-to-button
235 msg msg-start msg-end "original")))
236 (while (and (< (point) (point-max))
237 (re-search-forward notmuch-wash-citation-regexp nil t))
238 (let* ((cite-start (match-beginning 0))
239 (cite-end (match-end 0))
240 (cite-lines (count-lines cite-start cite-end)))
241 (overlay-put (make-overlay cite-start cite-end) 'face 'notmuch-wash-cited-text)
242 (when (> cite-lines (+ notmuch-wash-citation-lines-prefix
243 notmuch-wash-citation-lines-suffix
245 (goto-char cite-start)
246 (forward-line notmuch-wash-citation-lines-prefix)
247 (let ((hidden-start (point-marker)))
249 (forward-line (- notmuch-wash-citation-lines-suffix))
250 (notmuch-wash-region-to-button
251 msg hidden-start (point-marker)
253 (if (and (not (eobp))
254 (re-search-forward notmuch-wash-signature-regexp nil t))
255 (let* ((sig-start (match-beginning 0))
256 (sig-end (match-end 0))
257 (sig-lines (count-lines sig-start (point-max))))
258 (if (<= sig-lines notmuch-wash-signature-lines-max)
259 (let ((sig-start-marker (make-marker))
260 (sig-end-marker (make-marker)))
261 (set-marker sig-start-marker sig-start)
262 (set-marker sig-end-marker (point-max))
263 (overlay-put (make-overlay sig-start-marker sig-end-marker) 'face 'message-cited-text)
264 (notmuch-wash-region-to-button
265 msg sig-start-marker sig-end-marker
270 (defun notmuch-wash-elide-blank-lines (msg depth)
271 "Elide leading, trailing and successive blank lines."
273 ;; Algorithm derived from `article-strip-multiple-blank-lines' in
276 ;; Make all blank lines empty.
277 (goto-char (point-min))
278 (while (re-search-forward "^[[:space:]\t]+$" nil t)
279 (replace-match "" nil t))
281 ;; Replace multiple empty lines with a single empty line.
282 (goto-char (point-min))
283 (while (re-search-forward "^\n\\(\n+\\)" nil t)
284 (delete-region (match-beginning 1) (match-end 1)))
286 ;; Remove a leading blank line.
287 (goto-char (point-min))
288 (if (looking-at "\n")
289 (delete-region (match-beginning 0) (match-end 0)))
291 ;; Remove a trailing blank line.
292 (goto-char (point-max))
293 (if (looking-at "\n")
294 (delete-region (match-beginning 0) (match-end 0))))
298 (defun notmuch-wash-tidy-citations (msg depth)
299 "Improve the display of cited regions of a message.
301 Perform several transformations on the message body:
303 - Remove lines of repeated citation leaders with no other
305 - Remove citation leaders standing alone before a block of cited
307 - Remove citation trailers standing alone after a block of cited
310 ;; Remove lines of repeated citation leaders with no other content.
311 (goto-char (point-min))
312 (while (re-search-forward "\\(^>[> ]*\n\\)\\{2,\\}" nil t)
313 (replace-match "\\1"))
315 ;; Remove citation leaders standing alone before a block of cited
317 (goto-char (point-min))
318 (while (re-search-forward "\\(\n\\|^[^>].*\\)\n\\(^>[> ]*\n\\)" nil t)
319 (replace-match "\\1\n"))
321 ;; Remove citation trailers standing alone after a block of cited
323 (goto-char (point-min))
324 (while (re-search-forward "\\(^>[> ]*\n\\)\\(^$\\|^[^>].*\\)" nil t)
325 (replace-match "\\2")))
329 (defun notmuch-wash-wrap-long-lines (msg depth)
330 "Wrap long lines in the message.
332 If `notmuch-wash-wrap-lines-length' is a number, this will wrap
333 the message lines to the minimum of the width of the window or
334 its value. Otherwise, this function will wrap long lines in the
335 message at the window width. When doing so, citation leaders in
336 the wrapped text are maintained."
338 (let* ((coolj-wrap-follows-window-size nil)
339 (indent (* depth notmuch-show-indent-messages-width))
340 (limit (if (numberp notmuch-wash-wrap-lines-length)
341 (min (+ notmuch-wash-wrap-lines-length indent)
344 (fill-column (- limit
346 ;; 2 to avoid poor interaction with
349 (coolj-wrap-region (point-min) (point-max))))
355 (defvar diff-file-header-re) ; From `diff-mode.el'.
357 (defun notmuch-wash-subject-to-filename (subject &optional maxlen)
358 "Convert a mail SUBJECT into a filename.
360 The resulting filename is similar to the names generated by \"git
361 format-patch\", without the leading patch sequence number
362 \"0001-\" and \".patch\" extension. Any leading \"[PREFIX]\"
363 style strings are removed prior to conversion.
365 Optional argument MAXLEN is the maximum length of the resulting
366 filename, before trimming any trailing . and - characters."
367 (let* ((s (replace-regexp-in-string "^ *\\(\\[[^]]*\\] *\\)*" "" subject))
368 (s (replace-regexp-in-string "[^A-Za-z0-9._]+" "-" s))
369 (s (replace-regexp-in-string "\\.+" "." s))
370 (s (if maxlen (substring s 0 (min (length s) maxlen)) s))
371 (s (replace-regexp-in-string "[.-]*$" "" s)))
374 (defun notmuch-wash-subject-to-patch-sequence-number (subject)
375 "Convert a patch mail SUBJECT into a patch sequence number.
377 Return the patch sequence number N from the last \"[PATCH N/M]\"
378 style prefix in SUBJECT, or nil if such a prefix can't be found."
380 "^ *\\(\\[[^]]*\\] *\\)*\\[[^]]*?\\([0-9]+\\)/[0-9]+[^]]*\\].*"
382 (string-to-number (substring subject (match-beginning 2) (match-end 2)))))
384 (defun notmuch-wash-subject-to-patch-filename (subject)
385 "Convert a patch mail SUBJECT into a filename.
387 The resulting filename is similar to the names generated by \"git
388 format-patch\". If the patch mail was generated and sent using
389 \"git format-patch/send-email\", this should re-create the
390 original filename the sender had."
391 (format "%04d-%s.patch"
392 (or (notmuch-wash-subject-to-patch-sequence-number subject) 1)
393 (notmuch-wash-subject-to-filename subject 52)))
395 (defun notmuch-wash-convert-inline-patch-to-part (msg depth)
396 "Convert an inline patch into a fake 'text/x-diff' attachment.
398 Given that this function guesses whether a buffer includes a
399 patch and then guesses the extent of the patch, there is scope
402 (goto-char (point-min))
403 (when (re-search-forward diff-file-header-re nil t)
404 (beginning-of-line -1)
405 (let ((patch-start (point))
406 (patch-end (point-max))
408 (goto-char patch-start)
410 ;; Patch ends with signature.
411 (re-search-forward notmuch-wash-signature-regexp nil t)
412 ;; Patch ends with bugtraq comment.
413 (re-search-forward "^\\*\\*\\* " nil t))
414 (setq patch-end (match-beginning 0)))
416 (narrow-to-region patch-start patch-end)
417 (setq part (plist-put part :content-type "inline patch"))
418 (setq part (plist-put part :content (buffer-string)))
419 (setq part (plist-put part :id -1))
420 (setq part (plist-put part :filename
421 (notmuch-wash-subject-to-patch-filename
423 (plist-get msg :headers) :Subject))))
424 (delete-region (point-min) (point-max))
425 (notmuch-show-insert-bodypart nil part depth)))))
429 (provide 'notmuch-wash)
431 ;;; notmuch-wash.el ends here