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