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