]> git.notmuchmail.org Git - notmuch/blob - notmuch.el
f1523ac0419c5dac502469339a26fac637502a6f
[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 (require 'cl)
23 (require 'mm-view)
24
25 (defvar notmuch-show-mode-map
26   (let ((map (make-sparse-keymap)))
27     ; I don't actually want all of these toggle commands occupying
28     ; keybindings. They steal valuable key-binding space, are hard
29     ; to remember, and act globally rather than locally.
30     ;
31     ; Will be much preferable to switch to direct manipulation for
32     ; toggling visibility of these components. Probably using
33     ; overlays-at to query and manipulate the current overlay.
34     (define-key map "a" 'notmuch-show-archive-thread)
35     (define-key map "A" 'notmuch-show-mark-read-then-archive-thread)
36     (define-key map "b" 'notmuch-show-toggle-body-read-visible)
37     (define-key map "c" 'notmuch-show-toggle-citations-visible)
38     (define-key map "h" 'notmuch-show-toggle-headers-visible)
39     (define-key map "m" 'message-mail)
40     (define-key map "n" 'notmuch-show-next-message)
41     (define-key map "N" 'notmuch-show-mark-read-then-next-open-message)
42     (define-key map "p" 'notmuch-show-previous-message)
43     (define-key map (kbd "C-n") 'notmuch-show-next-line)
44     (define-key map (kbd "C-p") 'notmuch-show-previous-line)
45     (define-key map "q" 'kill-this-buffer)
46     (define-key map "r" 'notmuch-show-reply)
47     (define-key map "s" 'notmuch-show-toggle-signatures-visible)
48     (define-key map "v" 'notmuch-show-view-all-mime-parts)
49     (define-key map "w" 'notmuch-show-view-raw-message)
50     (define-key map "x" 'kill-this-buffer)
51     (define-key map "+" 'notmuch-show-add-tag)
52     (define-key map "-" 'notmuch-show-remove-tag)
53     (define-key map (kbd "DEL") 'notmuch-show-rewind)
54     (define-key map " " 'notmuch-show-advance-marking-read-and-archiving)
55     (define-key map "|" 'notmuch-show-pipe-message)
56     map)
57   "Keymap for \"notmuch show\" buffers.")
58 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
59
60 (defvar notmuch-show-signature-lines-max 12
61   "Maximum length of signature that will be hidden by default.")
62
63 (set 'notmuch-show-message-begin-regexp    "\fmessage{")
64 (set 'notmuch-show-message-end-regexp      "\fmessage}")
65 (set 'notmuch-show-header-begin-regexp     "\fheader{")
66 (set 'notmuch-show-header-end-regexp       "\fheader}")
67 (set 'notmuch-show-body-begin-regexp       "\fbody{")
68 (set 'notmuch-show-body-end-regexp         "\fbody}")
69 (set 'notmuch-show-attachment-begin-regexp "\fattachment{")
70 (set 'notmuch-show-attachment-end-regexp   "\fattachment}")
71 (set 'notmuch-show-part-begin-regexp       "\fpart{")
72 (set 'notmuch-show-part-end-regexp         "\fpart}")
73 (set 'notmuch-show-marker-regexp "\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
74
75 (set 'notmuch-show-id-regexp "\\(id:[^ ]*\\)")
76 (set 'notmuch-show-depth-regexp " depth:\\([0-9]*\\) ")
77 (set 'notmuch-show-filename-regexp "filename:\\(.*\\)$")
78 (set 'notmuch-show-tags-regexp "(\\([^)]*\\))$")
79
80 ; XXX: This should be a generic function in emacs somewhere, not here
81 (defun point-invisible-p ()
82   "Return whether the character at point is invisible.
83
84 Here visibility is determined by `buffer-invisibility-spec' and
85 the invisible property of any overlays for point. It doesn't have
86 anything to do with whether point is currently being displayed
87 within the current window."
88   (let ((prop (get-char-property (point) 'invisible)))
89     (if (eq buffer-invisibility-spec t)
90         prop
91       (or (memq prop buffer-invisibility-spec)
92           (assq prop buffer-invisibility-spec)))))
93
94 (defun notmuch-show-next-line ()
95   "Like builtin `next-line' but ensuring we end on a visible character.
96
97 By advancing forward until reaching a visible character.
98
99 Unlike builtin `next-line' this version accepts no arguments."
100   (interactive)
101   (set 'this-command 'next-line)
102   (call-interactively 'next-line)
103   (while (point-invisible-p)
104     (forward-char)))
105
106 (defun notmuch-show-previous-line ()
107   "Like builtin `previous-line' but ensuring we end on a visible character.
108
109 By advancing forward until reaching a visible character.
110
111 Unlike builtin `next-line' this version accepts no arguments."
112   (interactive)
113   (set 'this-command 'previous-line)
114   (call-interactively 'previous-line)
115   (while (point-invisible-p)
116     (forward-char)))
117
118 (defun notmuch-show-get-message-id ()
119   (save-excursion
120     (beginning-of-line)
121     (if (not (looking-at notmuch-show-message-begin-regexp))
122         (re-search-backward notmuch-show-message-begin-regexp))
123     (re-search-forward notmuch-show-id-regexp)
124     (buffer-substring (match-beginning 1) (match-end 1))))
125
126 (defun notmuch-show-get-filename ()
127   (save-excursion
128     (beginning-of-line)
129     (if (not (looking-at notmuch-show-message-begin-regexp))
130         (re-search-backward notmuch-show-message-begin-regexp))
131     (re-search-forward notmuch-show-filename-regexp)
132     (buffer-substring (match-beginning 1) (match-end 1))))
133
134 (defun notmuch-show-set-tags (tags)
135   (save-excursion
136     (beginning-of-line)
137     (if (not (looking-at notmuch-show-message-begin-regexp))
138         (re-search-backward notmuch-show-message-begin-regexp))
139     (re-search-forward notmuch-show-tags-regexp)
140     (let ((inhibit-read-only t)
141           (beg (match-beginning 1))
142           (end (match-end 1)))
143       (delete-region beg end)
144       (goto-char beg)
145       (insert (mapconcat 'identity tags " ")))))
146
147 (defun notmuch-show-get-tags ()
148   (save-excursion
149     (beginning-of-line)
150     (if (not (looking-at notmuch-show-message-begin-regexp))
151         (re-search-backward notmuch-show-message-begin-regexp))
152     (re-search-forward notmuch-show-tags-regexp)
153     (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
154
155 (defun notmuch-show-add-tag (&rest toadd)
156   "Add a tag to the current message."
157   (interactive "sTag to add: ")
158   (apply 'notmuch-call-notmuch-process
159          (append (cons "tag"
160                        (mapcar (lambda (s) (concat "+" s)) toadd))
161                  (cons (notmuch-show-get-message-id) nil)))
162   (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
163
164 (defun notmuch-show-remove-tag (&rest toremove)
165   "Remove a tag from the current message."
166   (interactive "sTag to remove: ")
167   (let ((tags (notmuch-show-get-tags)))
168     (if (intersection tags toremove :test 'string=)
169         (progn
170           (apply 'notmuch-call-notmuch-process
171                  (append (cons "tag"
172                                (mapcar (lambda (s) (concat "-" s)) toremove))
173                          (cons (notmuch-show-get-message-id) nil)))
174           (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
175
176 (defun notmuch-show-archive-thread-maybe-mark-read (markread)
177   (save-excursion
178     (goto-char (point-min))
179     (while (not (eobp))
180       (if markread
181           (notmuch-show-remove-tag "unread" "inbox")
182         (notmuch-show-remove-tag "inbox"))
183       (if (not (eobp))
184           (forward-char))
185       (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
186           (goto-char (point-max)))))
187   (let ((parent-buffer notmuch-show-parent-buffer))
188     (kill-this-buffer)
189     (if parent-buffer
190         (progn
191           (switch-to-buffer parent-buffer)
192           (forward-line)
193           (notmuch-search-show-thread)))))
194
195 (defun notmuch-show-mark-read-then-archive-thread ()
196   "Remove \"unread\" tag from each message, then archive and show next thread.
197
198 Archive each message currrently shown by removing the \"unread\"
199 and \"inbox\" tag from each. Then kill this buffer and show the
200 next thread from the search from which this thread was originally
201 shown.
202
203 Note: This command is safe from any race condition of new messages
204 being delivered to the same thread. It does not archive the
205 entire thread, but only the messages shown in the current
206 buffer."
207   (interactive)
208   (notmuch-show-archive-thread-maybe-mark-read t))
209
210 (defun notmuch-show-archive-thread ()
211   "Archive each message in thread, and show next thread from search.
212
213 Archive each message currrently shown by removing the \"inbox\"
214 tag from each. Then kill this buffer and show the next thread
215 from the search from which this thread was originally shown.
216
217 Note: This command is safe from any race condition of new messages
218 being delivered to the same thread. It does not archive the
219 entire thread, but only the messages shown in the current
220 buffer."
221   (interactive)
222   (notmuch-show-archive-thread-maybe-mark-read nil))
223
224 (defun notmuch-show-view-raw-message ()
225   "View the raw email of the current message."
226   (interactive)
227   (view-file (notmuch-show-get-filename)))
228
229 (defun notmuch-show-view-all-mime-parts ()
230   "Use external viewers (according to mailcap) to view all MIME-encoded parts."
231   (interactive)
232   (save-excursion
233     (let ((filename (notmuch-show-get-filename)))
234       (switch-to-buffer (generate-new-buffer (concat "*notmuch-mime-"
235                                                      filename
236                                                      "*")))
237       (insert-file-contents filename nil nil nil t)
238       (mm-display-parts (mm-dissect-buffer))
239       (kill-this-buffer))))
240
241 (defun notmuch-show-reply ()
242   "Begin composing a reply to the current message in a new buffer."
243   (interactive)
244   (let ((message-id (notmuch-show-get-message-id)))
245     (switch-to-buffer (generate-new-buffer "notmuch-draft"))
246     (call-process "notmuch" nil t nil "reply" message-id)
247     (goto-char (point-min))
248     (if (re-search-forward "^$" nil t)
249         (progn
250           (insert "--text follows this line--")
251           (forward-line)))
252     (message-mode)))
253
254 (defun notmuch-show-pipe-message (command)
255   "Pipe the contents of the current message to the given command.
256
257 The given command will be executed with the raw contents of the
258 current email message as stdin. Anything printed by the command
259 to stdout or stderr will appear in the *Messages* buffer."
260   (interactive "sPipe message to command: ")
261   (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*" (split-string (concat command " < " (notmuch-show-get-filename)))))
262
263 (defun notmuch-show-move-to-current-message-summary-line ()
264   "Move to the beginning of the one-line summary of the current message.
265
266 This gives us a stable place to move to and work from since the
267 summary line is always visible. This is important since moving to
268 an invisible location is unreliable, (the main command loop moves
269 point either forward or backward to the next visible character
270 when a command ends with point on an invisible character).
271
272 Emits an error if point is not within a valid message, (that is
273 not pattern of `notmuch-show-message-begin-regexp' could be found
274 by searching backward)."
275   (beginning-of-line)
276   (if (not (looking-at notmuch-show-message-begin-regexp))
277       (if (re-search-backward notmuch-show-message-begin-regexp nil t)
278           (forward-line 2)
279         (error "Not within a valid message."))
280     (forward-line 2)))
281
282 (defun notmuch-show-last-message-p ()
283   "Predicate testing whether point is within the last message."
284   (save-window-excursion
285     (save-excursion
286       (notmuch-show-move-to-current-message-summary-line)
287       (not (re-search-forward notmuch-show-message-begin-regexp nil t)))))
288
289 (defun notmuch-show-message-unread-p ()
290   "Preficate testing whether current message is unread."
291   (member "unread" (notmuch-show-get-tags)))
292
293 (defun notmuch-show-next-message ()
294   "Advance to the beginning of the next message in the buffer.
295
296 Moves to the last visible character of the current message if
297 already on the last message in the buffer."
298   (interactive)
299   (notmuch-show-move-to-current-message-summary-line)
300   (if (re-search-forward notmuch-show-message-begin-regexp nil t)
301       (notmuch-show-move-to-current-message-summary-line)
302     (goto-char (- (point-max) 1))
303     (while (point-invisible-p)
304       (backward-char)))
305   (recenter 0))
306
307 (defun notmuch-show-find-next-message ()
308   "Returns the position of the next message in the buffer.
309
310 Or the position of the last visible character of the current
311 message if already within the last message in the buffer."
312   ; save-excursion doesn't save our window position
313   ; save-window-excursion doesn't save point
314   ; Looks like we have to use both.
315   (save-excursion
316     (save-window-excursion
317       (notmuch-show-next-message)
318       (point))))
319
320 (defun notmuch-show-next-unread-message ()
321   "Advance to the beginning of the next unread message in the buffer.
322
323 Moves to the last visible character of the current message if
324 there are no more unread messages past the current point."
325   (notmuch-show-next-message)
326   (while (and (not (notmuch-show-last-message-p))
327               (not (notmuch-show-message-unread-p)))
328     (notmuch-show-next-message))
329   (if (not (notmuch-show-message-unread-p))
330       (notmuch-show-next-message)))
331
332 (defun notmuch-show-next-open-message ()
333   "Advance to the the next message which is not hidden.
334
335 If read messages are currently hidden, advance to the next unread
336 message. Otherwise, advance to the next message."
337   (if (or (memq 'notmuch-show-body-read buffer-invisibility-spec)
338           (assq 'notmuch-show-body-read buffer-invisibility-spec))
339       (notmuch-show-next-unread-message)
340     (notmuch-show-next-message)))
341
342 (defun notmuch-show-previous-message ()
343   "Backup to the beginning of the previous message in the buffer.
344
345 If within a message rather than at the beginning of it, then
346 simply move to the beginning of the current message."
347   (interactive)
348   (let ((start (point)))
349     (notmuch-show-move-to-current-message-summary-line)
350     (if (not (< (point) start))
351         ; Go backward twice to skip the current message's marker
352         (progn
353           (re-search-backward notmuch-show-message-begin-regexp nil t)
354           (re-search-backward notmuch-show-message-begin-regexp nil t)
355           (notmuch-show-move-to-current-message-summary-line)
356           ))
357     (recenter 0)))
358
359 (defun notmuch-show-find-previous-message ()
360   "Returns the position of the previous message in the buffer.
361
362 Or the position of the beginning of the current message if point
363 is originally within the message rather than at the beginning of
364 it."
365   ; save-excursion doesn't save our window position
366   ; save-window-excursion doesn't save point
367   ; Looks like we have to use both.
368   (save-excursion
369     (save-window-excursion
370       (notmuch-show-previous-message)
371       (point))))
372
373 (defun notmuch-show-mark-read-then-next-open-message ()
374   "Remove unread tag from current message, then advance to next unread message."
375   (interactive)
376   (notmuch-show-remove-tag "unread")
377   (notmuch-show-next-open-message))
378
379 (defun notmuch-show-rewind ()
380   "Do reverse scrolling compared to `notmuch-show-advance-marking-read-and-archiving'
381
382 Specifically, if the beginning of the previous email is fewer
383 than `window-height' lines from the current point, move to it
384 just like `notmuch-show-previous-message'.
385
386 Otherwise, just scroll down a screenful of the current message.
387
388 This command does not modify any message tags, (it does not undo
389 any effects from previous calls to
390 `notmuch-show-advance-marking-read-and-archiving'."
391   (interactive)
392   (let ((previous (notmuch-show-find-previous-message)))
393     (if (> (count-lines previous (point)) (- (window-height) next-screen-context-lines))
394         (progn
395           (condition-case nil
396               (scroll-down nil)
397             ((beginning-of-buffer) nil))
398           (goto-char (window-start)))
399       (notmuch-show-previous-message))))
400
401 (defun notmuch-show-advance-marking-read-and-archiving ()
402   "Advance through buffer, marking read and archiving.
403
404 This command is intended to be one of the simplest ways to
405 process a thread of email. It does the following:
406
407 If the current message in the thread is not yet fully visible,
408 scroll by a near screenful to read more of the message.
409
410 Otherwise, (the end of the current message is already within the
411 current window), remove the \"unread\" tag (if present) from the
412 current message and advance to the next open message.
413
414 Finally, if there is no further message to advance to, and this
415 last message is already read, then archive the entire current
416 thread, (remove the \"inbox\" tag from each message). Also kill
417 this buffer, and display the next thread from the search from
418 which this thread was originally shown."
419   (interactive)
420   (let ((next (notmuch-show-find-next-message))
421         (unread (notmuch-show-message-unread-p)))
422     (if (> next (window-end))
423         (scroll-up nil)
424       (let ((last (notmuch-show-last-message-p)))
425         (notmuch-show-mark-read-then-next-open-message)
426         (if last
427             (notmuch-show-archive-thread))))))
428
429 (defun notmuch-show-markup-citations-region (beg end depth)
430   (goto-char beg)
431   (beginning-of-line)
432   (while (< (point) end)
433     (let ((beg-sub (point-marker))
434           (indent (make-string depth ? ))
435           (citation "[[:space:]]*>"))
436       (if (looking-at citation)
437           (progn
438             (while (looking-at citation)
439               (forward-line))
440             (let ((overlay (make-overlay beg-sub (point))))
441               (overlay-put overlay 'invisible 'notmuch-show-citation)
442               (overlay-put overlay 'before-string
443                            (concat indent
444                                    "[" (number-to-string (count-lines beg-sub (point)))
445                                    "-line citation. Press 'c' to show.]\n")))))
446       (if (looking-at "[[:space:]]*-- ?$")
447           (let ((sig-lines (- (count-lines beg-sub end) 1)))
448             (if (<= sig-lines notmuch-show-signature-lines-max)
449                 (progn
450                   (overlay-put (make-overlay beg-sub end)
451                                'invisible 'notmuch-show-signature)
452                   (overlay-put (make-overlay beg (- beg-sub 1))
453                                'after-string
454                                (concat "\n" indent
455                                        "[" (number-to-string sig-lines)
456                                        "-line signature. Press 's' to show.]"))
457                   (goto-char end)))))
458       (forward-line))))
459
460 (defun notmuch-show-markup-part (beg end depth)
461   (if (re-search-forward notmuch-show-part-begin-regexp nil t)
462       (progn
463         (forward-line)
464         (let ((beg (point-marker)))
465           (re-search-forward notmuch-show-part-end-regexp)
466           (let ((end (copy-marker (match-beginning 0))))
467             (goto-char end)
468             (if (not (bolp))
469                 (insert "\n"))
470             (indent-rigidly beg end depth)
471             (notmuch-show-markup-citations-region beg end depth)
472             ; Advance to the next part (if any) (so the outer loop can
473             ; determine whether we've left the current message.
474             (if (re-search-forward notmuch-show-part-begin-regexp nil t)
475                 (beginning-of-line)))))
476     (goto-char end)))
477
478 (defun notmuch-show-markup-parts-region (beg end depth)
479   (save-excursion
480     (goto-char beg)
481     (while (< (point) end)
482       (notmuch-show-markup-part beg end depth))))
483
484 (defun notmuch-show-markup-body (depth)
485   (re-search-forward notmuch-show-body-begin-regexp)
486   (forward-line)
487   (let ((beg (point-marker)))
488     (re-search-forward notmuch-show-body-end-regexp)
489     (let ((end (copy-marker (match-beginning 0))))
490       (notmuch-show-markup-parts-region beg end depth)
491       (if (not (notmuch-show-message-unread-p))
492           (overlay-put (make-overlay beg end)
493                        'invisible 'notmuch-show-body-read))
494       (set-marker beg nil)
495       (set-marker end nil)
496       )))
497
498 (defun notmuch-show-markup-header (depth)
499   (re-search-forward notmuch-show-header-begin-regexp)
500   (forward-line)
501   (let ((beg (point-marker)))
502     (end-of-line)
503     ; Inverse video for subject
504     (overlay-put (make-overlay beg (point)) 'face '((cons :inverse-video t)))
505     (forward-line 2)
506     (let ((beg-hidden (point-marker)))
507       (re-search-forward notmuch-show-header-end-regexp)
508       (beginning-of-line)
509       (let ((end (point-marker)))
510         (indent-rigidly beg end depth)
511         (overlay-put (make-overlay beg-hidden end)
512                      'invisible 'notmuch-show-header)
513         (set-marker beg nil)
514         (set-marker beg-hidden nil)
515         (set-marker end nil)
516         ))))
517
518 (defun notmuch-show-markup-message ()
519   (if (re-search-forward notmuch-show-message-begin-regexp nil t)
520       (progn
521         (re-search-forward notmuch-show-depth-regexp)
522         (let ((depth (string-to-number (buffer-substring (match-beginning 1) (match-end 1)))))
523           (notmuch-show-markup-header depth)
524           (notmuch-show-markup-body depth)))
525     (goto-char (point-max))))
526
527 (defun notmuch-show-hide-markers ()
528   (save-excursion
529     (goto-char (point-min))
530     (while (not (eobp))
531       (if (re-search-forward notmuch-show-marker-regexp nil t)
532           (progn
533             (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
534                          'invisible 'notmuch-show-marker))
535         (goto-char (point-max))))))
536
537 (defun notmuch-show-markup-messages ()
538   (save-excursion
539     (goto-char (point-min))
540     (while (not (eobp))
541       (notmuch-show-markup-message)))
542   (notmuch-show-hide-markers))
543
544 (defun notmuch-show-toggle-citations-visible ()
545   "Toggle visibility of citations"
546   (interactive)
547   (if notmuch-show-citations-visible
548       (add-to-invisibility-spec 'notmuch-show-citation)
549     (remove-from-invisibility-spec 'notmuch-show-citation))
550   (set 'notmuch-show-citations-visible (not notmuch-show-citations-visible))
551   ; Need to force the redisplay for some reason
552   (force-window-update)
553   (redisplay t))
554
555 (defun notmuch-show-toggle-signatures-visible ()
556   "Toggle visibility of signatures"
557   (interactive)
558   (if notmuch-show-signatures-visible
559       (add-to-invisibility-spec 'notmuch-show-signature)
560     (remove-from-invisibility-spec 'notmuch-show-signature))
561   (set 'notmuch-show-signatures-visible (not notmuch-show-signatures-visible))
562   ; Need to force the redisplay for some reason
563   (force-window-update)
564   (redisplay t))
565
566 (defun notmuch-show-toggle-headers-visible ()
567   "Toggle visibility of header fields"
568   (interactive)
569   (if notmuch-show-headers-visible
570       (add-to-invisibility-spec 'notmuch-show-header)
571     (remove-from-invisibility-spec 'notmuch-show-header))
572   (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
573   ; Need to force the redisplay for some reason
574   (force-window-update)
575   (redisplay t))
576
577 (defun notmuch-show-toggle-body-read-visible ()
578   "Toggle visibility of message bodies of read messages"
579   (interactive)
580   (if notmuch-show-body-read-visible
581       (add-to-invisibility-spec 'notmuch-show-body-read)
582     (remove-from-invisibility-spec 'notmuch-show-body-read))
583   (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
584   ; Need to force the redisplay for some reason
585   (force-window-update)
586   (redisplay t))
587
588 ;;;###autoload
589 (defun notmuch-show-mode ()
590   "Major mode for viewing a thread with notmuch.
591
592 This buffer contains the results of the \"notmuch show\" command
593 for displaying a single thread of email from your email archives.
594
595 By default, various components of email messages, (citations,
596 signatures, already-read messages), are invisible to help you
597 focus on the most important things, (new text from unread
598 messages). See the various commands below for toggling the
599 visibility of hidden components.
600
601 The `notmuch-show-next-message' and
602 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
603 default), allow you to navigate to the next and previous
604 messages. Each time you navigate away from a message with
605 `notmuch-show-next-message' the current message will have its
606 \"unread\" tag removed.
607
608 You can add or remove tags from the current message with '+' and
609 '-'.  You can also archive all messages in the current
610 view, (remove the \"inbox\" tag from each), with
611 `notmuch-show-archive-thread' (bound to 'a' by default).
612
613 \\{notmuch-show-mode-map}"
614   (interactive)
615   (kill-all-local-variables)
616   (set (make-local-variable 'notmuch-show-headers-visible) t)
617   (notmuch-show-toggle-headers-visible)
618   (set (make-local-variable 'notmuch-show-body-read-visible) t)
619   (notmuch-show-toggle-body-read-visible)
620   (set (make-local-variable 'notmuch-show-citations-visible) t)
621   (notmuch-show-toggle-citations-visible)
622   (set (make-local-variable 'notmuch-show-signatures-visible) t)
623   (notmuch-show-toggle-signatures-visible)
624   (add-to-invisibility-spec 'notmuch-show-marker)
625   (use-local-map notmuch-show-mode-map)
626   (setq major-mode 'notmuch-show-mode
627         mode-name "notmuch-show")
628   (setq buffer-read-only t))
629
630 (defun notmuch-show (thread-id &optional parent-buffer)
631   "Run \"notmuch show\" with the given thread ID and display results.
632
633 The optional PARENT-BUFFER is the notmuch-search buffer from
634 which this notmuch-show command was executed, (so that the next
635 thread from that buffer can be show when done with this one)."
636   (interactive "sNotmuch show: ")
637   (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
638     (switch-to-buffer buffer)
639     (notmuch-show-mode)
640     (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
641     (let ((proc (get-buffer-process (current-buffer)))
642           (inhibit-read-only t))
643       (if proc
644           (error "notmuch search process already running for query `%s'" query)
645         )
646       (erase-buffer)
647       (goto-char (point-min))
648       (save-excursion
649         (call-process "notmuch" nil t nil "show" thread-id)
650         (notmuch-show-markup-messages)
651         )
652       ; Move straight to the first unread message
653       (if (not (notmuch-show-message-unread-p))
654           (progn
655             (notmuch-show-next-unread-message)
656             ; But if there are no unread messages, go back to the
657             ; beginning of the buffer, and open up the bodies of all
658             ; read message.
659             (if (not (notmuch-show-message-unread-p))
660                 (progn
661                   (goto-char (point-min))
662                   (notmuch-show-toggle-body-read-visible)))))
663       )))
664
665 (defvar notmuch-search-authors-width 40
666   "Number of columns to use to diplay authors in a notmuch-search buffer.")
667
668 (defvar notmuch-search-mode-map
669   (let ((map (make-sparse-keymap)))
670     (define-key map "a" 'notmuch-search-archive-thread)
671     (define-key map "b" 'notmuch-search-scroll-down)
672     (define-key map "f" 'notmuch-search-filter)
673     (define-key map "m" 'message-mail)
674     (define-key map "n" 'next-line)
675     (define-key map "o" 'notmuch-search-toggle-order)
676     (define-key map "p" 'previous-line)
677     (define-key map "q" 'kill-this-buffer)
678     (define-key map "s" 'notmuch-search)
679     (define-key map "t" 'notmuch-search-filter-by-tag)
680     (define-key map "x" 'kill-this-buffer)
681     (define-key map (kbd "RET") 'notmuch-search-show-thread)
682     (define-key map "+" 'notmuch-search-add-tag)
683     (define-key map "-" 'notmuch-search-remove-tag)
684     (define-key map "<" 'beginning-of-buffer)
685     (define-key map ">" 'notmuch-search-goto-last-thread)
686     (define-key map "=" 'notmuch-search-refresh-view)
687     (define-key map "\M->" 'notmuch-search-goto-last-thread)
688     (define-key map " " 'notmuch-search-scroll-up)
689     (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
690     map)
691   "Keymap for \"notmuch search\" buffers.")
692 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
693
694 (defun notmuch-search-scroll-up ()
695   "Scroll up, moving point to last message in thread if at end."
696   (interactive)
697   (condition-case nil
698       (scroll-up nil)
699     ((end-of-buffer) (notmuch-search-goto-last-thread))))
700
701 (defun notmuch-search-scroll-down ()
702   "Scroll down, moving point to first message in thread if at beginning."
703   (interactive)
704   ; I don't know why scroll-down doesn't signal beginning-of-buffer
705   ; the way that scroll-up signals end-of-buffer, but c'est la vie.
706   ;
707   ; So instead of trapping a signal we instead check whether the
708   ; window begins on the first line of the buffer and if so, move
709   ; directly to that position. (We have to count lines since the
710   ; window-start position is not the same as point-min due to the
711   ; invisible thread-ID characters on the first line.
712   (if (equal (count-lines (point-min) (window-start)) 1)
713       (goto-char (window-start))
714     (scroll-down nil)))
715
716 (defun notmuch-search-goto-last-thread (&optional arg)
717   "Move point to the last thread in the buffer."
718   (interactive "^P")
719   (end-of-buffer arg)
720   (forward-line -1))
721
722 ;;;###autoload
723 (defun notmuch-search-mode ()
724   "Major mode for searching mail with notmuch.
725
726 This buffer contains the results of a \"notmuch search\" of your
727 email archives. Each line in the buffer represents a single
728 thread giving a relative date for the thread and a subject.
729
730 Pressing RET on any line displays that thread. The '+' and '-'
731 keys can be used to add or remove tags from a thread. The 'a' key
732 is a convenience key for archiving a thread (removing the
733 \"inbox\" tag).
734
735 Other useful commands are `notmuch-search-filter' for filtering
736 the current search based on an additional query string,
737 `notmuch-search-filter-by-tag' for filtering to include only
738 messages with a given tag, and `notmuch-search' to execute a new,
739 global search.
740
741 \\{notmuch-search-mode-map}"
742   (interactive)
743   (kill-all-local-variables)
744   (make-local-variable 'notmuch-search-query-string)
745   (make-local-variable 'notmuch-search-oldest-first)
746   (set (make-local-variable 'scroll-preserve-screen-position) t)
747   (add-to-invisibility-spec 'notmuch-search)
748   (use-local-map notmuch-search-mode-map)
749   (setq major-mode 'notmuch-search-mode
750         mode-name "notmuch-search")
751   (setq buffer-read-only t))
752
753 (defun notmuch-search-find-thread-id ()
754   (save-excursion
755     (beginning-of-line)
756     (let ((beg (point)))
757       (re-search-forward "thread:[a-fA-F0-9]*" nil t)
758       (filter-buffer-substring beg (point)))))
759
760 (defun notmuch-search-markup-this-thread-id ()
761   (beginning-of-line)
762   (let ((beg (point)))
763     (if (re-search-forward "thread:[a-fA-F0-9]*" nil t)
764         (progn
765           (forward-char)
766           (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)
767           (re-search-forward ".*\\[[0-9]*/[0-9]*\\] \\([^;]*\\)\\(;\\)")
768           (let* ((authors (buffer-substring (match-beginning 1) (match-end 1)))
769                  (authors-length (length authors)))
770             ;; Drop the semi-colon
771             (replace-match "" t nil nil 2)
772             (if (<= authors-length notmuch-search-authors-width)
773                 (replace-match (concat authors (make-string
774                                                 (- notmuch-search-authors-width
775                                                    authors-length) ? )) t t nil 1)
776               (replace-match (concat (substring authors 0 (- notmuch-search-authors-width 3)) "...") t t nil 1)))))))
777
778 (defun notmuch-search-markup-thread-ids ()
779   (save-excursion
780     (goto-char (point-min))
781     (while (not (eobp))
782       (notmuch-search-markup-this-thread-id)
783       (next-line))))
784
785 (defun notmuch-search-show-thread ()
786   (interactive)
787   (let ((thread-id (notmuch-search-find-thread-id)))
788     (if (> (length thread-id) 0)
789         (notmuch-show thread-id (current-buffer))
790       (error "End of search results"))))
791
792 (defun notmuch-call-notmuch-process (&rest args)
793   "Synchronously invoke \"notmuch\" with the given list of arguments.
794
795 Output from the process will be presented to the user as an error
796 and will also appear in a buffer named \"*Notmuch errors*\"."
797   (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
798     (with-current-buffer error-buffer
799         (erase-buffer))
800     (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
801         (point)
802       (progn
803         (with-current-buffer error-buffer
804           (let ((beg (point-min))
805                 (end (- (point-max) 1)))
806             (error (buffer-substring beg end))
807             ))))))
808
809 (defun notmuch-search-set-tags (tags)
810   (save-excursion
811     (end-of-line)
812     (re-search-backward "(")
813     (forward-char)
814     (let ((beg (point))
815           (inhibit-read-only t))
816       (re-search-forward ")")
817       (backward-char)
818       (let ((end (point)))
819         (delete-region beg end)
820         (insert (mapconcat  'identity tags " "))))))
821
822 (defun notmuch-search-get-tags ()
823   (save-excursion
824     (end-of-line)
825     (re-search-backward "(")
826     (let ((beg (+ (point) 1)))
827       (re-search-forward ")")
828       (let ((end (- (point) 1)))
829         (split-string (buffer-substring beg end))))))
830
831 (defun notmuch-search-add-tag (tag)
832   (interactive "sTag to add: ")
833   (notmuch-call-notmuch-process "tag" (concat "+" tag) (notmuch-search-find-thread-id))
834   (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
835
836 (defun notmuch-search-remove-tag (tag)
837   (interactive "sTag to remove: ")
838   (notmuch-call-notmuch-process "tag" (concat "-" tag) (notmuch-search-find-thread-id))
839   (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
840
841 (defun notmuch-search-archive-thread ()
842   "Archive the current thread (remove its \"inbox\" tag).
843
844 This function advances point to the next line when finished."
845   (interactive)
846   (notmuch-search-remove-tag "inbox")
847   (forward-line))
848
849 (defun notmuch-search (query &optional oldest-first)
850   "Run \"notmuch search\" with the given query string and display results."
851   (interactive "sNotmuch search: ")
852   (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
853     (switch-to-buffer buffer)
854     (notmuch-search-mode)
855     (set 'notmuch-search-query-string query)
856     (set 'notmuch-search-oldest-first oldest-first)
857     (let ((proc (get-buffer-process (current-buffer)))
858           (inhibit-read-only t))
859       (if proc
860           (error "notmuch search process already running for query `%s'" query)
861         )
862       (erase-buffer)
863       (goto-char (point-min))
864       (save-excursion
865         (if oldest-first
866             (call-process "notmuch" nil t nil "search" query)
867           (call-process "notmuch" nil t nil "search" "--reverse" query))
868         (notmuch-search-markup-thread-ids)
869         ))))
870
871 (defun notmuch-search-refresh-view ()
872   "Refresh the current view.
873
874 Kills the current buffer and runs a new search with the same
875 query string as the current search. If the current thread is in
876 the new search results, then point will be placed on the same
877 thread. Otherwise, point will be moved to attempt to be in the
878 same relative position within the new buffer."
879   (interactive)
880   (let ((here (point))
881         (oldest-first notmuch-search-oldest-first)
882         (thread (notmuch-search-find-thread-id))
883         (query notmuch-search-query-string))
884     (kill-this-buffer)
885     (notmuch-search query oldest-first)
886     (goto-char (point-min))
887     (if (re-search-forward (concat "^" thread) nil t)
888         (beginning-of-line)
889       (goto-char here))))
890
891 (defun notmuch-search-toggle-order ()
892   "Toggle the current search order.
893
894 By default, the \"inbox\" view created by `notmuch' is displayed
895 in chronological order (oldest thread at the beginning of the
896 buffer), while any global searches created by `notmuch-search'
897 are displayed in reverse-chronological order (newest thread at
898 the beginning of the buffer).
899
900 This command toggles the sort order for the current search.
901
902 Note that any fitlered searches created by
903 `notmuch-search-filter' retain the search order of the parent
904 search."
905   (interactive)
906   (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
907   (notmuch-search-refresh-view))
908
909 (defun notmuch-search-filter (query)
910   "Filter the current search results based on an additional query string.
911
912 Runs a new search matching only messages that match both the
913 current search results AND the additional query string provided."
914   (interactive "sFilter search: ")
915   (notmuch-search (concat notmuch-search-query-string " and " query) notmuch-search-oldest-first))
916
917 (defun notmuch-search-filter-by-tag (tag)
918   "Filter the current search results based on a single tag.
919
920 Runs a new search matching only messages that match both the
921 current search results AND that are tagged with the given tag."
922   (interactive "sFilter by tag: ")
923   (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
924
925 (defun notmuch ()
926   "Run notmuch to display all mail with tag of 'inbox'"
927   (interactive)
928   (notmuch-search "tag:inbox" t))
929
930 (provide 'notmuch)