]> git.notmuchmail.org Git - notmuch/blob - notmuch.el
Add some very rudimentary support for handling html parts
[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 ; 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-mode-map
55   (let ((map (make-sparse-keymap)))
56     ; I don't actually want all of these toggle commands occupying
57     ; keybindings. They steal valuable key-binding space, are hard
58     ; to remember, and act globally rather than locally.
59     ;
60     ; Will be much preferable to switch to direct manipulation for
61     ; toggling visibility of these components. Probably using
62     ; overlays-at to query and manipulate the current overlay.
63     (define-key map "a" 'notmuch-show-archive-thread)
64     (define-key map "A" 'notmuch-show-mark-read-then-archive-thread)
65     (define-key map "m" 'message-mail)
66     (define-key map "n" 'notmuch-show-next-message)
67     (define-key map "N" 'notmuch-show-mark-read-then-next-open-message)
68     (define-key map "p" 'notmuch-show-previous-message)
69     (define-key map (kbd "C-n") 'notmuch-show-next-line)
70     (define-key map (kbd "C-p") 'notmuch-show-previous-line)
71     (define-key map "q" 'kill-this-buffer)
72     (define-key map "r" 'notmuch-show-reply)
73     (define-key map "v" 'notmuch-show-view-all-mime-parts)
74     (define-key map "w" 'notmuch-show-view-raw-message)
75     (define-key map "x" 'kill-this-buffer)
76     (define-key map "+" 'notmuch-show-add-tag)
77     (define-key map "-" 'notmuch-show-remove-tag)
78     (define-key map (kbd "DEL") 'notmuch-show-rewind)
79     (define-key map " " 'notmuch-show-advance-marking-read-and-archiving)
80     (define-key map "|" 'notmuch-show-pipe-message)
81     (define-key map "?" 'describe-mode)
82     (define-key map (kbd "TAB") 'notmuch-show-next-button)
83     (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
84     map)
85   "Keymap for \"notmuch show\" buffers.")
86 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
87
88 (defvar notmuch-show-signature-regexp "\\(-- ?\\|_+\\)$"
89   "Pattern to match a line that separates content from signature.
90
91 The regexp can (and should) include $ to match the end of the
92 line, but should not include ^ to match the beginning of the
93 line. This is because notmuch may have inserted additional space
94 for indentation at the beginning of the line. But notmuch will
95 move past the indentation when testing this pattern, (so that the
96 pattern can still test against the entire line).")
97
98 (defvar notmuch-show-signature-lines-max 12
99   "Maximum length of signature that will be hidden by default.")
100
101 (defvar notmuch-command "notmuch"
102   "Command to run the notmuch binary.")
103
104 (defvar notmuch-show-message-begin-regexp    "\fmessage{")
105 (defvar notmuch-show-message-end-regexp      "\fmessage}")
106 (defvar notmuch-show-header-begin-regexp     "\fheader{")
107 (defvar notmuch-show-header-end-regexp       "\fheader}")
108 (defvar notmuch-show-body-begin-regexp       "\fbody{")
109 (defvar notmuch-show-body-end-regexp         "\fbody}")
110 (defvar notmuch-show-attachment-begin-regexp "\fattachment{")
111 (defvar notmuch-show-attachment-end-regexp   "\fattachment}")
112 (defvar notmuch-show-part-begin-regexp       "\fpart{")
113 (defvar notmuch-show-part-end-regexp         "\fpart}")
114 (defvar notmuch-show-marker-regexp "\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
115
116 (defvar notmuch-show-id-regexp "\\(id:[^ ]*\\)")
117 (defvar notmuch-show-depth-regexp " depth:\\([0-9]*\\) ")
118 (defvar notmuch-show-filename-regexp "filename:\\(.*\\)$")
119 (defvar notmuch-show-tags-regexp "(\\([^)]*\\))$")
120
121 (defvar notmuch-show-parent-buffer nil)
122 (defvar notmuch-show-body-read-visible nil)
123 (defvar notmuch-show-citations-visible nil)
124 (defvar notmuch-show-signatures-visible nil)
125 (defvar notmuch-show-headers-visible nil)
126
127 ; XXX: This should be a generic function in emacs somewhere, not here
128 (defun point-invisible-p ()
129   "Return whether the character at point is invisible.
130
131 Here visibility is determined by `buffer-invisibility-spec' and
132 the invisible property of any overlays for point. It doesn't have
133 anything to do with whether point is currently being displayed
134 within the current window."
135   (let ((prop (get-char-property (point) 'invisible)))
136     (if (eq buffer-invisibility-spec t)
137         prop
138       (or (memq prop buffer-invisibility-spec)
139           (assq prop buffer-invisibility-spec)))))
140
141 (defun notmuch-select-tag-with-completion (prompt &rest search-terms)
142   (let ((tag-list
143          (with-output-to-string
144            (with-current-buffer standard-output
145              (apply 'call-process notmuch-command nil t nil "search-tags" search-terms)))))
146     (completing-read prompt (split-string tag-list "\n+" t) nil nil nil)))
147
148 (defun notmuch-show-next-line ()
149   "Like builtin `next-line' but ensuring we end on a visible character.
150
151 By advancing forward until reaching a visible character.
152
153 Unlike builtin `next-line' this version accepts no arguments."
154   (interactive)
155   (set 'this-command 'next-line)
156   (call-interactively 'next-line)
157   (while (point-invisible-p)
158     (forward-char)))
159
160 (defun notmuch-show-previous-line ()
161   "Like builtin `previous-line' but ensuring we end on a visible character.
162
163 By advancing forward until reaching a visible character.
164
165 Unlike builtin `next-line' this version accepts no arguments."
166   (interactive)
167   (set 'this-command 'previous-line)
168   (call-interactively 'previous-line)
169   (while (point-invisible-p)
170     (forward-char)))
171
172 (defun notmuch-show-get-message-id ()
173   (save-excursion
174     (beginning-of-line)
175     (if (not (looking-at notmuch-show-message-begin-regexp))
176         (re-search-backward notmuch-show-message-begin-regexp))
177     (re-search-forward notmuch-show-id-regexp)
178     (buffer-substring-no-properties (match-beginning 1) (match-end 1))))
179
180 (defun notmuch-show-get-filename ()
181   (save-excursion
182     (beginning-of-line)
183     (if (not (looking-at notmuch-show-message-begin-regexp))
184         (re-search-backward notmuch-show-message-begin-regexp))
185     (re-search-forward notmuch-show-filename-regexp)
186     (buffer-substring-no-properties (match-beginning 1) (match-end 1))))
187
188 (defun notmuch-show-set-tags (tags)
189   (save-excursion
190     (beginning-of-line)
191     (if (not (looking-at notmuch-show-message-begin-regexp))
192         (re-search-backward notmuch-show-message-begin-regexp))
193     (re-search-forward notmuch-show-tags-regexp)
194     (let ((inhibit-read-only t)
195           (beg (match-beginning 1))
196           (end (match-end 1)))
197       (delete-region beg end)
198       (goto-char beg)
199       (insert (mapconcat 'identity tags " ")))))
200
201 (defun notmuch-show-get-tags ()
202   (save-excursion
203     (beginning-of-line)
204     (if (not (looking-at notmuch-show-message-begin-regexp))
205         (re-search-backward notmuch-show-message-begin-regexp))
206     (re-search-forward notmuch-show-tags-regexp)
207     (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
208
209 (defun notmuch-show-add-tag (&rest toadd)
210   "Add a tag to the current message."
211   (interactive
212    (list (notmuch-select-tag-with-completion "Tag to add: ")))
213   (apply 'notmuch-call-notmuch-process
214          (append (cons "tag"
215                        (mapcar (lambda (s) (concat "+" s)) toadd))
216                  (cons (notmuch-show-get-message-id) nil)))
217   (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
218
219 (defun notmuch-show-remove-tag (&rest toremove)
220   "Remove a tag from the current message."
221   (interactive
222    (list (notmuch-select-tag-with-completion "Tag to remove: " (notmuch-show-get-message-id))))
223   (let ((tags (notmuch-show-get-tags)))
224     (if (intersection tags toremove :test 'string=)
225         (progn
226           (apply 'notmuch-call-notmuch-process
227                  (append (cons "tag"
228                                (mapcar (lambda (s) (concat "-" s)) toremove))
229                          (cons (notmuch-show-get-message-id) nil)))
230           (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
231
232 (defun notmuch-show-archive-thread-maybe-mark-read (markread)
233   (save-excursion
234     (goto-char (point-min))
235     (while (not (eobp))
236       (if markread
237           (notmuch-show-remove-tag "unread" "inbox")
238         (notmuch-show-remove-tag "inbox"))
239       (if (not (eobp))
240           (forward-char))
241       (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
242           (goto-char (point-max)))))
243   (let ((parent-buffer notmuch-show-parent-buffer))
244     (kill-this-buffer)
245     (if parent-buffer
246         (progn
247           (switch-to-buffer parent-buffer)
248           (forward-line)
249           (notmuch-search-show-thread)))))
250
251 (defun notmuch-show-mark-read-then-archive-thread ()
252   "Remove \"unread\" tag from each message, then archive and show next thread.
253
254 Archive each message currently shown by removing the \"unread\"
255 and \"inbox\" tag from each. Then kill this buffer and show the
256 next thread from the search from which this thread was originally
257 shown.
258
259 Note: This command is safe from any race condition of new messages
260 being delivered to the same thread. It does not archive the
261 entire thread, but only the messages shown in the current
262 buffer."
263   (interactive)
264   (notmuch-show-archive-thread-maybe-mark-read t))
265
266 (defun notmuch-show-archive-thread ()
267   "Archive each message in thread, and show next thread from search.
268
269 Archive each message currently shown by removing the \"inbox\"
270 tag from each. Then kill this buffer and show the next thread
271 from the search from which this thread was originally shown.
272
273 Note: This command is safe from any race condition of new messages
274 being delivered to the same thread. It does not archive the
275 entire thread, but only the messages shown in the current
276 buffer."
277   (interactive)
278   (notmuch-show-archive-thread-maybe-mark-read nil))
279
280 (defun notmuch-show-view-raw-message ()
281   "View the raw email of the current message."
282   (interactive)
283   (view-file (notmuch-show-get-filename)))
284
285 (defun notmuch-show-view-all-mime-parts ()
286   "Use external viewers (according to mailcap) to view all MIME-encoded parts."
287   (interactive)
288   (save-excursion
289     (let ((filename (notmuch-show-get-filename)))
290       (switch-to-buffer (generate-new-buffer (concat "*notmuch-mime-"
291                                                      filename
292                                                      "*")))
293       (insert-file-contents filename nil nil nil t)
294       (mm-display-parts (mm-dissect-buffer))
295       (kill-this-buffer))))
296
297 (defun notmuch-reply (query-string)
298   (switch-to-buffer (generate-new-buffer "notmuch-draft"))
299   (call-process notmuch-command nil t nil "reply" query-string)
300   (message-insert-signature)
301   (goto-char (point-min))
302   (if (re-search-forward "^$" nil t)
303       (progn
304         (insert "--text follows this line--")
305         (forward-line)))
306   (message-mode))
307
308 (defun notmuch-show-reply ()
309   "Begin composing a reply to the current message in a new buffer."
310   (interactive)
311   (let ((message-id (notmuch-show-get-message-id)))
312     (notmuch-reply message-id)))
313
314 (defun notmuch-show-pipe-message (command)
315   "Pipe the contents of the current message to the given command.
316
317 The given command will be executed with the raw contents of the
318 current email message as stdin. Anything printed by the command
319 to stdout or stderr will appear in the *Messages* buffer."
320   (interactive "sPipe message to command: ")
321   (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*"
322          (list command " < " (shell-quote-argument (notmuch-show-get-filename)))))
323
324 (defun notmuch-show-move-to-current-message-summary-line ()
325   "Move to the beginning of the one-line summary of the current message.
326
327 This gives us a stable place to move to and work from since the
328 summary line is always visible. This is important since moving to
329 an invisible location is unreliable, (the main command loop moves
330 point either forward or backward to the next visible character
331 when a command ends with point on an invisible character).
332
333 Emits an error if point is not within a valid message, (that is
334 not pattern of `notmuch-show-message-begin-regexp' could be found
335 by searching backward)."
336   (beginning-of-line)
337   (if (not (looking-at notmuch-show-message-begin-regexp))
338       (if (re-search-backward notmuch-show-message-begin-regexp nil t)
339           (forward-line 2)
340         (error "Not within a valid message."))
341     (forward-line 2)))
342
343 (defun notmuch-show-last-message-p ()
344   "Predicate testing whether point is within the last message."
345   (save-window-excursion
346     (save-excursion
347       (notmuch-show-move-to-current-message-summary-line)
348       (not (re-search-forward notmuch-show-message-begin-regexp nil t)))))
349
350 (defun notmuch-show-message-unread-p ()
351   "Preficate testing whether current message is unread."
352   (member "unread" (notmuch-show-get-tags)))
353
354 (defun notmuch-show-next-message ()
355   "Advance to the beginning of the next message in the buffer.
356
357 Moves to the last visible character of the current message if
358 already on the last message in the buffer."
359   (interactive)
360   (notmuch-show-move-to-current-message-summary-line)
361   (if (re-search-forward notmuch-show-message-begin-regexp nil t)
362       (notmuch-show-move-to-current-message-summary-line)
363     (goto-char (- (point-max) 1))
364     (while (point-invisible-p)
365       (backward-char)))
366   (recenter 0))
367
368 (defun notmuch-show-find-next-message ()
369   "Returns the position of the next message in the buffer.
370
371 Or the position of the last visible character of the current
372 message if already within the last message in the buffer."
373   ; save-excursion doesn't save our window position
374   ; save-window-excursion doesn't save point
375   ; Looks like we have to use both.
376   (save-excursion
377     (save-window-excursion
378       (notmuch-show-next-message)
379       (point))))
380
381 (defun notmuch-show-next-unread-message ()
382   "Advance to the beginning of the next unread message in the buffer.
383
384 Moves to the last visible character of the current message if
385 there are no more unread messages past the current point."
386   (notmuch-show-next-message)
387   (while (and (not (notmuch-show-last-message-p))
388               (not (notmuch-show-message-unread-p)))
389     (notmuch-show-next-message))
390   (if (not (notmuch-show-message-unread-p))
391       (notmuch-show-next-message)))
392
393 (defun notmuch-show-next-open-message ()
394   "Advance to the next message which is not hidden.
395
396 If read messages are currently hidden, advance to the next unread
397 message. Otherwise, advance to the next message."
398   (if (or (memq 'notmuch-show-body-read buffer-invisibility-spec)
399           (assq 'notmuch-show-body-read buffer-invisibility-spec))
400       (notmuch-show-next-unread-message)
401     (notmuch-show-next-message)))
402
403 (defun notmuch-show-previous-message ()
404   "Backup to the beginning of the previous message in the buffer.
405
406 If within a message rather than at the beginning of it, then
407 simply move to the beginning of the current message."
408   (interactive)
409   (let ((start (point)))
410     (notmuch-show-move-to-current-message-summary-line)
411     (if (not (< (point) start))
412         ; Go backward twice to skip the current message's marker
413         (progn
414           (re-search-backward notmuch-show-message-begin-regexp nil t)
415           (re-search-backward notmuch-show-message-begin-regexp nil t)
416           (notmuch-show-move-to-current-message-summary-line)
417           ))
418     (recenter 0)))
419
420 (defun notmuch-show-find-previous-message ()
421   "Returns the position of the previous message in the buffer.
422
423 Or the position of the beginning of the current message if point
424 is originally within the message rather than at the beginning of
425 it."
426   ; save-excursion doesn't save our window position
427   ; save-window-excursion doesn't save point
428   ; Looks like we have to use both.
429   (save-excursion
430     (save-window-excursion
431       (notmuch-show-previous-message)
432       (point))))
433
434 (defun notmuch-show-mark-read-then-next-open-message ()
435   "Remove unread tag from current message, then advance to next unread message."
436   (interactive)
437   (notmuch-show-remove-tag "unread")
438   (notmuch-show-next-open-message))
439
440 (defun notmuch-show-rewind ()
441   "Do reverse scrolling compared to `notmuch-show-advance-marking-read-and-archiving'
442
443 Specifically, if the beginning of the previous email is fewer
444 than `window-height' lines from the current point, move to it
445 just like `notmuch-show-previous-message'.
446
447 Otherwise, just scroll down a screenful of the current message.
448
449 This command does not modify any message tags, (it does not undo
450 any effects from previous calls to
451 `notmuch-show-advance-marking-read-and-archiving'."
452   (interactive)
453   (let ((previous (notmuch-show-find-previous-message)))
454     (if (> (count-lines previous (point)) (- (window-height) next-screen-context-lines))
455         (progn
456           (condition-case nil
457               (scroll-down nil)
458             ((beginning-of-buffer) nil))
459           (goto-char (window-start)))
460       (notmuch-show-previous-message))))
461
462 (defun notmuch-show-advance-marking-read-and-archiving ()
463   "Advance through buffer, marking read and archiving.
464
465 This command is intended to be one of the simplest ways to
466 process a thread of email. It does the following:
467
468 If the current message in the thread is not yet fully visible,
469 scroll by a near screenful to read more of the message.
470
471 Otherwise, (the end of the current message is already within the
472 current window), remove the \"unread\" tag (if present) from the
473 current message and advance to the next open message.
474
475 Finally, if there is no further message to advance to, and this
476 last message is already read, then archive the entire current
477 thread, (remove the \"inbox\" tag from each message). Also kill
478 this buffer, and display the next thread from the search from
479 which this thread was originally shown."
480   (interactive)
481   (let ((next (notmuch-show-find-next-message))
482         (unread (notmuch-show-message-unread-p)))
483     (if (> next (window-end))
484         (scroll-up nil)
485       (let ((last (notmuch-show-last-message-p)))
486         (notmuch-show-mark-read-then-next-open-message)
487         (if last
488             (notmuch-show-archive-thread))))))
489
490 (defun notmuch-show-next-button ()
491   "Advance point to the next button in the buffer."
492   (interactive)
493   (goto-char (button-start (next-button (point)))))
494
495 (defun notmuch-show-previous-button ()
496   "Move point back to the previous button in the buffer."
497   (interactive)
498   (goto-char (button-start (previous-button (point)))))
499
500 (defun notmuch-toggle-invisible-action (cite-button)
501   (let ((invis-spec (button-get button 'invisibility-spec)))
502         (if (invisible-p invis-spec)
503             (remove-from-invisibility-spec invis-spec)
504           (add-to-invisibility-spec invis-spec)
505           ))
506   (force-window-update)
507   (redisplay t))
508
509 (define-button-type 'notmuch-button-invisibility-toggle-type 'action 'notmuch-toggle-invisible-action 'follow-link t)
510 (define-button-type 'notmuch-button-citation-toggle-type 'help-echo "mouse-1, RET: Show citation"
511   :supertype 'notmuch-button-invisibility-toggle-type)
512 (define-button-type 'notmuch-button-signature-toggle-type 'help-echo "mouse-1, RET: Show signature"
513   :supertype 'notmuch-button-invisibility-toggle-type)
514 (define-button-type 'notmuch-button-headers-toggle-type 'help-echo "mouse-1, RET: Show headers"
515   :supertype 'notmuch-button-invisibility-toggle-type)
516 (define-button-type 'notmuch-button-body-toggle-type 'help-echo "mouse-1, RET: Show message"
517   :supertype 'notmuch-button-invisibility-toggle-type)
518
519 (defun notmuch-show-markup-citations-region (beg end depth)
520   (goto-char beg)
521   (beginning-of-line)
522   (while (< (point) end)
523     (let ((beg-sub (point-marker))
524           (indent (make-string depth ? ))
525           (citation "[[:space:]]*>"))
526       (if (looking-at citation)
527           (progn
528             (while (looking-at citation)
529               (forward-line))
530             (let ((overlay (make-overlay beg-sub (point)))
531                   (invis-spec (make-symbol "notmuch-citation-region")))
532               (add-to-invisibility-spec invis-spec)
533               (overlay-put overlay 'invisible invis-spec)
534               (let ((p (point))
535                     (cite-button-text
536                      (concat "["  (number-to-string (count-lines beg-sub (point)))
537                              "-line citation.]")))
538                 (goto-char (- beg-sub 1))
539                 (insert (concat "\n" indent))
540                 (insert-button cite-button-text
541                                'invisibility-spec invis-spec
542                                :type 'notmuch-button-citation-toggle-type)
543                 (insert "\n")
544                 (goto-char (+ (length cite-button-text) p))
545               ))))
546       (move-to-column depth)
547       (if (looking-at notmuch-show-signature-regexp)
548           (let ((sig-lines (- (count-lines beg-sub end) 1)))
549             (if (<= sig-lines notmuch-show-signature-lines-max)
550                 (progn
551                   (let ((invis-spec (make-symbol "notmuch-signature-region")))
552                     (add-to-invisibility-spec invis-spec)
553                     (overlay-put (make-overlay beg-sub end)
554                                  'invisible invis-spec)
555                   
556                     (goto-char (- beg-sub 1))
557                     (insert (concat "\n" indent))
558                     (let ((sig-button-text (concat "[" (number-to-string sig-lines)
559                                                    "-line signature.]")))
560                       (insert-button sig-button-text 'invisibility-spec invis-spec
561                                      :type 'notmuch-button-signature-toggle-type)
562                      )
563                     (insert "\n")
564                     (goto-char end))))))
565       (forward-line))))
566
567 (defun notmuch-show-markup-part (beg end depth mime-message)
568   (if (re-search-forward notmuch-show-part-begin-regexp nil t)
569       (progn
570         (if (eq mime-message nil)
571             (let ((filename (notmuch-show-get-filename)))
572               (with-temp-buffer
573                 (insert-file-contents filename nil nil nil t)
574                 (setq mime-message (mm-dissect-buffer)))))
575         (forward-line)
576         (let ((part-beg (point-marker)))
577           (re-search-forward notmuch-show-part-end-regexp)
578
579           (let ((part-end (copy-marker (match-beginning 0))))
580             (goto-char part-end)
581             (if (not (bolp))
582                 (insert "\n"))
583             (indent-rigidly part-beg part-end depth)
584             (save-excursion
585               (goto-char part-beg)
586               (forward-line -1)
587               (beginning-of-line)
588               (let ((handle-type (mm-handle-type mime-message))
589                     mime-type)
590                 (if (sequencep (car handle-type))
591                     (setq mime-type (car handle-type))
592                   (setq mime-type (car (car (cdr handle-type))))
593                   )
594                 (if (equal mime-type "text/html")
595                     (mm-display-part mime-message))))
596
597             (notmuch-show-markup-citations-region part-beg part-end depth)
598             ; Advance to the next part (if any) (so the outer loop can
599             ; determine whether we've left the current message.
600             (if (re-search-forward notmuch-show-part-begin-regexp nil t)
601                 (beginning-of-line)))))
602     (goto-char end))
603   mime-message)
604
605 (defun notmuch-show-markup-parts-region (beg end depth)
606   (save-excursion
607     (goto-char beg)
608     (let (mime-message)
609       (while (< (point) end)
610         (setq mime-message
611               (notmuch-show-markup-part
612                beg end depth mime-message))))))
613
614 (defun notmuch-show-markup-body (depth btn)
615   (re-search-forward notmuch-show-body-begin-regexp)
616   (forward-line)
617   (let ((beg (point-marker)))
618     (re-search-forward notmuch-show-body-end-regexp)
619     (let ((end (copy-marker (match-beginning 0))))
620       (notmuch-show-markup-parts-region beg end depth)
621       (let ((invis-spec (make-symbol "notmuch-show-body-read")))
622         (overlay-put (make-overlay beg end)
623                      'invisible invis-spec)
624         (button-put btn 'invisibility-spec invis-spec)
625         (if (not (notmuch-show-message-unread-p))
626             (add-to-invisibility-spec invis-spec)))
627       (set-marker beg nil)
628       (set-marker end nil)
629       )))
630
631 (defun notmuch-show-markup-header (depth)
632   (re-search-forward notmuch-show-header-begin-regexp)
633   (forward-line)
634   (let ((beg (point-marker))
635         (btn nil))
636     (end-of-line)
637     ; Inverse video for subject
638     (overlay-put (make-overlay beg (point)) 'face '(:inverse-video t))
639     (setq btn (make-button beg (point) :type 'notmuch-button-body-toggle-type))
640     (forward-line 1)
641     (end-of-line)
642     (let ((beg-hidden (point-marker)))
643       (re-search-forward notmuch-show-header-end-regexp)
644       (beginning-of-line)
645       (let ((end (point-marker)))
646         (goto-char beg)
647         (forward-line)
648         (while (looking-at "[A-Za-z][-A-Za-z0-9]*:")
649           (beginning-of-line)
650           (overlay-put (make-overlay (point) (re-search-forward ":"))
651                        'face 'bold)
652           (forward-line)
653           )
654         (indent-rigidly beg end depth)
655         (let ((invis-spec (make-symbol "notmuch-show-header")))
656           (add-to-invisibility-spec (cons invis-spec t))
657           (overlay-put (make-overlay beg-hidden end)
658                        'invisible invis-spec)
659           (goto-char beg)
660           (forward-line)
661           (make-button (line-beginning-position) (line-end-position)
662                         'invisibility-spec (cons invis-spec t)
663                         :type 'notmuch-button-headers-toggle-type))
664         (goto-char end)
665         (insert "\n")
666         (set-marker beg nil)
667         (set-marker beg-hidden nil)
668         (set-marker end nil)
669         ))
670     btn))
671
672 (defun notmuch-show-markup-message ()
673   (if (re-search-forward notmuch-show-message-begin-regexp nil t)
674       (progn
675         (re-search-forward notmuch-show-depth-regexp)
676         (let ((depth (string-to-number (buffer-substring (match-beginning 1) (match-end 1))))
677               (btn nil))
678           (setq btn (notmuch-show-markup-header depth))
679           (notmuch-show-markup-body depth btn)))
680     (goto-char (point-max))))
681
682 (defun notmuch-show-hide-markers ()
683   (save-excursion
684     (goto-char (point-min))
685     (while (not (eobp))
686       (if (re-search-forward notmuch-show-marker-regexp nil t)
687           (progn
688             (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
689                          'invisible 'notmuch-show-marker))
690         (goto-char (point-max))))))
691
692 (defun notmuch-show-markup-messages ()
693   (save-excursion
694     (goto-char (point-min))
695     (while (not (eobp))
696       (notmuch-show-markup-message)))
697   (notmuch-show-hide-markers))
698
699 ;;;###autoload
700 (defun notmuch-show-mode ()
701   "Major mode for viewing a thread with notmuch.
702
703 This buffer contains the results of the \"notmuch show\" command
704 for displaying a single thread of email from your email archives.
705
706 By default, various components of email messages, (citations,
707 signatures, already-read messages), are invisible to help you
708 focus on the most important things, (new text from unread
709 messages). See the various commands below for toggling the
710 visibility of hidden components.
711
712 The `notmuch-show-next-message' and
713 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
714 default), allow you to navigate to the next and previous
715 messages. Each time you navigate away from a message with
716 `notmuch-show-next-message' the current message will have its
717 \"unread\" tag removed.
718
719 You can add or remove tags from the current message with '+' and
720 '-'.  You can also archive all messages in the current
721 view, (remove the \"inbox\" tag from each), with
722 `notmuch-show-archive-thread' (bound to 'a' by default).
723
724 \\{notmuch-show-mode-map}"
725   (interactive)
726   (kill-all-local-variables)
727   (add-to-invisibility-spec 'notmuch-show-marker)
728   (use-local-map notmuch-show-mode-map)
729   (setq major-mode 'notmuch-show-mode
730         mode-name "notmuch-show")
731   (setq buffer-read-only t))
732
733 ;;;###autoload
734
735 (defgroup notmuch nil
736   "Notmuch mail reader for Emacs."
737   :group 'mail)
738
739 (defcustom notmuch-show-hook nil
740   "List of functions to call when notmuch displays a message."
741   :type 'hook
742   :options '(goto-address)
743   :group 'notmuch)
744
745 (defcustom notmuch-search-hook nil
746   "List of functions to call when notmuch displays the search results."
747   :type 'hook
748   :options '(hl-line-mode)
749   :group 'notmuch)
750
751 ; Make show mode a bit prettier, highlighting URLs and using word wrap
752
753 (defun notmuch-show-pretty-hook ()
754   (goto-address-mode 1)
755   (visual-line-mode))
756
757 (add-hook 'notmuch-show-hook 'notmuch-show-pretty-hook)
758 (add-hook 'notmuch-search-hook
759           (lambda()
760             (hl-line-mode 1) ))
761
762 (defun notmuch-show (thread-id &optional parent-buffer)
763   "Run \"notmuch show\" with the given thread ID and display results.
764
765 The optional PARENT-BUFFER is the notmuch-search buffer from
766 which this notmuch-show command was executed, (so that the next
767 thread from that buffer can be show when done with this one)."
768   (interactive "sNotmuch show: ")
769   (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
770     (switch-to-buffer buffer)
771     (notmuch-show-mode)
772     (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
773     (let ((proc (get-buffer-process (current-buffer)))
774           (inhibit-read-only t))
775       (if proc
776           (error "notmuch search process already running for query `%s'" thread-id)
777         )
778       (erase-buffer)
779       (goto-char (point-min))
780       (save-excursion
781         (call-process notmuch-command nil t nil "show" thread-id)
782         (notmuch-show-markup-messages)
783         )
784       (run-hooks 'notmuch-show-hook)
785       ; Move straight to the first unread message
786       (if (not (notmuch-show-message-unread-p))
787           (progn
788             (notmuch-show-next-unread-message)
789             ; But if there are no unread messages, go back to the
790             ; beginning of the buffer, and open up the bodies of all
791             ; read message.
792             (if (not (notmuch-show-message-unread-p))
793                 (progn
794                   (goto-char (point-min))
795                   (let ((btn (forward-button 1)))
796                     (while btn
797                       (if (button-has-type-p btn 'notmuch-button-body-toggle-type)
798                           (push-button))
799                       (condition-case err
800                           (setq btn (forward-button 1))
801                         (error (setq btn nil)))
802                     ))
803                   (beginning-of-buffer)
804                   ))))
805       )))
806
807 (defvar notmuch-search-authors-width 40
808   "Number of columns to use to display authors in a notmuch-search buffer.")
809
810 (defvar notmuch-search-mode-map
811   (let ((map (make-sparse-keymap)))
812     (define-key map "a" 'notmuch-search-archive-thread)
813     (define-key map "b" 'notmuch-search-scroll-down)
814     (define-key map "f" 'notmuch-search-filter)
815     (define-key map "m" 'message-mail)
816     (define-key map "n" 'next-line)
817     (define-key map "o" 'notmuch-search-toggle-order)
818     (define-key map "p" 'previous-line)
819     (define-key map "q" 'kill-this-buffer)
820     (define-key map "r" 'notmuch-search-reply-to-thread)
821     (define-key map "s" 'notmuch-search)
822     (define-key map "t" 'notmuch-search-filter-by-tag)
823     (define-key map "x" 'kill-this-buffer)
824     (define-key map (kbd "RET") 'notmuch-search-show-thread)
825     (define-key map [mouse-1] 'notmuch-search-show-thread)
826     (define-key map "+" 'notmuch-search-add-tag)
827     (define-key map "-" 'notmuch-search-remove-tag)
828     (define-key map "*" 'notmuch-search-operate-all)
829     (define-key map "<" 'beginning-of-buffer)
830     (define-key map ">" 'notmuch-search-goto-last-thread)
831     (define-key map "=" 'notmuch-search-refresh-view)
832     (define-key map "\M->" 'notmuch-search-goto-last-thread)
833     (define-key map " " 'notmuch-search-scroll-up)
834     (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
835     (define-key map "?" 'describe-mode)
836     map)
837   "Keymap for \"notmuch search\" buffers.")
838 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
839
840 (defvar notmuch-search-query-string)
841 (defvar notmuch-search-oldest-first t
842   "Show the oldest mail first in the search-mode")
843
844
845 (defun notmuch-search-scroll-up ()
846   "Scroll up, moving point to last message in thread if at end."
847   (interactive)
848   (condition-case nil
849       (scroll-up nil)
850     ((end-of-buffer) (notmuch-search-goto-last-thread))))
851
852 (defun notmuch-search-scroll-down ()
853   "Scroll down, moving point to first message in thread if at beginning."
854   (interactive)
855   ; I don't know why scroll-down doesn't signal beginning-of-buffer
856   ; the way that scroll-up signals end-of-buffer, but c'est la vie.
857   ;
858   ; So instead of trapping a signal we instead check whether the
859   ; window begins on the first line of the buffer and if so, move
860   ; directly to that position. (We have to count lines since the
861   ; window-start position is not the same as point-min due to the
862   ; invisible thread-ID characters on the first line.
863   (if (equal (count-lines (point-min) (window-start)) 1)
864       (goto-char (window-start))
865     (scroll-down nil)))
866
867 (defun notmuch-search-goto-last-thread ()
868   "Move point to the last thread in the buffer."
869   (interactive)
870   (goto-char (point-max))
871   (forward-line -1))
872
873 ;;;###autoload
874 (defun notmuch-search-mode ()
875   "Major mode for searching mail with notmuch.
876
877 This buffer contains the results of a \"notmuch search\" of your
878 email archives. Each line in the buffer represents a single
879 thread giving a relative date for the thread and a subject.
880
881 Pressing RET on any line displays that thread. The '+' and '-'
882 keys can be used to add or remove tags from a thread. The 'a' key
883 is a convenience key for archiving a thread (removing the
884 \"inbox\" tag).
885
886 Other useful commands are `notmuch-search-filter' for filtering
887 the current search based on an additional query string,
888 `notmuch-search-filter-by-tag' for filtering to include only
889 messages with a given tag, and `notmuch-search' to execute a new,
890 global search.
891
892 \\{notmuch-search-mode-map}"
893   (interactive)
894   (kill-all-local-variables)
895   (make-local-variable 'notmuch-search-query-string)
896   (make-local-variable 'notmuch-search-oldest-first)
897   (set (make-local-variable 'scroll-preserve-screen-position) t)
898   (add-to-invisibility-spec 'notmuch-search)
899   (use-local-map notmuch-search-mode-map)
900   (setq truncate-lines t)
901   (setq major-mode 'notmuch-search-mode
902         mode-name "notmuch-search")
903   (setq buffer-read-only t))
904
905 (defun notmuch-search-find-thread-id ()
906   "Return the thread for the current thread"
907   (get-text-property (point) 'notmuch-search-thread-id))
908
909 (defun notmuch-search-show-thread ()
910   (interactive)
911   (let ((thread-id (notmuch-search-find-thread-id)))
912     (if (> (length thread-id) 0)
913         (notmuch-show thread-id (current-buffer))
914       (error "End of search results"))))
915
916 (defun notmuch-search-reply-to-thread ()
917   "Begin composing a reply to the entire current thread in a new buffer."
918   (interactive)
919   (let ((message-id (notmuch-search-find-thread-id)))
920     (notmuch-reply message-id)))
921
922 (defun notmuch-call-notmuch-process (&rest args)
923   "Synchronously invoke \"notmuch\" with the given list of arguments.
924
925 Output from the process will be presented to the user as an error
926 and will also appear in a buffer named \"*Notmuch errors*\"."
927   (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
928     (with-current-buffer error-buffer
929         (erase-buffer))
930     (if (eq (apply 'call-process notmuch-command nil error-buffer nil args) 0)
931         (point)
932       (progn
933         (with-current-buffer error-buffer
934           (let ((beg (point-min))
935                 (end (- (point-max) 1)))
936             (error (buffer-substring beg end))
937             ))))))
938
939 (defun notmuch-search-set-tags (tags)
940   (save-excursion
941     (end-of-line)
942     (re-search-backward "(")
943     (forward-char)
944     (let ((beg (point))
945           (inhibit-read-only t))
946       (re-search-forward ")")
947       (backward-char)
948       (let ((end (point)))
949         (delete-region beg end)
950         (insert (mapconcat  'identity tags " "))))))
951
952 (defun notmuch-search-get-tags ()
953   (save-excursion
954     (end-of-line)
955     (re-search-backward "(")
956     (let ((beg (+ (point) 1)))
957       (re-search-forward ")")
958       (let ((end (- (point) 1)))
959         (split-string (buffer-substring beg end))))))
960
961 (defun notmuch-search-add-tag (tag)
962   "Add a tag to messages in the current thread matching the
963 active query."
964   (interactive
965    (list (notmuch-select-tag-with-completion "Tag to add: ")))
966   (notmuch-call-notmuch-process "tag" (concat "+" tag) (notmuch-search-find-thread-id) " and " notmuch-search-query-string)
967   (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
968
969 (defun notmuch-search-remove-tag (tag)
970   "Remove a tag from messages in the current thread matching the
971 active query."
972   (interactive
973    (list (notmuch-select-tag-with-completion "Tag to remove: " (notmuch-search-find-thread-id))))
974   (notmuch-call-notmuch-process "tag" (concat "-" tag) (notmuch-search-find-thread-id) " and " notmuch-search-query-string)
975   (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
976
977 (defun notmuch-search-archive-thread ()
978   "Archive the current thread (remove its \"inbox\" tag).
979
980 This function advances point to the next line when finished."
981   (interactive)
982   (notmuch-search-remove-tag "inbox")
983   (forward-line))
984
985 (defun notmuch-search-process-sentinel (proc msg)
986   "Add a message to let user know when \"notmuch search\" exits"
987   (let ((buffer (process-buffer proc))
988         (status (process-status proc))
989         (exit-status (process-exit-status proc)))
990     (if (memq status '(exit signal))
991         (if (buffer-live-p buffer)
992             (with-current-buffer buffer
993               (save-excursion
994                 (let ((inhibit-read-only t))
995                   (goto-char (point-max))
996                   (if (eq status 'signal)
997                       (insert "Incomplete search results (search process was killed).\n"))
998                   (if (eq status 'exit)
999                       (progn
1000                         (insert "End of search results.")
1001                         (if (not (= exit-status 0))
1002                             (insert (format " (process returned %d)" exit-status)))
1003                         (insert "\n"))))))))))
1004
1005 (defun notmuch-search-process-filter (proc string)
1006   "Process and filter the output of \"notmuch search\""
1007   (let ((buffer (process-buffer proc)))
1008     (if (buffer-live-p buffer)
1009         (with-current-buffer buffer
1010           (save-excursion
1011             (let ((line 0)
1012                   (more t)
1013                   (inhibit-read-only t))
1014               (while more
1015                 (if (string-match "^\\(thread:[0-9A-Fa-f]*\\) \\(.*\\) \\(\\[[0-9/]*\\]\\) \\([^:]*\\); \\(.*\\) (\\([^()]*\\))$" string line)
1016                     (let* ((thread-id (match-string 1 string))
1017                            (date (match-string 2 string))
1018                            (count (match-string 3 string))
1019                            (authors (match-string 4 string))
1020                            (authors-length (length authors))
1021                            (subject (match-string 5 string))
1022                            (tags (match-string 6 string)))
1023                       (if (> authors-length 40)
1024                           (set 'authors (concat (substring authors 0 (- 40 3)) "...")))
1025                       (goto-char (point-max))
1026                       (let ((beg (point-marker)))
1027                         (insert (format "%s %-7s %-40s %s (%s)\n" date count authors subject tags))
1028                         (put-text-property beg (point-marker) 'notmuch-search-thread-id thread-id))
1029                       (set 'line (match-end 0)))
1030                   (set 'more nil))))))
1031       (delete-process proc))))
1032
1033 (defun notmuch-search-operate-all (action)
1034   "Operate on all messages matching the current query.  Any
1035 number of whitespace separated actions can be given.  Each action
1036 must have one of the two forms
1037
1038   +tagname              Add the tag `tagname'
1039   -tagname              Remove the tag `tagname'
1040
1041 Each character of the tag name may consist of alphanumeric
1042 characters as well as `_.+-'.
1043 "
1044   (interactive "sOperation (+add -drop): notmuch tag ")
1045   (let ((action-split (split-string action " +")))
1046     ;; Perform some validation
1047     (let ((words action-split))
1048       (when (null words) (error "No operation given"))
1049       (while words
1050         (unless (string-match-p "^[\+\-][_\+\-\\w]+$" (car words))
1051           (error "Action must be of the form `+thistag -that_tag'"))
1052         (setq words (cdr words))))
1053     (apply 'notmuch-call-notmuch-process "tag"
1054            (append action-split (list notmuch-search-query-string) nil))))
1055
1056 (defun notmuch-search (query &optional oldest-first)
1057   "Run \"notmuch search\" with the given query string and display results."
1058   (interactive "sNotmuch search: ")
1059   (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
1060     (switch-to-buffer buffer)
1061     (notmuch-search-mode)
1062     (set 'notmuch-search-query-string query)
1063     (set 'notmuch-search-oldest-first oldest-first)
1064     (let ((proc (get-buffer-process (current-buffer)))
1065           (inhibit-read-only t))
1066       (if proc
1067           (error "notmuch search process already running for query `%s'" query)
1068         )
1069       (erase-buffer)
1070       (goto-char (point-min))
1071       (save-excursion
1072         (let ((proc (start-process-shell-command
1073                      "notmuch-search" buffer notmuch-command "search"
1074                      (if oldest-first "--sort=oldest-first" "--sort=newest-first")
1075                      (shell-quote-argument query))))
1076           (set-process-sentinel proc 'notmuch-search-process-sentinel)
1077           (set-process-filter proc 'notmuch-search-process-filter))))
1078     (run-hooks 'notmuch-search-hook)))
1079
1080 (defun notmuch-search-refresh-view ()
1081   "Refresh the current view.
1082
1083 Kills the current buffer and runs a new search with the same
1084 query string as the current search. If the current thread is in
1085 the new search results, then point will be placed on the same
1086 thread. Otherwise, point will be moved to attempt to be in the
1087 same relative position within the new buffer."
1088   (interactive)
1089   (let ((here (point))
1090         (oldest-first notmuch-search-oldest-first)
1091         (thread (notmuch-search-find-thread-id))
1092         (query notmuch-search-query-string))
1093     (kill-this-buffer)
1094     (notmuch-search query oldest-first)
1095     (goto-char (point-min))
1096     (if (re-search-forward (concat "^" thread) nil t)
1097         (beginning-of-line)
1098       (goto-char here))))
1099
1100 (defun notmuch-search-toggle-order ()
1101   "Toggle the current search order.
1102
1103 By default, the \"inbox\" view created by `notmuch' is displayed
1104 in chronological order (oldest thread at the beginning of the
1105 buffer), while any global searches created by `notmuch-search'
1106 are displayed in reverse-chronological order (newest thread at
1107 the beginning of the buffer).
1108
1109 This command toggles the sort order for the current search.
1110
1111 Note that any filtered searches created by
1112 `notmuch-search-filter' retain the search order of the parent
1113 search."
1114   (interactive)
1115   (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
1116   (notmuch-search-refresh-view))
1117
1118 (defun notmuch-search-filter (query)
1119   "Filter the current search results based on an additional query string.
1120
1121 Runs a new search matching only messages that match both the
1122 current search results AND the additional query string provided."
1123   (interactive "sFilter search: ")
1124   (notmuch-search (concat notmuch-search-query-string " and " query) notmuch-search-oldest-first))
1125
1126 (defun notmuch-search-filter-by-tag (tag)
1127   "Filter the current search results based on a single tag.
1128
1129 Runs a new search matching only messages that match both the
1130 current search results AND that are tagged with the given tag."
1131   (interactive
1132    (list (notmuch-select-tag-with-completion "Filter by tag: ")))
1133   (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
1134
1135 (defun notmuch ()
1136   "Run notmuch to display all mail with tag of 'inbox'"
1137   (interactive)
1138   (notmuch-search "tag:inbox" notmuch-search-oldest-first))
1139
1140 (setq mail-user-agent 'message-user-agent)
1141
1142 (defvar notmuch-folder-mode-map
1143   (let ((map (make-sparse-keymap)))
1144     (define-key map "n" 'next-line)
1145     (define-key map "p" 'previous-line)
1146     (define-key map "x" 'kill-this-buffer)
1147     (define-key map "q" 'kill-this-buffer)
1148     (define-key map "s" 'notmuch-search)
1149     (define-key map (kbd "RET") 'notmuch-folder-show-search)
1150     (define-key map "<" 'beginning-of-buffer)
1151     (define-key map "=" 'notmuch-folder)
1152     (define-key map "?" 'describe-mode)
1153     (define-key map [mouse-1] 'notmuch-folder-show-search)
1154     map)
1155   "Keymap for \"notmuch folder\" buffers.")
1156
1157 (fset 'notmuch-folder-mode-map notmuch-folder-mode-map)
1158
1159 (defcustom notmuch-folders (quote (("inbox" . "tag:inbox") ("unread" . "tag:unread")))
1160   "List of searches for the notmuch folder view"
1161   :type '(alist :key-type (string) :value-type (string))
1162   :group 'notmuch)
1163
1164 (defun notmuch-folder-mode ()
1165   "Major mode for showing notmuch 'folders'.
1166
1167 This buffer contains a list of messages counts returned by a
1168 customizable set of searches of your email archives. Each line
1169 in the buffer shows the search terms and the resulting message count.
1170
1171 Pressing RET on any line opens a search window containing the
1172 results for the search terms in that line.
1173
1174 \\{notmuch-folder-mode-map}"
1175   (interactive)
1176   (kill-all-local-variables)
1177   (use-local-map 'notmuch-folder-mode-map)
1178   (setq truncate-lines t)
1179   (hl-line-mode 1)
1180   (setq major-mode 'notmuch-folder-mode
1181         mode-name "notmuch-folder")
1182   (setq buffer-read-only t))
1183
1184 (defun notmuch-folder-add (folders)
1185   (if folders
1186       (let ((name (car (car folders)))
1187             (inhibit-read-only t)
1188             (search (cdr (car folders))))
1189         (insert name)
1190         (indent-to 16 1)
1191         (call-process notmuch-command nil t nil "count" search)
1192         (notmuch-folder-add (cdr folders)))))
1193
1194 (defun notmuch-folder-find-name ()
1195   (save-excursion
1196     (beginning-of-line)
1197     (let ((beg (point)))
1198       (forward-word)
1199       (filter-buffer-substring beg (point)))))
1200
1201 (defun notmuch-folder-show-search (&optional folder)
1202   "Show a search window for the search related to the specified folder."
1203   (interactive)
1204   (if (null folder)
1205       (setq folder (notmuch-folder-find-name)))
1206   (let ((search (assoc folder notmuch-folders)))
1207     (if search
1208         (notmuch-search (cdr search) notmuch-search-oldest-first))))
1209
1210 (defun notmuch-folder ()
1211   "Show the notmuch folder view and update the displayed counts."
1212   (interactive)
1213   (let ((buffer (get-buffer-create "*notmuch-folders*")))
1214     (switch-to-buffer buffer)
1215     (let ((inhibit-read-only t)
1216           (n (line-number-at-pos)))
1217       (erase-buffer)
1218       (notmuch-folder-mode)
1219       (notmuch-folder-add notmuch-folders)
1220       (goto-char (point-min))
1221       (goto-line n))))
1222
1223 (provide 'notmuch)