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).
53 (defvar notmuch-show-mode-map
54 (let ((map (make-sparse-keymap)))
55 ; I don't actually want all of these toggle commands occupying
56 ; keybindings. They steal valuable key-binding space, are hard
57 ; to remember, and act globally rather than locally.
59 ; Will be much preferable to switch to direct manipulation for
60 ; toggling visibility of these components. Probably using
61 ; overlays-at to query and manipulate the current overlay.
62 (define-key map "a" 'notmuch-show-archive-thread)
63 (define-key map "A" 'notmuch-show-mark-read-then-archive-thread)
64 (define-key map "b" 'notmuch-show-toggle-body-read-visible)
65 (define-key map "m" 'message-mail)
66 (define-key map "n" 'notmuch-show-next-message)
67 (define-key map "N" 'notmuch-show-mark-read-then-next-open-message)
68 (define-key map "p" 'notmuch-show-previous-message)
69 (define-key map (kbd "C-n") 'notmuch-show-next-line)
70 (define-key map (kbd "C-p") 'notmuch-show-previous-line)
71 (define-key map "q" 'kill-this-buffer)
72 (define-key map "r" 'notmuch-show-reply)
73 (define-key map "v" 'notmuch-show-view-all-mime-parts)
74 (define-key map "w" 'notmuch-show-view-raw-message)
75 (define-key map "x" 'kill-this-buffer)
76 (define-key map "+" 'notmuch-show-add-tag)
77 (define-key map "-" 'notmuch-show-remove-tag)
78 (define-key map (kbd "DEL") 'notmuch-show-rewind)
79 (define-key map " " 'notmuch-show-advance-marking-read-and-archiving)
80 (define-key map "|" 'notmuch-show-pipe-message)
81 (define-key map "?" 'describe-mode)
82 (define-key map (kbd "TAB") 'notmuch-show-next-button)
83 (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
85 "Keymap for \"notmuch show\" buffers.")
86 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
88 (defvar notmuch-show-signature-regexp "\\(-- ?\\|_+\\)$"
89 "Pattern to match a line that separates content from signature.
91 The regexp can (and should) include $ to match the end of the
92 line, but should not include ^ to match the beginning of the
93 line. This is because notmuch may have inserted additional space
94 for indentation at the beginning of the line. But notmuch will
95 move past the indentation when testing this pattern, (so that the
96 pattern can still test against the entire line).")
98 (defvar notmuch-show-signature-lines-max 12
99 "Maximum length of signature that will be hidden by default.")
101 (defvar notmuch-command "notmuch"
102 "Command to run the notmuch binary.")
104 (defvar notmuch-show-message-begin-regexp "\fmessage{")
105 (defvar notmuch-show-message-end-regexp "\fmessage}")
106 (defvar notmuch-show-header-begin-regexp "\fheader{")
107 (defvar notmuch-show-header-end-regexp "\fheader}")
108 (defvar notmuch-show-body-begin-regexp "\fbody{")
109 (defvar notmuch-show-body-end-regexp "\fbody}")
110 (defvar notmuch-show-attachment-begin-regexp "\fattachment{")
111 (defvar notmuch-show-attachment-end-regexp "\fattachment}")
112 (defvar notmuch-show-part-begin-regexp "\fpart{")
113 (defvar notmuch-show-part-end-regexp "\fpart}")
114 (defvar notmuch-show-marker-regexp "\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
116 (defvar notmuch-show-id-regexp "\\(id:[^ ]*\\)")
117 (defvar notmuch-show-depth-regexp " depth:\\([0-9]*\\) ")
118 (defvar notmuch-show-filename-regexp "filename:\\(.*\\)$")
119 (defvar notmuch-show-tags-regexp "(\\([^)]*\\))$")
121 (defvar notmuch-show-parent-buffer nil)
122 (defvar notmuch-show-body-read-visible nil)
123 (defvar notmuch-show-citations-visible nil)
124 (defvar notmuch-show-signatures-visible nil)
125 (defvar notmuch-show-headers-visible nil)
127 ; XXX: This should be a generic function in emacs somewhere, not here
128 (defun point-invisible-p ()
129 "Return whether the character at point is invisible.
131 Here visibility is determined by `buffer-invisibility-spec' and
132 the invisible property of any overlays for point. It doesn't have
133 anything to do with whether point is currently being displayed
134 within the current window."
135 (let ((prop (get-char-property (point) 'invisible)))
136 (if (eq buffer-invisibility-spec t)
138 (or (memq prop buffer-invisibility-spec)
139 (assq prop buffer-invisibility-spec)))))
141 (defun notmuch-show-next-line ()
142 "Like builtin `next-line' but ensuring we end on a visible character.
144 By advancing forward until reaching a visible character.
146 Unlike builtin `next-line' this version accepts no arguments."
148 (set 'this-command 'next-line)
149 (call-interactively 'next-line)
150 (while (point-invisible-p)
153 (defun notmuch-show-previous-line ()
154 "Like builtin `previous-line' but ensuring we end on a visible character.
156 By advancing forward until reaching a visible character.
158 Unlike builtin `next-line' this version accepts no arguments."
160 (set 'this-command 'previous-line)
161 (call-interactively 'previous-line)
162 (while (point-invisible-p)
165 (defun notmuch-show-get-message-id ()
168 (if (not (looking-at notmuch-show-message-begin-regexp))
169 (re-search-backward notmuch-show-message-begin-regexp))
170 (re-search-forward notmuch-show-id-regexp)
171 (buffer-substring (match-beginning 1) (match-end 1))))
173 (defun notmuch-show-get-filename ()
176 (if (not (looking-at notmuch-show-message-begin-regexp))
177 (re-search-backward notmuch-show-message-begin-regexp))
178 (re-search-forward notmuch-show-filename-regexp)
179 (buffer-substring (match-beginning 1) (match-end 1))))
181 (defun notmuch-show-set-tags (tags)
184 (if (not (looking-at notmuch-show-message-begin-regexp))
185 (re-search-backward notmuch-show-message-begin-regexp))
186 (re-search-forward notmuch-show-tags-regexp)
187 (let ((inhibit-read-only t)
188 (beg (match-beginning 1))
190 (delete-region beg end)
192 (insert (mapconcat 'identity tags " ")))))
194 (defun notmuch-show-get-tags ()
197 (if (not (looking-at notmuch-show-message-begin-regexp))
198 (re-search-backward notmuch-show-message-begin-regexp))
199 (re-search-forward notmuch-show-tags-regexp)
200 (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
202 (defun notmuch-show-add-tag (&rest toadd)
203 "Add a tag to the current message."
204 (interactive "sTag to add: ")
205 (apply 'notmuch-call-notmuch-process
207 (mapcar (lambda (s) (concat "+" s)) toadd))
208 (cons (notmuch-show-get-message-id) nil)))
209 (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
211 (defun notmuch-show-remove-tag (&rest toremove)
212 "Remove a tag from the current message."
213 (interactive "sTag to remove: ")
214 (let ((tags (notmuch-show-get-tags)))
215 (if (intersection tags toremove :test 'string=)
217 (apply 'notmuch-call-notmuch-process
219 (mapcar (lambda (s) (concat "-" s)) toremove))
220 (cons (notmuch-show-get-message-id) nil)))
221 (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
223 (defun notmuch-show-archive-thread-maybe-mark-read (markread)
225 (goto-char (point-min))
228 (notmuch-show-remove-tag "unread" "inbox")
229 (notmuch-show-remove-tag "inbox"))
232 (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
233 (goto-char (point-max)))))
234 (let ((parent-buffer notmuch-show-parent-buffer))
238 (switch-to-buffer parent-buffer)
240 (notmuch-search-show-thread)))))
242 (defun notmuch-show-mark-read-then-archive-thread ()
243 "Remove \"unread\" tag from each message, then archive and show next thread.
245 Archive each message currently shown by removing the \"unread\"
246 and \"inbox\" tag from each. Then kill this buffer and show the
247 next thread from the search from which this thread was originally
250 Note: This command is safe from any race condition of new messages
251 being delivered to the same thread. It does not archive the
252 entire thread, but only the messages shown in the current
255 (notmuch-show-archive-thread-maybe-mark-read t))
257 (defun notmuch-show-archive-thread ()
258 "Archive each message in thread, and show next thread from search.
260 Archive each message currently shown by removing the \"inbox\"
261 tag from each. Then kill this buffer and show the next thread
262 from the search from which this thread was originally shown.
264 Note: This command is safe from any race condition of new messages
265 being delivered to the same thread. It does not archive the
266 entire thread, but only the messages shown in the current
269 (notmuch-show-archive-thread-maybe-mark-read nil))
271 (defun notmuch-show-view-raw-message ()
272 "View the raw email of the current message."
274 (view-file (notmuch-show-get-filename)))
276 (defun notmuch-show-view-all-mime-parts ()
277 "Use external viewers (according to mailcap) to view all MIME-encoded parts."
280 (let ((filename (notmuch-show-get-filename)))
281 (switch-to-buffer (generate-new-buffer (concat "*notmuch-mime-"
284 (insert-file-contents filename nil nil nil t)
285 (mm-display-parts (mm-dissect-buffer))
286 (kill-this-buffer))))
288 (defun notmuch-reply (query-string)
289 (switch-to-buffer (generate-new-buffer "notmuch-draft"))
290 (call-process notmuch-command nil t nil "reply" query-string)
291 (message-insert-signature)
292 (goto-char (point-min))
293 (if (re-search-forward "^$" nil t)
295 (insert "--text follows this line--")
299 (defun notmuch-show-reply ()
300 "Begin composing a reply to the current message in a new buffer."
302 (let ((message-id (notmuch-show-get-message-id)))
303 (notmuch-reply message-id)))
305 (defun notmuch-show-pipe-message (command)
306 "Pipe the contents of the current message to the given command.
308 The given command will be executed with the raw contents of the
309 current email message as stdin. Anything printed by the command
310 to stdout or stderr will appear in the *Messages* buffer."
311 (interactive "sPipe message to command: ")
312 (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*"
313 (list command " < " (shell-quote-argument (notmuch-show-get-filename)))))
315 (defun notmuch-show-move-to-current-message-summary-line ()
316 "Move to the beginning of the one-line summary of the current message.
318 This gives us a stable place to move to and work from since the
319 summary line is always visible. This is important since moving to
320 an invisible location is unreliable, (the main command loop moves
321 point either forward or backward to the next visible character
322 when a command ends with point on an invisible character).
324 Emits an error if point is not within a valid message, (that is
325 not pattern of `notmuch-show-message-begin-regexp' could be found
326 by searching backward)."
328 (if (not (looking-at notmuch-show-message-begin-regexp))
329 (if (re-search-backward notmuch-show-message-begin-regexp nil t)
331 (error "Not within a valid message."))
334 (defun notmuch-show-last-message-p ()
335 "Predicate testing whether point is within the last message."
336 (save-window-excursion
338 (notmuch-show-move-to-current-message-summary-line)
339 (not (re-search-forward notmuch-show-message-begin-regexp nil t)))))
341 (defun notmuch-show-message-unread-p ()
342 "Preficate testing whether current message is unread."
343 (member "unread" (notmuch-show-get-tags)))
345 (defun notmuch-show-next-message ()
346 "Advance to the beginning of the next message in the buffer.
348 Moves to the last visible character of the current message if
349 already on the last message in the buffer."
351 (notmuch-show-move-to-current-message-summary-line)
352 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
353 (notmuch-show-move-to-current-message-summary-line)
354 (goto-char (- (point-max) 1))
355 (while (point-invisible-p)
359 (defun notmuch-show-find-next-message ()
360 "Returns the position of the next message in the buffer.
362 Or the position of the last visible character of the current
363 message if already within the last message in the buffer."
364 ; save-excursion doesn't save our window position
365 ; save-window-excursion doesn't save point
366 ; Looks like we have to use both.
368 (save-window-excursion
369 (notmuch-show-next-message)
372 (defun notmuch-show-next-unread-message ()
373 "Advance to the beginning of the next unread message in the buffer.
375 Moves to the last visible character of the current message if
376 there are no more unread messages past the current point."
377 (notmuch-show-next-message)
378 (while (and (not (notmuch-show-last-message-p))
379 (not (notmuch-show-message-unread-p)))
380 (notmuch-show-next-message))
381 (if (not (notmuch-show-message-unread-p))
382 (notmuch-show-next-message)))
384 (defun notmuch-show-next-open-message ()
385 "Advance to the next message which is not hidden.
387 If read messages are currently hidden, advance to the next unread
388 message. Otherwise, advance to the next message."
389 (if (or (memq 'notmuch-show-body-read buffer-invisibility-spec)
390 (assq 'notmuch-show-body-read buffer-invisibility-spec))
391 (notmuch-show-next-unread-message)
392 (notmuch-show-next-message)))
394 (defun notmuch-show-previous-message ()
395 "Backup to the beginning of the previous message in the buffer.
397 If within a message rather than at the beginning of it, then
398 simply move to the beginning of the current message."
400 (let ((start (point)))
401 (notmuch-show-move-to-current-message-summary-line)
402 (if (not (< (point) start))
403 ; Go backward twice to skip the current message's marker
405 (re-search-backward notmuch-show-message-begin-regexp nil t)
406 (re-search-backward notmuch-show-message-begin-regexp nil t)
407 (notmuch-show-move-to-current-message-summary-line)
411 (defun notmuch-show-find-previous-message ()
412 "Returns the position of the previous message in the buffer.
414 Or the position of the beginning of the current message if point
415 is originally within the message rather than at the beginning of
417 ; save-excursion doesn't save our window position
418 ; save-window-excursion doesn't save point
419 ; Looks like we have to use both.
421 (save-window-excursion
422 (notmuch-show-previous-message)
425 (defun notmuch-show-mark-read-then-next-open-message ()
426 "Remove unread tag from current message, then advance to next unread message."
428 (notmuch-show-remove-tag "unread")
429 (notmuch-show-next-open-message))
431 (defun notmuch-show-rewind ()
432 "Do reverse scrolling compared to `notmuch-show-advance-marking-read-and-archiving'
434 Specifically, if the beginning of the previous email is fewer
435 than `window-height' lines from the current point, move to it
436 just like `notmuch-show-previous-message'.
438 Otherwise, just scroll down a screenful of the current message.
440 This command does not modify any message tags, (it does not undo
441 any effects from previous calls to
442 `notmuch-show-advance-marking-read-and-archiving'."
444 (let ((previous (notmuch-show-find-previous-message)))
445 (if (> (count-lines previous (point)) (- (window-height) next-screen-context-lines))
449 ((beginning-of-buffer) nil))
450 (goto-char (window-start)))
451 (notmuch-show-previous-message))))
453 (defun notmuch-show-advance-marking-read-and-archiving ()
454 "Advance through buffer, marking read and archiving.
456 This command is intended to be one of the simplest ways to
457 process a thread of email. It does the following:
459 If the current message in the thread is not yet fully visible,
460 scroll by a near screenful to read more of the message.
462 Otherwise, (the end of the current message is already within the
463 current window), remove the \"unread\" tag (if present) from the
464 current message and advance to the next open message.
466 Finally, if there is no further message to advance to, and this
467 last message is already read, then archive the entire current
468 thread, (remove the \"inbox\" tag from each message). Also kill
469 this buffer, and display the next thread from the search from
470 which this thread was originally shown."
472 (let ((next (notmuch-show-find-next-message))
473 (unread (notmuch-show-message-unread-p)))
474 (if (> next (window-end))
476 (let ((last (notmuch-show-last-message-p)))
477 (notmuch-show-mark-read-then-next-open-message)
479 (notmuch-show-archive-thread))))))
481 (defun notmuch-show-next-button ()
482 "Advance point to the next button in the buffer."
484 (goto-char (button-start (next-button (point)))))
486 (defun notmuch-show-previous-button ()
487 "Move point back to the previous button in the buffer."
489 (goto-char (button-start (previous-button (point)))))
491 (defun notmuch-toggle-invisible-action (cite-button)
492 (let ((invis-spec (button-get button 'invisibility-spec)))
493 (if (invisible-p invis-spec)
494 (remove-from-invisibility-spec invis-spec)
495 (add-to-invisibility-spec invis-spec)
497 (force-window-update)
500 (define-button-type 'notmuch-button-invisibility-toggle-type 'action 'notmuch-toggle-invisible-action 'follow-link t)
501 (define-button-type 'notmuch-button-citation-toggle-type 'help-echo "mouse-1, RET: Show citation"
502 :supertype 'notmuch-button-invisibility-toggle-type)
503 (define-button-type 'notmuch-button-signature-toggle-type 'help-echo "mouse-1, RET: Show signature"
504 :supertype 'notmuch-button-invisibility-toggle-type)
505 (define-button-type 'notmuch-button-headers-toggle-type 'help-echo "mouse-1, RET: Show headers"
506 :supertype 'notmuch-button-invisibility-toggle-type)
507 (define-button-type 'notmuch-button-body-toggle-type 'help-echo "mouse-1, RET: Show message"
508 :supertype 'notmuch-button-invisibility-toggle-type)
510 (defun notmuch-show-markup-citations-region (beg end depth)
513 (while (< (point) end)
514 (let ((beg-sub (point-marker))
515 (indent (make-string depth ? ))
516 (citation "[[:space:]]*>"))
517 (if (looking-at citation)
519 (while (looking-at citation)
521 (let ((overlay (make-overlay beg-sub (point)))
522 (invis-spec (make-symbol "notmuch-citation-region")))
523 (add-to-invisibility-spec invis-spec)
524 (overlay-put overlay 'invisible invis-spec)
527 (concat "[" (number-to-string (count-lines beg-sub (point)))
528 "-line citation.]")))
529 (goto-char (- beg-sub 1))
530 (insert (concat "\n" indent))
531 (insert-button cite-button-text
532 'invisibility-spec invis-spec
533 :type 'notmuch-button-citation-toggle-type)
535 (goto-char (+ (length cite-button-text) p))
537 (move-to-column depth)
538 (if (looking-at notmuch-show-signature-regexp)
539 (let ((sig-lines (- (count-lines beg-sub end) 1)))
540 (if (<= sig-lines notmuch-show-signature-lines-max)
542 (let ((invis-spec (make-symbol "notmuch-signature-region")))
543 (add-to-invisibility-spec invis-spec)
544 (overlay-put (make-overlay beg-sub end)
545 'invisible invis-spec)
547 (goto-char (- beg-sub 1))
548 (insert (concat "\n" indent))
549 (let ((sig-button-text (concat "[" (number-to-string sig-lines)
550 "-line signature.]")))
551 (insert-button sig-button-text 'invisibility-spec invis-spec
552 :type 'notmuch-button-signature-toggle-type)
558 (defun notmuch-show-markup-part (beg end depth)
559 (if (re-search-forward notmuch-show-part-begin-regexp nil t)
562 (let ((beg (point-marker)))
563 (re-search-forward notmuch-show-part-end-regexp)
564 (let ((end (copy-marker (match-beginning 0))))
568 (indent-rigidly beg end depth)
569 (notmuch-show-markup-citations-region beg end depth)
570 ; Advance to the next part (if any) (so the outer loop can
571 ; determine whether we've left the current message.
572 (if (re-search-forward notmuch-show-part-begin-regexp nil t)
573 (beginning-of-line)))))
576 (defun notmuch-show-markup-parts-region (beg end depth)
579 (while (< (point) end)
580 (notmuch-show-markup-part beg end depth))))
582 (defun notmuch-show-markup-body (depth btn)
583 (re-search-forward notmuch-show-body-begin-regexp)
585 (let ((beg (point-marker)))
586 (re-search-forward notmuch-show-body-end-regexp)
587 (let ((end (copy-marker (match-beginning 0))))
588 (notmuch-show-markup-parts-region beg end depth)
589 (let ((invis-spec (make-symbol "notmuch-show-body-read")))
590 (overlay-put (make-overlay beg end)
591 'invisible invis-spec)
592 (button-put btn 'invisibility-spec invis-spec)
593 (if (not (notmuch-show-message-unread-p))
594 (add-to-invisibility-spec invis-spec)))
599 (defun notmuch-show-markup-header (depth)
600 (re-search-forward notmuch-show-header-begin-regexp)
602 (let ((beg (point-marker))
605 ; Inverse video for subject
606 (overlay-put (make-overlay beg (point)) 'face '(:inverse-video t))
607 (setq btn (make-button beg (point) :type 'notmuch-button-body-toggle-type))
610 (let ((beg-hidden (point-marker)))
611 (re-search-forward notmuch-show-header-end-regexp)
613 (let ((end (point-marker)))
616 (while (looking-at "[A-Za-z][-A-Za-z0-9]*:")
618 (overlay-put (make-overlay (point) (re-search-forward ":"))
622 (indent-rigidly beg end depth)
623 (let ((invis-spec (make-symbol "notmuch-show-header")))
624 (add-to-invisibility-spec (cons invis-spec t))
625 (overlay-put (make-overlay beg-hidden end)
626 'invisible invis-spec)
629 (make-button (line-beginning-position) (line-end-position)
630 'invisibility-spec (cons invis-spec t)
631 :type 'notmuch-button-headers-toggle-type))
635 (set-marker beg-hidden nil)
640 (defun notmuch-show-markup-message ()
641 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
643 (re-search-forward notmuch-show-depth-regexp)
644 (let ((depth (string-to-number (buffer-substring (match-beginning 1) (match-end 1))))
646 (setq btn (notmuch-show-markup-header depth))
647 (notmuch-show-markup-body depth btn)))
648 (goto-char (point-max))))
650 (defun notmuch-show-hide-markers ()
652 (goto-char (point-min))
654 (if (re-search-forward notmuch-show-marker-regexp nil t)
656 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
657 'invisible 'notmuch-show-marker))
658 (goto-char (point-max))))))
660 (defun notmuch-show-markup-messages ()
662 (goto-char (point-min))
664 (notmuch-show-markup-message)))
665 (notmuch-show-hide-markers))
668 (defun notmuch-show-mode ()
669 "Major mode for viewing a thread with notmuch.
671 This buffer contains the results of the \"notmuch show\" command
672 for displaying a single thread of email from your email archives.
674 By default, various components of email messages, (citations,
675 signatures, already-read messages), are invisible to help you
676 focus on the most important things, (new text from unread
677 messages). See the various commands below for toggling the
678 visibility of hidden components.
680 The `notmuch-show-next-message' and
681 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
682 default), allow you to navigate to the next and previous
683 messages. Each time you navigate away from a message with
684 `notmuch-show-next-message' the current message will have its
685 \"unread\" tag removed.
687 You can add or remove tags from the current message with '+' and
688 '-'. You can also archive all messages in the current
689 view, (remove the \"inbox\" tag from each), with
690 `notmuch-show-archive-thread' (bound to 'a' by default).
692 \\{notmuch-show-mode-map}"
694 (kill-all-local-variables)
695 (add-to-invisibility-spec 'notmuch-show-marker)
696 (use-local-map notmuch-show-mode-map)
697 (setq major-mode 'notmuch-show-mode
698 mode-name "notmuch-show")
699 (setq buffer-read-only t))
703 (defgroup notmuch nil
704 "Notmuch mail reader for Emacs."
707 (defcustom notmuch-show-hook nil
708 "List of functions to call when notmuch displays a message."
710 :options '(goto-address)
713 (defcustom notmuch-search-hook nil
714 "List of functions to call when notmuch displays the search results."
716 :options '(hl-line-mode)
719 ; Make show mode a bit prettier, highlighting URLs and using word wrap
721 (defun notmuch-show-pretty-hook ()
722 (goto-address-mode 1)
725 (add-hook 'notmuch-show-hook 'notmuch-show-pretty-hook)
726 (add-hook 'notmuch-search-hook
730 (defun notmuch-show (thread-id &optional parent-buffer)
731 "Run \"notmuch show\" with the given thread ID and display results.
733 The optional PARENT-BUFFER is the notmuch-search buffer from
734 which this notmuch-show command was executed, (so that the next
735 thread from that buffer can be show when done with this one)."
736 (interactive "sNotmuch show: ")
737 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
738 (switch-to-buffer buffer)
740 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
741 (let ((proc (get-buffer-process (current-buffer)))
742 (inhibit-read-only t))
744 (error "notmuch search process already running for query `%s'" thread-id)
747 (goto-char (point-min))
749 (call-process notmuch-command nil t nil "show" thread-id)
750 (notmuch-show-markup-messages)
752 (run-hooks 'notmuch-show-hook)
753 ; Move straight to the first unread message
754 (if (not (notmuch-show-message-unread-p))
756 (notmuch-show-next-unread-message)
757 ; But if there are no unread messages, go back to the
758 ; beginning of the buffer, and open up the bodies of all
760 (if (not (notmuch-show-message-unread-p))
762 (goto-char (point-min))
763 (let ((btn (forward-button 1)))
765 (if (button-has-type-p btn 'notmuch-button-body-toggle-type)
768 (setq btn (forward-button 1))
769 (error (setq btn nil)))
771 (beginning-of-buffer)
775 (defvar notmuch-search-authors-width 40
776 "Number of columns to use to display authors in a notmuch-search buffer.")
778 (defvar notmuch-search-mode-map
779 (let ((map (make-sparse-keymap)))
780 (define-key map "a" 'notmuch-search-archive-thread)
781 (define-key map "b" 'notmuch-search-scroll-down)
782 (define-key map "f" 'notmuch-search-filter)
783 (define-key map "m" 'message-mail)
784 (define-key map "n" 'next-line)
785 (define-key map "o" 'notmuch-search-toggle-order)
786 (define-key map "p" 'previous-line)
787 (define-key map "q" 'kill-this-buffer)
788 (define-key map "r" 'notmuch-search-reply-to-thread)
789 (define-key map "s" 'notmuch-search)
790 (define-key map "t" 'notmuch-search-filter-by-tag)
791 (define-key map "x" 'kill-this-buffer)
792 (define-key map (kbd "RET") 'notmuch-search-show-thread)
793 (define-key map [mouse-1] 'notmuch-search-show-thread)
794 (define-key map "+" 'notmuch-search-add-tag)
795 (define-key map "-" 'notmuch-search-remove-tag)
796 (define-key map "<" 'beginning-of-buffer)
797 (define-key map ">" 'notmuch-search-goto-last-thread)
798 (define-key map "=" 'notmuch-search-refresh-view)
799 (define-key map "\M->" 'notmuch-search-goto-last-thread)
800 (define-key map " " 'notmuch-search-scroll-up)
801 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
802 (define-key map "?" 'describe-mode)
804 "Keymap for \"notmuch search\" buffers.")
805 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
807 (defvar notmuch-search-query-string)
808 (defvar notmuch-search-oldest-first)
811 (defun notmuch-search-scroll-up ()
812 "Scroll up, moving point to last message in thread if at end."
816 ((end-of-buffer) (notmuch-search-goto-last-thread))))
818 (defun notmuch-search-scroll-down ()
819 "Scroll down, moving point to first message in thread if at beginning."
821 ; I don't know why scroll-down doesn't signal beginning-of-buffer
822 ; the way that scroll-up signals end-of-buffer, but c'est la vie.
824 ; So instead of trapping a signal we instead check whether the
825 ; window begins on the first line of the buffer and if so, move
826 ; directly to that position. (We have to count lines since the
827 ; window-start position is not the same as point-min due to the
828 ; invisible thread-ID characters on the first line.
829 (if (equal (count-lines (point-min) (window-start)) 1)
830 (goto-char (window-start))
833 (defun notmuch-search-goto-last-thread ()
834 "Move point to the last thread in the buffer."
836 (goto-char (point-max))
840 (defun notmuch-search-mode ()
841 "Major mode for searching mail with notmuch.
843 This buffer contains the results of a \"notmuch search\" of your
844 email archives. Each line in the buffer represents a single
845 thread giving a relative date for the thread and a subject.
847 Pressing RET on any line displays that thread. The '+' and '-'
848 keys can be used to add or remove tags from a thread. The 'a' key
849 is a convenience key for archiving a thread (removing the
852 Other useful commands are `notmuch-search-filter' for filtering
853 the current search based on an additional query string,
854 `notmuch-search-filter-by-tag' for filtering to include only
855 messages with a given tag, and `notmuch-search' to execute a new,
858 \\{notmuch-search-mode-map}"
860 (kill-all-local-variables)
861 (make-local-variable 'notmuch-search-query-string)
862 (make-local-variable 'notmuch-search-oldest-first)
863 (set (make-local-variable 'scroll-preserve-screen-position) t)
864 (add-to-invisibility-spec 'notmuch-search)
865 (use-local-map notmuch-search-mode-map)
866 (setq truncate-lines t)
867 (setq major-mode 'notmuch-search-mode
868 mode-name "notmuch-search")
869 (setq buffer-read-only t))
871 (defun notmuch-search-find-thread-id ()
875 (re-search-forward "thread:[a-fA-F0-9]*" nil t)
876 (filter-buffer-substring beg (point)))))
878 (defun notmuch-search-markup-this-thread-id ()
881 (if (re-search-forward "thread:[a-fA-F0-9]*" nil t)
884 (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)
885 (re-search-forward ".*\\[[0-9]*/[0-9]*\\] \\([^;]*\\)\\(;\\)")
886 (let* ((authors (buffer-substring (match-beginning 1) (match-end 1)))
887 (authors-length (length authors)))
888 ;; Drop the semi-colon
889 (replace-match "" t nil nil 2)
890 (if (<= authors-length notmuch-search-authors-width)
891 (replace-match (concat authors (make-string
892 (- notmuch-search-authors-width
893 authors-length) ? )) t t nil 1)
894 (replace-match (concat (substring authors 0 (- notmuch-search-authors-width 3)) "...") t t nil 1)))))))
896 (defun notmuch-search-markup-thread-ids ()
898 (goto-char (point-min))
900 (notmuch-search-markup-this-thread-id)
903 (defun notmuch-search-show-thread ()
905 (let ((thread-id (notmuch-search-find-thread-id)))
906 (if (> (length thread-id) 0)
907 (notmuch-show thread-id (current-buffer))
908 (error "End of search results"))))
910 (defun notmuch-search-reply-to-thread ()
911 "Begin composing a reply to the entire current thread in a new buffer."
913 (let ((message-id (notmuch-search-find-thread-id)))
914 (notmuch-reply message-id)))
916 (defun notmuch-call-notmuch-process (&rest args)
917 "Synchronously invoke \"notmuch\" with the given list of arguments.
919 Output from the process will be presented to the user as an error
920 and will also appear in a buffer named \"*Notmuch errors*\"."
921 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
922 (with-current-buffer error-buffer
924 (if (eq (apply 'call-process notmuch-command nil error-buffer nil args) 0)
927 (with-current-buffer error-buffer
928 (let ((beg (point-min))
929 (end (- (point-max) 1)))
930 (error (buffer-substring beg end))
933 (defun notmuch-search-set-tags (tags)
936 (re-search-backward "(")
939 (inhibit-read-only t))
940 (re-search-forward ")")
943 (delete-region beg end)
944 (insert (mapconcat 'identity tags " "))))))
946 (defun notmuch-search-get-tags ()
949 (re-search-backward "(")
950 (let ((beg (+ (point) 1)))
951 (re-search-forward ")")
952 (let ((end (- (point) 1)))
953 (split-string (buffer-substring beg end))))))
955 (defun notmuch-search-add-tag (tag)
956 (interactive "sTag to add: ")
957 (notmuch-call-notmuch-process "tag" (concat "+" tag) (notmuch-search-find-thread-id))
958 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
960 (defun notmuch-search-remove-tag (tag)
961 (interactive "sTag to remove: ")
962 (notmuch-call-notmuch-process "tag" (concat "-" tag) (notmuch-search-find-thread-id))
963 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
965 (defun notmuch-search-archive-thread ()
966 "Archive the current thread (remove its \"inbox\" tag).
968 This function advances point to the next line when finished."
970 (notmuch-search-remove-tag "inbox")
973 (defun notmuch-search (query &optional oldest-first)
974 "Run \"notmuch search\" with the given query string and display results."
975 (interactive "sNotmuch search: ")
976 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
977 (switch-to-buffer buffer)
978 (notmuch-search-mode)
979 (set 'notmuch-search-query-string query)
980 (set 'notmuch-search-oldest-first oldest-first)
981 (let ((proc (get-buffer-process (current-buffer)))
982 (inhibit-read-only t))
984 (error "notmuch search process already running for query `%s'" query)
987 (goto-char (point-min))
990 (call-process notmuch-command nil t nil "search" "--sort=oldest-first" query)
991 (call-process notmuch-command nil t nil "search" "--sort=newest-first" query))
992 (notmuch-search-markup-thread-ids)
994 (run-hooks 'notmuch-search-hook)))
996 (defun notmuch-search-refresh-view ()
997 "Refresh the current view.
999 Kills the current buffer and runs a new search with the same
1000 query string as the current search. If the current thread is in
1001 the new search results, then point will be placed on the same
1002 thread. Otherwise, point will be moved to attempt to be in the
1003 same relative position within the new buffer."
1005 (let ((here (point))
1006 (oldest-first notmuch-search-oldest-first)
1007 (thread (notmuch-search-find-thread-id))
1008 (query notmuch-search-query-string))
1010 (notmuch-search query oldest-first)
1011 (goto-char (point-min))
1012 (if (re-search-forward (concat "^" thread) nil t)
1016 (defun notmuch-search-toggle-order ()
1017 "Toggle the current search order.
1019 By default, the \"inbox\" view created by `notmuch' is displayed
1020 in chronological order (oldest thread at the beginning of the
1021 buffer), while any global searches created by `notmuch-search'
1022 are displayed in reverse-chronological order (newest thread at
1023 the beginning of the buffer).
1025 This command toggles the sort order for the current search.
1027 Note that any filtered searches created by
1028 `notmuch-search-filter' retain the search order of the parent
1031 (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
1032 (notmuch-search-refresh-view))
1034 (defun notmuch-search-filter (query)
1035 "Filter the current search results based on an additional query string.
1037 Runs a new search matching only messages that match both the
1038 current search results AND the additional query string provided."
1039 (interactive "sFilter search: ")
1040 (notmuch-search (concat notmuch-search-query-string " and " query) notmuch-search-oldest-first))
1042 (defun notmuch-search-filter-by-tag (tag)
1043 "Filter the current search results based on a single tag.
1045 Runs a new search matching only messages that match both the
1046 current search results AND that are tagged with the given tag."
1047 (interactive "sFilter by tag: ")
1048 (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
1051 "Run notmuch to display all mail with tag of 'inbox'"
1053 (notmuch-search "tag:inbox" t))
1055 (setq mail-user-agent 'message-user-agent)
1057 (defvar notmuch-folder-mode-map
1058 (let ((map (make-sparse-keymap)))
1059 (define-key map "n" 'next-line)
1060 (define-key map "p" 'previous-line)
1061 (define-key map "x" 'kill-this-buffer)
1062 (define-key map "q" 'kill-this-buffer)
1063 (define-key map "s" 'notmuch-search)
1064 (define-key map (kbd "RET") 'notmuch-folder-show-search)
1065 (define-key map "<" 'beginning-of-buffer)
1066 (define-key map "=" 'notmuch-folder)
1067 (define-key map "?" 'describe-mode)
1068 (define-key map [mouse-1] 'notmuch-folder-show-search)
1070 "Keymap for \"notmuch folder\" buffers.")
1072 (fset 'notmuch-folder-mode-map notmuch-folder-mode-map)
1074 (defcustom notmuch-folders (quote (("inbox" . "tag:inbox") ("unread" . "tag:unread")))
1075 "List of searches for the notmuch folder view"
1076 :type '(alist :key-type (string) :value-type (string))
1079 (defun notmuch-folder-mode ()
1080 "Major mode for showing notmuch 'folders'.
1082 This buffer contains a list of messages counts returned by a
1083 customizable set of searches of your email archives. Each line
1084 in the buffer shows the search terms and the resulting message count.
1086 Pressing RET on any line opens a search window containing the
1087 results for the search terms in that line.
1089 \\{notmuch-folder-mode-map}"
1091 (kill-all-local-variables)
1092 (use-local-map 'notmuch-folder-mode-map)
1093 (setq truncate-lines t)
1095 (setq major-mode 'notmuch-folder-mode
1096 mode-name "notmuch-folder")
1097 (setq buffer-read-only t))
1099 (defun notmuch-folder-add (folders)
1101 (let ((name (car (car folders)))
1102 (inhibit-read-only t)
1103 (search (cdr (car folders))))
1106 (call-process notmuch-command nil t nil "count" search)
1107 (notmuch-folder-add (cdr folders)))))
1109 (defun notmuch-folder-find-name ()
1112 (let ((beg (point)))
1114 (filter-buffer-substring beg (point)))))
1116 (defun notmuch-folder-show-search (&optional folder)
1117 "Show a search window for the search related to the specified folder."
1120 (setq folder (notmuch-folder-find-name)))
1121 (let ((search (assoc folder notmuch-folders)))
1123 (notmuch-search (cdr search) t))))
1125 (defun notmuch-folder ()
1126 "Show the notmuch folder view and update the displayed counts."
1128 (let ((buffer (get-buffer-create "*notmuch-folders*")))
1129 (switch-to-buffer buffer)
1130 (let ((inhibit-read-only t)
1131 (n (line-number-at-pos)))
1133 (notmuch-folder-mode)
1134 (notmuch-folder-add notmuch-folders)
1135 (goto-char (point-min))