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-isearch-show (overlay)
81 (remove-from-invisibility-spec (overlay-get overlay 'invisible)))
83 (defun notmuch-wash-region-to-button (beg end type prefix button-text)
84 "Auxilary function to do the actual making of overlays and buttons
86 BEG and END are buffer locations. TYPE should a string, either
87 \"citation\" or \"signature\". PREFIX is some arbitrary text to
88 insert before the button, probably for indentation. BUTTON-TEXT
89 is what to put on the button."
91 ;; This uses some slightly tricky conversions between strings and
92 ;; symbols because of the way the button code works. Note that
93 ;; replacing intern-soft with make-symbol will cause this to fail,
94 ;; since the newly created symbol has no plist.
96 (let ((overlay (make-overlay beg end))
97 (invis-spec (make-symbol (concat "notmuch-" type "-region")))
98 (button-type (intern-soft (concat "notmuch-wash-button-"
99 type "-toggle-type"))))
100 (add-to-invisibility-spec invis-spec)
101 (overlay-put overlay 'invisible invis-spec)
102 (overlay-put overlay 'isearch-open-invisible #'notmuch-wash-region-isearch-show)
107 (insert-button button-text
108 'invisibility-spec invis-spec
109 :type button-type))))
111 (defun notmuch-wash-text/plain-citations (depth)
112 "Markup citations, and up to one signature in the buffer."
113 (goto-char (point-min))
115 (while (and (< (point) (point-max))
116 (re-search-forward notmuch-wash-citation-regexp nil t))
117 (let* ((cite-start (match-beginning 0))
118 (cite-end (match-end 0))
119 (cite-lines (count-lines cite-start cite-end)))
120 (overlay-put (make-overlay cite-start cite-end) 'face 'message-cited-text-face)
121 (when (> cite-lines (+ notmuch-wash-citation-lines-prefix
122 notmuch-wash-citation-lines-suffix
124 (goto-char cite-start)
125 (forward-line notmuch-wash-citation-lines-prefix)
126 (let ((hidden-start (point-marker)))
128 (forward-line (- notmuch-wash-citation-lines-suffix))
129 (notmuch-wash-region-to-button
130 hidden-start (point-marker)
132 (format notmuch-wash-citation-button-format
134 notmuch-wash-citation-lines-prefix
135 notmuch-wash-citation-lines-suffix)))))))
136 (if (and (not (eobp))
137 (re-search-forward notmuch-wash-signature-regexp nil t))
138 (let* ((sig-start (match-beginning 0))
139 (sig-end (match-end 0))
140 (sig-lines (1- (count-lines sig-start (point-max)))))
141 (if (<= sig-lines notmuch-wash-signature-lines-max)
142 (let ((sig-start-marker (make-marker))
143 (sig-end-marker (make-marker)))
144 (set-marker sig-start-marker sig-start)
145 (set-marker sig-end-marker (point-max))
146 (overlay-put (make-overlay sig-start-marker sig-end-marker) 'face 'message-cited-text-face)
147 (notmuch-wash-region-to-button
148 sig-start-marker sig-end-marker
150 (format notmuch-wash-signature-button-format sig-lines)))))))
154 (provide 'notmuch-wash)