]> git.notmuchmail.org Git - notmuch/blob - notmuch.el
notmuch.el: Hide citations and signatures.
[notmuch] / notmuch.el
1 ; notmuch.el --- run notmuch within emacs
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 ; Much of notmuch.el was written by looking at the implementation of
23 ; compile.el from the emacs distribution source which has the
24 ; following copyright and authorsip (and the identical license as
25 ; above):
26 ;
27 ; Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
28 ;   2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
29 ;   Free Software Foundation, Inc.
30
31 ; Authors: Roland McGrath <roland@gnu.org>,
32 ;           Daniel Pfeiffer <occitan@esperanto.org>
33
34 (defvar notmuch-show-mode-map
35   (let ((map (make-sparse-keymap)))
36     ; I don't actually want all of these toggle commands occupying
37     ; keybindings. They steal valuable key-binding space, are hard
38     ; to remember, and act globally rather than locally.
39     ;
40     ; Will be much preferable to switch to direct manipulation for
41     ; toggling visibility of these components. Probably using
42     ; overlays-at to query and manipulate the current overlay.
43     (define-key map "b" 'notmuch-show-toggle-body-read-visible)
44     (define-key map "c" 'notmuch-show-toggle-citations-visible)
45     (define-key map "h" 'notmuch-show-toggle-headers-visible)
46     (define-key map "n" 'notmuch-show-next-message)
47     (define-key map "p" 'notmuch-show-previous-message)
48     (define-key map "q" 'kill-this-buffer)
49     (define-key map "s" 'notmuch-show-toggle-signatures-visible)
50     (define-key map "x" 'kill-this-buffer)
51     map)
52   "Keymap for \"notmuch show\" buffers.")
53 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
54
55 (defvar notmuch-show-message-begin-regexp    "\fmessage{")
56 (defvar notmuch-show-message-end-regexp      "\fmessage}")
57 (defvar notmuch-show-header-begin-regexp     "\fheader{")
58 (defvar notmuch-show-header-end-regexp       "\fheader}")
59 (defvar notmuch-show-body-begin-regexp       "\fbody{")
60 (defvar notmuch-show-body-end-regexp         "\fbody}")
61 (defvar notmuch-show-attachment-begin-regexp "\fattachment{")
62 (defvar notmuch-show-attachment-end-regexp   "\fattachment}")
63 (defvar notmuch-show-part-begin-regexp       "\fpart{")
64 (defvar notmuch-show-part-end-regexp         "\fpart}")
65 (defvar notmuch-show-marker-regexp "\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
66
67 (defun notmuch-show-next-message ()
68   "Advance point to the beginning of the next message in the buffer."
69   (interactive)
70   ; First, ensure we get off the current message marker
71   (if (not (eobp))
72       (forward-char))
73   (re-search-forward notmuch-show-message-begin-regexp nil t)
74   (beginning-of-line)
75   (recenter 0))
76
77 (defun notmuch-show-previous-message ()
78   "Advance point to the beginning of the previous message in the buffer."
79   (interactive)
80   ; First, ensure we get off the current message marker
81   (if (not (bobp))
82       (previous-line))
83   (re-search-backward notmuch-show-message-begin-regexp nil t)
84   (beginning-of-line)
85   (recenter 0))
86
87 (defun notmuch-show-markup-citations-region (beg end)
88   (goto-char beg)
89   (beginning-of-line)
90   (while (< (point) end)
91     (let ((beg-sub (point)))
92       (if (looking-at ">")
93           (progn
94             (while (looking-at ">")
95               (next-line))
96             (let ((overlay (make-overlay beg-sub (point))))
97               (overlay-put overlay 'invisible 'notmuch-show-citation)
98               (overlay-put overlay 'before-string
99                            (concat "[" (number-to-string (count-lines beg-sub (point)))
100                                    " quoted lines.]")))))
101       (if (looking-at "--[ ]?$")
102           (let ((overlay (make-overlay beg-sub end)))
103             (overlay-put overlay 'invisible 'notmuch-show-signature)
104             (overlay-put overlay 'before-string
105                          (concat "[" (number-to-string (count-lines beg-sub (point)))
106                                  "-line signature.]"))
107             (goto-char end)))
108       (next-line))))
109
110 (defun notmuch-show-markup-body (unread)
111   (re-search-forward notmuch-show-body-begin-regexp)
112   (next-line 1)
113   (beginning-of-line)
114   (let ((beg (point)))
115     (re-search-forward notmuch-show-body-end-regexp)
116     (if (not unread)
117         (overlay-put (make-overlay beg (match-beginning 0))
118                      'invisible 'notmuch-show-body-read))
119     (notmuch-show-markup-citations-region beg (point))
120     ))
121
122 (defun notmuch-show-markup-header ()
123   (re-search-forward notmuch-show-header-begin-regexp)
124   (next-line 2)
125   (beginning-of-line)
126   (let ((beg (point)))
127     (re-search-forward notmuch-show-header-end-regexp)
128     (overlay-put (make-overlay beg (match-beginning 0))
129                  'invisible 'notmuch-show-header)))
130
131 (defun notmuch-show-markup-message ()
132   (if (re-search-forward notmuch-show-message-begin-regexp nil t)
133       (progn
134         (let ((unread (looking-at ".*unread$")))
135           (notmuch-show-markup-header)
136           (notmuch-show-markup-body unread)))
137     (goto-char (point-max))))
138
139 (defun notmuch-show-hide-markers ()
140   (save-excursion
141     (goto-char (point-min))
142     (while (not (eobp))
143       (if (re-search-forward notmuch-show-marker-regexp nil t)
144           (progn
145             (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
146                          'invisible 'notmuch-show-marker))
147         (goto-char (point-max))))))
148
149 (defun notmuch-show-markup-messages ()
150   (save-excursion
151     (goto-char (point-min))
152     (while (not (eobp))
153       (notmuch-show-markup-message)))
154   (notmuch-show-hide-markers))
155
156 (defun notmuch-show-toggle-citations-visible ()
157   "Toggle visibility of citations"
158   (interactive)
159   (if notmuch-show-citations-visible
160       (add-to-invisibility-spec 'notmuch-show-citation)
161     (remove-from-invisibility-spec 'notmuch-show-citation))
162   (set 'notmuch-show-citations-visible (not notmuch-show-citations-visible))
163   ; Need to force the redisplay for some reason
164   (force-window-update)
165   (redisplay t))
166
167 (defun notmuch-show-toggle-signatures-visible ()
168   "Toggle visibility of signatures"
169   (interactive)
170   (if notmuch-show-signatures-visible
171       (add-to-invisibility-spec 'notmuch-show-signature)
172     (remove-from-invisibility-spec 'notmuch-show-signature))
173   (set 'notmuch-show-signatures-visible (not notmuch-show-signatures-visible))
174   ; Need to force the redisplay for some reason
175   (force-window-update)
176   (redisplay t))
177
178 (defun notmuch-show-toggle-headers-visible ()
179   "Toggle visibility of header fields"
180   (interactive)
181   (if notmuch-show-headers-visible
182       (add-to-invisibility-spec 'notmuch-show-header)
183     (remove-from-invisibility-spec 'notmuch-show-header))
184   (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
185   ; Need to force the redisplay for some reason
186   (force-window-update)
187   (redisplay t))
188
189 (defun notmuch-show-toggle-body-read-visible ()
190   "Toggle visibility of message bodies of read messages"
191   (interactive)
192   (if notmuch-show-body-read-visible
193       (add-to-invisibility-spec 'notmuch-show-body-read)
194     (remove-from-invisibility-spec 'notmuch-show-body-read))
195   (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
196   ; Need to force the redisplay for some reason
197   (force-window-update)
198   (redisplay t))
199
200 ;;;###autoload
201 (defun notmuch-show-mode ()
202   "Major mode for handling the output of \"notmuch show\""
203   (interactive)
204   (kill-all-local-variables)
205   (set (make-local-variable 'notmuch-show-headers-visible) t)
206   (notmuch-show-toggle-headers-visible)
207   (set (make-local-variable 'notmuch-show-body-read-visible) t)
208   (notmuch-show-toggle-body-read-visible)
209   (set (make-local-variable 'notmuch-show-citations-visible) t)
210   (notmuch-show-toggle-citations-visible)
211   (set (make-local-variable 'notmuch-show-signatures-visible) t)
212   (notmuch-show-toggle-signatures-visible)
213   (add-to-invisibility-spec 'notmuch-show-marker)
214   (use-local-map notmuch-show-mode-map)
215   (setq major-mode 'notmuch-show-mode
216         mode-name "notmuch-show")
217   (setq buffer-read-only t))
218
219 (defun notmuch-show (thread-id)
220   "Run \"notmuch show\" with the given thread ID and display results."
221   (interactive "sNotmuch show: ")
222   (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
223     (switch-to-buffer buffer)
224     (notmuch-show-mode)
225     (let ((proc (get-buffer-process (current-buffer)))
226           (inhibit-read-only t))
227       (if proc
228           (error "notmuch search process already running for query `%s'" query)
229         )
230       (erase-buffer)
231       (goto-char (point-min))
232       (save-excursion
233         (call-process "notmuch" nil t nil "show" thread-id)
234         (notmuch-show-markup-messages)
235         )
236       )))
237
238 (defvar notmuch-search-mode-map
239   (let ((map (make-sparse-keymap)))
240     (define-key map "a" 'notmuch-search-archive-thread)
241     (define-key map "f" 'notmuch-search-filter)
242     (define-key map "n" 'next-line)
243     (define-key map "p" 'previous-line)
244     (define-key map "q" 'kill-this-buffer)
245     (define-key map "s" 'notmuch-search)
246     (define-key map "x" 'kill-this-buffer)
247     (define-key map "\r" 'notmuch-search-show-thread)
248     (define-key map "+" 'notmuch-search-add-tag)
249     (define-key map "-" 'notmuch-search-remove-tag)
250     (define-key map "<" 'beginning-of-buffer)
251     (define-key map ">" 'notmuch-search-goto-last-thread)
252     (define-key map "\M->" 'notmuch-search-goto-last-thread)
253     map)
254   "Keymap for \"notmuch search\" buffers.")
255 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
256
257 (defun notmuch-search-goto-last-thread (&optional arg)
258   "Move point to the last thread in the buffer."
259   (interactive "^P")
260   (end-of-buffer arg)
261   (beginning-of-line))
262
263 ;;;###autoload
264 (defun notmuch-search-mode ()
265   "Major mode for handling the output of \"notmuch search\""
266   (interactive)
267   (kill-all-local-variables)
268   (make-local-variable 'notmuch-search-query-string)
269   (use-local-map notmuch-search-mode-map)
270   (setq major-mode 'notmuch-search-mode
271         mode-name "notmuch-search")
272   (setq buffer-read-only t))
273
274 (defun notmuch-search-find-thread-id ()
275   (save-excursion
276     (beginning-of-line)
277     (let ((beg (point)))
278       (re-search-forward "[a-fA-F0-9]*")
279       (filter-buffer-substring beg (point)))))
280
281 (defun notmuch-search-markup-this-thread-id ()
282   (beginning-of-line)
283   (let ((beg (point)))
284     (re-search-forward "[a-fA-F0-9]*")
285     (forward-char)
286     (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)))
287
288 (defun notmuch-search-markup-thread-ids ()
289   (save-excursion
290     (goto-char (point-min))
291     (while (not (eobp))
292       (notmuch-search-markup-this-thread-id)
293       (next-line))))
294
295 (defun notmuch-search-hide-thread-ids ()
296   (interactive)
297   (add-to-invisibility-spec 'notmuch-search))
298
299 (defun notmuch-search-show-thread-ids ()
300   (interactive)
301   (remove-from-invisibility-spec 'notmuch-search))
302
303 (defun notmuch-search-show-thread ()
304   (interactive)
305   (notmuch-show (notmuch-search-find-thread-id)))
306
307 (defun notmuch-search-call-notmuch-process (&rest args)
308   (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
309     (with-current-buffer error-buffer
310         (erase-buffer))
311     (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
312         (point)
313       (progn
314         (with-current-buffer error-buffer
315           (let ((beg (point-min))
316                 (end (- (point-max) 1)))
317             (error (buffer-substring beg end))
318             ))))))
319
320 (defun notmuch-search-set-tags (tags)
321   (save-excursion
322     (end-of-line)
323     (re-search-backward "(")
324     (forward-char)
325     (let ((beg (point))
326           (inhibit-read-only t))
327       (re-search-forward ")")
328       (backward-char)
329       (let ((end (point)))
330         (delete-region beg end)
331         (insert (mapconcat  'identity tags " "))))))
332
333 (defun notmuch-search-get-tags ()
334   (save-excursion
335     (end-of-line)
336     (re-search-backward "(")
337     (let ((beg (+ (point) 1)))
338       (re-search-forward ")")
339       (let ((end (- (point) 1)))
340         (split-string (buffer-substring beg end))))))
341
342 (defun notmuch-search-add-tag (tag)
343   (interactive "sTag to add: ")
344   (notmuch-search-call-notmuch-process "tag" (concat "+" tag) (concat "thread:" (notmuch-search-find-thread-id)))
345   (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
346
347 (defun notmuch-search-remove-tag (tag)
348   (interactive "sTag to remove: ")
349   (notmuch-search-call-notmuch-process "tag" (concat "-" tag) (concat "thread:" (notmuch-search-find-thread-id)))
350   (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
351
352 (defun notmuch-search-archive-thread ()
353   (interactive)
354   (notmuch-search-remove-tag "inbox"))
355
356 (defun notmuch-search (query)
357   "Run \"notmuch search\" with the given query string and display results."
358   (interactive "sNotmuch search: ")
359   (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
360     (switch-to-buffer buffer)
361     (notmuch-search-mode)
362     (set 'notmuch-search-query-string query)
363     (let ((proc (get-buffer-process (current-buffer)))
364           (inhibit-read-only t))
365       (if proc
366           (error "notmuch search process already running for query `%s'" query)
367         )
368       (erase-buffer)
369       (goto-char (point-min))
370       (save-excursion
371         (call-process "notmuch" nil t nil "search" query)
372         (notmuch-search-markup-thread-ids)
373         ; A well-behaved program ends its output with a newline, but we
374         ; don't actually want the blank line at the end of the file.
375         (goto-char (point-max))
376         (if (looking-at "^$")
377             (delete-backward-char 1)
378           )
379         ))))
380
381 (defun notmuch-search-filter (query)
382   "Run \"notmuch search\" to refine the current search results.
383
384 A search string will be constructed by appending QUERY to the
385 current search string, and the results of \"notmuch search\" for
386 the combined query will be displayed."
387   (interactive "sFilter search: ")
388   (notmuch-search (concat notmuch-search-query-string " and " query)))
389
390 (defun notmuch ()
391   "Run notmuch to display all mail with tag of 'inbox'"
392   (interactive)
393   (notmuch-search "tag:inbox"))
394
395 (provide 'notmuch)