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."
207 ;; This uses some slightly tricky conversions between strings and
208 ;; symbols because of the way the button code works. Note that
209 ;; replacing intern-soft with make-symbol will cause this to fail,
210 ;; since the newly created symbol has no plist.
212 (let ((overlay (make-overlay beg end))
213 (button-type (intern-soft (concat "notmuch-wash-button-"
214 type "-toggle-type"))))
215 (overlay-put overlay 'invisible t)
216 (overlay-put overlay 'isearch-open-invisible #'notmuch-wash-region-isearch-show)
217 (overlay-put overlay 'type type)
222 (insert-before-markers prefix))
223 (let ((button-beg (point)))
224 (insert-before-markers (notmuch-wash-button-label overlay) "\n")
225 (let ((button (make-button button-beg (1- (point))
228 (overlay-put overlay 'notmuch-wash-button button))))))
230 (defun notmuch-wash-excerpt-citations (msg depth)
231 "Excerpt citations and up to one signature."
232 (goto-char (point-min))
234 (if (and (< (point) (point-max))
235 (re-search-forward notmuch-wash-original-regexp nil t))
236 (let* ((msg-start (match-beginning 0))
237 (msg-end (point-max))
238 (msg-lines (count-lines msg-start msg-end)))
239 (notmuch-wash-region-to-button
240 msg msg-start msg-end "original")))
241 (while (and (< (point) (point-max))
242 (re-search-forward notmuch-wash-citation-regexp nil t))
243 (let* ((cite-start (match-beginning 0))
244 (cite-end (match-end 0))
245 (cite-lines (count-lines cite-start cite-end)))
246 (overlay-put (make-overlay cite-start cite-end)
247 'face 'notmuch-wash-cited-text)
248 (when (> cite-lines (+ notmuch-wash-citation-lines-prefix
249 notmuch-wash-citation-lines-suffix
251 (goto-char cite-start)
252 (forward-line notmuch-wash-citation-lines-prefix)
253 (let ((hidden-start (point-marker)))
255 (forward-line (- notmuch-wash-citation-lines-suffix))
256 (notmuch-wash-region-to-button
257 msg hidden-start (point-marker)
259 (if (and (not (eobp))
260 (re-search-forward notmuch-wash-signature-regexp nil t))
261 (let* ((sig-start (match-beginning 0))
262 (sig-end (match-end 0))
263 (sig-lines (count-lines sig-start (point-max))))
264 (if (<= sig-lines notmuch-wash-signature-lines-max)
265 (let ((sig-start-marker (make-marker))
266 (sig-end-marker (make-marker)))
267 (set-marker sig-start-marker sig-start)
268 (set-marker sig-end-marker (point-max))
269 (overlay-put (make-overlay sig-start-marker sig-end-marker)
270 'face 'message-cited-text)
271 (notmuch-wash-region-to-button
272 msg sig-start-marker sig-end-marker
277 (defun notmuch-wash-elide-blank-lines (msg depth)
278 "Elide leading, trailing and successive blank lines."
280 ;; Algorithm derived from `article-strip-multiple-blank-lines' in
283 ;; Make all blank lines empty.
284 (goto-char (point-min))
285 (while (re-search-forward "^[[:space:]\t]+$" nil t)
286 (replace-match "" nil t))
288 ;; Replace multiple empty lines with a single empty line.
289 (goto-char (point-min))
290 (while (re-search-forward "^\n\\(\n+\\)" nil t)
291 (delete-region (match-beginning 1) (match-end 1)))
293 ;; Remove a leading blank line.
294 (goto-char (point-min))
295 (if (looking-at "\n")
296 (delete-region (match-beginning 0) (match-end 0)))
298 ;; Remove a trailing blank line.
299 (goto-char (point-max))
300 (if (looking-at "\n")
301 (delete-region (match-beginning 0) (match-end 0))))
305 (defun notmuch-wash-tidy-citations (msg depth)
306 "Improve the display of cited regions of a message.
308 Perform several transformations on the message body:
310 - Remove lines of repeated citation leaders with no other
312 - Remove citation leaders standing alone before a block of cited
314 - Remove citation trailers standing alone after a block of cited
317 ;; Remove lines of repeated citation leaders with no other content.
318 (goto-char (point-min))
319 (while (re-search-forward "\\(^>[> ]*\n\\)\\{2,\\}" nil t)
320 (replace-match "\\1"))
322 ;; Remove citation leaders standing alone before a block of cited
324 (goto-char (point-min))
325 (while (re-search-forward "\\(\n\\|^[^>].*\\)\n\\(^>[> ]*\n\\)" nil t)
326 (replace-match "\\1\n"))
328 ;; Remove citation trailers standing alone after a block of cited
330 (goto-char (point-min))
331 (while (re-search-forward "\\(^>[> ]*\n\\)\\(^$\\|^[^>].*\\)" nil t)
332 (replace-match "\\2")))
336 (defun notmuch-wash-wrap-long-lines (msg depth)
337 "Wrap long lines in the message.
339 If `notmuch-wash-wrap-lines-length' is a number, this will wrap
340 the message lines to the minimum of the width of the window or
341 its value. Otherwise, this function will wrap long lines in the
342 message at the window width. When doing so, citation leaders in
343 the wrapped text are maintained."
345 (let* ((coolj-wrap-follows-window-size nil)
346 (indent (* depth notmuch-show-indent-messages-width))
347 (limit (if (numberp notmuch-wash-wrap-lines-length)
348 (min (+ notmuch-wash-wrap-lines-length indent)
351 (fill-column (- limit
353 ;; 2 to avoid poor interaction with
356 (coolj-wrap-region (point-min) (point-max))))
362 (defvar diff-file-header-re) ; From `diff-mode.el'.
364 (defun notmuch-wash-subject-to-filename (subject &optional maxlen)
365 "Convert a mail SUBJECT into a filename.
367 The resulting filename is similar to the names generated by \"git
368 format-patch\", without the leading patch sequence number
369 \"0001-\" and \".patch\" extension. Any leading \"[PREFIX]\"
370 style strings are removed prior to conversion.
372 Optional argument MAXLEN is the maximum length of the resulting
373 filename, before trimming any trailing . and - characters."
374 (let* ((s (replace-regexp-in-string "^ *\\(\\[[^]]*\\] *\\)*" "" subject))
375 (s (replace-regexp-in-string "[^A-Za-z0-9._]+" "-" s))
376 (s (replace-regexp-in-string "\\.+" "." s))
377 (s (if maxlen (substring s 0 (min (length s) maxlen)) s))
378 (s (replace-regexp-in-string "[.-]*$" "" s)))
381 (defun notmuch-wash-subject-to-patch-sequence-number (subject)
382 "Convert a patch mail SUBJECT into a patch sequence number.
384 Return the patch sequence number N from the last \"[PATCH N/M]\"
385 style prefix in SUBJECT, or nil if such a prefix can't be found."
387 "^ *\\(\\[[^]]*\\] *\\)*\\[[^]]*?\\([0-9]+\\)/[0-9]+[^]]*\\].*"
389 (string-to-number (substring subject (match-beginning 2) (match-end 2)))))
391 (defun notmuch-wash-subject-to-patch-filename (subject)
392 "Convert a patch mail SUBJECT into a filename.
394 The resulting filename is similar to the names generated by \"git
395 format-patch\". If the patch mail was generated and sent using
396 \"git format-patch/send-email\", this should re-create the
397 original filename the sender had."
398 (format "%04d-%s.patch"
399 (or (notmuch-wash-subject-to-patch-sequence-number subject) 1)
400 (notmuch-wash-subject-to-filename subject 52)))
402 (defun notmuch-wash-convert-inline-patch-to-part (msg depth)
403 "Convert an inline patch into a fake 'text/x-diff' attachment.
405 Given that this function guesses whether a buffer includes a
406 patch and then guesses the extent of the patch, there is scope
409 (goto-char (point-min))
410 (when (re-search-forward diff-file-header-re nil t)
411 (beginning-of-line -1)
412 (let ((patch-start (point))
413 (patch-end (point-max))
415 (goto-char patch-start)
417 ;; Patch ends with signature.
418 (re-search-forward notmuch-wash-signature-regexp nil t)
419 ;; Patch ends with bugtraq comment.
420 (re-search-forward "^\\*\\*\\* " nil t))
421 (setq patch-end (match-beginning 0)))
423 (narrow-to-region patch-start patch-end)
424 (setq part (plist-put part :content-type "inline patch"))
425 (setq part (plist-put part :content (buffer-string)))
426 (setq part (plist-put part :id -1))
427 (setq part (plist-put part :filename
428 (notmuch-wash-subject-to-patch-filename
430 (plist-get msg :headers) :Subject))))
431 (delete-region (point-min) (point-max))
432 (notmuch-show-insert-bodypart nil part depth)))))
436 (provide 'notmuch-wash)
438 ;;; notmuch-wash.el ends here