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 "c" 'notmuch-show-toggle-citations-visible)
66 (define-key map "h" 'notmuch-show-toggle-headers-visible)
67 (define-key map "m" 'message-mail)
68 (define-key map "n" 'notmuch-show-next-message)
69 (define-key map "N" 'notmuch-show-mark-read-then-next-open-message)
70 (define-key map "p" 'notmuch-show-previous-message)
71 (define-key map (kbd "C-n") 'notmuch-show-next-line)
72 (define-key map (kbd "C-p") 'notmuch-show-previous-line)
73 (define-key map "q" 'kill-this-buffer)
74 (define-key map "r" 'notmuch-show-reply)
75 (define-key map "s" 'notmuch-show-toggle-signatures-visible)
76 (define-key map "v" 'notmuch-show-view-all-mime-parts)
77 (define-key map "w" 'notmuch-show-view-raw-message)
78 (define-key map "x" 'kill-this-buffer)
79 (define-key map "+" 'notmuch-show-add-tag)
80 (define-key map "-" 'notmuch-show-remove-tag)
81 (define-key map (kbd "DEL") 'notmuch-show-rewind)
82 (define-key map " " 'notmuch-show-advance-marking-read-and-archiving)
83 (define-key map "|" 'notmuch-show-pipe-message)
84 (define-key map "?" 'describe-mode)
86 "Keymap for \"notmuch show\" buffers.")
87 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
89 (defvar notmuch-show-signature-regexp "\\(-- ?\\|_+\\)$"
90 "Pattern to match a line that separates content from signature.
92 The regexp can (and should) include $ to match the end of the
93 line, but should not include ^ to match the beginning of the
94 line. This is because notmuch may have inserted additional space
95 for indentation at the beginning of the line. But notmuch will
96 move past the indentation when testing this pattern, (so that the
97 pattern can still test against the entire line).")
99 (defvar notmuch-show-signature-lines-max 12
100 "Maximum length of signature that will be hidden by default.")
102 (defvar notmuch-command "notmuch"
103 "Command to run the notmuch binary.")
105 (defvar notmuch-show-message-begin-regexp "\fmessage{")
106 (defvar notmuch-show-message-end-regexp "\fmessage}")
107 (defvar notmuch-show-header-begin-regexp "\fheader{")
108 (defvar notmuch-show-header-end-regexp "\fheader}")
109 (defvar notmuch-show-body-begin-regexp "\fbody{")
110 (defvar notmuch-show-body-end-regexp "\fbody}")
111 (defvar notmuch-show-attachment-begin-regexp "\fattachment{")
112 (defvar notmuch-show-attachment-end-regexp "\fattachment}")
113 (defvar notmuch-show-part-begin-regexp "\fpart{")
114 (defvar notmuch-show-part-end-regexp "\fpart}")
115 (defvar notmuch-show-marker-regexp "\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
117 (defvar notmuch-show-id-regexp "\\(id:[^ ]*\\)")
118 (defvar notmuch-show-depth-regexp " depth:\\([0-9]*\\) ")
119 (defvar notmuch-show-filename-regexp "filename:\\(.*\\)$")
120 (defvar notmuch-show-tags-regexp "(\\([^)]*\\))$")
122 (defvar notmuch-show-parent-buffer nil)
123 (defvar notmuch-show-body-read-visible nil)
124 (defvar notmuch-show-citations-visible nil)
125 (defvar notmuch-show-signatures-visible nil)
126 (defvar notmuch-show-headers-visible nil)
128 ; XXX: This should be a generic function in emacs somewhere, not here
129 (defun point-invisible-p ()
130 "Return whether the character at point is invisible.
132 Here visibility is determined by `buffer-invisibility-spec' and
133 the invisible property of any overlays for point. It doesn't have
134 anything to do with whether point is currently being displayed
135 within the current window."
136 (let ((prop (get-char-property (point) 'invisible)))
137 (if (eq buffer-invisibility-spec t)
139 (or (memq prop buffer-invisibility-spec)
140 (assq prop buffer-invisibility-spec)))))
142 (defun notmuch-show-next-line ()
143 "Like builtin `next-line' but ensuring we end on a visible character.
145 By advancing forward until reaching a visible character.
147 Unlike builtin `next-line' this version accepts no arguments."
149 (set 'this-command 'next-line)
150 (call-interactively 'next-line)
151 (while (point-invisible-p)
154 (defun notmuch-show-previous-line ()
155 "Like builtin `previous-line' but ensuring we end on a visible character.
157 By advancing forward until reaching a visible character.
159 Unlike builtin `next-line' this version accepts no arguments."
161 (set 'this-command 'previous-line)
162 (call-interactively 'previous-line)
163 (while (point-invisible-p)
166 (defun notmuch-show-get-message-id ()
169 (if (not (looking-at notmuch-show-message-begin-regexp))
170 (re-search-backward notmuch-show-message-begin-regexp))
171 (re-search-forward notmuch-show-id-regexp)
172 (buffer-substring (match-beginning 1) (match-end 1))))
174 (defun notmuch-show-get-filename ()
177 (if (not (looking-at notmuch-show-message-begin-regexp))
178 (re-search-backward notmuch-show-message-begin-regexp))
179 (re-search-forward notmuch-show-filename-regexp)
180 (buffer-substring (match-beginning 1) (match-end 1))))
182 (defun notmuch-show-set-tags (tags)
185 (if (not (looking-at notmuch-show-message-begin-regexp))
186 (re-search-backward notmuch-show-message-begin-regexp))
187 (re-search-forward notmuch-show-tags-regexp)
188 (let ((inhibit-read-only t)
189 (beg (match-beginning 1))
191 (delete-region beg end)
193 (insert (mapconcat 'identity tags " ")))))
195 (defun notmuch-show-get-tags ()
198 (if (not (looking-at notmuch-show-message-begin-regexp))
199 (re-search-backward notmuch-show-message-begin-regexp))
200 (re-search-forward notmuch-show-tags-regexp)
201 (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
203 (defun notmuch-show-add-tag (&rest toadd)
204 "Add a tag to the current message."
205 (interactive "sTag to add: ")
206 (apply 'notmuch-call-notmuch-process
208 (mapcar (lambda (s) (concat "+" s)) toadd))
209 (cons (notmuch-show-get-message-id) nil)))
210 (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
212 (defun notmuch-show-remove-tag (&rest toremove)
213 "Remove a tag from the current message."
214 (interactive "sTag to remove: ")
215 (let ((tags (notmuch-show-get-tags)))
216 (if (intersection tags toremove :test 'string=)
218 (apply 'notmuch-call-notmuch-process
220 (mapcar (lambda (s) (concat "-" s)) toremove))
221 (cons (notmuch-show-get-message-id) nil)))
222 (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
224 (defun notmuch-show-archive-thread-maybe-mark-read (markread)
226 (goto-char (point-min))
229 (notmuch-show-remove-tag "unread" "inbox")
230 (notmuch-show-remove-tag "inbox"))
233 (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
234 (goto-char (point-max)))))
235 (let ((parent-buffer notmuch-show-parent-buffer))
239 (switch-to-buffer parent-buffer)
241 (notmuch-search-show-thread)))))
243 (defun notmuch-show-mark-read-then-archive-thread ()
244 "Remove \"unread\" tag from each message, then archive and show next thread.
246 Archive each message currently shown by removing the \"unread\"
247 and \"inbox\" tag from each. Then kill this buffer and show the
248 next thread from the search from which this thread was originally
251 Note: This command is safe from any race condition of new messages
252 being delivered to the same thread. It does not archive the
253 entire thread, but only the messages shown in the current
256 (notmuch-show-archive-thread-maybe-mark-read t))
258 (defun notmuch-show-archive-thread ()
259 "Archive each message in thread, and show next thread from search.
261 Archive each message currently shown by removing the \"inbox\"
262 tag from each. Then kill this buffer and show the next thread
263 from the search from which this thread was originally shown.
265 Note: This command is safe from any race condition of new messages
266 being delivered to the same thread. It does not archive the
267 entire thread, but only the messages shown in the current
270 (notmuch-show-archive-thread-maybe-mark-read nil))
272 (defun notmuch-show-view-raw-message ()
273 "View the raw email of the current message."
275 (view-file (notmuch-show-get-filename)))
277 (defun notmuch-show-view-all-mime-parts ()
278 "Use external viewers (according to mailcap) to view all MIME-encoded parts."
281 (let ((filename (notmuch-show-get-filename)))
282 (switch-to-buffer (generate-new-buffer (concat "*notmuch-mime-"
285 (insert-file-contents filename nil nil nil t)
286 (mm-display-parts (mm-dissect-buffer))
287 (kill-this-buffer))))
289 (defun notmuch-reply (query-string)
290 (switch-to-buffer (generate-new-buffer "notmuch-draft"))
291 (call-process notmuch-command nil t nil "reply" query-string)
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*" (split-string (concat command " < " (notmuch-show-get-filename)))))
314 (defun notmuch-show-move-to-current-message-summary-line ()
315 "Move to the beginning of the one-line summary of the current message.
317 This gives us a stable place to move to and work from since the
318 summary line is always visible. This is important since moving to
319 an invisible location is unreliable, (the main command loop moves
320 point either forward or backward to the next visible character
321 when a command ends with point on an invisible character).
323 Emits an error if point is not within a valid message, (that is
324 not pattern of `notmuch-show-message-begin-regexp' could be found
325 by searching backward)."
327 (if (not (looking-at notmuch-show-message-begin-regexp))
328 (if (re-search-backward notmuch-show-message-begin-regexp nil t)
330 (error "Not within a valid message."))
333 (defun notmuch-show-last-message-p ()
334 "Predicate testing whether point is within the last message."
335 (save-window-excursion
337 (notmuch-show-move-to-current-message-summary-line)
338 (not (re-search-forward notmuch-show-message-begin-regexp nil t)))))
340 (defun notmuch-show-message-unread-p ()
341 "Preficate testing whether current message is unread."
342 (member "unread" (notmuch-show-get-tags)))
344 (defun notmuch-show-next-message ()
345 "Advance to the beginning of the next message in the buffer.
347 Moves to the last visible character of the current message if
348 already on the last message in the buffer."
350 (notmuch-show-move-to-current-message-summary-line)
351 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
352 (notmuch-show-move-to-current-message-summary-line)
353 (goto-char (- (point-max) 1))
354 (while (point-invisible-p)
358 (defun notmuch-show-find-next-message ()
359 "Returns the position of the next message in the buffer.
361 Or the position of the last visible character of the current
362 message if already within the last message in the buffer."
363 ; save-excursion doesn't save our window position
364 ; save-window-excursion doesn't save point
365 ; Looks like we have to use both.
367 (save-window-excursion
368 (notmuch-show-next-message)
371 (defun notmuch-show-next-unread-message ()
372 "Advance to the beginning of the next unread message in the buffer.
374 Moves to the last visible character of the current message if
375 there are no more unread messages past the current point."
376 (notmuch-show-next-message)
377 (while (and (not (notmuch-show-last-message-p))
378 (not (notmuch-show-message-unread-p)))
379 (notmuch-show-next-message))
380 (if (not (notmuch-show-message-unread-p))
381 (notmuch-show-next-message)))
383 (defun notmuch-show-next-open-message ()
384 "Advance to the next message which is not hidden.
386 If read messages are currently hidden, advance to the next unread
387 message. Otherwise, advance to the next message."
388 (if (or (memq 'notmuch-show-body-read buffer-invisibility-spec)
389 (assq 'notmuch-show-body-read buffer-invisibility-spec))
390 (notmuch-show-next-unread-message)
391 (notmuch-show-next-message)))
393 (defun notmuch-show-previous-message ()
394 "Backup to the beginning of the previous message in the buffer.
396 If within a message rather than at the beginning of it, then
397 simply move to the beginning of the current message."
399 (let ((start (point)))
400 (notmuch-show-move-to-current-message-summary-line)
401 (if (not (< (point) start))
402 ; Go backward twice to skip the current message's marker
404 (re-search-backward notmuch-show-message-begin-regexp nil t)
405 (re-search-backward notmuch-show-message-begin-regexp nil t)
406 (notmuch-show-move-to-current-message-summary-line)
410 (defun notmuch-show-find-previous-message ()
411 "Returns the position of the previous message in the buffer.
413 Or the position of the beginning of the current message if point
414 is originally within the message rather than at the beginning of
416 ; save-excursion doesn't save our window position
417 ; save-window-excursion doesn't save point
418 ; Looks like we have to use both.
420 (save-window-excursion
421 (notmuch-show-previous-message)
424 (defun notmuch-show-mark-read-then-next-open-message ()
425 "Remove unread tag from current message, then advance to next unread message."
427 (notmuch-show-remove-tag "unread")
428 (notmuch-show-next-open-message))
430 (defun notmuch-show-rewind ()
431 "Do reverse scrolling compared to `notmuch-show-advance-marking-read-and-archiving'
433 Specifically, if the beginning of the previous email is fewer
434 than `window-height' lines from the current point, move to it
435 just like `notmuch-show-previous-message'.
437 Otherwise, just scroll down a screenful of the current message.
439 This command does not modify any message tags, (it does not undo
440 any effects from previous calls to
441 `notmuch-show-advance-marking-read-and-archiving'."
443 (let ((previous (notmuch-show-find-previous-message)))
444 (if (> (count-lines previous (point)) (- (window-height) next-screen-context-lines))
448 ((beginning-of-buffer) nil))
449 (goto-char (window-start)))
450 (notmuch-show-previous-message))))
452 (defun notmuch-show-advance-marking-read-and-archiving ()
453 "Advance through buffer, marking read and archiving.
455 This command is intended to be one of the simplest ways to
456 process a thread of email. It does the following:
458 If the current message in the thread is not yet fully visible,
459 scroll by a near screenful to read more of the message.
461 Otherwise, (the end of the current message is already within the
462 current window), remove the \"unread\" tag (if present) from the
463 current message and advance to the next open message.
465 Finally, if there is no further message to advance to, and this
466 last message is already read, then archive the entire current
467 thread, (remove the \"inbox\" tag from each message). Also kill
468 this buffer, and display the next thread from the search from
469 which this thread was originally shown."
471 (let ((next (notmuch-show-find-next-message))
472 (unread (notmuch-show-message-unread-p)))
473 (if (> next (window-end))
475 (let ((last (notmuch-show-last-message-p)))
476 (notmuch-show-mark-read-then-next-open-message)
478 (notmuch-show-archive-thread))))))
480 (defun notmuch-show-markup-citations-region (beg end depth)
483 (while (< (point) end)
484 (let ((beg-sub (point-marker))
485 (indent (make-string depth ? ))
486 (citation "[[:space:]]*>"))
487 (if (looking-at citation)
489 (while (looking-at citation)
491 (let ((overlay (make-overlay beg-sub (point))))
492 (overlay-put overlay 'invisible 'notmuch-show-citation)
493 (overlay-put overlay 'before-string
495 "[" (number-to-string (count-lines beg-sub (point)))
496 "-line citation. Press 'c' to show.]\n")))))
497 (move-to-column depth)
498 (if (looking-at notmuch-show-signature-regexp)
499 (let ((sig-lines (- (count-lines beg-sub end) 1)))
500 (if (<= sig-lines notmuch-show-signature-lines-max)
502 (overlay-put (make-overlay beg-sub end)
503 'invisible 'notmuch-show-signature)
504 (overlay-put (make-overlay beg (- beg-sub 1))
507 "[" (number-to-string sig-lines)
508 "-line signature. Press 's' to show.]"))
512 (defun notmuch-show-markup-part (beg end depth)
513 (if (re-search-forward notmuch-show-part-begin-regexp nil t)
516 (let ((beg (point-marker)))
517 (re-search-forward notmuch-show-part-end-regexp)
518 (let ((end (copy-marker (match-beginning 0))))
522 (indent-rigidly beg end depth)
523 (notmuch-show-markup-citations-region beg end depth)
524 ; Advance to the next part (if any) (so the outer loop can
525 ; determine whether we've left the current message.
526 (if (re-search-forward notmuch-show-part-begin-regexp nil t)
527 (beginning-of-line)))))
530 (defun notmuch-show-markup-parts-region (beg end depth)
533 (while (< (point) end)
534 (notmuch-show-markup-part beg end depth))))
536 (defun notmuch-show-markup-body (depth)
537 (re-search-forward notmuch-show-body-begin-regexp)
539 (let ((beg (point-marker)))
540 (re-search-forward notmuch-show-body-end-regexp)
541 (let ((end (copy-marker (match-beginning 0))))
542 (notmuch-show-markup-parts-region beg end depth)
543 (if (not (notmuch-show-message-unread-p))
544 (overlay-put (make-overlay beg end)
545 'invisible 'notmuch-show-body-read))
550 (defun notmuch-show-markup-header (depth)
551 (re-search-forward notmuch-show-header-begin-regexp)
553 (let ((beg (point-marker)))
555 ; Inverse video for subject
556 (overlay-put (make-overlay beg (point)) 'face '(:inverse-video t))
558 (let ((beg-hidden (point-marker)))
559 (re-search-forward notmuch-show-header-end-regexp)
561 (let ((end (point-marker)))
562 (indent-rigidly beg end depth)
563 (overlay-put (make-overlay beg-hidden end)
564 'invisible 'notmuch-show-header)
566 (set-marker beg-hidden nil)
570 (defun notmuch-show-markup-message ()
571 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
573 (re-search-forward notmuch-show-depth-regexp)
574 (let ((depth (string-to-number (buffer-substring (match-beginning 1) (match-end 1)))))
575 (notmuch-show-markup-header depth)
576 (notmuch-show-markup-body depth)))
577 (goto-char (point-max))))
579 (defun notmuch-show-hide-markers ()
581 (goto-char (point-min))
583 (if (re-search-forward notmuch-show-marker-regexp nil t)
585 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
586 'invisible 'notmuch-show-marker))
587 (goto-char (point-max))))))
589 (defun notmuch-show-markup-messages ()
591 (goto-char (point-min))
593 (notmuch-show-markup-message)))
594 (notmuch-show-hide-markers))
596 (defun notmuch-show-toggle-citations-visible ()
597 "Toggle visibility of citations"
599 (if notmuch-show-citations-visible
600 (add-to-invisibility-spec 'notmuch-show-citation)
601 (remove-from-invisibility-spec 'notmuch-show-citation))
602 (set 'notmuch-show-citations-visible (not notmuch-show-citations-visible))
603 ; Need to force the redisplay for some reason
604 (force-window-update)
607 (defun notmuch-show-toggle-signatures-visible ()
608 "Toggle visibility of signatures"
610 (if notmuch-show-signatures-visible
611 (add-to-invisibility-spec 'notmuch-show-signature)
612 (remove-from-invisibility-spec 'notmuch-show-signature))
613 (set 'notmuch-show-signatures-visible (not notmuch-show-signatures-visible))
614 ; Need to force the redisplay for some reason
615 (force-window-update)
618 (defun notmuch-show-toggle-headers-visible ()
619 "Toggle visibility of header fields"
621 (if notmuch-show-headers-visible
622 (add-to-invisibility-spec 'notmuch-show-header)
623 (remove-from-invisibility-spec 'notmuch-show-header))
624 (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
625 ; Need to force the redisplay for some reason
626 (force-window-update)
629 (defun notmuch-show-toggle-body-read-visible ()
630 "Toggle visibility of message bodies of read messages"
632 (if notmuch-show-body-read-visible
633 (add-to-invisibility-spec 'notmuch-show-body-read)
634 (remove-from-invisibility-spec 'notmuch-show-body-read))
635 (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
636 ; Need to force the redisplay for some reason
637 (force-window-update)
641 (defun notmuch-show-mode ()
642 "Major mode for viewing a thread with notmuch.
644 This buffer contains the results of the \"notmuch show\" command
645 for displaying a single thread of email from your email archives.
647 By default, various components of email messages, (citations,
648 signatures, already-read messages), are invisible to help you
649 focus on the most important things, (new text from unread
650 messages). See the various commands below for toggling the
651 visibility of hidden components.
653 The `notmuch-show-next-message' and
654 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
655 default), allow you to navigate to the next and previous
656 messages. Each time you navigate away from a message with
657 `notmuch-show-next-message' the current message will have its
658 \"unread\" tag removed.
660 You can add or remove tags from the current message with '+' and
661 '-'. You can also archive all messages in the current
662 view, (remove the \"inbox\" tag from each), with
663 `notmuch-show-archive-thread' (bound to 'a' by default).
665 \\{notmuch-show-mode-map}"
667 (kill-all-local-variables)
668 (set (make-local-variable 'notmuch-show-headers-visible) t)
669 (notmuch-show-toggle-headers-visible)
670 (set (make-local-variable 'notmuch-show-body-read-visible) t)
671 (notmuch-show-toggle-body-read-visible)
672 (set (make-local-variable 'notmuch-show-citations-visible) t)
673 (notmuch-show-toggle-citations-visible)
674 (set (make-local-variable 'notmuch-show-signatures-visible) t)
675 (notmuch-show-toggle-signatures-visible)
676 (add-to-invisibility-spec 'notmuch-show-marker)
677 (use-local-map notmuch-show-mode-map)
678 (setq major-mode 'notmuch-show-mode
679 mode-name "notmuch-show")
680 (setq buffer-read-only t))
684 (defgroup notmuch nil
685 "Notmuch mail reader for Emacs."
688 (defcustom notmuch-show-hook nil
689 "List of functions to call when notmuch displays a message."
691 :options '(goto-address)
694 (defcustom notmuch-search-hook nil
695 "List of functions to call when notmuch displays the search results."
697 :options '(hl-line-mode)
700 ; Make show mode a bit prettier, highlighting URLs and using word wrap
702 (defun notmuch-show-pretty-hook ()
703 (goto-address-mode 1)
706 (add-hook 'notmuch-show-hook 'notmuch-show-pretty-hook)
707 (add-hook 'notmuch-search-hook
711 (defun notmuch-show (thread-id &optional parent-buffer)
712 "Run \"notmuch show\" with the given thread ID and display results.
714 The optional PARENT-BUFFER is the notmuch-search buffer from
715 which this notmuch-show command was executed, (so that the next
716 thread from that buffer can be show when done with this one)."
717 (interactive "sNotmuch show: ")
718 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
719 (switch-to-buffer buffer)
721 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
722 (let ((proc (get-buffer-process (current-buffer)))
723 (inhibit-read-only t))
725 (error "notmuch search process already running for query `%s'" thread-id)
728 (goto-char (point-min))
730 (call-process notmuch-command nil t nil "show" thread-id)
731 (notmuch-show-markup-messages)
733 (run-hooks 'notmuch-show-hook)
734 ; Move straight to the first unread message
735 (if (not (notmuch-show-message-unread-p))
737 (notmuch-show-next-unread-message)
738 ; But if there are no unread messages, go back to the
739 ; beginning of the buffer, and open up the bodies of all
741 (if (not (notmuch-show-message-unread-p))
743 (goto-char (point-min))
744 (notmuch-show-toggle-body-read-visible)))))
747 (defvar notmuch-search-authors-width 40
748 "Number of columns to use to display authors in a notmuch-search buffer.")
750 (defvar notmuch-search-mode-map
751 (let ((map (make-sparse-keymap)))
752 (define-key map "a" 'notmuch-search-archive-thread)
753 (define-key map "b" 'notmuch-search-scroll-down)
754 (define-key map "f" 'notmuch-search-filter)
755 (define-key map "m" 'message-mail)
756 (define-key map "n" 'next-line)
757 (define-key map "o" 'notmuch-search-toggle-order)
758 (define-key map "p" 'previous-line)
759 (define-key map "q" 'kill-this-buffer)
760 (define-key map "r" 'notmuch-search-reply-to-thread)
761 (define-key map "s" 'notmuch-search)
762 (define-key map "t" 'notmuch-search-filter-by-tag)
763 (define-key map "x" 'kill-this-buffer)
764 (define-key map (kbd "RET") 'notmuch-search-show-thread)
765 (define-key map "+" 'notmuch-search-add-tag)
766 (define-key map "-" 'notmuch-search-remove-tag)
767 (define-key map "<" 'beginning-of-buffer)
768 (define-key map ">" 'notmuch-search-goto-last-thread)
769 (define-key map "=" 'notmuch-search-refresh-view)
770 (define-key map "\M->" 'notmuch-search-goto-last-thread)
771 (define-key map " " 'notmuch-search-scroll-up)
772 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
773 (define-key map "?" 'describe-mode)
775 "Keymap for \"notmuch search\" buffers.")
776 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
778 (defvar notmuch-search-query-string)
779 (defvar notmuch-search-oldest-first)
781 (defun notmuch-search-scroll-up ()
782 "Scroll up, moving point to last message in thread if at end."
786 ((end-of-buffer) (notmuch-search-goto-last-thread))))
788 (defun notmuch-search-scroll-down ()
789 "Scroll down, moving point to first message in thread if at beginning."
791 ; I don't know why scroll-down doesn't signal beginning-of-buffer
792 ; the way that scroll-up signals end-of-buffer, but c'est la vie.
794 ; So instead of trapping a signal we instead check whether the
795 ; window begins on the first line of the buffer and if so, move
796 ; directly to that position. (We have to count lines since the
797 ; window-start position is not the same as point-min due to the
798 ; invisible thread-ID characters on the first line.
799 (if (equal (count-lines (point-min) (window-start)) 1)
800 (goto-char (window-start))
803 (defun notmuch-search-goto-last-thread ()
804 "Move point to the last thread in the buffer."
806 (goto-char (point-max))
810 (defun notmuch-search-mode ()
811 "Major mode for searching mail with notmuch.
813 This buffer contains the results of a \"notmuch search\" of your
814 email archives. Each line in the buffer represents a single
815 thread giving a relative date for the thread and a subject.
817 Pressing RET on any line displays that thread. The '+' and '-'
818 keys can be used to add or remove tags from a thread. The 'a' key
819 is a convenience key for archiving a thread (removing the
822 Other useful commands are `notmuch-search-filter' for filtering
823 the current search based on an additional query string,
824 `notmuch-search-filter-by-tag' for filtering to include only
825 messages with a given tag, and `notmuch-search' to execute a new,
828 \\{notmuch-search-mode-map}"
830 (kill-all-local-variables)
831 (make-local-variable 'notmuch-search-query-string)
832 (make-local-variable 'notmuch-search-oldest-first)
833 (set (make-local-variable 'scroll-preserve-screen-position) t)
834 (add-to-invisibility-spec 'notmuch-search)
835 (use-local-map notmuch-search-mode-map)
836 (setq truncate-lines t)
837 (setq major-mode 'notmuch-search-mode
838 mode-name "notmuch-search")
839 (setq buffer-read-only t))
841 (defun notmuch-search-find-thread-id ()
845 (re-search-forward "thread:[a-fA-F0-9]*" nil t)
846 (filter-buffer-substring beg (point)))))
848 (defun notmuch-search-markup-this-thread-id ()
851 (if (re-search-forward "thread:[a-fA-F0-9]*" nil t)
854 (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)
855 (re-search-forward ".*\\[[0-9]*/[0-9]*\\] \\([^;]*\\)\\(;\\)")
856 (let* ((authors (buffer-substring (match-beginning 1) (match-end 1)))
857 (authors-length (length authors)))
858 ;; Drop the semi-colon
859 (replace-match "" t nil nil 2)
860 (if (<= authors-length notmuch-search-authors-width)
861 (replace-match (concat authors (make-string
862 (- notmuch-search-authors-width
863 authors-length) ? )) t t nil 1)
864 (replace-match (concat (substring authors 0 (- notmuch-search-authors-width 3)) "...") t t nil 1)))))))
866 (defun notmuch-search-markup-thread-ids ()
868 (goto-char (point-min))
870 (notmuch-search-markup-this-thread-id)
873 (defun notmuch-search-show-thread ()
875 (let ((thread-id (notmuch-search-find-thread-id)))
876 (if (> (length thread-id) 0)
877 (notmuch-show thread-id (current-buffer))
878 (error "End of search results"))))
880 (defun notmuch-search-reply-to-thread ()
881 "Begin composing a reply to the entire current thread in a new buffer."
883 (let ((message-id (notmuch-search-find-thread-id)))
884 (notmuch-reply message-id)))
886 (defun notmuch-call-notmuch-process (&rest args)
887 "Synchronously invoke \"notmuch\" with the given list of arguments.
889 Output from the process will be presented to the user as an error
890 and will also appear in a buffer named \"*Notmuch errors*\"."
891 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
892 (with-current-buffer error-buffer
894 (if (eq (apply 'call-process notmuch-command nil error-buffer nil args) 0)
897 (with-current-buffer error-buffer
898 (let ((beg (point-min))
899 (end (- (point-max) 1)))
900 (error (buffer-substring beg end))
903 (defun notmuch-search-set-tags (tags)
906 (re-search-backward "(")
909 (inhibit-read-only t))
910 (re-search-forward ")")
913 (delete-region beg end)
914 (insert (mapconcat 'identity tags " "))))))
916 (defun notmuch-search-get-tags ()
919 (re-search-backward "(")
920 (let ((beg (+ (point) 1)))
921 (re-search-forward ")")
922 (let ((end (- (point) 1)))
923 (split-string (buffer-substring beg end))))))
925 (defun notmuch-search-add-tag (tag)
926 (interactive "sTag to add: ")
927 (notmuch-call-notmuch-process "tag" (concat "+" tag) (notmuch-search-find-thread-id))
928 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
930 (defun notmuch-search-remove-tag (tag)
931 (interactive "sTag to remove: ")
932 (notmuch-call-notmuch-process "tag" (concat "-" tag) (notmuch-search-find-thread-id))
933 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
935 (defun notmuch-search-archive-thread ()
936 "Archive the current thread (remove its \"inbox\" tag).
938 This function advances point to the next line when finished."
940 (notmuch-search-remove-tag "inbox")
943 (defun notmuch-search (query &optional oldest-first)
944 "Run \"notmuch search\" with the given query string and display results."
945 (interactive "sNotmuch search: ")
946 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
947 (switch-to-buffer buffer)
948 (notmuch-search-mode)
949 (set 'notmuch-search-query-string query)
950 (set 'notmuch-search-oldest-first oldest-first)
951 (let ((proc (get-buffer-process (current-buffer)))
952 (inhibit-read-only t))
954 (error "notmuch search process already running for query `%s'" query)
957 (goto-char (point-min))
960 (call-process notmuch-command nil t nil "search" "--sort=oldest-first" query)
961 (call-process notmuch-command nil t nil "search" "--sort=newest-first" query))
962 (notmuch-search-markup-thread-ids)
964 (run-hooks 'notmuch-search-hook)))
966 (defun notmuch-search-refresh-view ()
967 "Refresh the current view.
969 Kills the current buffer and runs a new search with the same
970 query string as the current search. If the current thread is in
971 the new search results, then point will be placed on the same
972 thread. Otherwise, point will be moved to attempt to be in the
973 same relative position within the new buffer."
976 (oldest-first notmuch-search-oldest-first)
977 (thread (notmuch-search-find-thread-id))
978 (query notmuch-search-query-string))
980 (notmuch-search query oldest-first)
981 (goto-char (point-min))
982 (if (re-search-forward (concat "^" thread) nil t)
986 (defun notmuch-search-toggle-order ()
987 "Toggle the current search order.
989 By default, the \"inbox\" view created by `notmuch' is displayed
990 in chronological order (oldest thread at the beginning of the
991 buffer), while any global searches created by `notmuch-search'
992 are displayed in reverse-chronological order (newest thread at
993 the beginning of the buffer).
995 This command toggles the sort order for the current search.
997 Note that any filtered searches created by
998 `notmuch-search-filter' retain the search order of the parent
1001 (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
1002 (notmuch-search-refresh-view))
1004 (defun notmuch-search-filter (query)
1005 "Filter the current search results based on an additional query string.
1007 Runs a new search matching only messages that match both the
1008 current search results AND the additional query string provided."
1009 (interactive "sFilter search: ")
1010 (notmuch-search (concat notmuch-search-query-string " and " query) notmuch-search-oldest-first))
1012 (defun notmuch-search-filter-by-tag (tag)
1013 "Filter the current search results based on a single tag.
1015 Runs a new search matching only messages that match both the
1016 current search results AND that are tagged with the given tag."
1017 (interactive "sFilter by tag: ")
1018 (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
1021 "Run notmuch to display all mail with tag of 'inbox'"
1023 (notmuch-search "tag:inbox" t))
1025 (setq mail-user-agent 'message-user-agent)