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