]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-wash.el
emacs/notmuch-show.el: Part headers are real buttons that save the part
[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-to-button (beg end type prefix button-text)
81   "Auxilary function to do the actual making of overlays and buttons
82
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."
87
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.
92
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)
99     (goto-char (1+ end))
100     (save-excursion
101       (goto-char (1- beg))
102       (insert prefix)
103       (insert-button button-text
104                      'invisibility-spec invis-spec
105                      :type button-type))))
106
107 (defun notmuch-wash-text/plain-citations (depth)
108   "Markup citations, and up to one signature in the buffer."
109   (goto-char (point-min))
110   (beginning-of-line)
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
119                              1))
120         (goto-char cite-start)
121         (forward-line notmuch-wash-citation-lines-prefix)
122         (let ((hidden-start (point-marker)))
123           (goto-char cite-end)
124           (forward-line (- notmuch-wash-citation-lines-suffix))
125           (notmuch-wash-region-to-button
126            hidden-start (point-marker)
127            "citation" "\n"
128            (format notmuch-wash-citation-button-format
129                    (- cite-lines
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
145                "signature" "\n"
146                (format notmuch-wash-signature-button-format sig-lines)))))))
147
148 ;;
149
150 (provide 'notmuch-wash)