]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-wash.el
Merge branch '0.3.x' immediately after the 0.3.1 release
[notmuch] / emacs / notmuch-wash.el
1 ;; notmuch-wash.el --- cleaning up message bodies
2 ;;
3 ;; Copyright © Carl Worth
4 ;; Copyright © David Edmondson
5 ;;
6 ;; This file is part of Notmuch.
7 ;;
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.
12 ;;
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.
17 ;;
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/>.
20 ;;
21 ;; Authors: Carl Worth <cworth@cworth.org>
22 ;;          David Edmondson <dme@dme.org>
23
24 (require 'coolj)
25
26 (declare-function notmuch-show-insert-bodypart "notmuch-show" (msg part depth))
27
28 ;;
29
30 (defvar notmuch-wash-signature-regexp
31   "^\\(-- ?\\|_+\\)$"
32   "Pattern to match a line that separates content from signature.")
33
34 (defvar notmuch-wash-citation-regexp
35   "\\(^[[:space:]]*>.*\n\\)+"
36   "Pattern to match citation lines.")
37
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")
42
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")
47
48 (defvar notmuch-wash-signature-lines-max 12
49   "Maximum length of signature that will be hidden by default.")
50
51 (defvar notmuch-wash-citation-lines-prefix 3
52   "Always show at least this many lines from the start of a citation.
53
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.")
58
59 (defvar notmuch-wash-citation-lines-suffix 3
60   "Always show at least this many lines from the end of a citation.
61
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.")
66
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)))
72   (force-window-update)
73   (redisplay t))
74
75 (define-button-type 'notmuch-wash-button-invisibility-toggle-type
76   'action 'notmuch-wash-toggle-invisible-action
77   'follow-link t
78   'face 'font-lock-comment-face)
79
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)
83
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)
87
88 (defun notmuch-wash-region-isearch-show (overlay)
89   (remove-from-invisibility-spec (overlay-get overlay 'invisible)))
90
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
93
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."
98
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.
103
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)
111     (goto-char (1+ end))
112     (save-excursion
113       (goto-char (1- beg))
114       (insert prefix)
115       (insert-button button-text
116                      'invisibility-spec invis-spec
117                      :type button-type))))
118
119 (defun notmuch-wash-excerpt-citations (depth)
120   "Excerpt citations and up to one signature."
121   (goto-char (point-min))
122   (beginning-of-line)
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
131                              1))
132         (goto-char cite-start)
133         (forward-line notmuch-wash-citation-lines-prefix)
134         (let ((hidden-start (point-marker)))
135           (goto-char cite-end)
136           (forward-line (- notmuch-wash-citation-lines-suffix))
137           (notmuch-wash-region-to-button
138            hidden-start (point-marker)
139            "citation" "\n"
140            (format notmuch-wash-citation-button-format
141                    (- cite-lines
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
157                "signature" "\n"
158                (format notmuch-wash-signature-button-format sig-lines)))))))
159
160 ;;
161
162 (defun notmuch-wash-elide-blank-lines (depth)
163   "Elide leading, trailing and successive blank lines."
164
165   ;; Algorithm derived from `article-strip-multiple-blank-lines' in
166   ;; `gnus-art.el'.
167
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))
172
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)))
177
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)))
182
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))))
187
188 ;;
189
190 (defun notmuch-wash-tidy-citations (depth)
191   "Improve the display of cited regions of a message.
192
193 Perform four transformations on the message body:
194
195 - Remove lines of repeated citation leaders with no other
196   content,
197 - Remove citation leaders standing alone before a block of cited
198   text,
199 - Remove citation trailers standing alone after a block of cited
200   text."
201
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"))
206
207   ;; Remove citation leaders standing alone before a block of cited
208   ;; text.
209   (goto-char (point-min))
210   (while (re-search-forward "\\(\n\\|^[^>].*\\)\n\\(^>[> ]*\n\\)" nil t)
211     (replace-match "\\1\n"))
212
213   ;; Remove citation trailers standing alone after a block of cited
214   ;; text.
215   (goto-char (point-min))
216   (while (re-search-forward "\\(^>[> ]*\n\\)\\(^$\\|^[^>].*\\)" nil t)
217     (replace-match "\\2")))
218
219 ;;
220
221 (defun notmuch-wash-wrap-long-lines (depth)
222   "Wrap any long lines in the message to the width of the window.
223
224 When doing so, maintaining citation leaders in the wrapped text."
225
226   (let ((coolj-wrap-follows-window-size nil)
227         (fill-column (- (window-width)
228                         depth
229                         ;; 2 to avoid poor interaction with
230                         ;; `word-wrap'.
231                         2)))
232     (coolj-wrap-region (point-min) (point-max))))
233
234 ;;
235
236 (require 'diff-mode)
237
238 (defvar diff-file-header-re) ; From `diff-mode.el'.
239
240 (defun notmuch-wash-convert-inline-patch-to-part (depth)
241   "Convert an inline patch into a fake 'text/x-diff' attachment.
242
243 Given that this function guesses whether a buffer includes a
244 patch and then guesses the extent of the patch, there is scope
245 for error."
246
247   (goto-char (point-min))
248   (if (re-search-forward diff-file-header-re nil t)
249       (progn
250         (beginning-of-line -1)
251         (let ((patch-start (point))
252               (patch-end (point-max))
253               part)
254           (goto-char patch-start)
255           (if (or
256                ;; Patch ends with signature.
257                (re-search-forward notmuch-wash-signature-regexp nil t)
258                ;; Patch ends with bugtraq comment.
259                (re-search-forward "^\\*\\*\\* " nil t))
260               (setq patch-end (match-beginning 0)))
261           (save-restriction
262             (narrow-to-region patch-start patch-end)
263             (setq part (plist-put part :content-type "text/x-diff"))
264             (setq part (plist-put part :content (buffer-string)))
265             (setq part (plist-put part :id -1))
266             (setq part (plist-put part :filename "inline patch"))
267             (delete-region (point-min) (point-max))
268             (notmuch-show-insert-bodypart nil part depth))))))
269
270 ;;
271
272 (provide 'notmuch-wash)