]> git.notmuchmail.org Git - notmuch/blob - notmuch.el
7f087c7240de754e1510af878885129ba3d08a03
[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 (defvar notmuch-show-mode-map
23   (let ((map (make-sparse-keymap)))
24     ; I don't actually want all of these toggle commands occupying
25     ; keybindings. They steal valuable key-binding space, are hard
26     ; to remember, and act globally rather than locally.
27     ;
28     ; Will be much preferable to switch to direct manipulation for
29     ; toggling visibility of these components. Probably using
30     ; overlays-at to query and manipulate the current overlay.
31     (define-key map "a" 'notmuch-show-archive-thread)
32     (define-key map "b" 'notmuch-show-toggle-body-read-visible)
33     (define-key map "c" 'notmuch-show-toggle-citations-visible)
34     (define-key map "h" 'notmuch-show-toggle-headers-visible)
35     (define-key map "n" 'notmuch-show-mark-read-then-next-message)
36     (define-key map "p" 'notmuch-show-previous-message)
37     (define-key map "q" 'kill-this-buffer)
38     (define-key map "s" 'notmuch-show-toggle-signatures-visible)
39     (define-key map "x" 'kill-this-buffer)
40     (define-key map "+" 'notmuch-show-add-tag)
41     (define-key map "-" 'notmuch-show-remove-tag)
42     map)
43   "Keymap for \"notmuch show\" buffers.")
44 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
45
46 (defvar notmuch-show-message-begin-regexp    "\fmessage{")
47 (defvar notmuch-show-message-end-regexp      "\fmessage}")
48 (defvar notmuch-show-header-begin-regexp     "\fheader{")
49 (defvar notmuch-show-header-end-regexp       "\fheader}")
50 (defvar notmuch-show-body-begin-regexp       "\fbody{")
51 (defvar notmuch-show-body-end-regexp         "\fbody}")
52 (defvar notmuch-show-attachment-begin-regexp "\fattachment{")
53 (defvar notmuch-show-attachment-end-regexp   "\fattachment}")
54 (defvar notmuch-show-part-begin-regexp       "\fpart{")
55 (defvar notmuch-show-part-end-regexp         "\fpart}")
56 (defvar notmuch-show-marker-regexp "\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
57
58 (defvar notmuch-show-id-regexp "ID: \\(.*\\)$")
59 (defvar notmuch-show-tags-regexp "(\\([^)]*\\))$")
60
61 (defun notmuch-show-get-message-id ()
62   (save-excursion
63     (beginning-of-line)
64     (if (not (looking-at notmuch-show-message-begin-regexp))
65         (re-search-backward notmuch-show-message-begin-regexp))
66     (re-search-forward notmuch-show-id-regexp)
67     (buffer-substring (match-beginning 1) (match-end 1))))
68
69 (defun notmuch-show-set-tags (tags)
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-tags-regexp)
75     (let ((inhibit-read-only t)
76           (beg (match-beginning 1))
77           (end (match-end 1)))
78       (delete-region beg end)
79       (goto-char beg)
80       (insert (mapconcat 'identity tags " ")))))
81
82 (defun notmuch-show-get-tags ()
83   (save-excursion
84     (beginning-of-line)
85     (if (not (looking-at notmuch-show-message-begin-regexp))
86         (re-search-backward notmuch-show-message-begin-regexp))
87     (re-search-forward notmuch-show-tags-regexp)
88     (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
89
90 (defun notmuch-show-add-tag (tag)
91   (interactive "sTag to add: ")
92   (notmuch-call-notmuch-process "tag" (concat "+" tag) (concat "id:" (notmuch-show-get-message-id)))
93   (notmuch-show-set-tags (delete-dups (sort (cons tag (notmuch-show-get-tags)) 'string<))))
94
95 (defun notmuch-show-remove-tag (tag)
96   (interactive "sTag to remove: ")
97   (notmuch-call-notmuch-process "tag" (concat "-" tag) (concat "id:" (notmuch-show-get-message-id)))
98   (notmuch-show-set-tags (delete tag (notmuch-show-get-tags))))
99
100 (defun notmuch-show-archive-thread ()
101   "Archive each message currrently shown by removing the \"inbox\" tag from each.
102
103 This command is safe from any race condition of new messages
104 being delivered to the same thread. It does not archive the
105 entire thread, but only the messages shown in the current
106 buffer."
107   (interactive)
108   (save-excursion
109     (goto-char (point-min))
110     (while (not (eobp))
111       (notmuch-show-remove-tag "inbox")
112       (if (not (eobp))
113           (forward-char))
114       (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
115           (goto-char (point-max))))))
116
117 (defun notmuch-show-next-message ()
118   "Advance point to the beginning of the next message in the buffer.
119
120 Does nothing if already on the last message."
121   (interactive)
122   ; First, ensure we get off the current message marker
123   (if (not (eobp))
124       (forward-char))
125   (re-search-forward notmuch-show-message-begin-regexp nil t)
126   ; This dance might look pointless, but it's important. I originally
127   ; just had (beginning-of-line) here which looked right on the
128   ; display but actually put point all the way back to the first
129   ; character of the first invisible line. That is, it put point into
130   ; the closing markers of the previous message rather than at the
131   ; beginning of the current message. And that in turn meant that
132   ; looking up the current message-ID would actually return the
133   ; previous message ID.
134   ;
135   ; So this dance ensures that we're actually on the current message
136   ; when it looks like we are.
137   (end-of-visible-line)
138   (beginning-of-line)
139   (recenter 0))
140
141 (defun notmuch-show-previous-message ()
142   "Backup to the beginning of the previous message in the buffer.
143
144 Does nothing if already on the first message in the buffer."
145   (interactive)
146   ; First, ensure we get off the current message marker
147   (if (not (bobp))
148       (previous-line))
149   (re-search-backward notmuch-show-message-begin-regexp nil t)
150   ; This dance might look pointless, but it's important. I originally
151   ; just had (beginning-of-line) here which looked right on the
152   ; display but actually put point all the way back to the first
153   ; character of the first invisible line. That is, it put point into
154   ; the closing markers of the previous message rather than at the
155   ; beginning of the current message. And that in turn meant that
156   ; looking up the current message-ID would actually return the
157   ; previous message ID.
158   ;
159   ; So this dance ensures that we're actually on the current message
160   ; when it looks like we are.
161   (end-of-visible-line)
162   (beginning-of-line)
163   (recenter 0))
164
165 (defun notmuch-show-mark-read-then-next-message ()
166   "Remove uread tag from current message, then advance to next message."
167   (interactive)
168   (if (member "unread" (notmuch-show-get-tags))
169       (notmuch-show-remove-tag "unread"))
170   (notmuch-show-next-message))
171
172 (defun notmuch-show-markup-citations-region (beg end)
173   (goto-char beg)
174   (beginning-of-line)
175   (while (< (point) end)
176     (let ((beg-sub (point)))
177       (if (looking-at ">")
178           (progn
179             (while (looking-at ">")
180               (next-line))
181             (let ((overlay (make-overlay beg-sub (point))))
182               (overlay-put overlay 'invisible 'notmuch-show-citation)
183               (overlay-put overlay 'before-string
184                            (concat "[" (number-to-string (count-lines beg-sub (point)))
185                                    " quoted lines.]")))))
186       (if (looking-at "--[ ]?$")
187           (let ((overlay (make-overlay beg-sub end)))
188             (overlay-put overlay 'invisible 'notmuch-show-signature)
189             (overlay-put overlay 'before-string
190                          (concat "[" (number-to-string (count-lines beg-sub (point)))
191                                  "-line signature.]"))
192             (goto-char end)))
193       (next-line))))
194
195 (defun notmuch-show-markup-body ()
196   (re-search-forward notmuch-show-body-begin-regexp)
197   (next-line 1)
198   (beginning-of-line)
199   (let ((beg (point)))
200     (re-search-forward notmuch-show-body-end-regexp)
201     (let ((end (match-beginning 0)))
202       (if (not (member "unread" (notmuch-show-get-tags)))
203           (overlay-put (make-overlay beg end)
204                        'invisible 'notmuch-show-body-read))
205       (notmuch-show-markup-citations-region beg end))))
206
207 (defun notmuch-show-markup-header ()
208   (re-search-forward notmuch-show-header-begin-regexp)
209   (next-line 2)
210   (beginning-of-line)
211   (let ((beg (point)))
212     (re-search-forward notmuch-show-header-end-regexp)
213     (overlay-put (make-overlay beg (match-beginning 0))
214                  'invisible 'notmuch-show-header)))
215
216 (defun notmuch-show-markup-message ()
217   (if (re-search-forward notmuch-show-message-begin-regexp nil t)
218       (progn
219         (notmuch-show-markup-header)
220         (notmuch-show-markup-body))
221     (goto-char (point-max))))
222
223 (defun notmuch-show-hide-markers ()
224   (save-excursion
225     (goto-char (point-min))
226     (while (not (eobp))
227       (if (re-search-forward notmuch-show-marker-regexp nil t)
228           (progn
229             (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
230                          'invisible 'notmuch-show-marker))
231         (goto-char (point-max))))))
232
233 (defun notmuch-show-markup-messages ()
234   (save-excursion
235     (goto-char (point-min))
236     (while (not (eobp))
237       (notmuch-show-markup-message)))
238   (notmuch-show-hide-markers))
239
240 (defun notmuch-show-toggle-citations-visible ()
241   "Toggle visibility of citations"
242   (interactive)
243   (if notmuch-show-citations-visible
244       (add-to-invisibility-spec 'notmuch-show-citation)
245     (remove-from-invisibility-spec 'notmuch-show-citation))
246   (set 'notmuch-show-citations-visible (not notmuch-show-citations-visible))
247   ; Need to force the redisplay for some reason
248   (force-window-update)
249   (redisplay t))
250
251 (defun notmuch-show-toggle-signatures-visible ()
252   "Toggle visibility of signatures"
253   (interactive)
254   (if notmuch-show-signatures-visible
255       (add-to-invisibility-spec 'notmuch-show-signature)
256     (remove-from-invisibility-spec 'notmuch-show-signature))
257   (set 'notmuch-show-signatures-visible (not notmuch-show-signatures-visible))
258   ; Need to force the redisplay for some reason
259   (force-window-update)
260   (redisplay t))
261
262 (defun notmuch-show-toggle-headers-visible ()
263   "Toggle visibility of header fields"
264   (interactive)
265   (if notmuch-show-headers-visible
266       (add-to-invisibility-spec 'notmuch-show-header)
267     (remove-from-invisibility-spec 'notmuch-show-header))
268   (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
269   ; Need to force the redisplay for some reason
270   (force-window-update)
271   (redisplay t))
272
273 (defun notmuch-show-toggle-body-read-visible ()
274   "Toggle visibility of message bodies of read messages"
275   (interactive)
276   (if notmuch-show-body-read-visible
277       (add-to-invisibility-spec 'notmuch-show-body-read)
278     (remove-from-invisibility-spec 'notmuch-show-body-read))
279   (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
280   ; Need to force the redisplay for some reason
281   (force-window-update)
282   (redisplay t))
283
284 ;;;###autoload
285 (defun notmuch-show-mode ()
286   "Major mode for viewing a thread with notmuch.
287
288 This buffer contains the results of the \"notmuch show\" command
289 for displaying a single thread of email from your email archives.
290
291 By default, various components of email messages, (citations,
292 signatures, already-read messages), are invisible to help you
293 focus on the most important things, (new text from unread
294 messages). See the various commands below for toggling the
295 visibility of hidden components.
296
297 The `notmuch-show-next-message' and
298 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
299 default), allow you to navigate to the next and previous
300 messages. Each time you navigate away from a message with
301 `notmuch-show-next-message' the current message will have its
302 \"unread\" tag removed.
303
304 You can add or remove tags from the current message with '+' and
305 '-'.  You can also archive all messages in the current
306 view, (remove the \"inbox\" tag from each), with
307 `notmuch-show-archive-thread' (bound to 'a' by default).
308
309 \\{notmuch-show-mode-map}"
310   (interactive)
311   (kill-all-local-variables)
312   (set (make-local-variable 'notmuch-show-headers-visible) t)
313   (notmuch-show-toggle-headers-visible)
314   (set (make-local-variable 'notmuch-show-body-read-visible) t)
315   (notmuch-show-toggle-body-read-visible)
316   (set (make-local-variable 'notmuch-show-citations-visible) t)
317   (notmuch-show-toggle-citations-visible)
318   (set (make-local-variable 'notmuch-show-signatures-visible) t)
319   (notmuch-show-toggle-signatures-visible)
320   (add-to-invisibility-spec 'notmuch-show-marker)
321   (use-local-map notmuch-show-mode-map)
322   (setq major-mode 'notmuch-show-mode
323         mode-name "notmuch-show")
324   (setq buffer-read-only t))
325
326 (defun notmuch-show (thread-id)
327   "Run \"notmuch show\" with the given thread ID and display results."
328   (interactive "sNotmuch show: ")
329   (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
330     (switch-to-buffer buffer)
331     (notmuch-show-mode)
332     (let ((proc (get-buffer-process (current-buffer)))
333           (inhibit-read-only t))
334       (if proc
335           (error "notmuch search process already running for query `%s'" query)
336         )
337       (erase-buffer)
338       (goto-char (point-min))
339       (save-excursion
340         (call-process "notmuch" nil t nil "show" thread-id)
341         (notmuch-show-markup-messages)
342         )
343       )))
344
345 (defvar notmuch-search-mode-map
346   (let ((map (make-sparse-keymap)))
347     (define-key map "a" 'notmuch-search-archive-thread)
348     (define-key map "b" 'scroll-down)
349     (define-key map "f" 'notmuch-search-filter)
350     (define-key map "n" 'next-line)
351     (define-key map "p" 'previous-line)
352     (define-key map "q" 'kill-this-buffer)
353     (define-key map "s" 'notmuch-search)
354     (define-key map "t" 'notmuch-search-filter-by-tag)
355     (define-key map "x" 'kill-this-buffer)
356     (define-key map "\r" 'notmuch-search-show-thread)
357     (define-key map "+" 'notmuch-search-add-tag)
358     (define-key map "-" 'notmuch-search-remove-tag)
359     (define-key map "<" 'beginning-of-buffer)
360     (define-key map ">" 'notmuch-search-goto-last-thread)
361     (define-key map "=" 'notmuch-search-refresh-view)
362     (define-key map "\M->" 'notmuch-search-goto-last-thread)
363     (define-key map " " 'scroll-up)
364     (define-key map (kbd "<DEL>") 'scroll-down)
365     map)
366   "Keymap for \"notmuch search\" buffers.")
367 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
368
369 (defun notmuch-search-goto-last-thread (&optional arg)
370   "Move point to the last thread in the buffer."
371   (interactive "^P")
372   (end-of-buffer arg)
373   (beginning-of-line))
374
375 ;;;###autoload
376 (defun notmuch-search-mode ()
377   "Major mode for searching mail with notmuch.
378
379 This buffer contains the results of a \"notmuch search\" of your
380 email archives. Each line in the buffer represents a single
381 thread giving a relative date for the thread and a subject.
382
383 Pressing RET on any line displays that thread. The '+' and '-'
384 keys can be used to add or remove tags from a thread. The 'a' key
385 is a convenience key for archiving a thread (removing the
386 \"inbox\" tag).
387
388 Other useful commands are `notmuch-search-filter' for filtering
389 the current search based on an additional query string,
390 `notmuch-search-filter-by-tag' for filtering to include only
391 messages with a given tag, and `notmuch-search' to execute a new,
392 global search.
393
394 \\{notmuch-search-mode-map}"
395   (interactive)
396   (kill-all-local-variables)
397   (make-local-variable 'notmuch-search-query-string)
398   (set (make-local-variable 'scroll-preserve-screen-position) t)
399   (add-to-invisibility-spec 'notmuch-search)
400   (use-local-map notmuch-search-mode-map)
401   (setq major-mode 'notmuch-search-mode
402         mode-name "notmuch-search")
403   (setq buffer-read-only t))
404
405 (defun notmuch-search-find-thread-id ()
406   (save-excursion
407     (beginning-of-line)
408     (let ((beg (point)))
409       (re-search-forward "[a-fA-F0-9]*")
410       (filter-buffer-substring beg (point)))))
411
412 (defun notmuch-search-markup-this-thread-id ()
413   (beginning-of-line)
414   (let ((beg (point)))
415     (re-search-forward "[a-fA-F0-9]*")
416     (forward-char)
417     (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)))
418
419 (defun notmuch-search-markup-thread-ids ()
420   (save-excursion
421     (goto-char (point-min))
422     (while (not (eobp))
423       (notmuch-search-markup-this-thread-id)
424       (next-line))))
425
426 (defun notmuch-search-hide-thread-ids ()
427   (interactive)
428   (add-to-invisibility-spec 'notmuch-search)
429   (force-window-update)
430   (redisplay t))
431
432 (defun notmuch-search-show-thread-ids ()
433   (interactive)
434   (remove-from-invisibility-spec 'notmuch-search)
435   (force-window-update)
436   (redisplay t))
437
438 (defun notmuch-search-show-thread ()
439   (interactive)
440   (notmuch-show (notmuch-search-find-thread-id)))
441
442 (defun notmuch-call-notmuch-process (&rest args)
443   (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
444     (with-current-buffer error-buffer
445         (erase-buffer))
446     (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
447         (point)
448       (progn
449         (with-current-buffer error-buffer
450           (let ((beg (point-min))
451                 (end (- (point-max) 1)))
452             (error (buffer-substring beg end))
453             ))))))
454
455 (defun notmuch-search-set-tags (tags)
456   (save-excursion
457     (end-of-line)
458     (re-search-backward "(")
459     (forward-char)
460     (let ((beg (point))
461           (inhibit-read-only t))
462       (re-search-forward ")")
463       (backward-char)
464       (let ((end (point)))
465         (delete-region beg end)
466         (insert (mapconcat  'identity tags " "))))))
467
468 (defun notmuch-search-get-tags ()
469   (save-excursion
470     (end-of-line)
471     (re-search-backward "(")
472     (let ((beg (+ (point) 1)))
473       (re-search-forward ")")
474       (let ((end (- (point) 1)))
475         (split-string (buffer-substring beg end))))))
476
477 (defun notmuch-search-add-tag (tag)
478   (interactive "sTag to add: ")
479   (notmuch-call-notmuch-process "tag" (concat "+" tag) (concat "thread:" (notmuch-search-find-thread-id)))
480   (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
481
482 (defun notmuch-search-remove-tag (tag)
483   (interactive "sTag to remove: ")
484   (notmuch-call-notmuch-process "tag" (concat "-" tag) (concat "thread:" (notmuch-search-find-thread-id)))
485   (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
486
487 (defun notmuch-search-archive-thread ()
488   "Archive the current thread (remove its \"inbox\" tag).
489
490 This function advances point to the next line when finished."
491   (interactive)
492   (notmuch-search-remove-tag "inbox")
493   (next-line))
494
495 (defun notmuch-search (query)
496   "Run \"notmuch search\" with the given query string and display results."
497   (interactive "sNotmuch search: ")
498   (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
499     (switch-to-buffer buffer)
500     (notmuch-search-mode)
501     (set 'notmuch-search-query-string query)
502     (let ((proc (get-buffer-process (current-buffer)))
503           (inhibit-read-only t))
504       (if proc
505           (error "notmuch search process already running for query `%s'" query)
506         )
507       (erase-buffer)
508       (goto-char (point-min))
509       (save-excursion
510         (call-process "notmuch" nil t nil "search" query)
511         (notmuch-search-markup-thread-ids)
512         ; A well-behaved program ends its output with a newline, but we
513         ; don't actually want the blank line at the end of the file.
514         (goto-char (point-max))
515         (if (looking-at "^$")
516             (delete-backward-char 1)
517           )
518         ))))
519
520 (defun notmuch-search-refresh-view ()
521   "Refresh the current view.
522
523 Kills the current buffer and runs a new search with the same
524 query string as the current search. If the current thread is in
525 the new search results, then point will be placed on the same
526 thread. Otherwise, point will be moved to attempt to be in the
527 same relative position within the new buffer."
528   (interactive)
529   (let ((here (point))
530         (thread (notmuch-search-find-thread-id))
531         (query notmuch-search-query-string))
532     (kill-this-buffer)
533     (notmuch-search query)
534     (goto-char (point-min))
535     (if (re-search-forward (concat "^" thread) nil t)
536         (beginning-of-line)
537       (goto-char here))))
538
539 (defun notmuch-search-filter (query)
540   "Filter the current search results based on an additional query string.
541
542 Runs a new search matching only messages that match both the
543 current search results AND the additional query string provided."
544   (interactive "sFilter search: ")
545   (notmuch-search (concat notmuch-search-query-string " and " query)))
546
547 (defun notmuch-search-filter-by-tag (tag)
548   "Filter the current search results based on a single tag.
549
550 Runs a new search matching only messages that match both the
551 current search results AND that are tagged with the given tag."
552   (interactive "sFilter by tag: ")
553   (notmuch-search (concat notmuch-search-query-string " and tag:" tag)))
554
555 (defun notmuch ()
556   "Run notmuch to display all mail with tag of 'inbox'"
557   (interactive)
558   (notmuch-search "tag:inbox"))
559
560 (provide 'notmuch)