]> git.notmuchmail.org Git - notmuch/blob - notmuch.el
notmuch.el: Display authors in a column separate from subject.
[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             (indent-rigidly beg end depth)
468             (notmuch-show-markup-citations-region beg end depth)
469             ; Advance to the next part (if any) (so the outer loop can
470             ; determine whether we've left the current message.
471             (re-search-forward notmuch-show-part-begin-regexp nil t))))
472     (goto-char end)))
473
474 (defun notmuch-show-markup-parts-region (beg end depth)
475   (save-excursion
476     (goto-char beg)
477     (while (< (point) end)
478       (notmuch-show-markup-part beg end depth))))
479
480 (defun notmuch-show-markup-body (depth)
481   (re-search-forward notmuch-show-body-begin-regexp)
482   (forward-line)
483   (let ((beg (point-marker)))
484     (re-search-forward notmuch-show-body-end-regexp)
485     (let ((end (copy-marker (match-beginning 0))))
486       (notmuch-show-markup-parts-region beg end depth)
487       (if (not (notmuch-show-message-unread-p))
488           (overlay-put (make-overlay beg end)
489                        'invisible 'notmuch-show-body-read))
490       (set-marker beg nil)
491       (set-marker end nil)
492       )))
493
494 (defun notmuch-show-markup-header (depth)
495   (re-search-forward notmuch-show-header-begin-regexp)
496   (forward-line)
497   (let ((beg (point-marker)))
498     (end-of-line)
499     ; Inverse video for subject
500     (overlay-put (make-overlay beg (point)) 'face '((cons :inverse-video t)))
501     (forward-line 2)
502     (let ((beg-hidden (point-marker)))
503       (re-search-forward notmuch-show-header-end-regexp)
504       (beginning-of-line)
505       (let ((end (point-marker)))
506         (indent-rigidly beg end depth)
507         (overlay-put (make-overlay beg-hidden end)
508                      'invisible 'notmuch-show-header)
509         (set-marker beg nil)
510         (set-marker beg-hidden nil)
511         (set-marker end nil)
512         ))))
513
514 (defun notmuch-show-markup-message ()
515   (if (re-search-forward notmuch-show-message-begin-regexp nil t)
516       (progn
517         (re-search-forward notmuch-show-depth-regexp)
518         (let ((depth (string-to-number (buffer-substring (match-beginning 1) (match-end 1)))))
519           (notmuch-show-markup-header depth)
520           (notmuch-show-markup-body depth)))
521     (goto-char (point-max))))
522
523 (defun notmuch-show-hide-markers ()
524   (save-excursion
525     (goto-char (point-min))
526     (while (not (eobp))
527       (if (re-search-forward notmuch-show-marker-regexp nil t)
528           (progn
529             (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
530                          'invisible 'notmuch-show-marker))
531         (goto-char (point-max))))))
532
533 (defun notmuch-show-markup-messages ()
534   (save-excursion
535     (goto-char (point-min))
536     (while (not (eobp))
537       (notmuch-show-markup-message)))
538   (notmuch-show-hide-markers))
539
540 (defun notmuch-show-toggle-citations-visible ()
541   "Toggle visibility of citations"
542   (interactive)
543   (if notmuch-show-citations-visible
544       (add-to-invisibility-spec 'notmuch-show-citation)
545     (remove-from-invisibility-spec 'notmuch-show-citation))
546   (set 'notmuch-show-citations-visible (not notmuch-show-citations-visible))
547   ; Need to force the redisplay for some reason
548   (force-window-update)
549   (redisplay t))
550
551 (defun notmuch-show-toggle-signatures-visible ()
552   "Toggle visibility of signatures"
553   (interactive)
554   (if notmuch-show-signatures-visible
555       (add-to-invisibility-spec 'notmuch-show-signature)
556     (remove-from-invisibility-spec 'notmuch-show-signature))
557   (set 'notmuch-show-signatures-visible (not notmuch-show-signatures-visible))
558   ; Need to force the redisplay for some reason
559   (force-window-update)
560   (redisplay t))
561
562 (defun notmuch-show-toggle-headers-visible ()
563   "Toggle visibility of header fields"
564   (interactive)
565   (if notmuch-show-headers-visible
566       (add-to-invisibility-spec 'notmuch-show-header)
567     (remove-from-invisibility-spec 'notmuch-show-header))
568   (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
569   ; Need to force the redisplay for some reason
570   (force-window-update)
571   (redisplay t))
572
573 (defun notmuch-show-toggle-body-read-visible ()
574   "Toggle visibility of message bodies of read messages"
575   (interactive)
576   (if notmuch-show-body-read-visible
577       (add-to-invisibility-spec 'notmuch-show-body-read)
578     (remove-from-invisibility-spec 'notmuch-show-body-read))
579   (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
580   ; Need to force the redisplay for some reason
581   (force-window-update)
582   (redisplay t))
583
584 ;;;###autoload
585 (defun notmuch-show-mode ()
586   "Major mode for viewing a thread with notmuch.
587
588 This buffer contains the results of the \"notmuch show\" command
589 for displaying a single thread of email from your email archives.
590
591 By default, various components of email messages, (citations,
592 signatures, already-read messages), are invisible to help you
593 focus on the most important things, (new text from unread
594 messages). See the various commands below for toggling the
595 visibility of hidden components.
596
597 The `notmuch-show-next-message' and
598 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
599 default), allow you to navigate to the next and previous
600 messages. Each time you navigate away from a message with
601 `notmuch-show-next-message' the current message will have its
602 \"unread\" tag removed.
603
604 You can add or remove tags from the current message with '+' and
605 '-'.  You can also archive all messages in the current
606 view, (remove the \"inbox\" tag from each), with
607 `notmuch-show-archive-thread' (bound to 'a' by default).
608
609 \\{notmuch-show-mode-map}"
610   (interactive)
611   (kill-all-local-variables)
612   (set (make-local-variable 'notmuch-show-headers-visible) t)
613   (notmuch-show-toggle-headers-visible)
614   (set (make-local-variable 'notmuch-show-body-read-visible) t)
615   (notmuch-show-toggle-body-read-visible)
616   (set (make-local-variable 'notmuch-show-citations-visible) t)
617   (notmuch-show-toggle-citations-visible)
618   (set (make-local-variable 'notmuch-show-signatures-visible) t)
619   (notmuch-show-toggle-signatures-visible)
620   (add-to-invisibility-spec 'notmuch-show-marker)
621   (use-local-map notmuch-show-mode-map)
622   (setq major-mode 'notmuch-show-mode
623         mode-name "notmuch-show")
624   (setq buffer-read-only t))
625
626 (defun notmuch-show (thread-id &optional parent-buffer)
627   "Run \"notmuch show\" with the given thread ID and display results.
628
629 The optional PARENT-BUFFER is the notmuch-search buffer from
630 which this notmuch-show command was executed, (so that the next
631 thread from that buffer can be show when done with this one)."
632   (interactive "sNotmuch show: ")
633   (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
634     (switch-to-buffer buffer)
635     (notmuch-show-mode)
636     (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
637     (let ((proc (get-buffer-process (current-buffer)))
638           (inhibit-read-only t))
639       (if proc
640           (error "notmuch search process already running for query `%s'" query)
641         )
642       (erase-buffer)
643       (goto-char (point-min))
644       (save-excursion
645         (call-process "notmuch" nil t nil "show" thread-id)
646         (notmuch-show-markup-messages)
647         )
648       ; Move straight to the first unread message
649       (if (not (notmuch-show-message-unread-p))
650           (progn
651             (notmuch-show-next-unread-message)
652             ; But if there are no unread messages, go back to the
653             ; beginning of the buffer, and open up the bodies of all
654             ; read message.
655             (if (not (notmuch-show-message-unread-p))
656                 (progn
657                   (goto-char (point-min))
658                   (notmuch-show-toggle-body-read-visible)))))
659       )))
660
661 (defvar notmuch-search-authors-width 40
662   "Number of columns to use to diplay authors in a notmuch-search buffer.")
663
664 (defvar notmuch-search-mode-map
665   (let ((map (make-sparse-keymap)))
666     (define-key map "a" 'notmuch-search-archive-thread)
667     (define-key map "b" 'notmuch-search-scroll-down)
668     (define-key map "f" 'notmuch-search-filter)
669     (define-key map "m" 'message-mail)
670     (define-key map "n" 'next-line)
671     (define-key map "o" 'notmuch-search-toggle-order)
672     (define-key map "p" 'previous-line)
673     (define-key map "q" 'kill-this-buffer)
674     (define-key map "s" 'notmuch-search)
675     (define-key map "t" 'notmuch-search-filter-by-tag)
676     (define-key map "x" 'kill-this-buffer)
677     (define-key map (kbd "RET") 'notmuch-search-show-thread)
678     (define-key map "+" 'notmuch-search-add-tag)
679     (define-key map "-" 'notmuch-search-remove-tag)
680     (define-key map "<" 'beginning-of-buffer)
681     (define-key map ">" 'notmuch-search-goto-last-thread)
682     (define-key map "=" 'notmuch-search-refresh-view)
683     (define-key map "\M->" 'notmuch-search-goto-last-thread)
684     (define-key map " " 'notmuch-search-scroll-up)
685     (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
686     map)
687   "Keymap for \"notmuch search\" buffers.")
688 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
689
690 (defun notmuch-search-scroll-up ()
691   "Scroll up, moving point to last message in thread if at end."
692   (interactive)
693   (condition-case nil
694       (scroll-up nil)
695     ((end-of-buffer) (notmuch-search-goto-last-thread))))
696
697 (defun notmuch-search-scroll-down ()
698   "Scroll down, moving point to first message in thread if at beginning."
699   (interactive)
700   ; I don't know why scroll-down doesn't signal beginning-of-buffer
701   ; the way that scroll-up signals end-of-buffer, but c'est la vie.
702   ;
703   ; So instead of trapping a signal we instead check whether the
704   ; window begins on the first line of the buffer and if so, move
705   ; directly to that position. (We have to count lines since the
706   ; window-start position is not the same as point-min due to the
707   ; invisible thread-ID characters on the first line.
708   (if (equal (count-lines (point-min) (window-start)) 1)
709       (goto-char (window-start))
710     (scroll-down nil)))
711
712 (defun notmuch-search-goto-last-thread (&optional arg)
713   "Move point to the last thread in the buffer."
714   (interactive "^P")
715   (end-of-buffer arg)
716   (forward-line -1))
717
718 ;;;###autoload
719 (defun notmuch-search-mode ()
720   "Major mode for searching mail with notmuch.
721
722 This buffer contains the results of a \"notmuch search\" of your
723 email archives. Each line in the buffer represents a single
724 thread giving a relative date for the thread and a subject.
725
726 Pressing RET on any line displays that thread. The '+' and '-'
727 keys can be used to add or remove tags from a thread. The 'a' key
728 is a convenience key for archiving a thread (removing the
729 \"inbox\" tag).
730
731 Other useful commands are `notmuch-search-filter' for filtering
732 the current search based on an additional query string,
733 `notmuch-search-filter-by-tag' for filtering to include only
734 messages with a given tag, and `notmuch-search' to execute a new,
735 global search.
736
737 \\{notmuch-search-mode-map}"
738   (interactive)
739   (kill-all-local-variables)
740   (make-local-variable 'notmuch-search-query-string)
741   (make-local-variable 'notmuch-search-oldest-first)
742   (set (make-local-variable 'scroll-preserve-screen-position) t)
743   (add-to-invisibility-spec 'notmuch-search)
744   (use-local-map notmuch-search-mode-map)
745   (setq major-mode 'notmuch-search-mode
746         mode-name "notmuch-search")
747   (setq buffer-read-only t))
748
749 (defun notmuch-search-find-thread-id ()
750   (save-excursion
751     (beginning-of-line)
752     (let ((beg (point)))
753       (re-search-forward "thread:[a-fA-F0-9]*" nil t)
754       (filter-buffer-substring beg (point)))))
755
756 (defun notmuch-search-markup-this-thread-id ()
757   (beginning-of-line)
758   (let ((beg (point)))
759     (if (re-search-forward "thread:[a-fA-F0-9]*" nil t)
760         (progn
761           (forward-char)
762           (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)
763           (re-search-forward ".*\\[[0-9]*/[0-9]*\\] \\([^;]*\\)\\(;\\)")
764           (let* ((authors (buffer-substring (match-beginning 1) (match-end 1)))
765                  (authors-length (length authors)))
766             ;; Drop the semi-colon
767             (replace-match "" t nil nil 2)
768             (if (<= authors-length notmuch-search-authors-width)
769                 (replace-match (concat authors (make-string
770                                                 (- notmuch-search-authors-width
771                                                    authors-length) ? )) t t nil 1)
772               (replace-match (concat (substring authors 0 (- notmuch-search-authors-width 3)) "...") t t nil 1)))))))
773
774 (defun notmuch-search-markup-thread-ids ()
775   (save-excursion
776     (goto-char (point-min))
777     (while (not (eobp))
778       (notmuch-search-markup-this-thread-id)
779       (next-line))))
780
781 (defun notmuch-search-show-thread ()
782   (interactive)
783   (let ((thread-id (notmuch-search-find-thread-id)))
784     (if (> (length thread-id) 0)
785         (notmuch-show thread-id (current-buffer))
786       (error "End of search results"))))
787
788 (defun notmuch-call-notmuch-process (&rest args)
789   "Synchronously invoke \"notmuch\" with the given list of arguments.
790
791 Output from the process will be presented to the user as an error
792 and will also appear in a buffer named \"*Notmuch errors*\"."
793   (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
794     (with-current-buffer error-buffer
795         (erase-buffer))
796     (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
797         (point)
798       (progn
799         (with-current-buffer error-buffer
800           (let ((beg (point-min))
801                 (end (- (point-max) 1)))
802             (error (buffer-substring beg end))
803             ))))))
804
805 (defun notmuch-search-set-tags (tags)
806   (save-excursion
807     (end-of-line)
808     (re-search-backward "(")
809     (forward-char)
810     (let ((beg (point))
811           (inhibit-read-only t))
812       (re-search-forward ")")
813       (backward-char)
814       (let ((end (point)))
815         (delete-region beg end)
816         (insert (mapconcat  'identity tags " "))))))
817
818 (defun notmuch-search-get-tags ()
819   (save-excursion
820     (end-of-line)
821     (re-search-backward "(")
822     (let ((beg (+ (point) 1)))
823       (re-search-forward ")")
824       (let ((end (- (point) 1)))
825         (split-string (buffer-substring beg end))))))
826
827 (defun notmuch-search-add-tag (tag)
828   (interactive "sTag to add: ")
829   (notmuch-call-notmuch-process "tag" (concat "+" tag) (notmuch-search-find-thread-id))
830   (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
831
832 (defun notmuch-search-remove-tag (tag)
833   (interactive "sTag to remove: ")
834   (notmuch-call-notmuch-process "tag" (concat "-" tag) (notmuch-search-find-thread-id))
835   (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
836
837 (defun notmuch-search-archive-thread ()
838   "Archive the current thread (remove its \"inbox\" tag).
839
840 This function advances point to the next line when finished."
841   (interactive)
842   (notmuch-search-remove-tag "inbox")
843   (forward-line))
844
845 (defun notmuch-search (query &optional oldest-first)
846   "Run \"notmuch search\" with the given query string and display results."
847   (interactive "sNotmuch search: ")
848   (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
849     (switch-to-buffer buffer)
850     (notmuch-search-mode)
851     (set 'notmuch-search-query-string query)
852     (set 'notmuch-search-oldest-first oldest-first)
853     (let ((proc (get-buffer-process (current-buffer)))
854           (inhibit-read-only t))
855       (if proc
856           (error "notmuch search process already running for query `%s'" query)
857         )
858       (erase-buffer)
859       (goto-char (point-min))
860       (save-excursion
861         (if oldest-first
862             (call-process "notmuch" nil t nil "search" query)
863           (call-process "notmuch" nil t nil "search" "--reverse" query))
864         (notmuch-search-markup-thread-ids)
865         ))))
866
867 (defun notmuch-search-refresh-view ()
868   "Refresh the current view.
869
870 Kills the current buffer and runs a new search with the same
871 query string as the current search. If the current thread is in
872 the new search results, then point will be placed on the same
873 thread. Otherwise, point will be moved to attempt to be in the
874 same relative position within the new buffer."
875   (interactive)
876   (let ((here (point))
877         (oldest-first notmuch-search-oldest-first)
878         (thread (notmuch-search-find-thread-id))
879         (query notmuch-search-query-string))
880     (kill-this-buffer)
881     (notmuch-search query oldest-first)
882     (goto-char (point-min))
883     (if (re-search-forward (concat "^" thread) nil t)
884         (beginning-of-line)
885       (goto-char here))))
886
887 (defun notmuch-search-toggle-order ()
888   "Toggle the current search order.
889
890 By default, the \"inbox\" view created by `notmuch' is displayed
891 in chronological order (oldest thread at the beginning of the
892 buffer), while any global searches created by `notmuch-search'
893 are displayed in reverse-chronological order (newest thread at
894 the beginning of the buffer).
895
896 This command toggles the sort order for the current search.
897
898 Note that any fitlered searches created by
899 `notmuch-search-filter' retain the search order of the parent
900 search."
901   (interactive)
902   (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
903   (notmuch-search-refresh-view))
904
905 (defun notmuch-search-filter (query)
906   "Filter the current search results based on an additional query string.
907
908 Runs a new search matching only messages that match both the
909 current search results AND the additional query string provided."
910   (interactive "sFilter search: ")
911   (notmuch-search (concat notmuch-search-query-string " and " query) notmuch-search-oldest-first))
912
913 (defun notmuch-search-filter-by-tag (tag)
914   "Filter the current search results based on a single tag.
915
916 Runs a new search matching only messages that match both the
917 current search results AND that are tagged with the given tag."
918   (interactive "sFilter by tag: ")
919   (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
920
921 (defun notmuch ()
922   "Run notmuch to display all mail with tag of 'inbox'"
923   (interactive)
924   (notmuch-search "tag:inbox" t))
925
926 (provide 'notmuch)