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>
25 (defvar notmuch-show-mode-map
26 (let ((map (make-sparse-keymap)))
27 ; I don't actually want all of these toggle commands occupying
28 ; keybindings. They steal valuable key-binding space, are hard
29 ; to remember, and act globally rather than locally.
31 ; Will be much preferable to switch to direct manipulation for
32 ; toggling visibility of these components. Probably using
33 ; overlays-at to query and manipulate the current overlay.
34 (define-key map "a" 'notmuch-show-archive-thread)
35 (define-key map "A" 'notmuch-show-mark-read-then-archive-thread)
36 (define-key map "b" 'notmuch-show-toggle-body-read-visible)
37 (define-key map "c" 'notmuch-show-toggle-citations-visible)
38 (define-key map "h" 'notmuch-show-toggle-headers-visible)
39 (define-key map "m" 'message-mail)
40 (define-key map "n" 'notmuch-show-next-message)
41 (define-key map "N" 'notmuch-show-mark-read-then-next-open-message)
42 (define-key map "p" 'notmuch-show-previous-message)
43 (define-key map (kbd "C-n") 'notmuch-show-next-line)
44 (define-key map (kbd "C-p") 'notmuch-show-previous-line)
45 (define-key map "q" 'kill-this-buffer)
46 (define-key map "r" 'notmuch-show-reply)
47 (define-key map "s" 'notmuch-show-toggle-signatures-visible)
48 (define-key map "v" 'notmuch-show-view-all-mime-parts)
49 (define-key map "w" 'notmuch-show-view-raw-message)
50 (define-key map "x" 'kill-this-buffer)
51 (define-key map "+" 'notmuch-show-add-tag)
52 (define-key map "-" 'notmuch-show-remove-tag)
53 (define-key map (kbd "DEL") 'notmuch-show-rewind)
54 (define-key map " " 'notmuch-show-advance-marking-read-and-archiving)
55 (define-key map "|" 'notmuch-show-pipe-message)
56 (define-key map "?" 'describe-mode)
58 "Keymap for \"notmuch show\" buffers.")
59 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
61 (defvar notmuch-show-signature-regexp "\\(-- ?\\|_+\\)$"
62 "Pattern to match a line that separates content from signature.
64 The regexp can (and should) include $ to match the end of the
65 line, but should not include ^ to match the beginning of the
66 line. This is because notmuch may have inserted additional space
67 for indentation at the beginning of the line. But notmuch will
68 move past the indentation when testing this pattern, (so that the
69 pattern can still test against the entire line).")
71 (defvar notmuch-show-signature-lines-max 12
72 "Maximum length of signature that will be hidden by default.")
74 (defvar notmuch-command "notmuch"
75 "Command to run the notmuch binary.")
77 (defvar notmuch-show-message-begin-regexp "\fmessage{")
78 (defvar notmuch-show-message-end-regexp "\fmessage}")
79 (defvar notmuch-show-header-begin-regexp "\fheader{")
80 (defvar notmuch-show-header-end-regexp "\fheader}")
81 (defvar notmuch-show-body-begin-regexp "\fbody{")
82 (defvar notmuch-show-body-end-regexp "\fbody}")
83 (defvar notmuch-show-attachment-begin-regexp "\fattachment{")
84 (defvar notmuch-show-attachment-end-regexp "\fattachment}")
85 (defvar notmuch-show-part-begin-regexp "\fpart{")
86 (defvar notmuch-show-part-end-regexp "\fpart}")
87 (defvar notmuch-show-marker-regexp "\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
89 (defvar notmuch-show-id-regexp "\\(id:[^ ]*\\)")
90 (defvar notmuch-show-depth-regexp " depth:\\([0-9]*\\) ")
91 (defvar notmuch-show-filename-regexp "filename:\\(.*\\)$")
92 (defvar notmuch-show-tags-regexp "(\\([^)]*\\))$")
94 (defvar notmuch-show-parent-buffer nil)
95 (defvar notmuch-show-body-read-visible nil)
96 (defvar notmuch-show-citations-visible nil)
97 (defvar notmuch-show-signatures-visible nil)
98 (defvar notmuch-show-headers-visible nil)
100 ; XXX: This should be a generic function in emacs somewhere, not here
101 (defun point-invisible-p ()
102 "Return whether the character at point is invisible.
104 Here visibility is determined by `buffer-invisibility-spec' and
105 the invisible property of any overlays for point. It doesn't have
106 anything to do with whether point is currently being displayed
107 within the current window."
108 (let ((prop (get-char-property (point) 'invisible)))
109 (if (eq buffer-invisibility-spec t)
111 (or (memq prop buffer-invisibility-spec)
112 (assq prop buffer-invisibility-spec)))))
114 (defun notmuch-show-next-line ()
115 "Like builtin `next-line' but ensuring we end on a visible character.
117 By advancing forward until reaching a visible character.
119 Unlike builtin `next-line' this version accepts no arguments."
121 (set 'this-command 'next-line)
122 (call-interactively 'next-line)
123 (while (point-invisible-p)
126 (defun notmuch-show-previous-line ()
127 "Like builtin `previous-line' but ensuring we end on a visible character.
129 By advancing forward until reaching a visible character.
131 Unlike builtin `next-line' this version accepts no arguments."
133 (set 'this-command 'previous-line)
134 (call-interactively 'previous-line)
135 (while (point-invisible-p)
138 (defun notmuch-show-get-message-id ()
141 (if (not (looking-at notmuch-show-message-begin-regexp))
142 (re-search-backward notmuch-show-message-begin-regexp))
143 (re-search-forward notmuch-show-id-regexp)
144 (buffer-substring (match-beginning 1) (match-end 1))))
146 (defun notmuch-show-get-filename ()
149 (if (not (looking-at notmuch-show-message-begin-regexp))
150 (re-search-backward notmuch-show-message-begin-regexp))
151 (re-search-forward notmuch-show-filename-regexp)
152 (buffer-substring (match-beginning 1) (match-end 1))))
154 (defun notmuch-show-set-tags (tags)
157 (if (not (looking-at notmuch-show-message-begin-regexp))
158 (re-search-backward notmuch-show-message-begin-regexp))
159 (re-search-forward notmuch-show-tags-regexp)
160 (let ((inhibit-read-only t)
161 (beg (match-beginning 1))
163 (delete-region beg end)
165 (insert (mapconcat 'identity tags " ")))))
167 (defun notmuch-show-get-tags ()
170 (if (not (looking-at notmuch-show-message-begin-regexp))
171 (re-search-backward notmuch-show-message-begin-regexp))
172 (re-search-forward notmuch-show-tags-regexp)
173 (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
175 (defun notmuch-show-add-tag (&rest toadd)
176 "Add a tag to the current message."
177 (interactive "sTag to add: ")
178 (apply 'notmuch-call-notmuch-process
180 (mapcar (lambda (s) (concat "+" s)) toadd))
181 (cons (notmuch-show-get-message-id) nil)))
182 (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
184 (defun notmuch-show-remove-tag (&rest toremove)
185 "Remove a tag from the current message."
186 (interactive "sTag to remove: ")
187 (let ((tags (notmuch-show-get-tags)))
188 (if (intersection tags toremove :test 'string=)
190 (apply 'notmuch-call-notmuch-process
192 (mapcar (lambda (s) (concat "-" s)) toremove))
193 (cons (notmuch-show-get-message-id) nil)))
194 (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
196 (defun notmuch-show-archive-thread-maybe-mark-read (markread)
198 (goto-char (point-min))
201 (notmuch-show-remove-tag "unread" "inbox")
202 (notmuch-show-remove-tag "inbox"))
205 (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
206 (goto-char (point-max)))))
207 (let ((parent-buffer notmuch-show-parent-buffer))
211 (switch-to-buffer parent-buffer)
213 (notmuch-search-show-thread)))))
215 (defun notmuch-show-mark-read-then-archive-thread ()
216 "Remove \"unread\" tag from each message, then archive and show next thread.
218 Archive each message currently shown by removing the \"unread\"
219 and \"inbox\" tag from each. Then kill this buffer and show the
220 next thread from the search from which this thread was originally
223 Note: This command is safe from any race condition of new messages
224 being delivered to the same thread. It does not archive the
225 entire thread, but only the messages shown in the current
228 (notmuch-show-archive-thread-maybe-mark-read t))
230 (defun notmuch-show-archive-thread ()
231 "Archive each message in thread, and show next thread from search.
233 Archive each message currently shown by removing the \"inbox\"
234 tag from each. Then kill this buffer and show the next thread
235 from the search from which this thread was originally shown.
237 Note: This command is safe from any race condition of new messages
238 being delivered to the same thread. It does not archive the
239 entire thread, but only the messages shown in the current
242 (notmuch-show-archive-thread-maybe-mark-read nil))
244 (defun notmuch-show-view-raw-message ()
245 "View the raw email of the current message."
247 (view-file (notmuch-show-get-filename)))
249 (defun notmuch-show-view-all-mime-parts ()
250 "Use external viewers (according to mailcap) to view all MIME-encoded parts."
253 (let ((filename (notmuch-show-get-filename)))
254 (switch-to-buffer (generate-new-buffer (concat "*notmuch-mime-"
257 (insert-file-contents filename nil nil nil t)
258 (mm-display-parts (mm-dissect-buffer))
259 (kill-this-buffer))))
261 (defun notmuch-reply (query-string)
262 (switch-to-buffer (generate-new-buffer "notmuch-draft"))
263 (call-process notmuch-command nil t nil "reply" query-string)
264 (goto-char (point-min))
265 (if (re-search-forward "^$" nil t)
267 (insert "--text follows this line--")
271 (defun notmuch-show-reply ()
272 "Begin composing a reply to the current message in a new buffer."
274 (let ((message-id (notmuch-show-get-message-id)))
275 (notmuch-reply message-id)))
277 (defun notmuch-show-pipe-message (command)
278 "Pipe the contents of the current message to the given command.
280 The given command will be executed with the raw contents of the
281 current email message as stdin. Anything printed by the command
282 to stdout or stderr will appear in the *Messages* buffer."
283 (interactive "sPipe message to command: ")
284 (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*" (split-string (concat command " < " (notmuch-show-get-filename)))))
286 (defun notmuch-show-move-to-current-message-summary-line ()
287 "Move to the beginning of the one-line summary of the current message.
289 This gives us a stable place to move to and work from since the
290 summary line is always visible. This is important since moving to
291 an invisible location is unreliable, (the main command loop moves
292 point either forward or backward to the next visible character
293 when a command ends with point on an invisible character).
295 Emits an error if point is not within a valid message, (that is
296 not pattern of `notmuch-show-message-begin-regexp' could be found
297 by searching backward)."
299 (if (not (looking-at notmuch-show-message-begin-regexp))
300 (if (re-search-backward notmuch-show-message-begin-regexp nil t)
302 (error "Not within a valid message."))
305 (defun notmuch-show-last-message-p ()
306 "Predicate testing whether point is within the last message."
307 (save-window-excursion
309 (notmuch-show-move-to-current-message-summary-line)
310 (not (re-search-forward notmuch-show-message-begin-regexp nil t)))))
312 (defun notmuch-show-message-unread-p ()
313 "Preficate testing whether current message is unread."
314 (member "unread" (notmuch-show-get-tags)))
316 (defun notmuch-show-next-message ()
317 "Advance to the beginning of the next message in the buffer.
319 Moves to the last visible character of the current message if
320 already on the last message in the buffer."
322 (notmuch-show-move-to-current-message-summary-line)
323 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
324 (notmuch-show-move-to-current-message-summary-line)
325 (goto-char (- (point-max) 1))
326 (while (point-invisible-p)
330 (defun notmuch-show-find-next-message ()
331 "Returns the position of the next message in the buffer.
333 Or the position of the last visible character of the current
334 message if already within the last message in the buffer."
335 ; save-excursion doesn't save our window position
336 ; save-window-excursion doesn't save point
337 ; Looks like we have to use both.
339 (save-window-excursion
340 (notmuch-show-next-message)
343 (defun notmuch-show-next-unread-message ()
344 "Advance to the beginning of the next unread message in the buffer.
346 Moves to the last visible character of the current message if
347 there are no more unread messages past the current point."
348 (notmuch-show-next-message)
349 (while (and (not (notmuch-show-last-message-p))
350 (not (notmuch-show-message-unread-p)))
351 (notmuch-show-next-message))
352 (if (not (notmuch-show-message-unread-p))
353 (notmuch-show-next-message)))
355 (defun notmuch-show-next-open-message ()
356 "Advance to the next message which is not hidden.
358 If read messages are currently hidden, advance to the next unread
359 message. Otherwise, advance to the next message."
360 (if (or (memq 'notmuch-show-body-read buffer-invisibility-spec)
361 (assq 'notmuch-show-body-read buffer-invisibility-spec))
362 (notmuch-show-next-unread-message)
363 (notmuch-show-next-message)))
365 (defun notmuch-show-previous-message ()
366 "Backup to the beginning of the previous message in the buffer.
368 If within a message rather than at the beginning of it, then
369 simply move to the beginning of the current message."
371 (let ((start (point)))
372 (notmuch-show-move-to-current-message-summary-line)
373 (if (not (< (point) start))
374 ; Go backward twice to skip the current message's marker
376 (re-search-backward notmuch-show-message-begin-regexp nil t)
377 (re-search-backward notmuch-show-message-begin-regexp nil t)
378 (notmuch-show-move-to-current-message-summary-line)
382 (defun notmuch-show-find-previous-message ()
383 "Returns the position of the previous message in the buffer.
385 Or the position of the beginning of the current message if point
386 is originally within the message rather than at the beginning of
388 ; save-excursion doesn't save our window position
389 ; save-window-excursion doesn't save point
390 ; Looks like we have to use both.
392 (save-window-excursion
393 (notmuch-show-previous-message)
396 (defun notmuch-show-mark-read-then-next-open-message ()
397 "Remove unread tag from current message, then advance to next unread message."
399 (notmuch-show-remove-tag "unread")
400 (notmuch-show-next-open-message))
402 (defun notmuch-show-rewind ()
403 "Do reverse scrolling compared to `notmuch-show-advance-marking-read-and-archiving'
405 Specifically, if the beginning of the previous email is fewer
406 than `window-height' lines from the current point, move to it
407 just like `notmuch-show-previous-message'.
409 Otherwise, just scroll down a screenful of the current message.
411 This command does not modify any message tags, (it does not undo
412 any effects from previous calls to
413 `notmuch-show-advance-marking-read-and-archiving'."
415 (let ((previous (notmuch-show-find-previous-message)))
416 (if (> (count-lines previous (point)) (- (window-height) next-screen-context-lines))
420 ((beginning-of-buffer) nil))
421 (goto-char (window-start)))
422 (notmuch-show-previous-message))))
424 (defun notmuch-show-advance-marking-read-and-archiving ()
425 "Advance through buffer, marking read and archiving.
427 This command is intended to be one of the simplest ways to
428 process a thread of email. It does the following:
430 If the current message in the thread is not yet fully visible,
431 scroll by a near screenful to read more of the message.
433 Otherwise, (the end of the current message is already within the
434 current window), remove the \"unread\" tag (if present) from the
435 current message and advance to the next open message.
437 Finally, if there is no further message to advance to, and this
438 last message is already read, then archive the entire current
439 thread, (remove the \"inbox\" tag from each message). Also kill
440 this buffer, and display the next thread from the search from
441 which this thread was originally shown."
443 (let ((next (notmuch-show-find-next-message))
444 (unread (notmuch-show-message-unread-p)))
445 (if (> next (window-end))
447 (let ((last (notmuch-show-last-message-p)))
448 (notmuch-show-mark-read-then-next-open-message)
450 (notmuch-show-archive-thread))))))
452 (defun notmuch-show-markup-citations-region (beg end depth)
455 (while (< (point) end)
456 (let ((beg-sub (point-marker))
457 (indent (make-string depth ? ))
458 (citation "[[:space:]]*>"))
459 (if (looking-at citation)
461 (while (looking-at citation)
463 (let ((overlay (make-overlay beg-sub (point))))
464 (overlay-put overlay 'invisible 'notmuch-show-citation)
465 (overlay-put overlay 'before-string
467 "[" (number-to-string (count-lines beg-sub (point)))
468 "-line citation. Press 'c' to show.]\n")))))
469 (move-to-column depth)
470 (if (looking-at notmuch-show-signature-regexp)
471 (let ((sig-lines (- (count-lines beg-sub end) 1)))
472 (if (<= sig-lines notmuch-show-signature-lines-max)
474 (overlay-put (make-overlay beg-sub end)
475 'invisible 'notmuch-show-signature)
476 (overlay-put (make-overlay beg (- beg-sub 1))
479 "[" (number-to-string sig-lines)
480 "-line signature. Press 's' to show.]"))
484 (defun notmuch-show-markup-part (beg end depth)
485 (if (re-search-forward notmuch-show-part-begin-regexp nil t)
488 (let ((beg (point-marker)))
489 (re-search-forward notmuch-show-part-end-regexp)
490 (let ((end (copy-marker (match-beginning 0))))
494 (indent-rigidly beg end depth)
495 (notmuch-show-markup-citations-region beg end depth)
496 ; Advance to the next part (if any) (so the outer loop can
497 ; determine whether we've left the current message.
498 (if (re-search-forward notmuch-show-part-begin-regexp nil t)
499 (beginning-of-line)))))
502 (defun notmuch-show-markup-parts-region (beg end depth)
505 (while (< (point) end)
506 (notmuch-show-markup-part beg end depth))))
508 (defun notmuch-show-markup-body (depth)
509 (re-search-forward notmuch-show-body-begin-regexp)
511 (let ((beg (point-marker)))
512 (re-search-forward notmuch-show-body-end-regexp)
513 (let ((end (copy-marker (match-beginning 0))))
514 (notmuch-show-markup-parts-region beg end depth)
515 (if (not (notmuch-show-message-unread-p))
516 (overlay-put (make-overlay beg end)
517 'invisible 'notmuch-show-body-read))
522 (defun notmuch-show-markup-header (depth)
523 (re-search-forward notmuch-show-header-begin-regexp)
525 (let ((beg (point-marker)))
527 ; Inverse video for subject
528 (overlay-put (make-overlay beg (point)) 'face '((cons :inverse-video t)))
530 (let ((beg-hidden (point-marker)))
531 (re-search-forward notmuch-show-header-end-regexp)
533 (let ((end (point-marker)))
534 (indent-rigidly beg end depth)
535 (overlay-put (make-overlay beg-hidden end)
536 'invisible 'notmuch-show-header)
538 (set-marker beg-hidden nil)
542 (defun notmuch-show-markup-message ()
543 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
545 (re-search-forward notmuch-show-depth-regexp)
546 (let ((depth (string-to-number (buffer-substring (match-beginning 1) (match-end 1)))))
547 (notmuch-show-markup-header depth)
548 (notmuch-show-markup-body depth)))
549 (goto-char (point-max))))
551 (defun notmuch-show-hide-markers ()
553 (goto-char (point-min))
555 (if (re-search-forward notmuch-show-marker-regexp nil t)
557 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
558 'invisible 'notmuch-show-marker))
559 (goto-char (point-max))))))
561 (defun notmuch-show-markup-messages ()
563 (goto-char (point-min))
565 (notmuch-show-markup-message)))
566 (notmuch-show-hide-markers))
568 (defun notmuch-show-toggle-citations-visible ()
569 "Toggle visibility of citations"
571 (if notmuch-show-citations-visible
572 (add-to-invisibility-spec 'notmuch-show-citation)
573 (remove-from-invisibility-spec 'notmuch-show-citation))
574 (set 'notmuch-show-citations-visible (not notmuch-show-citations-visible))
575 ; Need to force the redisplay for some reason
576 (force-window-update)
579 (defun notmuch-show-toggle-signatures-visible ()
580 "Toggle visibility of signatures"
582 (if notmuch-show-signatures-visible
583 (add-to-invisibility-spec 'notmuch-show-signature)
584 (remove-from-invisibility-spec 'notmuch-show-signature))
585 (set 'notmuch-show-signatures-visible (not notmuch-show-signatures-visible))
586 ; Need to force the redisplay for some reason
587 (force-window-update)
590 (defun notmuch-show-toggle-headers-visible ()
591 "Toggle visibility of header fields"
593 (if notmuch-show-headers-visible
594 (add-to-invisibility-spec 'notmuch-show-header)
595 (remove-from-invisibility-spec 'notmuch-show-header))
596 (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
597 ; Need to force the redisplay for some reason
598 (force-window-update)
601 (defun notmuch-show-toggle-body-read-visible ()
602 "Toggle visibility of message bodies of read messages"
604 (if notmuch-show-body-read-visible
605 (add-to-invisibility-spec 'notmuch-show-body-read)
606 (remove-from-invisibility-spec 'notmuch-show-body-read))
607 (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
608 ; Need to force the redisplay for some reason
609 (force-window-update)
613 (defun notmuch-show-mode ()
614 "Major mode for viewing a thread with notmuch.
616 This buffer contains the results of the \"notmuch show\" command
617 for displaying a single thread of email from your email archives.
619 By default, various components of email messages, (citations,
620 signatures, already-read messages), are invisible to help you
621 focus on the most important things, (new text from unread
622 messages). See the various commands below for toggling the
623 visibility of hidden components.
625 The `notmuch-show-next-message' and
626 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
627 default), allow you to navigate to the next and previous
628 messages. Each time you navigate away from a message with
629 `notmuch-show-next-message' the current message will have its
630 \"unread\" tag removed.
632 You can add or remove tags from the current message with '+' and
633 '-'. You can also archive all messages in the current
634 view, (remove the \"inbox\" tag from each), with
635 `notmuch-show-archive-thread' (bound to 'a' by default).
637 \\{notmuch-show-mode-map}"
639 (kill-all-local-variables)
640 (set (make-local-variable 'notmuch-show-headers-visible) t)
641 (notmuch-show-toggle-headers-visible)
642 (set (make-local-variable 'notmuch-show-body-read-visible) t)
643 (notmuch-show-toggle-body-read-visible)
644 (set (make-local-variable 'notmuch-show-citations-visible) t)
645 (notmuch-show-toggle-citations-visible)
646 (set (make-local-variable 'notmuch-show-signatures-visible) t)
647 (notmuch-show-toggle-signatures-visible)
648 (add-to-invisibility-spec 'notmuch-show-marker)
649 (use-local-map notmuch-show-mode-map)
650 (setq major-mode 'notmuch-show-mode
651 mode-name "notmuch-show")
652 (setq buffer-read-only t))
656 (defgroup notmuch nil
657 "Notmuch mail reader for Emacs."
660 (defcustom notmuch-show-hook nil
661 "List of functions to call when notmuch displays a message."
663 :options '(goto-address)
666 (defcustom notmuch-search-hook nil
667 "List of functions to call when notmuch displays the search results."
669 :options '(hl-line-mode)
672 ; Make show mode a bit prettier, highlighting URLs and using word wrap
674 (defun notmuch-show-pretty-hook ()
675 (goto-address-mode 1)
678 (add-hook 'notmuch-show-hook 'notmuch-show-pretty-hook)
679 (add-hook 'notmuch-search-hook
683 (defun notmuch-show (thread-id &optional parent-buffer)
684 "Run \"notmuch show\" with the given thread ID and display results.
686 The optional PARENT-BUFFER is the notmuch-search buffer from
687 which this notmuch-show command was executed, (so that the next
688 thread from that buffer can be show when done with this one)."
689 (interactive "sNotmuch show: ")
690 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
691 (switch-to-buffer buffer)
693 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
694 (let ((proc (get-buffer-process (current-buffer)))
695 (inhibit-read-only t))
697 (error "notmuch search process already running for query `%s'" thread-id)
700 (goto-char (point-min))
702 (call-process notmuch-command nil t nil "show" thread-id)
703 (notmuch-show-markup-messages)
705 (run-hooks 'notmuch-show-hook)
706 ; Move straight to the first unread message
707 (if (not (notmuch-show-message-unread-p))
709 (notmuch-show-next-unread-message)
710 ; But if there are no unread messages, go back to the
711 ; beginning of the buffer, and open up the bodies of all
713 (if (not (notmuch-show-message-unread-p))
715 (goto-char (point-min))
716 (notmuch-show-toggle-body-read-visible)))))
719 (defvar notmuch-search-authors-width 40
720 "Number of columns to use to display authors in a notmuch-search buffer.")
722 (defvar notmuch-search-mode-map
723 (let ((map (make-sparse-keymap)))
724 (define-key map "a" 'notmuch-search-archive-thread)
725 (define-key map "b" 'notmuch-search-scroll-down)
726 (define-key map "f" 'notmuch-search-filter)
727 (define-key map "m" 'message-mail)
728 (define-key map "n" 'next-line)
729 (define-key map "o" 'notmuch-search-toggle-order)
730 (define-key map "p" 'previous-line)
731 (define-key map "q" 'kill-this-buffer)
732 (define-key map "r" 'notmuch-search-reply-to-thread)
733 (define-key map "s" 'notmuch-search)
734 (define-key map "t" 'notmuch-search-filter-by-tag)
735 (define-key map "x" 'kill-this-buffer)
736 (define-key map (kbd "RET") 'notmuch-search-show-thread)
737 (define-key map "+" 'notmuch-search-add-tag)
738 (define-key map "-" 'notmuch-search-remove-tag)
739 (define-key map "<" 'beginning-of-buffer)
740 (define-key map ">" 'notmuch-search-goto-last-thread)
741 (define-key map "=" 'notmuch-search-refresh-view)
742 (define-key map "\M->" 'notmuch-search-goto-last-thread)
743 (define-key map " " 'notmuch-search-scroll-up)
744 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
745 (define-key map "?" 'describe-mode)
747 "Keymap for \"notmuch search\" buffers.")
748 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
750 (defvar notmuch-search-query-string)
751 (defvar notmuch-search-oldest-first)
753 (defun notmuch-search-scroll-up ()
754 "Scroll up, moving point to last message in thread if at end."
758 ((end-of-buffer) (notmuch-search-goto-last-thread))))
760 (defun notmuch-search-scroll-down ()
761 "Scroll down, moving point to first message in thread if at beginning."
763 ; I don't know why scroll-down doesn't signal beginning-of-buffer
764 ; the way that scroll-up signals end-of-buffer, but c'est la vie.
766 ; So instead of trapping a signal we instead check whether the
767 ; window begins on the first line of the buffer and if so, move
768 ; directly to that position. (We have to count lines since the
769 ; window-start position is not the same as point-min due to the
770 ; invisible thread-ID characters on the first line.
771 (if (equal (count-lines (point-min) (window-start)) 1)
772 (goto-char (window-start))
775 (defun notmuch-search-goto-last-thread ()
776 "Move point to the last thread in the buffer."
778 (goto-char (point-max))
782 (defun notmuch-search-mode ()
783 "Major mode for searching mail with notmuch.
785 This buffer contains the results of a \"notmuch search\" of your
786 email archives. Each line in the buffer represents a single
787 thread giving a relative date for the thread and a subject.
789 Pressing RET on any line displays that thread. The '+' and '-'
790 keys can be used to add or remove tags from a thread. The 'a' key
791 is a convenience key for archiving a thread (removing the
794 Other useful commands are `notmuch-search-filter' for filtering
795 the current search based on an additional query string,
796 `notmuch-search-filter-by-tag' for filtering to include only
797 messages with a given tag, and `notmuch-search' to execute a new,
800 \\{notmuch-search-mode-map}"
802 (kill-all-local-variables)
803 (make-local-variable 'notmuch-search-query-string)
804 (make-local-variable 'notmuch-search-oldest-first)
805 (set (make-local-variable 'scroll-preserve-screen-position) t)
806 (add-to-invisibility-spec 'notmuch-search)
807 (use-local-map notmuch-search-mode-map)
808 (setq truncate-lines t)
809 (setq major-mode 'notmuch-search-mode
810 mode-name "notmuch-search")
811 (setq buffer-read-only t))
813 (defun notmuch-search-find-thread-id ()
817 (re-search-forward "thread:[a-fA-F0-9]*" nil t)
818 (filter-buffer-substring beg (point)))))
820 (defun notmuch-search-markup-this-thread-id ()
823 (if (re-search-forward "thread:[a-fA-F0-9]*" nil t)
826 (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)
827 (re-search-forward ".*\\[[0-9]*/[0-9]*\\] \\([^;]*\\)\\(;\\)")
828 (let* ((authors (buffer-substring (match-beginning 1) (match-end 1)))
829 (authors-length (length authors)))
830 ;; Drop the semi-colon
831 (replace-match "" t nil nil 2)
832 (if (<= authors-length notmuch-search-authors-width)
833 (replace-match (concat authors (make-string
834 (- notmuch-search-authors-width
835 authors-length) ? )) t t nil 1)
836 (replace-match (concat (substring authors 0 (- notmuch-search-authors-width 3)) "...") t t nil 1)))))))
838 (defun notmuch-search-markup-thread-ids ()
840 (goto-char (point-min))
842 (notmuch-search-markup-this-thread-id)
845 (defun notmuch-search-show-thread ()
847 (let ((thread-id (notmuch-search-find-thread-id)))
848 (if (> (length thread-id) 0)
849 (notmuch-show thread-id (current-buffer))
850 (error "End of search results"))))
852 (defun notmuch-search-reply-to-thread ()
853 "Begin composing a reply to the entire current thread in a new buffer."
855 (let ((message-id (notmuch-search-find-thread-id)))
856 (notmuch-reply message-id)))
858 (defun notmuch-call-notmuch-process (&rest args)
859 "Synchronously invoke \"notmuch\" with the given list of arguments.
861 Output from the process will be presented to the user as an error
862 and will also appear in a buffer named \"*Notmuch errors*\"."
863 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
864 (with-current-buffer error-buffer
866 (if (eq (apply 'call-process notmuch-command nil error-buffer nil args) 0)
869 (with-current-buffer error-buffer
870 (let ((beg (point-min))
871 (end (- (point-max) 1)))
872 (error (buffer-substring beg end))
875 (defun notmuch-search-set-tags (tags)
878 (re-search-backward "(")
881 (inhibit-read-only t))
882 (re-search-forward ")")
885 (delete-region beg end)
886 (insert (mapconcat 'identity tags " "))))))
888 (defun notmuch-search-get-tags ()
891 (re-search-backward "(")
892 (let ((beg (+ (point) 1)))
893 (re-search-forward ")")
894 (let ((end (- (point) 1)))
895 (split-string (buffer-substring beg end))))))
897 (defun notmuch-search-add-tag (tag)
898 (interactive "sTag to add: ")
899 (notmuch-call-notmuch-process "tag" (concat "+" tag) (notmuch-search-find-thread-id))
900 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
902 (defun notmuch-search-remove-tag (tag)
903 (interactive "sTag to remove: ")
904 (notmuch-call-notmuch-process "tag" (concat "-" tag) (notmuch-search-find-thread-id))
905 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
907 (defun notmuch-search-archive-thread ()
908 "Archive the current thread (remove its \"inbox\" tag).
910 This function advances point to the next line when finished."
912 (notmuch-search-remove-tag "inbox")
915 (defun notmuch-search (query &optional oldest-first)
916 "Run \"notmuch search\" with the given query string and display results."
917 (interactive "sNotmuch search: ")
918 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
919 (switch-to-buffer buffer)
920 (notmuch-search-mode)
921 (set 'notmuch-search-query-string query)
922 (set 'notmuch-search-oldest-first oldest-first)
923 (let ((proc (get-buffer-process (current-buffer)))
924 (inhibit-read-only t))
926 (error "notmuch search process already running for query `%s'" query)
929 (goto-char (point-min))
932 (call-process notmuch-command nil t nil "search" "--sort=oldest-first" query)
933 (call-process notmuch-command nil t nil "search" "--sort=newest-first" query))
934 (notmuch-search-markup-thread-ids)
936 (run-hooks 'notmuch-search-hook)))
938 (defun notmuch-search-refresh-view ()
939 "Refresh the current view.
941 Kills the current buffer and runs a new search with the same
942 query string as the current search. If the current thread is in
943 the new search results, then point will be placed on the same
944 thread. Otherwise, point will be moved to attempt to be in the
945 same relative position within the new buffer."
948 (oldest-first notmuch-search-oldest-first)
949 (thread (notmuch-search-find-thread-id))
950 (query notmuch-search-query-string))
952 (notmuch-search query oldest-first)
953 (goto-char (point-min))
954 (if (re-search-forward (concat "^" thread) nil t)
958 (defun notmuch-search-toggle-order ()
959 "Toggle the current search order.
961 By default, the \"inbox\" view created by `notmuch' is displayed
962 in chronological order (oldest thread at the beginning of the
963 buffer), while any global searches created by `notmuch-search'
964 are displayed in reverse-chronological order (newest thread at
965 the beginning of the buffer).
967 This command toggles the sort order for the current search.
969 Note that any filtered searches created by
970 `notmuch-search-filter' retain the search order of the parent
973 (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
974 (notmuch-search-refresh-view))
976 (defun notmuch-search-filter (query)
977 "Filter the current search results based on an additional query string.
979 Runs a new search matching only messages that match both the
980 current search results AND the additional query string provided."
981 (interactive "sFilter search: ")
982 (notmuch-search (concat notmuch-search-query-string " and " query) notmuch-search-oldest-first))
984 (defun notmuch-search-filter-by-tag (tag)
985 "Filter the current search results based on a single tag.
987 Runs a new search matching only messages that match both the
988 current search results AND that are tagged with the given tag."
989 (interactive "sFilter by tag: ")
990 (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
993 "Run notmuch to display all mail with tag of 'inbox'"
995 (notmuch-search "tag:inbox" t))
997 (setq mail-user-agent 'message-user-agent)