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>
27 (require 'notmuch-lib)
29 (declare-function notmuch-show-insert-bodypart "notmuch-show"
30 (msg part depth &optional hide))
31 (defvar notmuch-show-indent-messages-width)
35 (defgroup notmuch-wash nil
36 "Cleaning up messages for display."
39 (defcustom notmuch-wash-signature-regexp "^\\(-- ?\\|_+\\)$"
40 "Pattern to match a line that separates content from signature."
44 (defcustom notmuch-wash-citation-regexp "\\(^[[:space:]]*>.*\n\\)+"
45 "Pattern to match citation lines."
49 (defcustom notmuch-wash-original-regexp "^\\(--+\s?[oO]riginal [mM]essage\s?--+\\)$"
50 "Pattern to match a line that separates original message from
51 reply in top-posted message."
55 (defcustom notmuch-wash-button-signature-hidden-format
56 "[ %d-line signature. Click/Enter to show. ]"
57 "String used to construct button text for hidden signatures.
58 Can use up to one integer format parameter, i.e. %d."
62 (defcustom notmuch-wash-button-signature-visible-format
63 "[ %d-line signature. Click/Enter to hide. ]"
64 "String used to construct button text for visible signatures.
65 Can use up to one integer format parameter, i.e. %d."
69 (defcustom notmuch-wash-button-citation-hidden-format
70 "[ %d more citation lines. Click/Enter to show. ]"
71 "String used to construct button text for hidden citations.
72 Can use up to one integer format parameter, i.e. %d."
76 (defcustom notmuch-wash-button-citation-visible-format
77 "[ %d more citation lines. Click/Enter to hide. ]"
78 "String used to construct button text for visible citations.
79 Can use up to one integer format parameter, i.e. %d."
83 (defcustom notmuch-wash-button-original-hidden-format
84 "[ %d-line hidden original message. Click/Enter to show. ]"
85 "String used to construct button text for hidden citations.
86 Can use up to one integer format parameter, i.e. %d."
90 (defcustom notmuch-wash-button-original-visible-format
91 "[ %d-line original message. Click/Enter to hide. ]"
92 "String used to construct button text for visible citations.
93 Can use up to one integer format parameter, i.e. %d."
97 (defcustom notmuch-wash-signature-lines-max 12
98 "Maximum length of signature that will be hidden by default."
100 :group 'notmuch-wash)
102 (defcustom notmuch-wash-citation-lines-prefix 3
103 "Always show at least this many lines from the start of a citation.
105 If there is one more line than the sum of
106 `notmuch-wash-citation-lines-prefix' and
107 `notmuch-wash-citation-lines-suffix', show that, otherwise
108 collapse the remaining lines into a button."
110 :group 'notmuch-wash)
112 (defcustom notmuch-wash-citation-lines-suffix 3
113 "Always show at least this many lines from the end of a citation.
115 If there is one more line than the sum of
116 `notmuch-wash-citation-lines-prefix' and
117 `notmuch-wash-citation-lines-suffix', show that, otherwise
118 collapse the remaining lines into a button."
120 :group 'notmuch-wash)
122 (defcustom notmuch-wash-wrap-lines-length nil
123 "Wrap line after at most this many characters.
125 If this is nil, lines in messages will be wrapped to fit in the
126 current window. If this is a number, lines will be wrapped after
127 this many characters (ignoring indentation due to thread depth)
128 or at the window width (whichever one is lower)."
129 :type '(choice (const :tag "window width" nil)
130 (integer :tag "number of characters"))
131 :group 'notmuch-wash)
133 (defface notmuch-wash-toggle-button
134 '((t (:inherit font-lock-comment-face)))
135 "Face used for buttons toggling the visibility of washed away
138 :group 'notmuch-faces)
140 (defface notmuch-wash-cited-text
141 '((t (:inherit message-cited-text)))
142 "Face used for cited text."
144 :group 'notmuch-faces)
146 (defun notmuch-wash-toggle-invisible-action (cite-button)
147 ;; Toggle overlay visibility
148 (let ((overlay (button-get cite-button 'overlay)))
149 (overlay-put overlay 'invisible (not (overlay-get overlay 'invisible))))
150 ;; Update button text
151 (let* ((new-start (button-start cite-button))
152 (overlay (button-get cite-button 'overlay))
153 (button-label (notmuch-wash-button-label overlay))
155 (properties (text-properties-at (point)))
156 (inhibit-read-only t))
157 (goto-char new-start)
158 (insert button-label)
159 (set-text-properties new-start (point) properties)
160 (let ((old-end (button-end cite-button)))
161 (move-overlay cite-button new-start (point))
162 (delete-region (point) old-end))
163 (goto-char (min old-point (1- (button-end cite-button))))))
165 (define-button-type 'notmuch-wash-button-invisibility-toggle-type
166 'action 'notmuch-wash-toggle-invisible-action
168 'face 'notmuch-wash-toggle-button
169 :supertype 'notmuch-button-type)
171 (define-button-type 'notmuch-wash-button-citation-toggle-type
172 'help-echo "mouse-1, RET: Show citation"
173 :supertype 'notmuch-wash-button-invisibility-toggle-type)
175 (define-button-type 'notmuch-wash-button-signature-toggle-type
176 'help-echo "mouse-1, RET: Show signature"
177 :supertype 'notmuch-wash-button-invisibility-toggle-type)
179 (define-button-type 'notmuch-wash-button-original-toggle-type
180 'help-echo "mouse-1, RET: Show original message"
181 :supertype 'notmuch-wash-button-invisibility-toggle-type)
183 (defun notmuch-wash-region-isearch-show (overlay)
184 (notmuch-wash-toggle-invisible-action
185 (overlay-get overlay 'notmuch-wash-button)))
187 (defun notmuch-wash-button-label (overlay)
188 (let* ((type (overlay-get overlay 'type))
189 (invis-spec (overlay-get overlay 'invisible))
190 (state (if (invisible-p invis-spec) "hidden" "visible"))
191 (label-format (symbol-value
193 (format "notmuch-wash-button-%s-%s-format"
195 (lines-count (count-lines (overlay-start overlay)
196 (overlay-end overlay))))
197 (format label-format lines-count)))
199 (defun notmuch-wash-region-to-button (msg beg end type &optional prefix)
200 "Auxiliary function to do the actual making of overlays and buttons.
202 BEG and END are buffer locations. TYPE should a string, either
203 \"citation\" or \"signature\". Optional PREFIX is some arbitrary
204 text to insert before the button, probably for indentation. Note
205 that PREFIX should not include a newline."
206 ;; This uses some slightly tricky conversions between strings and
207 ;; symbols because of the way the button code works. Note that
208 ;; replacing intern-soft with make-symbol will cause this to fail,
209 ;; since the newly created symbol has no plist.
210 (let ((overlay (make-overlay beg end))
211 (button-type (intern-soft (concat "notmuch-wash-button-"
212 type "-toggle-type"))))
213 (overlay-put overlay 'invisible t)
214 (overlay-put overlay 'isearch-open-invisible #'notmuch-wash-region-isearch-show)
215 (overlay-put overlay 'type type)
220 (insert-before-markers prefix))
221 (let ((button-beg (point)))
222 (insert-before-markers (notmuch-wash-button-label overlay) "\n")
223 (let ((button (make-button button-beg (1- (point))
226 (overlay-put overlay 'notmuch-wash-button button))))))
228 (defun notmuch-wash-excerpt-citations (msg depth)
229 "Excerpt citations and up to one signature."
230 (goto-char (point-min))
232 (when (and (< (point) (point-max))
233 (re-search-forward notmuch-wash-original-regexp nil t))
234 (let* ((msg-start (match-beginning 0))
235 (msg-end (point-max))
236 (msg-lines (count-lines msg-start msg-end)))
237 (notmuch-wash-region-to-button
238 msg msg-start msg-end "original")))
239 (while (and (< (point) (point-max))
240 (re-search-forward notmuch-wash-citation-regexp nil t))
241 (let* ((cite-start (match-beginning 0))
242 (cite-end (match-end 0))
243 (cite-lines (count-lines cite-start cite-end)))
244 (overlay-put (make-overlay cite-start cite-end)
245 'face 'notmuch-wash-cited-text)
246 (when (> cite-lines (+ notmuch-wash-citation-lines-prefix
247 notmuch-wash-citation-lines-suffix
249 (goto-char cite-start)
250 (forward-line notmuch-wash-citation-lines-prefix)
251 (let ((hidden-start (point-marker)))
253 (forward-line (- notmuch-wash-citation-lines-suffix))
254 (notmuch-wash-region-to-button
255 msg hidden-start (point-marker)
257 (when (and (not (eobp))
258 (re-search-forward notmuch-wash-signature-regexp nil t))
259 (let* ((sig-start (match-beginning 0))
260 (sig-end (match-end 0))
261 (sig-lines (count-lines sig-start (point-max))))
262 (when (<= sig-lines notmuch-wash-signature-lines-max)
263 (let ((sig-start-marker (make-marker))
264 (sig-end-marker (make-marker)))
265 (set-marker sig-start-marker sig-start)
266 (set-marker sig-end-marker (point-max))
267 (overlay-put (make-overlay sig-start-marker sig-end-marker)
268 'face 'message-cited-text)
269 (notmuch-wash-region-to-button
270 msg sig-start-marker sig-end-marker
275 (defun notmuch-wash-elide-blank-lines (msg depth)
276 "Elide leading, trailing and successive blank lines."
277 ;; Algorithm derived from `article-strip-multiple-blank-lines' in
279 ;; Make all blank lines empty.
280 (goto-char (point-min))
281 (while (re-search-forward "^[[:space:]\t]+$" nil t)
282 (replace-match "" nil t))
283 ;; Replace multiple empty lines with a single empty line.
284 (goto-char (point-min))
285 (while (re-search-forward "^\n\\(\n+\\)" nil t)
286 (delete-region (match-beginning 1) (match-end 1)))
287 ;; Remove a leading blank line.
288 (goto-char (point-min))
289 (when (looking-at "\n")
290 (delete-region (match-beginning 0) (match-end 0)))
291 ;; Remove a trailing blank line.
292 (goto-char (point-max))
293 (when (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
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"))
313 ;; Remove citation leaders standing alone before a block of cited text.
314 (goto-char (point-min))
315 (while (re-search-forward "\\(\n\\|^[^>].*\\)\n\\(^>[> ]*\n\\)" nil t)
316 (replace-match "\\1\n"))
317 ;; Remove citation trailers standing alone after a block of cited text.
318 (goto-char (point-min))
319 (while (re-search-forward "\\(^>[> ]*\n\\)\\(^$\\|^[^>].*\\)" nil t)
320 (replace-match "\\2")))
324 (defun notmuch-wash-wrap-long-lines (msg depth)
325 "Wrap long lines in the message.
327 If `notmuch-wash-wrap-lines-length' is a number, this will wrap
328 the message lines to the minimum of the width of the window or
329 its value. Otherwise, this function will wrap long lines in the
330 message at the window width. When doing so, citation leaders in
331 the wrapped text are maintained."
332 (let* ((coolj-wrap-follows-window-size nil)
333 (indent (* depth notmuch-show-indent-messages-width))
334 (limit (if (numberp notmuch-wash-wrap-lines-length)
335 (min (+ notmuch-wash-wrap-lines-length indent)
338 (fill-column (- limit
340 ;; 2 to avoid poor interaction with
343 (coolj-wrap-region (point-min) (point-max))))
349 (defvar diff-file-header-re) ; From `diff-mode.el'.
351 (defun notmuch-wash-subject-to-filename (subject &optional maxlen)
352 "Convert a mail SUBJECT into a filename.
354 The resulting filename is similar to the names generated by \"git
355 format-patch\", without the leading patch sequence number
356 \"0001-\" and \".patch\" extension. Any leading \"[PREFIX]\"
357 style strings are removed prior to conversion.
359 Optional argument MAXLEN is the maximum length of the resulting
360 filename, before trimming any trailing . and - characters."
361 (let* ((s (replace-regexp-in-string "^ *\\(\\[[^]]*\\] *\\)*" "" subject))
362 (s (replace-regexp-in-string "[^A-Za-z0-9._]+" "-" s))
363 (s (replace-regexp-in-string "\\.+" "." s))
364 (s (if maxlen (substring s 0 (min (length s) maxlen)) s))
365 (s (replace-regexp-in-string "[.-]*$" "" s)))
368 (defun notmuch-wash-subject-to-patch-sequence-number (subject)
369 "Convert a patch mail SUBJECT into a patch sequence number.
371 Return the patch sequence number N from the last \"[PATCH N/M]\"
372 style prefix in SUBJECT, or nil if such a prefix can't be found."
374 "^ *\\(\\[[^]]*\\] *\\)*\\[[^]]*?\\([0-9]+\\)/[0-9]+[^]]*\\].*"
376 (string-to-number (substring subject (match-beginning 2) (match-end 2)))))
378 (defun notmuch-wash-subject-to-patch-filename (subject)
379 "Convert a patch mail SUBJECT into a filename.
381 The resulting filename is similar to the names generated by \"git
382 format-patch\". If the patch mail was generated and sent using
383 \"git format-patch/send-email\", this should re-create the
384 original filename the sender had."
385 (format "%04d-%s.patch"
386 (or (notmuch-wash-subject-to-patch-sequence-number subject) 1)
387 (notmuch-wash-subject-to-filename subject 52)))
389 (defun notmuch-wash-convert-inline-patch-to-part (msg depth)
390 "Convert an inline patch into a fake 'text/x-diff' attachment.
392 Given that this function guesses whether a buffer includes a
393 patch and then guesses the extent of the patch, there is scope
395 (goto-char (point-min))
396 (when (re-search-forward diff-file-header-re nil t)
397 (beginning-of-line -1)
398 (let ((patch-start (point))
399 (patch-end (point-max))
401 (goto-char patch-start)
403 ;; Patch ends with signature.
404 (re-search-forward notmuch-wash-signature-regexp nil t)
405 ;; Patch ends with bugtraq comment.
406 (re-search-forward "^\\*\\*\\* " nil t))
407 (setq patch-end (match-beginning 0)))
409 (narrow-to-region patch-start patch-end)
410 (setq part (plist-put part :content-type "inline patch"))
411 (setq part (plist-put part :content (buffer-string)))
412 (setq part (plist-put part :id -1))
413 (setq part (plist-put part :filename
414 (notmuch-wash-subject-to-patch-filename
416 (plist-get msg :headers) :Subject))))
417 (delete-region (point-min) (point-max))
418 (notmuch-show-insert-bodypart nil part depth)))))
422 (provide 'notmuch-wash)
424 ;;; notmuch-wash.el ends here