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