]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch.el
emacs: Move emacs UI (currently just one file) to subdirectory.
[notmuch] / emacs / 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 ; This is an emacs-based interface to the notmuch mail system.
23 ;
24 ; You will first need to have the notmuch program installed and have a
25 ; notmuch database built in order to use this. See
26 ; http://notmuchmail.org for details.
27 ;
28 ; To install this software, copy it to a directory that is on the
29 ; `load-path' variable within emacs (a good candidate is
30 ; /usr/local/share/emacs/site-lisp). If you are viewing this from the
31 ; notmuch source distribution then you can simply run:
32 ;
33 ;       sudo make install-emacs
34 ;
35 ; to install it.
36 ;
37 ; Then, to actually run it, add:
38 ;
39 ;       (require 'notmuch)
40 ;
41 ; to your ~/.emacs file, and then run "M-x notmuch" from within emacs,
42 ; or run:
43 ;
44 ;       emacs -f notmuch
45 ;
46 ; Have fun, and let us know if you have any comment, questions, or
47 ; kudos: Notmuch list <notmuch@notmuchmail.org> (subscription is not
48 ; required, but is available from http://notmuchmail.org).
49
50 (require 'cl)
51 (require 'mm-view)
52 (require 'message)
53
54 (defvar notmuch-show-stash-map
55   (let ((map (make-sparse-keymap)))
56     (define-key map "c" 'notmuch-show-stash-cc)
57     (define-key map "d" 'notmuch-show-stash-date)
58     (define-key map "F" 'notmuch-show-stash-filename)
59     (define-key map "f" 'notmuch-show-stash-from)
60     (define-key map "i" 'notmuch-show-stash-message-id)
61     (define-key map "s" 'notmuch-show-stash-subject)
62     (define-key map "T" 'notmuch-show-stash-tags)
63     (define-key map "t" 'notmuch-show-stash-to)
64     map)
65   "Submap for stash commands"
66   )
67
68 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
69
70 (defvar notmuch-show-mode-map
71   (let ((map (make-sparse-keymap)))
72     (define-key map "?" 'notmuch-help)
73     (define-key map "q" 'kill-this-buffer)
74     (define-key map (kbd "C-p") 'notmuch-show-previous-line)
75     (define-key map (kbd "C-n") 'notmuch-show-next-line)
76     (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
77     (define-key map (kbd "TAB") 'notmuch-show-next-button)
78     (define-key map "s" 'notmuch-search)
79     (define-key map "m" 'message-mail)
80     (define-key map "f" 'notmuch-show-forward-current)
81     (define-key map "r" 'notmuch-show-reply)
82     (define-key map "|" 'notmuch-show-pipe-message)
83     (define-key map "w" 'notmuch-show-save-attachments)
84     (define-key map "V" 'notmuch-show-view-raw-message)
85     (define-key map "v" 'notmuch-show-view-all-mime-parts)
86     (define-key map "c" 'notmuch-show-stash-map)
87     (define-key map "b" 'notmuch-show-toggle-current-body)
88     (define-key map "h" 'notmuch-show-toggle-current-header)
89     (define-key map "-" 'notmuch-show-remove-tag)
90     (define-key map "+" 'notmuch-show-add-tag)
91     (define-key map "x" 'notmuch-show-archive-thread-then-exit)
92     (define-key map "a" 'notmuch-show-archive-thread)
93     (define-key map "P" 'notmuch-show-previous-message)
94     (define-key map "N" 'notmuch-show-next-message)
95     (define-key map "p" 'notmuch-show-previous-open-message)
96     (define-key map "n" 'notmuch-show-next-open-message)
97     (define-key map (kbd "DEL") 'notmuch-show-rewind)
98     (define-key map " " 'notmuch-show-advance-and-archive)
99     map)
100   "Keymap for \"notmuch show\" buffers.")
101 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
102
103 (defvar notmuch-show-signature-regexp "\\(-- ?\\|_+\\)$"
104   "Pattern to match a line that separates content from signature.
105
106 The regexp can (and should) include $ to match the end of the
107 line, but should not include ^ to match the beginning of the
108 line. This is because notmuch may have inserted additional space
109 for indentation at the beginning of the line. But notmuch will
110 move past the indentation when testing this pattern, (so that the
111 pattern can still test against the entire line).")
112
113 (defvar notmuch-show-signature-button-format
114   "[ %d-line signature. Click/Enter to toggle visibility. ]"
115   "String used to construct button text for hidden signatures
116
117 Can use up to one integer format parameter, i.e. %d")
118
119 (defvar notmuch-show-citation-button-format
120   "[ %d more citation lines. Click/Enter to toggle visibility. ]"
121   "String used to construct button text for hidden citations.
122
123 Can use up to one integer format parameter, i.e. %d")
124
125 (defvar notmuch-show-signature-lines-max 12
126   "Maximum length of signature that will be hidden by default.")
127
128 (defvar notmuch-show-citation-lines-prefix 4
129   "Always show at least this many lines of a citation.
130
131 If there is one more line, show that, otherwise collapse
132 remaining lines into a button.")
133
134 (defvar notmuch-command "notmuch"
135   "Command to run the notmuch binary.")
136
137 (defvar notmuch-show-message-begin-regexp    "\fmessage{")
138 (defvar notmuch-show-message-end-regexp      "\fmessage}")
139 (defvar notmuch-show-header-begin-regexp     "\fheader{")
140 (defvar notmuch-show-header-end-regexp       "\fheader}")
141 (defvar notmuch-show-body-begin-regexp       "\fbody{")
142 (defvar notmuch-show-body-end-regexp         "\fbody}")
143 (defvar notmuch-show-attachment-begin-regexp "\fattachment{")
144 (defvar notmuch-show-attachment-end-regexp   "\fattachment}")
145 (defvar notmuch-show-part-begin-regexp       "\fpart{")
146 (defvar notmuch-show-part-end-regexp         "\fpart}")
147 (defvar notmuch-show-marker-regexp "\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
148
149 (defvar notmuch-show-id-regexp "\\(id:[^ ]*\\)")
150 (defvar notmuch-show-depth-match-regexp " depth:\\([0-9]*\\).*match:\\([01]\\) ")
151 (defvar notmuch-show-filename-regexp "filename:\\(.*\\)$")
152 (defvar notmuch-show-contentype-regexp "Content-type: \\(.*\\)")
153
154 (defvar notmuch-show-tags-regexp "(\\([^)]*\\))$")
155
156 (defvar notmuch-show-parent-buffer nil)
157 (defvar notmuch-show-body-read-visible nil)
158 (defvar notmuch-show-citations-visible nil)
159 (defvar notmuch-show-signatures-visible nil)
160 (defvar notmuch-show-headers-visible nil)
161
162 ; XXX: This should be a generic function in emacs somewhere, not here
163 (defun point-invisible-p ()
164   "Return whether the character at point is invisible.
165
166 Here visibility is determined by `buffer-invisibility-spec' and
167 the invisible property of any overlays for point. It doesn't have
168 anything to do with whether point is currently being displayed
169 within the current window."
170   (let ((prop (get-char-property (point) 'invisible)))
171     (if (eq buffer-invisibility-spec t)
172         prop
173       (or (memq prop buffer-invisibility-spec)
174           (assq prop buffer-invisibility-spec)))))
175
176 (defun notmuch-select-tag-with-completion (prompt &rest search-terms)
177   (let ((tag-list
178          (with-output-to-string
179            (with-current-buffer standard-output
180              (apply 'call-process notmuch-command nil t nil "search-tags" search-terms)))))
181     (completing-read prompt (split-string tag-list "\n+" t) nil nil nil)))
182
183 (defun notmuch-show-next-line ()
184   "Like builtin `next-line' but ensuring we end on a visible character.
185
186 By advancing forward until reaching a visible character.
187
188 Unlike builtin `next-line' this version accepts no arguments."
189   (interactive)
190   (set 'this-command 'next-line)
191   (call-interactively 'next-line)
192   (while (point-invisible-p)
193     (forward-char)))
194
195 (defun notmuch-show-previous-line ()
196   "Like builtin `previous-line' but ensuring we end on a visible character.
197
198 By advancing forward until reaching a visible character.
199
200 Unlike builtin `previous-line' this version accepts no arguments."
201   (interactive)
202   (set 'this-command 'previous-line)
203   (call-interactively 'previous-line)
204   (while (point-invisible-p)
205     (forward-char)))
206
207 (defun notmuch-show-get-message-id ()
208   (save-excursion
209     (beginning-of-line)
210     (if (not (looking-at notmuch-show-message-begin-regexp))
211         (re-search-backward notmuch-show-message-begin-regexp))
212     (re-search-forward notmuch-show-id-regexp)
213     (buffer-substring-no-properties (match-beginning 1) (match-end 1))))
214
215 (defun notmuch-show-get-filename ()
216   (save-excursion
217     (beginning-of-line)
218     (if (not (looking-at notmuch-show-message-begin-regexp))
219         (re-search-backward notmuch-show-message-begin-regexp))
220     (re-search-forward notmuch-show-filename-regexp)
221     (buffer-substring-no-properties (match-beginning 1) (match-end 1))))
222
223 (defun notmuch-show-set-tags (tags)
224   (save-excursion
225     (beginning-of-line)
226     (if (not (looking-at notmuch-show-message-begin-regexp))
227         (re-search-backward notmuch-show-message-begin-regexp))
228     (re-search-forward notmuch-show-tags-regexp)
229     (let ((inhibit-read-only t)
230           (beg (match-beginning 1))
231           (end (match-end 1)))
232       (delete-region beg end)
233       (goto-char beg)
234       (insert (mapconcat 'identity tags " ")))))
235
236 (defun notmuch-show-get-tags ()
237   (save-excursion
238     (beginning-of-line)
239     (if (not (looking-at notmuch-show-message-begin-regexp))
240         (re-search-backward notmuch-show-message-begin-regexp))
241     (re-search-forward notmuch-show-tags-regexp)
242     (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
243
244 (defun notmuch-show-get-bcc ()
245   "Return BCC address(es) of current message"
246   (notmuch-show-get-header-field 'bcc))
247
248 (defun notmuch-show-get-cc ()
249   "Return CC address(es) of current message"
250   (notmuch-show-get-header-field 'cc))
251
252 (defun notmuch-show-get-date ()
253   "Return Date of current message"
254   (notmuch-show-get-header-field 'date))
255
256 (defun notmuch-show-get-from ()
257   "Return From address of current message"
258   (notmuch-show-get-header-field 'from))
259
260 (defun notmuch-show-get-subject ()
261   "Return Subject of current message"
262   (notmuch-show-get-header-field 'subject))
263
264 (defun notmuch-show-get-to ()
265   "Return To address(es) of current message"
266   (notmuch-show-get-header-field 'to))
267
268 (defun notmuch-show-get-header-field (name)
269   "Retrieve the header field NAME from the current message.
270 NAME should be a symbol, in lower case, as returned by
271 mail-header-extract-no-properties"
272   (let* ((result (assoc name (notmuch-show-get-header)))
273         (val (and result (cdr result))))
274     val))
275
276 (defun notmuch-show-get-header ()
277   "Retrieve and parse the header from the current message. Returns an alist with of (header . value)
278 where header is a symbol and value is a string.  The summary from notmuch-show is returned as the
279 pseudoheader summary"
280   (require 'mailheader)
281   (save-excursion
282     (beginning-of-line)
283     (if (not (looking-at notmuch-show-message-begin-regexp))
284         (re-search-backward notmuch-show-message-begin-regexp))
285     (re-search-forward (concat notmuch-show-header-begin-regexp "\n[[:space:]]*\\(.*\\)\n"))
286     (let* ((summary (buffer-substring-no-properties (match-beginning 1) (match-end 1)))
287           (beg (point)))
288       (re-search-forward notmuch-show-header-end-regexp)
289       (let ((text (buffer-substring beg (match-beginning 0))))
290         (with-temp-buffer
291           (insert text)
292           (goto-char (point-min))
293           (while (looking-at "\\([[:space:]]*\\)[A-Za-z][-A-Za-z0-9]*:")
294             (delete-region (match-beginning 1) (match-end 1))
295             (forward-line)
296             )
297           (goto-char (point-min))
298           (cons (cons 'summary summary) (mail-header-extract-no-properties)))))))
299
300 (defun notmuch-show-add-tag (&rest toadd)
301   "Add a tag to the current message."
302   (interactive
303    (list (notmuch-select-tag-with-completion "Tag to add: ")))
304   (apply 'notmuch-call-notmuch-process
305          (append (cons "tag"
306                        (mapcar (lambda (s) (concat "+" s)) toadd))
307                  (cons (notmuch-show-get-message-id) nil)))
308   (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
309
310 (defun notmuch-show-remove-tag (&rest toremove)
311   "Remove a tag from the current message."
312   (interactive
313    (list (notmuch-select-tag-with-completion "Tag to remove: " (notmuch-show-get-message-id))))
314   (let ((tags (notmuch-show-get-tags)))
315     (if (intersection tags toremove :test 'string=)
316         (progn
317           (apply 'notmuch-call-notmuch-process
318                  (append (cons "tag"
319                                (mapcar (lambda (s) (concat "-" s)) toremove))
320                          (cons (notmuch-show-get-message-id) nil)))
321           (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
322
323 (defun notmuch-show-archive-thread ()
324   "Archive each message in thread, then show next thread from search.
325
326 Archive each message currently shown by removing the \"inbox\"
327 tag from each. Then kill this buffer and show the next thread
328 from the search from which this thread was originally shown.
329
330 Note: This command is safe from any race condition of new messages
331 being delivered to the same thread. It does not archive the
332 entire thread, but only the messages shown in the current
333 buffer."
334   (interactive)
335   (save-excursion
336     (goto-char (point-min))
337     (while (not (eobp))
338       (notmuch-show-remove-tag "inbox")
339       (if (not (eobp))
340           (forward-char))
341       (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
342           (goto-char (point-max)))))
343   (let ((parent-buffer notmuch-show-parent-buffer))
344     (kill-this-buffer)
345     (if parent-buffer
346         (progn
347           (switch-to-buffer parent-buffer)
348           (forward-line)
349           (notmuch-search-show-thread)))))
350
351 (defun notmuch-show-archive-thread-then-exit ()
352   "Archive each message in thread, then exit back to search results."
353   (interactive)
354   (notmuch-show-archive-thread)
355   (kill-this-buffer))
356
357 (defun notmuch-show-view-raw-message ()
358   "View the raw email of the current message."
359   (interactive)
360   (view-file (notmuch-show-get-filename)))
361
362 (defmacro with-current-notmuch-show-message (&rest body)
363   "Evaluate body with current buffer set to the text of current message"
364   `(save-excursion
365      (let ((filename (notmuch-show-get-filename)))
366        (let ((buf (generate-new-buffer (concat "*notmuch-msg-" filename "*"))))
367          (with-current-buffer buf
368            (insert-file-contents filename nil nil nil t)
369            ,@body)
370          (kill-buffer buf)))))
371
372 (defun notmuch-show-view-all-mime-parts ()
373   "Use external viewers to view all attachments from the current message."
374   (interactive)
375   (with-current-notmuch-show-message
376    ; We ovverride the mm-inline-media-tests to indicate which message
377    ; parts are already sufficiently handled by the original
378    ; presentation of the message in notmuch-show mode. These parts
379    ; will be inserted directly into the temporary buffer of
380    ; with-current-notmuch-show-message and silently discarded.
381    ;
382    ; Any MIME part not explicitly mentioned here will be handled by an
383    ; external viewer as configured in the various mailcap files.
384    (let ((mm-inline-media-tests '(
385                                   ("text/.*" ignore identity)
386                                   ("application/pgp-signature" ignore identity)
387                                   ("multipart/alternative" ignore identity)
388                                   ("multipart/mixed" ignore identity)
389                                   ("multipart/related" ignore identity)
390                                  )))
391      (mm-display-parts (mm-dissect-buffer)))))
392
393 (defun notmuch-foreach-mime-part (function mm-handle)
394   (cond ((stringp (car mm-handle))
395          (dolist (part (cdr mm-handle))
396            (notmuch-foreach-mime-part function part)))
397         ((bufferp (car mm-handle))
398          (funcall function mm-handle))
399         (t (dolist (part mm-handle)
400              (notmuch-foreach-mime-part function part)))))
401
402 (defun notmuch-count-attachments (mm-handle)
403   (let ((count 0))
404     (notmuch-foreach-mime-part
405      (lambda (p)
406        (let ((disposition (mm-handle-disposition p)))
407          (and (listp disposition)
408               (or (equal (car disposition) "attachment")
409                   (and (equal (car disposition) "inline")
410                        (assq 'filename disposition)))
411               (incf count))))
412      mm-handle)
413     count))
414
415 (defun notmuch-save-attachments (mm-handle &optional queryp)
416   (notmuch-foreach-mime-part
417    (lambda (p)
418      (let ((disposition (mm-handle-disposition p)))
419        (and (listp disposition)
420             (or (equal (car disposition) "attachment")
421                 (and (equal (car disposition) "inline")
422                      (assq 'filename disposition)))
423             (or (not queryp)
424                 (y-or-n-p
425                  (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
426             (mm-save-part p))))
427    mm-handle))
428
429 (defun notmuch-show-save-attachments ()
430   "Save all attachments from the current message."
431   (interactive)
432   (with-current-notmuch-show-message
433    (let ((mm-handle (mm-dissect-buffer)))
434      (notmuch-save-attachments
435       mm-handle (> (notmuch-count-attachments mm-handle) 1))))
436   (message "Done"))
437
438 (defun notmuch-reply (query-string)
439   (switch-to-buffer (generate-new-buffer "notmuch-draft"))
440   (call-process notmuch-command nil t nil "reply" query-string)
441   (message-insert-signature)
442   (goto-char (point-min))
443   (if (re-search-forward "^$" nil t)
444       (progn
445         (insert "--text follows this line--")
446         (forward-line)))
447   (message-mode))
448
449 (defun notmuch-show-reply ()
450   "Begin composing a reply to the current message in a new buffer."
451   (interactive)
452   (let ((message-id (notmuch-show-get-message-id)))
453     (notmuch-reply message-id)))
454
455 (defun notmuch-show-forward-current ()
456   "Forward the current message."
457   (interactive)
458   (with-current-notmuch-show-message
459    (message-forward)))
460
461 (defun notmuch-show-pipe-message (command)
462   "Pipe the contents of the current message to the given command.
463
464 The given command will be executed with the raw contents of the
465 current email message as stdin. Anything printed by the command
466 to stdout or stderr will appear in the *Messages* buffer."
467   (interactive "sPipe message to command: ")
468   (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*"
469          (list command " < " (shell-quote-argument (notmuch-show-get-filename)))))
470
471 (defun notmuch-show-move-to-current-message-summary-line ()
472   "Move to the beginning of the one-line summary of the current message.
473
474 This gives us a stable place to move to and work from since the
475 summary line is always visible. This is important since moving to
476 an invisible location is unreliable, (the main command loop moves
477 point either forward or backward to the next visible character
478 when a command ends with point on an invisible character).
479
480 Emits an error if point is not within a valid message, (that is
481 no pattern of `notmuch-show-message-begin-regexp' could be found
482 by searching backward)."
483   (beginning-of-line)
484   (if (not (looking-at notmuch-show-message-begin-regexp))
485       (if (re-search-backward notmuch-show-message-begin-regexp nil t)
486           (forward-line 2)
487         (error "Not within a valid message."))
488     (forward-line 2)))
489
490 (defun notmuch-show-last-message-p ()
491   "Predicate testing whether point is within the last message."
492   (save-window-excursion
493     (save-excursion
494       (notmuch-show-move-to-current-message-summary-line)
495       (not (re-search-forward notmuch-show-message-begin-regexp nil t)))))
496
497 (defun notmuch-show-message-unread-p ()
498   "Predicate testing whether current message is unread."
499   (member "unread" (notmuch-show-get-tags)))
500
501 (defun notmuch-show-message-open-p ()
502   "Predicate testing whether current message is open (body is visible)."
503   (let ((btn (previous-button (point) t)))
504     (while (not (button-has-type-p btn 'notmuch-button-body-toggle-type))
505       (setq btn (previous-button (button-start btn))))
506     (not (invisible-p (button-get btn 'invisibility-spec)))))
507
508 (defun notmuch-show-next-message-without-marking-read ()
509   "Advance to the beginning of the next message in the buffer.
510
511 Moves to the last visible character of the current message if
512 already on the last message in the buffer.
513
514 Returns nil if already on the last message in the buffer."
515   (notmuch-show-move-to-current-message-summary-line)
516   (if (re-search-forward notmuch-show-message-begin-regexp nil t)
517       (progn
518         (notmuch-show-move-to-current-message-summary-line)
519         (recenter 0)
520         t)
521     (goto-char (- (point-max) 1))
522     (while (point-invisible-p)
523       (backward-char))
524     (recenter 0)
525     nil))
526
527 (defun notmuch-show-next-message ()
528   "Advance to the next message (whether open or closed)
529 and remove the unread tag from that message.
530
531 Moves to the last visible character of the current message if
532 already on the last message in the buffer.
533
534 Returns nil if already on the last message in the buffer."
535   (interactive)
536   (notmuch-show-next-message-without-marking-read)
537   (notmuch-show-mark-read))
538
539 (defun notmuch-show-find-next-message ()
540   "Returns the position of the next message in the buffer.
541
542 Or the position of the last visible character of the current
543 message if already within the last message in the buffer."
544   ; save-excursion doesn't save our window position
545   ; save-window-excursion doesn't save point
546   ; Looks like we have to use both.
547   (save-excursion
548     (save-window-excursion
549       (notmuch-show-next-message-without-marking-read)
550       (point))))
551
552 (defun notmuch-show-next-unread-message ()
553   "Advance to the next unread message.
554
555 Moves to the last visible character of the current message if
556 there are no more unread messages past the current point."
557   (notmuch-show-next-message-without-marking-read)
558   (while (and (not (notmuch-show-last-message-p))
559               (not (notmuch-show-message-unread-p)))
560     (notmuch-show-next-message-without-marking-read))
561   (if (not (notmuch-show-message-unread-p))
562       (notmuch-show-next-message-without-marking-read))
563   (notmuch-show-mark-read))
564
565 (defun notmuch-show-next-open-message ()
566   "Advance to the next open message (that is, body is visible).
567
568 Moves to the last visible character of the final message in the buffer
569 if there are no more open messages."
570   (interactive)
571   (while (and (notmuch-show-next-message-without-marking-read)
572               (not (notmuch-show-message-open-p))))
573   (notmuch-show-mark-read))
574
575 (defun notmuch-show-previous-message-without-marking-read ()
576   "Backup to the beginning of the previous message in the buffer.
577
578 If within a message rather than at the beginning of it, then
579 simply move to the beginning of the current message.
580
581 Returns nil if already on the first message in the buffer."
582   (let ((start (point)))
583     (notmuch-show-move-to-current-message-summary-line)
584     (if (not (< (point) start))
585         ; Go backward twice to skip the current message's marker
586         (progn
587           (re-search-backward notmuch-show-message-begin-regexp nil t)
588           (re-search-backward notmuch-show-message-begin-regexp nil t)
589           (notmuch-show-move-to-current-message-summary-line)
590           (recenter 0)
591           (if (= (point) start)
592               nil
593             t))
594       (recenter 0)
595       nil)))
596
597 (defun notmuch-show-previous-message ()
598   "Backup to the previous message (whether open or closed)
599 and remove the unread tag from that message.
600
601 If within a message rather than at the beginning of it, then
602 simply move to the beginning of the current message."
603   (interactive)
604   (notmuch-show-previous-message-without-marking-read)
605   (notmuch-show-mark-read))
606
607 (defun notmuch-show-find-previous-message ()
608   "Returns the position of the previous message in the buffer.
609
610 Or the position of the beginning of the current message if point
611 is originally within the message rather than at the beginning of
612 it."
613   ; save-excursion doesn't save our window position
614   ; save-window-excursion doesn't save point
615   ; Looks like we have to use both.
616   (save-excursion
617     (save-window-excursion
618       (notmuch-show-previous-message-without-marking-read)
619       (point))))
620
621 (defun notmuch-show-previous-open-message ()
622   "Backup to previous open message (that is, body is visible).
623
624 Moves to the first message in the buffer if there are no previous
625 open messages."
626   (interactive)
627   (while (and (notmuch-show-previous-message-without-marking-read)
628               (not (notmuch-show-message-open-p))))
629   (notmuch-show-mark-read))
630
631 (defun notmuch-show-rewind ()
632   "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
633
634 Specifically, if the beginning of the previous email is fewer
635 than `window-height' lines from the current point, move to it
636 just like `notmuch-show-previous-message'.
637
638 Otherwise, just scroll down a screenful of the current message.
639
640 This command does not modify any message tags, (it does not undo
641 any effects from previous calls to
642 `notmuch-show-advance-and-archive'."
643   (interactive)
644   (let ((previous (notmuch-show-find-previous-message)))
645     (if (> (count-lines previous (point)) (- (window-height) next-screen-context-lines))
646         (progn
647           (condition-case nil
648               (scroll-down nil)
649             ((beginning-of-buffer) nil))
650           (goto-char (window-start))
651           ; Because count-lines counts invivisible lines, we may have
652           ; scrolled to far. If so., notice this and fix it up.
653           (if (< (point) previous)
654               (progn
655                 (goto-char previous)
656                 (recenter 0))))
657       (notmuch-show-previous-message))))
658
659 (defun notmuch-show-advance-and-archive ()
660   "Advance through thread and archive.
661
662 This command is intended to be one of the simplest ways to
663 process a thread of email. It does the following:
664
665 If the current message in the thread is not yet fully visible,
666 scroll by a near screenful to read more of the message.
667
668 Otherwise, (the end of the current message is already within the
669 current window), advance to the next open message.
670
671 Finally, if there is no further message to advance to, and this
672 last message is already read, then archive the entire current
673 thread, (remove the \"inbox\" tag from each message). Also kill
674 this buffer, and display the next thread from the search from
675 which this thread was originally shown."
676   (interactive)
677   (let ((next (notmuch-show-find-next-message))
678         (unread (notmuch-show-message-unread-p)))
679     (if (> next (window-end))
680         (scroll-up nil)
681       (let ((last (notmuch-show-last-message-p)))
682         (notmuch-show-next-open-message)
683         (if last
684             (notmuch-show-archive-thread))))))
685
686 (defun notmuch-show-next-button ()
687   "Advance point to the next button in the buffer."
688   (interactive)
689   (forward-button 1))
690
691 (defun notmuch-show-previous-button ()
692   "Move point back to the previous button in the buffer."
693   (interactive)
694   (backward-button 1))
695
696 (defun notmuch-toggle-invisible-action (cite-button)
697   (let ((invis-spec (button-get cite-button 'invisibility-spec)))
698         (if (invisible-p invis-spec)
699             (remove-from-invisibility-spec invis-spec)
700           (add-to-invisibility-spec invis-spec)
701           ))
702   (force-window-update)
703   (redisplay t))
704
705 (defun notmuch-show-toggle-current-body ()
706   "Toggle the display of the current message body."
707   (interactive)
708   (save-excursion
709     (notmuch-show-move-to-current-message-summary-line)
710     (unless (button-at (point))
711       (notmuch-show-next-button))
712     (push-button))
713   )
714
715 (defun notmuch-show-toggle-current-header ()
716   "Toggle the display of the current message header."
717   (interactive)
718   (save-excursion
719     (notmuch-show-move-to-current-message-summary-line)
720     (forward-line)
721     (unless (button-at (point))
722       (notmuch-show-next-button))
723     (push-button))
724   )
725
726 (define-button-type 'notmuch-button-invisibility-toggle-type
727   'action 'notmuch-toggle-invisible-action
728   'follow-link t
729   'face 'font-lock-comment-face)
730 (define-button-type 'notmuch-button-citation-toggle-type 'help-echo "mouse-1, RET: Show citation"
731   :supertype 'notmuch-button-invisibility-toggle-type)
732 (define-button-type 'notmuch-button-signature-toggle-type 'help-echo "mouse-1, RET: Show signature"
733   :supertype 'notmuch-button-invisibility-toggle-type)
734 (define-button-type 'notmuch-button-headers-toggle-type 'help-echo "mouse-1, RET: Show headers"
735   :supertype 'notmuch-button-invisibility-toggle-type)
736 (define-button-type 'notmuch-button-body-toggle-type
737   'help-echo "mouse-1, RET: Show message"
738   'face 'notmuch-message-summary-face
739   :supertype 'notmuch-button-invisibility-toggle-type)
740
741 (defun notmuch-show-citation-regexp (depth)
742   "Build a regexp for matching citations at a given DEPTH (indent)"
743   (let ((line-regexp (format "[[:space:]]\\{%d\\}>.*\n" depth)))
744     (concat "\\(?:^" line-regexp
745             "\\(?:[[:space:]]*\n" line-regexp
746             "\\)?\\)+")))
747
748 (defun notmuch-show-region-to-button (beg end type prefix button-text)
749   "Auxilary function to do the actual making of overlays and buttons
750
751 BEG and END are buffer locations. TYPE should a string, either
752 \"citation\" or \"signature\". PREFIX is some arbitrary text to
753 insert before the button, probably for indentation.  BUTTON-TEXT
754 is what to put on the button."
755
756 ;; This uses some slightly tricky conversions between strings and
757 ;; symbols because of the way the button code works. Note that
758 ;; replacing intern-soft with make-symbol will cause this to fail,
759 ;; since the newly created symbol has no plist.
760
761   (let ((overlay (make-overlay beg end))
762         (invis-spec (make-symbol (concat "notmuch-" type "-region")))
763         (button-type (intern-soft (concat "notmuch-button-"
764                                           type "-toggle-type"))))
765     (add-to-invisibility-spec invis-spec)
766     (overlay-put overlay 'invisible invis-spec)
767     (goto-char (1+ end))
768     (save-excursion
769       (goto-char (1- beg))
770       (insert prefix)
771       (insert-button button-text
772                      'invisibility-spec invis-spec
773                      :type button-type)
774       )))
775
776
777 (defun notmuch-show-markup-citations-region (beg end depth)
778   "Markup citations, and up to one signature in the given region"
779   ;; it would be nice if the untabify was not required, but
780   ;; that would require notmuch to indent with spaces.
781   (untabify beg end)
782   (let ((citation-regexp (notmuch-show-citation-regexp depth))
783         (signature-regexp (concat (format "^[[:space:]]\\{%d\\}" depth)
784                                   notmuch-show-signature-regexp))
785         (indent (concat "\n" (make-string depth ? ))))
786     (goto-char beg)
787     (beginning-of-line)
788     (while (and (< (point) end)
789                 (re-search-forward citation-regexp end t))
790       (let* ((cite-start (match-beginning 0))
791              (cite-end  (match-end 0))
792              (cite-lines (count-lines cite-start cite-end)))
793         (when (> cite-lines (1+ notmuch-show-citation-lines-prefix))
794           (goto-char cite-start)
795           (forward-line notmuch-show-citation-lines-prefix)
796           (notmuch-show-region-to-button
797            (point) cite-end
798            "citation"
799            indent
800            (format notmuch-show-citation-button-format
801                    (- cite-lines notmuch-show-citation-lines-prefix))
802            ))))
803     (if (and (< (point) end)
804              (re-search-forward signature-regexp end t))
805         (let* ((sig-start (match-beginning 0))
806                (sig-end (match-end 0))
807                (sig-lines (1- (count-lines sig-start end))))
808           (if (<= sig-lines notmuch-show-signature-lines-max)
809               (notmuch-show-region-to-button
810                sig-start
811                end
812                "signature"
813                indent
814                (format notmuch-show-signature-button-format sig-lines)
815                ))))))
816
817 (defun notmuch-show-markup-part (beg end depth)
818   (if (re-search-forward notmuch-show-part-begin-regexp nil t)
819       (progn
820         (let (mime-message mime-type)
821           (save-excursion
822             (re-search-forward notmuch-show-contentype-regexp end t)
823             (setq mime-type (car (split-string (buffer-substring
824                                                 (match-beginning 1) (match-end 1))))))
825
826           (if (equal mime-type "text/html")
827               (let ((filename (notmuch-show-get-filename)))
828                 (with-temp-buffer
829                   (insert-file-contents filename nil nil nil t)
830                   (setq mime-message (mm-dissect-buffer)))))
831           (forward-line)
832           (let ((beg (point-marker)))
833             (re-search-forward notmuch-show-part-end-regexp)
834             (let ((end (copy-marker (match-beginning 0))))
835               (goto-char end)
836               (if (not (bolp))
837                   (insert "\n"))
838               (indent-rigidly beg end depth)
839               (if (not (eq mime-message nil))
840                   (save-excursion
841                     (goto-char beg)
842                     (forward-line -1)
843                     (let ((handle-type (mm-handle-type mime-message))
844                           mime-type)
845                       (if (sequencep (car handle-type))
846                           (setq mime-type (car handle-type))
847                         (setq mime-type (car (car (cdr handle-type))))
848                         )
849                       (if (equal mime-type "text/html")
850                           (mm-display-part mime-message))))
851                 )
852               (notmuch-show-markup-citations-region beg end depth)
853               ; Advance to the next part (if any) (so the outer loop can
854               ; determine whether we've left the current message.
855               (if (re-search-forward notmuch-show-part-begin-regexp nil t)
856                   (beginning-of-line)))))
857         (goto-char end))
858     (goto-char end)))
859
860 (defun notmuch-show-markup-parts-region (beg end depth)
861   (save-excursion
862     (goto-char beg)
863     (while (< (point) end)
864       (notmuch-show-markup-part beg end depth))))
865
866 (defun notmuch-show-markup-body (depth match btn)
867   "Markup a message body, (indenting, buttonizing citations,
868 etc.), and hiding the body itself if the message does not match
869 the current search.
870
871 DEPTH specifies the depth at which this message appears in the
872 tree of the current thread, (the top-level messages have depth 0
873 and each reply increases depth by 1). MATCH indicates whether
874 this message is regarded as matching the current search. BTN is
875 the button which is used to toggle the visibility of this
876 message.
877
878 When this function is called, point must be within the message, but
879 before the delimiter marking the beginning of the body."
880   (re-search-forward notmuch-show-body-begin-regexp)
881   (forward-line)
882   (let ((beg (point-marker)))
883     (re-search-forward notmuch-show-body-end-regexp)
884     (let ((end (copy-marker (match-beginning 0))))
885       (notmuch-show-markup-parts-region beg end depth)
886       (let ((invis-spec (make-symbol "notmuch-show-body-read")))
887         (overlay-put (make-overlay beg end)
888                      'invisible invis-spec)
889         (button-put btn 'invisibility-spec invis-spec)
890         (if (not match)
891             (add-to-invisibility-spec invis-spec)))
892       (set-marker beg nil)
893       (set-marker end nil)
894       )))
895
896 (defun notmuch-fontify-headers ()
897   (while (looking-at "[[:space:]]")
898     (forward-char))
899   (if (looking-at "[Tt]o:")
900       (progn
901         (overlay-put (make-overlay (point) (re-search-forward ":"))
902                      'face 'message-header-name)
903         (overlay-put (make-overlay (point) (re-search-forward ".*$"))
904                      'face 'message-header-to))
905     (if (looking-at "[B]?[Cc][Cc]:")
906         (progn
907           (overlay-put (make-overlay (point) (re-search-forward ":"))
908                        'face 'message-header-name)
909           (overlay-put (make-overlay (point) (re-search-forward ".*$"))
910                        'face 'message-header-cc))
911       (if (looking-at "[Ss]ubject:")
912           (progn
913             (overlay-put (make-overlay (point) (re-search-forward ":"))
914                          'face 'message-header-name)
915             (overlay-put (make-overlay (point) (re-search-forward ".*$"))
916                          'face 'message-header-subject))
917         (if (looking-at "[Ff]rom:")
918             (progn
919               (overlay-put (make-overlay (point) (re-search-forward ":"))
920                            'face 'message-header-name)
921               (overlay-put (make-overlay (point) (re-search-forward ".*$"))
922                            'face 'message-header-other)))))))
923
924 (defun notmuch-show-markup-header (message-begin depth)
925   "Buttonize and decorate faces in a message header.
926
927 MESSAGE-BEGIN is the position of the absolute first character in
928 the message (including all delimiters that will end up being
929 invisible etc.). This is to allow a button to reliably extend to
930 the beginning of the message even if point is positioned at an
931 invisible character (such as the beginning of the buffer).
932
933 DEPTH specifies the depth at which this message appears in the
934 tree of the current thread, (the top-level messages have depth 0
935 and each reply increases depth by 1)."
936   (re-search-forward notmuch-show-header-begin-regexp)
937   (forward-line)
938   (let ((beg (point-marker))
939         (summary-end (copy-marker (line-beginning-position 2)))
940         (subject-end (copy-marker (line-end-position 2)))
941         (invis-spec (make-symbol "notmuch-show-header"))
942         (btn nil))
943     (re-search-forward notmuch-show-header-end-regexp)
944     (beginning-of-line)
945     (let ((end (point-marker)))
946       (indent-rigidly beg end depth)
947       (goto-char beg)
948       (setq btn (make-button message-begin summary-end :type 'notmuch-button-body-toggle-type))
949       (forward-line)
950       (add-to-invisibility-spec invis-spec)
951       (overlay-put (make-overlay subject-end end)
952                    'invisible invis-spec)
953       (make-button (line-beginning-position) subject-end
954                    'invisibility-spec invis-spec
955                    :type 'notmuch-button-headers-toggle-type)
956       (while (looking-at "[[:space:]]*[A-Za-z][-A-Za-z0-9]*:")
957         (beginning-of-line)
958         (notmuch-fontify-headers)
959         (forward-line)
960         )
961       (goto-char end)
962       (insert "\n")
963       (set-marker beg nil)
964       (set-marker summary-end nil)
965       (set-marker subject-end nil)
966       (set-marker end nil)
967       )
968   btn))
969
970 (defun notmuch-show-markup-message ()
971   (if (re-search-forward notmuch-show-message-begin-regexp nil t)
972       (let ((message-begin (match-beginning 0)))
973         (re-search-forward notmuch-show-depth-match-regexp)
974         (let ((depth (string-to-number (buffer-substring (match-beginning 1) (match-end 1))))
975               (match (string= "1" (buffer-substring (match-beginning 2) (match-end 2))))
976               (btn nil))
977           (setq btn (notmuch-show-markup-header message-begin depth))
978           (notmuch-show-markup-body depth match btn)))
979     (goto-char (point-max))))
980
981 (defun notmuch-show-hide-markers ()
982   (save-excursion
983     (goto-char (point-min))
984     (while (not (eobp))
985       (if (re-search-forward notmuch-show-marker-regexp nil t)
986           (progn
987             (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
988                          'invisible 'notmuch-show-marker))
989         (goto-char (point-max))))))
990
991 (defun notmuch-show-markup-messages ()
992   (save-excursion
993     (goto-char (point-min))
994     (while (not (eobp))
995       (notmuch-show-markup-message)))
996   (notmuch-show-hide-markers))
997
998 (defun notmuch-documentation-first-line (symbol)
999   "Return the first line of the documentation string for SYMBOL."
1000   (let ((doc (documentation symbol)))
1001     (if doc
1002         (with-temp-buffer
1003           (insert (documentation symbol t))
1004           (goto-char (point-min))
1005           (let ((beg (point)))
1006             (end-of-line)
1007             (buffer-substring beg (point))))
1008       "")))
1009
1010 (defun notmuch-prefix-key-description (key)
1011   "Given a prefix key code, return a human-readable string representation.
1012
1013 This is basically just `format-kbd-macro' but we also convert ESC to M-."
1014   (let ((desc (format-kbd-macro (vector key))))
1015     (if (string= desc "ESC")
1016         "M-"
1017       (concat desc " "))))
1018
1019 ; I would think that emacs would have code handy for walking a keymap
1020 ; and generating strings for each key, and I would prefer to just call
1021 ; that. But I couldn't find any (could be all implemented in C I
1022 ; suppose), so I wrote my own here.
1023 (defun notmuch-substitute-one-command-key-with-prefix (prefix binding)
1024   "For a key binding, return a string showing a human-readable
1025 representation of the prefixed key as well as the first line of
1026 documentation from the bound function.
1027
1028 For a mouse binding, return nil."
1029   (let ((key (car binding))
1030         (action (cdr binding)))
1031     (if (mouse-event-p key)
1032         nil
1033       (if (keymapp action)
1034           (let ((substitute (apply-partially 'notmuch-substitute-one-command-key-with-prefix (notmuch-prefix-key-description key)))
1035                 (as-list))
1036             (map-keymap (lambda (a b)
1037                           (push (cons a b) as-list))
1038                         action)
1039             (mapconcat substitute as-list "\n"))
1040         (concat prefix (format-kbd-macro (vector key))
1041                 "\t"
1042                 (notmuch-documentation-first-line action))))))
1043
1044 (defalias 'notmuch-substitute-one-command-key
1045   (apply-partially 'notmuch-substitute-one-command-key-with-prefix nil))
1046
1047 (defun notmuch-substitute-command-keys (doc)
1048   "Like `substitute-command-keys' but with documentation, not function names."
1049   (let ((beg 0))
1050     (while (string-match "\\\\{\\([^}[:space:]]*\\)}" doc beg)
1051       (let ((map (substring doc (match-beginning 1) (match-end 1))))
1052         (setq doc (replace-match (mapconcat 'notmuch-substitute-one-command-key
1053                                             (cdr (symbol-value (intern map))) "\n") 1 1 doc)))
1054       (setq beg (match-end 0)))
1055     doc))
1056
1057 (defun notmuch-help ()
1058   "Display help for the current notmuch mode."
1059   (interactive)
1060   (let* ((mode major-mode)
1061          (doc (substitute-command-keys (notmuch-substitute-command-keys (documentation mode t)))))
1062     (with-current-buffer (generate-new-buffer "*notmuch-help*")
1063       (insert doc)
1064       (goto-char (point-min))
1065       (set-buffer-modified-p nil)
1066       (view-buffer (current-buffer) 'kill-buffer-if-not-modified))))
1067
1068 ;;;###autoload
1069 (defun notmuch-show-mode ()
1070   "Major mode for viewing a thread with notmuch.
1071
1072 This buffer contains the results of the \"notmuch show\" command
1073 for displaying a single thread of email from your email archives.
1074
1075 By default, various components of email messages, (citations,
1076 signatures, already-read messages), are hidden. You can make
1077 these parts visible by clicking with the mouse button or by
1078 pressing RET after positioning the cursor on a hidden part, (for
1079 which \\[notmuch-show-next-button] and \\[notmuch-show-previous-button] are helpful).
1080
1081 Reading the thread sequentially is well-supported by pressing
1082 \\[notmuch-show-advance-and-archive]. This will
1083 scroll the current message (if necessary), advance to the next
1084 message, or advance to the next thread (if already on the last
1085 message of a thread).
1086
1087 Other commands are available to read or manipulate the thread more
1088 selectively, (such as '\\[notmuch-show-next-message]' and '\\[notmuch-show-previous-message]' to advance to messages without
1089 removing any tags, and '\\[notmuch-show-archive-thread]' to archive an entire thread without
1090 scrolling through with \\[notmuch-show-advance-and-archive]).
1091
1092 You can add or remove arbitary tags from the current message with
1093 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
1094
1095 All currently available key bindings:
1096
1097 \\{notmuch-show-mode-map}"
1098   (interactive)
1099   (kill-all-local-variables)
1100   (add-to-invisibility-spec 'notmuch-show-marker)
1101   (use-local-map notmuch-show-mode-map)
1102   (setq major-mode 'notmuch-show-mode
1103         mode-name "notmuch-show")
1104   (setq buffer-read-only t))
1105
1106 (defgroup notmuch nil
1107   "Notmuch mail reader for Emacs."
1108   :group 'mail)
1109
1110 (defcustom notmuch-show-hook nil
1111   "List of functions to call when notmuch displays a message."
1112   :type 'hook
1113   :options '(goto-address)
1114   :group 'notmuch)
1115
1116 (defcustom notmuch-search-hook nil
1117   "List of functions to call when notmuch displays the search results."
1118   :type 'hook
1119   :options '(hl-line-mode)
1120   :group 'notmuch)
1121
1122 (defun notmuch-show-do-stash (text)
1123     (kill-new text)
1124     (message (concat "Saved: " text)))
1125
1126 (defun notmuch-show-stash-cc ()
1127   "Copy CC field of current message to kill-ring."
1128   (interactive)
1129   (notmuch-show-do-stash (notmuch-show-get-cc)))
1130
1131 (defun notmuch-show-stash-date ()
1132   "Copy date of current message to kill-ring."
1133   (interactive)
1134   (notmuch-show-do-stash (notmuch-show-get-date)))
1135
1136 (defun notmuch-show-stash-filename ()
1137   "Copy filename of current message to kill-ring."
1138   (interactive)
1139   (notmuch-show-do-stash (notmuch-show-get-filename)))
1140
1141 (defun notmuch-show-stash-from ()
1142   "Copy From address of current message to kill-ring."
1143   (interactive)
1144   (notmuch-show-do-stash (notmuch-show-get-from)))
1145
1146 (defun notmuch-show-stash-message-id ()
1147   "Copy message ID of current message to kill-ring."
1148   (interactive)
1149   (notmuch-show-do-stash (notmuch-show-get-message-id)))
1150
1151 (defun notmuch-show-stash-subject ()
1152   "Copy Subject field of current message to kill-ring."
1153   (interactive)
1154   (notmuch-show-do-stash (notmuch-show-get-subject)))
1155
1156 (defun notmuch-show-stash-tags ()
1157   "Copy tags of current message to kill-ring as a comma separated list."
1158   (interactive)
1159   (notmuch-show-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1160
1161 (defun notmuch-show-stash-to ()
1162   "Copy To address of current message to kill-ring."
1163   (interactive)
1164   (notmuch-show-do-stash (notmuch-show-get-to)))
1165
1166 ; Make show mode a bit prettier, highlighting URLs and using word wrap
1167
1168 (defun notmuch-show-mark-read ()
1169   (notmuch-show-remove-tag "unread"))
1170
1171 (defun notmuch-show-pretty-hook ()
1172   (goto-address-mode 1)
1173   (visual-line-mode))
1174
1175 (add-hook 'notmuch-show-hook 'notmuch-show-mark-read)
1176 (add-hook 'notmuch-show-hook 'notmuch-show-pretty-hook)
1177 (add-hook 'notmuch-search-hook
1178           (lambda()
1179             (hl-line-mode 1) ))
1180
1181 (defun notmuch-show (thread-id &optional parent-buffer query-context)
1182   "Run \"notmuch show\" with the given thread ID and display results.
1183
1184 The optional PARENT-BUFFER is the notmuch-search buffer from
1185 which this notmuch-show command was executed, (so that the next
1186 thread from that buffer can be show when done with this one).
1187
1188 The optional QUERY-CONTEXT is a notmuch search term. Only messages from the thread
1189 matching this search term are shown if non-nil. "
1190   (interactive "sNotmuch show: ")
1191   (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
1192     (switch-to-buffer buffer)
1193     (notmuch-show-mode)
1194     (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
1195     (let ((proc (get-buffer-process (current-buffer)))
1196           (inhibit-read-only t))
1197       (if proc
1198           (error "notmuch search process already running for query `%s'" thread-id)
1199         )
1200       (erase-buffer)
1201       (goto-char (point-min))
1202       (save-excursion
1203         (let* ((basic-args (list notmuch-command nil t nil "show" "--entire-thread" thread-id))
1204                 (args (if query-context (append basic-args (list "and (" query-context ")")) basic-args)))
1205           (apply 'call-process args)
1206           (when (and (eq (buffer-size) 0) query-context)
1207             (apply 'call-process basic-args)))
1208         (notmuch-show-markup-messages)
1209         )
1210       (run-hooks 'notmuch-show-hook)
1211       ; Move straight to the first open message
1212       (if (not (notmuch-show-message-open-p))
1213           (notmuch-show-next-open-message))
1214       )))
1215
1216 (defvar notmuch-search-authors-width 40
1217   "Number of columns to use to display authors in a notmuch-search buffer.")
1218
1219 (defvar notmuch-search-mode-map
1220   (let ((map (make-sparse-keymap)))
1221     (define-key map "?" 'notmuch-help)
1222     (define-key map "q" 'kill-this-buffer)
1223     (define-key map "x" 'kill-this-buffer)
1224     (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
1225     (define-key map "b" 'notmuch-search-scroll-down)
1226     (define-key map " " 'notmuch-search-scroll-up)
1227     (define-key map "<" 'notmuch-search-first-thread)
1228     (define-key map ">" 'notmuch-search-last-thread)
1229     (define-key map "p" 'notmuch-search-previous-thread)
1230     (define-key map "n" 'notmuch-search-next-thread)
1231     (define-key map "r" 'notmuch-search-reply-to-thread)
1232     (define-key map "m" 'message-mail)
1233     (define-key map "s" 'notmuch-search)
1234     (define-key map "o" 'notmuch-search-toggle-order)
1235     (define-key map "=" 'notmuch-search-refresh-view)
1236     (define-key map "t" 'notmuch-search-filter-by-tag)
1237     (define-key map "f" 'notmuch-search-filter)
1238     (define-key map [mouse-1] 'notmuch-search-show-thread)
1239     (define-key map "*" 'notmuch-search-operate-all)
1240     (define-key map "a" 'notmuch-search-archive-thread)
1241     (define-key map "-" 'notmuch-search-remove-tag)
1242     (define-key map "+" 'notmuch-search-add-tag)
1243     (define-key map (kbd "RET") 'notmuch-search-show-thread)
1244     map)
1245   "Keymap for \"notmuch search\" buffers.")
1246 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
1247
1248 (defvar notmuch-search-query-string)
1249 (defvar notmuch-search-target-thread)
1250 (defvar notmuch-search-target-position)
1251 (defvar notmuch-search-oldest-first t
1252   "Show the oldest mail first in the search-mode")
1253
1254 (defvar notmuch-search-disjunctive-regexp      "\\<[oO][rR]\\>")
1255
1256 (defun notmuch-search-scroll-up ()
1257   "Move forward through search results by one window's worth."
1258   (interactive)
1259   (condition-case nil
1260       (scroll-up nil)
1261     ((end-of-buffer) (notmuch-search-last-thread))))
1262
1263 (defun notmuch-search-scroll-down ()
1264   "Move backward through the search results by one window's worth."
1265   (interactive)
1266   ; I don't know why scroll-down doesn't signal beginning-of-buffer
1267   ; the way that scroll-up signals end-of-buffer, but c'est la vie.
1268   ;
1269   ; So instead of trapping a signal we instead check whether the
1270   ; window begins on the first line of the buffer and if so, move
1271   ; directly to that position. (We have to count lines since the
1272   ; window-start position is not the same as point-min due to the
1273   ; invisible thread-ID characters on the first line.
1274   (if (equal (count-lines (point-min) (window-start)) 0)
1275       (goto-char (point-min))
1276     (scroll-down nil)))
1277
1278 (defun notmuch-search-next-thread ()
1279   "Select the next thread in the search results."
1280   (interactive)
1281   (forward-line 1))
1282
1283 (defun notmuch-search-previous-thread ()
1284   "Select the previous thread in the search results."
1285   (interactive)
1286   (forward-line -1))
1287
1288 (defun notmuch-search-last-thread ()
1289   "Select the last thread in the search results."
1290   (interactive)
1291   (goto-char (point-max))
1292   (forward-line -2))
1293
1294 (defun notmuch-search-first-thread ()
1295   "Select the first thread in the search results."
1296   (interactive)
1297   (goto-char (point-min)))
1298
1299 (defface notmuch-message-summary-face
1300  '((((class color) (background light)) (:background "#f0f0f0"))
1301    (((class color) (background dark)) (:background "#303030")))
1302  "Face for the single-line message summary in notmuch-show-mode."
1303  :group 'notmuch)
1304
1305 (defface notmuch-tag-face
1306   '((((class color)
1307       (background dark))
1308      (:foreground "OliveDrab1"))
1309     (((class color)
1310       (background light))
1311      (:foreground "navy blue" :bold t))
1312     (t
1313      (:bold t)))
1314   "Notmuch search mode face used to highligh tags."
1315   :group 'notmuch)
1316
1317 (defvar notmuch-tag-face-alist nil
1318   "List containing the tag list that need to be highlighed")
1319
1320 (defvar notmuch-search-font-lock-keywords  nil)
1321
1322 ;;;###autoload
1323 (defun notmuch-search-mode ()
1324   "Major mode displaying results of a notmuch search.
1325
1326 This buffer contains the results of a \"notmuch search\" of your
1327 email archives. Each line in the buffer represents a single
1328 thread giving a summary of the thread (a relative date, the
1329 number of matched messages and total messages in the thread,
1330 participants in the thread, a representative subject line, and
1331 any tags).
1332
1333 Pressing \\[notmuch-search-show-thread] on any line displays that thread. The '\\[notmuch-search-add-tag]' and '\\[notmuch-search-remove-tag]'
1334 keys can be used to add or remove tags from a thread. The '\\[notmuch-search-archive-thread]' key
1335 is a convenience for archiving a thread (removing the \"inbox\"
1336 tag). The '\\[notmuch-search-operate-all]' key can be used to add or remove a tag from all
1337 threads in the current buffer.
1338
1339 Other useful commands are '\\[notmuch-search-filter]' for filtering the current search
1340 based on an additional query string, '\\[notmuch-search-filter-by-tag]' for filtering to include
1341 only messages with a given tag, and '\\[notmuch-search]' to execute a new, global
1342 search.
1343
1344 Complete list of currently available key bindings:
1345
1346 \\{notmuch-search-mode-map}"
1347   (interactive)
1348   (kill-all-local-variables)
1349   (make-local-variable 'notmuch-search-query-string)
1350   (make-local-variable 'notmuch-search-oldest-first)
1351   (make-local-variable 'notmuch-search-target-thread)
1352   (make-local-variable 'notmuch-search-target-position)
1353   (set (make-local-variable 'scroll-preserve-screen-position) t)
1354   (add-to-invisibility-spec 'notmuch-search)
1355   (use-local-map notmuch-search-mode-map)
1356   (setq truncate-lines t)
1357   (setq major-mode 'notmuch-search-mode
1358         mode-name "notmuch-search")
1359   (setq buffer-read-only t)
1360   (if (not notmuch-tag-face-alist)
1361       (add-to-list 'notmuch-search-font-lock-keywords (list
1362                 "(\\([^)]*\\))$" '(1  'notmuch-tag-face)))
1363     (let ((notmuch-search-tags (mapcar 'car notmuch-tag-face-alist)))
1364       (loop for notmuch-search-tag  in notmuch-search-tags
1365             do (add-to-list 'notmuch-search-font-lock-keywords (list
1366                         (concat "([^)]*\\(" notmuch-search-tag "\\)[^)]*)$")
1367                         `(1  ,(cdr (assoc notmuch-search-tag notmuch-tag-face-alist))))))))
1368   (set (make-local-variable 'font-lock-defaults)
1369          '(notmuch-search-font-lock-keywords t)))
1370
1371 (defun notmuch-search-find-thread-id ()
1372   "Return the thread for the current thread"
1373   (get-text-property (point) 'notmuch-search-thread-id))
1374
1375 (defun notmuch-search-find-authors ()
1376   "Return the authors for the current thread"
1377   (get-text-property (point) 'notmuch-search-authors))
1378
1379 (defun notmuch-search-find-subject ()
1380   "Return the subject for the current thread"
1381   (get-text-property (point) 'notmuch-search-subject))
1382
1383 (defun notmuch-search-show-thread ()
1384   "Display the currently selected thread."
1385   (interactive)
1386   (let ((thread-id (notmuch-search-find-thread-id)))
1387     (if (> (length thread-id) 0)
1388         (notmuch-show thread-id (current-buffer) notmuch-search-query-string)
1389       (error "End of search results"))))
1390
1391 (defun notmuch-search-reply-to-thread ()
1392   "Begin composing a reply to the entire current thread in a new buffer."
1393   (interactive)
1394   (let ((message-id (notmuch-search-find-thread-id)))
1395     (notmuch-reply message-id)))
1396
1397 (defun notmuch-call-notmuch-process (&rest args)
1398   "Synchronously invoke \"notmuch\" with the given list of arguments.
1399
1400 Output from the process will be presented to the user as an error
1401 and will also appear in a buffer named \"*Notmuch errors*\"."
1402   (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
1403     (with-current-buffer error-buffer
1404         (erase-buffer))
1405     (if (eq (apply 'call-process notmuch-command nil error-buffer nil args) 0)
1406         (point)
1407       (progn
1408         (with-current-buffer error-buffer
1409           (let ((beg (point-min))
1410                 (end (- (point-max) 1)))
1411             (error (buffer-substring beg end))
1412             ))))))
1413
1414 (defun notmuch-search-set-tags (tags)
1415   (save-excursion
1416     (end-of-line)
1417     (re-search-backward "(")
1418     (forward-char)
1419     (let ((beg (point))
1420           (inhibit-read-only t))
1421       (re-search-forward ")")
1422       (backward-char)
1423       (let ((end (point)))
1424         (delete-region beg end)
1425         (insert (mapconcat  'identity tags " "))))))
1426
1427 (defun notmuch-search-get-tags ()
1428   (save-excursion
1429     (end-of-line)
1430     (re-search-backward "(")
1431     (let ((beg (+ (point) 1)))
1432       (re-search-forward ")")
1433       (let ((end (- (point) 1)))
1434         (split-string (buffer-substring beg end))))))
1435
1436 (defun notmuch-search-add-tag (tag)
1437   "Add a tag to the currently selected thread.
1438
1439 The tag is added to messages in the currently selected thread
1440 which match the current search terms."
1441   (interactive
1442    (list (notmuch-select-tag-with-completion "Tag to add: ")))
1443   (notmuch-call-notmuch-process "tag" (concat "+" tag) (notmuch-search-find-thread-id))
1444   (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
1445
1446 (defun notmuch-search-remove-tag (tag)
1447   "Remove a tag from the currently selected thread.
1448
1449 The tag is removed from all messages in the currently selected thread."
1450   (interactive
1451    (list (notmuch-select-tag-with-completion "Tag to remove: " (notmuch-search-find-thread-id))))
1452   (notmuch-call-notmuch-process "tag" (concat "-" tag) (notmuch-search-find-thread-id))
1453   (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
1454
1455 (defun notmuch-search-archive-thread ()
1456   "Archive the currently selected thread (remove its \"inbox\" tag).
1457
1458 This function advances the next thread when finished."
1459   (interactive)
1460   (notmuch-search-remove-tag "inbox")
1461   (forward-line))
1462
1463 (defun notmuch-search-process-sentinel (proc msg)
1464   "Add a message to let user know when \"notmuch search\" exits"
1465   (let ((buffer (process-buffer proc))
1466         (status (process-status proc))
1467         (exit-status (process-exit-status proc))
1468         (never-found-target-thread nil))
1469     (if (memq status '(exit signal))
1470         (if (buffer-live-p buffer)
1471             (with-current-buffer buffer
1472               (save-excursion
1473                 (let ((inhibit-read-only t)
1474                       (atbob (bobp)))
1475                   (goto-char (point-max))
1476                   (if (eq status 'signal)
1477                       (insert "Incomplete search results (search process was killed).\n"))
1478                   (if (eq status 'exit)
1479                       (progn
1480                         (insert "End of search results.")
1481                         (if (not (= exit-status 0))
1482                             (insert (format " (process returned %d)" exit-status)))
1483                         (insert "\n")
1484                         (if (and atbob
1485                                  notmuch-search-target-thread)
1486                             (set 'never-found-target-thread t))))))
1487               (if never-found-target-thread
1488                   (goto-char notmuch-search-target-position)))))))
1489
1490 (defun notmuch-search-process-filter (proc string)
1491   "Process and filter the output of \"notmuch search\""
1492   (let ((buffer (process-buffer proc))
1493         (found-target nil))
1494     (if (buffer-live-p buffer)
1495         (with-current-buffer buffer
1496           (save-excursion
1497             (let ((line 0)
1498                   (more t)
1499                   (inhibit-read-only t))
1500               (while more
1501                 (if (string-match "^\\(thread:[0-9A-Fa-f]*\\) \\(.*\\) \\(\\[[0-9/]*\\]\\) \\([^;]*\\); \\(.*\\) (\\([^()]*\\))$" string line)
1502                     (let* ((thread-id (match-string 1 string))
1503                            (date (match-string 2 string))
1504                            (count (match-string 3 string))
1505                            (authors (match-string 4 string))
1506                            (authors-length (length authors))
1507                            (subject (match-string 5 string))
1508                            (tags (match-string 6 string)))
1509                       (if (> authors-length 40)
1510                           (set 'authors (concat (substring authors 0 (- 40 3)) "...")))
1511                       (goto-char (point-max))
1512                       (let ((beg (point-marker)))
1513                         (insert (format "%s %-7s %-40s %s (%s)\n" date count authors subject tags))
1514                         (put-text-property beg (point-marker) 'notmuch-search-thread-id thread-id)
1515                         (put-text-property beg (point-marker) 'notmuch-search-authors authors)
1516                         (put-text-property beg (point-marker) 'notmuch-search-subject subject)
1517                         (if (and notmuch-search-target-thread
1518                                  (string= thread-id notmuch-search-target-thread))
1519                             (progn
1520                               (set 'found-target beg)
1521                               (set 'notmuch-search-target-thread nil))))
1522                       (set 'line (match-end 0)))
1523                   (set 'more nil)))))
1524           (if found-target
1525               (goto-char found-target)))
1526       (delete-process proc))))
1527
1528 (defun notmuch-search-operate-all (action)
1529   "Add/remove tags from all matching messages.
1530
1531 Tis command adds or removes tags from all messages matching the
1532 current search terms. When called interactively, this command
1533 will prompt for tags to be added or removed. Tags prefixed with
1534 '+' will be added and tags prefixed with '-' will be removed.
1535
1536 Each character of the tag name may consist of alphanumeric
1537 characters as well as `_.+-'.
1538 "
1539   (interactive "sOperation (+add -drop): notmuch tag ")
1540   (let ((action-split (split-string action " +")))
1541     ;; Perform some validation
1542     (let ((words action-split))
1543       (when (null words) (error "No operation given"))
1544       (while words
1545         (unless (string-match-p "^[-+][-+_.[:word:]]+$" (car words))
1546           (error "Action must be of the form `+thistag -that_tag'"))
1547         (setq words (cdr words))))
1548     (apply 'notmuch-call-notmuch-process "tag"
1549            (append action-split (list notmuch-search-query-string) nil))))
1550
1551 ;;;###autoload
1552 (defun notmuch-search (query &optional oldest-first target-thread target-position)
1553   "Run \"notmuch search\" with the given query string and display results.
1554
1555 The optional parameters are used as follows:
1556
1557   oldest-first: A Boolean controlling the sort order of returned threads
1558   target-thread: A thread ID (with the thread: prefix) that will be made
1559                  current if it appears in the search results.
1560   saved-position: If the search results complete, and the target thread is
1561                   not found in the results, and the point is still at the
1562                   beginning of the buffer, then the point will be moved to
1563                   the saved position.
1564 "
1565   (interactive "sNotmuch search: ")
1566   (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
1567     (switch-to-buffer buffer)
1568     (notmuch-search-mode)
1569     (set 'notmuch-search-query-string query)
1570     (set 'notmuch-search-oldest-first oldest-first)
1571     (set 'notmuch-search-target-thread target-thread)
1572     (set 'notmuch-search-target-position target-position)
1573     (let ((proc (get-buffer-process (current-buffer)))
1574           (inhibit-read-only t))
1575       (if proc
1576           (error "notmuch search process already running for query `%s'" query)
1577         )
1578       (erase-buffer)
1579       (goto-char (point-min))
1580       (save-excursion
1581         (let ((proc (start-process-shell-command
1582                      "notmuch-search" buffer notmuch-command "search"
1583                      (if oldest-first "--sort=oldest-first" "--sort=newest-first")
1584                      (shell-quote-argument query))))
1585           (set-process-sentinel proc 'notmuch-search-process-sentinel)
1586           (set-process-filter proc 'notmuch-search-process-filter))))
1587     (run-hooks 'notmuch-search-hook)))
1588
1589 (defun notmuch-search-refresh-view ()
1590   "Refresh the current view.
1591
1592 Kills the current buffer and runs a new search with the same
1593 query string as the current search. If the current thread is in
1594 the new search results, then point will be placed on the same
1595 thread. Otherwise, point will be moved to attempt to be in the
1596 same relative position within the new buffer."
1597   (interactive)
1598   (let ((here (point))
1599         (oldest-first notmuch-search-oldest-first)
1600         (thread (notmuch-search-find-thread-id))
1601         (query notmuch-search-query-string))
1602     (kill-this-buffer)
1603     (notmuch-search query oldest-first thread here)
1604     (goto-char (point-min))
1605     ))
1606
1607 (defun notmuch-search-toggle-order ()
1608   "Toggle the current search order.
1609
1610 By default, the \"inbox\" view created by `notmuch' is displayed
1611 in chronological order (oldest thread at the beginning of the
1612 buffer), while any global searches created by `notmuch-search'
1613 are displayed in reverse-chronological order (newest thread at
1614 the beginning of the buffer).
1615
1616 This command toggles the sort order for the current search.
1617
1618 Note that any filtered searches created by
1619 `notmuch-search-filter' retain the search order of the parent
1620 search."
1621   (interactive)
1622   (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
1623   (notmuch-search-refresh-view))
1624
1625 (defun notmuch-search-filter (query)
1626   "Filter the current search results based on an additional query string.
1627
1628 Runs a new search matching only messages that match both the
1629 current search results AND the additional query string provided."
1630   (interactive "sFilter search: ")
1631   (let ((grouped-query (if (string-match-p notmuch-search-disjunctive-regexp query) (concat "( " query " )") query)))
1632     (notmuch-search (concat notmuch-search-query-string " and " grouped-query) notmuch-search-oldest-first)))
1633
1634 (defun notmuch-search-filter-by-tag (tag)
1635   "Filter the current search results based on a single tag.
1636
1637 Runs a new search matching only messages that match both the
1638 current search results AND that are tagged with the given tag."
1639   (interactive
1640    (list (notmuch-select-tag-with-completion "Filter by tag: ")))
1641   (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
1642
1643
1644 ;;;###autoload
1645 (defun notmuch ()
1646   "Run notmuch to display all mail with tag of 'inbox'"
1647   (interactive)
1648   (notmuch-search "tag:inbox" notmuch-search-oldest-first))
1649
1650 (setq mail-user-agent 'message-user-agent)
1651
1652 (defvar notmuch-folder-mode-map
1653   (let ((map (make-sparse-keymap)))
1654     (define-key map "?" 'notmuch-help)
1655     (define-key map "x" 'kill-this-buffer)
1656     (define-key map "q" 'kill-this-buffer)
1657     (define-key map "m" 'message-mail)
1658     (define-key map "e" 'notmuch-folder-show-empty-toggle)
1659     (define-key map ">" 'notmuch-folder-last)
1660     (define-key map "<" 'notmuch-folder-first)
1661     (define-key map "=" 'notmuch-folder)
1662     (define-key map "s" 'notmuch-search)
1663     (define-key map [mouse-1] 'notmuch-folder-show-search)
1664     (define-key map (kbd "RET") 'notmuch-folder-show-search)
1665     (define-key map " " 'notmuch-folder-show-search)
1666     (define-key map "p" 'notmuch-folder-previous)
1667     (define-key map "n" 'notmuch-folder-next)
1668     map)
1669   "Keymap for \"notmuch folder\" buffers.")
1670
1671 (fset 'notmuch-folder-mode-map notmuch-folder-mode-map)
1672
1673 (defcustom notmuch-folders (quote (("inbox" . "tag:inbox") ("unread" . "tag:unread")))
1674   "List of searches for the notmuch folder view"
1675   :type '(alist :key-type (string) :value-type (string))
1676   :group 'notmuch)
1677
1678 (defun notmuch-folder-mode ()
1679   "Major mode for showing notmuch 'folders'.
1680
1681 This buffer contains a list of message counts returned by a
1682 customizable set of searches of your email archives. Each line in
1683 the buffer shows the name of a saved search and the resulting
1684 message count.
1685
1686 Pressing RET on any line opens a search window containing the
1687 results for the saved search on that line.
1688
1689 Here is an example of how the search list could be
1690 customized, (the following text would be placed in your ~/.emacs
1691 file):
1692
1693 (setq notmuch-folders '((\"inbox\" . \"tag:inbox\")
1694                         (\"unread\" . \"tag:inbox AND tag:unread\")
1695                         (\"notmuch\" . \"tag:inbox AND to:notmuchmail.org\")))
1696
1697 Of course, you can have any number of folders, each configured
1698 with any supported search terms (see \"notmuch help search-terms\").
1699
1700 Currently available key bindings:
1701
1702 \\{notmuch-folder-mode-map}"
1703   (interactive)
1704   (kill-all-local-variables)
1705   (use-local-map 'notmuch-folder-mode-map)
1706   (setq truncate-lines t)
1707   (hl-line-mode 1)
1708   (setq major-mode 'notmuch-folder-mode
1709         mode-name "notmuch-folder")
1710   (setq buffer-read-only t))
1711
1712 (defun notmuch-folder-next ()
1713   "Select the next folder in the list."
1714   (interactive)
1715   (forward-line 1)
1716   (if (eobp)
1717       (forward-line -1)))
1718
1719 (defun notmuch-folder-previous ()
1720   "Select the previous folder in the list."
1721   (interactive)
1722   (forward-line -1))
1723
1724 (defun notmuch-folder-first ()
1725   "Select the first folder in the list."
1726   (interactive)
1727   (goto-char (point-min)))
1728
1729 (defun notmuch-folder-last ()
1730   "Select the last folder in the list."
1731   (interactive)
1732   (goto-char (point-max))
1733   (forward-line -1))
1734
1735 (defun notmuch-folder-count (search)
1736   (car (process-lines notmuch-command "count" search)))
1737
1738 (setq notmuch-folder-show-empty t)
1739
1740 (defun notmuch-folder-show-empty-toggle ()
1741   "Toggle the listing of empty folders"
1742   (interactive)
1743   (setq notmuch-folder-show-empty (not notmuch-folder-show-empty))
1744   (notmuch-folder))
1745
1746 (defun notmuch-folder-add (folders)
1747   (if folders
1748       (let* ((name (car (car folders)))
1749             (inhibit-read-only t)
1750             (search (cdr (car folders)))
1751             (count (notmuch-folder-count search)))
1752         (if (or notmuch-folder-show-empty
1753                 (not (equal count "0")))
1754             (progn
1755               (insert name)
1756               (indent-to 16 1)
1757               (insert count)
1758               (insert "\n")
1759               )
1760           )
1761         (notmuch-folder-add (cdr folders)))))
1762
1763 (defun notmuch-folder-find-name ()
1764   (save-excursion
1765     (beginning-of-line)
1766     (let ((beg (point)))
1767       (re-search-forward "\\([ \t]*[^ \t]+\\)")
1768       (filter-buffer-substring (match-beginning 1) (match-end 1)))))
1769
1770 (defun notmuch-folder-show-search (&optional folder)
1771   "Show a search window for the search related to the specified folder."
1772   (interactive)
1773   (if (null folder)
1774       (setq folder (notmuch-folder-find-name)))
1775   (let ((search (assoc folder notmuch-folders)))
1776     (if search
1777         (notmuch-search (cdr search) notmuch-search-oldest-first))))
1778
1779 ;;;###autoload
1780 (defun notmuch-folder ()
1781   "Show the notmuch folder view and update the displayed counts."
1782   (interactive)
1783   (let ((buffer (get-buffer-create "*notmuch-folders*")))
1784     (switch-to-buffer buffer)
1785     (let ((inhibit-read-only t)
1786           (n (line-number-at-pos)))
1787       (erase-buffer)
1788       (notmuch-folder-mode)
1789       (notmuch-folder-add notmuch-folders)
1790       (goto-char (point-min))
1791       (goto-line n))))
1792
1793 (provide 'notmuch)