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 (defun notmuch-configure-invisibility-button (btn invis-spec help-msg)
501 (button-put btn 'invisibility-spec invis-spec)
502 (button-put btn 'action 'notmuch-toggle-invisible-action)
503 (button-put btn 'follow-link t)
504 (button-put btn 'help-echo (concat "mouse-1, RET: " help-msg))
507 (defun notmuch-show-markup-citations-region (beg end depth)
510 (while (< (point) end)
511 (let ((beg-sub (point-marker))
512 (indent (make-string depth ? ))
513 (citation "[[:space:]]*>"))
514 (if (looking-at citation)
516 (while (looking-at citation)
518 (let ((overlay (make-overlay beg-sub (point)))
519 (invis-spec (make-symbol "notmuch-citation-region")))
520 (add-to-invisibility-spec invis-spec)
521 (overlay-put overlay 'invisible invis-spec)
524 (concat "[" (number-to-string (count-lines beg-sub (point)))
525 "-line citation.]")))
526 (goto-char (- beg-sub 1))
527 (insert (concat "\n" indent))
528 (notmuch-configure-invisibility-button (insert-button cite-button-text) invis-spec "Show citation")
530 (goto-char (+ (length cite-button-text) p))
532 (move-to-column depth)
533 (if (looking-at notmuch-show-signature-regexp)
534 (let ((sig-lines (- (count-lines beg-sub end) 1)))
535 (if (<= sig-lines notmuch-show-signature-lines-max)
537 (let ((invis-spec (make-symbol "notmuch-signature-region")))
538 (add-to-invisibility-spec invis-spec)
539 (overlay-put (make-overlay beg-sub end)
540 'invisible invis-spec)
542 (goto-char (- beg-sub 1))
543 (insert (concat "\n" indent))
544 (notmuch-configure-invisibility-button
545 (insert-button (concat "[" (number-to-string sig-lines)
546 "-line signature.]")) invis-spec "Show signature")
551 (defun notmuch-show-markup-part (beg end depth)
552 (if (re-search-forward notmuch-show-part-begin-regexp nil t)
555 (let ((beg (point-marker)))
556 (re-search-forward notmuch-show-part-end-regexp)
557 (let ((end (copy-marker (match-beginning 0))))
561 (indent-rigidly beg end depth)
562 (notmuch-show-markup-citations-region beg end depth)
563 ; Advance to the next part (if any) (so the outer loop can
564 ; determine whether we've left the current message.
565 (if (re-search-forward notmuch-show-part-begin-regexp nil t)
566 (beginning-of-line)))))
569 (defun notmuch-show-markup-parts-region (beg end depth)
572 (while (< (point) end)
573 (notmuch-show-markup-part beg end depth))))
575 (defun notmuch-show-markup-body (depth)
576 (re-search-forward notmuch-show-body-begin-regexp)
578 (let ((beg (point-marker)))
579 (re-search-forward notmuch-show-body-end-regexp)
580 (let ((end (copy-marker (match-beginning 0))))
581 (notmuch-show-markup-parts-region beg end depth)
582 (if (not (notmuch-show-message-unread-p))
583 (overlay-put (make-overlay beg end)
584 'invisible 'notmuch-show-body-read))
589 (defun notmuch-show-markup-header (depth)
590 (re-search-forward notmuch-show-header-begin-regexp)
592 (let ((beg (point-marker)))
594 ; Inverse video for subject
595 (overlay-put (make-overlay beg (point)) 'face '(:inverse-video t))
598 (let ((beg-hidden (point-marker)))
599 (re-search-forward notmuch-show-header-end-regexp)
601 (let ((end (point-marker)))
604 (while (looking-at "[A-Za-z][-A-Za-z0-9]*:")
606 (overlay-put (make-overlay (point) (re-search-forward ":"))
610 (indent-rigidly beg end depth)
611 (let ((invis-spec (make-symbol "notmuch-show-header")))
612 (add-to-invisibility-spec (cons invis-spec t))
613 (overlay-put (make-overlay beg-hidden end)
614 'invisible invis-spec)
617 (notmuch-configure-invisibility-button
618 (make-button (line-beginning-position) (line-end-position))
619 (cons invis-spec t) "Show headers"))
623 (set-marker beg-hidden nil)
627 (defun notmuch-show-markup-message ()
628 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
630 (re-search-forward notmuch-show-depth-regexp)
631 (let ((depth (string-to-number (buffer-substring (match-beginning 1) (match-end 1)))))
632 (notmuch-show-markup-header depth)
633 (notmuch-show-markup-body depth)))
634 (goto-char (point-max))))
636 (defun notmuch-show-hide-markers ()
638 (goto-char (point-min))
640 (if (re-search-forward notmuch-show-marker-regexp nil t)
642 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
643 'invisible 'notmuch-show-marker))
644 (goto-char (point-max))))))
646 (defun notmuch-show-markup-messages ()
648 (goto-char (point-min))
650 (notmuch-show-markup-message)))
651 (notmuch-show-hide-markers))
653 (defun notmuch-show-toggle-body-read-visible ()
654 "Toggle visibility of message bodies of read messages"
656 (if notmuch-show-body-read-visible
657 (add-to-invisibility-spec 'notmuch-show-body-read)
658 (remove-from-invisibility-spec 'notmuch-show-body-read))
659 (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
660 ; Need to force the redisplay for some reason
661 (force-window-update)
665 (defun notmuch-show-mode ()
666 "Major mode for viewing a thread with notmuch.
668 This buffer contains the results of the \"notmuch show\" command
669 for displaying a single thread of email from your email archives.
671 By default, various components of email messages, (citations,
672 signatures, already-read messages), are invisible to help you
673 focus on the most important things, (new text from unread
674 messages). See the various commands below for toggling the
675 visibility of hidden components.
677 The `notmuch-show-next-message' and
678 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
679 default), allow you to navigate to the next and previous
680 messages. Each time you navigate away from a message with
681 `notmuch-show-next-message' the current message will have its
682 \"unread\" tag removed.
684 You can add or remove tags from the current message with '+' and
685 '-'. You can also archive all messages in the current
686 view, (remove the \"inbox\" tag from each), with
687 `notmuch-show-archive-thread' (bound to 'a' by default).
689 \\{notmuch-show-mode-map}"
691 (kill-all-local-variables)
692 (set (make-local-variable 'notmuch-show-headers-visible) t)
693 (notmuch-show-toggle-headers-visible)
694 (set (make-local-variable 'notmuch-show-body-read-visible) t)
695 (notmuch-show-toggle-body-read-visible)
696 (set (make-local-variable 'notmuch-show-citations-visible) t)
697 (notmuch-show-toggle-citations-visible)
698 (set (make-local-variable 'notmuch-show-signatures-visible) t)
699 (notmuch-show-toggle-signatures-visible)
700 (add-to-invisibility-spec 'notmuch-show-marker)
701 (use-local-map notmuch-show-mode-map)
702 (setq major-mode 'notmuch-show-mode
703 mode-name "notmuch-show")
704 (setq buffer-read-only t))
708 (defgroup notmuch nil
709 "Notmuch mail reader for Emacs."
712 (defcustom notmuch-show-hook nil
713 "List of functions to call when notmuch displays a message."
715 :options '(goto-address)
718 (defcustom notmuch-search-hook nil
719 "List of functions to call when notmuch displays the search results."
721 :options '(hl-line-mode)
724 ; Make show mode a bit prettier, highlighting URLs and using word wrap
726 (defun notmuch-show-pretty-hook ()
727 (goto-address-mode 1)
730 (add-hook 'notmuch-show-hook 'notmuch-show-pretty-hook)
731 (add-hook 'notmuch-search-hook
735 (defun notmuch-show (thread-id &optional parent-buffer)
736 "Run \"notmuch show\" with the given thread ID and display results.
738 The optional PARENT-BUFFER is the notmuch-search buffer from
739 which this notmuch-show command was executed, (so that the next
740 thread from that buffer can be show when done with this one)."
741 (interactive "sNotmuch show: ")
742 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
743 (switch-to-buffer buffer)
745 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
746 (let ((proc (get-buffer-process (current-buffer)))
747 (inhibit-read-only t))
749 (error "notmuch search process already running for query `%s'" thread-id)
752 (goto-char (point-min))
754 (call-process notmuch-command nil t nil "show" thread-id)
755 (notmuch-show-markup-messages)
757 (run-hooks 'notmuch-show-hook)
758 ; Move straight to the first unread message
759 (if (not (notmuch-show-message-unread-p))
761 (notmuch-show-next-unread-message)
762 ; But if there are no unread messages, go back to the
763 ; beginning of the buffer, and open up the bodies of all
765 (if (not (notmuch-show-message-unread-p))
767 (goto-char (point-min))
768 (notmuch-show-toggle-body-read-visible)))))
771 (defvar notmuch-search-authors-width 40
772 "Number of columns to use to display authors in a notmuch-search buffer.")
774 (defvar notmuch-search-mode-map
775 (let ((map (make-sparse-keymap)))
776 (define-key map "a" 'notmuch-search-archive-thread)
777 (define-key map "b" 'notmuch-search-scroll-down)
778 (define-key map "f" 'notmuch-search-filter)
779 (define-key map "m" 'message-mail)
780 (define-key map "n" 'next-line)
781 (define-key map "o" 'notmuch-search-toggle-order)
782 (define-key map "p" 'previous-line)
783 (define-key map "q" 'kill-this-buffer)
784 (define-key map "r" 'notmuch-search-reply-to-thread)
785 (define-key map "s" 'notmuch-search)
786 (define-key map "t" 'notmuch-search-filter-by-tag)
787 (define-key map "x" 'kill-this-buffer)
788 (define-key map (kbd "RET") 'notmuch-search-show-thread)
789 (define-key map [mouse-1] 'notmuch-search-show-thread)
790 (define-key map "+" 'notmuch-search-add-tag)
791 (define-key map "-" 'notmuch-search-remove-tag)
792 (define-key map "<" 'beginning-of-buffer)
793 (define-key map ">" 'notmuch-search-goto-last-thread)
794 (define-key map "=" 'notmuch-search-refresh-view)
795 (define-key map "\M->" 'notmuch-search-goto-last-thread)
796 (define-key map " " 'notmuch-search-scroll-up)
797 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
798 (define-key map "?" 'describe-mode)
800 "Keymap for \"notmuch search\" buffers.")
801 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
803 (defvar notmuch-search-query-string)
804 (defvar notmuch-search-oldest-first)
806 (defun notmuch-search-scroll-up ()
807 "Scroll up, moving point to last message in thread if at end."
811 ((end-of-buffer) (notmuch-search-goto-last-thread))))
813 (defun notmuch-search-scroll-down ()
814 "Scroll down, moving point to first message in thread if at beginning."
816 ; I don't know why scroll-down doesn't signal beginning-of-buffer
817 ; the way that scroll-up signals end-of-buffer, but c'est la vie.
819 ; So instead of trapping a signal we instead check whether the
820 ; window begins on the first line of the buffer and if so, move
821 ; directly to that position. (We have to count lines since the
822 ; window-start position is not the same as point-min due to the
823 ; invisible thread-ID characters on the first line.
824 (if (equal (count-lines (point-min) (window-start)) 1)
825 (goto-char (window-start))
828 (defun notmuch-search-goto-last-thread ()
829 "Move point to the last thread in the buffer."
831 (goto-char (point-max))
835 (defun notmuch-search-mode ()
836 "Major mode for searching mail with notmuch.
838 This buffer contains the results of a \"notmuch search\" of your
839 email archives. Each line in the buffer represents a single
840 thread giving a relative date for the thread and a subject.
842 Pressing RET on any line displays that thread. The '+' and '-'
843 keys can be used to add or remove tags from a thread. The 'a' key
844 is a convenience key for archiving a thread (removing the
847 Other useful commands are `notmuch-search-filter' for filtering
848 the current search based on an additional query string,
849 `notmuch-search-filter-by-tag' for filtering to include only
850 messages with a given tag, and `notmuch-search' to execute a new,
853 \\{notmuch-search-mode-map}"
855 (kill-all-local-variables)
856 (make-local-variable 'notmuch-search-query-string)
857 (make-local-variable 'notmuch-search-oldest-first)
858 (set (make-local-variable 'scroll-preserve-screen-position) t)
859 (add-to-invisibility-spec 'notmuch-search)
860 (use-local-map notmuch-search-mode-map)
861 (setq truncate-lines t)
862 (setq major-mode 'notmuch-search-mode
863 mode-name "notmuch-search")
864 (setq buffer-read-only t))
866 (defun notmuch-search-find-thread-id ()
870 (re-search-forward "thread:[a-fA-F0-9]*" nil t)
871 (filter-buffer-substring beg (point)))))
873 (defun notmuch-search-markup-this-thread-id ()
876 (if (re-search-forward "thread:[a-fA-F0-9]*" nil t)
879 (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)
880 (re-search-forward ".*\\[[0-9]*/[0-9]*\\] \\([^;]*\\)\\(;\\)")
881 (let* ((authors (buffer-substring (match-beginning 1) (match-end 1)))
882 (authors-length (length authors)))
883 ;; Drop the semi-colon
884 (replace-match "" t nil nil 2)
885 (if (<= authors-length notmuch-search-authors-width)
886 (replace-match (concat authors (make-string
887 (- notmuch-search-authors-width
888 authors-length) ? )) t t nil 1)
889 (replace-match (concat (substring authors 0 (- notmuch-search-authors-width 3)) "...") t t nil 1)))))))
891 (defun notmuch-search-markup-thread-ids ()
893 (goto-char (point-min))
895 (notmuch-search-markup-this-thread-id)
898 (defun notmuch-search-show-thread ()
900 (let ((thread-id (notmuch-search-find-thread-id)))
901 (if (> (length thread-id) 0)
902 (notmuch-show thread-id (current-buffer))
903 (error "End of search results"))))
905 (defun notmuch-search-reply-to-thread ()
906 "Begin composing a reply to the entire current thread in a new buffer."
908 (let ((message-id (notmuch-search-find-thread-id)))
909 (notmuch-reply message-id)))
911 (defun notmuch-call-notmuch-process (&rest args)
912 "Synchronously invoke \"notmuch\" with the given list of arguments.
914 Output from the process will be presented to the user as an error
915 and will also appear in a buffer named \"*Notmuch errors*\"."
916 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
917 (with-current-buffer error-buffer
919 (if (eq (apply 'call-process notmuch-command nil error-buffer nil args) 0)
922 (with-current-buffer error-buffer
923 (let ((beg (point-min))
924 (end (- (point-max) 1)))
925 (error (buffer-substring beg end))
928 (defun notmuch-search-set-tags (tags)
931 (re-search-backward "(")
934 (inhibit-read-only t))
935 (re-search-forward ")")
938 (delete-region beg end)
939 (insert (mapconcat 'identity tags " "))))))
941 (defun notmuch-search-get-tags ()
944 (re-search-backward "(")
945 (let ((beg (+ (point) 1)))
946 (re-search-forward ")")
947 (let ((end (- (point) 1)))
948 (split-string (buffer-substring beg end))))))
950 (defun notmuch-search-add-tag (tag)
951 (interactive "sTag to add: ")
952 (notmuch-call-notmuch-process "tag" (concat "+" tag) (notmuch-search-find-thread-id))
953 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
955 (defun notmuch-search-remove-tag (tag)
956 (interactive "sTag to remove: ")
957 (notmuch-call-notmuch-process "tag" (concat "-" tag) (notmuch-search-find-thread-id))
958 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
960 (defun notmuch-search-archive-thread ()
961 "Archive the current thread (remove its \"inbox\" tag).
963 This function advances point to the next line when finished."
965 (notmuch-search-remove-tag "inbox")
968 (defun notmuch-search (query &optional oldest-first)
969 "Run \"notmuch search\" with the given query string and display results."
970 (interactive "sNotmuch search: ")
971 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
972 (switch-to-buffer buffer)
973 (notmuch-search-mode)
974 (set 'notmuch-search-query-string query)
975 (set 'notmuch-search-oldest-first oldest-first)
976 (let ((proc (get-buffer-process (current-buffer)))
977 (inhibit-read-only t))
979 (error "notmuch search process already running for query `%s'" query)
982 (goto-char (point-min))
985 (call-process notmuch-command nil t nil "search" "--sort=oldest-first" query)
986 (call-process notmuch-command nil t nil "search" "--sort=newest-first" query))
987 (notmuch-search-markup-thread-ids)
989 (run-hooks 'notmuch-search-hook)))
991 (defun notmuch-search-refresh-view ()
992 "Refresh the current view.
994 Kills the current buffer and runs a new search with the same
995 query string as the current search. If the current thread is in
996 the new search results, then point will be placed on the same
997 thread. Otherwise, point will be moved to attempt to be in the
998 same relative position within the new buffer."
1000 (let ((here (point))
1001 (oldest-first notmuch-search-oldest-first)
1002 (thread (notmuch-search-find-thread-id))
1003 (query notmuch-search-query-string))
1005 (notmuch-search query oldest-first)
1006 (goto-char (point-min))
1007 (if (re-search-forward (concat "^" thread) nil t)
1011 (defun notmuch-search-toggle-order ()
1012 "Toggle the current search order.
1014 By default, the \"inbox\" view created by `notmuch' is displayed
1015 in chronological order (oldest thread at the beginning of the
1016 buffer), while any global searches created by `notmuch-search'
1017 are displayed in reverse-chronological order (newest thread at
1018 the beginning of the buffer).
1020 This command toggles the sort order for the current search.
1022 Note that any filtered searches created by
1023 `notmuch-search-filter' retain the search order of the parent
1026 (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
1027 (notmuch-search-refresh-view))
1029 (defun notmuch-search-filter (query)
1030 "Filter the current search results based on an additional query string.
1032 Runs a new search matching only messages that match both the
1033 current search results AND the additional query string provided."
1034 (interactive "sFilter search: ")
1035 (notmuch-search (concat notmuch-search-query-string " and " query) notmuch-search-oldest-first))
1037 (defun notmuch-search-filter-by-tag (tag)
1038 "Filter the current search results based on a single tag.
1040 Runs a new search matching only messages that match both the
1041 current search results AND that are tagged with the given tag."
1042 (interactive "sFilter by tag: ")
1043 (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
1046 "Run notmuch to display all mail with tag of 'inbox'"
1048 (notmuch-search "tag:inbox" t))
1050 (setq mail-user-agent 'message-user-agent)
1052 (defvar notmuch-folder-mode-map
1053 (let ((map (make-sparse-keymap)))
1054 (define-key map "n" 'next-line)
1055 (define-key map "p" 'previous-line)
1056 (define-key map "x" 'kill-this-buffer)
1057 (define-key map "q" 'kill-this-buffer)
1058 (define-key map "s" 'notmuch-search)
1059 (define-key map (kbd "RET") 'notmuch-folder-show-search)
1060 (define-key map "<" 'beginning-of-buffer)
1061 (define-key map "=" 'notmuch-folder)
1062 (define-key map "?" 'describe-mode)
1063 (define-key map [mouse-1] 'notmuch-folder-show-search)
1065 "Keymap for \"notmuch folder\" buffers.")
1067 (fset 'notmuch-folder-mode-map notmuch-folder-mode-map)
1069 (defcustom notmuch-folders (quote (("inbox" . "tag:inbox") ("unread" . "tag:unread")))
1070 "List of searches for the notmuch folder view"
1071 :type '(alist :key-type (string) :value-type (string))
1074 (defun notmuch-folder-mode ()
1075 "Major mode for showing notmuch 'folders'.
1077 This buffer contains a list of messages counts returned by a
1078 customizable set of searches of your email archives. Each line
1079 in the buffer shows the search terms and the resulting message count.
1081 Pressing RET on any line opens a search window containing the
1082 results for the search terms in that line.
1084 \\{notmuch-folder-mode-map}"
1086 (kill-all-local-variables)
1087 (use-local-map 'notmuch-folder-mode-map)
1088 (setq truncate-lines t)
1090 (setq major-mode 'notmuch-folder-mode
1091 mode-name "notmuch-folder")
1092 (setq buffer-read-only t))
1094 (defun notmuch-folder-add (folders)
1096 (let ((name (car (car folders)))
1097 (inhibit-read-only t)
1098 (search (cdr (car folders))))
1101 (call-process notmuch-command nil t nil "count" search)
1102 (notmuch-folder-add (cdr folders)))))
1104 (defun notmuch-folder-find-name ()
1107 (let ((beg (point)))
1109 (filter-buffer-substring beg (point)))))
1111 (defun notmuch-folder-show-search (&optional folder)
1112 "Show a search window for the search related to the specified folder."
1115 (setq folder (notmuch-folder-find-name)))
1116 (let ((search (assoc folder notmuch-folders)))
1118 (notmuch-search (cdr search) t))))
1120 (defun notmuch-folder ()
1121 "Show the notmuch folder view and update the displayed counts."
1123 (let ((buffer (get-buffer-create "*notmuch-folders*")))
1124 (switch-to-buffer buffer)
1125 (let ((inhibit-read-only t)
1126 (n (line-number-at-pos)))
1128 (notmuch-folder-mode)
1129 (notmuch-folder-add notmuch-folders)
1130 (goto-char (point-min))