]> git.notmuchmail.org Git - notmuch/blob - notmuch.el
Factor out message buffer mgmt from notmuch-show-view-all-mime-parts
[notmuch] / notmuch.el
1 ; notmuch.el --- run notmuch within emacs
2 ;
3 ; Copyright © Carl Worth
4 ;
5 ; This file is part of Notmuch.
6 ;
7 ; Notmuch is free software: you can redistribute it and/or modify it
8 ; under the terms of the GNU General Public License as published by
9 ; the Free Software Foundation, either version 3 of the License, or
10 ; (at your option) any later version.
11 ;
12 ; Notmuch is distributed in the hope that it will be useful, but
13 ; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 ; General Public License for more details.
16 ;
17 ; You should have received a copy of the GNU General Public License
18 ; along with Notmuch.  If not, see <http://www.gnu.org/licenses/>.
19 ;
20 ; Authors: Carl Worth <cworth@cworth.org>
21
22 ; This is an emacs-based interface to the notmuch mail system.
23 ;
24 ; You will first need to have the notmuch program installed and have a
25 ; notmuch database built in order to use this. See
26 ; http://notmuchmail.org for details.
27 ;
28 ; To install this software, copy it to a directory that is on the
29 ; `load-path' variable within emacs (a good candidate is
30 ; /usr/local/share/emacs/site-lisp). If you are viewing this from the
31 ; notmuch source distribution then you can simply run:
32 ;
33 ;       sudo make install-emacs
34 ;
35 ; to install it.
36 ;
37 ; Then, to actually run it, add:
38 ;
39 ;       (require 'notmuch)
40 ;
41 ; to your ~/.emacs file, and then run "M-x notmuch" from within emacs,
42 ; or run:
43 ;
44 ;       emacs -f notmuch
45 ;
46 ; Have fun, and let us know if you have any comment, questions, or
47 ; kudos: Notmuch list <notmuch@notmuchmail.org> (subscription is not
48 ; required, but is available from http://notmuchmail.org).
49
50 (require 'cl)
51 (require 'mm-view)
52 (require 'message)
53
54 (defvar notmuch-show-mode-map
55   (let ((map (make-sparse-keymap)))
56     ; I don't actually want all of these toggle commands occupying
57     ; keybindings. They steal valuable key-binding space, are hard
58     ; to remember, and act globally rather than locally.
59     ;
60     ; Will be much preferable to switch to direct manipulation for
61     ; toggling visibility of these components. Probably using
62     ; overlays-at to query and manipulate the current overlay.
63     (define-key map "a" 'notmuch-show-archive-thread)
64     (define-key map "A" 'notmuch-show-mark-read-then-archive-thread)
65     (define-key map "m" 'message-mail)
66     (define-key map "n" 'notmuch-show-next-message)
67     (define-key map "N" 'notmuch-show-mark-read-then-next-open-message)
68     (define-key map "p" 'notmuch-show-previous-message)
69     (define-key map (kbd "C-n") 'notmuch-show-next-line)
70     (define-key map (kbd "C-p") 'notmuch-show-previous-line)
71     (define-key map "q" 'kill-this-buffer)
72     (define-key map "r" 'notmuch-show-reply)
73     (define-key map "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 (defmacro with-current-notmuch-show-message (&rest body)
287   "Evaluate body with current buffer set to the text of current message"
288   `(save-excursion
289      (let ((filename (notmuch-show-get-filename)))
290        (let ((buf (generate-new-buffer (concat "*notmuch-msg-" filename "*"))))
291          (with-current-buffer buf
292            (insert-file-contents filename nil nil nil t)
293            ,@body)
294         (kill-buffer buf)))))
295
296 (defun notmuch-show-view-all-mime-parts ()
297   "Use external viewers (according to mailcap) to view all MIME-encoded parts."
298   (interactive)
299   (with-current-notmuch-show-message
300    (mm-display-parts (mm-dissect-buffer))))
301
302 (defun notmuch-reply (query-string)
303   (switch-to-buffer (generate-new-buffer "notmuch-draft"))
304   (call-process notmuch-command nil t nil "reply" query-string)
305   (message-insert-signature)
306   (goto-char (point-min))
307   (if (re-search-forward "^$" nil t)
308       (progn
309         (insert "--text follows this line--")
310         (forward-line)))
311   (message-mode))
312
313 (defun notmuch-show-reply ()
314   "Begin composing a reply to the current message in a new buffer."
315   (interactive)
316   (let ((message-id (notmuch-show-get-message-id)))
317     (notmuch-reply message-id)))
318
319 (defun notmuch-show-pipe-message (command)
320   "Pipe the contents of the current message to the given command.
321
322 The given command will be executed with the raw contents of the
323 current email message as stdin. Anything printed by the command
324 to stdout or stderr will appear in the *Messages* buffer."
325   (interactive "sPipe message to command: ")
326   (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*"
327          (list command " < " (shell-quote-argument (notmuch-show-get-filename)))))
328
329 (defun notmuch-show-move-to-current-message-summary-line ()
330   "Move to the beginning of the one-line summary of the current message.
331
332 This gives us a stable place to move to and work from since the
333 summary line is always visible. This is important since moving to
334 an invisible location is unreliable, (the main command loop moves
335 point either forward or backward to the next visible character
336 when a command ends with point on an invisible character).
337
338 Emits an error if point is not within a valid message, (that is
339 not pattern of `notmuch-show-message-begin-regexp' could be found
340 by searching backward)."
341   (beginning-of-line)
342   (if (not (looking-at notmuch-show-message-begin-regexp))
343       (if (re-search-backward notmuch-show-message-begin-regexp nil t)
344           (forward-line 2)
345         (error "Not within a valid message."))
346     (forward-line 2)))
347
348 (defun notmuch-show-last-message-p ()
349   "Predicate testing whether point is within the last message."
350   (save-window-excursion
351     (save-excursion
352       (notmuch-show-move-to-current-message-summary-line)
353       (not (re-search-forward notmuch-show-message-begin-regexp nil t)))))
354
355 (defun notmuch-show-message-unread-p ()
356   "Preficate testing whether current message is unread."
357   (member "unread" (notmuch-show-get-tags)))
358
359 (defun notmuch-show-next-message ()
360   "Advance to the beginning of the next message in the buffer.
361
362 Moves to the last visible character of the current message if
363 already on the last message in the buffer."
364   (interactive)
365   (notmuch-show-move-to-current-message-summary-line)
366   (if (re-search-forward notmuch-show-message-begin-regexp nil t)
367       (notmuch-show-move-to-current-message-summary-line)
368     (goto-char (- (point-max) 1))
369     (while (point-invisible-p)
370       (backward-char)))
371   (recenter 0))
372
373 (defun notmuch-show-find-next-message ()
374   "Returns the position of the next message in the buffer.
375
376 Or the position of the last visible character of the current
377 message if already within the last message in the buffer."
378   ; save-excursion doesn't save our window position
379   ; save-window-excursion doesn't save point
380   ; Looks like we have to use both.
381   (save-excursion
382     (save-window-excursion
383       (notmuch-show-next-message)
384       (point))))
385
386 (defun notmuch-show-next-unread-message ()
387   "Advance to the beginning of the next unread message in the buffer.
388
389 Moves to the last visible character of the current message if
390 there are no more unread messages past the current point."
391   (notmuch-show-next-message)
392   (while (and (not (notmuch-show-last-message-p))
393               (not (notmuch-show-message-unread-p)))
394     (notmuch-show-next-message))
395   (if (not (notmuch-show-message-unread-p))
396       (notmuch-show-next-message)))
397
398 (defun notmuch-show-next-open-message ()
399   "Advance to the next message which is not hidden.
400
401 If read messages are currently hidden, advance to the next unread
402 message. Otherwise, advance to the next message."
403   (if (or (memq 'notmuch-show-body-read buffer-invisibility-spec)
404           (assq 'notmuch-show-body-read buffer-invisibility-spec))
405       (notmuch-show-next-unread-message)
406     (notmuch-show-next-message)))
407
408 (defun notmuch-show-previous-message ()
409   "Backup to the beginning of the previous message in the buffer.
410
411 If within a message rather than at the beginning of it, then
412 simply move to the beginning of the current message."
413   (interactive)
414   (let ((start (point)))
415     (notmuch-show-move-to-current-message-summary-line)
416     (if (not (< (point) start))
417         ; Go backward twice to skip the current message's marker
418         (progn
419           (re-search-backward notmuch-show-message-begin-regexp nil t)
420           (re-search-backward notmuch-show-message-begin-regexp nil t)
421           (notmuch-show-move-to-current-message-summary-line)
422           ))
423     (recenter 0)))
424
425 (defun notmuch-show-find-previous-message ()
426   "Returns the position of the previous message in the buffer.
427
428 Or the position of the beginning of the current message if point
429 is originally within the message rather than at the beginning of
430 it."
431   ; save-excursion doesn't save our window position
432   ; save-window-excursion doesn't save point
433   ; Looks like we have to use both.
434   (save-excursion
435     (save-window-excursion
436       (notmuch-show-previous-message)
437       (point))))
438
439 (defun notmuch-show-mark-read-then-next-open-message ()
440   "Remove unread tag from current message, then advance to next unread message."
441   (interactive)
442   (notmuch-show-remove-tag "unread")
443   (notmuch-show-next-open-message))
444
445 (defun notmuch-show-rewind ()
446   "Do reverse scrolling compared to `notmuch-show-advance-marking-read-and-archiving'
447
448 Specifically, if the beginning of the previous email is fewer
449 than `window-height' lines from the current point, move to it
450 just like `notmuch-show-previous-message'.
451
452 Otherwise, just scroll down a screenful of the current message.
453
454 This command does not modify any message tags, (it does not undo
455 any effects from previous calls to
456 `notmuch-show-advance-marking-read-and-archiving'."
457   (interactive)
458   (let ((previous (notmuch-show-find-previous-message)))
459     (if (> (count-lines previous (point)) (- (window-height) next-screen-context-lines))
460         (progn
461           (condition-case nil
462               (scroll-down nil)
463             ((beginning-of-buffer) nil))
464           (goto-char (window-start)))
465       (notmuch-show-previous-message))))
466
467 (defun notmuch-show-advance-marking-read-and-archiving ()
468   "Advance through buffer, marking read and archiving.
469
470 This command is intended to be one of the simplest ways to
471 process a thread of email. It does the following:
472
473 If the current message in the thread is not yet fully visible,
474 scroll by a near screenful to read more of the message.
475
476 Otherwise, (the end of the current message is already within the
477 current window), remove the \"unread\" tag (if present) from the
478 current message and advance to the next open message.
479
480 Finally, if there is no further message to advance to, and this
481 last message is already read, then archive the entire current
482 thread, (remove the \"inbox\" tag from each message). Also kill
483 this buffer, and display the next thread from the search from
484 which this thread was originally shown."
485   (interactive)
486   (let ((next (notmuch-show-find-next-message))
487         (unread (notmuch-show-message-unread-p)))
488     (if (> next (window-end))
489         (scroll-up nil)
490       (let ((last (notmuch-show-last-message-p)))
491         (notmuch-show-mark-read-then-next-open-message)
492         (if last
493             (notmuch-show-archive-thread))))))
494
495 (defun notmuch-show-next-button ()
496   "Advance point to the next button in the buffer."
497   (interactive)
498   (goto-char (button-start (next-button (point)))))
499
500 (defun notmuch-show-previous-button ()
501   "Move point back to the previous button in the buffer."
502   (interactive)
503   (goto-char (button-start (previous-button (point)))))
504
505 (defun notmuch-toggle-invisible-action (cite-button)
506   (let ((invis-spec (button-get button 'invisibility-spec)))
507         (if (invisible-p invis-spec)
508             (remove-from-invisibility-spec invis-spec)
509           (add-to-invisibility-spec invis-spec)
510           ))
511   (force-window-update)
512   (redisplay t))
513
514 (define-button-type 'notmuch-button-invisibility-toggle-type 'action 'notmuch-toggle-invisible-action 'follow-link t)
515 (define-button-type 'notmuch-button-citation-toggle-type 'help-echo "mouse-1, RET: Show citation"
516   :supertype 'notmuch-button-invisibility-toggle-type)
517 (define-button-type 'notmuch-button-signature-toggle-type 'help-echo "mouse-1, RET: Show signature"
518   :supertype 'notmuch-button-invisibility-toggle-type)
519 (define-button-type 'notmuch-button-headers-toggle-type 'help-echo "mouse-1, RET: Show headers"
520   :supertype 'notmuch-button-invisibility-toggle-type)
521 (define-button-type 'notmuch-button-body-toggle-type 'help-echo "mouse-1, RET: Show message"
522   :supertype 'notmuch-button-invisibility-toggle-type)
523
524 (defun notmuch-show-markup-citations-region (beg end depth)
525   (goto-char beg)
526   (beginning-of-line)
527   (while (< (point) end)
528     (let ((beg-sub (point-marker))
529           (indent (make-string depth ? ))
530           (citation "[[:space:]]*>"))
531       (if (looking-at citation)
532           (progn
533             (while (looking-at citation)
534               (forward-line))
535             (let ((overlay (make-overlay beg-sub (point)))
536                   (invis-spec (make-symbol "notmuch-citation-region")))
537               (add-to-invisibility-spec invis-spec)
538               (overlay-put overlay 'invisible invis-spec)
539               (let ((p (point))
540                     (cite-button-text
541                      (concat "["  (number-to-string (count-lines beg-sub (point)))
542                              "-line citation.]")))
543                 (goto-char (- beg-sub 1))
544                 (insert (concat "\n" indent))
545                 (insert-button cite-button-text
546                                'invisibility-spec invis-spec
547                                :type 'notmuch-button-citation-toggle-type)
548                 (insert "\n")
549                 (goto-char (+ (length cite-button-text) p))
550               ))))
551       (move-to-column depth)
552       (if (looking-at notmuch-show-signature-regexp)
553           (let ((sig-lines (- (count-lines beg-sub end) 1)))
554             (if (<= sig-lines notmuch-show-signature-lines-max)
555                 (progn
556                   (let ((invis-spec (make-symbol "notmuch-signature-region")))
557                     (add-to-invisibility-spec invis-spec)
558                     (overlay-put (make-overlay beg-sub end)
559                                  'invisible invis-spec)
560                   
561                     (goto-char (- beg-sub 1))
562                     (insert (concat "\n" indent))
563                     (let ((sig-button-text (concat "[" (number-to-string sig-lines)
564                                                    "-line signature.]")))
565                       (insert-button sig-button-text 'invisibility-spec invis-spec
566                                      :type 'notmuch-button-signature-toggle-type)
567                      )
568                     (insert "\n")
569                     (goto-char end))))))
570       (forward-line))))
571
572 (defun notmuch-show-markup-part (beg end depth mime-message)
573   (if (re-search-forward notmuch-show-part-begin-regexp nil t)
574       (progn
575         (if (eq mime-message nil)
576             (let ((filename (notmuch-show-get-filename)))
577               (with-temp-buffer
578                 (insert-file-contents filename nil nil nil t)
579                 (setq mime-message (mm-dissect-buffer)))))
580         (forward-line)
581         (let ((part-beg (point-marker)))
582           (re-search-forward notmuch-show-part-end-regexp)
583
584           (let ((part-end (copy-marker (match-beginning 0))))
585             (goto-char part-end)
586             (if (not (bolp))
587                 (insert "\n"))
588             (indent-rigidly part-beg part-end depth)
589             (save-excursion
590               (goto-char part-beg)
591               (forward-line -1)
592               (beginning-of-line)
593               (let ((handle-type (mm-handle-type mime-message))
594                     mime-type)
595                 (if (sequencep (car handle-type))
596                     (setq mime-type (car handle-type))
597                   (setq mime-type (car (car (cdr handle-type))))
598                   )
599                 (if (equal mime-type "text/html")
600                     (mm-display-part mime-message))))
601
602             (notmuch-show-markup-citations-region part-beg part-end depth)
603             ; Advance to the next part (if any) (so the outer loop can
604             ; determine whether we've left the current message.
605             (if (re-search-forward notmuch-show-part-begin-regexp nil t)
606                 (beginning-of-line)))))
607     (goto-char end))
608   mime-message)
609
610 (defun notmuch-show-markup-parts-region (beg end depth)
611   (save-excursion
612     (goto-char beg)
613     (let (mime-message)
614       (while (< (point) end)
615         (setq mime-message
616               (notmuch-show-markup-part
617                beg end depth mime-message))))))
618
619 (defun notmuch-show-markup-body (depth btn)
620   (re-search-forward notmuch-show-body-begin-regexp)
621   (forward-line)
622   (let ((beg (point-marker)))
623     (re-search-forward notmuch-show-body-end-regexp)
624     (let ((end (copy-marker (match-beginning 0))))
625       (notmuch-show-markup-parts-region beg end depth)
626       (let ((invis-spec (make-symbol "notmuch-show-body-read")))
627         (overlay-put (make-overlay beg end)
628                      'invisible invis-spec)
629         (button-put btn 'invisibility-spec invis-spec)
630         (if (not (notmuch-show-message-unread-p))
631             (add-to-invisibility-spec invis-spec)))
632       (set-marker beg nil)
633       (set-marker end nil)
634       )))
635 (defun notmuch-fontify-headers ()
636   (progn
637     (if (looking-at "[Tt]o:")
638         (progn
639           (overlay-put (make-overlay (point) (re-search-forward ":"))
640                        'face 'message-header-name)
641           (overlay-put (make-overlay (point) (re-search-forward ".*$"))
642                        'face 'message-header-to))
643     (if (looking-at "[B]?[Cc][Cc]:")
644         (progn
645           (overlay-put (make-overlay (point) (re-search-forward ":"))
646                        'face 'message-header-name)
647           (overlay-put (make-overlay (point) (re-search-forward ".*$"))
648                        'face 'message-header-cc))
649     (if (looking-at "[Ss]ubject:")
650         (progn
651           (overlay-put (make-overlay (point) (re-search-forward ":"))
652                        'face 'message-header-name)
653           (overlay-put (make-overlay (point) (re-search-forward ".*$"))
654                        'face 'message-header-subject))
655     (if (looking-at "[Ff]rom:")
656         (progn
657           (overlay-put (make-overlay (point) (re-search-forward ":"))
658                        'face 'message-header-name)
659           (overlay-put (make-overlay (point) (re-search-forward ".*$"))
660                        'face 'message-header-other))))))))
661
662 (defun notmuch-show-markup-header (depth)
663   (re-search-forward notmuch-show-header-begin-regexp)
664   (forward-line)
665   (let ((beg (point-marker))
666         (btn nil))
667     (end-of-line)
668     ; Inverse video for subject
669     (overlay-put (make-overlay beg (point)) 'face '(:inverse-video t))
670     (setq btn (make-button beg (point) :type 'notmuch-button-body-toggle-type))
671     (forward-line 1)
672     (end-of-line)
673     (let ((beg-hidden (point-marker)))
674       (re-search-forward notmuch-show-header-end-regexp)
675       (beginning-of-line)
676       (let ((end (point-marker)))
677         (goto-char beg)
678         (forward-line)
679         (while (looking-at "[A-Za-z][-A-Za-z0-9]*:")
680           (beginning-of-line)
681           (notmuch-fontify-headers)
682           (forward-line)
683           )
684         (indent-rigidly beg end depth)
685         (let ((invis-spec (make-symbol "notmuch-show-header")))
686           (add-to-invisibility-spec (cons invis-spec t))
687           (overlay-put (make-overlay beg-hidden end)
688                        'invisible invis-spec)
689           (goto-char beg)
690           (forward-line)
691           (make-button (line-beginning-position) (line-end-position)
692                         'invisibility-spec (cons invis-spec t)
693                         :type 'notmuch-button-headers-toggle-type))
694         (goto-char end)
695         (insert "\n")
696         (set-marker beg nil)
697         (set-marker beg-hidden nil)
698         (set-marker end nil)
699         ))
700     btn))
701
702 (defun notmuch-show-markup-message ()
703   (if (re-search-forward notmuch-show-message-begin-regexp nil t)
704       (progn
705         (re-search-forward notmuch-show-depth-regexp)
706         (let ((depth (string-to-number (buffer-substring (match-beginning 1) (match-end 1))))
707               (btn nil))
708           (setq btn (notmuch-show-markup-header depth))
709           (notmuch-show-markup-body depth btn)))
710     (goto-char (point-max))))
711
712 (defun notmuch-show-hide-markers ()
713   (save-excursion
714     (goto-char (point-min))
715     (while (not (eobp))
716       (if (re-search-forward notmuch-show-marker-regexp nil t)
717           (progn
718             (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
719                          'invisible 'notmuch-show-marker))
720         (goto-char (point-max))))))
721
722 (defun notmuch-show-markup-messages ()
723   (save-excursion
724     (goto-char (point-min))
725     (while (not (eobp))
726       (notmuch-show-markup-message)))
727   (notmuch-show-hide-markers))
728
729 ;;;###autoload
730 (defun notmuch-show-mode ()
731   "Major mode for viewing a thread with notmuch.
732
733 This buffer contains the results of the \"notmuch show\" command
734 for displaying a single thread of email from your email archives.
735
736 By default, various components of email messages, (citations,
737 signatures, already-read messages), are invisible to help you
738 focus on the most important things, (new text from unread
739 messages). See the various commands below for toggling the
740 visibility of hidden components.
741
742 The `notmuch-show-next-message' and
743 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
744 default), allow you to navigate to the next and previous
745 messages. Each time you navigate away from a message with
746 `notmuch-show-next-message' the current message will have its
747 \"unread\" tag removed.
748
749 You can add or remove tags from the current message with '+' and
750 '-'.  You can also archive all messages in the current
751 view, (remove the \"inbox\" tag from each), with
752 `notmuch-show-archive-thread' (bound to 'a' by default).
753
754 \\{notmuch-show-mode-map}"
755   (interactive)
756   (kill-all-local-variables)
757   (add-to-invisibility-spec 'notmuch-show-marker)
758   (use-local-map notmuch-show-mode-map)
759   (setq major-mode 'notmuch-show-mode
760         mode-name "notmuch-show")
761   (setq buffer-read-only t))
762
763 ;;;###autoload
764
765 (defgroup notmuch nil
766   "Notmuch mail reader for Emacs."
767   :group 'mail)
768
769 (defcustom notmuch-show-hook nil
770   "List of functions to call when notmuch displays a message."
771   :type 'hook
772   :options '(goto-address)
773   :group 'notmuch)
774
775 (defcustom notmuch-search-hook nil
776   "List of functions to call when notmuch displays the search results."
777   :type 'hook
778   :options '(hl-line-mode)
779   :group 'notmuch)
780
781 ; Make show mode a bit prettier, highlighting URLs and using word wrap
782
783 (defun notmuch-show-pretty-hook ()
784   (goto-address-mode 1)
785   (visual-line-mode))
786
787 (add-hook 'notmuch-show-hook 'notmuch-show-pretty-hook)
788 (add-hook 'notmuch-search-hook
789           (lambda()
790             (hl-line-mode 1) ))
791
792 (defun notmuch-show (thread-id &optional parent-buffer)
793   "Run \"notmuch show\" with the given thread ID and display results.
794
795 The optional PARENT-BUFFER is the notmuch-search buffer from
796 which this notmuch-show command was executed, (so that the next
797 thread from that buffer can be show when done with this one)."
798   (interactive "sNotmuch show: ")
799   (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
800     (switch-to-buffer buffer)
801     (notmuch-show-mode)
802     (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
803     (let ((proc (get-buffer-process (current-buffer)))
804           (inhibit-read-only t))
805       (if proc
806           (error "notmuch search process already running for query `%s'" thread-id)
807         )
808       (erase-buffer)
809       (goto-char (point-min))
810       (save-excursion
811         (call-process notmuch-command nil t nil "show" thread-id)
812         (notmuch-show-markup-messages)
813         )
814       (run-hooks 'notmuch-show-hook)
815       ; Move straight to the first unread message
816       (if (not (notmuch-show-message-unread-p))
817           (progn
818             (notmuch-show-next-unread-message)
819             ; But if there are no unread messages, go back to the
820             ; beginning of the buffer, and open up the bodies of all
821             ; read message.
822             (if (not (notmuch-show-message-unread-p))
823                 (progn
824                   (goto-char (point-min))
825                   (let ((btn (forward-button 1)))
826                     (while btn
827                       (if (button-has-type-p btn 'notmuch-button-body-toggle-type)
828                           (push-button))
829                       (condition-case err
830                           (setq btn (forward-button 1))
831                         (error (setq btn nil)))
832                     ))
833                   (beginning-of-buffer)
834                   ))))
835       )))
836
837 (defvar notmuch-search-authors-width 40
838   "Number of columns to use to display authors in a notmuch-search buffer.")
839
840 (defvar notmuch-search-mode-map
841   (let ((map (make-sparse-keymap)))
842     (define-key map "a" 'notmuch-search-archive-thread)
843     (define-key map "b" 'notmuch-search-scroll-down)
844     (define-key map "f" 'notmuch-search-filter)
845     (define-key map "m" 'message-mail)
846     (define-key map "n" 'next-line)
847     (define-key map "o" 'notmuch-search-toggle-order)
848     (define-key map "p" 'previous-line)
849     (define-key map "q" 'kill-this-buffer)
850     (define-key map "r" 'notmuch-search-reply-to-thread)
851     (define-key map "s" 'notmuch-search)
852     (define-key map "t" 'notmuch-search-filter-by-tag)
853     (define-key map "x" 'kill-this-buffer)
854     (define-key map (kbd "RET") 'notmuch-search-show-thread)
855     (define-key map [mouse-1] 'notmuch-search-show-thread)
856     (define-key map "+" 'notmuch-search-add-tag)
857     (define-key map "-" 'notmuch-search-remove-tag)
858     (define-key map "*" 'notmuch-search-operate-all)
859     (define-key map "<" 'beginning-of-buffer)
860     (define-key map ">" 'notmuch-search-goto-last-thread)
861     (define-key map "=" 'notmuch-search-refresh-view)
862     (define-key map "\M->" 'notmuch-search-goto-last-thread)
863     (define-key map " " 'notmuch-search-scroll-up)
864     (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
865     (define-key map "?" 'describe-mode)
866     map)
867   "Keymap for \"notmuch search\" buffers.")
868 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
869
870 (defvar notmuch-search-query-string)
871 (defvar notmuch-search-oldest-first t
872   "Show the oldest mail first in the search-mode")
873
874
875 (defun notmuch-search-scroll-up ()
876   "Scroll up, moving point to last message in thread if at end."
877   (interactive)
878   (condition-case nil
879       (scroll-up nil)
880     ((end-of-buffer) (notmuch-search-goto-last-thread))))
881
882 (defun notmuch-search-scroll-down ()
883   "Scroll down, moving point to first message in thread if at beginning."
884   (interactive)
885   ; I don't know why scroll-down doesn't signal beginning-of-buffer
886   ; the way that scroll-up signals end-of-buffer, but c'est la vie.
887   ;
888   ; So instead of trapping a signal we instead check whether the
889   ; window begins on the first line of the buffer and if so, move
890   ; directly to that position. (We have to count lines since the
891   ; window-start position is not the same as point-min due to the
892   ; invisible thread-ID characters on the first line.
893   (if (equal (count-lines (point-min) (window-start)) 1)
894       (goto-char (window-start))
895     (scroll-down nil)))
896
897 (defun notmuch-search-goto-last-thread ()
898   "Move point to the last thread in the buffer."
899   (interactive)
900   (goto-char (point-max))
901   (forward-line -1))
902
903 ;;;###autoload
904 (defun notmuch-search-mode ()
905   "Major mode for searching mail with notmuch.
906
907 This buffer contains the results of a \"notmuch search\" of your
908 email archives. Each line in the buffer represents a single
909 thread giving a relative date for the thread and a subject.
910
911 Pressing RET on any line displays that thread. The '+' and '-'
912 keys can be used to add or remove tags from a thread. The 'a' key
913 is a convenience key for archiving a thread (removing the
914 \"inbox\" tag).
915
916 Other useful commands are `notmuch-search-filter' for filtering
917 the current search based on an additional query string,
918 `notmuch-search-filter-by-tag' for filtering to include only
919 messages with a given tag, and `notmuch-search' to execute a new,
920 global search.
921
922 \\{notmuch-search-mode-map}"
923   (interactive)
924   (kill-all-local-variables)
925   (make-local-variable 'notmuch-search-query-string)
926   (make-local-variable 'notmuch-search-oldest-first)
927   (set (make-local-variable 'scroll-preserve-screen-position) t)
928   (add-to-invisibility-spec 'notmuch-search)
929   (use-local-map notmuch-search-mode-map)
930   (setq truncate-lines t)
931   (setq major-mode 'notmuch-search-mode
932         mode-name "notmuch-search")
933   (setq buffer-read-only t))
934
935 (defun notmuch-search-find-thread-id ()
936   "Return the thread for the current thread"
937   (get-text-property (point) 'notmuch-search-thread-id))
938
939 (defun notmuch-search-show-thread ()
940   (interactive)
941   (let ((thread-id (notmuch-search-find-thread-id)))
942     (if (> (length thread-id) 0)
943         (notmuch-show thread-id (current-buffer))
944       (error "End of search results"))))
945
946 (defun notmuch-search-reply-to-thread ()
947   "Begin composing a reply to the entire current thread in a new buffer."
948   (interactive)
949   (let ((message-id (notmuch-search-find-thread-id)))
950     (notmuch-reply message-id)))
951
952 (defun notmuch-call-notmuch-process (&rest args)
953   "Synchronously invoke \"notmuch\" with the given list of arguments.
954
955 Output from the process will be presented to the user as an error
956 and will also appear in a buffer named \"*Notmuch errors*\"."
957   (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
958     (with-current-buffer error-buffer
959         (erase-buffer))
960     (if (eq (apply 'call-process notmuch-command nil error-buffer nil args) 0)
961         (point)
962       (progn
963         (with-current-buffer error-buffer
964           (let ((beg (point-min))
965                 (end (- (point-max) 1)))
966             (error (buffer-substring beg end))
967             ))))))
968
969 (defun notmuch-search-set-tags (tags)
970   (save-excursion
971     (end-of-line)
972     (re-search-backward "(")
973     (forward-char)
974     (let ((beg (point))
975           (inhibit-read-only t))
976       (re-search-forward ")")
977       (backward-char)
978       (let ((end (point)))
979         (delete-region beg end)
980         (insert (mapconcat  'identity tags " "))))))
981
982 (defun notmuch-search-get-tags ()
983   (save-excursion
984     (end-of-line)
985     (re-search-backward "(")
986     (let ((beg (+ (point) 1)))
987       (re-search-forward ")")
988       (let ((end (- (point) 1)))
989         (split-string (buffer-substring beg end))))))
990
991 (defun notmuch-search-add-tag (tag)
992   "Add a tag to messages in the current thread matching the
993 active query."
994   (interactive
995    (list (notmuch-select-tag-with-completion "Tag to add: ")))
996   (notmuch-call-notmuch-process "tag" (concat "+" tag) (notmuch-search-find-thread-id) " and " notmuch-search-query-string)
997   (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
998
999 (defun notmuch-search-remove-tag (tag)
1000   "Remove a tag from messages in the current thread matching the
1001 active query."
1002   (interactive
1003    (list (notmuch-select-tag-with-completion "Tag to remove: " (notmuch-search-find-thread-id))))
1004   (notmuch-call-notmuch-process "tag" (concat "-" tag) (notmuch-search-find-thread-id) " and " notmuch-search-query-string)
1005   (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
1006
1007 (defun notmuch-search-archive-thread ()
1008   "Archive the current thread (remove its \"inbox\" tag).
1009
1010 This function advances point to the next line when finished."
1011   (interactive)
1012   (notmuch-search-remove-tag "inbox")
1013   (forward-line))
1014
1015 (defun notmuch-search-process-sentinel (proc msg)
1016   "Add a message to let user know when \"notmuch search\" exits"
1017   (let ((buffer (process-buffer proc))
1018         (status (process-status proc))
1019         (exit-status (process-exit-status proc)))
1020     (if (memq status '(exit signal))
1021         (if (buffer-live-p buffer)
1022             (with-current-buffer buffer
1023               (save-excursion
1024                 (let ((inhibit-read-only t))
1025                   (goto-char (point-max))
1026                   (if (eq status 'signal)
1027                       (insert "Incomplete search results (search process was killed).\n"))
1028                   (if (eq status 'exit)
1029                       (progn
1030                         (insert "End of search results.")
1031                         (if (not (= exit-status 0))
1032                             (insert (format " (process returned %d)" exit-status)))
1033                         (insert "\n"))))))))))
1034
1035 (defun notmuch-search-process-filter (proc string)
1036   "Process and filter the output of \"notmuch search\""
1037   (let ((buffer (process-buffer proc)))
1038     (if (buffer-live-p buffer)
1039         (with-current-buffer buffer
1040           (save-excursion
1041             (let ((line 0)
1042                   (more t)
1043                   (inhibit-read-only t))
1044               (while more
1045                 (if (string-match "^\\(thread:[0-9A-Fa-f]*\\) \\(.*\\) \\(\\[[0-9/]*\\]\\) \\([^:]*\\); \\(.*\\) (\\([^()]*\\))$" string line)
1046                     (let* ((thread-id (match-string 1 string))
1047                            (date (match-string 2 string))
1048                            (count (match-string 3 string))
1049                            (authors (match-string 4 string))
1050                            (authors-length (length authors))
1051                            (subject (match-string 5 string))
1052                            (tags (match-string 6 string)))
1053                       (if (> authors-length 40)
1054                           (set 'authors (concat (substring authors 0 (- 40 3)) "...")))
1055                       (goto-char (point-max))
1056                       (let ((beg (point-marker)))
1057                         (insert (format "%s %-7s %-40s %s (%s)\n" date count authors subject tags))
1058                         (put-text-property beg (point-marker) 'notmuch-search-thread-id thread-id))
1059                       (set 'line (match-end 0)))
1060                   (set 'more nil))))))
1061       (delete-process proc))))
1062
1063 (defun notmuch-search-operate-all (action)
1064   "Operate on all messages matching the current query.  Any
1065 number of whitespace separated actions can be given.  Each action
1066 must have one of the two forms
1067
1068   +tagname              Add the tag `tagname'
1069   -tagname              Remove the tag `tagname'
1070
1071 Each character of the tag name may consist of alphanumeric
1072 characters as well as `_.+-'.
1073 "
1074   (interactive "sOperation (+add -drop): notmuch tag ")
1075   (let ((action-split (split-string action " +")))
1076     ;; Perform some validation
1077     (let ((words action-split))
1078       (when (null words) (error "No operation given"))
1079       (while words
1080         (unless (string-match-p "^[\+\-][_\+\-\\w]+$" (car words))
1081           (error "Action must be of the form `+thistag -that_tag'"))
1082         (setq words (cdr words))))
1083     (apply 'notmuch-call-notmuch-process "tag"
1084            (append action-split (list notmuch-search-query-string) nil))))
1085
1086 (defun notmuch-search (query &optional oldest-first)
1087   "Run \"notmuch search\" with the given query string and display results."
1088   (interactive "sNotmuch search: ")
1089   (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
1090     (switch-to-buffer buffer)
1091     (notmuch-search-mode)
1092     (set 'notmuch-search-query-string query)
1093     (set 'notmuch-search-oldest-first oldest-first)
1094     (let ((proc (get-buffer-process (current-buffer)))
1095           (inhibit-read-only t))
1096       (if proc
1097           (error "notmuch search process already running for query `%s'" query)
1098         )
1099       (erase-buffer)
1100       (goto-char (point-min))
1101       (save-excursion
1102         (let ((proc (start-process-shell-command
1103                      "notmuch-search" buffer notmuch-command "search"
1104                      (if oldest-first "--sort=oldest-first" "--sort=newest-first")
1105                      (shell-quote-argument query))))
1106           (set-process-sentinel proc 'notmuch-search-process-sentinel)
1107           (set-process-filter proc 'notmuch-search-process-filter))))
1108     (run-hooks 'notmuch-search-hook)))
1109
1110 (defun notmuch-search-refresh-view ()
1111   "Refresh the current view.
1112
1113 Kills the current buffer and runs a new search with the same
1114 query string as the current search. If the current thread is in
1115 the new search results, then point will be placed on the same
1116 thread. Otherwise, point will be moved to attempt to be in the
1117 same relative position within the new buffer."
1118   (interactive)
1119   (let ((here (point))
1120         (oldest-first notmuch-search-oldest-first)
1121         (thread (notmuch-search-find-thread-id))
1122         (query notmuch-search-query-string))
1123     (kill-this-buffer)
1124     (notmuch-search query oldest-first)
1125     (goto-char (point-min))
1126     (if (re-search-forward (concat "^" thread) nil t)
1127         (beginning-of-line)
1128       (goto-char here))))
1129
1130 (defun notmuch-search-toggle-order ()
1131   "Toggle the current search order.
1132
1133 By default, the \"inbox\" view created by `notmuch' is displayed
1134 in chronological order (oldest thread at the beginning of the
1135 buffer), while any global searches created by `notmuch-search'
1136 are displayed in reverse-chronological order (newest thread at
1137 the beginning of the buffer).
1138
1139 This command toggles the sort order for the current search.
1140
1141 Note that any filtered searches created by
1142 `notmuch-search-filter' retain the search order of the parent
1143 search."
1144   (interactive)
1145   (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
1146   (notmuch-search-refresh-view))
1147
1148 (defun notmuch-search-filter (query)
1149   "Filter the current search results based on an additional query string.
1150
1151 Runs a new search matching only messages that match both the
1152 current search results AND the additional query string provided."
1153   (interactive "sFilter search: ")
1154   (notmuch-search (concat notmuch-search-query-string " and " query) notmuch-search-oldest-first))
1155
1156 (defun notmuch-search-filter-by-tag (tag)
1157   "Filter the current search results based on a single tag.
1158
1159 Runs a new search matching only messages that match both the
1160 current search results AND that are tagged with the given tag."
1161   (interactive
1162    (list (notmuch-select-tag-with-completion "Filter by tag: ")))
1163   (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
1164
1165 (defun notmuch ()
1166   "Run notmuch to display all mail with tag of 'inbox'"
1167   (interactive)
1168   (notmuch-search "tag:inbox" notmuch-search-oldest-first))
1169
1170 (setq mail-user-agent 'message-user-agent)
1171
1172 (defvar notmuch-folder-mode-map
1173   (let ((map (make-sparse-keymap)))
1174     (define-key map "n" 'next-line)
1175     (define-key map "p" 'previous-line)
1176     (define-key map "x" 'kill-this-buffer)
1177     (define-key map "q" 'kill-this-buffer)
1178     (define-key map "s" 'notmuch-search)
1179     (define-key map (kbd "RET") 'notmuch-folder-show-search)
1180     (define-key map "<" 'beginning-of-buffer)
1181     (define-key map "=" 'notmuch-folder)
1182     (define-key map "?" 'describe-mode)
1183     (define-key map [mouse-1] 'notmuch-folder-show-search)
1184     map)
1185   "Keymap for \"notmuch folder\" buffers.")
1186
1187 (fset 'notmuch-folder-mode-map notmuch-folder-mode-map)
1188
1189 (defcustom notmuch-folders (quote (("inbox" . "tag:inbox") ("unread" . "tag:unread")))
1190   "List of searches for the notmuch folder view"
1191   :type '(alist :key-type (string) :value-type (string))
1192   :group 'notmuch)
1193
1194 (defun notmuch-folder-mode ()
1195   "Major mode for showing notmuch 'folders'.
1196
1197 This buffer contains a list of messages counts returned by a
1198 customizable set of searches of your email archives. Each line
1199 in the buffer shows the search terms and the resulting message count.
1200
1201 Pressing RET on any line opens a search window containing the
1202 results for the search terms in that line.
1203
1204 \\{notmuch-folder-mode-map}"
1205   (interactive)
1206   (kill-all-local-variables)
1207   (use-local-map 'notmuch-folder-mode-map)
1208   (setq truncate-lines t)
1209   (hl-line-mode 1)
1210   (setq major-mode 'notmuch-folder-mode
1211         mode-name "notmuch-folder")
1212   (setq buffer-read-only t))
1213
1214 (defun notmuch-folder-add (folders)
1215   (if folders
1216       (let ((name (car (car folders)))
1217             (inhibit-read-only t)
1218             (search (cdr (car folders))))
1219         (insert name)
1220         (indent-to 16 1)
1221         (call-process notmuch-command nil t nil "count" search)
1222         (notmuch-folder-add (cdr folders)))))
1223
1224 (defun notmuch-folder-find-name ()
1225   (save-excursion
1226     (beginning-of-line)
1227     (let ((beg (point)))
1228       (forward-word)
1229       (filter-buffer-substring beg (point)))))
1230
1231 (defun notmuch-folder-show-search (&optional folder)
1232   "Show a search window for the search related to the specified folder."
1233   (interactive)
1234   (if (null folder)
1235       (setq folder (notmuch-folder-find-name)))
1236   (let ((search (assoc folder notmuch-folders)))
1237     (if search
1238         (notmuch-search (cdr search) notmuch-search-oldest-first))))
1239
1240 (defun notmuch-folder ()
1241   "Show the notmuch folder view and update the displayed counts."
1242   (interactive)
1243   (let ((buffer (get-buffer-create "*notmuch-folders*")))
1244     (switch-to-buffer buffer)
1245     (let ((inhibit-read-only t)
1246           (n (line-number-at-pos)))
1247       (erase-buffer)
1248       (notmuch-folder-mode)
1249       (notmuch-folder-add notmuch-folders)
1250       (goto-char (point-min))
1251       (goto-line n))))
1252
1253 (provide 'notmuch)