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>
26 (declare-function notmuch-show-insert-bodypart "notmuch-show" (msg part depth &optional hide))
30 (defvar notmuch-wash-signature-regexp
32 "Pattern to match a line that separates content from signature.")
34 (defvar notmuch-wash-citation-regexp
35 "\\(^[[:space:]]*>.*\n\\)+"
36 "Pattern to match citation lines.")
38 (defvar notmuch-wash-original-regexp "^\\(--+\s?[oO]riginal [mM]essage\s?--+\\)$"
39 "Pattern to match a line that separates original message from reply in top-posted message.")
41 (defvar notmuch-wash-button-signature-hidden-format
42 "[ %d-line signature. Click/Enter to show. ]"
43 "String used to construct button text for hidden signatures.
44 Can use up to one integer format parameter, i.e. %d")
46 (defvar notmuch-wash-button-signature-visible-format
47 "[ %d-line signature. Click/Enter to hide. ]"
48 "String used to construct button text for visible signatures.
49 Can use up to one integer format parameter, i.e. %d")
51 (defvar notmuch-wash-button-citation-hidden-format
52 "[ %d more citation lines. Click/Enter to show. ]"
53 "String used to construct button text for hidden citations.
54 Can use up to one integer format parameter, i.e. %d")
56 (defvar notmuch-wash-button-citation-visible-format
57 "[ %d more citation lines. Click/Enter to hide. ]"
58 "String used to construct button text for visible citations.
59 Can use up to one integer format parameter, i.e. %d")
61 (defvar notmuch-wash-button-original-hidden-format
62 "[ %d-line hidden original message. Click/Enter to show. ]"
63 "String used to construct button text for hidden citations.
64 Can use up to one integer format parameter, i.e. %d")
66 (defvar notmuch-wash-button-original-visible-format
67 "[ %d-line original message. Click/Enter to hide. ]"
68 "String used to construct button text for visible citations.
69 Can use up to one integer format parameter, i.e. %d")
71 (defvar notmuch-wash-signature-lines-max 12
72 "Maximum length of signature that will be hidden by default.")
74 (defvar notmuch-wash-citation-lines-prefix 3
75 "Always show at least this many lines from the start of a citation.
77 If there is one more line than the sum of
78 `notmuch-wash-citation-lines-prefix' and
79 `notmuch-wash-citation-lines-suffix', show that, otherwise
80 collapse the remaining lines into a button.")
82 (defvar notmuch-wash-citation-lines-suffix 3
83 "Always show at least this many lines from the end of a citation.
85 If there is one more line than the sum of
86 `notmuch-wash-citation-lines-prefix' and
87 `notmuch-wash-citation-lines-suffix', show that, otherwise
88 collapse the remaining lines into a button.")
90 (defvar notmuch-wash-wrap-lines-length nil
91 "Wrap line after at most this many characters.
93 If this is nil, lines in messages will be wrapped to fit in the
94 current window. If this is a number, lines will be wrapped after
95 this many characters or at the window width (whichever one is
98 (defun notmuch-wash-toggle-invisible-action (cite-button)
99 ;; Toggle overlay visibility
100 (let ((overlay (button-get cite-button 'overlay)))
101 (overlay-put overlay 'invisible (not (overlay-get overlay 'invisible))))
102 ;; Update button text
103 (let* ((new-start (button-start cite-button))
104 (overlay (button-get cite-button 'overlay))
105 (button-label (notmuch-wash-button-label overlay))
107 (properties (text-properties-at (point)))
108 (inhibit-read-only t))
109 (goto-char new-start)
110 (insert button-label)
111 (set-text-properties new-start (point) properties)
112 (let ((old-end (button-end cite-button)))
113 (move-overlay cite-button new-start (point))
114 (delete-region (point) old-end))
115 (goto-char (min old-point (1- (button-end cite-button))))))
117 (define-button-type 'notmuch-wash-button-invisibility-toggle-type
118 'action 'notmuch-wash-toggle-invisible-action
120 'face 'font-lock-comment-face
121 :supertype 'notmuch-button-type)
123 (define-button-type 'notmuch-wash-button-citation-toggle-type
124 'help-echo "mouse-1, RET: Show citation"
125 :supertype 'notmuch-wash-button-invisibility-toggle-type)
127 (define-button-type 'notmuch-wash-button-signature-toggle-type
128 'help-echo "mouse-1, RET: Show signature"
129 :supertype 'notmuch-wash-button-invisibility-toggle-type)
131 (define-button-type 'notmuch-wash-button-original-toggle-type
132 'help-echo "mouse-1, RET: Show original message"
133 :supertype 'notmuch-wash-button-invisibility-toggle-type)
135 (defun notmuch-wash-region-isearch-show (overlay)
136 (notmuch-wash-toggle-invisible-action
137 (overlay-get overlay 'notmuch-wash-button)))
139 (defun notmuch-wash-button-label (overlay)
140 (let* ((type (overlay-get overlay 'type))
141 (invis-spec (overlay-get overlay 'invisible))
142 (state (if (invisible-p invis-spec) "hidden" "visible"))
143 (label-format (symbol-value (intern-soft (concat "notmuch-wash-button-"
144 type "-" state "-format"))))
145 (lines-count (count-lines (overlay-start overlay) (overlay-end overlay))))
146 (format label-format lines-count)))
148 (defun notmuch-wash-region-to-button (msg beg end type &optional prefix)
149 "Auxiliary function to do the actual making of overlays and buttons
151 BEG and END are buffer locations. TYPE should a string, either
152 \"citation\" or \"signature\". Optional PREFIX is some arbitrary
153 text to insert before the button, probably for indentation. Note
154 that PREFIX should not include a newline."
156 ;; This uses some slightly tricky conversions between strings and
157 ;; symbols because of the way the button code works. Note that
158 ;; replacing intern-soft with make-symbol will cause this to fail,
159 ;; since the newly created symbol has no plist.
161 (let ((overlay (make-overlay beg end))
162 (button-type (intern-soft (concat "notmuch-wash-button-"
163 type "-toggle-type"))))
164 (overlay-put overlay 'invisible t)
165 (overlay-put overlay 'isearch-open-invisible #'notmuch-wash-region-isearch-show)
166 (overlay-put overlay 'type type)
171 (insert-before-markers prefix))
172 (let ((button-beg (point)))
173 (insert-before-markers (notmuch-wash-button-label overlay) "\n")
174 (let ((button (make-button button-beg (1- (point))
177 (overlay-put overlay 'notmuch-wash-button button))))))
179 (defun notmuch-wash-excerpt-citations (msg depth)
180 "Excerpt citations and up to one signature."
181 (goto-char (point-min))
183 (if (and (< (point) (point-max))
184 (re-search-forward notmuch-wash-original-regexp nil t))
185 (let* ((msg-start (match-beginning 0))
186 (msg-end (point-max))
187 (msg-lines (count-lines msg-start msg-end)))
188 (notmuch-wash-region-to-button
189 msg msg-start msg-end "original")))
190 (while (and (< (point) (point-max))
191 (re-search-forward notmuch-wash-citation-regexp nil t))
192 (let* ((cite-start (match-beginning 0))
193 (cite-end (match-end 0))
194 (cite-lines (count-lines cite-start cite-end)))
195 (overlay-put (make-overlay cite-start cite-end) 'face 'message-cited-text)
196 (when (> cite-lines (+ notmuch-wash-citation-lines-prefix
197 notmuch-wash-citation-lines-suffix
199 (goto-char cite-start)
200 (forward-line notmuch-wash-citation-lines-prefix)
201 (let ((hidden-start (point-marker)))
203 (forward-line (- notmuch-wash-citation-lines-suffix))
204 (notmuch-wash-region-to-button
205 msg hidden-start (point-marker)
207 (if (and (not (eobp))
208 (re-search-forward notmuch-wash-signature-regexp nil t))
209 (let* ((sig-start (match-beginning 0))
210 (sig-end (match-end 0))
211 (sig-lines (count-lines sig-start (point-max))))
212 (if (<= sig-lines notmuch-wash-signature-lines-max)
213 (let ((sig-start-marker (make-marker))
214 (sig-end-marker (make-marker)))
215 (set-marker sig-start-marker sig-start)
216 (set-marker sig-end-marker (point-max))
217 (overlay-put (make-overlay sig-start-marker sig-end-marker) 'face 'message-cited-text)
218 (notmuch-wash-region-to-button
219 msg sig-start-marker sig-end-marker
224 (defun notmuch-wash-elide-blank-lines (msg depth)
225 "Elide leading, trailing and successive blank lines."
227 ;; Algorithm derived from `article-strip-multiple-blank-lines' in
230 ;; Make all blank lines empty.
231 (goto-char (point-min))
232 (while (re-search-forward "^[[:space:]\t]+$" nil t)
233 (replace-match "" nil t))
235 ;; Replace multiple empty lines with a single empty line.
236 (goto-char (point-min))
237 (while (re-search-forward "^\n\\(\n+\\)" nil t)
238 (delete-region (match-beginning 1) (match-end 1)))
240 ;; Remove a leading blank line.
241 (goto-char (point-min))
242 (if (looking-at "\n")
243 (delete-region (match-beginning 0) (match-end 0)))
245 ;; Remove a trailing blank line.
246 (goto-char (point-max))
247 (if (looking-at "\n")
248 (delete-region (match-beginning 0) (match-end 0))))
252 (defun notmuch-wash-tidy-citations (msg depth)
253 "Improve the display of cited regions of a message.
255 Perform several transformations on the message body:
257 - Remove lines of repeated citation leaders with no other
259 - Remove citation leaders standing alone before a block of cited
261 - Remove citation trailers standing alone after a block of cited
264 ;; Remove lines of repeated citation leaders with no other content.
265 (goto-char (point-min))
266 (while (re-search-forward "\\(^>[> ]*\n\\)\\{2,\\}" nil t)
267 (replace-match "\\1"))
269 ;; Remove citation leaders standing alone before a block of cited
271 (goto-char (point-min))
272 (while (re-search-forward "\\(\n\\|^[^>].*\\)\n\\(^>[> ]*\n\\)" nil t)
273 (replace-match "\\1\n"))
275 ;; Remove citation trailers standing alone after a block of cited
277 (goto-char (point-min))
278 (while (re-search-forward "\\(^>[> ]*\n\\)\\(^$\\|^[^>].*\\)" nil t)
279 (replace-match "\\2")))
283 (defun notmuch-wash-wrap-long-lines (msg depth)
284 "Wrap long lines in the message.
286 If `notmuch-wash-wrap-lines-length' is a number, this will wrap
287 the message lines to the minimum of the width of the window or
288 its value. Otherwise, this function will wrap long lines in the
289 message at the window width. When doing so, citation leaders in
290 the wrapped text are maintained."
292 (let* ((coolj-wrap-follows-window-size nil)
293 (limit (if (numberp notmuch-wash-wrap-lines-length)
294 (min notmuch-wash-wrap-lines-length
297 (fill-column (- limit
299 ;; 2 to avoid poor interaction with
302 (coolj-wrap-region (point-min) (point-max))))
308 (defvar diff-file-header-re) ; From `diff-mode.el'.
310 (defun notmuch-wash-subject-to-filename (subject &optional maxlen)
311 "Convert a mail SUBJECT into a filename.
313 The resulting filename is similar to the names generated by \"git
314 format-patch\", without the leading patch sequence number
315 \"0001-\" and \".patch\" extension. Any leading \"[PREFIX]\"
316 style strings are removed prior to conversion.
318 Optional argument MAXLEN is the maximum length of the resulting
319 filename, before trimming any trailing . and - characters."
320 (let* ((s (replace-regexp-in-string "^ *\\(\\[[^]]*\\] *\\)*" "" subject))
321 (s (replace-regexp-in-string "[^A-Za-z0-9._]+" "-" s))
322 (s (replace-regexp-in-string "\\.+" "." s))
323 (s (if maxlen (substring s 0 (min (length s) maxlen)) s))
324 (s (replace-regexp-in-string "[.-]*$" "" s)))
327 (defun notmuch-wash-subject-to-patch-sequence-number (subject)
328 "Convert a patch mail SUBJECT into a patch sequence number.
330 Return the patch sequence number N from the last \"[PATCH N/M]\"
331 style prefix in SUBJECT, or nil if such a prefix can't be found."
333 "^ *\\(\\[[^]]*\\] *\\)*\\[[^]]*?\\([0-9]+\\)/[0-9]+[^]]*\\].*"
335 (string-to-number (substring subject (match-beginning 2) (match-end 2)))))
337 (defun notmuch-wash-subject-to-patch-filename (subject)
338 "Convert a patch mail SUBJECT into a filename.
340 The resulting filename is similar to the names generated by \"git
341 format-patch\". If the patch mail was generated and sent using
342 \"git format-patch/send-email\", this should re-create the
343 original filename the sender had."
344 (format "%04d-%s.patch"
345 (or (notmuch-wash-subject-to-patch-sequence-number subject) 1)
346 (notmuch-wash-subject-to-filename subject 52)))
348 (defun notmuch-wash-convert-inline-patch-to-part (msg depth)
349 "Convert an inline patch into a fake 'text/x-diff' attachment.
351 Given that this function guesses whether a buffer includes a
352 patch and then guesses the extent of the patch, there is scope
355 (goto-char (point-min))
356 (when (re-search-forward diff-file-header-re nil t)
357 (beginning-of-line -1)
358 (let ((patch-start (point))
359 (patch-end (point-max))
361 (goto-char patch-start)
363 ;; Patch ends with signature.
364 (re-search-forward notmuch-wash-signature-regexp nil t)
365 ;; Patch ends with bugtraq comment.
366 (re-search-forward "^\\*\\*\\* " nil t))
367 (setq patch-end (match-beginning 0)))
369 (narrow-to-region patch-start patch-end)
370 (setq part (plist-put part :content-type "inline patch"))
371 (setq part (plist-put part :content (buffer-string)))
372 (setq part (plist-put part :id -1))
373 (setq part (plist-put part :filename
374 (notmuch-wash-subject-to-patch-filename
376 (plist-get msg :headers) :Subject))))
377 (delete-region (point-min) (point-max))
378 (notmuch-show-insert-bodypart nil part depth)))))
382 (provide 'notmuch-wash)