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>
28 (defvar notmuch-wash-signature-regexp
30 "Pattern to match a line that separates content from signature.")
32 (defvar notmuch-wash-citation-regexp
33 "\\(^[[:space:]]*>.*\n\\)+"
34 "Pattern to match citation lines.")
36 (defvar notmuch-wash-signature-button-format
37 "[ %d-line signature. Click/Enter to toggle visibility. ]"
38 "String used to construct button text for hidden signatures.
39 Can use up to one integer format parameter, i.e. %d")
41 (defvar notmuch-wash-citation-button-format
42 "[ %d more citation lines. Click/Enter to toggle visibility. ]"
43 "String used to construct button text for hidden citations.
44 Can use up to one integer format parameter, i.e. %d")
46 (defvar notmuch-wash-signature-lines-max 12
47 "Maximum length of signature that will be hidden by default.")
49 (defvar notmuch-wash-citation-lines-prefix 3
50 "Always show at least this many lines from the start of a citation.
52 If there is one more line than the sum of
53 `notmuch-wash-citation-lines-prefix' and
54 `notmuch-wash-citation-lines-suffix', show that, otherwise
55 collapse the remaining lines into a button.")
57 (defvar notmuch-wash-citation-lines-suffix 3
58 "Always show at least this many lines from the end of a citation.
60 If there is one more line than the sum of
61 `notmuch-wash-citation-lines-prefix' and
62 `notmuch-wash-citation-lines-suffix', show that, otherwise
63 collapse the remaining lines into a button.")
65 (defun notmuch-wash-toggle-invisible-action (cite-button)
66 (let ((invis-spec (button-get cite-button 'invisibility-spec)))
67 (if (invisible-p invis-spec)
68 (remove-from-invisibility-spec invis-spec)
69 (add-to-invisibility-spec invis-spec)))
73 (define-button-type 'notmuch-wash-button-invisibility-toggle-type
74 'action 'notmuch-wash-toggle-invisible-action
76 'face 'font-lock-comment-face)
78 (define-button-type 'notmuch-wash-button-citation-toggle-type
79 'help-echo "mouse-1, RET: Show citation"
80 :supertype 'notmuch-wash-button-invisibility-toggle-type)
82 (define-button-type 'notmuch-wash-button-signature-toggle-type
83 'help-echo "mouse-1, RET: Show signature"
84 :supertype 'notmuch-wash-button-invisibility-toggle-type)
86 (defun notmuch-wash-region-isearch-show (overlay)
87 (remove-from-invisibility-spec (overlay-get overlay 'invisible)))
89 (defun notmuch-wash-region-to-button (beg end type prefix button-text)
90 "Auxilary function to do the actual making of overlays and buttons
92 BEG and END are buffer locations. TYPE should a string, either
93 \"citation\" or \"signature\". PREFIX is some arbitrary text to
94 insert before the button, probably for indentation. BUTTON-TEXT
95 is what to put on the button."
97 ;; This uses some slightly tricky conversions between strings and
98 ;; symbols because of the way the button code works. Note that
99 ;; replacing intern-soft with make-symbol will cause this to fail,
100 ;; since the newly created symbol has no plist.
102 (let ((overlay (make-overlay beg end))
103 (invis-spec (make-symbol (concat "notmuch-" type "-region")))
104 (button-type (intern-soft (concat "notmuch-wash-button-"
105 type "-toggle-type"))))
106 (add-to-invisibility-spec invis-spec)
107 (overlay-put overlay 'invisible invis-spec)
108 (overlay-put overlay 'isearch-open-invisible #'notmuch-wash-region-isearch-show)
113 (insert-button button-text
114 'invisibility-spec invis-spec
115 :type button-type))))
117 (defun notmuch-wash-excerpt-citations (depth)
118 "Excerpt citations and up to one signature."
119 (goto-char (point-min))
121 (while (and (< (point) (point-max))
122 (re-search-forward notmuch-wash-citation-regexp nil t))
123 (let* ((cite-start (match-beginning 0))
124 (cite-end (match-end 0))
125 (cite-lines (count-lines cite-start cite-end)))
126 (overlay-put (make-overlay cite-start cite-end) 'face 'message-cited-text-face)
127 (when (> cite-lines (+ notmuch-wash-citation-lines-prefix
128 notmuch-wash-citation-lines-suffix
130 (goto-char cite-start)
131 (forward-line notmuch-wash-citation-lines-prefix)
132 (let ((hidden-start (point-marker)))
134 (forward-line (- notmuch-wash-citation-lines-suffix))
135 (notmuch-wash-region-to-button
136 hidden-start (point-marker)
138 (format notmuch-wash-citation-button-format
140 notmuch-wash-citation-lines-prefix
141 notmuch-wash-citation-lines-suffix)))))))
142 (if (and (not (eobp))
143 (re-search-forward notmuch-wash-signature-regexp nil t))
144 (let* ((sig-start (match-beginning 0))
145 (sig-end (match-end 0))
146 (sig-lines (1- (count-lines sig-start (point-max)))))
147 (if (<= sig-lines notmuch-wash-signature-lines-max)
148 (let ((sig-start-marker (make-marker))
149 (sig-end-marker (make-marker)))
150 (set-marker sig-start-marker sig-start)
151 (set-marker sig-end-marker (point-max))
152 (overlay-put (make-overlay sig-start-marker sig-end-marker) 'face 'message-cited-text-face)
153 (notmuch-wash-region-to-button
154 sig-start-marker sig-end-marker
156 (format notmuch-wash-signature-button-format sig-lines)))))))
160 (defun notmuch-wash-elide-blank-lines (depth)
161 "Elide leading, trailing and successive blank lines."
163 ;; Algorithm derived from `article-strip-multiple-blank-lines' in
166 ;; Make all blank lines empty.
167 (goto-char (point-min))
168 (while (re-search-forward "^[[:space:]\t]+$" nil t)
169 (replace-match "" nil t))
171 ;; Replace multiple empty lines with a single empty line.
172 (goto-char (point-min))
173 (while (re-search-forward "^\n\\(\n+\\)" nil t)
174 (delete-region (match-beginning 1) (match-end 1)))
176 ;; Remove a leading blank line.
177 (goto-char (point-min))
178 (if (looking-at "\n")
179 (delete-region (match-beginning 0) (match-end 0)))
181 ;; Remove a trailing blank line.
182 (goto-char (point-max))
183 (if (looking-at "\n")
184 (delete-region (match-beginning 0) (match-end 0))))
188 (defun notmuch-wash-tidy-citations (depth)
189 "Improve the display of cited regions of a message.
191 Perform four transformations on the message body:
193 - Remove lines of repeated citation leaders with no other
195 - Remove citation leaders standing alone before a block of cited
197 - Remove citation trailers standing alone after a block of cited
200 ;; Remove lines of repeated citation leaders with no other content.
201 (goto-char (point-min))
202 (while (re-search-forward "\\(^>[> ]*\n\\)\\{2,\\}" nil t)
203 (replace-match "\\1"))
205 ;; Remove citation leaders standing alone before a block of cited
207 (goto-char (point-min))
208 (while (re-search-forward "\\(\n\\|^[^>].*\\)\n\\(^>[> ]*\n\\)" nil t)
209 (replace-match "\\1\n"))
211 ;; Remove citation trailers standing alone after a block of cited
213 (goto-char (point-min))
214 (while (re-search-forward "\\(^>[> ]*\n\\)\\(^$\\|^[^>].*\\)" nil t)
215 (replace-match "\\2")))
219 (defun notmuch-wash-wrap-long-lines (depth)
220 "Wrap any long lines in the message to the width of the window.
222 When doing so, maintaining citation leaders in the wrapped text."
224 (let ((coolj-wrap-follows-window-size nil)
225 (fill-column (- (window-width)
227 ;; 2 to avoid poor interaction with
230 (coolj-wrap-region (point-min) (point-max))))
234 (provide 'notmuch-wash)