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