1 ;;; notmuch-wash.el --- cleaning up message bodies -*- lexical-binding: t -*-
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)
135 (defface notmuch-wash-toggle-button
136 '((t (:inherit font-lock-comment-face)))
137 "Face used for buttons toggling the visibility of washed away
140 :group 'notmuch-faces)
142 (defface notmuch-wash-cited-text
143 '((t (:inherit message-cited-text)))
144 "Face used for cited text."
146 :group 'notmuch-faces)
150 (defun notmuch-wash-toggle-invisible-action (cite-button)
151 ;; Toggle overlay visibility
152 (let ((overlay (button-get cite-button 'overlay)))
153 (overlay-put overlay 'invisible (not (overlay-get overlay 'invisible))))
154 ;; Update button text
155 (let* ((new-start (button-start cite-button))
156 (overlay (button-get cite-button 'overlay))
157 (button-label (notmuch-wash-button-label overlay))
159 (properties (text-properties-at (point)))
160 (inhibit-read-only t))
161 (goto-char new-start)
162 (insert button-label)
163 (set-text-properties new-start (point) properties)
164 (let ((old-end (button-end cite-button)))
165 (move-overlay cite-button new-start (point))
166 (delete-region (point) old-end))
167 (goto-char (min old-point (1- (button-end cite-button))))))
169 (define-button-type 'notmuch-wash-button-invisibility-toggle-type
170 'action 'notmuch-wash-toggle-invisible-action
172 'face 'notmuch-wash-toggle-button
173 :supertype 'notmuch-button-type)
175 (define-button-type 'notmuch-wash-button-citation-toggle-type
176 'help-echo "mouse-1, RET: Show citation"
177 :supertype 'notmuch-wash-button-invisibility-toggle-type)
179 (define-button-type 'notmuch-wash-button-signature-toggle-type
180 'help-echo "mouse-1, RET: Show signature"
181 :supertype 'notmuch-wash-button-invisibility-toggle-type)
183 (define-button-type 'notmuch-wash-button-original-toggle-type
184 'help-echo "mouse-1, RET: Show original message"
185 :supertype 'notmuch-wash-button-invisibility-toggle-type)
187 (defun notmuch-wash-region-isearch-show (overlay)
188 (notmuch-wash-toggle-invisible-action
189 (overlay-get overlay 'notmuch-wash-button)))
191 (defun notmuch-wash-button-label (overlay)
192 (let* ((type (overlay-get overlay 'type))
193 (invis-spec (overlay-get overlay 'invisible))
194 (state (if (invisible-p invis-spec) "hidden" "visible"))
195 (label-format (symbol-value
197 (format "notmuch-wash-button-%s-%s-format"
199 (lines-count (count-lines (overlay-start overlay)
200 (overlay-end overlay))))
201 (format label-format lines-count)))
203 (defun notmuch-wash-region-to-button (msg beg end type &optional prefix)
204 "Auxiliary function to do the actual making of overlays and buttons.
206 BEG and END are buffer locations. TYPE should a string, either
207 \"citation\" or \"signature\". Optional PREFIX is some arbitrary
208 text to insert before the button, probably for indentation. Note
209 that PREFIX should not include a newline."
210 ;; This uses some slightly tricky conversions between strings and
211 ;; symbols because of the way the button code works. Note that
212 ;; replacing intern-soft with make-symbol will cause this to fail,
213 ;; since the newly created symbol has no plist.
214 (let ((overlay (make-overlay beg end))
215 (button-type (intern-soft (concat "notmuch-wash-button-"
216 type "-toggle-type"))))
217 (overlay-put overlay 'invisible t)
218 (overlay-put overlay 'isearch-open-invisible #'notmuch-wash-region-isearch-show)
219 (overlay-put overlay 'type type)
224 (insert-before-markers prefix))
225 (let ((button-beg (point)))
226 (insert-before-markers (notmuch-wash-button-label overlay) "\n")
227 (let ((button (make-button button-beg (1- (point))
230 (overlay-put overlay 'notmuch-wash-button button))))))
234 (defun notmuch-wash-excerpt-citations (msg depth)
235 "Excerpt citations and up to one signature."
236 (goto-char (point-min))
238 (when (and (< (point) (point-max))
239 (re-search-forward notmuch-wash-original-regexp nil t))
240 (notmuch-wash-region-to-button msg
244 (while (and (< (point) (point-max))
245 (re-search-forward notmuch-wash-citation-regexp nil t))
246 (let* ((cite-start (match-beginning 0))
247 (cite-end (match-end 0))
248 (cite-lines (count-lines cite-start cite-end)))
249 (overlay-put (make-overlay cite-start cite-end)
250 'face 'notmuch-wash-cited-text)
251 (when (> cite-lines (+ notmuch-wash-citation-lines-prefix
252 notmuch-wash-citation-lines-suffix
254 (goto-char cite-start)
255 (forward-line notmuch-wash-citation-lines-prefix)
256 (let ((hidden-start (point-marker)))
258 (forward-line (- notmuch-wash-citation-lines-suffix))
259 (notmuch-wash-region-to-button
260 msg hidden-start (point-marker)
262 (when (and (not (eobp))
263 (re-search-forward notmuch-wash-signature-regexp nil t))
264 (let ((sig-start (match-beginning 0)))
265 (when (<= (count-lines sig-start (point-max))
266 notmuch-wash-signature-lines-max)
267 (let ((sig-start-marker (make-marker))
268 (sig-end-marker (make-marker)))
269 (set-marker sig-start-marker sig-start)
270 (set-marker sig-end-marker (point-max))
271 (overlay-put (make-overlay sig-start-marker sig-end-marker)
272 'face 'message-cited-text)
273 (notmuch-wash-region-to-button
274 msg sig-start-marker sig-end-marker
277 (defun notmuch-wash-elide-blank-lines (msg depth)
278 "Elide leading, trailing and successive blank lines."
279 ;; Algorithm derived from `article-strip-multiple-blank-lines' in
281 ;; Make all blank lines empty.
282 (goto-char (point-min))
283 (while (re-search-forward "^[[:space:]\t]+$" nil t)
284 (replace-match "" nil t))
285 ;; Replace multiple empty lines with a single empty line.
286 (goto-char (point-min))
287 (while (re-search-forward "^\n\\(\n+\\)" nil t)
288 (delete-region (match-beginning 1) (match-end 1)))
289 ;; Remove a leading blank line.
290 (goto-char (point-min))
291 (when (looking-at "\n")
292 (delete-region (match-beginning 0) (match-end 0)))
293 ;; Remove a trailing blank line.
294 (goto-char (point-max))
295 (when (looking-at "\n")
296 (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")))
322 (defun notmuch-wash-wrap-long-lines (msg depth)
323 "Wrap long lines in the message.
325 If `notmuch-wash-wrap-lines-length' is a number, this will wrap
326 the message lines to the minimum of the width of the window or
327 its value. Otherwise, this function will wrap long lines in the
328 message at the window width. When doing so, citation leaders in
329 the wrapped text are maintained."
330 (let* ((coolj-wrap-follows-window-size nil)
331 (indent (* depth notmuch-show-indent-messages-width))
332 (limit (if (numberp notmuch-wash-wrap-lines-length)
333 (min (+ notmuch-wash-wrap-lines-length indent)
336 (fill-column (- limit
338 ;; 2 to avoid poor interaction with
341 (coolj-wrap-region (point-min) (point-max))))
343 ;;;; Convert Inline Patches
347 (defvar diff-file-header-re) ; From `diff-mode.el'.
349 (defun notmuch-wash-subject-to-filename (subject &optional maxlen)
350 "Convert a mail SUBJECT into a filename.
352 The resulting filename is similar to the names generated by \"git
353 format-patch\", without the leading patch sequence number
354 \"0001-\" and \".patch\" extension. Any leading \"[PREFIX]\"
355 style strings are removed prior to conversion.
357 Optional argument MAXLEN is the maximum length of the resulting
358 filename, before trimming any trailing . and - characters."
359 (let* ((s (replace-regexp-in-string "^ *\\(\\[[^]]*\\] *\\)*" "" subject))
360 (s (replace-regexp-in-string "[^A-Za-z0-9._]+" "-" s))
361 (s (replace-regexp-in-string "\\.+" "." s))
362 (s (if maxlen (substring s 0 (min (length s) maxlen)) s))
363 (s (replace-regexp-in-string "[.-]*$" "" s)))
366 (defun notmuch-wash-subject-to-patch-sequence-number (subject)
367 "Convert a patch mail SUBJECT into a patch sequence number.
369 Return the patch sequence number N from the last \"[PATCH N/M]\"
370 style prefix in SUBJECT, or nil if such a prefix can't be found."
372 "^ *\\(\\[[^]]*\\] *\\)*\\[[^]]*?\\([0-9]+\\)/[0-9]+[^]]*\\].*"
374 (string-to-number (substring subject (match-beginning 2) (match-end 2)))))
376 (defun notmuch-wash-subject-to-patch-filename (subject)
377 "Convert a patch mail SUBJECT into a filename.
379 The resulting filename is similar to the names generated by \"git
380 format-patch\". If the patch mail was generated and sent using
381 \"git format-patch/send-email\", this should re-create the
382 original filename the sender had."
383 (format "%04d-%s.patch"
384 (or (notmuch-wash-subject-to-patch-sequence-number subject) 1)
385 (notmuch-wash-subject-to-filename subject 52)))
387 (defun notmuch-wash-convert-inline-patch-to-part (msg depth)
388 "Convert an inline patch into a fake 'text/x-diff' attachment.
390 Given that this function guesses whether a buffer includes a
391 patch and then guesses the extent of the patch, there is scope
393 (goto-char (point-min))
394 (when (re-search-forward diff-file-header-re nil t)
395 (beginning-of-line -1)
396 (let ((patch-start (point))
397 (patch-end (point-max))
399 (goto-char patch-start)
401 ;; Patch ends with signature.
402 (re-search-forward notmuch-wash-signature-regexp nil t)
403 ;; Patch ends with bugtraq comment.
404 (re-search-forward "^\\*\\*\\* " nil t))
405 (setq patch-end (match-beginning 0)))
407 (narrow-to-region patch-start patch-end)
408 (setq part (plist-put part :content-type "inline patch"))
409 (setq part (plist-put part :content (buffer-string)))
410 (setq part (plist-put part :id -1))
411 (setq part (plist-put part :filename
412 (notmuch-wash-subject-to-patch-filename
414 (plist-get msg :headers) :Subject))))
415 (delete-region (point-min) (point-max))
416 (notmuch-show-insert-bodypart nil part depth)))))
420 (provide 'notmuch-wash)
422 ;;; notmuch-wash.el ends here