]> git.notmuchmail.org Git - notmuch/blob - notmuch.el
c00d359598a85b05d4b699c8e7079c0284d810a9
[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 "=" 'notmuch-search-refresh-view)
290     (define-key map "\M->" 'notmuch-search-goto-last-thread)
291     map)
292   "Keymap for \"notmuch search\" buffers.")
293 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
294
295 (defun notmuch-search-goto-last-thread (&optional arg)
296   "Move point to the last thread in the buffer."
297   (interactive "^P")
298   (end-of-buffer arg)
299   (beginning-of-line))
300
301 ;;;###autoload
302 (defun notmuch-search-mode ()
303   "Major mode for handling the output of \"notmuch search\""
304   (interactive)
305   (kill-all-local-variables)
306   (make-local-variable 'notmuch-search-query-string)
307   (use-local-map notmuch-search-mode-map)
308   (setq major-mode 'notmuch-search-mode
309         mode-name "notmuch-search")
310   (setq buffer-read-only t))
311
312 (defun notmuch-search-find-thread-id ()
313   (save-excursion
314     (beginning-of-line)
315     (let ((beg (point)))
316       (re-search-forward "[a-fA-F0-9]*")
317       (filter-buffer-substring beg (point)))))
318
319 (defun notmuch-search-markup-this-thread-id ()
320   (beginning-of-line)
321   (let ((beg (point)))
322     (re-search-forward "[a-fA-F0-9]*")
323     (forward-char)
324     (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)))
325
326 (defun notmuch-search-markup-thread-ids ()
327   (save-excursion
328     (goto-char (point-min))
329     (while (not (eobp))
330       (notmuch-search-markup-this-thread-id)
331       (next-line))))
332
333 (defun notmuch-search-hide-thread-ids ()
334   (interactive)
335   (add-to-invisibility-spec 'notmuch-search))
336
337 (defun notmuch-search-show-thread-ids ()
338   (interactive)
339   (remove-from-invisibility-spec 'notmuch-search))
340
341 (defun notmuch-search-show-thread ()
342   (interactive)
343   (notmuch-show (notmuch-search-find-thread-id)))
344
345 (defun notmuch-call-notmuch-process (&rest args)
346   (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
347     (with-current-buffer error-buffer
348         (erase-buffer))
349     (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
350         (point)
351       (progn
352         (with-current-buffer error-buffer
353           (let ((beg (point-min))
354                 (end (- (point-max) 1)))
355             (error (buffer-substring beg end))
356             ))))))
357
358 (defun notmuch-search-set-tags (tags)
359   (save-excursion
360     (end-of-line)
361     (re-search-backward "(")
362     (forward-char)
363     (let ((beg (point))
364           (inhibit-read-only t))
365       (re-search-forward ")")
366       (backward-char)
367       (let ((end (point)))
368         (delete-region beg end)
369         (insert (mapconcat  'identity tags " "))))))
370
371 (defun notmuch-search-get-tags ()
372   (save-excursion
373     (end-of-line)
374     (re-search-backward "(")
375     (let ((beg (+ (point) 1)))
376       (re-search-forward ")")
377       (let ((end (- (point) 1)))
378         (split-string (buffer-substring beg end))))))
379
380 (defun notmuch-search-add-tag (tag)
381   (interactive "sTag to add: ")
382   (notmuch-call-notmuch-process "tag" (concat "+" tag) (concat "thread:" (notmuch-search-find-thread-id)))
383   (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
384
385 (defun notmuch-search-remove-tag (tag)
386   (interactive "sTag to remove: ")
387   (notmuch-call-notmuch-process "tag" (concat "-" tag) (concat "thread:" (notmuch-search-find-thread-id)))
388   (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
389
390 (defun notmuch-search-archive-thread ()
391   (interactive)
392   (notmuch-search-remove-tag "inbox"))
393
394 (defun notmuch-search (query)
395   "Run \"notmuch search\" with the given query string and display results."
396   (interactive "sNotmuch search: ")
397   (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
398     (switch-to-buffer buffer)
399     (notmuch-search-mode)
400     (set 'notmuch-search-query-string query)
401     (let ((proc (get-buffer-process (current-buffer)))
402           (inhibit-read-only t))
403       (if proc
404           (error "notmuch search process already running for query `%s'" query)
405         )
406       (erase-buffer)
407       (goto-char (point-min))
408       (save-excursion
409         (call-process "notmuch" nil t nil "search" query)
410         (notmuch-search-markup-thread-ids)
411         ; A well-behaved program ends its output with a newline, but we
412         ; don't actually want the blank line at the end of the file.
413         (goto-char (point-max))
414         (if (looking-at "^$")
415             (delete-backward-char 1)
416           )
417         ))))
418
419 (defun notmuch-search-refresh-view ()
420   "Refresh the current view.
421
422 Kills the current buffer and runs a new search with the same
423 query string as the current search."
424   (interactive)
425   (let ((query notmuch-search-query-string))
426     (kill-this-buffer)
427     (notmuch-search query)))
428
429 (defun notmuch-search-filter (query)
430   "Run \"notmuch search\" to refine the current search results.
431
432 A search string will be constructed by appending QUERY to the
433 current search string, and the results of \"notmuch search\" for
434 the combined query will be displayed."
435   (interactive "sFilter search: ")
436   (notmuch-search (concat notmuch-search-query-string " and " query)))
437
438 (defun notmuch ()
439   "Run notmuch to display all mail with tag of 'inbox'"
440   (interactive)
441   (notmuch-search "tag:inbox"))
442
443 (provide 'notmuch)