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 (set 'notmuch-show-message-begin-regexp "
\fmessage{")
75 (set 'notmuch-show-message-end-regexp "
\fmessage}")
76 (set 'notmuch-show-header-begin-regexp "
\fheader{")
77 (set 'notmuch-show-header-end-regexp "
\fheader}")
78 (set 'notmuch-show-body-begin-regexp "
\fbody{")
79 (set 'notmuch-show-body-end-regexp "
\fbody}")
80 (set 'notmuch-show-attachment-begin-regexp "
\fattachment{")
81 (set 'notmuch-show-attachment-end-regexp "
\fattachment}")
82 (set 'notmuch-show-part-begin-regexp "
\fpart{")
83 (set 'notmuch-show-part-end-regexp "
\fpart}")
84 (set 'notmuch-show-marker-regexp "
\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
86 (set 'notmuch-show-id-regexp "\\(id:[^ ]*\\)")
87 (set 'notmuch-show-depth-regexp " depth:\\([0-9]*\\) ")
88 (set 'notmuch-show-filename-regexp "filename:\\(.*\\)$")
89 (set 'notmuch-show-tags-regexp "(\\([^)]*\\))$")
91 ; XXX: This should be a generic function in emacs somewhere, not here
92 (defun point-invisible-p ()
93 "Return whether the character at point is invisible.
95 Here visibility is determined by `buffer-invisibility-spec' and
96 the invisible property of any overlays for point. It doesn't have
97 anything to do with whether point is currently being displayed
98 within the current window."
99 (let ((prop (get-char-property (point) 'invisible)))
100 (if (eq buffer-invisibility-spec t)
102 (or (memq prop buffer-invisibility-spec)
103 (assq prop buffer-invisibility-spec)))))
105 (defun notmuch-show-next-line ()
106 "Like builtin `next-line' but ensuring we end on a visible character.
108 By advancing forward until reaching a visible character.
110 Unlike builtin `next-line' this version accepts no arguments."
112 (set 'this-command 'next-line)
113 (call-interactively 'next-line)
114 (while (point-invisible-p)
117 (defun notmuch-show-previous-line ()
118 "Like builtin `previous-line' but ensuring we end on a visible character.
120 By advancing forward until reaching a visible character.
122 Unlike builtin `next-line' this version accepts no arguments."
124 (set 'this-command 'previous-line)
125 (call-interactively 'previous-line)
126 (while (point-invisible-p)
129 (defun notmuch-show-get-message-id ()
132 (if (not (looking-at notmuch-show-message-begin-regexp))
133 (re-search-backward notmuch-show-message-begin-regexp))
134 (re-search-forward notmuch-show-id-regexp)
135 (buffer-substring (match-beginning 1) (match-end 1))))
137 (defun notmuch-show-get-filename ()
140 (if (not (looking-at notmuch-show-message-begin-regexp))
141 (re-search-backward notmuch-show-message-begin-regexp))
142 (re-search-forward notmuch-show-filename-regexp)
143 (buffer-substring (match-beginning 1) (match-end 1))))
145 (defun notmuch-show-set-tags (tags)
148 (if (not (looking-at notmuch-show-message-begin-regexp))
149 (re-search-backward notmuch-show-message-begin-regexp))
150 (re-search-forward notmuch-show-tags-regexp)
151 (let ((inhibit-read-only t)
152 (beg (match-beginning 1))
154 (delete-region beg end)
156 (insert (mapconcat 'identity tags " ")))))
158 (defun notmuch-show-get-tags ()
161 (if (not (looking-at notmuch-show-message-begin-regexp))
162 (re-search-backward notmuch-show-message-begin-regexp))
163 (re-search-forward notmuch-show-tags-regexp)
164 (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
166 (defun notmuch-show-add-tag (&rest toadd)
167 "Add a tag to the current message."
168 (interactive "sTag to add: ")
169 (apply 'notmuch-call-notmuch-process
171 (mapcar (lambda (s) (concat "+" s)) toadd))
172 (cons (notmuch-show-get-message-id) nil)))
173 (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
175 (defun notmuch-show-remove-tag (&rest toremove)
176 "Remove a tag from the current message."
177 (interactive "sTag to remove: ")
178 (let ((tags (notmuch-show-get-tags)))
179 (if (intersection tags toremove :test 'string=)
181 (apply 'notmuch-call-notmuch-process
183 (mapcar (lambda (s) (concat "-" s)) toremove))
184 (cons (notmuch-show-get-message-id) nil)))
185 (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
187 (defun notmuch-show-archive-thread-maybe-mark-read (markread)
189 (goto-char (point-min))
192 (notmuch-show-remove-tag "unread" "inbox")
193 (notmuch-show-remove-tag "inbox"))
196 (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
197 (goto-char (point-max)))))
198 (let ((parent-buffer notmuch-show-parent-buffer))
202 (switch-to-buffer parent-buffer)
204 (notmuch-search-show-thread)))))
206 (defun notmuch-show-mark-read-then-archive-thread ()
207 "Remove \"unread\" tag from each message, then archive and show next thread.
209 Archive each message currently shown by removing the \"unread\"
210 and \"inbox\" tag from each. Then kill this buffer and show the
211 next thread from the search from which this thread was originally
214 Note: This command is safe from any race condition of new messages
215 being delivered to the same thread. It does not archive the
216 entire thread, but only the messages shown in the current
219 (notmuch-show-archive-thread-maybe-mark-read t))
221 (defun notmuch-show-archive-thread ()
222 "Archive each message in thread, and show next thread from search.
224 Archive each message currently shown by removing the \"inbox\"
225 tag from each. Then kill this buffer and show the next thread
226 from the search from which this thread was originally shown.
228 Note: This command is safe from any race condition of new messages
229 being delivered to the same thread. It does not archive the
230 entire thread, but only the messages shown in the current
233 (notmuch-show-archive-thread-maybe-mark-read nil))
235 (defun notmuch-show-view-raw-message ()
236 "View the raw email of the current message."
238 (view-file (notmuch-show-get-filename)))
240 (defun notmuch-show-view-all-mime-parts ()
241 "Use external viewers (according to mailcap) to view all MIME-encoded parts."
244 (let ((filename (notmuch-show-get-filename)))
245 (switch-to-buffer (generate-new-buffer (concat "*notmuch-mime-"
248 (insert-file-contents filename nil nil nil t)
249 (mm-display-parts (mm-dissect-buffer))
250 (kill-this-buffer))))
252 (defun notmuch-show-reply ()
253 "Begin composing a reply to the current message in a new buffer."
255 (let ((message-id (notmuch-show-get-message-id)))
256 (switch-to-buffer (generate-new-buffer "notmuch-draft"))
257 (call-process "notmuch" nil t nil "reply" message-id)
258 (goto-char (point-min))
259 (if (re-search-forward "^$" nil t)
261 (insert "--text follows this line--")
265 (defun notmuch-show-pipe-message (command)
266 "Pipe the contents of the current message to the given command.
268 The given command will be executed with the raw contents of the
269 current email message as stdin. Anything printed by the command
270 to stdout or stderr will appear in the *Messages* buffer."
271 (interactive "sPipe message to command: ")
272 (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*" (split-string (concat command " < " (notmuch-show-get-filename)))))
274 (defun notmuch-show-move-to-current-message-summary-line ()
275 "Move to the beginning of the one-line summary of the current message.
277 This gives us a stable place to move to and work from since the
278 summary line is always visible. This is important since moving to
279 an invisible location is unreliable, (the main command loop moves
280 point either forward or backward to the next visible character
281 when a command ends with point on an invisible character).
283 Emits an error if point is not within a valid message, (that is
284 not pattern of `notmuch-show-message-begin-regexp' could be found
285 by searching backward)."
287 (if (not (looking-at notmuch-show-message-begin-regexp))
288 (if (re-search-backward notmuch-show-message-begin-regexp nil t)
290 (error "Not within a valid message."))
293 (defun notmuch-show-last-message-p ()
294 "Predicate testing whether point is within the last message."
295 (save-window-excursion
297 (notmuch-show-move-to-current-message-summary-line)
298 (not (re-search-forward notmuch-show-message-begin-regexp nil t)))))
300 (defun notmuch-show-message-unread-p ()
301 "Preficate testing whether current message is unread."
302 (member "unread" (notmuch-show-get-tags)))
304 (defun notmuch-show-next-message ()
305 "Advance to the beginning of the next message in the buffer.
307 Moves to the last visible character of the current message if
308 already on the last message in the buffer."
310 (notmuch-show-move-to-current-message-summary-line)
311 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
312 (notmuch-show-move-to-current-message-summary-line)
313 (goto-char (- (point-max) 1))
314 (while (point-invisible-p)
318 (defun notmuch-show-find-next-message ()
319 "Returns the position of the next message in the buffer.
321 Or the position of the last visible character of the current
322 message if already within the last message in the buffer."
323 ; save-excursion doesn't save our window position
324 ; save-window-excursion doesn't save point
325 ; Looks like we have to use both.
327 (save-window-excursion
328 (notmuch-show-next-message)
331 (defun notmuch-show-next-unread-message ()
332 "Advance to the beginning of the next unread message in the buffer.
334 Moves to the last visible character of the current message if
335 there are no more unread messages past the current point."
336 (notmuch-show-next-message)
337 (while (and (not (notmuch-show-last-message-p))
338 (not (notmuch-show-message-unread-p)))
339 (notmuch-show-next-message))
340 (if (not (notmuch-show-message-unread-p))
341 (notmuch-show-next-message)))
343 (defun notmuch-show-next-open-message ()
344 "Advance to the next message which is not hidden.
346 If read messages are currently hidden, advance to the next unread
347 message. Otherwise, advance to the next message."
348 (if (or (memq 'notmuch-show-body-read buffer-invisibility-spec)
349 (assq 'notmuch-show-body-read buffer-invisibility-spec))
350 (notmuch-show-next-unread-message)
351 (notmuch-show-next-message)))
353 (defun notmuch-show-previous-message ()
354 "Backup to the beginning of the previous message in the buffer.
356 If within a message rather than at the beginning of it, then
357 simply move to the beginning of the current message."
359 (let ((start (point)))
360 (notmuch-show-move-to-current-message-summary-line)
361 (if (not (< (point) start))
362 ; Go backward twice to skip the current message's marker
364 (re-search-backward notmuch-show-message-begin-regexp nil t)
365 (re-search-backward notmuch-show-message-begin-regexp nil t)
366 (notmuch-show-move-to-current-message-summary-line)
370 (defun notmuch-show-find-previous-message ()
371 "Returns the position of the previous message in the buffer.
373 Or the position of the beginning of the current message if point
374 is originally within the message rather than at the beginning of
376 ; save-excursion doesn't save our window position
377 ; save-window-excursion doesn't save point
378 ; Looks like we have to use both.
380 (save-window-excursion
381 (notmuch-show-previous-message)
384 (defun notmuch-show-mark-read-then-next-open-message ()
385 "Remove unread tag from current message, then advance to next unread message."
387 (notmuch-show-remove-tag "unread")
388 (notmuch-show-next-open-message))
390 (defun notmuch-show-rewind ()
391 "Do reverse scrolling compared to `notmuch-show-advance-marking-read-and-archiving'
393 Specifically, if the beginning of the previous email is fewer
394 than `window-height' lines from the current point, move to it
395 just like `notmuch-show-previous-message'.
397 Otherwise, just scroll down a screenful of the current message.
399 This command does not modify any message tags, (it does not undo
400 any effects from previous calls to
401 `notmuch-show-advance-marking-read-and-archiving'."
403 (let ((previous (notmuch-show-find-previous-message)))
404 (if (> (count-lines previous (point)) (- (window-height) next-screen-context-lines))
408 ((beginning-of-buffer) nil))
409 (goto-char (window-start)))
410 (notmuch-show-previous-message))))
412 (defun notmuch-show-advance-marking-read-and-archiving ()
413 "Advance through buffer, marking read and archiving.
415 This command is intended to be one of the simplest ways to
416 process a thread of email. It does the following:
418 If the current message in the thread is not yet fully visible,
419 scroll by a near screenful to read more of the message.
421 Otherwise, (the end of the current message is already within the
422 current window), remove the \"unread\" tag (if present) from the
423 current message and advance to the next open message.
425 Finally, if there is no further message to advance to, and this
426 last message is already read, then archive the entire current
427 thread, (remove the \"inbox\" tag from each message). Also kill
428 this buffer, and display the next thread from the search from
429 which this thread was originally shown."
431 (let ((next (notmuch-show-find-next-message))
432 (unread (notmuch-show-message-unread-p)))
433 (if (> next (window-end))
435 (let ((last (notmuch-show-last-message-p)))
436 (notmuch-show-mark-read-then-next-open-message)
438 (notmuch-show-archive-thread))))))
440 (defun notmuch-show-markup-citations-region (beg end depth)
443 (while (< (point) end)
444 (let ((beg-sub (point-marker))
445 (indent (make-string depth ? ))
446 (citation "[[:space:]]*>"))
447 (if (looking-at citation)
449 (while (looking-at citation)
451 (let ((overlay (make-overlay beg-sub (point))))
452 (overlay-put overlay 'invisible 'notmuch-show-citation)
453 (overlay-put overlay 'before-string
455 "[" (number-to-string (count-lines beg-sub (point)))
456 "-line citation. Press 'c' to show.]\n")))))
457 (move-to-column depth)
458 (if (looking-at notmuch-show-signature-regexp)
459 (let ((sig-lines (- (count-lines beg-sub end) 1)))
460 (if (<= sig-lines notmuch-show-signature-lines-max)
462 (overlay-put (make-overlay beg-sub end)
463 'invisible 'notmuch-show-signature)
464 (overlay-put (make-overlay beg (- beg-sub 1))
467 "[" (number-to-string sig-lines)
468 "-line signature. Press 's' to show.]"))
472 (defun notmuch-show-markup-part (beg end depth)
473 (if (re-search-forward notmuch-show-part-begin-regexp nil t)
476 (let ((beg (point-marker)))
477 (re-search-forward notmuch-show-part-end-regexp)
478 (let ((end (copy-marker (match-beginning 0))))
482 (indent-rigidly beg end depth)
483 (notmuch-show-markup-citations-region beg end depth)
484 ; Advance to the next part (if any) (so the outer loop can
485 ; determine whether we've left the current message.
486 (if (re-search-forward notmuch-show-part-begin-regexp nil t)
487 (beginning-of-line)))))
490 (defun notmuch-show-markup-parts-region (beg end depth)
493 (while (< (point) end)
494 (notmuch-show-markup-part beg end depth))))
496 (defun notmuch-show-markup-body (depth)
497 (re-search-forward notmuch-show-body-begin-regexp)
499 (let ((beg (point-marker)))
500 (re-search-forward notmuch-show-body-end-regexp)
501 (let ((end (copy-marker (match-beginning 0))))
502 (notmuch-show-markup-parts-region beg end depth)
503 (if (not (notmuch-show-message-unread-p))
504 (overlay-put (make-overlay beg end)
505 'invisible 'notmuch-show-body-read))
510 (defun notmuch-show-markup-header (depth)
511 (re-search-forward notmuch-show-header-begin-regexp)
513 (let ((beg (point-marker)))
515 ; Inverse video for subject
516 (overlay-put (make-overlay beg (point)) 'face '((cons :inverse-video t)))
518 (let ((beg-hidden (point-marker)))
519 (re-search-forward notmuch-show-header-end-regexp)
521 (let ((end (point-marker)))
522 (indent-rigidly beg end depth)
523 (overlay-put (make-overlay beg-hidden end)
524 'invisible 'notmuch-show-header)
526 (set-marker beg-hidden nil)
530 (defun notmuch-show-markup-message ()
531 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
533 (re-search-forward notmuch-show-depth-regexp)
534 (let ((depth (string-to-number (buffer-substring (match-beginning 1) (match-end 1)))))
535 (notmuch-show-markup-header depth)
536 (notmuch-show-markup-body depth)))
537 (goto-char (point-max))))
539 (defun notmuch-show-hide-markers ()
541 (goto-char (point-min))
543 (if (re-search-forward notmuch-show-marker-regexp nil t)
545 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
546 'invisible 'notmuch-show-marker))
547 (goto-char (point-max))))))
549 (defun notmuch-show-markup-messages ()
551 (goto-char (point-min))
553 (notmuch-show-markup-message)))
554 (notmuch-show-hide-markers))
556 (defun notmuch-show-toggle-citations-visible ()
557 "Toggle visibility of citations"
559 (if notmuch-show-citations-visible
560 (add-to-invisibility-spec 'notmuch-show-citation)
561 (remove-from-invisibility-spec 'notmuch-show-citation))
562 (set 'notmuch-show-citations-visible (not notmuch-show-citations-visible))
563 ; Need to force the redisplay for some reason
564 (force-window-update)
567 (defun notmuch-show-toggle-signatures-visible ()
568 "Toggle visibility of signatures"
570 (if notmuch-show-signatures-visible
571 (add-to-invisibility-spec 'notmuch-show-signature)
572 (remove-from-invisibility-spec 'notmuch-show-signature))
573 (set 'notmuch-show-signatures-visible (not notmuch-show-signatures-visible))
574 ; Need to force the redisplay for some reason
575 (force-window-update)
578 (defun notmuch-show-toggle-headers-visible ()
579 "Toggle visibility of header fields"
581 (if notmuch-show-headers-visible
582 (add-to-invisibility-spec 'notmuch-show-header)
583 (remove-from-invisibility-spec 'notmuch-show-header))
584 (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
585 ; Need to force the redisplay for some reason
586 (force-window-update)
589 (defun notmuch-show-toggle-body-read-visible ()
590 "Toggle visibility of message bodies of read messages"
592 (if notmuch-show-body-read-visible
593 (add-to-invisibility-spec 'notmuch-show-body-read)
594 (remove-from-invisibility-spec 'notmuch-show-body-read))
595 (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
596 ; Need to force the redisplay for some reason
597 (force-window-update)
601 (defun notmuch-show-mode ()
602 "Major mode for viewing a thread with notmuch.
604 This buffer contains the results of the \"notmuch show\" command
605 for displaying a single thread of email from your email archives.
607 By default, various components of email messages, (citations,
608 signatures, already-read messages), are invisible to help you
609 focus on the most important things, (new text from unread
610 messages). See the various commands below for toggling the
611 visibility of hidden components.
613 The `notmuch-show-next-message' and
614 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
615 default), allow you to navigate to the next and previous
616 messages. Each time you navigate away from a message with
617 `notmuch-show-next-message' the current message will have its
618 \"unread\" tag removed.
620 You can add or remove tags from the current message with '+' and
621 '-'. You can also archive all messages in the current
622 view, (remove the \"inbox\" tag from each), with
623 `notmuch-show-archive-thread' (bound to 'a' by default).
625 \\{notmuch-show-mode-map}"
627 (kill-all-local-variables)
628 (set (make-local-variable 'notmuch-show-headers-visible) t)
629 (notmuch-show-toggle-headers-visible)
630 (set (make-local-variable 'notmuch-show-body-read-visible) t)
631 (notmuch-show-toggle-body-read-visible)
632 (set (make-local-variable 'notmuch-show-citations-visible) t)
633 (notmuch-show-toggle-citations-visible)
634 (set (make-local-variable 'notmuch-show-signatures-visible) t)
635 (notmuch-show-toggle-signatures-visible)
636 (add-to-invisibility-spec 'notmuch-show-marker)
637 (use-local-map notmuch-show-mode-map)
638 (setq major-mode 'notmuch-show-mode
639 mode-name "notmuch-show")
640 (setq buffer-read-only t))
644 (defgroup notmuch nil
645 "Notmuch mail reader for Emacs."
648 (defcustom notmuch-show-hook nil
649 "List of functions to call when notmuch displays a message."
651 :options '(goto-address)
654 (defun notmuch-show (thread-id &optional parent-buffer)
655 "Run \"notmuch show\" with the given thread ID and display results.
657 The optional PARENT-BUFFER is the notmuch-search buffer from
658 which this notmuch-show command was executed, (so that the next
659 thread from that buffer can be show when done with this one)."
660 (interactive "sNotmuch show: ")
661 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
662 (switch-to-buffer buffer)
664 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
665 (let ((proc (get-buffer-process (current-buffer)))
666 (inhibit-read-only t))
668 (error "notmuch search process already running for query `%s'" query)
671 (goto-char (point-min))
673 (call-process "notmuch" nil t nil "show" thread-id)
674 (notmuch-show-markup-messages)
676 (run-hooks 'notmuch-show-hook)
677 ; Move straight to the first unread message
678 (if (not (notmuch-show-message-unread-p))
680 (notmuch-show-next-unread-message)
681 ; But if there are no unread messages, go back to the
682 ; beginning of the buffer, and open up the bodies of all
684 (if (not (notmuch-show-message-unread-p))
686 (goto-char (point-min))
687 (notmuch-show-toggle-body-read-visible)))))
690 (defvar notmuch-search-authors-width 40
691 "Number of columns to use to display authors in a notmuch-search buffer.")
693 (defvar notmuch-search-mode-map
694 (let ((map (make-sparse-keymap)))
695 (define-key map "a" 'notmuch-search-archive-thread)
696 (define-key map "b" 'notmuch-search-scroll-down)
697 (define-key map "f" 'notmuch-search-filter)
698 (define-key map "m" 'message-mail)
699 (define-key map "n" 'next-line)
700 (define-key map "o" 'notmuch-search-toggle-order)
701 (define-key map "p" 'previous-line)
702 (define-key map "q" 'kill-this-buffer)
703 (define-key map "s" 'notmuch-search)
704 (define-key map "t" 'notmuch-search-filter-by-tag)
705 (define-key map "x" 'kill-this-buffer)
706 (define-key map (kbd "RET") 'notmuch-search-show-thread)
707 (define-key map "+" 'notmuch-search-add-tag)
708 (define-key map "-" 'notmuch-search-remove-tag)
709 (define-key map "<" 'beginning-of-buffer)
710 (define-key map ">" 'notmuch-search-goto-last-thread)
711 (define-key map "=" 'notmuch-search-refresh-view)
712 (define-key map "\M->" 'notmuch-search-goto-last-thread)
713 (define-key map " " 'notmuch-search-scroll-up)
714 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
715 (define-key map "?" 'describe-mode)
717 "Keymap for \"notmuch search\" buffers.")
718 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
720 (defun notmuch-search-scroll-up ()
721 "Scroll up, moving point to last message in thread if at end."
725 ((end-of-buffer) (notmuch-search-goto-last-thread))))
727 (defun notmuch-search-scroll-down ()
728 "Scroll down, moving point to first message in thread if at beginning."
730 ; I don't know why scroll-down doesn't signal beginning-of-buffer
731 ; the way that scroll-up signals end-of-buffer, but c'est la vie.
733 ; So instead of trapping a signal we instead check whether the
734 ; window begins on the first line of the buffer and if so, move
735 ; directly to that position. (We have to count lines since the
736 ; window-start position is not the same as point-min due to the
737 ; invisible thread-ID characters on the first line.
738 (if (equal (count-lines (point-min) (window-start)) 1)
739 (goto-char (window-start))
742 (defun notmuch-search-goto-last-thread (&optional arg)
743 "Move point to the last thread in the buffer."
749 (defun notmuch-search-mode ()
750 "Major mode for searching mail with notmuch.
752 This buffer contains the results of a \"notmuch search\" of your
753 email archives. Each line in the buffer represents a single
754 thread giving a relative date for the thread and a subject.
756 Pressing RET on any line displays that thread. The '+' and '-'
757 keys can be used to add or remove tags from a thread. The 'a' key
758 is a convenience key for archiving a thread (removing the
761 Other useful commands are `notmuch-search-filter' for filtering
762 the current search based on an additional query string,
763 `notmuch-search-filter-by-tag' for filtering to include only
764 messages with a given tag, and `notmuch-search' to execute a new,
767 \\{notmuch-search-mode-map}"
769 (kill-all-local-variables)
770 (make-local-variable 'notmuch-search-query-string)
771 (make-local-variable 'notmuch-search-oldest-first)
772 (set (make-local-variable 'scroll-preserve-screen-position) t)
773 (add-to-invisibility-spec 'notmuch-search)
774 (use-local-map notmuch-search-mode-map)
775 (setq major-mode 'notmuch-search-mode
776 mode-name "notmuch-search")
777 (setq buffer-read-only t))
779 (defun notmuch-search-find-thread-id ()
783 (re-search-forward "thread:[a-fA-F0-9]*" nil t)
784 (filter-buffer-substring beg (point)))))
786 (defun notmuch-search-markup-this-thread-id ()
789 (if (re-search-forward "thread:[a-fA-F0-9]*" nil t)
792 (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)
793 (re-search-forward ".*\\[[0-9]*/[0-9]*\\] \\([^;]*\\)\\(;\\)")
794 (let* ((authors (buffer-substring (match-beginning 1) (match-end 1)))
795 (authors-length (length authors)))
796 ;; Drop the semi-colon
797 (replace-match "" t nil nil 2)
798 (if (<= authors-length notmuch-search-authors-width)
799 (replace-match (concat authors (make-string
800 (- notmuch-search-authors-width
801 authors-length) ? )) t t nil 1)
802 (replace-match (concat (substring authors 0 (- notmuch-search-authors-width 3)) "...") t t nil 1)))))))
804 (defun notmuch-search-markup-thread-ids ()
806 (goto-char (point-min))
808 (notmuch-search-markup-this-thread-id)
811 (defun notmuch-search-show-thread ()
813 (let ((thread-id (notmuch-search-find-thread-id)))
814 (if (> (length thread-id) 0)
815 (notmuch-show thread-id (current-buffer))
816 (error "End of search results"))))
818 (defun notmuch-call-notmuch-process (&rest args)
819 "Synchronously invoke \"notmuch\" with the given list of arguments.
821 Output from the process will be presented to the user as an error
822 and will also appear in a buffer named \"*Notmuch errors*\"."
823 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
824 (with-current-buffer error-buffer
826 (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
829 (with-current-buffer error-buffer
830 (let ((beg (point-min))
831 (end (- (point-max) 1)))
832 (error (buffer-substring beg end))
835 (defun notmuch-search-set-tags (tags)
838 (re-search-backward "(")
841 (inhibit-read-only t))
842 (re-search-forward ")")
845 (delete-region beg end)
846 (insert (mapconcat 'identity tags " "))))))
848 (defun notmuch-search-get-tags ()
851 (re-search-backward "(")
852 (let ((beg (+ (point) 1)))
853 (re-search-forward ")")
854 (let ((end (- (point) 1)))
855 (split-string (buffer-substring beg end))))))
857 (defun notmuch-search-add-tag (tag)
858 (interactive "sTag to add: ")
859 (notmuch-call-notmuch-process "tag" (concat "+" tag) (notmuch-search-find-thread-id))
860 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
862 (defun notmuch-search-remove-tag (tag)
863 (interactive "sTag to remove: ")
864 (notmuch-call-notmuch-process "tag" (concat "-" tag) (notmuch-search-find-thread-id))
865 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
867 (defun notmuch-search-archive-thread ()
868 "Archive the current thread (remove its \"inbox\" tag).
870 This function advances point to the next line when finished."
872 (notmuch-search-remove-tag "inbox")
875 (defun notmuch-search (query &optional oldest-first)
876 "Run \"notmuch search\" with the given query string and display results."
877 (interactive "sNotmuch search: ")
878 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
879 (switch-to-buffer buffer)
880 (notmuch-search-mode)
881 (set 'notmuch-search-query-string query)
882 (set 'notmuch-search-oldest-first oldest-first)
883 (let ((proc (get-buffer-process (current-buffer)))
884 (inhibit-read-only t))
886 (error "notmuch search process already running for query `%s'" query)
889 (goto-char (point-min))
892 (call-process "notmuch" nil t nil "search" "--sort=oldest-first" query)
893 (call-process "notmuch" nil t nil "search" "--sort=newest-first" query))
894 (notmuch-search-markup-thread-ids)
897 (defun notmuch-search-refresh-view ()
898 "Refresh the current view.
900 Kills the current buffer and runs a new search with the same
901 query string as the current search. If the current thread is in
902 the new search results, then point will be placed on the same
903 thread. Otherwise, point will be moved to attempt to be in the
904 same relative position within the new buffer."
907 (oldest-first notmuch-search-oldest-first)
908 (thread (notmuch-search-find-thread-id))
909 (query notmuch-search-query-string))
911 (notmuch-search query oldest-first)
912 (goto-char (point-min))
913 (if (re-search-forward (concat "^" thread) nil t)
917 (defun notmuch-search-toggle-order ()
918 "Toggle the current search order.
920 By default, the \"inbox\" view created by `notmuch' is displayed
921 in chronological order (oldest thread at the beginning of the
922 buffer), while any global searches created by `notmuch-search'
923 are displayed in reverse-chronological order (newest thread at
924 the beginning of the buffer).
926 This command toggles the sort order for the current search.
928 Note that any filtered searches created by
929 `notmuch-search-filter' retain the search order of the parent
932 (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
933 (notmuch-search-refresh-view))
935 (defun notmuch-search-filter (query)
936 "Filter the current search results based on an additional query string.
938 Runs a new search matching only messages that match both the
939 current search results AND the additional query string provided."
940 (interactive "sFilter search: ")
941 (notmuch-search (concat notmuch-search-query-string " and " query) notmuch-search-oldest-first))
943 (defun notmuch-search-filter-by-tag (tag)
944 "Filter the current search results based on a single tag.
946 Runs a new search matching only messages that match both the
947 current search results AND that are tagged with the given tag."
948 (interactive "sFilter by tag: ")
949 (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
952 "Run notmuch to display all mail with tag of 'inbox'"
954 (notmuch-search "tag:inbox" t))