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-stash-map
55 (let ((map (make-sparse-keymap)))
56 (define-key map "c" 'notmuch-show-stash-cc)
57 (define-key map "d" 'notmuch-show-stash-date)
58 (define-key map "F" 'notmuch-show-stash-filename)
59 (define-key map "f" 'notmuch-show-stash-from)
60 (define-key map "i" 'notmuch-show-stash-message-id)
61 (define-key map "s" 'notmuch-show-stash-subject)
62 (define-key map "T" 'notmuch-show-stash-tags)
63 (define-key map "t" 'notmuch-show-stash-to)
65 "Submap for stash commands"
68 (fset 'notmuch-show-stash-map notmuch-show-stash-map)
70 (defvar notmuch-show-mode-map
71 (let ((map (make-sparse-keymap)))
72 (define-key map "?" 'notmuch-help)
73 (define-key map "q" 'kill-this-buffer)
74 (define-key map (kbd "C-p") 'notmuch-show-previous-line)
75 (define-key map (kbd "C-n") 'notmuch-show-next-line)
76 (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
77 (define-key map (kbd "TAB") 'notmuch-show-next-button)
78 (define-key map "s" 'notmuch-search)
79 (define-key map "m" 'message-mail)
80 (define-key map "f" 'notmuch-show-forward-current)
81 (define-key map "r" 'notmuch-show-reply)
82 (define-key map "|" 'notmuch-show-pipe-message)
83 (define-key map "w" 'notmuch-show-save-attachments)
84 (define-key map "V" 'notmuch-show-view-raw-message)
85 (define-key map "v" 'notmuch-show-view-all-mime-parts)
86 (define-key map "c" 'notmuch-show-stash-map)
87 (define-key map "b" 'notmuch-show-toggle-current-body)
88 (define-key map "h" 'notmuch-show-toggle-current-header)
89 (define-key map "-" 'notmuch-show-remove-tag)
90 (define-key map "+" 'notmuch-show-add-tag)
91 (define-key map "X" 'notmuch-show-mark-read-then-archive-then-exit)
92 (define-key map "x" 'notmuch-show-archive-thread-then-exit)
93 (define-key map "A" 'notmuch-show-mark-read-then-archive-thread)
94 (define-key map "a" 'notmuch-show-archive-thread)
95 (define-key map "p" 'notmuch-show-previous-message)
96 (define-key map "N" 'notmuch-show-mark-read-then-next-open-message)
97 (define-key map "n" 'notmuch-show-next-message)
98 (define-key map (kbd "DEL") 'notmuch-show-rewind)
99 (define-key map " " 'notmuch-show-advance-marking-read-and-archiving)
101 "Keymap for \"notmuch show\" buffers.")
102 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
104 (defvar notmuch-show-signature-regexp "\\(-- ?\\|_+\\)$"
105 "Pattern to match a line that separates content from signature.
107 The regexp can (and should) include $ to match the end of the
108 line, but should not include ^ to match the beginning of the
109 line. This is because notmuch may have inserted additional space
110 for indentation at the beginning of the line. But notmuch will
111 move past the indentation when testing this pattern, (so that the
112 pattern can still test against the entire line).")
114 (defvar notmuch-show-signature-button-format
115 "[ %d-line signature. Click/Enter to toggle visibility. ]"
116 "String used to construct button text for hidden signatures
118 Can use up to one integer format parameter, i.e. %d")
120 (defvar notmuch-show-citation-button-format
121 "[ %d more citation lines. Click/Enter to toggle visibility. ]"
122 "String used to construct button text for hidden citations.
124 Can use up to one integer format parameter, i.e. %d")
126 (defvar notmuch-show-signature-lines-max 12
127 "Maximum length of signature that will be hidden by default.")
129 (defvar notmuch-show-citation-lines-prefix 4
130 "Always show at least this many lines of a citation.
132 If there is one more line, show that, otherwise collapse
133 remaining lines into a button.")
135 (defvar notmuch-command "notmuch"
136 "Command to run the notmuch binary.")
138 (defvar notmuch-show-message-begin-regexp "\fmessage{")
139 (defvar notmuch-show-message-end-regexp "\fmessage}")
140 (defvar notmuch-show-header-begin-regexp "\fheader{")
141 (defvar notmuch-show-header-end-regexp "\fheader}")
142 (defvar notmuch-show-body-begin-regexp "\fbody{")
143 (defvar notmuch-show-body-end-regexp "\fbody}")
144 (defvar notmuch-show-attachment-begin-regexp "\fattachment{")
145 (defvar notmuch-show-attachment-end-regexp "\fattachment}")
146 (defvar notmuch-show-part-begin-regexp "\fpart{")
147 (defvar notmuch-show-part-end-regexp "\fpart}")
148 (defvar notmuch-show-marker-regexp "\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
150 (defvar notmuch-show-id-regexp "\\(id:[^ ]*\\)")
151 (defvar notmuch-show-depth-match-regexp " depth:\\([0-9]*\\).*match:\\([01]\\) ")
152 (defvar notmuch-show-filename-regexp "filename:\\(.*\\)$")
153 (defvar notmuch-show-contentype-regexp "Content-type: \\(.*\\)")
155 (defvar notmuch-show-tags-regexp "(\\([^)]*\\))$")
157 (defvar notmuch-show-parent-buffer nil)
158 (defvar notmuch-show-body-read-visible nil)
159 (defvar notmuch-show-citations-visible nil)
160 (defvar notmuch-show-signatures-visible nil)
161 (defvar notmuch-show-headers-visible nil)
163 ; XXX: This should be a generic function in emacs somewhere, not here
164 (defun point-invisible-p ()
165 "Return whether the character at point is invisible.
167 Here visibility is determined by `buffer-invisibility-spec' and
168 the invisible property of any overlays for point. It doesn't have
169 anything to do with whether point is currently being displayed
170 within the current window."
171 (let ((prop (get-char-property (point) 'invisible)))
172 (if (eq buffer-invisibility-spec t)
174 (or (memq prop buffer-invisibility-spec)
175 (assq prop buffer-invisibility-spec)))))
177 (defun notmuch-select-tag-with-completion (prompt &rest search-terms)
179 (with-output-to-string
180 (with-current-buffer standard-output
181 (apply 'call-process notmuch-command nil t nil "search-tags" search-terms)))))
182 (completing-read prompt (split-string tag-list "\n+" t) nil nil nil)))
184 (defun notmuch-show-next-line ()
185 "Like builtin `next-line' but ensuring we end on a visible character.
187 By advancing forward until reaching a visible character.
189 Unlike builtin `next-line' this version accepts no arguments."
191 (set 'this-command 'next-line)
192 (call-interactively 'next-line)
193 (while (point-invisible-p)
196 (defun notmuch-show-previous-line ()
197 "Like builtin `previous-line' but ensuring we end on a visible character.
199 By advancing forward until reaching a visible character.
201 Unlike builtin `previous-line' this version accepts no arguments."
203 (set 'this-command 'previous-line)
204 (call-interactively 'previous-line)
205 (while (point-invisible-p)
208 (defun notmuch-show-get-message-id ()
211 (if (not (looking-at notmuch-show-message-begin-regexp))
212 (re-search-backward notmuch-show-message-begin-regexp))
213 (re-search-forward notmuch-show-id-regexp)
214 (buffer-substring-no-properties (match-beginning 1) (match-end 1))))
216 (defun notmuch-show-get-filename ()
219 (if (not (looking-at notmuch-show-message-begin-regexp))
220 (re-search-backward notmuch-show-message-begin-regexp))
221 (re-search-forward notmuch-show-filename-regexp)
222 (buffer-substring-no-properties (match-beginning 1) (match-end 1))))
224 (defun notmuch-show-set-tags (tags)
227 (if (not (looking-at notmuch-show-message-begin-regexp))
228 (re-search-backward notmuch-show-message-begin-regexp))
229 (re-search-forward notmuch-show-tags-regexp)
230 (let ((inhibit-read-only t)
231 (beg (match-beginning 1))
233 (delete-region beg end)
235 (insert (mapconcat 'identity tags " ")))))
237 (defun notmuch-show-get-tags ()
240 (if (not (looking-at notmuch-show-message-begin-regexp))
241 (re-search-backward notmuch-show-message-begin-regexp))
242 (re-search-forward notmuch-show-tags-regexp)
243 (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
245 (defun notmuch-show-get-bcc ()
246 "Return BCC address(es) of current message"
247 (notmuch-show-get-header-field 'bcc))
249 (defun notmuch-show-get-cc ()
250 "Return CC address(es) of current message"
251 (notmuch-show-get-header-field 'cc))
253 (defun notmuch-show-get-date ()
254 "Return Date of current message"
255 (notmuch-show-get-header-field 'date))
257 (defun notmuch-show-get-from ()
258 "Return From address of current message"
259 (notmuch-show-get-header-field 'from))
261 (defun notmuch-show-get-subject ()
262 "Return Subject of current message"
263 (notmuch-show-get-header-field 'subject))
265 (defun notmuch-show-get-to ()
266 "Return To address(es) of current message"
267 (notmuch-show-get-header-field 'to))
269 (defun notmuch-show-get-header-field (name)
270 "Retrieve the header field NAME from the current message.
271 NAME should be a symbol, in lower case, as returned by
272 mail-header-extract-no-properties"
273 (let* ((result (assoc name (notmuch-show-get-header)))
274 (val (and result (cdr result))))
277 (defun notmuch-show-get-header ()
278 "Retrieve and parse the header from the current message. Returns an alist with of (header . value)
279 where header is a symbol and value is a string. The summary from notmuch-show is returned as the
280 pseudoheader summary"
281 (require 'mailheader)
284 (if (not (looking-at notmuch-show-message-begin-regexp))
285 (re-search-backward notmuch-show-message-begin-regexp))
286 (re-search-forward (concat notmuch-show-header-begin-regexp "\n[[:space:]]*\\(.*\\)\n"))
287 (let* ((summary (buffer-substring-no-properties (match-beginning 1) (match-end 1)))
289 (re-search-forward notmuch-show-header-end-regexp)
290 (let ((text (buffer-substring beg (match-beginning 0))))
293 (goto-char (point-min))
294 (while (looking-at "\\([[:space:]]*\\)[A-Za-z][-A-Za-z0-9]*:")
295 (delete-region (match-beginning 1) (match-end 1))
298 (goto-char (point-min))
299 (cons (cons 'summary summary) (mail-header-extract-no-properties)))))))
301 (defun notmuch-show-add-tag (&rest toadd)
302 "Add a tag to the current message."
304 (list (notmuch-select-tag-with-completion "Tag to add: ")))
305 (apply 'notmuch-call-notmuch-process
307 (mapcar (lambda (s) (concat "+" s)) toadd))
308 (cons (notmuch-show-get-message-id) nil)))
309 (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
311 (defun notmuch-show-remove-tag (&rest toremove)
312 "Remove a tag from the current message."
314 (list (notmuch-select-tag-with-completion "Tag to remove: " (notmuch-show-get-message-id))))
315 (let ((tags (notmuch-show-get-tags)))
316 (if (intersection tags toremove :test 'string=)
318 (apply 'notmuch-call-notmuch-process
320 (mapcar (lambda (s) (concat "-" s)) toremove))
321 (cons (notmuch-show-get-message-id) nil)))
322 (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
324 (defun notmuch-show-archive-thread-maybe-mark-read (markread)
326 (goto-char (point-min))
329 (notmuch-show-remove-tag "unread" "inbox")
330 (notmuch-show-remove-tag "inbox"))
333 (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
334 (goto-char (point-max)))))
335 (let ((parent-buffer notmuch-show-parent-buffer))
339 (switch-to-buffer parent-buffer)
341 (notmuch-search-show-thread)))))
343 (defun notmuch-show-mark-read-then-archive-thread ()
344 "Remove unread tags from thread, then archive and show next thread.
346 Archive each message currently shown by removing the \"unread\"
347 and \"inbox\" tag from each. Then kill this buffer and show the
348 next thread from the search from which this thread was originally
351 Note: This command is safe from any race condition of new messages
352 being delivered to the same thread. It does not archive the
353 entire thread, but only the messages shown in the current
356 (notmuch-show-archive-thread-maybe-mark-read t))
358 (defun notmuch-show-archive-thread ()
359 "Archive each message in thread, then show next thread from search.
361 Archive each message currently shown by removing the \"inbox\"
362 tag from each. Then kill this buffer and show the next thread
363 from the search from which this thread was originally shown.
365 Note: This command is safe from any race condition of new messages
366 being delivered to the same thread. It does not archive the
367 entire thread, but only the messages shown in the current
370 (notmuch-show-archive-thread-maybe-mark-read nil))
372 (defun notmuch-show-archive-thread-then-exit ()
373 "Archive each message in thread, then exit back to search results."
375 (notmuch-show-archive-thread)
378 (defun notmuch-show-mark-read-then-archive-then-exit ()
379 "Remove unread tags from thread, then archive and exit to search results."
381 (notmuch-show-mark-read-then-archive-thread)
384 (defun notmuch-show-view-raw-message ()
385 "View the raw email of the current message."
387 (view-file (notmuch-show-get-filename)))
389 (defmacro with-current-notmuch-show-message (&rest body)
390 "Evaluate body with current buffer set to the text of current message"
392 (let ((filename (notmuch-show-get-filename)))
393 (let ((buf (generate-new-buffer (concat "*notmuch-msg-" filename "*"))))
394 (with-current-buffer buf
395 (insert-file-contents filename nil nil nil t)
397 (kill-buffer buf)))))
399 (defun notmuch-show-view-all-mime-parts ()
400 "Use external viewers to view all attachments from the current message."
402 (with-current-notmuch-show-message
403 ; We ovverride the mm-inline-media-tests to indicate which message
404 ; parts are already sufficiently handled by the original
405 ; presentation of the message in notmuch-show mode. These parts
406 ; will be inserted directly into the temporary buffer of
407 ; with-current-notmuch-show-message and silently discarded.
409 ; Any MIME part not explicitly mentioned here will be handled by an
410 ; external viewer as configured in the various mailcap files.
411 (let ((mm-inline-media-tests '(
412 ("text/.*" ignore identity)
413 ("application/pgp-signature" ignore identity)
414 ("multipart/alternative" ignore identity)
415 ("multipart/mixed" ignore identity)
416 ("multipart/related" ignore identity)
418 (mm-display-parts (mm-dissect-buffer)))))
420 (defun notmuch-foreach-mime-part (function mm-handle)
421 (cond ((stringp (car mm-handle))
422 (dolist (part (cdr mm-handle))
423 (notmuch-foreach-mime-part function part)))
424 ((bufferp (car mm-handle))
425 (funcall function mm-handle))
426 (t (dolist (part mm-handle)
427 (notmuch-foreach-mime-part function part)))))
429 (defun notmuch-count-attachments (mm-handle)
431 (notmuch-foreach-mime-part
433 (let ((disposition (mm-handle-disposition p)))
434 (and (listp disposition)
435 (or (equal (car disposition) "attachment")
436 (and (equal (car disposition) "inline")
437 (assq 'filename disposition)))
442 (defun notmuch-save-attachments (mm-handle &optional queryp)
443 (notmuch-foreach-mime-part
445 (let ((disposition (mm-handle-disposition p)))
446 (and (listp disposition)
447 (or (equal (car disposition) "attachment")
448 (and (equal (car disposition) "inline")
449 (assq 'filename disposition)))
452 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
456 (defun notmuch-show-save-attachments ()
457 "Save all attachments from the current message."
459 (with-current-notmuch-show-message
460 (let ((mm-handle (mm-dissect-buffer)))
461 (notmuch-save-attachments
462 mm-handle (> (notmuch-count-attachments mm-handle) 1))))
465 (defun notmuch-reply (query-string)
466 (switch-to-buffer (generate-new-buffer "notmuch-draft"))
467 (call-process notmuch-command nil t nil "reply" query-string)
468 (message-insert-signature)
469 (goto-char (point-min))
470 (if (re-search-forward "^$" nil t)
472 (insert "--text follows this line--")
476 (defun notmuch-show-reply ()
477 "Begin composing a reply to the current message in a new buffer."
479 (let ((message-id (notmuch-show-get-message-id)))
480 (notmuch-reply message-id)))
482 (defun notmuch-show-forward-current ()
483 "Forward the current message."
485 (with-current-notmuch-show-message
488 (defun notmuch-show-pipe-message (command)
489 "Pipe the contents of the current message to the given command.
491 The given command will be executed with the raw contents of the
492 current email message as stdin. Anything printed by the command
493 to stdout or stderr will appear in the *Messages* buffer."
494 (interactive "sPipe message to command: ")
495 (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*"
496 (list command " < " (shell-quote-argument (notmuch-show-get-filename)))))
498 (defun notmuch-show-move-to-current-message-summary-line ()
499 "Move to the beginning of the one-line summary of the current message.
501 This gives us a stable place to move to and work from since the
502 summary line is always visible. This is important since moving to
503 an invisible location is unreliable, (the main command loop moves
504 point either forward or backward to the next visible character
505 when a command ends with point on an invisible character).
507 Emits an error if point is not within a valid message, (that is
508 no pattern of `notmuch-show-message-begin-regexp' could be found
509 by searching backward)."
511 (if (not (looking-at notmuch-show-message-begin-regexp))
512 (if (re-search-backward notmuch-show-message-begin-regexp nil t)
514 (error "Not within a valid message."))
517 (defun notmuch-show-last-message-p ()
518 "Predicate testing whether point is within the last message."
519 (save-window-excursion
521 (notmuch-show-move-to-current-message-summary-line)
522 (not (re-search-forward notmuch-show-message-begin-regexp nil t)))))
524 (defun notmuch-show-message-unread-p ()
525 "Predicate testing whether current message is unread."
526 (member "unread" (notmuch-show-get-tags)))
528 (defun notmuch-show-message-open-p ()
529 "Predicate testing whether current message is open (body is visible)."
530 (let ((btn (previous-button (point) t)))
531 (while (not (button-has-type-p btn 'notmuch-button-body-toggle-type))
532 (setq btn (previous-button (button-start btn))))
533 (not (invisible-p (button-get btn 'invisibility-spec)))))
535 (defun notmuch-show-next-message ()
536 "Advance to the beginning of the next message in the buffer.
538 Moves to the last visible character of the current message if
539 already on the last message in the buffer.
541 Returns nil if already on the last message in the buffer."
543 (notmuch-show-move-to-current-message-summary-line)
544 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
546 (notmuch-show-move-to-current-message-summary-line)
549 (goto-char (- (point-max) 1))
550 (while (point-invisible-p)
555 (defun notmuch-show-find-next-message ()
556 "Returns the position of the next message in the buffer.
558 Or the position of the last visible character of the current
559 message if already within the last message in the buffer."
560 ; save-excursion doesn't save our window position
561 ; save-window-excursion doesn't save point
562 ; Looks like we have to use both.
564 (save-window-excursion
565 (notmuch-show-next-message)
568 (defun notmuch-show-next-unread-message ()
569 "Advance to the beginning of the next unread message in the buffer.
571 Moves to the last visible character of the current message if
572 there are no more unread messages past the current point."
573 (notmuch-show-next-message)
574 (while (and (not (notmuch-show-last-message-p))
575 (not (notmuch-show-message-unread-p)))
576 (notmuch-show-next-message))
577 (if (not (notmuch-show-message-unread-p))
578 (notmuch-show-next-message)))
580 (defun notmuch-show-next-open-message ()
581 "Advance to the next open message (that is, body is not invisible)."
582 (while (and (notmuch-show-next-message)
583 (not (notmuch-show-message-open-p)))))
585 (defun notmuch-show-previous-message ()
586 "Backup to the beginning of the previous message in the buffer.
588 If within a message rather than at the beginning of it, then
589 simply move to the beginning of the current message."
591 (let ((start (point)))
592 (notmuch-show-move-to-current-message-summary-line)
593 (if (not (< (point) start))
594 ; Go backward twice to skip the current message's marker
596 (re-search-backward notmuch-show-message-begin-regexp nil t)
597 (re-search-backward notmuch-show-message-begin-regexp nil t)
598 (notmuch-show-move-to-current-message-summary-line)
602 (defun notmuch-show-find-previous-message ()
603 "Returns the position of the previous message in the buffer.
605 Or the position of the beginning of the current message if point
606 is originally within the message rather than at the beginning of
608 ; save-excursion doesn't save our window position
609 ; save-window-excursion doesn't save point
610 ; Looks like we have to use both.
612 (save-window-excursion
613 (notmuch-show-previous-message)
616 (defun notmuch-show-mark-read-then-next-open-message ()
617 "Remove unread tag from this message, then advance to next open message."
619 (notmuch-show-remove-tag "unread")
620 (notmuch-show-next-open-message))
622 (defun notmuch-show-rewind ()
623 "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-marking-read-and-archiving]).
625 Specifically, if the beginning of the previous email is fewer
626 than `window-height' lines from the current point, move to it
627 just like `notmuch-show-previous-message'.
629 Otherwise, just scroll down a screenful of the current message.
631 This command does not modify any message tags, (it does not undo
632 any effects from previous calls to
633 `notmuch-show-advance-marking-read-and-archiving'."
635 (let ((previous (notmuch-show-find-previous-message)))
636 (if (> (count-lines previous (point)) (- (window-height) next-screen-context-lines))
640 ((beginning-of-buffer) nil))
641 (goto-char (window-start)))
642 (notmuch-show-previous-message))))
644 (defun notmuch-show-advance-marking-read-and-archiving ()
645 "Advance through thread, marking read and archiving.
647 This command is intended to be one of the simplest ways to
648 process a thread of email. It does the following:
650 If the current message in the thread is not yet fully visible,
651 scroll by a near screenful to read more of the message.
653 Otherwise, (the end of the current message is already within the
654 current window), remove the \"unread\" tag (if present) from the
655 current message and advance to the next open message.
657 Finally, if there is no further message to advance to, and this
658 last message is already read, then archive the entire current
659 thread, (remove the \"inbox\" tag from each message). Also kill
660 this buffer, and display the next thread from the search from
661 which this thread was originally shown."
663 (let ((next (notmuch-show-find-next-message))
664 (unread (notmuch-show-message-unread-p)))
665 (if (> next (window-end))
667 (let ((last (notmuch-show-last-message-p)))
668 (notmuch-show-mark-read-then-next-open-message)
670 (notmuch-show-archive-thread))))))
672 (defun notmuch-show-next-button ()
673 "Advance point to the next button in the buffer."
677 (defun notmuch-show-previous-button ()
678 "Move point back to the previous button in the buffer."
682 (defun notmuch-toggle-invisible-action (cite-button)
683 (let ((invis-spec (button-get cite-button 'invisibility-spec)))
684 (if (invisible-p invis-spec)
685 (remove-from-invisibility-spec invis-spec)
686 (add-to-invisibility-spec invis-spec)
688 (force-window-update)
691 (defun notmuch-show-toggle-current-body ()
692 "Toggle the display of the current message body."
695 (notmuch-show-move-to-current-message-summary-line)
696 (unless (button-at (point))
697 (notmuch-show-next-button))
701 (defun notmuch-show-toggle-current-header ()
702 "Toggle the display of the current message header."
705 (notmuch-show-move-to-current-message-summary-line)
707 (unless (button-at (point))
708 (notmuch-show-next-button))
712 (define-button-type 'notmuch-button-invisibility-toggle-type
713 'action 'notmuch-toggle-invisible-action
715 'face 'font-lock-comment-face)
716 (define-button-type 'notmuch-button-citation-toggle-type 'help-echo "mouse-1, RET: Show citation"
717 :supertype 'notmuch-button-invisibility-toggle-type)
718 (define-button-type 'notmuch-button-signature-toggle-type 'help-echo "mouse-1, RET: Show signature"
719 :supertype 'notmuch-button-invisibility-toggle-type)
720 (define-button-type 'notmuch-button-headers-toggle-type 'help-echo "mouse-1, RET: Show headers"
721 :supertype 'notmuch-button-invisibility-toggle-type)
722 (define-button-type 'notmuch-button-body-toggle-type
723 'help-echo "mouse-1, RET: Show message"
724 'face 'notmuch-message-summary-face
725 :supertype 'notmuch-button-invisibility-toggle-type)
727 (defun notmuch-show-citation-regexp (depth)
728 "Build a regexp for matching citations at a given DEPTH (indent)"
729 (let ((line-regexp (format "[[:space:]]\\{%d\\}>.*\n" depth)))
730 (concat "\\(?:^" line-regexp
731 "\\(?:[[:space:]]*\n" line-regexp
734 (defun notmuch-show-region-to-button (beg end type prefix button-text)
735 "Auxilary function to do the actual making of overlays and buttons
737 BEG and END are buffer locations. TYPE should a string, either
738 \"citation\" or \"signature\". PREFIX is some arbitrary text to
739 insert before the button, probably for indentation. BUTTON-TEXT
740 is what to put on the button."
742 ;; This uses some slightly tricky conversions between strings and
743 ;; symbols because of the way the button code works. Note that
744 ;; replacing intern-soft with make-symbol will cause this to fail,
745 ;; since the newly created symbol has no plist.
747 (let ((overlay (make-overlay beg end))
748 (invis-spec (make-symbol (concat "notmuch-" type "-region")))
749 (button-type (intern-soft (concat "notmuch-button-"
750 type "-toggle-type"))))
751 (add-to-invisibility-spec invis-spec)
752 (overlay-put overlay 'invisible invis-spec)
757 (insert-button button-text
758 'invisibility-spec invis-spec
763 (defun notmuch-show-markup-citations-region (beg end depth)
764 "Markup citations, and up to one signature in the given region"
765 ;; it would be nice if the untabify was not required, but
766 ;; that would require notmuch to indent with spaces.
768 (let ((citation-regexp (notmuch-show-citation-regexp depth))
769 (signature-regexp (concat (format "^[[:space:]]\\{%d\\}" depth)
770 notmuch-show-signature-regexp))
771 (indent (concat "\n" (make-string depth ? ))))
774 (while (and (< (point) end)
775 (re-search-forward citation-regexp end t))
776 (let* ((cite-start (match-beginning 0))
777 (cite-end (match-end 0))
778 (cite-lines (count-lines cite-start cite-end)))
779 (when (> cite-lines (1+ notmuch-show-citation-lines-prefix))
780 (goto-char cite-start)
781 (forward-line notmuch-show-citation-lines-prefix)
782 (notmuch-show-region-to-button
786 (format notmuch-show-citation-button-format
787 (- cite-lines notmuch-show-citation-lines-prefix))
789 (if (and (< (point) end)
790 (re-search-forward signature-regexp end t))
791 (let* ((sig-start (match-beginning 0))
792 (sig-end (match-end 0))
793 (sig-lines (1- (count-lines sig-start end))))
794 (if (<= sig-lines notmuch-show-signature-lines-max)
795 (notmuch-show-region-to-button
800 (format notmuch-show-signature-button-format sig-lines)
803 (defun notmuch-show-markup-part (beg end depth)
804 (if (re-search-forward notmuch-show-part-begin-regexp nil t)
806 (let (mime-message mime-type)
808 (re-search-forward notmuch-show-contentype-regexp end t)
809 (setq mime-type (car (split-string (buffer-substring
810 (match-beginning 1) (match-end 1))))))
812 (if (equal mime-type "text/html")
813 (let ((filename (notmuch-show-get-filename)))
815 (insert-file-contents filename nil nil nil t)
816 (setq mime-message (mm-dissect-buffer)))))
818 (let ((beg (point-marker)))
819 (re-search-forward notmuch-show-part-end-regexp)
820 (let ((end (copy-marker (match-beginning 0))))
824 (indent-rigidly beg end depth)
825 (if (not (eq mime-message nil))
829 (let ((handle-type (mm-handle-type mime-message))
831 (if (sequencep (car handle-type))
832 (setq mime-type (car handle-type))
833 (setq mime-type (car (car (cdr handle-type))))
835 (if (equal mime-type "text/html")
836 (mm-display-part mime-message))))
838 (notmuch-show-markup-citations-region beg end depth)
839 ; Advance to the next part (if any) (so the outer loop can
840 ; determine whether we've left the current message.
841 (if (re-search-forward notmuch-show-part-begin-regexp nil t)
842 (beginning-of-line)))))
846 (defun notmuch-show-markup-parts-region (beg end depth)
849 (while (< (point) end)
850 (notmuch-show-markup-part beg end depth))))
852 (defun notmuch-show-markup-body (depth match btn)
853 "Markup a message body, (indenting, buttonizing citations,
854 etc.), and conditionally hiding the body itself if the message
855 has been read and does not match the current search.
857 DEPTH specifies the depth at which this message appears in the
858 tree of the current thread, (the top-level messages have depth 0
859 and each reply increases depth by 1). MATCH indicates whether
860 this message is regarded as matching the current search. BTN is
861 the button which is used to toggle the visibility of this
864 When this function is called, point must be within the message, but
865 before the delimiter marking the beginning of the body."
866 (re-search-forward notmuch-show-body-begin-regexp)
868 (let ((beg (point-marker)))
869 (re-search-forward notmuch-show-body-end-regexp)
870 (let ((end (copy-marker (match-beginning 0))))
871 (notmuch-show-markup-parts-region beg end depth)
872 (let ((invis-spec (make-symbol "notmuch-show-body-read")))
873 (overlay-put (make-overlay beg end)
874 'invisible invis-spec)
875 (button-put btn 'invisibility-spec invis-spec)
876 (if (not (or (notmuch-show-message-unread-p) match))
877 (add-to-invisibility-spec invis-spec)))
882 (defun notmuch-fontify-headers ()
883 (while (looking-at "[[:space:]]")
885 (if (looking-at "[Tt]o:")
887 (overlay-put (make-overlay (point) (re-search-forward ":"))
888 'face 'message-header-name)
889 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
890 'face 'message-header-to))
891 (if (looking-at "[B]?[Cc][Cc]:")
893 (overlay-put (make-overlay (point) (re-search-forward ":"))
894 'face 'message-header-name)
895 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
896 'face 'message-header-cc))
897 (if (looking-at "[Ss]ubject:")
899 (overlay-put (make-overlay (point) (re-search-forward ":"))
900 'face 'message-header-name)
901 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
902 'face 'message-header-subject))
903 (if (looking-at "[Ff]rom:")
905 (overlay-put (make-overlay (point) (re-search-forward ":"))
906 'face 'message-header-name)
907 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
908 'face 'message-header-other)))))))
910 (defun notmuch-show-markup-header (message-begin depth)
911 "Buttonize and decorate faces in a message header.
913 MESSAGE-BEGIN is the position of the absolute first character in
914 the message (including all delimiters that will end up being
915 invisible etc.). This is to allow a button to reliably extend to
916 the beginning of the message even if point is positioned at an
917 invisible character (such as the beginning of the buffer).
919 DEPTH specifies the depth at which this message appears in the
920 tree of the current thread, (the top-level messages have depth 0
921 and each reply increases depth by 1)."
922 (re-search-forward notmuch-show-header-begin-regexp)
924 (let ((beg (point-marker))
925 (summary-end (copy-marker (line-beginning-position 2)))
926 (subject-end (copy-marker (line-end-position 2)))
927 (invis-spec (make-symbol "notmuch-show-header"))
929 (re-search-forward notmuch-show-header-end-regexp)
931 (let ((end (point-marker)))
932 (indent-rigidly beg end depth)
934 (setq btn (make-button message-begin summary-end :type 'notmuch-button-body-toggle-type))
936 (add-to-invisibility-spec invis-spec)
937 (overlay-put (make-overlay subject-end end)
938 'invisible invis-spec)
939 (make-button (line-beginning-position) subject-end
940 'invisibility-spec invis-spec
941 :type 'notmuch-button-headers-toggle-type)
942 (while (looking-at "[[:space:]]*[A-Za-z][-A-Za-z0-9]*:")
944 (notmuch-fontify-headers)
950 (set-marker summary-end nil)
951 (set-marker subject-end nil)
956 (defun notmuch-show-markup-message ()
957 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
958 (let ((message-begin (match-beginning 0)))
959 (re-search-forward notmuch-show-depth-match-regexp)
960 (let ((depth (string-to-number (buffer-substring (match-beginning 1) (match-end 1))))
961 (match (string= "1" (buffer-substring (match-beginning 2) (match-end 2))))
963 (setq btn (notmuch-show-markup-header message-begin depth))
964 (notmuch-show-markup-body depth match btn)))
965 (goto-char (point-max))))
967 (defun notmuch-show-hide-markers ()
969 (goto-char (point-min))
971 (if (re-search-forward notmuch-show-marker-regexp nil t)
973 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
974 'invisible 'notmuch-show-marker))
975 (goto-char (point-max))))))
977 (defun notmuch-show-markup-messages ()
979 (goto-char (point-min))
981 (notmuch-show-markup-message)))
982 (notmuch-show-hide-markers))
984 (defun notmuch-documentation-first-line (symbol)
985 "Return the first line of the documentation string for SYMBOL."
986 (let ((doc (documentation symbol)))
989 (insert (documentation symbol t))
990 (goto-char (point-min))
993 (buffer-substring beg (point))))
996 (defun notmuch-prefix-key-description (key)
997 "Given a prefix key code, return a human-readable string representation.
999 This is basically just `format-kbd-macro' but we also convert ESC to M-."
1000 (let ((desc (format-kbd-macro (vector key))))
1001 (if (string= desc "ESC")
1003 (concat desc " "))))
1005 ; I would think that emacs would have code handy for walking a keymap
1006 ; and generating strings for each key, and I would prefer to just call
1007 ; that. But I couldn't find any (could be all implemented in C I
1008 ; suppose), so I wrote my own here.
1009 (defun notmuch-substitute-one-command-key-with-prefix (prefix binding)
1010 "For a key binding, return a string showing a human-readable
1011 representation of the prefixed key as well as the first line of
1012 documentation from the bound function.
1014 For a mouse binding, return nil."
1015 (let ((key (car binding))
1016 (action (cdr binding)))
1017 (if (mouse-event-p key)
1019 (if (keymapp action)
1020 (let ((substitute (apply-partially 'notmuch-substitute-one-command-key-with-prefix (notmuch-prefix-key-description key)))
1022 (map-keymap (lambda (a b)
1023 (push (cons a b) as-list))
1025 (mapconcat substitute as-list "\n"))
1026 (concat prefix (format-kbd-macro (vector key))
1028 (notmuch-documentation-first-line action))))))
1030 (defalias 'notmuch-substitute-one-command-key
1031 (apply-partially 'notmuch-substitute-one-command-key-with-prefix nil))
1033 (defun notmuch-substitute-command-keys (doc)
1034 "Like `substitute-command-keys' but with documentation, not function names."
1036 (while (string-match "\\\\{\\([^}[:space:]]*\\)}" doc beg)
1037 (let ((map (substring doc (match-beginning 1) (match-end 1))))
1038 (setq doc (replace-match (mapconcat 'notmuch-substitute-one-command-key
1039 (cdr (symbol-value (intern map))) "\n") 1 1 doc)))
1040 (setq beg (match-end 0)))
1043 (defun notmuch-help ()
1044 "Display help for the current notmuch mode."
1046 (let* ((mode major-mode)
1047 (doc (substitute-command-keys (notmuch-substitute-command-keys (documentation mode t)))))
1048 (with-current-buffer (generate-new-buffer "*notmuch-help*")
1050 (goto-char (point-min))
1051 (set-buffer-modified-p nil)
1052 (view-buffer (current-buffer) 'kill-buffer-if-not-modified))))
1055 (defun notmuch-show-mode ()
1056 "Major mode for viewing a thread with notmuch.
1058 This buffer contains the results of the \"notmuch show\" command
1059 for displaying a single thread of email from your email archives.
1061 By default, various components of email messages, (citations,
1062 signatures, already-read messages), are hidden. You can make
1063 these parts visible by clicking with the mouse button or by
1064 pressing RET after positioning the cursor on a hidden part, (for
1065 which \\[notmuch-show-next-button] and \\[notmuch-show-previous-button] are helpful).
1067 Reading the thread sequentially is well-supported by pressing
1068 \\[notmuch-show-advance-marking-read-and-archiving]. This will scroll the current message (if necessary),
1069 advance to the next message, or advance to the next thread (if
1070 already on the last message of a thread). As each message is
1071 scrolled away its \"unread\" tag will be removed, and as each
1072 thread is scrolled away the \"inbox\" tag will be removed from
1073 each message in the thread.
1075 Other commands are available to read or manipulate the thread more
1076 selectively, (such as '\\[notmuch-show-next-message]' and '\\[notmuch-show-previous-message]' to advance to messages without
1077 removing any tags, and '\\[notmuch-show-archive-thread]' to archive an entire thread without
1078 scrolling through with \\[notmuch-show-advance-marking-read-and-archiving]).
1080 You can add or remove arbitary tags from the current message with
1081 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
1083 All currently available key bindings:
1085 \\{notmuch-show-mode-map}"
1087 (kill-all-local-variables)
1088 (add-to-invisibility-spec 'notmuch-show-marker)
1089 (use-local-map notmuch-show-mode-map)
1090 (setq major-mode 'notmuch-show-mode
1091 mode-name "notmuch-show")
1092 (setq buffer-read-only t))
1094 (defgroup notmuch nil
1095 "Notmuch mail reader for Emacs."
1098 (defcustom notmuch-show-hook nil
1099 "List of functions to call when notmuch displays a message."
1101 :options '(goto-address)
1104 (defcustom notmuch-search-hook nil
1105 "List of functions to call when notmuch displays the search results."
1107 :options '(hl-line-mode)
1110 (defun notmuch-show-do-stash (text)
1112 (message (concat "Saved: " text)))
1114 (defun notmuch-show-stash-cc ()
1115 "Copy CC field of current message to kill-ring."
1117 (notmuch-show-do-stash (notmuch-show-get-cc)))
1119 (defun notmuch-show-stash-date ()
1120 "Copy date of current message to kill-ring."
1122 (notmuch-show-do-stash (notmuch-show-get-date)))
1124 (defun notmuch-show-stash-filename ()
1125 "Copy filename of current message to kill-ring."
1127 (notmuch-show-do-stash (notmuch-show-get-filename)))
1129 (defun notmuch-show-stash-from ()
1130 "Copy From address of current message to kill-ring."
1132 (notmuch-show-do-stash (notmuch-show-get-from)))
1134 (defun notmuch-show-stash-message-id ()
1135 "Copy message ID of current message to kill-ring."
1137 (notmuch-show-do-stash (notmuch-show-get-message-id)))
1139 (defun notmuch-show-stash-subject ()
1140 "Copy Subject field of current message to kill-ring."
1142 (notmuch-show-do-stash (notmuch-show-get-subject)))
1144 (defun notmuch-show-stash-tags ()
1145 "Copy tags of current message to kill-ring as a comma separated list."
1147 (notmuch-show-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1149 (defun notmuch-show-stash-to ()
1150 "Copy To address of current message to kill-ring."
1152 (notmuch-show-do-stash (notmuch-show-get-to)))
1154 ; Make show mode a bit prettier, highlighting URLs and using word wrap
1156 (defun notmuch-show-pretty-hook ()
1157 (goto-address-mode 1)
1160 (add-hook 'notmuch-show-hook 'notmuch-show-pretty-hook)
1161 (add-hook 'notmuch-search-hook
1165 (defun notmuch-show (thread-id &optional parent-buffer query-context)
1166 "Run \"notmuch show\" with the given thread ID and display results.
1168 The optional PARENT-BUFFER is the notmuch-search buffer from
1169 which this notmuch-show command was executed, (so that the next
1170 thread from that buffer can be show when done with this one).
1172 The optional QUERY-CONTEXT is a notmuch search term. Only messages from the thread
1173 matching this search term are shown if non-nil. "
1174 (interactive "sNotmuch show: ")
1175 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
1176 (switch-to-buffer buffer)
1178 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
1179 (let ((proc (get-buffer-process (current-buffer)))
1180 (inhibit-read-only t))
1182 (error "notmuch search process already running for query `%s'" thread-id)
1185 (goto-char (point-min))
1187 (let* ((basic-args (list notmuch-command nil t nil "show" "--entire-thread" thread-id))
1188 (args (if query-context (append basic-args (list "and (" query-context ")")) basic-args)))
1189 (apply 'call-process args)
1190 (when (and (eq (buffer-size) 0) query-context)
1191 (apply 'call-process basic-args)))
1192 (notmuch-show-markup-messages)
1194 (run-hooks 'notmuch-show-hook)
1195 ; Move straight to the first open message
1196 (if (not (notmuch-show-message-open-p))
1197 (notmuch-show-next-open-message))
1200 (defvar notmuch-search-authors-width 40
1201 "Number of columns to use to display authors in a notmuch-search buffer.")
1203 (defvar notmuch-search-mode-map
1204 (let ((map (make-sparse-keymap)))
1205 (define-key map "?" 'notmuch-help)
1206 (define-key map "q" 'kill-this-buffer)
1207 (define-key map "x" 'kill-this-buffer)
1208 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
1209 (define-key map "b" 'notmuch-search-scroll-down)
1210 (define-key map " " 'notmuch-search-scroll-up)
1211 (define-key map "<" 'notmuch-search-first-thread)
1212 (define-key map ">" 'notmuch-search-last-thread)
1213 (define-key map "p" 'notmuch-search-previous-thread)
1214 (define-key map "n" 'notmuch-search-next-thread)
1215 (define-key map "r" 'notmuch-search-reply-to-thread)
1216 (define-key map "m" 'message-mail)
1217 (define-key map "s" 'notmuch-search)
1218 (define-key map "o" 'notmuch-search-toggle-order)
1219 (define-key map "=" 'notmuch-search-refresh-view)
1220 (define-key map "t" 'notmuch-search-filter-by-tag)
1221 (define-key map "f" 'notmuch-search-filter)
1222 (define-key map [mouse-1] 'notmuch-search-show-thread)
1223 (define-key map "*" 'notmuch-search-operate-all)
1224 (define-key map "a" 'notmuch-search-archive-thread)
1225 (define-key map "-" 'notmuch-search-remove-tag)
1226 (define-key map "+" 'notmuch-search-add-tag)
1227 (define-key map (kbd "RET") 'notmuch-search-show-thread)
1229 "Keymap for \"notmuch search\" buffers.")
1230 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
1232 (defvar notmuch-search-query-string)
1233 (defvar notmuch-search-oldest-first t
1234 "Show the oldest mail first in the search-mode")
1236 (defvar notmuch-search-disjunctive-regexp "\\<[oO][rR]\\>")
1238 (defun notmuch-search-scroll-up ()
1239 "Move forward through search results by one window's worth."
1243 ((end-of-buffer) (notmuch-search-last-thread))))
1245 (defun notmuch-search-scroll-down ()
1246 "Move backward through the search results by one window's worth."
1248 ; I don't know why scroll-down doesn't signal beginning-of-buffer
1249 ; the way that scroll-up signals end-of-buffer, but c'est la vie.
1251 ; So instead of trapping a signal we instead check whether the
1252 ; window begins on the first line of the buffer and if so, move
1253 ; directly to that position. (We have to count lines since the
1254 ; window-start position is not the same as point-min due to the
1255 ; invisible thread-ID characters on the first line.
1256 (if (equal (count-lines (point-min) (window-start)) 0)
1257 (goto-char (point-min))
1260 (defun notmuch-search-next-thread ()
1261 "Select the next thread in the search results."
1265 (defun notmuch-search-previous-thread ()
1266 "Select the previous thread in the search results."
1270 (defun notmuch-search-last-thread ()
1271 "Select the last thread in the search results."
1273 (goto-char (point-max))
1276 (defun notmuch-search-first-thread ()
1277 "Select the first thread in the search results."
1279 (goto-char (point-min)))
1281 (defface notmuch-message-summary-face
1282 '((((class color) (background light)) (:background "#f0f0f0"))
1283 (((class color) (background dark)) (:background "#303030")))
1284 "Face for the single-line message summary in notmuch-show-mode."
1287 (defface notmuch-tag-face
1290 (:foreground "OliveDrab1"))
1293 (:foreground "navy blue" :bold t))
1296 "Notmuch search mode face used to highligh tags."
1299 (defvar notmuch-tag-face-alist nil
1300 "List containing the tag list that need to be highlighed")
1302 (defvar notmuch-search-font-lock-keywords nil)
1305 (defun notmuch-search-mode ()
1306 "Major mode displaying results of a notmuch search.
1308 This buffer contains the results of a \"notmuch search\" of your
1309 email archives. Each line in the buffer represents a single
1310 thread giving a summary of the thread (a relative date, the
1311 number of matched messages and total messages in the thread,
1312 participants in the thread, a representative subject line, and
1315 Pressing \\[notmuch-search-show-thread] on any line displays that thread. The '\\[notmuch-search-add-tag]' and '\\[notmuch-search-remove-tag]'
1316 keys can be used to add or remove tags from a thread. The '\\[notmuch-search-archive-thread]' key
1317 is a convenience for archiving a thread (removing the \"inbox\"
1318 tag). The '\\[notmuch-search-operate-all]' key can be used to add or remove a tag from all
1319 threads in the current buffer.
1321 Other useful commands are '\\[notmuch-search-filter]' for filtering the current search
1322 based on an additional query string, '\\[notmuch-search-filter-by-tag]' for filtering to include
1323 only messages with a given tag, and '\\[notmuch-search]' to execute a new, global
1326 Complete list of currently available key bindings:
1328 \\{notmuch-search-mode-map}"
1330 (kill-all-local-variables)
1331 (make-local-variable 'notmuch-search-query-string)
1332 (make-local-variable 'notmuch-search-oldest-first)
1333 (set (make-local-variable 'scroll-preserve-screen-position) t)
1334 (add-to-invisibility-spec 'notmuch-search)
1335 (use-local-map notmuch-search-mode-map)
1336 (setq truncate-lines t)
1337 (setq major-mode 'notmuch-search-mode
1338 mode-name "notmuch-search")
1339 (setq buffer-read-only t)
1340 (if (not notmuch-tag-face-alist)
1341 (add-to-list 'notmuch-search-font-lock-keywords (list
1342 "(\\([^)]*\\))$" '(1 'notmuch-tag-face)))
1343 (let ((notmuch-search-tags (mapcar 'car notmuch-tag-face-alist)))
1344 (loop for notmuch-search-tag in notmuch-search-tags
1345 do (add-to-list 'notmuch-search-font-lock-keywords (list
1346 (concat "([^)]*\\(" notmuch-search-tag "\\)[^)]*)$")
1347 `(1 ,(cdr (assoc notmuch-search-tag notmuch-tag-face-alist))))))))
1348 (set (make-local-variable 'font-lock-defaults)
1349 '(notmuch-search-font-lock-keywords t)))
1351 (defun notmuch-search-find-thread-id ()
1352 "Return the thread for the current thread"
1353 (get-text-property (point) 'notmuch-search-thread-id))
1355 (defun notmuch-search-find-authors ()
1356 "Return the authors for the current thread"
1357 (get-text-property (point) 'notmuch-search-authors))
1359 (defun notmuch-search-find-subject ()
1360 "Return the subject for the current thread"
1361 (get-text-property (point) 'notmuch-search-subject))
1363 (defun notmuch-search-show-thread ()
1364 "Display the currently selected thread."
1366 (let ((thread-id (notmuch-search-find-thread-id)))
1367 (if (> (length thread-id) 0)
1368 (notmuch-show thread-id (current-buffer) notmuch-search-query-string)
1369 (error "End of search results"))))
1371 (defun notmuch-search-reply-to-thread ()
1372 "Begin composing a reply to the entire current thread in a new buffer."
1374 (let ((message-id (notmuch-search-find-thread-id)))
1375 (notmuch-reply message-id)))
1377 (defun notmuch-call-notmuch-process (&rest args)
1378 "Synchronously invoke \"notmuch\" with the given list of arguments.
1380 Output from the process will be presented to the user as an error
1381 and will also appear in a buffer named \"*Notmuch errors*\"."
1382 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
1383 (with-current-buffer error-buffer
1385 (if (eq (apply 'call-process notmuch-command nil error-buffer nil args) 0)
1388 (with-current-buffer error-buffer
1389 (let ((beg (point-min))
1390 (end (- (point-max) 1)))
1391 (error (buffer-substring beg end))
1394 (defun notmuch-search-set-tags (tags)
1397 (re-search-backward "(")
1400 (inhibit-read-only t))
1401 (re-search-forward ")")
1403 (let ((end (point)))
1404 (delete-region beg end)
1405 (insert (mapconcat 'identity tags " "))))))
1407 (defun notmuch-search-get-tags ()
1410 (re-search-backward "(")
1411 (let ((beg (+ (point) 1)))
1412 (re-search-forward ")")
1413 (let ((end (- (point) 1)))
1414 (split-string (buffer-substring beg end))))))
1416 (defun notmuch-search-add-tag (tag)
1417 "Add a tag to the currently selected thread.
1419 The tag is added to messages in the currently selected thread
1420 which match the current search terms."
1422 (list (notmuch-select-tag-with-completion "Tag to add: ")))
1423 (notmuch-call-notmuch-process "tag" (concat "+" tag) (notmuch-search-find-thread-id))
1424 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
1426 (defun notmuch-search-remove-tag (tag)
1427 "Remove a tag from the currently selected thread.
1429 The tag is removed from messages in the currently selected thread
1430 which match the current search terms."
1432 (list (notmuch-select-tag-with-completion "Tag to remove: " (notmuch-search-find-thread-id))))
1433 (notmuch-call-notmuch-process "tag" (concat "-" tag) (notmuch-search-find-thread-id))
1434 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
1436 (defun notmuch-search-archive-thread ()
1437 "Archive the currently selected thread (remove its \"inbox\" tag).
1439 This function advances the next thread when finished."
1441 (notmuch-search-remove-tag "inbox")
1444 (defun notmuch-search-process-sentinel (proc msg)
1445 "Add a message to let user know when \"notmuch search\" exits"
1446 (let ((buffer (process-buffer proc))
1447 (status (process-status proc))
1448 (exit-status (process-exit-status proc)))
1449 (if (memq status '(exit signal))
1450 (if (buffer-live-p buffer)
1451 (with-current-buffer buffer
1453 (let ((inhibit-read-only t))
1454 (goto-char (point-max))
1455 (if (eq status 'signal)
1456 (insert "Incomplete search results (search process was killed).\n"))
1457 (if (eq status 'exit)
1459 (insert "End of search results.")
1460 (if (not (= exit-status 0))
1461 (insert (format " (process returned %d)" exit-status)))
1462 (insert "\n"))))))))))
1464 (defun notmuch-search-process-filter (proc string)
1465 "Process and filter the output of \"notmuch search\""
1466 (let ((buffer (process-buffer proc)))
1467 (if (buffer-live-p buffer)
1468 (with-current-buffer buffer
1472 (inhibit-read-only t))
1474 (if (string-match "^\\(thread:[0-9A-Fa-f]*\\) \\(.*\\) \\(\\[[0-9/]*\\]\\) \\([^;]*\\); \\(.*\\) (\\([^()]*\\))$" string line)
1475 (let* ((thread-id (match-string 1 string))
1476 (date (match-string 2 string))
1477 (count (match-string 3 string))
1478 (authors (match-string 4 string))
1479 (authors-length (length authors))
1480 (subject (match-string 5 string))
1481 (tags (match-string 6 string)))
1482 (if (> authors-length 40)
1483 (set 'authors (concat (substring authors 0 (- 40 3)) "...")))
1484 (goto-char (point-max))
1485 (let ((beg (point-marker)))
1486 (insert (format "%s %-7s %-40s %s (%s)\n" date count authors subject tags))
1487 (put-text-property beg (point-marker) 'notmuch-search-thread-id thread-id)
1488 (put-text-property beg (point-marker) 'notmuch-search-authors authors)
1489 (put-text-property beg (point-marker) 'notmuch-search-subject subject))
1490 (set 'line (match-end 0)))
1491 (set 'more nil))))))
1492 (delete-process proc))))
1494 (defun notmuch-search-operate-all (action)
1495 "Add/remove tags from all matching messages.
1497 Tis command adds or removes tags from all messages matching the
1498 current search terms. When called interactively, this command
1499 will prompt for tags to be added or removed. Tags prefixed with
1500 '+' will be added and tags prefixed with '-' will be removed.
1502 Each character of the tag name may consist of alphanumeric
1503 characters as well as `_.+-'.
1505 (interactive "sOperation (+add -drop): notmuch tag ")
1506 (let ((action-split (split-string action " +")))
1507 ;; Perform some validation
1508 (let ((words action-split))
1509 (when (null words) (error "No operation given"))
1511 (unless (string-match-p "^[-+][-+_.[:word:]]+$" (car words))
1512 (error "Action must be of the form `+thistag -that_tag'"))
1513 (setq words (cdr words))))
1514 (apply 'notmuch-call-notmuch-process "tag"
1515 (append action-split (list notmuch-search-query-string) nil))))
1518 (defun notmuch-search (query &optional oldest-first)
1519 "Run \"notmuch search\" with the given query string and display results."
1520 (interactive "sNotmuch search: ")
1521 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
1522 (switch-to-buffer buffer)
1523 (notmuch-search-mode)
1524 (set 'notmuch-search-query-string query)
1525 (set 'notmuch-search-oldest-first oldest-first)
1526 (let ((proc (get-buffer-process (current-buffer)))
1527 (inhibit-read-only t))
1529 (error "notmuch search process already running for query `%s'" query)
1532 (goto-char (point-min))
1534 (let ((proc (start-process-shell-command
1535 "notmuch-search" buffer notmuch-command "search"
1536 (if oldest-first "--sort=oldest-first" "--sort=newest-first")
1537 (shell-quote-argument query))))
1538 (set-process-sentinel proc 'notmuch-search-process-sentinel)
1539 (set-process-filter proc 'notmuch-search-process-filter))))
1540 (run-hooks 'notmuch-search-hook)))
1542 (defun notmuch-search-refresh-view ()
1543 "Refresh the current view.
1545 Kills the current buffer and runs a new search with the same
1546 query string as the current search. If the current thread is in
1547 the new search results, then point will be placed on the same
1548 thread. Otherwise, point will be moved to attempt to be in the
1549 same relative position within the new buffer."
1551 (let ((here (point))
1552 (oldest-first notmuch-search-oldest-first)
1553 (thread (notmuch-search-find-thread-id))
1554 (query notmuch-search-query-string))
1556 (notmuch-search query oldest-first)
1557 (goto-char (point-min))
1558 (if (re-search-forward (concat "^" thread) nil t)
1562 (defun notmuch-search-toggle-order ()
1563 "Toggle the current search order.
1565 By default, the \"inbox\" view created by `notmuch' is displayed
1566 in chronological order (oldest thread at the beginning of the
1567 buffer), while any global searches created by `notmuch-search'
1568 are displayed in reverse-chronological order (newest thread at
1569 the beginning of the buffer).
1571 This command toggles the sort order for the current search.
1573 Note that any filtered searches created by
1574 `notmuch-search-filter' retain the search order of the parent
1577 (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
1578 (notmuch-search-refresh-view))
1580 (defun notmuch-search-filter (query)
1581 "Filter the current search results based on an additional query string.
1583 Runs a new search matching only messages that match both the
1584 current search results AND the additional query string provided."
1585 (interactive "sFilter search: ")
1586 (let ((grouped-query (if (string-match-p notmuch-search-disjunctive-regexp query) (concat "( " query " )") query)))
1587 (notmuch-search (concat notmuch-search-query-string " and " grouped-query) notmuch-search-oldest-first)))
1589 (defun notmuch-search-filter-by-tag (tag)
1590 "Filter the current search results based on a single tag.
1592 Runs a new search matching only messages that match both the
1593 current search results AND that are tagged with the given tag."
1595 (list (notmuch-select-tag-with-completion "Filter by tag: ")))
1596 (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
1601 "Run notmuch to display all mail with tag of 'inbox'"
1603 (notmuch-search "tag:inbox" notmuch-search-oldest-first))
1605 (setq mail-user-agent 'message-user-agent)
1607 (defvar notmuch-folder-mode-map
1608 (let ((map (make-sparse-keymap)))
1609 (define-key map "?" 'notmuch-help)
1610 (define-key map "x" 'kill-this-buffer)
1611 (define-key map "q" 'kill-this-buffer)
1612 (define-key map "m" 'message-mail)
1613 (define-key map "e" 'notmuch-folder-show-empty-toggle)
1614 (define-key map ">" 'notmuch-folder-last)
1615 (define-key map "<" 'notmuch-folder-first)
1616 (define-key map "=" 'notmuch-folder)
1617 (define-key map "s" 'notmuch-search)
1618 (define-key map [mouse-1] 'notmuch-folder-show-search)
1619 (define-key map (kbd "RET") 'notmuch-folder-show-search)
1620 (define-key map " " 'notmuch-folder-show-search)
1621 (define-key map "p" 'notmuch-folder-previous)
1622 (define-key map "n" 'notmuch-folder-next)
1624 "Keymap for \"notmuch folder\" buffers.")
1626 (fset 'notmuch-folder-mode-map notmuch-folder-mode-map)
1628 (defcustom notmuch-folders (quote (("inbox" . "tag:inbox") ("unread" . "tag:unread")))
1629 "List of searches for the notmuch folder view"
1630 :type '(alist :key-type (string) :value-type (string))
1633 (defun notmuch-folder-mode ()
1634 "Major mode for showing notmuch 'folders'.
1636 This buffer contains a list of message counts returned by a
1637 customizable set of searches of your email archives. Each line in
1638 the buffer shows the name of a saved search and the resulting
1641 Pressing RET on any line opens a search window containing the
1642 results for the saved search on that line.
1644 Here is an example of how the search list could be
1645 customized, (the following text would be placed in your ~/.emacs
1648 (setq notmuch-folders '((\"inbox\" . \"tag:inbox\")
1649 (\"unread\" . \"tag:inbox AND tag:unread\")
1650 (\"notmuch\" . \"tag:inbox AND to:notmuchmail.org\")))
1652 Of course, you can have any number of folders, each configured
1653 with any supported search terms (see \"notmuch help search-terms\").
1655 Currently available key bindings:
1657 \\{notmuch-folder-mode-map}"
1659 (kill-all-local-variables)
1660 (use-local-map 'notmuch-folder-mode-map)
1661 (setq truncate-lines t)
1663 (setq major-mode 'notmuch-folder-mode
1664 mode-name "notmuch-folder")
1665 (setq buffer-read-only t))
1667 (defun notmuch-folder-next ()
1668 "Select the next folder in the list."
1674 (defun notmuch-folder-previous ()
1675 "Select the previous folder in the list."
1679 (defun notmuch-folder-first ()
1680 "Select the first folder in the list."
1682 (goto-char (point-min)))
1684 (defun notmuch-folder-last ()
1685 "Select the last folder in the list."
1687 (goto-char (point-max))
1690 (defun notmuch-folder-count (search)
1691 (car (process-lines notmuch-command "count" search)))
1693 (setq notmuch-folder-show-empty t)
1695 (defun notmuch-folder-show-empty-toggle ()
1696 "Toggle the listing of empty folders"
1698 (setq notmuch-folder-show-empty (not notmuch-folder-show-empty))
1701 (defun notmuch-folder-add (folders)
1703 (let* ((name (car (car folders)))
1704 (inhibit-read-only t)
1705 (search (cdr (car folders)))
1706 (count (notmuch-folder-count search)))
1707 (if (or notmuch-folder-show-empty
1708 (not (equal count "0")))
1716 (notmuch-folder-add (cdr folders)))))
1718 (defun notmuch-folder-find-name ()
1721 (let ((beg (point)))
1722 (re-search-forward "\\([ \t]*[^ \t]+\\)")
1723 (filter-buffer-substring (match-beginning 1) (match-end 1)))))
1725 (defun notmuch-folder-show-search (&optional folder)
1726 "Show a search window for the search related to the specified folder."
1729 (setq folder (notmuch-folder-find-name)))
1730 (let ((search (assoc folder notmuch-folders)))
1732 (notmuch-search (cdr search) notmuch-search-oldest-first))))
1735 (defun notmuch-folder ()
1736 "Show the notmuch folder view and update the displayed counts."
1738 (let ((buffer (get-buffer-create "*notmuch-folders*")))
1739 (switch-to-buffer buffer)
1740 (let ((inhibit-read-only t)
1741 (n (line-number-at-pos)))
1743 (notmuch-folder-mode)
1744 (notmuch-folder-add notmuch-folders)
1745 (goto-char (point-min))