1 ;; notmuch-wash.el --- cleaning up message bodies
3 ;; Copyright © Carl Worth
5 ;; This file is part of Notmuch.
7 ;; Notmuch is free software: you can redistribute it and/or modify it
8 ;; under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; Notmuch is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;; General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with Notmuch. If not, see <http://www.gnu.org/licenses/>.
20 ;; Authors: Carl Worth <cworth@cworth.org>
22 (defvar notmuch-wash-signature-regexp
24 "Pattern to match a line that separates content from signature.")
26 (defvar notmuch-wash-citation-regexp
27 "\\(^[[:space:]]*>.*\n\\)+"
28 "Pattern to match citation lines.")
30 (defvar notmuch-wash-signature-button-format
31 "[ %d-line signature. Click/Enter to toggle visibility. ]"
32 "String used to construct button text for hidden signatures.
33 Can use up to one integer format parameter, i.e. %d")
35 (defvar notmuch-wash-citation-button-format
36 "[ %d more citation lines. Click/Enter to toggle visibility. ]"
37 "String used to construct button text for hidden citations.
38 Can use up to one integer format parameter, i.e. %d")
40 (defvar notmuch-wash-signature-lines-max 12
41 "Maximum length of signature that will be hidden by default.")
43 (defvar notmuch-wash-citation-lines-prefix 3
44 "Always show at least this many lines from the start of a citation.
46 If there is one more line than the sum of
47 `notmuch-wash-citation-lines-prefix' and
48 `notmuch-wash-citation-lines-suffix', show that, otherwise
49 collapse the remaining lines into a button.")
51 (defvar notmuch-wash-citation-lines-suffix 3
52 "Always show at least this many lines from the end 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 (defun notmuch-wash-toggle-invisible-action (cite-button)
60 (let ((invis-spec (button-get cite-button 'invisibility-spec)))
61 (if (invisible-p invis-spec)
62 (remove-from-invisibility-spec invis-spec)
63 (add-to-invisibility-spec invis-spec)))
67 (define-button-type 'notmuch-wash-button-invisibility-toggle-type
68 'action 'notmuch-wash-toggle-invisible-action
70 'face 'font-lock-comment-face)
72 (define-button-type 'notmuch-wash-button-citation-toggle-type
73 'help-echo "mouse-1, RET: Show citation"
74 :supertype 'notmuch-wash-button-invisibility-toggle-type)
76 (define-button-type 'notmuch-wash-button-signature-toggle-type
77 'help-echo "mouse-1, RET: Show signature"
78 :supertype 'notmuch-wash-button-invisibility-toggle-type)
80 (defun notmuch-wash-region-to-button (beg end type prefix button-text)
81 "Auxilary function to do the actual making of overlays and buttons
83 BEG and END are buffer locations. TYPE should a string, either
84 \"citation\" or \"signature\". PREFIX is some arbitrary text to
85 insert before the button, probably for indentation. BUTTON-TEXT
86 is what to put on the button."
88 ;; This uses some slightly tricky conversions between strings and
89 ;; symbols because of the way the button code works. Note that
90 ;; replacing intern-soft with make-symbol will cause this to fail,
91 ;; since the newly created symbol has no plist.
93 (let ((overlay (make-overlay beg end))
94 (invis-spec (make-symbol (concat "notmuch-" type "-region")))
95 (button-type (intern-soft (concat "notmuch-wash-button-"
96 type "-toggle-type"))))
97 (add-to-invisibility-spec invis-spec)
98 (overlay-put overlay 'invisible invis-spec)
103 (insert-button button-text
104 'invisibility-spec invis-spec
105 :type button-type))))
107 (defun notmuch-wash-text/plain-citations (depth)
108 "Markup citations, and up to one signature in the buffer."
109 (goto-char (point-min))
111 (while (and (< (point) (point-max))
112 (re-search-forward notmuch-wash-citation-regexp nil t))
113 (let* ((cite-start (match-beginning 0))
114 (cite-end (match-end 0))
115 (cite-lines (count-lines cite-start cite-end)))
116 (overlay-put (make-overlay cite-start cite-end) 'face 'message-cited-text-face)
117 (when (> cite-lines (+ notmuch-wash-citation-lines-prefix
118 notmuch-wash-citation-lines-suffix
120 (goto-char cite-start)
121 (forward-line notmuch-wash-citation-lines-prefix)
122 (let ((hidden-start (point-marker)))
124 (forward-line (- notmuch-wash-citation-lines-suffix))
125 (notmuch-wash-region-to-button
126 hidden-start (point-marker)
128 (format notmuch-wash-citation-button-format
130 notmuch-wash-citation-lines-prefix
131 notmuch-wash-citation-lines-suffix)))))))
132 (if (and (not (eobp))
133 (re-search-forward notmuch-wash-signature-regexp nil t))
134 (let* ((sig-start (match-beginning 0))
135 (sig-end (match-end 0))
136 (sig-lines (1- (count-lines sig-start (point-max)))))
137 (if (<= sig-lines notmuch-wash-signature-lines-max)
138 (let ((sig-start-marker (make-marker))
139 (sig-end-marker (make-marker)))
140 (set-marker sig-start-marker sig-start)
141 (set-marker sig-end-marker (point-max))
142 (overlay-put (make-overlay sig-start-marker sig-end-marker) 'face 'message-cited-text-face)
143 (notmuch-wash-region-to-button
144 sig-start-marker sig-end-marker
146 (format notmuch-wash-signature-button-format sig-lines)))))))
150 (provide 'notmuch-wash)