]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-wash.el
5ca567f59be6ad1cc43a4864253e1e9f44979012
[notmuch] / emacs / notmuch-wash.el
1 ;; notmuch-wash.el --- cleaning up message bodies
2 ;;
3 ;; Copyright © Carl Worth
4 ;;
5 ;; This file is part of Notmuch.
6 ;;
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.
11 ;;
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.
16 ;;
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/>.
19 ;;
20 ;; Authors: Carl Worth <cworth@cworth.org>
21
22 (defvar notmuch-wash-signature-regexp
23   "^\\(-- ?\\|_+\\)$"
24   "Pattern to match a line that separates content from signature.")
25
26 (defvar notmuch-wash-citation-regexp
27   "\\(^[[:space:]]*>.*\n\\)+"
28   "Pattern to match citation lines.")
29
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")
34
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")
39
40 (defvar notmuch-wash-signature-lines-max 12
41   "Maximum length of signature that will be hidden by default.")
42
43 (defvar notmuch-wash-citation-lines-prefix 3
44   "Always show at least this many lines from the start of a citation.
45
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.")
50
51 (defvar notmuch-wash-citation-lines-suffix 3
52   "Always show at least this many lines from the end 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 (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)))
64   (force-window-update)
65   (redisplay t))
66
67 (define-button-type 'notmuch-wash-button-invisibility-toggle-type
68   'action 'notmuch-wash-toggle-invisible-action
69   'follow-link t
70   'face 'font-lock-comment-face)
71
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)
75
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)
79
80 (defun notmuch-wash-region-isearch-show (overlay)
81   (remove-from-invisibility-spec (overlay-get overlay 'invisible)))
82
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
85
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."
90
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.
95
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)
103     (goto-char (1+ end))
104     (save-excursion
105       (goto-char (1- beg))
106       (insert prefix)
107       (insert-button button-text
108                      'invisibility-spec invis-spec
109                      :type button-type))))
110
111 (defun notmuch-wash-text/plain-citations (depth)
112   "Markup citations, and up to one signature in the buffer."
113   (goto-char (point-min))
114   (beginning-of-line)
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
123                              1))
124         (goto-char cite-start)
125         (forward-line notmuch-wash-citation-lines-prefix)
126         (let ((hidden-start (point-marker)))
127           (goto-char cite-end)
128           (forward-line (- notmuch-wash-citation-lines-suffix))
129           (notmuch-wash-region-to-button
130            hidden-start (point-marker)
131            "citation" "\n"
132            (format notmuch-wash-citation-button-format
133                    (- cite-lines
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
149                "signature" "\n"
150                (format notmuch-wash-signature-button-format sig-lines)))))))
151
152 ;;
153
154 (provide 'notmuch-wash)