]> git.notmuchmail.org Git - notmuch/blob - notmuch.el
89089c53e7c40f032330a28bbc871aa7911376fa
[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     (define-key map "+" 'notmuch-show-add-tag)
52     (define-key map "-" 'notmuch-show-remove-tag)
53     map)
54   "Keymap for \"notmuch show\" buffers.")
55 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
56
57 (defvar notmuch-show-message-begin-regexp    "\fmessage{")
58 (defvar notmuch-show-message-end-regexp      "\fmessage}")
59 (defvar notmuch-show-header-begin-regexp     "\fheader{")
60 (defvar notmuch-show-header-end-regexp       "\fheader}")
61 (defvar notmuch-show-body-begin-regexp       "\fbody{")
62 (defvar notmuch-show-body-end-regexp         "\fbody}")
63 (defvar notmuch-show-attachment-begin-regexp "\fattachment{")
64 (defvar notmuch-show-attachment-end-regexp   "\fattachment}")
65 (defvar notmuch-show-part-begin-regexp       "\fpart{")
66 (defvar notmuch-show-part-end-regexp         "\fpart}")
67 (defvar notmuch-show-marker-regexp "\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
68
69 (defvar notmuch-show-id-regexp "ID: \\(.*\\)$")
70 (defvar notmuch-show-tags-regexp "(\\([^)]*\\))$")
71
72 (defun notmuch-show-get-message-id ()
73   (save-excursion
74     (beginning-of-line)
75     (if (not (looking-at notmuch-show-message-begin-regexp))
76         (re-search-backward notmuch-show-message-begin-regexp))
77     (re-search-forward notmuch-show-id-regexp)
78     (buffer-substring (match-beginning 1) (match-end 1))))
79
80 (defun notmuch-show-set-tags (tags)
81   (save-excursion
82     (beginning-of-line)
83     (if (not (looking-at notmuch-show-message-begin-regexp))
84         (re-search-backward notmuch-show-message-begin-regexp))
85     (re-search-forward notmuch-show-tags-regexp)
86     (let ((inhibit-read-only t)
87           (beg (match-beginning 1))
88           (end (match-end 1)))
89       (delete-region beg end)
90       (goto-char beg)
91       (insert (mapconcat 'identity tags " ")))))
92
93 (defun notmuch-show-get-tags ()
94   (save-excursion
95     (beginning-of-line)
96     (if (not (looking-at notmuch-show-message-begin-regexp))
97         (re-search-backward notmuch-show-message-begin-regexp))
98     (re-search-forward notmuch-show-tags-regexp)
99     (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
100
101 (defun notmuch-show-add-tag (tag)
102   (interactive "sTag to add: ")
103   (notmuch-call-notmuch-process "tag" (concat "+" tag) (concat "id:" (notmuch-show-get-message-id)))
104   (notmuch-show-set-tags (delete-dups (sort (cons tag (notmuch-show-get-tags)) 'string<))))
105
106 (defun notmuch-show-remove-tag (tag)
107   (interactive "sTag to remove: ")
108   (notmuch-call-notmuch-process "tag" (concat "-" tag) (concat "id:" (notmuch-show-get-message-id)))
109   (notmuch-show-set-tags (delete tag (notmuch-show-get-tags))))
110
111 (defun notmuch-show-next-message ()
112   "Advance point to the beginning of the next message in the buffer.
113
114 Before moving, also remove the \"unread\" tag from the current message."
115   (interactive)
116   (notmuch-show-remove-tag "unread")
117   ; First, ensure we get off the current message marker
118   (if (not (eobp))
119       (forward-char))
120   (re-search-forward notmuch-show-message-begin-regexp nil t)
121   ; This dance might look pointless, but it's important. I originally
122   ; just had (beginning-of-line) here which looked right on the
123   ; display but actually put point all the way back to the first
124   ; character of the first invisible line. That is, it put point into
125   ; the closing markers of the previous message rather than at the
126   ; beginning of the current message. And that in turn meant that
127   ; looking up the current message-ID would actually return the
128   ; previous message ID.
129   ;
130   ; So this dance ensures that we're actually on the current message
131   ; when it looks like we are.
132   (end-of-visible-line)
133   (beginning-of-line)
134   (recenter 0))
135
136 (defun notmuch-show-previous-message ()
137   "Advance point to the beginning of the previous message in the buffer."
138   (interactive)
139   ; First, ensure we get off the current message marker
140   (if (not (bobp))
141       (previous-line))
142   (re-search-backward notmuch-show-message-begin-regexp nil t)
143   ; This dance might look pointless, but it's important. I originally
144   ; just had (beginning-of-line) here which looked right on the
145   ; display but actually put point all the way back to the first
146   ; character of the first invisible line. That is, it put point into
147   ; the closing markers of the previous message rather than at the
148   ; beginning of the current message. And that in turn meant that
149   ; looking up the current message-ID would actually return the
150   ; previous message ID.
151   ;
152   ; So this dance ensures that we're actually on the current message
153   ; when it looks like we are.
154   (end-of-visible-line)
155   (beginning-of-line)
156   (recenter 0))
157
158 (defun notmuch-show-markup-citations-region (beg end)
159   (goto-char beg)
160   (beginning-of-line)
161   (while (< (point) end)
162     (let ((beg-sub (point)))
163       (if (looking-at ">")
164           (progn
165             (while (looking-at ">")
166               (next-line))
167             (let ((overlay (make-overlay beg-sub (point))))
168               (overlay-put overlay 'invisible 'notmuch-show-citation)
169               (overlay-put overlay 'before-string
170                            (concat "[" (number-to-string (count-lines beg-sub (point)))
171                                    " quoted lines.]")))))
172       (if (looking-at "--[ ]?$")
173           (let ((overlay (make-overlay beg-sub end)))
174             (overlay-put overlay 'invisible 'notmuch-show-signature)
175             (overlay-put overlay 'before-string
176                          (concat "[" (number-to-string (count-lines beg-sub (point)))
177                                  "-line signature.]"))
178             (goto-char end)))
179       (next-line))))
180
181 (defun notmuch-show-markup-body ()
182   (re-search-forward notmuch-show-body-begin-regexp)
183   (next-line 1)
184   (beginning-of-line)
185   (let ((beg (point)))
186     (re-search-forward notmuch-show-body-end-regexp)
187     (let ((end (match-beginning 0)))
188       (if (not (member "unread" (notmuch-show-get-tags)))
189           (overlay-put (make-overlay beg end)
190                        'invisible 'notmuch-show-body-read))
191       (notmuch-show-markup-citations-region beg end))))
192
193 (defun notmuch-show-markup-header ()
194   (re-search-forward notmuch-show-header-begin-regexp)
195   (next-line 2)
196   (beginning-of-line)
197   (let ((beg (point)))
198     (re-search-forward notmuch-show-header-end-regexp)
199     (overlay-put (make-overlay beg (match-beginning 0))
200                  'invisible 'notmuch-show-header)))
201
202 (defun notmuch-show-markup-message ()
203   (if (re-search-forward notmuch-show-message-begin-regexp nil t)
204       (progn
205         (notmuch-show-markup-header)
206         (notmuch-show-markup-body))
207     (goto-char (point-max))))
208
209 (defun notmuch-show-hide-markers ()
210   (save-excursion
211     (goto-char (point-min))
212     (while (not (eobp))
213       (if (re-search-forward notmuch-show-marker-regexp nil t)
214           (progn
215             (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
216                          'invisible 'notmuch-show-marker))
217         (goto-char (point-max))))))
218
219 (defun notmuch-show-markup-messages ()
220   (save-excursion
221     (goto-char (point-min))
222     (while (not (eobp))
223       (notmuch-show-markup-message)))
224   (notmuch-show-hide-markers))
225
226 (defun notmuch-show-toggle-citations-visible ()
227   "Toggle visibility of citations"
228   (interactive)
229   (if notmuch-show-citations-visible
230       (add-to-invisibility-spec 'notmuch-show-citation)
231     (remove-from-invisibility-spec 'notmuch-show-citation))
232   (set 'notmuch-show-citations-visible (not notmuch-show-citations-visible))
233   ; Need to force the redisplay for some reason
234   (force-window-update)
235   (redisplay t))
236
237 (defun notmuch-show-toggle-signatures-visible ()
238   "Toggle visibility of signatures"
239   (interactive)
240   (if notmuch-show-signatures-visible
241       (add-to-invisibility-spec 'notmuch-show-signature)
242     (remove-from-invisibility-spec 'notmuch-show-signature))
243   (set 'notmuch-show-signatures-visible (not notmuch-show-signatures-visible))
244   ; Need to force the redisplay for some reason
245   (force-window-update)
246   (redisplay t))
247
248 (defun notmuch-show-toggle-headers-visible ()
249   "Toggle visibility of header fields"
250   (interactive)
251   (if notmuch-show-headers-visible
252       (add-to-invisibility-spec 'notmuch-show-header)
253     (remove-from-invisibility-spec 'notmuch-show-header))
254   (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
255   ; Need to force the redisplay for some reason
256   (force-window-update)
257   (redisplay t))
258
259 (defun notmuch-show-toggle-body-read-visible ()
260   "Toggle visibility of message bodies of read messages"
261   (interactive)
262   (if notmuch-show-body-read-visible
263       (add-to-invisibility-spec 'notmuch-show-body-read)
264     (remove-from-invisibility-spec 'notmuch-show-body-read))
265   (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
266   ; Need to force the redisplay for some reason
267   (force-window-update)
268   (redisplay t))
269
270 ;;;###autoload
271 (defun notmuch-show-mode ()
272   "Major mode for handling the output of \"notmuch show\""
273   (interactive)
274   (kill-all-local-variables)
275   (set (make-local-variable 'notmuch-show-headers-visible) t)
276   (notmuch-show-toggle-headers-visible)
277   (set (make-local-variable 'notmuch-show-body-read-visible) t)
278   (notmuch-show-toggle-body-read-visible)
279   (set (make-local-variable 'notmuch-show-citations-visible) t)
280   (notmuch-show-toggle-citations-visible)
281   (set (make-local-variable 'notmuch-show-signatures-visible) t)
282   (notmuch-show-toggle-signatures-visible)
283   (add-to-invisibility-spec 'notmuch-show-marker)
284   (use-local-map notmuch-show-mode-map)
285   (setq major-mode 'notmuch-show-mode
286         mode-name "notmuch-show")
287   (setq buffer-read-only t))
288
289 (defun notmuch-show (thread-id)
290   "Run \"notmuch show\" with the given thread ID and display results."
291   (interactive "sNotmuch show: ")
292   (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
293     (switch-to-buffer buffer)
294     (notmuch-show-mode)
295     (let ((proc (get-buffer-process (current-buffer)))
296           (inhibit-read-only t))
297       (if proc
298           (error "notmuch search process already running for query `%s'" query)
299         )
300       (erase-buffer)
301       (goto-char (point-min))
302       (save-excursion
303         (call-process "notmuch" nil t nil "show" thread-id)
304         (notmuch-show-markup-messages)
305         )
306       )))
307
308 (defvar notmuch-search-mode-map
309   (let ((map (make-sparse-keymap)))
310     (define-key map "a" 'notmuch-search-archive-thread)
311     (define-key map "f" 'notmuch-search-filter)
312     (define-key map "n" 'next-line)
313     (define-key map "p" 'previous-line)
314     (define-key map "q" 'kill-this-buffer)
315     (define-key map "s" 'notmuch-search)
316     (define-key map "x" 'kill-this-buffer)
317     (define-key map "\r" 'notmuch-search-show-thread)
318     (define-key map "+" 'notmuch-search-add-tag)
319     (define-key map "-" 'notmuch-search-remove-tag)
320     (define-key map "<" 'beginning-of-buffer)
321     (define-key map ">" 'notmuch-search-goto-last-thread)
322     (define-key map "=" 'notmuch-search-refresh-view)
323     (define-key map "\M->" 'notmuch-search-goto-last-thread)
324     map)
325   "Keymap for \"notmuch search\" buffers.")
326 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
327
328 (defun notmuch-search-goto-last-thread (&optional arg)
329   "Move point to the last thread in the buffer."
330   (interactive "^P")
331   (end-of-buffer arg)
332   (beginning-of-line))
333
334 ;;;###autoload
335 (defun notmuch-search-mode ()
336   "Major mode for handling the output of \"notmuch search\""
337   (interactive)
338   (kill-all-local-variables)
339   (make-local-variable 'notmuch-search-query-string)
340   (use-local-map notmuch-search-mode-map)
341   (setq major-mode 'notmuch-search-mode
342         mode-name "notmuch-search")
343   (setq buffer-read-only t))
344
345 (defun notmuch-search-find-thread-id ()
346   (save-excursion
347     (beginning-of-line)
348     (let ((beg (point)))
349       (re-search-forward "[a-fA-F0-9]*")
350       (filter-buffer-substring beg (point)))))
351
352 (defun notmuch-search-markup-this-thread-id ()
353   (beginning-of-line)
354   (let ((beg (point)))
355     (re-search-forward "[a-fA-F0-9]*")
356     (forward-char)
357     (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)))
358
359 (defun notmuch-search-markup-thread-ids ()
360   (save-excursion
361     (goto-char (point-min))
362     (while (not (eobp))
363       (notmuch-search-markup-this-thread-id)
364       (next-line))))
365
366 (defun notmuch-search-hide-thread-ids ()
367   (interactive)
368   (add-to-invisibility-spec 'notmuch-search))
369
370 (defun notmuch-search-show-thread-ids ()
371   (interactive)
372   (remove-from-invisibility-spec 'notmuch-search))
373
374 (defun notmuch-search-show-thread ()
375   (interactive)
376   (notmuch-show (notmuch-search-find-thread-id)))
377
378 (defun notmuch-call-notmuch-process (&rest args)
379   (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
380     (with-current-buffer error-buffer
381         (erase-buffer))
382     (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
383         (point)
384       (progn
385         (with-current-buffer error-buffer
386           (let ((beg (point-min))
387                 (end (- (point-max) 1)))
388             (error (buffer-substring beg end))
389             ))))))
390
391 (defun notmuch-search-set-tags (tags)
392   (save-excursion
393     (end-of-line)
394     (re-search-backward "(")
395     (forward-char)
396     (let ((beg (point))
397           (inhibit-read-only t))
398       (re-search-forward ")")
399       (backward-char)
400       (let ((end (point)))
401         (delete-region beg end)
402         (insert (mapconcat  'identity tags " "))))))
403
404 (defun notmuch-search-get-tags ()
405   (save-excursion
406     (end-of-line)
407     (re-search-backward "(")
408     (let ((beg (+ (point) 1)))
409       (re-search-forward ")")
410       (let ((end (- (point) 1)))
411         (split-string (buffer-substring beg end))))))
412
413 (defun notmuch-search-add-tag (tag)
414   (interactive "sTag to add: ")
415   (notmuch-call-notmuch-process "tag" (concat "+" tag) (concat "thread:" (notmuch-search-find-thread-id)))
416   (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
417
418 (defun notmuch-search-remove-tag (tag)
419   (interactive "sTag to remove: ")
420   (notmuch-call-notmuch-process "tag" (concat "-" tag) (concat "thread:" (notmuch-search-find-thread-id)))
421   (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
422
423 (defun notmuch-search-archive-thread ()
424   (interactive)
425   (notmuch-search-remove-tag "inbox"))
426
427 (defun notmuch-search (query)
428   "Run \"notmuch search\" with the given query string and display results."
429   (interactive "sNotmuch search: ")
430   (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
431     (switch-to-buffer buffer)
432     (notmuch-search-mode)
433     (set 'notmuch-search-query-string query)
434     (let ((proc (get-buffer-process (current-buffer)))
435           (inhibit-read-only t))
436       (if proc
437           (error "notmuch search process already running for query `%s'" query)
438         )
439       (erase-buffer)
440       (goto-char (point-min))
441       (save-excursion
442         (call-process "notmuch" nil t nil "search" query)
443         (notmuch-search-markup-thread-ids)
444         ; A well-behaved program ends its output with a newline, but we
445         ; don't actually want the blank line at the end of the file.
446         (goto-char (point-max))
447         (if (looking-at "^$")
448             (delete-backward-char 1)
449           )
450         ))))
451
452 (defun notmuch-search-refresh-view ()
453   "Refresh the current view.
454
455 Kills the current buffer and runs a new search with the same
456 query string as the current search. If the current thread is in
457 the new search results, then point will be placed on the same
458 thread. Otherwise, point will be moved to attempt to be in the
459 same relative position within the new buffer."
460   (interactive)
461   (let ((here (point))
462         (thread (notmuch-search-find-thread-id))
463         (query notmuch-search-query-string))
464     (kill-this-buffer)
465     (notmuch-search query)
466     (goto-char (point-min))
467     (if (re-search-forward (concat "^" thread) nil t)
468         (beginning-of-line)
469       (goto-char here))))
470
471 (defun notmuch-search-filter (query)
472   "Run \"notmuch search\" to refine the current search results.
473
474 A search string will be constructed by appending QUERY to the
475 current search string, and the results of \"notmuch search\" for
476 the combined query will be displayed."
477   (interactive "sFilter search: ")
478   (notmuch-search (concat notmuch-search-query-string " and " query)))
479
480 (defun notmuch ()
481   "Run notmuch to display all mail with tag of 'inbox'"
482   (interactive)
483   (notmuch-search "tag:inbox"))
484
485 (provide 'notmuch)