]> git.notmuchmail.org Git - notmuch/blob - notmuch.el
notmuch.el: Use message-mode font-face to highlight mail headers
[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 (defun notmuch-fontify-headers ()
631   (progn
632     (if (looking-at "[Tt]o:")
633         (progn
634           (overlay-put (make-overlay (point) (re-search-forward ":"))
635                        'face 'message-header-name)
636           (overlay-put (make-overlay (point) (re-search-forward ".*$"))
637                        'face 'message-header-to))
638     (if (looking-at "[B]?[Cc][Cc]:")
639         (progn
640           (overlay-put (make-overlay (point) (re-search-forward ":"))
641                        'face 'message-header-name)
642           (overlay-put (make-overlay (point) (re-search-forward ".*$"))
643                        'face 'message-header-cc))
644     (if (looking-at "[Ss]ubject:")
645         (progn
646           (overlay-put (make-overlay (point) (re-search-forward ":"))
647                        'face 'message-header-name)
648           (overlay-put (make-overlay (point) (re-search-forward ".*$"))
649                        'face 'message-header-subject))
650     (if (looking-at "[Ff]rom:")
651         (progn
652           (overlay-put (make-overlay (point) (re-search-forward ":"))
653                        'face 'message-header-name)
654           (overlay-put (make-overlay (point) (re-search-forward ".*$"))
655                        'face 'message-header-other))))))))
656
657 (defun notmuch-show-markup-header (depth)
658   (re-search-forward notmuch-show-header-begin-regexp)
659   (forward-line)
660   (let ((beg (point-marker))
661         (btn nil))
662     (end-of-line)
663     ; Inverse video for subject
664     (overlay-put (make-overlay beg (point)) 'face '(:inverse-video t))
665     (setq btn (make-button beg (point) :type 'notmuch-button-body-toggle-type))
666     (forward-line 1)
667     (end-of-line)
668     (let ((beg-hidden (point-marker)))
669       (re-search-forward notmuch-show-header-end-regexp)
670       (beginning-of-line)
671       (let ((end (point-marker)))
672         (goto-char beg)
673         (forward-line)
674         (while (looking-at "[A-Za-z][-A-Za-z0-9]*:")
675           (beginning-of-line)
676           (notmuch-fontify-headers)
677           (forward-line)
678           )
679         (indent-rigidly beg end depth)
680         (let ((invis-spec (make-symbol "notmuch-show-header")))
681           (add-to-invisibility-spec (cons invis-spec t))
682           (overlay-put (make-overlay beg-hidden end)
683                        'invisible invis-spec)
684           (goto-char beg)
685           (forward-line)
686           (make-button (line-beginning-position) (line-end-position)
687                         'invisibility-spec (cons invis-spec t)
688                         :type 'notmuch-button-headers-toggle-type))
689         (goto-char end)
690         (insert "\n")
691         (set-marker beg nil)
692         (set-marker beg-hidden nil)
693         (set-marker end nil)
694         ))
695     btn))
696
697 (defun notmuch-show-markup-message ()
698   (if (re-search-forward notmuch-show-message-begin-regexp nil t)
699       (progn
700         (re-search-forward notmuch-show-depth-regexp)
701         (let ((depth (string-to-number (buffer-substring (match-beginning 1) (match-end 1))))
702               (btn nil))
703           (setq btn (notmuch-show-markup-header depth))
704           (notmuch-show-markup-body depth btn)))
705     (goto-char (point-max))))
706
707 (defun notmuch-show-hide-markers ()
708   (save-excursion
709     (goto-char (point-min))
710     (while (not (eobp))
711       (if (re-search-forward notmuch-show-marker-regexp nil t)
712           (progn
713             (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
714                          'invisible 'notmuch-show-marker))
715         (goto-char (point-max))))))
716
717 (defun notmuch-show-markup-messages ()
718   (save-excursion
719     (goto-char (point-min))
720     (while (not (eobp))
721       (notmuch-show-markup-message)))
722   (notmuch-show-hide-markers))
723
724 ;;;###autoload
725 (defun notmuch-show-mode ()
726   "Major mode for viewing a thread with notmuch.
727
728 This buffer contains the results of the \"notmuch show\" command
729 for displaying a single thread of email from your email archives.
730
731 By default, various components of email messages, (citations,
732 signatures, already-read messages), are invisible to help you
733 focus on the most important things, (new text from unread
734 messages). See the various commands below for toggling the
735 visibility of hidden components.
736
737 The `notmuch-show-next-message' and
738 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
739 default), allow you to navigate to the next and previous
740 messages. Each time you navigate away from a message with
741 `notmuch-show-next-message' the current message will have its
742 \"unread\" tag removed.
743
744 You can add or remove tags from the current message with '+' and
745 '-'.  You can also archive all messages in the current
746 view, (remove the \"inbox\" tag from each), with
747 `notmuch-show-archive-thread' (bound to 'a' by default).
748
749 \\{notmuch-show-mode-map}"
750   (interactive)
751   (kill-all-local-variables)
752   (add-to-invisibility-spec 'notmuch-show-marker)
753   (use-local-map notmuch-show-mode-map)
754   (setq major-mode 'notmuch-show-mode
755         mode-name "notmuch-show")
756   (setq buffer-read-only t))
757
758 ;;;###autoload
759
760 (defgroup notmuch nil
761   "Notmuch mail reader for Emacs."
762   :group 'mail)
763
764 (defcustom notmuch-show-hook nil
765   "List of functions to call when notmuch displays a message."
766   :type 'hook
767   :options '(goto-address)
768   :group 'notmuch)
769
770 (defcustom notmuch-search-hook nil
771   "List of functions to call when notmuch displays the search results."
772   :type 'hook
773   :options '(hl-line-mode)
774   :group 'notmuch)
775
776 ; Make show mode a bit prettier, highlighting URLs and using word wrap
777
778 (defun notmuch-show-pretty-hook ()
779   (goto-address-mode 1)
780   (visual-line-mode))
781
782 (add-hook 'notmuch-show-hook 'notmuch-show-pretty-hook)
783 (add-hook 'notmuch-search-hook
784           (lambda()
785             (hl-line-mode 1) ))
786
787 (defun notmuch-show (thread-id &optional parent-buffer)
788   "Run \"notmuch show\" with the given thread ID and display results.
789
790 The optional PARENT-BUFFER is the notmuch-search buffer from
791 which this notmuch-show command was executed, (so that the next
792 thread from that buffer can be show when done with this one)."
793   (interactive "sNotmuch show: ")
794   (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
795     (switch-to-buffer buffer)
796     (notmuch-show-mode)
797     (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
798     (let ((proc (get-buffer-process (current-buffer)))
799           (inhibit-read-only t))
800       (if proc
801           (error "notmuch search process already running for query `%s'" thread-id)
802         )
803       (erase-buffer)
804       (goto-char (point-min))
805       (save-excursion
806         (call-process notmuch-command nil t nil "show" thread-id)
807         (notmuch-show-markup-messages)
808         )
809       (run-hooks 'notmuch-show-hook)
810       ; Move straight to the first unread message
811       (if (not (notmuch-show-message-unread-p))
812           (progn
813             (notmuch-show-next-unread-message)
814             ; But if there are no unread messages, go back to the
815             ; beginning of the buffer, and open up the bodies of all
816             ; read message.
817             (if (not (notmuch-show-message-unread-p))
818                 (progn
819                   (goto-char (point-min))
820                   (let ((btn (forward-button 1)))
821                     (while btn
822                       (if (button-has-type-p btn 'notmuch-button-body-toggle-type)
823                           (push-button))
824                       (condition-case err
825                           (setq btn (forward-button 1))
826                         (error (setq btn nil)))
827                     ))
828                   (beginning-of-buffer)
829                   ))))
830       )))
831
832 (defvar notmuch-search-authors-width 40
833   "Number of columns to use to display authors in a notmuch-search buffer.")
834
835 (defvar notmuch-search-mode-map
836   (let ((map (make-sparse-keymap)))
837     (define-key map "a" 'notmuch-search-archive-thread)
838     (define-key map "b" 'notmuch-search-scroll-down)
839     (define-key map "f" 'notmuch-search-filter)
840     (define-key map "m" 'message-mail)
841     (define-key map "n" 'next-line)
842     (define-key map "o" 'notmuch-search-toggle-order)
843     (define-key map "p" 'previous-line)
844     (define-key map "q" 'kill-this-buffer)
845     (define-key map "r" 'notmuch-search-reply-to-thread)
846     (define-key map "s" 'notmuch-search)
847     (define-key map "t" 'notmuch-search-filter-by-tag)
848     (define-key map "x" 'kill-this-buffer)
849     (define-key map (kbd "RET") 'notmuch-search-show-thread)
850     (define-key map [mouse-1] 'notmuch-search-show-thread)
851     (define-key map "+" 'notmuch-search-add-tag)
852     (define-key map "-" 'notmuch-search-remove-tag)
853     (define-key map "*" 'notmuch-search-operate-all)
854     (define-key map "<" 'beginning-of-buffer)
855     (define-key map ">" 'notmuch-search-goto-last-thread)
856     (define-key map "=" 'notmuch-search-refresh-view)
857     (define-key map "\M->" 'notmuch-search-goto-last-thread)
858     (define-key map " " 'notmuch-search-scroll-up)
859     (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
860     (define-key map "?" 'describe-mode)
861     map)
862   "Keymap for \"notmuch search\" buffers.")
863 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
864
865 (defvar notmuch-search-query-string)
866 (defvar notmuch-search-oldest-first t
867   "Show the oldest mail first in the search-mode")
868
869
870 (defun notmuch-search-scroll-up ()
871   "Scroll up, moving point to last message in thread if at end."
872   (interactive)
873   (condition-case nil
874       (scroll-up nil)
875     ((end-of-buffer) (notmuch-search-goto-last-thread))))
876
877 (defun notmuch-search-scroll-down ()
878   "Scroll down, moving point to first message in thread if at beginning."
879   (interactive)
880   ; I don't know why scroll-down doesn't signal beginning-of-buffer
881   ; the way that scroll-up signals end-of-buffer, but c'est la vie.
882   ;
883   ; So instead of trapping a signal we instead check whether the
884   ; window begins on the first line of the buffer and if so, move
885   ; directly to that position. (We have to count lines since the
886   ; window-start position is not the same as point-min due to the
887   ; invisible thread-ID characters on the first line.
888   (if (equal (count-lines (point-min) (window-start)) 1)
889       (goto-char (window-start))
890     (scroll-down nil)))
891
892 (defun notmuch-search-goto-last-thread ()
893   "Move point to the last thread in the buffer."
894   (interactive)
895   (goto-char (point-max))
896   (forward-line -1))
897
898 ;;;###autoload
899 (defun notmuch-search-mode ()
900   "Major mode for searching mail with notmuch.
901
902 This buffer contains the results of a \"notmuch search\" of your
903 email archives. Each line in the buffer represents a single
904 thread giving a relative date for the thread and a subject.
905
906 Pressing RET on any line displays that thread. The '+' and '-'
907 keys can be used to add or remove tags from a thread. The 'a' key
908 is a convenience key for archiving a thread (removing the
909 \"inbox\" tag).
910
911 Other useful commands are `notmuch-search-filter' for filtering
912 the current search based on an additional query string,
913 `notmuch-search-filter-by-tag' for filtering to include only
914 messages with a given tag, and `notmuch-search' to execute a new,
915 global search.
916
917 \\{notmuch-search-mode-map}"
918   (interactive)
919   (kill-all-local-variables)
920   (make-local-variable 'notmuch-search-query-string)
921   (make-local-variable 'notmuch-search-oldest-first)
922   (set (make-local-variable 'scroll-preserve-screen-position) t)
923   (add-to-invisibility-spec 'notmuch-search)
924   (use-local-map notmuch-search-mode-map)
925   (setq truncate-lines t)
926   (setq major-mode 'notmuch-search-mode
927         mode-name "notmuch-search")
928   (setq buffer-read-only t))
929
930 (defun notmuch-search-find-thread-id ()
931   "Return the thread for the current thread"
932   (get-text-property (point) 'notmuch-search-thread-id))
933
934 (defun notmuch-search-show-thread ()
935   (interactive)
936   (let ((thread-id (notmuch-search-find-thread-id)))
937     (if (> (length thread-id) 0)
938         (notmuch-show thread-id (current-buffer))
939       (error "End of search results"))))
940
941 (defun notmuch-search-reply-to-thread ()
942   "Begin composing a reply to the entire current thread in a new buffer."
943   (interactive)
944   (let ((message-id (notmuch-search-find-thread-id)))
945     (notmuch-reply message-id)))
946
947 (defun notmuch-call-notmuch-process (&rest args)
948   "Synchronously invoke \"notmuch\" with the given list of arguments.
949
950 Output from the process will be presented to the user as an error
951 and will also appear in a buffer named \"*Notmuch errors*\"."
952   (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
953     (with-current-buffer error-buffer
954         (erase-buffer))
955     (if (eq (apply 'call-process notmuch-command nil error-buffer nil args) 0)
956         (point)
957       (progn
958         (with-current-buffer error-buffer
959           (let ((beg (point-min))
960                 (end (- (point-max) 1)))
961             (error (buffer-substring beg end))
962             ))))))
963
964 (defun notmuch-search-set-tags (tags)
965   (save-excursion
966     (end-of-line)
967     (re-search-backward "(")
968     (forward-char)
969     (let ((beg (point))
970           (inhibit-read-only t))
971       (re-search-forward ")")
972       (backward-char)
973       (let ((end (point)))
974         (delete-region beg end)
975         (insert (mapconcat  'identity tags " "))))))
976
977 (defun notmuch-search-get-tags ()
978   (save-excursion
979     (end-of-line)
980     (re-search-backward "(")
981     (let ((beg (+ (point) 1)))
982       (re-search-forward ")")
983       (let ((end (- (point) 1)))
984         (split-string (buffer-substring beg end))))))
985
986 (defun notmuch-search-add-tag (tag)
987   "Add a tag to messages in the current thread matching the
988 active query."
989   (interactive
990    (list (notmuch-select-tag-with-completion "Tag to add: ")))
991   (notmuch-call-notmuch-process "tag" (concat "+" tag) (notmuch-search-find-thread-id) " and " notmuch-search-query-string)
992   (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
993
994 (defun notmuch-search-remove-tag (tag)
995   "Remove a tag from messages in the current thread matching the
996 active query."
997   (interactive
998    (list (notmuch-select-tag-with-completion "Tag to remove: " (notmuch-search-find-thread-id))))
999   (notmuch-call-notmuch-process "tag" (concat "-" tag) (notmuch-search-find-thread-id) " and " notmuch-search-query-string)
1000   (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
1001
1002 (defun notmuch-search-archive-thread ()
1003   "Archive the current thread (remove its \"inbox\" tag).
1004
1005 This function advances point to the next line when finished."
1006   (interactive)
1007   (notmuch-search-remove-tag "inbox")
1008   (forward-line))
1009
1010 (defun notmuch-search-process-sentinel (proc msg)
1011   "Add a message to let user know when \"notmuch search\" exits"
1012   (let ((buffer (process-buffer proc))
1013         (status (process-status proc))
1014         (exit-status (process-exit-status proc)))
1015     (if (memq status '(exit signal))
1016         (if (buffer-live-p buffer)
1017             (with-current-buffer buffer
1018               (save-excursion
1019                 (let ((inhibit-read-only t))
1020                   (goto-char (point-max))
1021                   (if (eq status 'signal)
1022                       (insert "Incomplete search results (search process was killed).\n"))
1023                   (if (eq status 'exit)
1024                       (progn
1025                         (insert "End of search results.")
1026                         (if (not (= exit-status 0))
1027                             (insert (format " (process returned %d)" exit-status)))
1028                         (insert "\n"))))))))))
1029
1030 (defun notmuch-search-process-filter (proc string)
1031   "Process and filter the output of \"notmuch search\""
1032   (let ((buffer (process-buffer proc)))
1033     (if (buffer-live-p buffer)
1034         (with-current-buffer buffer
1035           (save-excursion
1036             (let ((line 0)
1037                   (more t)
1038                   (inhibit-read-only t))
1039               (while more
1040                 (if (string-match "^\\(thread:[0-9A-Fa-f]*\\) \\(.*\\) \\(\\[[0-9/]*\\]\\) \\([^:]*\\); \\(.*\\) (\\([^()]*\\))$" string line)
1041                     (let* ((thread-id (match-string 1 string))
1042                            (date (match-string 2 string))
1043                            (count (match-string 3 string))
1044                            (authors (match-string 4 string))
1045                            (authors-length (length authors))
1046                            (subject (match-string 5 string))
1047                            (tags (match-string 6 string)))
1048                       (if (> authors-length 40)
1049                           (set 'authors (concat (substring authors 0 (- 40 3)) "...")))
1050                       (goto-char (point-max))
1051                       (let ((beg (point-marker)))
1052                         (insert (format "%s %-7s %-40s %s (%s)\n" date count authors subject tags))
1053                         (put-text-property beg (point-marker) 'notmuch-search-thread-id thread-id))
1054                       (set 'line (match-end 0)))
1055                   (set 'more nil))))))
1056       (delete-process proc))))
1057
1058 (defun notmuch-search-operate-all (action)
1059   "Operate on all messages matching the current query.  Any
1060 number of whitespace separated actions can be given.  Each action
1061 must have one of the two forms
1062
1063   +tagname              Add the tag `tagname'
1064   -tagname              Remove the tag `tagname'
1065
1066 Each character of the tag name may consist of alphanumeric
1067 characters as well as `_.+-'.
1068 "
1069   (interactive "sOperation (+add -drop): notmuch tag ")
1070   (let ((action-split (split-string action " +")))
1071     ;; Perform some validation
1072     (let ((words action-split))
1073       (when (null words) (error "No operation given"))
1074       (while words
1075         (unless (string-match-p "^[\+\-][_\+\-\\w]+$" (car words))
1076           (error "Action must be of the form `+thistag -that_tag'"))
1077         (setq words (cdr words))))
1078     (apply 'notmuch-call-notmuch-process "tag"
1079            (append action-split (list notmuch-search-query-string) nil))))
1080
1081 (defun notmuch-search (query &optional oldest-first)
1082   "Run \"notmuch search\" with the given query string and display results."
1083   (interactive "sNotmuch search: ")
1084   (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
1085     (switch-to-buffer buffer)
1086     (notmuch-search-mode)
1087     (set 'notmuch-search-query-string query)
1088     (set 'notmuch-search-oldest-first oldest-first)
1089     (let ((proc (get-buffer-process (current-buffer)))
1090           (inhibit-read-only t))
1091       (if proc
1092           (error "notmuch search process already running for query `%s'" query)
1093         )
1094       (erase-buffer)
1095       (goto-char (point-min))
1096       (save-excursion
1097         (let ((proc (start-process-shell-command
1098                      "notmuch-search" buffer notmuch-command "search"
1099                      (if oldest-first "--sort=oldest-first" "--sort=newest-first")
1100                      (shell-quote-argument query))))
1101           (set-process-sentinel proc 'notmuch-search-process-sentinel)
1102           (set-process-filter proc 'notmuch-search-process-filter))))
1103     (run-hooks 'notmuch-search-hook)))
1104
1105 (defun notmuch-search-refresh-view ()
1106   "Refresh the current view.
1107
1108 Kills the current buffer and runs a new search with the same
1109 query string as the current search. If the current thread is in
1110 the new search results, then point will be placed on the same
1111 thread. Otherwise, point will be moved to attempt to be in the
1112 same relative position within the new buffer."
1113   (interactive)
1114   (let ((here (point))
1115         (oldest-first notmuch-search-oldest-first)
1116         (thread (notmuch-search-find-thread-id))
1117         (query notmuch-search-query-string))
1118     (kill-this-buffer)
1119     (notmuch-search query oldest-first)
1120     (goto-char (point-min))
1121     (if (re-search-forward (concat "^" thread) nil t)
1122         (beginning-of-line)
1123       (goto-char here))))
1124
1125 (defun notmuch-search-toggle-order ()
1126   "Toggle the current search order.
1127
1128 By default, the \"inbox\" view created by `notmuch' is displayed
1129 in chronological order (oldest thread at the beginning of the
1130 buffer), while any global searches created by `notmuch-search'
1131 are displayed in reverse-chronological order (newest thread at
1132 the beginning of the buffer).
1133
1134 This command toggles the sort order for the current search.
1135
1136 Note that any filtered searches created by
1137 `notmuch-search-filter' retain the search order of the parent
1138 search."
1139   (interactive)
1140   (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
1141   (notmuch-search-refresh-view))
1142
1143 (defun notmuch-search-filter (query)
1144   "Filter the current search results based on an additional query string.
1145
1146 Runs a new search matching only messages that match both the
1147 current search results AND the additional query string provided."
1148   (interactive "sFilter search: ")
1149   (notmuch-search (concat notmuch-search-query-string " and " query) notmuch-search-oldest-first))
1150
1151 (defun notmuch-search-filter-by-tag (tag)
1152   "Filter the current search results based on a single tag.
1153
1154 Runs a new search matching only messages that match both the
1155 current search results AND that are tagged with the given tag."
1156   (interactive
1157    (list (notmuch-select-tag-with-completion "Filter by tag: ")))
1158   (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
1159
1160 (defun notmuch ()
1161   "Run notmuch to display all mail with tag of 'inbox'"
1162   (interactive)
1163   (notmuch-search "tag:inbox" notmuch-search-oldest-first))
1164
1165 (setq mail-user-agent 'message-user-agent)
1166
1167 (defvar notmuch-folder-mode-map
1168   (let ((map (make-sparse-keymap)))
1169     (define-key map "n" 'next-line)
1170     (define-key map "p" 'previous-line)
1171     (define-key map "x" 'kill-this-buffer)
1172     (define-key map "q" 'kill-this-buffer)
1173     (define-key map "s" 'notmuch-search)
1174     (define-key map (kbd "RET") 'notmuch-folder-show-search)
1175     (define-key map "<" 'beginning-of-buffer)
1176     (define-key map "=" 'notmuch-folder)
1177     (define-key map "?" 'describe-mode)
1178     (define-key map [mouse-1] 'notmuch-folder-show-search)
1179     map)
1180   "Keymap for \"notmuch folder\" buffers.")
1181
1182 (fset 'notmuch-folder-mode-map notmuch-folder-mode-map)
1183
1184 (defcustom notmuch-folders (quote (("inbox" . "tag:inbox") ("unread" . "tag:unread")))
1185   "List of searches for the notmuch folder view"
1186   :type '(alist :key-type (string) :value-type (string))
1187   :group 'notmuch)
1188
1189 (defun notmuch-folder-mode ()
1190   "Major mode for showing notmuch 'folders'.
1191
1192 This buffer contains a list of messages counts returned by a
1193 customizable set of searches of your email archives. Each line
1194 in the buffer shows the search terms and the resulting message count.
1195
1196 Pressing RET on any line opens a search window containing the
1197 results for the search terms in that line.
1198
1199 \\{notmuch-folder-mode-map}"
1200   (interactive)
1201   (kill-all-local-variables)
1202   (use-local-map 'notmuch-folder-mode-map)
1203   (setq truncate-lines t)
1204   (hl-line-mode 1)
1205   (setq major-mode 'notmuch-folder-mode
1206         mode-name "notmuch-folder")
1207   (setq buffer-read-only t))
1208
1209 (defun notmuch-folder-add (folders)
1210   (if folders
1211       (let ((name (car (car folders)))
1212             (inhibit-read-only t)
1213             (search (cdr (car folders))))
1214         (insert name)
1215         (indent-to 16 1)
1216         (call-process notmuch-command nil t nil "count" search)
1217         (notmuch-folder-add (cdr folders)))))
1218
1219 (defun notmuch-folder-find-name ()
1220   (save-excursion
1221     (beginning-of-line)
1222     (let ((beg (point)))
1223       (forward-word)
1224       (filter-buffer-substring beg (point)))))
1225
1226 (defun notmuch-folder-show-search (&optional folder)
1227   "Show a search window for the search related to the specified folder."
1228   (interactive)
1229   (if (null folder)
1230       (setq folder (notmuch-folder-find-name)))
1231   (let ((search (assoc folder notmuch-folders)))
1232     (if search
1233         (notmuch-search (cdr search) notmuch-search-oldest-first))))
1234
1235 (defun notmuch-folder ()
1236   "Show the notmuch folder view and update the displayed counts."
1237   (interactive)
1238   (let ((buffer (get-buffer-create "*notmuch-folders*")))
1239     (switch-to-buffer buffer)
1240     (let ((inhibit-read-only t)
1241           (n (line-number-at-pos)))
1242       (erase-buffer)
1243       (notmuch-folder-mode)
1244       (notmuch-folder-add notmuch-folders)
1245       (goto-char (point-min))
1246       (goto-line n))))
1247
1248 (provide 'notmuch)