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