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))
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-signature-button-format
39 "[ %d-line signature. Click/Enter to toggle visibility. ]"
40 "String used to construct button text for hidden signatures.
41 Can use up to one integer format parameter, i.e. %d")
43 (defvar notmuch-wash-citation-button-format
44 "[ %d more citation lines. Click/Enter to toggle visibility. ]"
45 "String used to construct button text for hidden citations.
46 Can use up to one integer format parameter, i.e. %d")
48 (defvar notmuch-wash-signature-lines-max 12
49 "Maximum length of signature that will be hidden by default.")
51 (defvar notmuch-wash-citation-lines-prefix 3
52 "Always show at least this many lines from the start of a citation.
54 If there is one more line than the sum of
55 `notmuch-wash-citation-lines-prefix' and
56 `notmuch-wash-citation-lines-suffix', show that, otherwise
57 collapse the remaining lines into a button.")
59 (defvar notmuch-wash-citation-lines-suffix 3
60 "Always show at least this many lines from the end of a citation.
62 If there is one more line than the sum of
63 `notmuch-wash-citation-lines-prefix' and
64 `notmuch-wash-citation-lines-suffix', show that, otherwise
65 collapse the remaining lines into a button.")
67 (defun notmuch-wash-toggle-invisible-action (cite-button)
68 (let ((invis-spec (button-get cite-button 'invisibility-spec)))
69 (if (invisible-p invis-spec)
70 (remove-from-invisibility-spec invis-spec)
71 (add-to-invisibility-spec invis-spec)))
75 (define-button-type 'notmuch-wash-button-invisibility-toggle-type
76 'action 'notmuch-wash-toggle-invisible-action
78 'face 'font-lock-comment-face)
80 (define-button-type 'notmuch-wash-button-citation-toggle-type
81 'help-echo "mouse-1, RET: Show citation"
82 :supertype 'notmuch-wash-button-invisibility-toggle-type)
84 (define-button-type 'notmuch-wash-button-signature-toggle-type
85 'help-echo "mouse-1, RET: Show signature"
86 :supertype 'notmuch-wash-button-invisibility-toggle-type)
88 (defun notmuch-wash-region-isearch-show (overlay)
89 (remove-from-invisibility-spec (overlay-get overlay 'invisible)))
91 (defun notmuch-wash-region-to-button (beg end type prefix button-text)
92 "Auxilary function to do the actual making of overlays and buttons
94 BEG and END are buffer locations. TYPE should a string, either
95 \"citation\" or \"signature\". PREFIX is some arbitrary text to
96 insert before the button, probably for indentation. BUTTON-TEXT
97 is what to put on the button."
99 ;; This uses some slightly tricky conversions between strings and
100 ;; symbols because of the way the button code works. Note that
101 ;; replacing intern-soft with make-symbol will cause this to fail,
102 ;; since the newly created symbol has no plist.
104 (let ((overlay (make-overlay beg end))
105 (invis-spec (make-symbol (concat "notmuch-" type "-region")))
106 (button-type (intern-soft (concat "notmuch-wash-button-"
107 type "-toggle-type"))))
108 (add-to-invisibility-spec invis-spec)
109 (overlay-put overlay 'invisible invis-spec)
110 (overlay-put overlay 'isearch-open-invisible #'notmuch-wash-region-isearch-show)
115 (insert-button button-text
116 'invisibility-spec invis-spec
117 :type button-type))))
119 (defun notmuch-wash-excerpt-citations (depth)
120 "Excerpt citations and up to one signature."
121 (goto-char (point-min))
123 (while (and (< (point) (point-max))
124 (re-search-forward notmuch-wash-citation-regexp nil t))
125 (let* ((cite-start (match-beginning 0))
126 (cite-end (match-end 0))
127 (cite-lines (count-lines cite-start cite-end)))
128 (overlay-put (make-overlay cite-start cite-end) 'face 'message-cited-text-face)
129 (when (> cite-lines (+ notmuch-wash-citation-lines-prefix
130 notmuch-wash-citation-lines-suffix
132 (goto-char cite-start)
133 (forward-line notmuch-wash-citation-lines-prefix)
134 (let ((hidden-start (point-marker)))
136 (forward-line (- notmuch-wash-citation-lines-suffix))
137 (notmuch-wash-region-to-button
138 hidden-start (point-marker)
140 (format notmuch-wash-citation-button-format
142 notmuch-wash-citation-lines-prefix
143 notmuch-wash-citation-lines-suffix)))))))
144 (if (and (not (eobp))
145 (re-search-forward notmuch-wash-signature-regexp nil t))
146 (let* ((sig-start (match-beginning 0))
147 (sig-end (match-end 0))
148 (sig-lines (1- (count-lines sig-start (point-max)))))
149 (if (<= sig-lines notmuch-wash-signature-lines-max)
150 (let ((sig-start-marker (make-marker))
151 (sig-end-marker (make-marker)))
152 (set-marker sig-start-marker sig-start)
153 (set-marker sig-end-marker (point-max))
154 (overlay-put (make-overlay sig-start-marker sig-end-marker) 'face 'message-cited-text-face)
155 (notmuch-wash-region-to-button
156 sig-start-marker sig-end-marker
158 (format notmuch-wash-signature-button-format sig-lines)))))))
162 (defun notmuch-wash-elide-blank-lines (depth)
163 "Elide leading, trailing and successive blank lines."
165 ;; Algorithm derived from `article-strip-multiple-blank-lines' in
168 ;; Make all blank lines empty.
169 (goto-char (point-min))
170 (while (re-search-forward "^[[:space:]\t]+$" nil t)
171 (replace-match "" nil t))
173 ;; Replace multiple empty lines with a single empty line.
174 (goto-char (point-min))
175 (while (re-search-forward "^\n\\(\n+\\)" nil t)
176 (delete-region (match-beginning 1) (match-end 1)))
178 ;; Remove a leading blank line.
179 (goto-char (point-min))
180 (if (looking-at "\n")
181 (delete-region (match-beginning 0) (match-end 0)))
183 ;; Remove a trailing blank line.
184 (goto-char (point-max))
185 (if (looking-at "\n")
186 (delete-region (match-beginning 0) (match-end 0))))
190 (defun notmuch-wash-tidy-citations (depth)
191 "Improve the display of cited regions of a message.
193 Perform four transformations on the message body:
195 - Remove lines of repeated citation leaders with no other
197 - Remove citation leaders standing alone before a block of cited
199 - Remove citation trailers standing alone after a block of cited
202 ;; Remove lines of repeated citation leaders with no other content.
203 (goto-char (point-min))
204 (while (re-search-forward "\\(^>[> ]*\n\\)\\{2,\\}" nil t)
205 (replace-match "\\1"))
207 ;; Remove citation leaders standing alone before a block of cited
209 (goto-char (point-min))
210 (while (re-search-forward "\\(\n\\|^[^>].*\\)\n\\(^>[> ]*\n\\)" nil t)
211 (replace-match "\\1\n"))
213 ;; Remove citation trailers standing alone after a block of cited
215 (goto-char (point-min))
216 (while (re-search-forward "\\(^>[> ]*\n\\)\\(^$\\|^[^>].*\\)" nil t)
217 (replace-match "\\2"))
219 ;; Insert a blank line before a citation if there isn't one.
220 (goto-char (point-min))
221 (while (re-search-forward "\\(^[^>]+\\)\n>" nil t)
222 (replace-match "\\1\n\n>"))
224 ;; Insert a blank line after a citation if there isn't one.
225 (goto-char (point-min))
226 (while (re-search-forward "\\(^>.+\\)\n\\([^>]\\)" nil t)
227 (replace-match "\\1\n\n\\2")))
231 (defun notmuch-wash-wrap-long-lines (depth)
232 "Wrap any long lines in the message to the width of the window.
234 When doing so, maintaining citation leaders in the wrapped text."
236 (let ((coolj-wrap-follows-window-size nil)
237 (fill-column (- (window-width)
239 ;; 2 to avoid poor interaction with
242 (coolj-wrap-region (point-min) (point-max))))
248 (defvar diff-file-header-re) ; From `diff-mode.el'.
250 (defun notmuch-wash-convert-inline-patch-to-part (depth)
251 "Convert an inline patch into a fake 'text/x-diff' attachment.
253 Given that this function guesses whether a buffer includes a
254 patch and then guesses the extent of the patch, there is scope
257 (goto-char (point-min))
258 (if (re-search-forward diff-file-header-re nil t)
260 (beginning-of-line -1)
261 (let ((patch-start (point))
262 (patch-end (point-max))
264 (goto-char patch-start)
266 ;; Patch ends with signature.
267 (re-search-forward notmuch-wash-signature-regexp nil t)
268 ;; Patch ends with bugtraq comment.
269 (re-search-forward "^\\*\\*\\* " nil t))
270 (setq patch-end (match-beginning 0)))
272 (narrow-to-region patch-start patch-end)
273 (setq part (plist-put part :content-type "text/x-diff"))
274 (setq part (plist-put part :content (buffer-string)))
275 (setq part (plist-put part :id -1))
276 (setq part (plist-put part :filename "inline patch"))
277 (delete-region (point-min) (point-max))
278 (notmuch-show-insert-bodypart nil part depth))))))
282 (provide 'notmuch-wash)