1 ; notmuch.el --- run notmuch within emacs
3 ; Copyright © Carl Worth
5 ; This file is part of Notmuch.
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.
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.
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/>.
20 ; Authors: Carl Worth <cworth@cworth.org>
22 ; This is an emacs-based interface to the notmuch mail system.
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.
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:
33 ; sudo make install-emacs
37 ; Then, to actually run it, add:
41 ; to your ~/.emacs file, and then run "M-x notmuch" from within emacs,
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).
54 (defvar notmuch-show-mode-map
55 (let ((map (make-sparse-keymap)))
56 (define-key map "?" 'notmuch-help)
57 (define-key map "q" 'kill-this-buffer)
58 (define-key map (kbd "C-p") 'notmuch-show-previous-line)
59 (define-key map (kbd "C-n") 'notmuch-show-next-line)
60 (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
61 (define-key map (kbd "TAB") 'notmuch-show-next-button)
62 (define-key map "s" 'notmuch-search)
63 (define-key map "m" 'message-mail)
64 (define-key map "f" 'notmuch-show-forward-current)
65 (define-key map "r" 'notmuch-show-reply)
66 (define-key map "|" 'notmuch-show-pipe-message)
67 (define-key map "w" 'notmuch-show-save-attachments)
68 (define-key map "V" 'notmuch-show-view-raw-message)
69 (define-key map "v" 'notmuch-show-view-all-mime-parts)
70 (define-key map "b" 'notmuch-show-toggle-current-body)
71 (define-key map "h" 'notmuch-show-toggle-current-header)
72 (define-key map "-" 'notmuch-show-remove-tag)
73 (define-key map "+" 'notmuch-show-add-tag)
74 (define-key map "X" 'notmuch-show-mark-read-then-archive-then-exit)
75 (define-key map "x" 'notmuch-show-archive-thread-then-exit)
76 (define-key map "A" 'notmuch-show-mark-read-then-archive-thread)
77 (define-key map "a" 'notmuch-show-archive-thread)
78 (define-key map "p" 'notmuch-show-previous-message)
79 (define-key map "N" 'notmuch-show-mark-read-then-next-open-message)
80 (define-key map "n" 'notmuch-show-next-message)
81 (define-key map (kbd "DEL") 'notmuch-show-rewind)
82 (define-key map " " 'notmuch-show-advance-marking-read-and-archiving)
84 "Keymap for \"notmuch show\" buffers.")
85 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
87 (defvar notmuch-show-signature-regexp "\\(-- ?\\|_+\\)$"
88 "Pattern to match a line that separates content from signature.
90 The regexp can (and should) include $ to match the end of the
91 line, but should not include ^ to match the beginning of the
92 line. This is because notmuch may have inserted additional space
93 for indentation at the beginning of the line. But notmuch will
94 move past the indentation when testing this pattern, (so that the
95 pattern can still test against the entire line).")
97 (defvar notmuch-show-signature-lines-max 12
98 "Maximum length of signature that will be hidden by default.")
100 (defvar notmuch-command "notmuch"
101 "Command to run the notmuch binary.")
103 (defvar notmuch-show-message-begin-regexp "\fmessage{")
104 (defvar notmuch-show-message-end-regexp "\fmessage}")
105 (defvar notmuch-show-header-begin-regexp "\fheader{")
106 (defvar notmuch-show-header-end-regexp "\fheader}")
107 (defvar notmuch-show-body-begin-regexp "\fbody{")
108 (defvar notmuch-show-body-end-regexp "\fbody}")
109 (defvar notmuch-show-attachment-begin-regexp "\fattachment{")
110 (defvar notmuch-show-attachment-end-regexp "\fattachment}")
111 (defvar notmuch-show-part-begin-regexp "\fpart{")
112 (defvar notmuch-show-part-end-regexp "\fpart}")
113 (defvar notmuch-show-marker-regexp "\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
115 (defvar notmuch-show-id-regexp "\\(id:[^ ]*\\)")
116 (defvar notmuch-show-depth-match-regexp " depth:\\([0-9]*\\).*match:\\([01]\\) ")
117 (defvar notmuch-show-filename-regexp "filename:\\(.*\\)$")
118 (defvar notmuch-show-tags-regexp "(\\([^)]*\\))$")
120 (defvar notmuch-show-parent-buffer nil)
121 (defvar notmuch-show-body-read-visible nil)
122 (defvar notmuch-show-citations-visible nil)
123 (defvar notmuch-show-signatures-visible nil)
124 (defvar notmuch-show-headers-visible nil)
126 ; XXX: This should be a generic function in emacs somewhere, not here
127 (defun point-invisible-p ()
128 "Return whether the character at point is invisible.
130 Here visibility is determined by `buffer-invisibility-spec' and
131 the invisible property of any overlays for point. It doesn't have
132 anything to do with whether point is currently being displayed
133 within the current window."
134 (let ((prop (get-char-property (point) 'invisible)))
135 (if (eq buffer-invisibility-spec t)
137 (or (memq prop buffer-invisibility-spec)
138 (assq prop buffer-invisibility-spec)))))
140 (defun notmuch-select-tag-with-completion (prompt &rest search-terms)
142 (with-output-to-string
143 (with-current-buffer standard-output
144 (apply 'call-process notmuch-command nil t nil "search-tags" search-terms)))))
145 (completing-read prompt (split-string tag-list "\n+" t) nil nil nil)))
147 (defun notmuch-show-next-line ()
148 "Like builtin `next-line' but ensuring we end on a visible character.
150 By advancing forward until reaching a visible character.
152 Unlike builtin `next-line' this version accepts no arguments."
154 (set 'this-command 'next-line)
155 (call-interactively 'next-line)
156 (while (point-invisible-p)
159 (defun notmuch-show-previous-line ()
160 "Like builtin `previous-line' but ensuring we end on a visible character.
162 By advancing forward until reaching a visible character.
164 Unlike builtin `previous-line' this version accepts no arguments."
166 (set 'this-command 'previous-line)
167 (call-interactively 'previous-line)
168 (while (point-invisible-p)
171 (defun notmuch-show-get-message-id ()
174 (if (not (looking-at notmuch-show-message-begin-regexp))
175 (re-search-backward notmuch-show-message-begin-regexp))
176 (re-search-forward notmuch-show-id-regexp)
177 (buffer-substring-no-properties (match-beginning 1) (match-end 1))))
179 (defun notmuch-show-get-filename ()
182 (if (not (looking-at notmuch-show-message-begin-regexp))
183 (re-search-backward notmuch-show-message-begin-regexp))
184 (re-search-forward notmuch-show-filename-regexp)
185 (buffer-substring-no-properties (match-beginning 1) (match-end 1))))
187 (defun notmuch-show-set-tags (tags)
190 (if (not (looking-at notmuch-show-message-begin-regexp))
191 (re-search-backward notmuch-show-message-begin-regexp))
192 (re-search-forward notmuch-show-tags-regexp)
193 (let ((inhibit-read-only t)
194 (beg (match-beginning 1))
196 (delete-region beg end)
198 (insert (mapconcat 'identity tags " ")))))
200 (defun notmuch-show-get-tags ()
203 (if (not (looking-at notmuch-show-message-begin-regexp))
204 (re-search-backward notmuch-show-message-begin-regexp))
205 (re-search-forward notmuch-show-tags-regexp)
206 (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
208 (defun notmuch-show-add-tag (&rest toadd)
209 "Add a tag to the current message."
211 (list (notmuch-select-tag-with-completion "Tag to add: ")))
212 (apply 'notmuch-call-notmuch-process
214 (mapcar (lambda (s) (concat "+" s)) toadd))
215 (cons (notmuch-show-get-message-id) nil)))
216 (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
218 (defun notmuch-show-remove-tag (&rest toremove)
219 "Remove a tag from the current message."
221 (list (notmuch-select-tag-with-completion "Tag to remove: " (notmuch-show-get-message-id))))
222 (let ((tags (notmuch-show-get-tags)))
223 (if (intersection tags toremove :test 'string=)
225 (apply 'notmuch-call-notmuch-process
227 (mapcar (lambda (s) (concat "-" s)) toremove))
228 (cons (notmuch-show-get-message-id) nil)))
229 (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
231 (defun notmuch-show-archive-thread-maybe-mark-read (markread)
233 (goto-char (point-min))
236 (notmuch-show-remove-tag "unread" "inbox")
237 (notmuch-show-remove-tag "inbox"))
240 (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
241 (goto-char (point-max)))))
242 (let ((parent-buffer notmuch-show-parent-buffer))
246 (switch-to-buffer parent-buffer)
248 (notmuch-search-show-thread)))))
250 (defun notmuch-show-mark-read-then-archive-thread ()
251 "Remove unread tags from thread, then archive and show next thread.
253 Archive each message currently shown by removing the \"unread\"
254 and \"inbox\" tag from each. Then kill this buffer and show the
255 next thread from the search from which this thread was originally
258 Note: This command is safe from any race condition of new messages
259 being delivered to the same thread. It does not archive the
260 entire thread, but only the messages shown in the current
263 (notmuch-show-archive-thread-maybe-mark-read t))
265 (defun notmuch-show-archive-thread ()
266 "Archive each message in thread, then show next thread from search.
268 Archive each message currently shown by removing the \"inbox\"
269 tag from each. Then kill this buffer and show the next thread
270 from the search from which this thread was originally shown.
272 Note: This command is safe from any race condition of new messages
273 being delivered to the same thread. It does not archive the
274 entire thread, but only the messages shown in the current
277 (notmuch-show-archive-thread-maybe-mark-read nil))
279 (defun notmuch-show-archive-thread-then-exit ()
280 "Archive each message in thread, then exit back to search results."
282 (notmuch-show-archive-thread)
285 (defun notmuch-show-mark-read-then-archive-then-exit ()
286 "Remove unread tags from thread, then archive and exit to search results."
288 (notmuch-show-mark-read-then-archive-thread)
291 (defun notmuch-show-view-raw-message ()
292 "View the raw email of the current message."
294 (view-file (notmuch-show-get-filename)))
296 (defmacro with-current-notmuch-show-message (&rest body)
297 "Evaluate body with current buffer set to the text of current message"
299 (let ((filename (notmuch-show-get-filename)))
300 (let ((buf (generate-new-buffer (concat "*notmuch-msg-" filename "*"))))
301 (with-current-buffer buf
302 (insert-file-contents filename nil nil nil t)
304 (kill-buffer buf)))))
306 (defun notmuch-show-view-all-mime-parts ()
307 "Use external viewers to view all attachments from the current message."
309 (with-current-notmuch-show-message
310 (mm-display-parts (mm-dissect-buffer))))
312 (defun notmuch-foreach-mime-part (function mm-handle)
313 (cond ((stringp (car mm-handle))
314 (dolist (part (cdr mm-handle))
315 (notmuch-foreach-mime-part function part)))
316 ((bufferp (car mm-handle))
317 (funcall function mm-handle))
318 (t (dolist (part mm-handle)
319 (notmuch-foreach-mime-part function part)))))
321 (defun notmuch-count-attachments (mm-handle)
323 (notmuch-foreach-mime-part
325 (let ((disposition (mm-handle-disposition p)))
326 (and (listp disposition)
327 (or (equal (car disposition) "attachment")
328 (and (equal (car disposition) "inline")
329 (assq 'filename disposition)))
334 (defun notmuch-save-attachments (mm-handle &optional queryp)
335 (notmuch-foreach-mime-part
337 (let ((disposition (mm-handle-disposition p)))
338 (and (listp disposition)
339 (or (equal (car disposition) "attachment")
340 (and (equal (car disposition) "inline")
341 (assq 'filename disposition)))
344 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
348 (defun notmuch-show-save-attachments ()
349 "Save all attachments from the current message."
351 (with-current-notmuch-show-message
352 (let ((mm-handle (mm-dissect-buffer)))
353 (notmuch-save-attachments
354 mm-handle (> (notmuch-count-attachments mm-handle) 1))))
357 (defun notmuch-reply (query-string)
358 (switch-to-buffer (generate-new-buffer "notmuch-draft"))
359 (call-process notmuch-command nil t nil "reply" query-string)
360 (message-insert-signature)
361 (goto-char (point-min))
362 (if (re-search-forward "^$" nil t)
364 (insert "--text follows this line--")
368 (defun notmuch-show-reply ()
369 "Begin composing a reply to the current message in a new buffer."
371 (let ((message-id (notmuch-show-get-message-id)))
372 (notmuch-reply message-id)))
374 (defun notmuch-show-forward-current ()
375 "Forward the current message."
377 (with-current-notmuch-show-message
380 (defun notmuch-show-pipe-message (command)
381 "Pipe the contents of the current message to the given command.
383 The given command will be executed with the raw contents of the
384 current email message as stdin. Anything printed by the command
385 to stdout or stderr will appear in the *Messages* buffer."
386 (interactive "sPipe message to command: ")
387 (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*"
388 (list command " < " (shell-quote-argument (notmuch-show-get-filename)))))
390 (defun notmuch-show-move-to-current-message-summary-line ()
391 "Move to the beginning of the one-line summary of the current message.
393 This gives us a stable place to move to and work from since the
394 summary line is always visible. This is important since moving to
395 an invisible location is unreliable, (the main command loop moves
396 point either forward or backward to the next visible character
397 when a command ends with point on an invisible character).
399 Emits an error if point is not within a valid message, (that is
400 no pattern of `notmuch-show-message-begin-regexp' could be found
401 by searching backward)."
403 (if (not (looking-at notmuch-show-message-begin-regexp))
404 (if (re-search-backward notmuch-show-message-begin-regexp nil t)
406 (error "Not within a valid message."))
409 (defun notmuch-show-last-message-p ()
410 "Predicate testing whether point is within the last message."
411 (save-window-excursion
413 (notmuch-show-move-to-current-message-summary-line)
414 (not (re-search-forward notmuch-show-message-begin-regexp nil t)))))
416 (defun notmuch-show-message-unread-p ()
417 "Predicate testing whether current message is unread."
418 (member "unread" (notmuch-show-get-tags)))
420 (defun notmuch-show-message-open-p ()
421 "Predicate testing whether current message is open (body is visible)."
422 (let ((btn (previous-button (point) t)))
423 (while (not (button-has-type-p btn 'notmuch-button-body-toggle-type))
424 (setq btn (previous-button (button-start btn))))
425 (not (invisible-p (button-get btn 'invisibility-spec)))))
427 (defun notmuch-show-next-message ()
428 "Advance to the beginning of the next message in the buffer.
430 Moves to the last visible character of the current message if
431 already on the last message in the buffer.
433 Returns nil if already on the last message in the buffer."
435 (notmuch-show-move-to-current-message-summary-line)
436 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
438 (notmuch-show-move-to-current-message-summary-line)
441 (goto-char (- (point-max) 1))
442 (while (point-invisible-p)
447 (defun notmuch-show-find-next-message ()
448 "Returns the position of the next message in the buffer.
450 Or the position of the last visible character of the current
451 message if already within the last message in the buffer."
452 ; save-excursion doesn't save our window position
453 ; save-window-excursion doesn't save point
454 ; Looks like we have to use both.
456 (save-window-excursion
457 (notmuch-show-next-message)
460 (defun notmuch-show-next-unread-message ()
461 "Advance to the beginning of the next unread message in the buffer.
463 Moves to the last visible character of the current message if
464 there are no more unread messages past the current point."
465 (notmuch-show-next-message)
466 (while (and (not (notmuch-show-last-message-p))
467 (not (notmuch-show-message-unread-p)))
468 (notmuch-show-next-message))
469 (if (not (notmuch-show-message-unread-p))
470 (notmuch-show-next-message)))
472 (defun notmuch-show-next-open-message ()
473 "Advance to the next open message (that is, body is not invisible)."
474 (while (and (notmuch-show-next-message)
475 (not (notmuch-show-message-open-p)))))
477 (defun notmuch-show-previous-message ()
478 "Backup to the beginning of the previous message in the buffer.
480 If within a message rather than at the beginning of it, then
481 simply move to the beginning of the current message."
483 (let ((start (point)))
484 (notmuch-show-move-to-current-message-summary-line)
485 (if (not (< (point) start))
486 ; Go backward twice to skip the current message's marker
488 (re-search-backward notmuch-show-message-begin-regexp nil t)
489 (re-search-backward notmuch-show-message-begin-regexp nil t)
490 (notmuch-show-move-to-current-message-summary-line)
494 (defun notmuch-show-find-previous-message ()
495 "Returns the position of the previous message in the buffer.
497 Or the position of the beginning of the current message if point
498 is originally within the message rather than at the beginning of
500 ; save-excursion doesn't save our window position
501 ; save-window-excursion doesn't save point
502 ; Looks like we have to use both.
504 (save-window-excursion
505 (notmuch-show-previous-message)
508 (defun notmuch-show-mark-read-then-next-open-message ()
509 "Remove unread tag from this message, then advance to next open message."
511 (notmuch-show-remove-tag "unread")
512 (notmuch-show-next-open-message))
514 (defun notmuch-show-rewind ()
515 "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-marking-read-and-archiving]).
517 Specifically, if the beginning of the previous email is fewer
518 than `window-height' lines from the current point, move to it
519 just like `notmuch-show-previous-message'.
521 Otherwise, just scroll down a screenful of the current message.
523 This command does not modify any message tags, (it does not undo
524 any effects from previous calls to
525 `notmuch-show-advance-marking-read-and-archiving'."
527 (let ((previous (notmuch-show-find-previous-message)))
528 (if (> (count-lines previous (point)) (- (window-height) next-screen-context-lines))
532 ((beginning-of-buffer) nil))
533 (goto-char (window-start)))
534 (notmuch-show-previous-message))))
536 (defun notmuch-show-advance-marking-read-and-archiving ()
537 "Advance through thread, marking read and archiving.
539 This command is intended to be one of the simplest ways to
540 process a thread of email. It does the following:
542 If the current message in the thread is not yet fully visible,
543 scroll by a near screenful to read more of the message.
545 Otherwise, (the end of the current message is already within the
546 current window), remove the \"unread\" tag (if present) from the
547 current message and advance to the next open message.
549 Finally, if there is no further message to advance to, and this
550 last message is already read, then archive the entire current
551 thread, (remove the \"inbox\" tag from each message). Also kill
552 this buffer, and display the next thread from the search from
553 which this thread was originally shown."
555 (let ((next (notmuch-show-find-next-message))
556 (unread (notmuch-show-message-unread-p)))
557 (if (> next (window-end))
559 (let ((last (notmuch-show-last-message-p)))
560 (notmuch-show-mark-read-then-next-open-message)
562 (notmuch-show-archive-thread))))))
564 (defun notmuch-show-next-button ()
565 "Advance point to the next button in the buffer."
567 (goto-char (button-start (next-button (point)))))
569 (defun notmuch-show-previous-button ()
570 "Move point back to the previous button in the buffer."
572 (goto-char (button-start (previous-button (point)))))
574 (defun notmuch-toggle-invisible-action (cite-button)
575 (let ((invis-spec (button-get cite-button 'invisibility-spec)))
576 (if (invisible-p invis-spec)
577 (remove-from-invisibility-spec invis-spec)
578 (add-to-invisibility-spec invis-spec)
580 (force-window-update)
583 (defun notmuch-show-toggle-current-body ()
584 "Toggle the display of the current message body."
587 (notmuch-show-move-to-current-message-summary-line)
588 (unless (button-at (point))
589 (notmuch-show-next-button))
593 (defun notmuch-show-toggle-current-header ()
594 "Toggle the display of the current message header."
597 (notmuch-show-move-to-current-message-summary-line)
599 (unless (button-at (point))
600 (notmuch-show-next-button))
604 (define-button-type 'notmuch-button-invisibility-toggle-type
605 'action 'notmuch-toggle-invisible-action
608 (define-button-type 'notmuch-button-citation-toggle-type 'help-echo "mouse-1, RET: Show citation"
609 :supertype 'notmuch-button-invisibility-toggle-type)
610 (define-button-type 'notmuch-button-signature-toggle-type 'help-echo "mouse-1, RET: Show signature"
611 :supertype 'notmuch-button-invisibility-toggle-type)
612 (define-button-type 'notmuch-button-headers-toggle-type 'help-echo "mouse-1, RET: Show headers"
613 :supertype 'notmuch-button-invisibility-toggle-type)
614 (define-button-type 'notmuch-button-body-toggle-type
615 'help-echo "mouse-1, RET: Show message"
616 'face 'notmuch-message-summary-face
617 :supertype 'notmuch-button-invisibility-toggle-type)
619 (defun notmuch-show-markup-citations-region (beg end depth)
622 (while (< (point) end)
623 (let ((beg-sub (point-marker))
624 (indent (make-string depth ? ))
626 (move-to-column depth)
627 (if (looking-at citation)
629 (while (looking-at citation)
631 (move-to-column depth))
632 (let ((overlay (make-overlay beg-sub (point)))
633 (invis-spec (make-symbol "notmuch-citation-region")))
634 (add-to-invisibility-spec invis-spec)
635 (overlay-put overlay 'invisible invis-spec)
636 (let ((p (point-marker))
638 (concat "[" (number-to-string (count-lines beg-sub (point)))
639 "-line citation. Click/Enter to show.]")))
640 (goto-char (- beg-sub 1))
641 (insert (concat "\n" indent))
642 (insert-button cite-button-text
643 'invisibility-spec invis-spec
644 :type 'notmuch-button-citation-toggle-type)
647 (move-to-column depth)
648 (if (looking-at notmuch-show-signature-regexp)
649 (let ((sig-lines (- (count-lines beg-sub end) 1)))
650 (if (<= sig-lines notmuch-show-signature-lines-max)
652 (let ((invis-spec (make-symbol "notmuch-signature-region")))
653 (add-to-invisibility-spec invis-spec)
654 (overlay-put (make-overlay beg-sub end)
655 'invisible invis-spec)
657 (goto-char (- beg-sub 1))
658 (insert (concat "\n" indent))
659 (let ((sig-button-text (concat "[" (number-to-string sig-lines)
660 "-line signature. Click/Enter to show.]")))
661 (insert-button sig-button-text 'invisibility-spec invis-spec
662 :type 'notmuch-button-signature-toggle-type)
668 (defun notmuch-show-markup-part (beg end depth)
669 (if (re-search-forward notmuch-show-part-begin-regexp nil t)
672 (let ((beg (point-marker)))
673 (re-search-forward notmuch-show-part-end-regexp)
674 (let ((end (copy-marker (match-beginning 0))))
678 (indent-rigidly beg end depth)
679 (notmuch-show-markup-citations-region beg end depth)
680 ; Advance to the next part (if any) (so the outer loop can
681 ; determine whether we've left the current message.
682 (if (re-search-forward notmuch-show-part-begin-regexp nil t)
683 (beginning-of-line)))))
686 (defun notmuch-show-markup-parts-region (beg end depth)
689 (while (< (point) end)
690 (notmuch-show-markup-part beg end depth))))
692 (defun notmuch-show-markup-body (depth match btn)
693 "Markup a message body, (indenting, buttonizing citations,
694 etc.), and conditionally hiding the body itself if the message
695 has been read and does not match the current search.
697 DEPTH specifies the depth at which this message appears in the
698 tree of the current thread, (the top-level messages have depth 0
699 and each reply increases depth by 1). MATCH indicates whether
700 this message is regarded as matching the current search. BTN is
701 the button which is used to toggle the visibility of this
704 When this function is called, point must be within the message, but
705 before the delimiter marking the beginning of the body."
706 (re-search-forward notmuch-show-body-begin-regexp)
708 (let ((beg (point-marker)))
709 (re-search-forward notmuch-show-body-end-regexp)
710 (let ((end (copy-marker (match-beginning 0))))
711 (notmuch-show-markup-parts-region beg end depth)
712 (let ((invis-spec (make-symbol "notmuch-show-body-read")))
713 (overlay-put (make-overlay beg end)
714 'invisible invis-spec)
715 (button-put btn 'invisibility-spec invis-spec)
716 (if (not (or (notmuch-show-message-unread-p) match))
717 (add-to-invisibility-spec invis-spec)))
722 (defun notmuch-fontify-headers ()
723 (while (looking-at "[[:space:]]")
725 (if (looking-at "[Tt]o:")
727 (overlay-put (make-overlay (point) (re-search-forward ":"))
728 'face 'message-header-name)
729 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
730 'face 'message-header-to))
731 (if (looking-at "[B]?[Cc][Cc]:")
733 (overlay-put (make-overlay (point) (re-search-forward ":"))
734 'face 'message-header-name)
735 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
736 'face 'message-header-cc))
737 (if (looking-at "[Ss]ubject:")
739 (overlay-put (make-overlay (point) (re-search-forward ":"))
740 'face 'message-header-name)
741 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
742 'face 'message-header-subject))
743 (if (looking-at "[Ff]rom:")
745 (overlay-put (make-overlay (point) (re-search-forward ":"))
746 'face 'message-header-name)
747 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
748 'face 'message-header-other)))))))
750 (defun notmuch-show-markup-header (message-begin depth)
751 "Buttonize and decorate faces in a message header.
753 MESSAGE-BEGIN is the position of the absolute first character in
754 the message (including all delimiters that will end up being
755 invisible etc.). This is to allow a button to reliably extend to
756 the beginning of the message even if point is positioned at an
757 invisible character (such as the beginning of the buffer).
759 DEPTH specifies the depth at which this message appears in the
760 tree of the current thread, (the top-level messages have depth 0
761 and each reply increases depth by 1)."
762 (re-search-forward notmuch-show-header-begin-regexp)
764 (let ((beg (point-marker))
765 (summary-end (copy-marker (line-beginning-position 2)))
766 (subject-end (copy-marker (line-end-position 2)))
767 (invis-spec (make-symbol "notmuch-show-header"))
769 (re-search-forward notmuch-show-header-end-regexp)
771 (let ((end (point-marker)))
772 (indent-rigidly beg end depth)
774 (setq btn (make-button message-begin summary-end :type 'notmuch-button-body-toggle-type))
776 (add-to-invisibility-spec invis-spec)
777 (overlay-put (make-overlay subject-end end)
778 'invisible invis-spec)
779 (make-button (line-beginning-position) subject-end
780 'invisibility-spec invis-spec
781 :type 'notmuch-button-headers-toggle-type)
782 (while (looking-at "[[:space:]]*[A-Za-z][-A-Za-z0-9]*:")
784 (notmuch-fontify-headers)
790 (set-marker summary-end nil)
791 (set-marker subject-end nil)
796 (defun notmuch-show-markup-message ()
797 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
798 (let ((message-begin (match-beginning 0)))
799 (re-search-forward notmuch-show-depth-match-regexp)
800 (let ((depth (string-to-number (buffer-substring (match-beginning 1) (match-end 1))))
801 (match (string= "1" (buffer-substring (match-beginning 2) (match-end 2))))
803 (setq btn (notmuch-show-markup-header message-begin depth))
804 (notmuch-show-markup-body depth match btn)))
805 (goto-char (point-max))))
807 (defun notmuch-show-hide-markers ()
809 (goto-char (point-min))
811 (if (re-search-forward notmuch-show-marker-regexp nil t)
813 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
814 'invisible 'notmuch-show-marker))
815 (goto-char (point-max))))))
817 (defun notmuch-show-markup-messages ()
819 (goto-char (point-min))
821 (notmuch-show-markup-message)))
822 (notmuch-show-hide-markers))
824 (defun notmuch-documentation-first-line (symbol)
825 "Return the first line of the documentation string for SYMBOL."
826 (let ((doc (documentation symbol)))
829 (insert (documentation symbol t))
830 (goto-char (point-min))
833 (buffer-substring beg (point))))
836 (defun notmuch-prefix-key-description (key)
837 "Given a prefix key code, return a human-readable string representation.
839 This is basically just `format-kbd-macro' but we also convert ESC to M-."
840 (let ((desc (format-kbd-macro (vector key))))
841 (if (string= desc "ESC")
845 ; I would think that emacs would have code handy for walking a keymap
846 ; and generating strings for each key, and I would prefer to just call
847 ; that. But I couldn't find any (could be all implemented in C I
848 ; suppose), so I wrote my own here.
849 (defun notmuch-substitute-one-command-key-with-prefix (prefix binding)
850 "For a key binding, return a string showing a human-readable
851 representation of the prefixed key as well as the first line of
852 documentation from the bound function.
854 For a mouse binding, return nil."
855 (let ((key (car binding))
856 (action (cdr binding)))
857 (if (mouse-event-p key)
860 (let ((substitute (apply-partially 'notmuch-substitute-one-command-key-with-prefix (notmuch-prefix-key-description key))))
861 (mapconcat substitute (cdr action) "\n"))
862 (concat prefix (format-kbd-macro (vector key))
864 (notmuch-documentation-first-line action))))))
866 (defalias 'notmuch-substitute-one-command-key
867 (apply-partially 'notmuch-substitute-one-command-key-with-prefix nil))
869 (defun notmuch-substitute-command-keys (doc)
870 "Like `substitute-command-keys' but with documentation, not function names."
872 (while (string-match "\\\\{\\([^}[:space:]]*\\)}" doc beg)
873 (let ((map (substring doc (match-beginning 1) (match-end 1))))
874 (setq doc (replace-match (mapconcat 'notmuch-substitute-one-command-key
875 (cdr (symbol-value (intern map))) "\n") 1 1 doc)))
876 (setq beg (match-end 0)))
879 (defun notmuch-help ()
880 "Display help for the current notmuch mode."
882 (let* ((mode major-mode)
883 (doc (substitute-command-keys (notmuch-substitute-command-keys (documentation mode t)))))
884 (with-current-buffer (generate-new-buffer "*notmuch-help*")
886 (goto-char (point-min))
887 (set-buffer-modified-p nil)
888 (view-buffer (current-buffer) 'kill-buffer-if-not-modified))))
891 (defun notmuch-show-mode ()
892 "Major mode for viewing a thread with notmuch.
894 This buffer contains the results of the \"notmuch show\" command
895 for displaying a single thread of email from your email archives.
897 By default, various components of email messages, (citations,
898 signatures, already-read messages), are hidden. You can make
899 these parts visible by clicking with the mouse button or by
900 pressing RET after positioning the cursor on a hidden part, (for
901 which \\[notmuch-show-next-button] and \\[notmuch-show-previous-button] are helpful).
903 Reading the thread sequentially is well-supported by pressing
904 \\[notmuch-show-advance-marking-read-and-archiving]. This will scroll the current message (if necessary),
905 advance to the next message, or advance to the next thread (if
906 already on the last message of a thread). As each message is
907 scrolled away its \"unread\" tag will be removed, and as each
908 thread is scrolled away the \"inbox\" tag will be removed from
909 each message in the thread.
911 Other commands are available to read or manipulate the thread more
912 selectively, (such as '\\[notmuch-show-next-message]' and '\\[notmuch-show-previous-message]' to advance to messages without
913 removing any tags, and '\\[notmuch-show-archive-thread]' to archive an entire thread without
914 scrolling through with \\[notmuch-show-advance-marking-read-and-archiving]).
916 You can add or remove arbitary tags from the current message with
917 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
919 All currently available key bindings:
921 \\{notmuch-show-mode-map}"
923 (kill-all-local-variables)
924 (add-to-invisibility-spec 'notmuch-show-marker)
925 (use-local-map notmuch-show-mode-map)
926 (setq major-mode 'notmuch-show-mode
927 mode-name "notmuch-show")
928 (setq buffer-read-only t))
930 (defgroup notmuch nil
931 "Notmuch mail reader for Emacs."
934 (defcustom notmuch-show-hook nil
935 "List of functions to call when notmuch displays a message."
937 :options '(goto-address)
940 (defcustom notmuch-search-hook nil
941 "List of functions to call when notmuch displays the search results."
943 :options '(hl-line-mode)
946 ; Make show mode a bit prettier, highlighting URLs and using word wrap
948 (defun notmuch-show-pretty-hook ()
949 (goto-address-mode 1)
952 (add-hook 'notmuch-show-hook 'notmuch-show-pretty-hook)
953 (add-hook 'notmuch-search-hook
957 (defun notmuch-show (thread-id &optional parent-buffer query-context)
958 "Run \"notmuch show\" with the given thread ID and display results.
960 The optional PARENT-BUFFER is the notmuch-search buffer from
961 which this notmuch-show command was executed, (so that the next
962 thread from that buffer can be show when done with this one).
964 The optional QUERY-CONTEXT is a notmuch search term. Only messages from the thread
965 matching this search term are shown if non-nil. "
966 (interactive "sNotmuch show: ")
967 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
968 (switch-to-buffer buffer)
970 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
971 (let ((proc (get-buffer-process (current-buffer)))
972 (inhibit-read-only t))
974 (error "notmuch search process already running for query `%s'" thread-id)
977 (goto-char (point-min))
979 (let* ((basic-args (list notmuch-command nil t nil "show" "--entire-thread" thread-id))
980 (args (if query-context (append basic-args (list "and (" query-context ")")) basic-args)))
981 (apply 'call-process args)
982 (when (and (eq (buffer-size) 0) query-context)
983 (apply 'call-process basic-args)))
984 (notmuch-show-markup-messages)
986 (run-hooks 'notmuch-show-hook)
987 ; Move straight to the first open message
988 (if (not (notmuch-show-message-open-p))
989 (notmuch-show-next-open-message))
992 (defvar notmuch-search-authors-width 40
993 "Number of columns to use to display authors in a notmuch-search buffer.")
995 (defvar notmuch-search-mode-map
996 (let ((map (make-sparse-keymap)))
997 (define-key map "?" 'notmuch-help)
998 (define-key map "q" 'kill-this-buffer)
999 (define-key map "x" 'kill-this-buffer)
1000 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
1001 (define-key map "b" 'notmuch-search-scroll-down)
1002 (define-key map " " 'notmuch-search-scroll-up)
1003 (define-key map "<" 'notmuch-search-first-thread)
1004 (define-key map ">" 'notmuch-search-last-thread)
1005 (define-key map "p" 'notmuch-search-previous-thread)
1006 (define-key map "n" 'notmuch-search-next-thread)
1007 (define-key map "r" 'notmuch-search-reply-to-thread)
1008 (define-key map "m" 'message-mail)
1009 (define-key map "s" 'notmuch-search)
1010 (define-key map "o" 'notmuch-search-toggle-order)
1011 (define-key map "=" 'notmuch-search-refresh-view)
1012 (define-key map "t" 'notmuch-search-filter-by-tag)
1013 (define-key map "f" 'notmuch-search-filter)
1014 (define-key map [mouse-1] 'notmuch-search-show-thread)
1015 (define-key map "*" 'notmuch-search-operate-all)
1016 (define-key map "a" 'notmuch-search-archive-thread)
1017 (define-key map "-" 'notmuch-search-remove-tag)
1018 (define-key map "+" 'notmuch-search-add-tag)
1019 (define-key map (kbd "RET") 'notmuch-search-show-thread)
1021 "Keymap for \"notmuch search\" buffers.")
1022 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
1024 (defvar notmuch-search-query-string)
1025 (defvar notmuch-search-oldest-first t
1026 "Show the oldest mail first in the search-mode")
1028 (defvar notmuch-search-disjunctive-regexp "\\<[oO][rR]\\>")
1030 (defun notmuch-search-scroll-up ()
1031 "Move forward through search results by one window's worth."
1035 ((end-of-buffer) (notmuch-search-last-thread))))
1037 (defun notmuch-search-scroll-down ()
1038 "Move backward through the search results by one window's worth."
1040 ; I don't know why scroll-down doesn't signal beginning-of-buffer
1041 ; the way that scroll-up signals end-of-buffer, but c'est la vie.
1043 ; So instead of trapping a signal we instead check whether the
1044 ; window begins on the first line of the buffer and if so, move
1045 ; directly to that position. (We have to count lines since the
1046 ; window-start position is not the same as point-min due to the
1047 ; invisible thread-ID characters on the first line.
1048 (if (equal (count-lines (point-min) (window-start)) 0)
1049 (goto-char (point-min))
1052 (defun notmuch-search-next-thread ()
1053 "Select the next thread in the search results."
1057 (defun notmuch-search-previous-thread ()
1058 "Select the previous thread in the search results."
1062 (defun notmuch-search-last-thread ()
1063 "Select the last thread in the search results."
1065 (goto-char (point-max))
1068 (defun notmuch-search-first-thread ()
1069 "Select the first thread in the search results."
1071 (goto-char (point-min)))
1073 (defface notmuch-message-summary-face
1074 '((((class color) (background light)) (:background "#f0f0f0"))
1075 (((class color) (background dark)) (:background "#303030")))
1076 "Face for the single-line message summary in notmuch-show-mode."
1079 (defface notmuch-tag-face
1082 (:foreground "OliveDrab1"))
1085 (:foreground "navy blue" :bold t))
1088 "Notmuch search mode face used to highligh tags."
1091 (defvar notmuch-tag-face-alist nil
1092 "List containing the tag list that need to be highlighed")
1094 (defvar notmuch-search-font-lock-keywords nil)
1097 (defun notmuch-search-mode ()
1098 "Major mode displaying results of a notmuch search.
1100 This buffer contains the results of a \"notmuch search\" of your
1101 email archives. Each line in the buffer represents a single
1102 thread giving a summary of the thread (a relative date, the
1103 number of matched messages and total messages in the thread,
1104 participants in the thread, a representative subject line, and
1107 Pressing \\[notmuch-search-show-thread] on any line displays that thread. The '\\[notmuch-search-add-tag]' and '\\[notmuch-search-remove-tag]'
1108 keys can be used to add or remove tags from a thread. The '\\[notmuch-search-archive-thread]' key
1109 is a convenience for archiving a thread (removing the \"inbox\"
1110 tag). The '\\[notmuch-search-operate-all]' key can be used to add or remove a tag from all
1111 threads in the current buffer.
1113 Other useful commands are '\\[notmuch-search-filter]' for filtering the current search
1114 based on an additional query string, '\\[notmuch-search-filter-by-tag]' for filtering to include
1115 only messages with a given tag, and '\\[notmuch-search]' to execute a new, global
1118 Complete list of currently available key bindings:
1120 \\{notmuch-search-mode-map}"
1122 (kill-all-local-variables)
1123 (make-local-variable 'notmuch-search-query-string)
1124 (make-local-variable 'notmuch-search-oldest-first)
1125 (set (make-local-variable 'scroll-preserve-screen-position) t)
1126 (add-to-invisibility-spec 'notmuch-search)
1127 (use-local-map notmuch-search-mode-map)
1128 (setq truncate-lines t)
1129 (setq major-mode 'notmuch-search-mode
1130 mode-name "notmuch-search")
1131 (setq buffer-read-only t)
1132 (if (not notmuch-tag-face-alist)
1133 (add-to-list 'notmuch-search-font-lock-keywords (list
1134 "(\\([^)]*\\))$" '(1 'notmuch-tag-face)))
1135 (let ((notmuch-search-tags (mapcar 'car notmuch-tag-face-alist)))
1136 (loop for notmuch-search-tag in notmuch-search-tags
1137 do (add-to-list 'notmuch-search-font-lock-keywords (list
1138 (concat "([^)]*\\(" notmuch-search-tag "\\)[^)]*)$")
1139 `(1 ,(cdr (assoc notmuch-search-tag notmuch-tag-face-alist))))))))
1140 (set (make-local-variable 'font-lock-defaults)
1141 '(notmuch-search-font-lock-keywords t)))
1143 (defun notmuch-search-find-thread-id ()
1144 "Return the thread for the current thread"
1145 (get-text-property (point) 'notmuch-search-thread-id))
1147 (defun notmuch-search-find-authors ()
1148 "Return the authors for the current thread"
1149 (get-text-property (point) 'notmuch-search-authors))
1151 (defun notmuch-search-find-subject ()
1152 "Return the subject for the current thread"
1153 (get-text-property (point) 'notmuch-search-subject))
1155 (defun notmuch-search-show-thread ()
1156 "Display the currently selected thread."
1158 (let ((thread-id (notmuch-search-find-thread-id)))
1159 (if (> (length thread-id) 0)
1160 (notmuch-show thread-id (current-buffer) notmuch-search-query-string)
1161 (error "End of search results"))))
1163 (defun notmuch-search-reply-to-thread ()
1164 "Begin composing a reply to the entire current thread in a new buffer."
1166 (let ((message-id (notmuch-search-find-thread-id)))
1167 (notmuch-reply message-id)))
1169 (defun notmuch-call-notmuch-process (&rest args)
1170 "Synchronously invoke \"notmuch\" with the given list of arguments.
1172 Output from the process will be presented to the user as an error
1173 and will also appear in a buffer named \"*Notmuch errors*\"."
1174 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
1175 (with-current-buffer error-buffer
1177 (if (eq (apply 'call-process notmuch-command nil error-buffer nil args) 0)
1180 (with-current-buffer error-buffer
1181 (let ((beg (point-min))
1182 (end (- (point-max) 1)))
1183 (error (buffer-substring beg end))
1186 (defun notmuch-search-set-tags (tags)
1189 (re-search-backward "(")
1192 (inhibit-read-only t))
1193 (re-search-forward ")")
1195 (let ((end (point)))
1196 (delete-region beg end)
1197 (insert (mapconcat 'identity tags " "))))))
1199 (defun notmuch-search-get-tags ()
1202 (re-search-backward "(")
1203 (let ((beg (+ (point) 1)))
1204 (re-search-forward ")")
1205 (let ((end (- (point) 1)))
1206 (split-string (buffer-substring beg end))))))
1208 (defun notmuch-search-add-tag (tag)
1209 "Add a tag to the currently selected thread.
1211 The tag is added to messages in the currently selected thread
1212 which match the current search terms."
1214 (list (notmuch-select-tag-with-completion "Tag to add: ")))
1215 (notmuch-call-notmuch-process "tag" (concat "+" tag) (notmuch-search-find-thread-id))
1216 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
1218 (defun notmuch-search-remove-tag (tag)
1219 "Remove a tag from the currently selected thread.
1221 The tag is removed from messages in the currently selected thread
1222 which match the current search terms."
1224 (list (notmuch-select-tag-with-completion "Tag to remove: " (notmuch-search-find-thread-id))))
1225 (notmuch-call-notmuch-process "tag" (concat "-" tag) (notmuch-search-find-thread-id))
1226 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
1228 (defun notmuch-search-archive-thread ()
1229 "Archive the currently selected thread (remove its \"inbox\" tag).
1231 This function advances the next thread when finished."
1233 (notmuch-search-remove-tag "inbox")
1236 (defun notmuch-search-process-sentinel (proc msg)
1237 "Add a message to let user know when \"notmuch search\" exits"
1238 (let ((buffer (process-buffer proc))
1239 (status (process-status proc))
1240 (exit-status (process-exit-status proc)))
1241 (if (memq status '(exit signal))
1242 (if (buffer-live-p buffer)
1243 (with-current-buffer buffer
1245 (let ((inhibit-read-only t))
1246 (goto-char (point-max))
1247 (if (eq status 'signal)
1248 (insert "Incomplete search results (search process was killed).\n"))
1249 (if (eq status 'exit)
1251 (insert "End of search results.")
1252 (if (not (= exit-status 0))
1253 (insert (format " (process returned %d)" exit-status)))
1254 (insert "\n"))))))))))
1256 (defun notmuch-search-process-filter (proc string)
1257 "Process and filter the output of \"notmuch search\""
1258 (let ((buffer (process-buffer proc)))
1259 (if (buffer-live-p buffer)
1260 (with-current-buffer buffer
1264 (inhibit-read-only t))
1266 (if (string-match "^\\(thread:[0-9A-Fa-f]*\\) \\(.*\\) \\(\\[[0-9/]*\\]\\) \\([^:]*\\); \\(.*\\) (\\([^()]*\\))$" string line)
1267 (let* ((thread-id (match-string 1 string))
1268 (date (match-string 2 string))
1269 (count (match-string 3 string))
1270 (authors (match-string 4 string))
1271 (authors-length (length authors))
1272 (subject (match-string 5 string))
1273 (tags (match-string 6 string)))
1274 (if (> authors-length 40)
1275 (set 'authors (concat (substring authors 0 (- 40 3)) "...")))
1276 (goto-char (point-max))
1277 (let ((beg (point-marker)))
1278 (insert (format "%s %-7s %-40s %s (%s)\n" date count authors subject tags))
1279 (put-text-property beg (point-marker) 'notmuch-search-thread-id thread-id)
1280 (put-text-property beg (point-marker) 'notmuch-search-authors authors)
1281 (put-text-property beg (point-marker) 'notmuch-search-subject subject))
1282 (set 'line (match-end 0)))
1283 (set 'more nil))))))
1284 (delete-process proc))))
1286 (defun notmuch-search-operate-all (action)
1287 "Add/remove tags from all matching messages.
1289 Tis command adds or removes tags from all messages matching the
1290 current search terms. When called interactively, this command
1291 will prompt for tags to be added or removed. Tags prefixed with
1292 '+' will be added and tags prefixed with '-' will be removed.
1294 Each character of the tag name may consist of alphanumeric
1295 characters as well as `_.+-'.
1297 (interactive "sOperation (+add -drop): notmuch tag ")
1298 (let ((action-split (split-string action " +")))
1299 ;; Perform some validation
1300 (let ((words action-split))
1301 (when (null words) (error "No operation given"))
1303 (unless (string-match-p "^[-+][-+_.[:word:]]+$" (car words))
1304 (error "Action must be of the form `+thistag -that_tag'"))
1305 (setq words (cdr words))))
1306 (apply 'notmuch-call-notmuch-process "tag"
1307 (append action-split (list notmuch-search-query-string) nil))))
1310 (defun notmuch-search (query &optional oldest-first)
1311 "Run \"notmuch search\" with the given query string and display results."
1312 (interactive "sNotmuch search: ")
1313 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
1314 (switch-to-buffer buffer)
1315 (notmuch-search-mode)
1316 (set 'notmuch-search-query-string query)
1317 (set 'notmuch-search-oldest-first oldest-first)
1318 (let ((proc (get-buffer-process (current-buffer)))
1319 (inhibit-read-only t))
1321 (error "notmuch search process already running for query `%s'" query)
1324 (goto-char (point-min))
1326 (let ((proc (start-process-shell-command
1327 "notmuch-search" buffer notmuch-command "search"
1328 (if oldest-first "--sort=oldest-first" "--sort=newest-first")
1329 (shell-quote-argument query))))
1330 (set-process-sentinel proc 'notmuch-search-process-sentinel)
1331 (set-process-filter proc 'notmuch-search-process-filter))))
1332 (run-hooks 'notmuch-search-hook)))
1334 (defun notmuch-search-refresh-view ()
1335 "Refresh the current view.
1337 Kills the current buffer and runs a new search with the same
1338 query string as the current search. If the current thread is in
1339 the new search results, then point will be placed on the same
1340 thread. Otherwise, point will be moved to attempt to be in the
1341 same relative position within the new buffer."
1343 (let ((here (point))
1344 (oldest-first notmuch-search-oldest-first)
1345 (thread (notmuch-search-find-thread-id))
1346 (query notmuch-search-query-string))
1348 (notmuch-search query oldest-first)
1349 (goto-char (point-min))
1350 (if (re-search-forward (concat "^" thread) nil t)
1354 (defun notmuch-search-toggle-order ()
1355 "Toggle the current search order.
1357 By default, the \"inbox\" view created by `notmuch' is displayed
1358 in chronological order (oldest thread at the beginning of the
1359 buffer), while any global searches created by `notmuch-search'
1360 are displayed in reverse-chronological order (newest thread at
1361 the beginning of the buffer).
1363 This command toggles the sort order for the current search.
1365 Note that any filtered searches created by
1366 `notmuch-search-filter' retain the search order of the parent
1369 (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
1370 (notmuch-search-refresh-view))
1372 (defun notmuch-search-filter (query)
1373 "Filter the current search results based on an additional query string.
1375 Runs a new search matching only messages that match both the
1376 current search results AND the additional query string provided."
1377 (interactive "sFilter search: ")
1378 (let ((grouped-query (if (string-match-p notmuch-search-disjunctive-regexp query) (concat "( " query " )") query)))
1379 (notmuch-search (concat notmuch-search-query-string " and " grouped-query) notmuch-search-oldest-first)))
1381 (defun notmuch-search-filter-by-tag (tag)
1382 "Filter the current search results based on a single tag.
1384 Runs a new search matching only messages that match both the
1385 current search results AND that are tagged with the given tag."
1387 (list (notmuch-select-tag-with-completion "Filter by tag: ")))
1388 (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
1393 "Run notmuch to display all mail with tag of 'inbox'"
1395 (notmuch-search "tag:inbox" notmuch-search-oldest-first))
1397 (setq mail-user-agent 'message-user-agent)
1399 (defvar notmuch-folder-mode-map
1400 (let ((map (make-sparse-keymap)))
1401 (define-key map "?" 'notmuch-help)
1402 (define-key map "x" 'kill-this-buffer)
1403 (define-key map "q" 'kill-this-buffer)
1404 (define-key map ">" 'notmuch-folder-last)
1405 (define-key map "<" 'notmuch-folder-first)
1406 (define-key map "=" 'notmuch-folder)
1407 (define-key map "s" 'notmuch-search)
1408 (define-key map [mouse-1] 'notmuch-folder-show-search)
1409 (define-key map (kbd "RET") 'notmuch-folder-show-search)
1410 (define-key map "p" 'notmuch-folder-previous)
1411 (define-key map "n" 'notmuch-folder-next)
1413 "Keymap for \"notmuch folder\" buffers.")
1415 (fset 'notmuch-folder-mode-map notmuch-folder-mode-map)
1417 (defcustom notmuch-folders (quote (("inbox" . "tag:inbox") ("unread" . "tag:unread")))
1418 "List of searches for the notmuch folder view"
1419 :type '(alist :key-type (string) :value-type (string))
1422 (defun notmuch-folder-mode ()
1423 "Major mode for showing notmuch 'folders'.
1425 This buffer contains a list of message counts returned by a
1426 customizable set of searches of your email archives. Each line in
1427 the buffer shows the name of a saved search and the resulting
1430 Pressing RET on any line opens a search window containing the
1431 results for the saved search on that line.
1433 Here is an example of how the search list could be
1434 customized, (the following text would be placed in your ~/.emacs
1437 (setq notmuch-folders '((\"inbox\" . \"tag:inbox\")
1438 (\"unread\" . \"tag:inbox AND tag:unread\")
1439 (\"notmuch\" . \"tag:inbox AND to:notmuchmail.org\")))
1441 Of course, you can have any number of folders, each configured
1442 with any supported search terms (see \"notmuch help search-terms\").
1444 Currently available key bindings:
1446 \\{notmuch-folder-mode-map}"
1448 (kill-all-local-variables)
1449 (use-local-map 'notmuch-folder-mode-map)
1450 (setq truncate-lines t)
1452 (setq major-mode 'notmuch-folder-mode
1453 mode-name "notmuch-folder")
1454 (setq buffer-read-only t))
1456 (defun notmuch-folder-next ()
1457 "Select the next folder in the list."
1463 (defun notmuch-folder-previous ()
1464 "Select the previous folder in the list."
1468 (defun notmuch-folder-first ()
1469 "Select the first folder in the list."
1471 (goto-char (point-min)))
1473 (defun notmuch-folder-last ()
1474 "Select the last folder in the list."
1476 (goto-char (point-max))
1479 (defun notmuch-folder-add (folders)
1481 (let ((name (car (car folders)))
1482 (inhibit-read-only t)
1483 (search (cdr (car folders))))
1486 (call-process notmuch-command nil t nil "count" search)
1487 (notmuch-folder-add (cdr folders)))))
1489 (defun notmuch-folder-find-name ()
1492 (let ((beg (point)))
1494 (filter-buffer-substring beg (point)))))
1496 (defun notmuch-folder-show-search (&optional folder)
1497 "Show a search window for the search related to the specified folder."
1500 (setq folder (notmuch-folder-find-name)))
1501 (let ((search (assoc folder notmuch-folders)))
1503 (notmuch-search (cdr search) notmuch-search-oldest-first))))
1506 (defun notmuch-folder ()
1507 "Show the notmuch folder view and update the displayed counts."
1509 (let ((buffer (get-buffer-create "*notmuch-folders*")))
1510 (switch-to-buffer buffer)
1511 (let ((inhibit-read-only t)
1512 (n (line-number-at-pos)))
1514 (notmuch-folder-mode)
1515 (notmuch-folder-add notmuch-folders)
1516 (goto-char (point-min))