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-archive-thread-then-exit)
92 (define-key map "a" 'notmuch-show-archive-thread)
93 (define-key map "P" 'notmuch-show-previous-message)
94 (define-key map "N" 'notmuch-show-next-message)
95 (define-key map "p" 'notmuch-show-previous-open-message)
96 (define-key map "n" 'notmuch-show-next-open-message)
97 (define-key map (kbd "DEL") 'notmuch-show-rewind)
98 (define-key map " " 'notmuch-show-advance-and-archive)
100 "Keymap for \"notmuch show\" buffers.")
101 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
103 (defvar notmuch-show-signature-regexp "\\(-- ?\\|_+\\)$"
104 "Pattern to match a line that separates content from signature.
106 The regexp can (and should) include $ to match the end of the
107 line, but should not include ^ to match the beginning of the
108 line. This is because notmuch may have inserted additional space
109 for indentation at the beginning of the line. But notmuch will
110 move past the indentation when testing this pattern, (so that the
111 pattern can still test against the entire line).")
113 (defvar notmuch-show-signature-button-format
114 "[ %d-line signature. Click/Enter to toggle visibility. ]"
115 "String used to construct button text for hidden signatures
117 Can use up to one integer format parameter, i.e. %d")
119 (defvar notmuch-show-citation-button-format
120 "[ %d more citation lines. Click/Enter to toggle visibility. ]"
121 "String used to construct button text for hidden citations.
123 Can use up to one integer format parameter, i.e. %d")
125 (defvar notmuch-show-signature-lines-max 12
126 "Maximum length of signature that will be hidden by default.")
128 (defvar notmuch-show-citation-lines-prefix 4
129 "Always show at least this many lines of a citation.
131 If there is one more line, show that, otherwise collapse
132 remaining lines into a button.")
134 (defvar notmuch-command "notmuch"
135 "Command to run the notmuch binary.")
137 (defvar notmuch-show-message-begin-regexp "\fmessage{")
138 (defvar notmuch-show-message-end-regexp "\fmessage}")
139 (defvar notmuch-show-header-begin-regexp "\fheader{")
140 (defvar notmuch-show-header-end-regexp "\fheader}")
141 (defvar notmuch-show-body-begin-regexp "\fbody{")
142 (defvar notmuch-show-body-end-regexp "\fbody}")
143 (defvar notmuch-show-attachment-begin-regexp "\fattachment{")
144 (defvar notmuch-show-attachment-end-regexp "\fattachment}")
145 (defvar notmuch-show-part-begin-regexp "\fpart{")
146 (defvar notmuch-show-part-end-regexp "\fpart}")
147 (defvar notmuch-show-marker-regexp "\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
149 (defvar notmuch-show-id-regexp "\\(id:[^ ]*\\)")
150 (defvar notmuch-show-depth-match-regexp " depth:\\([0-9]*\\).*match:\\([01]\\) ")
151 (defvar notmuch-show-filename-regexp "filename:\\(.*\\)$")
152 (defvar notmuch-show-contentype-regexp "Content-type: \\(.*\\)")
154 (defvar notmuch-show-tags-regexp "(\\([^)]*\\))$")
156 (defvar notmuch-show-parent-buffer nil)
157 (defvar notmuch-show-body-read-visible nil)
158 (defvar notmuch-show-citations-visible nil)
159 (defvar notmuch-show-signatures-visible nil)
160 (defvar notmuch-show-headers-visible nil)
162 ; XXX: This should be a generic function in emacs somewhere, not here
163 (defun point-invisible-p ()
164 "Return whether the character at point is invisible.
166 Here visibility is determined by `buffer-invisibility-spec' and
167 the invisible property of any overlays for point. It doesn't have
168 anything to do with whether point is currently being displayed
169 within the current window."
170 (let ((prop (get-char-property (point) 'invisible)))
171 (if (eq buffer-invisibility-spec t)
173 (or (memq prop buffer-invisibility-spec)
174 (assq prop buffer-invisibility-spec)))))
176 (defun notmuch-select-tag-with-completion (prompt &rest search-terms)
178 (with-output-to-string
179 (with-current-buffer standard-output
180 (apply 'call-process notmuch-command nil t nil "search-tags" search-terms)))))
181 (completing-read prompt (split-string tag-list "\n+" t) nil nil nil)))
183 (defun notmuch-show-next-line ()
184 "Like builtin `next-line' but ensuring we end on a visible character.
186 By advancing forward until reaching a visible character.
188 Unlike builtin `next-line' this version accepts no arguments."
190 (set 'this-command 'next-line)
191 (call-interactively 'next-line)
192 (while (point-invisible-p)
195 (defun notmuch-show-previous-line ()
196 "Like builtin `previous-line' but ensuring we end on a visible character.
198 By advancing forward until reaching a visible character.
200 Unlike builtin `previous-line' this version accepts no arguments."
202 (set 'this-command 'previous-line)
203 (call-interactively 'previous-line)
204 (while (point-invisible-p)
207 (defun notmuch-show-get-message-id ()
210 (if (not (looking-at notmuch-show-message-begin-regexp))
211 (re-search-backward notmuch-show-message-begin-regexp))
212 (re-search-forward notmuch-show-id-regexp)
213 (buffer-substring-no-properties (match-beginning 1) (match-end 1))))
215 (defun notmuch-show-get-filename ()
218 (if (not (looking-at notmuch-show-message-begin-regexp))
219 (re-search-backward notmuch-show-message-begin-regexp))
220 (re-search-forward notmuch-show-filename-regexp)
221 (buffer-substring-no-properties (match-beginning 1) (match-end 1))))
223 (defun notmuch-show-set-tags (tags)
226 (if (not (looking-at notmuch-show-message-begin-regexp))
227 (re-search-backward notmuch-show-message-begin-regexp))
228 (re-search-forward notmuch-show-tags-regexp)
229 (let ((inhibit-read-only t)
230 (beg (match-beginning 1))
232 (delete-region beg end)
234 (insert (mapconcat 'identity tags " ")))))
236 (defun notmuch-show-get-tags ()
239 (if (not (looking-at notmuch-show-message-begin-regexp))
240 (re-search-backward notmuch-show-message-begin-regexp))
241 (re-search-forward notmuch-show-tags-regexp)
242 (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
244 (defun notmuch-show-get-bcc ()
245 "Return BCC address(es) of current message"
246 (notmuch-show-get-header-field 'bcc))
248 (defun notmuch-show-get-cc ()
249 "Return CC address(es) of current message"
250 (notmuch-show-get-header-field 'cc))
252 (defun notmuch-show-get-date ()
253 "Return Date of current message"
254 (notmuch-show-get-header-field 'date))
256 (defun notmuch-show-get-from ()
257 "Return From address of current message"
258 (notmuch-show-get-header-field 'from))
260 (defun notmuch-show-get-subject ()
261 "Return Subject of current message"
262 (notmuch-show-get-header-field 'subject))
264 (defun notmuch-show-get-to ()
265 "Return To address(es) of current message"
266 (notmuch-show-get-header-field 'to))
268 (defun notmuch-show-get-header-field (name)
269 "Retrieve the header field NAME from the current message.
270 NAME should be a symbol, in lower case, as returned by
271 mail-header-extract-no-properties"
272 (let* ((result (assoc name (notmuch-show-get-header)))
273 (val (and result (cdr result))))
276 (defun notmuch-show-get-header ()
277 "Retrieve and parse the header from the current message. Returns an alist with of (header . value)
278 where header is a symbol and value is a string. The summary from notmuch-show is returned as the
279 pseudoheader summary"
280 (require 'mailheader)
283 (if (not (looking-at notmuch-show-message-begin-regexp))
284 (re-search-backward notmuch-show-message-begin-regexp))
285 (re-search-forward (concat notmuch-show-header-begin-regexp "\n[[:space:]]*\\(.*\\)\n"))
286 (let* ((summary (buffer-substring-no-properties (match-beginning 1) (match-end 1)))
288 (re-search-forward notmuch-show-header-end-regexp)
289 (let ((text (buffer-substring beg (match-beginning 0))))
292 (goto-char (point-min))
293 (while (looking-at "\\([[:space:]]*\\)[A-Za-z][-A-Za-z0-9]*:")
294 (delete-region (match-beginning 1) (match-end 1))
297 (goto-char (point-min))
298 (cons (cons 'summary summary) (mail-header-extract-no-properties)))))))
300 (defun notmuch-show-add-tag (&rest toadd)
301 "Add a tag to the current message."
303 (list (notmuch-select-tag-with-completion "Tag to add: ")))
304 (apply 'notmuch-call-notmuch-process
306 (mapcar (lambda (s) (concat "+" s)) toadd))
307 (cons (notmuch-show-get-message-id) nil)))
308 (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
310 (defun notmuch-show-remove-tag (&rest toremove)
311 "Remove a tag from the current message."
313 (list (notmuch-select-tag-with-completion "Tag to remove: " (notmuch-show-get-message-id))))
314 (let ((tags (notmuch-show-get-tags)))
315 (if (intersection tags toremove :test 'string=)
317 (apply 'notmuch-call-notmuch-process
319 (mapcar (lambda (s) (concat "-" s)) toremove))
320 (cons (notmuch-show-get-message-id) nil)))
321 (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
323 (defun notmuch-show-archive-thread ()
324 "Archive each message in thread, then show next thread from search.
326 Archive each message currently shown by removing the \"inbox\"
327 tag from each. Then kill this buffer and show the next thread
328 from the search from which this thread was originally shown.
330 Note: This command is safe from any race condition of new messages
331 being delivered to the same thread. It does not archive the
332 entire thread, but only the messages shown in the current
336 (goto-char (point-min))
338 (notmuch-show-remove-tag "inbox")
341 (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
342 (goto-char (point-max)))))
343 (let ((parent-buffer notmuch-show-parent-buffer))
347 (switch-to-buffer parent-buffer)
349 (notmuch-search-show-thread)))))
351 (defun notmuch-show-archive-thread-then-exit ()
352 "Archive each message in thread, then exit back to search results."
354 (notmuch-show-archive-thread)
357 (defun notmuch-show-view-raw-message ()
358 "View the raw email of the current message."
360 (view-file (notmuch-show-get-filename)))
362 (defmacro with-current-notmuch-show-message (&rest body)
363 "Evaluate body with current buffer set to the text of current message"
365 (let ((filename (notmuch-show-get-filename)))
366 (let ((buf (generate-new-buffer (concat "*notmuch-msg-" filename "*"))))
367 (with-current-buffer buf
368 (insert-file-contents filename nil nil nil t)
370 (kill-buffer buf)))))
372 (defun notmuch-show-view-all-mime-parts ()
373 "Use external viewers to view all attachments from the current message."
375 (with-current-notmuch-show-message
376 ; We ovverride the mm-inline-media-tests to indicate which message
377 ; parts are already sufficiently handled by the original
378 ; presentation of the message in notmuch-show mode. These parts
379 ; will be inserted directly into the temporary buffer of
380 ; with-current-notmuch-show-message and silently discarded.
382 ; Any MIME part not explicitly mentioned here will be handled by an
383 ; external viewer as configured in the various mailcap files.
384 (let ((mm-inline-media-tests '(
385 ("text/.*" ignore identity)
386 ("application/pgp-signature" ignore identity)
387 ("multipart/alternative" ignore identity)
388 ("multipart/mixed" ignore identity)
389 ("multipart/related" ignore identity)
391 (mm-display-parts (mm-dissect-buffer)))))
393 (defun notmuch-foreach-mime-part (function mm-handle)
394 (cond ((stringp (car mm-handle))
395 (dolist (part (cdr mm-handle))
396 (notmuch-foreach-mime-part function part)))
397 ((bufferp (car mm-handle))
398 (funcall function mm-handle))
399 (t (dolist (part mm-handle)
400 (notmuch-foreach-mime-part function part)))))
402 (defun notmuch-count-attachments (mm-handle)
404 (notmuch-foreach-mime-part
406 (let ((disposition (mm-handle-disposition p)))
407 (and (listp disposition)
408 (or (equal (car disposition) "attachment")
409 (and (equal (car disposition) "inline")
410 (assq 'filename disposition)))
415 (defun notmuch-save-attachments (mm-handle &optional queryp)
416 (notmuch-foreach-mime-part
418 (let ((disposition (mm-handle-disposition p)))
419 (and (listp disposition)
420 (or (equal (car disposition) "attachment")
421 (and (equal (car disposition) "inline")
422 (assq 'filename disposition)))
425 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
429 (defun notmuch-show-save-attachments ()
430 "Save all attachments from the current message."
432 (with-current-notmuch-show-message
433 (let ((mm-handle (mm-dissect-buffer)))
434 (notmuch-save-attachments
435 mm-handle (> (notmuch-count-attachments mm-handle) 1))))
438 (defun notmuch-reply (query-string)
439 (switch-to-buffer (generate-new-buffer "notmuch-draft"))
440 (call-process notmuch-command nil t nil "reply" query-string)
441 (message-insert-signature)
442 (goto-char (point-min))
443 (if (re-search-forward "^$" nil t)
445 (insert "--text follows this line--")
449 (defun notmuch-show-reply ()
450 "Begin composing a reply to the current message in a new buffer."
452 (let ((message-id (notmuch-show-get-message-id)))
453 (notmuch-reply message-id)))
455 (defun notmuch-show-forward-current ()
456 "Forward the current message."
458 (with-current-notmuch-show-message
461 (defun notmuch-show-pipe-message (command)
462 "Pipe the contents of the current message to the given command.
464 The given command will be executed with the raw contents of the
465 current email message as stdin. Anything printed by the command
466 to stdout or stderr will appear in the *Messages* buffer."
467 (interactive "sPipe message to command: ")
468 (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*"
469 (list command " < " (shell-quote-argument (notmuch-show-get-filename)))))
471 (defun notmuch-show-move-to-current-message-summary-line ()
472 "Move to the beginning of the one-line summary of the current message.
474 This gives us a stable place to move to and work from since the
475 summary line is always visible. This is important since moving to
476 an invisible location is unreliable, (the main command loop moves
477 point either forward or backward to the next visible character
478 when a command ends with point on an invisible character).
480 Emits an error if point is not within a valid message, (that is
481 no pattern of `notmuch-show-message-begin-regexp' could be found
482 by searching backward)."
484 (if (not (looking-at notmuch-show-message-begin-regexp))
485 (if (re-search-backward notmuch-show-message-begin-regexp nil t)
487 (error "Not within a valid message."))
490 (defun notmuch-show-last-message-p ()
491 "Predicate testing whether point is within the last message."
492 (save-window-excursion
494 (notmuch-show-move-to-current-message-summary-line)
495 (not (re-search-forward notmuch-show-message-begin-regexp nil t)))))
497 (defun notmuch-show-message-unread-p ()
498 "Predicate testing whether current message is unread."
499 (member "unread" (notmuch-show-get-tags)))
501 (defun notmuch-show-message-open-p ()
502 "Predicate testing whether current message is open (body is visible)."
503 (let ((btn (previous-button (point) t)))
504 (while (not (button-has-type-p btn 'notmuch-button-body-toggle-type))
505 (setq btn (previous-button (button-start btn))))
506 (not (invisible-p (button-get btn 'invisibility-spec)))))
508 (defun notmuch-show-next-message-without-marking-read ()
509 "Advance to the beginning of the next message in the buffer.
511 Moves to the last visible character of the current message if
512 already on the last message in the buffer.
514 Returns nil if already on the last message in the buffer."
515 (notmuch-show-move-to-current-message-summary-line)
516 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
518 (notmuch-show-move-to-current-message-summary-line)
521 (goto-char (- (point-max) 1))
522 (while (point-invisible-p)
527 (defun notmuch-show-next-message ()
528 "Advance to the next message (whether open or closed)
529 and remove the unread tag from that message.
531 Moves to the last visible character of the current message if
532 already on the last message in the buffer.
534 Returns nil if already on the last message in the buffer."
536 (notmuch-show-next-message-without-marking-read)
537 (notmuch-show-mark-read))
539 (defun notmuch-show-find-next-message ()
540 "Returns the position of the next message in the buffer.
542 Or the position of the last visible character of the current
543 message if already within the last message in the buffer."
544 ; save-excursion doesn't save our window position
545 ; save-window-excursion doesn't save point
546 ; Looks like we have to use both.
548 (save-window-excursion
549 (notmuch-show-next-message-without-marking-read)
552 (defun notmuch-show-next-unread-message ()
553 "Advance to the next unread message.
555 Moves to the last visible character of the current message if
556 there are no more unread messages past the current point."
557 (notmuch-show-next-message-without-marking-read)
558 (while (and (not (notmuch-show-last-message-p))
559 (not (notmuch-show-message-unread-p)))
560 (notmuch-show-next-message-without-marking-read))
561 (if (not (notmuch-show-message-unread-p))
562 (notmuch-show-next-message-without-marking-read))
563 (notmuch-show-mark-read))
565 (defun notmuch-show-next-open-message ()
566 "Advance to the next open message (that is, body is visible).
568 Moves to the last visible character of the final message in the buffer
569 if there are no more open messages."
571 (while (and (notmuch-show-next-message-without-marking-read)
572 (not (notmuch-show-message-open-p))))
573 (notmuch-show-mark-read))
575 (defun notmuch-show-previous-message-without-marking-read ()
576 "Backup to the beginning of the previous message in the buffer.
578 If within a message rather than at the beginning of it, then
579 simply move to the beginning of the current message.
581 Returns nil if already on the first message in the buffer."
582 (let ((start (point)))
583 (notmuch-show-move-to-current-message-summary-line)
584 (if (not (< (point) start))
585 ; Go backward twice to skip the current message's marker
587 (re-search-backward notmuch-show-message-begin-regexp nil t)
588 (re-search-backward notmuch-show-message-begin-regexp nil t)
589 (notmuch-show-move-to-current-message-summary-line)
591 (if (= (point) start)
597 (defun notmuch-show-previous-message ()
598 "Backup to the previous message (whether open or closed)
599 and remove the unread tag from that message.
601 If within a message rather than at the beginning of it, then
602 simply move to the beginning of the current message."
604 (notmuch-show-previous-message-without-marking-read)
605 (notmuch-show-mark-read))
607 (defun notmuch-show-find-previous-message ()
608 "Returns the position of the previous message in the buffer.
610 Or the position of the beginning of the current message if point
611 is originally within the message rather than at the beginning of
613 ; save-excursion doesn't save our window position
614 ; save-window-excursion doesn't save point
615 ; Looks like we have to use both.
617 (save-window-excursion
618 (notmuch-show-previous-message-without-marking-read)
621 (defun notmuch-show-previous-open-message ()
622 "Backup to previous open message (that is, body is visible).
624 Moves to the first message in the buffer if there are no previous
627 (while (and (notmuch-show-previous-message-without-marking-read)
628 (not (notmuch-show-message-open-p))))
629 (notmuch-show-mark-read))
631 (defun notmuch-show-rewind ()
632 "Backup through the thread, (reverse scrolling compared to \\[notmuch-show-advance-and-archive]).
634 Specifically, if the beginning of the previous email is fewer
635 than `window-height' lines from the current point, move to it
636 just like `notmuch-show-previous-message'.
638 Otherwise, just scroll down a screenful of the current message.
640 This command does not modify any message tags, (it does not undo
641 any effects from previous calls to
642 `notmuch-show-advance-and-archive'."
644 (let ((previous (notmuch-show-find-previous-message)))
645 (if (> (count-lines previous (point)) (- (window-height) next-screen-context-lines))
649 ((beginning-of-buffer) nil))
650 (goto-char (window-start))
651 ; Because count-lines counts invivisible lines, we may have
652 ; scrolled to far. If so., notice this and fix it up.
653 (if (< (point) previous)
657 (notmuch-show-previous-message))))
659 (defun notmuch-show-advance-and-archive ()
660 "Advance through thread and archive.
662 This command is intended to be one of the simplest ways to
663 process a thread of email. It does the following:
665 If the current message in the thread is not yet fully visible,
666 scroll by a near screenful to read more of the message.
668 Otherwise, (the end of the current message is already within the
669 current window), advance to the next open message.
671 Finally, if there is no further message to advance to, and this
672 last message is already read, then archive the entire current
673 thread, (remove the \"inbox\" tag from each message). Also kill
674 this buffer, and display the next thread from the search from
675 which this thread was originally shown."
677 (let ((next (notmuch-show-find-next-message))
678 (unread (notmuch-show-message-unread-p)))
679 (if (> next (window-end))
681 (let ((last (notmuch-show-last-message-p)))
682 (notmuch-show-next-open-message)
684 (notmuch-show-archive-thread))))))
686 (defun notmuch-show-next-button ()
687 "Advance point to the next button in the buffer."
691 (defun notmuch-show-previous-button ()
692 "Move point back to the previous button in the buffer."
696 (defun notmuch-toggle-invisible-action (cite-button)
697 (let ((invis-spec (button-get cite-button 'invisibility-spec)))
698 (if (invisible-p invis-spec)
699 (remove-from-invisibility-spec invis-spec)
700 (add-to-invisibility-spec invis-spec)
702 (force-window-update)
705 (defun notmuch-show-toggle-current-body ()
706 "Toggle the display of the current message body."
709 (notmuch-show-move-to-current-message-summary-line)
710 (unless (button-at (point))
711 (notmuch-show-next-button))
715 (defun notmuch-show-toggle-current-header ()
716 "Toggle the display of the current message header."
719 (notmuch-show-move-to-current-message-summary-line)
721 (unless (button-at (point))
722 (notmuch-show-next-button))
726 (define-button-type 'notmuch-button-invisibility-toggle-type
727 'action 'notmuch-toggle-invisible-action
729 'face 'font-lock-comment-face)
730 (define-button-type 'notmuch-button-citation-toggle-type 'help-echo "mouse-1, RET: Show citation"
731 :supertype 'notmuch-button-invisibility-toggle-type)
732 (define-button-type 'notmuch-button-signature-toggle-type 'help-echo "mouse-1, RET: Show signature"
733 :supertype 'notmuch-button-invisibility-toggle-type)
734 (define-button-type 'notmuch-button-headers-toggle-type 'help-echo "mouse-1, RET: Show headers"
735 :supertype 'notmuch-button-invisibility-toggle-type)
736 (define-button-type 'notmuch-button-body-toggle-type
737 'help-echo "mouse-1, RET: Show message"
738 'face 'notmuch-message-summary-face
739 :supertype 'notmuch-button-invisibility-toggle-type)
741 (defun notmuch-show-citation-regexp (depth)
742 "Build a regexp for matching citations at a given DEPTH (indent)"
743 (let ((line-regexp (format "[[:space:]]\\{%d\\}>.*\n" depth)))
744 (concat "\\(?:^" line-regexp
745 "\\(?:[[:space:]]*\n" line-regexp
748 (defun notmuch-show-region-to-button (beg end type prefix button-text)
749 "Auxilary function to do the actual making of overlays and buttons
751 BEG and END are buffer locations. TYPE should a string, either
752 \"citation\" or \"signature\". PREFIX is some arbitrary text to
753 insert before the button, probably for indentation. BUTTON-TEXT
754 is what to put on the button."
756 ;; This uses some slightly tricky conversions between strings and
757 ;; symbols because of the way the button code works. Note that
758 ;; replacing intern-soft with make-symbol will cause this to fail,
759 ;; since the newly created symbol has no plist.
761 (let ((overlay (make-overlay beg end))
762 (invis-spec (make-symbol (concat "notmuch-" type "-region")))
763 (button-type (intern-soft (concat "notmuch-button-"
764 type "-toggle-type"))))
765 (add-to-invisibility-spec invis-spec)
766 (overlay-put overlay 'invisible invis-spec)
771 (insert-button button-text
772 'invisibility-spec invis-spec
777 (defun notmuch-show-markup-citations-region (beg end depth)
778 "Markup citations, and up to one signature in the given region"
779 ;; it would be nice if the untabify was not required, but
780 ;; that would require notmuch to indent with spaces.
782 (let ((citation-regexp (notmuch-show-citation-regexp depth))
783 (signature-regexp (concat (format "^[[:space:]]\\{%d\\}" depth)
784 notmuch-show-signature-regexp))
785 (indent (concat "\n" (make-string depth ? ))))
788 (while (and (< (point) end)
789 (re-search-forward citation-regexp end t))
790 (let* ((cite-start (match-beginning 0))
791 (cite-end (match-end 0))
792 (cite-lines (count-lines cite-start cite-end)))
793 (when (> cite-lines (1+ notmuch-show-citation-lines-prefix))
794 (goto-char cite-start)
795 (forward-line notmuch-show-citation-lines-prefix)
796 (notmuch-show-region-to-button
800 (format notmuch-show-citation-button-format
801 (- cite-lines notmuch-show-citation-lines-prefix))
803 (if (and (< (point) end)
804 (re-search-forward signature-regexp end t))
805 (let* ((sig-start (match-beginning 0))
806 (sig-end (match-end 0))
807 (sig-lines (1- (count-lines sig-start end))))
808 (if (<= sig-lines notmuch-show-signature-lines-max)
809 (notmuch-show-region-to-button
814 (format notmuch-show-signature-button-format sig-lines)
817 (defun notmuch-show-markup-part (beg end depth)
818 (if (re-search-forward notmuch-show-part-begin-regexp nil t)
820 (let (mime-message mime-type)
822 (re-search-forward notmuch-show-contentype-regexp end t)
823 (setq mime-type (car (split-string (buffer-substring
824 (match-beginning 1) (match-end 1))))))
826 (if (equal mime-type "text/html")
827 (let ((filename (notmuch-show-get-filename)))
829 (insert-file-contents filename nil nil nil t)
830 (setq mime-message (mm-dissect-buffer)))))
832 (let ((beg (point-marker)))
833 (re-search-forward notmuch-show-part-end-regexp)
834 (let ((end (copy-marker (match-beginning 0))))
838 (indent-rigidly beg end depth)
839 (if (not (eq mime-message nil))
843 (let ((handle-type (mm-handle-type mime-message))
845 (if (sequencep (car handle-type))
846 (setq mime-type (car handle-type))
847 (setq mime-type (car (car (cdr handle-type))))
849 (if (equal mime-type "text/html")
850 (mm-display-part mime-message))))
852 (notmuch-show-markup-citations-region beg end depth)
853 ; Advance to the next part (if any) (so the outer loop can
854 ; determine whether we've left the current message.
855 (if (re-search-forward notmuch-show-part-begin-regexp nil t)
856 (beginning-of-line)))))
860 (defun notmuch-show-markup-parts-region (beg end depth)
863 (while (< (point) end)
864 (notmuch-show-markup-part beg end depth))))
866 (defun notmuch-show-markup-body (depth match btn)
867 "Markup a message body, (indenting, buttonizing citations,
868 etc.), and hiding the body itself if the message does not match
871 DEPTH specifies the depth at which this message appears in the
872 tree of the current thread, (the top-level messages have depth 0
873 and each reply increases depth by 1). MATCH indicates whether
874 this message is regarded as matching the current search. BTN is
875 the button which is used to toggle the visibility of this
878 When this function is called, point must be within the message, but
879 before the delimiter marking the beginning of the body."
880 (re-search-forward notmuch-show-body-begin-regexp)
882 (let ((beg (point-marker)))
883 (re-search-forward notmuch-show-body-end-regexp)
884 (let ((end (copy-marker (match-beginning 0))))
885 (notmuch-show-markup-parts-region beg end depth)
886 (let ((invis-spec (make-symbol "notmuch-show-body-read")))
887 (overlay-put (make-overlay beg end)
888 'invisible invis-spec)
889 (button-put btn 'invisibility-spec invis-spec)
891 (add-to-invisibility-spec invis-spec)))
896 (defun notmuch-fontify-headers ()
897 (while (looking-at "[[:space:]]")
899 (if (looking-at "[Tt]o:")
901 (overlay-put (make-overlay (point) (re-search-forward ":"))
902 'face 'message-header-name)
903 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
904 'face 'message-header-to))
905 (if (looking-at "[B]?[Cc][Cc]:")
907 (overlay-put (make-overlay (point) (re-search-forward ":"))
908 'face 'message-header-name)
909 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
910 'face 'message-header-cc))
911 (if (looking-at "[Ss]ubject:")
913 (overlay-put (make-overlay (point) (re-search-forward ":"))
914 'face 'message-header-name)
915 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
916 'face 'message-header-subject))
917 (if (looking-at "[Ff]rom:")
919 (overlay-put (make-overlay (point) (re-search-forward ":"))
920 'face 'message-header-name)
921 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
922 'face 'message-header-other)))))))
924 (defun notmuch-show-markup-header (message-begin depth)
925 "Buttonize and decorate faces in a message header.
927 MESSAGE-BEGIN is the position of the absolute first character in
928 the message (including all delimiters that will end up being
929 invisible etc.). This is to allow a button to reliably extend to
930 the beginning of the message even if point is positioned at an
931 invisible character (such as the beginning of the buffer).
933 DEPTH specifies the depth at which this message appears in the
934 tree of the current thread, (the top-level messages have depth 0
935 and each reply increases depth by 1)."
936 (re-search-forward notmuch-show-header-begin-regexp)
938 (let ((beg (point-marker))
939 (summary-end (copy-marker (line-beginning-position 2)))
940 (subject-end (copy-marker (line-end-position 2)))
941 (invis-spec (make-symbol "notmuch-show-header"))
943 (re-search-forward notmuch-show-header-end-regexp)
945 (let ((end (point-marker)))
946 (indent-rigidly beg end depth)
948 (setq btn (make-button message-begin summary-end :type 'notmuch-button-body-toggle-type))
950 (add-to-invisibility-spec invis-spec)
951 (overlay-put (make-overlay subject-end end)
952 'invisible invis-spec)
953 (make-button (line-beginning-position) subject-end
954 'invisibility-spec invis-spec
955 :type 'notmuch-button-headers-toggle-type)
956 (while (looking-at "[[:space:]]*[A-Za-z][-A-Za-z0-9]*:")
958 (notmuch-fontify-headers)
964 (set-marker summary-end nil)
965 (set-marker subject-end nil)
970 (defun notmuch-show-markup-message ()
971 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
972 (let ((message-begin (match-beginning 0)))
973 (re-search-forward notmuch-show-depth-match-regexp)
974 (let ((depth (string-to-number (buffer-substring (match-beginning 1) (match-end 1))))
975 (match (string= "1" (buffer-substring (match-beginning 2) (match-end 2))))
977 (setq btn (notmuch-show-markup-header message-begin depth))
978 (notmuch-show-markup-body depth match btn)))
979 (goto-char (point-max))))
981 (defun notmuch-show-hide-markers ()
983 (goto-char (point-min))
985 (if (re-search-forward notmuch-show-marker-regexp nil t)
987 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
988 'invisible 'notmuch-show-marker))
989 (goto-char (point-max))))))
991 (defun notmuch-show-markup-messages ()
993 (goto-char (point-min))
995 (notmuch-show-markup-message)))
996 (notmuch-show-hide-markers))
998 (defun notmuch-documentation-first-line (symbol)
999 "Return the first line of the documentation string for SYMBOL."
1000 (let ((doc (documentation symbol)))
1003 (insert (documentation symbol t))
1004 (goto-char (point-min))
1005 (let ((beg (point)))
1007 (buffer-substring beg (point))))
1010 (defun notmuch-prefix-key-description (key)
1011 "Given a prefix key code, return a human-readable string representation.
1013 This is basically just `format-kbd-macro' but we also convert ESC to M-."
1014 (let ((desc (format-kbd-macro (vector key))))
1015 (if (string= desc "ESC")
1017 (concat desc " "))))
1019 ; I would think that emacs would have code handy for walking a keymap
1020 ; and generating strings for each key, and I would prefer to just call
1021 ; that. But I couldn't find any (could be all implemented in C I
1022 ; suppose), so I wrote my own here.
1023 (defun notmuch-substitute-one-command-key-with-prefix (prefix binding)
1024 "For a key binding, return a string showing a human-readable
1025 representation of the prefixed key as well as the first line of
1026 documentation from the bound function.
1028 For a mouse binding, return nil."
1029 (let ((key (car binding))
1030 (action (cdr binding)))
1031 (if (mouse-event-p key)
1033 (if (keymapp action)
1034 (let ((substitute (apply-partially 'notmuch-substitute-one-command-key-with-prefix (notmuch-prefix-key-description key)))
1036 (map-keymap (lambda (a b)
1037 (push (cons a b) as-list))
1039 (mapconcat substitute as-list "\n"))
1040 (concat prefix (format-kbd-macro (vector key))
1042 (notmuch-documentation-first-line action))))))
1044 (defalias 'notmuch-substitute-one-command-key
1045 (apply-partially 'notmuch-substitute-one-command-key-with-prefix nil))
1047 (defun notmuch-substitute-command-keys (doc)
1048 "Like `substitute-command-keys' but with documentation, not function names."
1050 (while (string-match "\\\\{\\([^}[:space:]]*\\)}" doc beg)
1051 (let ((map (substring doc (match-beginning 1) (match-end 1))))
1052 (setq doc (replace-match (mapconcat 'notmuch-substitute-one-command-key
1053 (cdr (symbol-value (intern map))) "\n") 1 1 doc)))
1054 (setq beg (match-end 0)))
1057 (defun notmuch-help ()
1058 "Display help for the current notmuch mode."
1060 (let* ((mode major-mode)
1061 (doc (substitute-command-keys (notmuch-substitute-command-keys (documentation mode t)))))
1062 (with-current-buffer (generate-new-buffer "*notmuch-help*")
1064 (goto-char (point-min))
1065 (set-buffer-modified-p nil)
1066 (view-buffer (current-buffer) 'kill-buffer-if-not-modified))))
1069 (defun notmuch-show-mode ()
1070 "Major mode for viewing a thread with notmuch.
1072 This buffer contains the results of the \"notmuch show\" command
1073 for displaying a single thread of email from your email archives.
1075 By default, various components of email messages, (citations,
1076 signatures, already-read messages), are hidden. You can make
1077 these parts visible by clicking with the mouse button or by
1078 pressing RET after positioning the cursor on a hidden part, (for
1079 which \\[notmuch-show-next-button] and \\[notmuch-show-previous-button] are helpful).
1081 Reading the thread sequentially is well-supported by pressing
1082 \\[notmuch-show-advance-and-archive]. This will
1083 scroll the current message (if necessary), advance to the next
1084 message, or advance to the next thread (if already on the last
1085 message of a thread).
1087 Other commands are available to read or manipulate the thread more
1088 selectively, (such as '\\[notmuch-show-next-message]' and '\\[notmuch-show-previous-message]' to advance to messages without
1089 removing any tags, and '\\[notmuch-show-archive-thread]' to archive an entire thread without
1090 scrolling through with \\[notmuch-show-advance-and-archive]).
1092 You can add or remove arbitary tags from the current message with
1093 '\\[notmuch-show-add-tag]' or '\\[notmuch-show-remove-tag]'.
1095 All currently available key bindings:
1097 \\{notmuch-show-mode-map}"
1099 (kill-all-local-variables)
1100 (add-to-invisibility-spec 'notmuch-show-marker)
1101 (use-local-map notmuch-show-mode-map)
1102 (setq major-mode 'notmuch-show-mode
1103 mode-name "notmuch-show")
1104 (setq buffer-read-only t))
1106 (defgroup notmuch nil
1107 "Notmuch mail reader for Emacs."
1110 (defcustom notmuch-show-hook nil
1111 "List of functions to call when notmuch displays a message."
1113 :options '(goto-address)
1116 (defcustom notmuch-search-hook nil
1117 "List of functions to call when notmuch displays the search results."
1119 :options '(hl-line-mode)
1122 (defun notmuch-show-do-stash (text)
1124 (message (concat "Saved: " text)))
1126 (defun notmuch-show-stash-cc ()
1127 "Copy CC field of current message to kill-ring."
1129 (notmuch-show-do-stash (notmuch-show-get-cc)))
1131 (defun notmuch-show-stash-date ()
1132 "Copy date of current message to kill-ring."
1134 (notmuch-show-do-stash (notmuch-show-get-date)))
1136 (defun notmuch-show-stash-filename ()
1137 "Copy filename of current message to kill-ring."
1139 (notmuch-show-do-stash (notmuch-show-get-filename)))
1141 (defun notmuch-show-stash-from ()
1142 "Copy From address of current message to kill-ring."
1144 (notmuch-show-do-stash (notmuch-show-get-from)))
1146 (defun notmuch-show-stash-message-id ()
1147 "Copy message ID of current message to kill-ring."
1149 (notmuch-show-do-stash (notmuch-show-get-message-id)))
1151 (defun notmuch-show-stash-subject ()
1152 "Copy Subject field of current message to kill-ring."
1154 (notmuch-show-do-stash (notmuch-show-get-subject)))
1156 (defun notmuch-show-stash-tags ()
1157 "Copy tags of current message to kill-ring as a comma separated list."
1159 (notmuch-show-do-stash (mapconcat 'identity (notmuch-show-get-tags) ",")))
1161 (defun notmuch-show-stash-to ()
1162 "Copy To address of current message to kill-ring."
1164 (notmuch-show-do-stash (notmuch-show-get-to)))
1166 ; Make show mode a bit prettier, highlighting URLs and using word wrap
1168 (defun notmuch-show-mark-read ()
1169 (notmuch-show-remove-tag "unread"))
1171 (defun notmuch-show-pretty-hook ()
1172 (goto-address-mode 1)
1175 (add-hook 'notmuch-show-hook 'notmuch-show-mark-read)
1176 (add-hook 'notmuch-show-hook 'notmuch-show-pretty-hook)
1177 (add-hook 'notmuch-search-hook
1181 (defun notmuch-show (thread-id &optional parent-buffer query-context)
1182 "Run \"notmuch show\" with the given thread ID and display results.
1184 The optional PARENT-BUFFER is the notmuch-search buffer from
1185 which this notmuch-show command was executed, (so that the next
1186 thread from that buffer can be show when done with this one).
1188 The optional QUERY-CONTEXT is a notmuch search term. Only messages from the thread
1189 matching this search term are shown if non-nil. "
1190 (interactive "sNotmuch show: ")
1191 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
1192 (switch-to-buffer buffer)
1194 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
1195 (let ((proc (get-buffer-process (current-buffer)))
1196 (inhibit-read-only t))
1198 (error "notmuch search process already running for query `%s'" thread-id)
1201 (goto-char (point-min))
1203 (let* ((basic-args (list notmuch-command nil t nil "show" "--entire-thread" thread-id))
1204 (args (if query-context (append basic-args (list "and (" query-context ")")) basic-args)))
1205 (apply 'call-process args)
1206 (when (and (eq (buffer-size) 0) query-context)
1207 (apply 'call-process basic-args)))
1208 (notmuch-show-markup-messages)
1210 (run-hooks 'notmuch-show-hook)
1211 ; Move straight to the first open message
1212 (if (not (notmuch-show-message-open-p))
1213 (notmuch-show-next-open-message))
1216 (defvar notmuch-search-authors-width 20
1217 "Number of columns to use to display authors in a notmuch-search buffer.")
1219 (defvar notmuch-search-mode-map
1220 (let ((map (make-sparse-keymap)))
1221 (define-key map "?" 'notmuch-help)
1222 (define-key map "q" 'kill-this-buffer)
1223 (define-key map "x" 'kill-this-buffer)
1224 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
1225 (define-key map "b" 'notmuch-search-scroll-down)
1226 (define-key map " " 'notmuch-search-scroll-up)
1227 (define-key map "<" 'notmuch-search-first-thread)
1228 (define-key map ">" 'notmuch-search-last-thread)
1229 (define-key map "p" 'notmuch-search-previous-thread)
1230 (define-key map "n" 'notmuch-search-next-thread)
1231 (define-key map "r" 'notmuch-search-reply-to-thread)
1232 (define-key map "m" 'message-mail)
1233 (define-key map "s" 'notmuch-search)
1234 (define-key map "o" 'notmuch-search-toggle-order)
1235 (define-key map "=" 'notmuch-search-refresh-view)
1236 (define-key map "t" 'notmuch-search-filter-by-tag)
1237 (define-key map "f" 'notmuch-search-filter)
1238 (define-key map [mouse-1] 'notmuch-search-show-thread)
1239 (define-key map "*" 'notmuch-search-operate-all)
1240 (define-key map "a" 'notmuch-search-archive-thread)
1241 (define-key map "-" 'notmuch-search-remove-tag)
1242 (define-key map "+" 'notmuch-search-add-tag)
1243 (define-key map (kbd "RET") 'notmuch-search-show-thread)
1245 "Keymap for \"notmuch search\" buffers.")
1246 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
1248 (defvar notmuch-search-query-string)
1249 (defvar notmuch-search-target-thread)
1250 (defvar notmuch-search-target-line)
1251 (defvar notmuch-search-oldest-first t
1252 "Show the oldest mail first in the search-mode")
1254 (defvar notmuch-search-disjunctive-regexp "\\<[oO][rR]\\>")
1256 (defun notmuch-search-scroll-up ()
1257 "Move forward through search results by one window's worth."
1261 ((end-of-buffer) (notmuch-search-last-thread))))
1263 (defun notmuch-search-scroll-down ()
1264 "Move backward through the search results by one window's worth."
1266 ; I don't know why scroll-down doesn't signal beginning-of-buffer
1267 ; the way that scroll-up signals end-of-buffer, but c'est la vie.
1269 ; So instead of trapping a signal we instead check whether the
1270 ; window begins on the first line of the buffer and if so, move
1271 ; directly to that position. (We have to count lines since the
1272 ; window-start position is not the same as point-min due to the
1273 ; invisible thread-ID characters on the first line.
1274 (if (equal (count-lines (point-min) (window-start)) 0)
1275 (goto-char (point-min))
1278 (defun notmuch-search-next-thread ()
1279 "Select the next thread in the search results."
1283 (defun notmuch-search-previous-thread ()
1284 "Select the previous thread in the search results."
1288 (defun notmuch-search-last-thread ()
1289 "Select the last thread in the search results."
1291 (goto-char (point-max))
1294 (defun notmuch-search-first-thread ()
1295 "Select the first thread in the search results."
1297 (goto-char (point-min)))
1299 (defface notmuch-message-summary-face
1300 '((((class color) (background light)) (:background "#f0f0f0"))
1301 (((class color) (background dark)) (:background "#303030")))
1302 "Face for the single-line message summary in notmuch-show-mode."
1305 (defface notmuch-tag-face
1308 (:foreground "OliveDrab1"))
1311 (:foreground "navy blue" :bold t))
1314 "Notmuch search mode face used to highligh tags."
1317 (defvar notmuch-tag-face-alist nil
1318 "List containing the tag list that need to be highlighed")
1320 (defvar notmuch-search-font-lock-keywords nil)
1323 (defun notmuch-search-mode ()
1324 "Major mode displaying results of a notmuch search.
1326 This buffer contains the results of a \"notmuch search\" of your
1327 email archives. Each line in the buffer represents a single
1328 thread giving a summary of the thread (a relative date, the
1329 number of matched messages and total messages in the thread,
1330 participants in the thread, a representative subject line, and
1333 Pressing \\[notmuch-search-show-thread] on any line displays that thread. The '\\[notmuch-search-add-tag]' and '\\[notmuch-search-remove-tag]'
1334 keys can be used to add or remove tags from a thread. The '\\[notmuch-search-archive-thread]' key
1335 is a convenience for archiving a thread (removing the \"inbox\"
1336 tag). The '\\[notmuch-search-operate-all]' key can be used to add or remove a tag from all
1337 threads in the current buffer.
1339 Other useful commands are '\\[notmuch-search-filter]' for filtering the current search
1340 based on an additional query string, '\\[notmuch-search-filter-by-tag]' for filtering to include
1341 only messages with a given tag, and '\\[notmuch-search]' to execute a new, global
1344 Complete list of currently available key bindings:
1346 \\{notmuch-search-mode-map}"
1348 (kill-all-local-variables)
1349 (make-local-variable 'notmuch-search-query-string)
1350 (make-local-variable 'notmuch-search-oldest-first)
1351 (make-local-variable 'notmuch-search-target-thread)
1352 (make-local-variable 'notmuch-search-target-line)
1353 (set (make-local-variable 'scroll-preserve-screen-position) t)
1354 (add-to-invisibility-spec 'notmuch-search)
1355 (use-local-map notmuch-search-mode-map)
1356 (setq truncate-lines t)
1357 (setq major-mode 'notmuch-search-mode
1358 mode-name "notmuch-search")
1359 (setq buffer-read-only t)
1360 (if (not notmuch-tag-face-alist)
1361 (add-to-list 'notmuch-search-font-lock-keywords (list
1362 "(\\([^()]*\\))$" '(1 'notmuch-tag-face)))
1363 (let ((notmuch-search-tags (mapcar 'car notmuch-tag-face-alist)))
1364 (loop for notmuch-search-tag in notmuch-search-tags
1365 do (add-to-list 'notmuch-search-font-lock-keywords (list
1366 (concat "([^)]*\\(" notmuch-search-tag "\\)[^)]*)$")
1367 `(1 ,(cdr (assoc notmuch-search-tag notmuch-tag-face-alist))))))))
1368 (set (make-local-variable 'font-lock-defaults)
1369 '(notmuch-search-font-lock-keywords t)))
1371 (defun notmuch-search-find-thread-id ()
1372 "Return the thread for the current thread"
1373 (get-text-property (point) 'notmuch-search-thread-id))
1375 (defun notmuch-search-find-authors ()
1376 "Return the authors for the current thread"
1377 (get-text-property (point) 'notmuch-search-authors))
1379 (defun notmuch-search-find-subject ()
1380 "Return the subject for the current thread"
1381 (get-text-property (point) 'notmuch-search-subject))
1383 (defun notmuch-search-show-thread ()
1384 "Display the currently selected thread."
1386 (let ((thread-id (notmuch-search-find-thread-id)))
1387 (if (> (length thread-id) 0)
1388 (notmuch-show thread-id (current-buffer) notmuch-search-query-string)
1389 (error "End of search results"))))
1391 (defun notmuch-search-reply-to-thread ()
1392 "Begin composing a reply to the entire current thread in a new buffer."
1394 (let ((message-id (notmuch-search-find-thread-id)))
1395 (notmuch-reply message-id)))
1397 (defun notmuch-call-notmuch-process (&rest args)
1398 "Synchronously invoke \"notmuch\" with the given list of arguments.
1400 Output from the process will be presented to the user as an error
1401 and will also appear in a buffer named \"*Notmuch errors*\"."
1402 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
1403 (with-current-buffer error-buffer
1405 (if (eq (apply 'call-process notmuch-command nil error-buffer nil args) 0)
1408 (with-current-buffer error-buffer
1409 (let ((beg (point-min))
1410 (end (- (point-max) 1)))
1411 (error (buffer-substring beg end))
1414 (defun notmuch-search-set-tags (tags)
1417 (re-search-backward "(")
1420 (inhibit-read-only t))
1421 (re-search-forward ")")
1423 (let ((end (point)))
1424 (delete-region beg end)
1425 (insert (mapconcat 'identity tags " "))))))
1427 (defun notmuch-search-get-tags ()
1430 (re-search-backward "(")
1431 (let ((beg (+ (point) 1)))
1432 (re-search-forward ")")
1433 (let ((end (- (point) 1)))
1434 (split-string (buffer-substring beg end))))))
1436 (defun notmuch-search-add-tag (tag)
1437 "Add a tag to the currently selected thread.
1439 The tag is added to messages in the currently selected thread
1440 which match the current search terms."
1442 (list (notmuch-select-tag-with-completion "Tag to add: ")))
1443 (notmuch-call-notmuch-process "tag" (concat "+" tag) (notmuch-search-find-thread-id))
1444 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
1446 (defun notmuch-search-remove-tag (tag)
1447 "Remove a tag from the currently selected thread.
1449 The tag is removed from all messages in the currently selected thread."
1451 (list (notmuch-select-tag-with-completion "Tag to remove: " (notmuch-search-find-thread-id))))
1452 (notmuch-call-notmuch-process "tag" (concat "-" tag) (notmuch-search-find-thread-id))
1453 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
1455 (defun notmuch-search-archive-thread ()
1456 "Archive the currently selected thread (remove its \"inbox\" tag).
1458 This function advances the next thread when finished."
1460 (notmuch-search-remove-tag "inbox")
1463 (defun notmuch-search-process-sentinel (proc msg)
1464 "Add a message to let user know when \"notmuch search\" exits"
1465 (let ((buffer (process-buffer proc))
1466 (status (process-status proc))
1467 (exit-status (process-exit-status proc))
1468 (never-found-target-thread nil))
1469 (if (memq status '(exit signal))
1470 (if (buffer-live-p buffer)
1471 (with-current-buffer buffer
1473 (let ((inhibit-read-only t)
1475 (goto-char (point-max))
1476 (if (eq status 'signal)
1477 (insert "Incomplete search results (search process was killed).\n"))
1478 (if (eq status 'exit)
1480 (insert "End of search results.")
1481 (if (not (= exit-status 0))
1482 (insert (format " (process returned %d)" exit-status)))
1485 (not (string= notmuch-search-target-thread "found")))
1486 (set 'never-found-target-thread t))))))
1487 (if (and never-found-target-thread
1488 notmuch-search-target-line)
1489 (goto-line notmuch-search-target-line)))))))
1491 (defun notmuch-search-process-filter (proc string)
1492 "Process and filter the output of \"notmuch search\""
1493 (let ((buffer (process-buffer proc))
1495 (if (buffer-live-p buffer)
1496 (with-current-buffer buffer
1500 (inhibit-read-only t))
1502 (if (string-match "^\\(thread:[0-9A-Fa-f]*\\) \\(.*\\) \\(\\[[0-9/]*\\]\\) \\([^;]*\\); \\(.*\\) (\\([^()]*\\))$" string line)
1503 (let* ((thread-id (match-string 1 string))
1504 (date (match-string 2 string))
1505 (count (match-string 3 string))
1506 (authors (match-string 4 string))
1507 (authors-length (length authors))
1508 (subject (match-string 5 string))
1509 (tags (match-string 6 string)))
1510 (if (> authors-length notmuch-search-authors-width)
1511 (set 'authors (concat (substring authors 0 (- notmuch-search-authors-width 3)) "...")))
1512 (goto-char (point-max))
1513 (let ((beg (point-marker))
1514 (format-string (format "%%s %%-7s %%-%ds %%s (%%s)\n" notmuch-search-authors-width)))
1515 (insert (format format-string date count authors subject tags))
1516 (put-text-property beg (point-marker) 'notmuch-search-thread-id thread-id)
1517 (put-text-property beg (point-marker) 'notmuch-search-authors authors)
1518 (put-text-property beg (point-marker) 'notmuch-search-subject subject)
1519 (if (string= thread-id notmuch-search-target-thread)
1521 (set 'found-target beg)
1522 (set 'notmuch-search-target-thread "found"))))
1523 (set 'line (match-end 0)))
1526 (goto-char found-target)))
1527 (delete-process proc))))
1529 (defun notmuch-search-operate-all (action)
1530 "Add/remove tags from all matching messages.
1532 Tis command adds or removes tags from all messages matching the
1533 current search terms. When called interactively, this command
1534 will prompt for tags to be added or removed. Tags prefixed with
1535 '+' will be added and tags prefixed with '-' will be removed.
1537 Each character of the tag name may consist of alphanumeric
1538 characters as well as `_.+-'.
1540 (interactive "sOperation (+add -drop): notmuch tag ")
1541 (let ((action-split (split-string action " +")))
1542 ;; Perform some validation
1543 (let ((words action-split))
1544 (when (null words) (error "No operation given"))
1546 (unless (string-match-p "^[-+][-+_.[:word:]]+$" (car words))
1547 (error "Action must be of the form `+thistag -that_tag'"))
1548 (setq words (cdr words))))
1549 (apply 'notmuch-call-notmuch-process "tag"
1550 (append action-split (list notmuch-search-query-string) nil))))
1553 (defun notmuch-search (query &optional oldest-first target-thread target-line)
1554 "Run \"notmuch search\" with the given query string and display results.
1556 The optional parameters are used as follows:
1558 oldest-first: A Boolean controlling the sort order of returned threads
1559 target-thread: A thread ID (with the thread: prefix) that will be made
1560 current if it appears in the search results.
1561 target-line: The line number to move to if the target thread does not
1562 appear in the search results."
1563 (interactive "sNotmuch search: ")
1564 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
1565 (switch-to-buffer buffer)
1566 (notmuch-search-mode)
1567 (set 'notmuch-search-query-string query)
1568 (set 'notmuch-search-oldest-first oldest-first)
1569 (set 'notmuch-search-target-thread target-thread)
1570 (set 'notmuch-search-target-line target-line)
1571 (let ((proc (get-buffer-process (current-buffer)))
1572 (inhibit-read-only t))
1574 (error "notmuch search process already running for query `%s'" query)
1577 (goto-char (point-min))
1579 (let ((proc (start-process-shell-command
1580 "notmuch-search" buffer notmuch-command "search"
1581 (if oldest-first "--sort=oldest-first" "--sort=newest-first")
1582 (shell-quote-argument query))))
1583 (set-process-sentinel proc 'notmuch-search-process-sentinel)
1584 (set-process-filter proc 'notmuch-search-process-filter))))
1585 (run-hooks 'notmuch-search-hook)))
1587 (defun notmuch-search-refresh-view ()
1588 "Refresh the current view.
1590 Kills the current buffer and runs a new search with the same
1591 query string as the current search. If the current thread is in
1592 the new search results, then point will be placed on the same
1593 thread. Otherwise, point will be moved to attempt to be in the
1594 same relative position within the new buffer."
1596 (let ((target-line (line-number-at-pos))
1597 (oldest-first notmuch-search-oldest-first)
1598 (target-thread (notmuch-search-find-thread-id))
1599 (query notmuch-search-query-string))
1601 (notmuch-search query oldest-first target-thread target-line)
1602 (goto-char (point-min))
1605 (defun notmuch-search-toggle-order ()
1606 "Toggle the current search order.
1608 By default, the \"inbox\" view created by `notmuch' is displayed
1609 in chronological order (oldest thread at the beginning of the
1610 buffer), while any global searches created by `notmuch-search'
1611 are displayed in reverse-chronological order (newest thread at
1612 the beginning of the buffer).
1614 This command toggles the sort order for the current search.
1616 Note that any filtered searches created by
1617 `notmuch-search-filter' retain the search order of the parent
1620 (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
1621 (notmuch-search-refresh-view))
1623 (defun notmuch-search-filter (query)
1624 "Filter the current search results based on an additional query string.
1626 Runs a new search matching only messages that match both the
1627 current search results AND the additional query string provided."
1628 (interactive "sFilter search: ")
1629 (let ((grouped-query (if (string-match-p notmuch-search-disjunctive-regexp query) (concat "( " query " )") query)))
1630 (notmuch-search (concat notmuch-search-query-string " and " grouped-query) notmuch-search-oldest-first)))
1632 (defun notmuch-search-filter-by-tag (tag)
1633 "Filter the current search results based on a single tag.
1635 Runs a new search matching only messages that match both the
1636 current search results AND that are tagged with the given tag."
1638 (list (notmuch-select-tag-with-completion "Filter by tag: ")))
1639 (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
1644 "Run notmuch to display all mail with tag of 'inbox'"
1646 (notmuch-search "tag:inbox" notmuch-search-oldest-first))
1648 (setq mail-user-agent 'message-user-agent)
1650 (defvar notmuch-folder-mode-map
1651 (let ((map (make-sparse-keymap)))
1652 (define-key map "?" 'notmuch-help)
1653 (define-key map "x" 'kill-this-buffer)
1654 (define-key map "q" 'kill-this-buffer)
1655 (define-key map "m" 'message-mail)
1656 (define-key map "e" 'notmuch-folder-show-empty-toggle)
1657 (define-key map ">" 'notmuch-folder-last)
1658 (define-key map "<" 'notmuch-folder-first)
1659 (define-key map "=" 'notmuch-folder)
1660 (define-key map "s" 'notmuch-search)
1661 (define-key map [mouse-1] 'notmuch-folder-show-search)
1662 (define-key map (kbd "RET") 'notmuch-folder-show-search)
1663 (define-key map " " 'notmuch-folder-show-search)
1664 (define-key map "p" 'notmuch-folder-previous)
1665 (define-key map "n" 'notmuch-folder-next)
1667 "Keymap for \"notmuch folder\" buffers.")
1669 (fset 'notmuch-folder-mode-map notmuch-folder-mode-map)
1671 (defcustom notmuch-folders (quote (("inbox" . "tag:inbox") ("unread" . "tag:unread")))
1672 "List of searches for the notmuch folder view"
1673 :type '(alist :key-type (string) :value-type (string))
1676 (defun notmuch-folder-mode ()
1677 "Major mode for showing notmuch 'folders'.
1679 This buffer contains a list of message counts returned by a
1680 customizable set of searches of your email archives. Each line in
1681 the buffer shows the name of a saved search and the resulting
1684 Pressing RET on any line opens a search window containing the
1685 results for the saved search on that line.
1687 Here is an example of how the search list could be
1688 customized, (the following text would be placed in your ~/.emacs
1691 (setq notmuch-folders '((\"inbox\" . \"tag:inbox\")
1692 (\"unread\" . \"tag:inbox AND tag:unread\")
1693 (\"notmuch\" . \"tag:inbox AND to:notmuchmail.org\")))
1695 Of course, you can have any number of folders, each configured
1696 with any supported search terms (see \"notmuch help search-terms\").
1698 Currently available key bindings:
1700 \\{notmuch-folder-mode-map}"
1702 (kill-all-local-variables)
1703 (use-local-map 'notmuch-folder-mode-map)
1704 (setq truncate-lines t)
1706 (setq major-mode 'notmuch-folder-mode
1707 mode-name "notmuch-folder")
1708 (setq buffer-read-only t))
1710 (defun notmuch-folder-next ()
1711 "Select the next folder in the list."
1717 (defun notmuch-folder-previous ()
1718 "Select the previous folder in the list."
1722 (defun notmuch-folder-first ()
1723 "Select the first folder in the list."
1725 (goto-char (point-min)))
1727 (defun notmuch-folder-last ()
1728 "Select the last folder in the list."
1730 (goto-char (point-max))
1733 (defun notmuch-folder-count (search)
1734 (car (process-lines notmuch-command "count" search)))
1736 (setq notmuch-folder-show-empty t)
1738 (defun notmuch-folder-show-empty-toggle ()
1739 "Toggle the listing of empty folders"
1741 (setq notmuch-folder-show-empty (not notmuch-folder-show-empty))
1744 (defun notmuch-folder-add (folders)
1746 (let* ((name (car (car folders)))
1747 (inhibit-read-only t)
1748 (search (cdr (car folders)))
1749 (count (notmuch-folder-count search)))
1750 (if (or notmuch-folder-show-empty
1751 (not (equal count "0")))
1759 (notmuch-folder-add (cdr folders)))))
1761 (defun notmuch-folder-find-name ()
1764 (let ((beg (point)))
1765 (re-search-forward "\\([ \t]*[^ \t]+\\)")
1766 (filter-buffer-substring (match-beginning 1) (match-end 1)))))
1768 (defun notmuch-folder-show-search (&optional folder)
1769 "Show a search window for the search related to the specified folder."
1772 (setq folder (notmuch-folder-find-name)))
1773 (let ((search (assoc folder notmuch-folders)))
1775 (notmuch-search (cdr search) notmuch-search-oldest-first))))
1778 (defun notmuch-folder ()
1779 "Show the notmuch folder view and update the displayed counts."
1781 (let ((buffer (get-buffer-create "*notmuch-folders*")))
1782 (switch-to-buffer buffer)
1783 (let ((inhibit-read-only t)
1784 (n (line-number-at-pos)))
1786 (notmuch-folder-mode)
1787 (notmuch-folder-add notmuch-folders)
1788 (goto-char (point-min))