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).
54 (defvar notmuch-show-mode-map
55 (let ((map (make-sparse-keymap)))
56 ; I don't actually want all of these toggle commands occupying
57 ; keybindings. They steal valuable key-binding space, are hard
58 ; to remember, and act globally rather than locally.
60 ; Will be much preferable to switch to direct manipulation for
61 ; toggling visibility of these components. Probably using
62 ; overlays-at to query and manipulate the current overlay.
63 (define-key map "a" 'notmuch-show-archive-thread)
64 (define-key map "A" 'notmuch-show-mark-read-then-archive-thread)
65 (define-key map "m" 'message-mail)
66 (define-key map "n" 'notmuch-show-next-message)
67 (define-key map "N" 'notmuch-show-mark-read-then-next-open-message)
68 (define-key map "p" 'notmuch-show-previous-message)
69 (define-key map (kbd "C-n") 'notmuch-show-next-line)
70 (define-key map (kbd "C-p") 'notmuch-show-previous-line)
71 (define-key map "q" 'kill-this-buffer)
72 (define-key map "r" 'notmuch-show-reply)
73 (define-key map "v" 'notmuch-show-view-all-mime-parts)
74 (define-key map "w" 'notmuch-show-view-raw-message)
75 (define-key map "x" 'kill-this-buffer)
76 (define-key map "+" 'notmuch-show-add-tag)
77 (define-key map "-" 'notmuch-show-remove-tag)
78 (define-key map (kbd "DEL") 'notmuch-show-rewind)
79 (define-key map " " 'notmuch-show-advance-marking-read-and-archiving)
80 (define-key map "|" 'notmuch-show-pipe-message)
81 (define-key map "?" 'describe-mode)
82 (define-key map (kbd "TAB") 'notmuch-show-next-button)
83 (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
85 "Keymap for \"notmuch show\" buffers.")
86 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
88 (defvar notmuch-show-signature-regexp "\\(-- ?\\|_+\\)$"
89 "Pattern to match a line that separates content from signature.
91 The regexp can (and should) include $ to match the end of the
92 line, but should not include ^ to match the beginning of the
93 line. This is because notmuch may have inserted additional space
94 for indentation at the beginning of the line. But notmuch will
95 move past the indentation when testing this pattern, (so that the
96 pattern can still test against the entire line).")
98 (defvar notmuch-show-signature-lines-max 12
99 "Maximum length of signature that will be hidden by default.")
101 (defvar notmuch-command "notmuch"
102 "Command to run the notmuch binary.")
104 (defvar notmuch-show-message-begin-regexp "\fmessage{")
105 (defvar notmuch-show-message-end-regexp "\fmessage}")
106 (defvar notmuch-show-header-begin-regexp "\fheader{")
107 (defvar notmuch-show-header-end-regexp "\fheader}")
108 (defvar notmuch-show-body-begin-regexp "\fbody{")
109 (defvar notmuch-show-body-end-regexp "\fbody}")
110 (defvar notmuch-show-attachment-begin-regexp "\fattachment{")
111 (defvar notmuch-show-attachment-end-regexp "\fattachment}")
112 (defvar notmuch-show-part-begin-regexp "\fpart{")
113 (defvar notmuch-show-part-end-regexp "\fpart}")
114 (defvar notmuch-show-marker-regexp "\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
116 (defvar notmuch-show-id-regexp "\\(id:[^ ]*\\)")
117 (defvar notmuch-show-depth-regexp " depth:\\([0-9]*\\) ")
118 (defvar notmuch-show-filename-regexp "filename:\\(.*\\)$")
119 (defvar notmuch-show-tags-regexp "(\\([^)]*\\))$")
121 (defvar notmuch-show-parent-buffer nil)
122 (defvar notmuch-show-body-read-visible nil)
123 (defvar notmuch-show-citations-visible nil)
124 (defvar notmuch-show-signatures-visible nil)
125 (defvar notmuch-show-headers-visible nil)
127 ; XXX: This should be a generic function in emacs somewhere, not here
128 (defun point-invisible-p ()
129 "Return whether the character at point is invisible.
131 Here visibility is determined by `buffer-invisibility-spec' and
132 the invisible property of any overlays for point. It doesn't have
133 anything to do with whether point is currently being displayed
134 within the current window."
135 (let ((prop (get-char-property (point) 'invisible)))
136 (if (eq buffer-invisibility-spec t)
138 (or (memq prop buffer-invisibility-spec)
139 (assq prop buffer-invisibility-spec)))))
141 (defun notmuch-select-tag-with-completion (prompt &rest search-terms)
143 (with-output-to-string
144 (with-current-buffer standard-output
145 (apply 'call-process notmuch-command nil t nil "search-tags" search-terms)))))
146 (completing-read prompt (split-string tag-list "\n+" t) nil nil nil)))
148 (defun notmuch-show-next-line ()
149 "Like builtin `next-line' but ensuring we end on a visible character.
151 By advancing forward until reaching a visible character.
153 Unlike builtin `next-line' this version accepts no arguments."
155 (set 'this-command 'next-line)
156 (call-interactively 'next-line)
157 (while (point-invisible-p)
160 (defun notmuch-show-previous-line ()
161 "Like builtin `previous-line' but ensuring we end on a visible character.
163 By advancing forward until reaching a visible character.
165 Unlike builtin `next-line' this version accepts no arguments."
167 (set 'this-command 'previous-line)
168 (call-interactively 'previous-line)
169 (while (point-invisible-p)
172 (defun notmuch-show-get-message-id ()
175 (if (not (looking-at notmuch-show-message-begin-regexp))
176 (re-search-backward notmuch-show-message-begin-regexp))
177 (re-search-forward notmuch-show-id-regexp)
178 (buffer-substring-no-properties (match-beginning 1) (match-end 1))))
180 (defun notmuch-show-get-filename ()
183 (if (not (looking-at notmuch-show-message-begin-regexp))
184 (re-search-backward notmuch-show-message-begin-regexp))
185 (re-search-forward notmuch-show-filename-regexp)
186 (buffer-substring-no-properties (match-beginning 1) (match-end 1))))
188 (defun notmuch-show-set-tags (tags)
191 (if (not (looking-at notmuch-show-message-begin-regexp))
192 (re-search-backward notmuch-show-message-begin-regexp))
193 (re-search-forward notmuch-show-tags-regexp)
194 (let ((inhibit-read-only t)
195 (beg (match-beginning 1))
197 (delete-region beg end)
199 (insert (mapconcat 'identity tags " ")))))
201 (defun notmuch-show-get-tags ()
204 (if (not (looking-at notmuch-show-message-begin-regexp))
205 (re-search-backward notmuch-show-message-begin-regexp))
206 (re-search-forward notmuch-show-tags-regexp)
207 (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
209 (defun notmuch-show-add-tag (&rest toadd)
210 "Add a tag to the current message."
212 (list (notmuch-select-tag-with-completion "Tag to add: ")))
213 (apply 'notmuch-call-notmuch-process
215 (mapcar (lambda (s) (concat "+" s)) toadd))
216 (cons (notmuch-show-get-message-id) nil)))
217 (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
219 (defun notmuch-show-remove-tag (&rest toremove)
220 "Remove a tag from the current message."
222 (list (notmuch-select-tag-with-completion "Tag to remove: " (notmuch-show-get-message-id))))
223 (let ((tags (notmuch-show-get-tags)))
224 (if (intersection tags toremove :test 'string=)
226 (apply 'notmuch-call-notmuch-process
228 (mapcar (lambda (s) (concat "-" s)) toremove))
229 (cons (notmuch-show-get-message-id) nil)))
230 (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
232 (defun notmuch-show-archive-thread-maybe-mark-read (markread)
234 (goto-char (point-min))
237 (notmuch-show-remove-tag "unread" "inbox")
238 (notmuch-show-remove-tag "inbox"))
241 (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
242 (goto-char (point-max)))))
243 (let ((parent-buffer notmuch-show-parent-buffer))
247 (switch-to-buffer parent-buffer)
249 (notmuch-search-show-thread)))))
251 (defun notmuch-show-mark-read-then-archive-thread ()
252 "Remove \"unread\" tag from each message, then archive and show next thread.
254 Archive each message currently shown by removing the \"unread\"
255 and \"inbox\" tag from each. Then kill this buffer and show the
256 next thread from the search from which this thread was originally
259 Note: This command is safe from any race condition of new messages
260 being delivered to the same thread. It does not archive the
261 entire thread, but only the messages shown in the current
264 (notmuch-show-archive-thread-maybe-mark-read t))
266 (defun notmuch-show-archive-thread ()
267 "Archive each message in thread, and show next thread from search.
269 Archive each message currently shown by removing the \"inbox\"
270 tag from each. Then kill this buffer and show the next thread
271 from the search from which this thread was originally shown.
273 Note: This command is safe from any race condition of new messages
274 being delivered to the same thread. It does not archive the
275 entire thread, but only the messages shown in the current
278 (notmuch-show-archive-thread-maybe-mark-read nil))
280 (defun notmuch-show-view-raw-message ()
281 "View the raw email of the current message."
283 (view-file (notmuch-show-get-filename)))
285 (defun notmuch-show-view-all-mime-parts ()
286 "Use external viewers (according to mailcap) to view all MIME-encoded parts."
289 (let ((filename (notmuch-show-get-filename)))
290 (switch-to-buffer (generate-new-buffer (concat "*notmuch-mime-"
293 (insert-file-contents filename nil nil nil t)
294 (mm-display-parts (mm-dissect-buffer))
295 (kill-this-buffer))))
297 (defun notmuch-reply (query-string)
298 (switch-to-buffer (generate-new-buffer "notmuch-draft"))
299 (call-process notmuch-command nil t nil "reply" query-string)
300 (message-insert-signature)
301 (goto-char (point-min))
302 (if (re-search-forward "^$" nil t)
304 (insert "--text follows this line--")
308 (defun notmuch-show-reply ()
309 "Begin composing a reply to the current message in a new buffer."
311 (let ((message-id (notmuch-show-get-message-id)))
312 (notmuch-reply message-id)))
314 (defun notmuch-show-pipe-message (command)
315 "Pipe the contents of the current message to the given command.
317 The given command will be executed with the raw contents of the
318 current email message as stdin. Anything printed by the command
319 to stdout or stderr will appear in the *Messages* buffer."
320 (interactive "sPipe message to command: ")
321 (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*"
322 (list command " < " (shell-quote-argument (notmuch-show-get-filename)))))
324 (defun notmuch-show-move-to-current-message-summary-line ()
325 "Move to the beginning of the one-line summary of the current message.
327 This gives us a stable place to move to and work from since the
328 summary line is always visible. This is important since moving to
329 an invisible location is unreliable, (the main command loop moves
330 point either forward or backward to the next visible character
331 when a command ends with point on an invisible character).
333 Emits an error if point is not within a valid message, (that is
334 not pattern of `notmuch-show-message-begin-regexp' could be found
335 by searching backward)."
337 (if (not (looking-at notmuch-show-message-begin-regexp))
338 (if (re-search-backward notmuch-show-message-begin-regexp nil t)
340 (error "Not within a valid message."))
343 (defun notmuch-show-last-message-p ()
344 "Predicate testing whether point is within the last message."
345 (save-window-excursion
347 (notmuch-show-move-to-current-message-summary-line)
348 (not (re-search-forward notmuch-show-message-begin-regexp nil t)))))
350 (defun notmuch-show-message-unread-p ()
351 "Preficate testing whether current message is unread."
352 (member "unread" (notmuch-show-get-tags)))
354 (defun notmuch-show-next-message ()
355 "Advance to the beginning of the next message in the buffer.
357 Moves to the last visible character of the current message if
358 already on the last message in the buffer."
360 (notmuch-show-move-to-current-message-summary-line)
361 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
362 (notmuch-show-move-to-current-message-summary-line)
363 (goto-char (- (point-max) 1))
364 (while (point-invisible-p)
368 (defun notmuch-show-find-next-message ()
369 "Returns the position of the next message in the buffer.
371 Or the position of the last visible character of the current
372 message if already within the last message in the buffer."
373 ; save-excursion doesn't save our window position
374 ; save-window-excursion doesn't save point
375 ; Looks like we have to use both.
377 (save-window-excursion
378 (notmuch-show-next-message)
381 (defun notmuch-show-next-unread-message ()
382 "Advance to the beginning of the next unread message in the buffer.
384 Moves to the last visible character of the current message if
385 there are no more unread messages past the current point."
386 (notmuch-show-next-message)
387 (while (and (not (notmuch-show-last-message-p))
388 (not (notmuch-show-message-unread-p)))
389 (notmuch-show-next-message))
390 (if (not (notmuch-show-message-unread-p))
391 (notmuch-show-next-message)))
393 (defun notmuch-show-next-open-message ()
394 "Advance to the next message which is not hidden.
396 If read messages are currently hidden, advance to the next unread
397 message. Otherwise, advance to the next message."
398 (if (or (memq 'notmuch-show-body-read buffer-invisibility-spec)
399 (assq 'notmuch-show-body-read buffer-invisibility-spec))
400 (notmuch-show-next-unread-message)
401 (notmuch-show-next-message)))
403 (defun notmuch-show-previous-message ()
404 "Backup to the beginning of the previous message in the buffer.
406 If within a message rather than at the beginning of it, then
407 simply move to the beginning of the current message."
409 (let ((start (point)))
410 (notmuch-show-move-to-current-message-summary-line)
411 (if (not (< (point) start))
412 ; Go backward twice to skip the current message's marker
414 (re-search-backward notmuch-show-message-begin-regexp nil t)
415 (re-search-backward notmuch-show-message-begin-regexp nil t)
416 (notmuch-show-move-to-current-message-summary-line)
420 (defun notmuch-show-find-previous-message ()
421 "Returns the position of the previous message in the buffer.
423 Or the position of the beginning of the current message if point
424 is originally within the message rather than at the beginning of
426 ; save-excursion doesn't save our window position
427 ; save-window-excursion doesn't save point
428 ; Looks like we have to use both.
430 (save-window-excursion
431 (notmuch-show-previous-message)
434 (defun notmuch-show-mark-read-then-next-open-message ()
435 "Remove unread tag from current message, then advance to next unread message."
437 (notmuch-show-remove-tag "unread")
438 (notmuch-show-next-open-message))
440 (defun notmuch-show-rewind ()
441 "Do reverse scrolling compared to `notmuch-show-advance-marking-read-and-archiving'
443 Specifically, if the beginning of the previous email is fewer
444 than `window-height' lines from the current point, move to it
445 just like `notmuch-show-previous-message'.
447 Otherwise, just scroll down a screenful of the current message.
449 This command does not modify any message tags, (it does not undo
450 any effects from previous calls to
451 `notmuch-show-advance-marking-read-and-archiving'."
453 (let ((previous (notmuch-show-find-previous-message)))
454 (if (> (count-lines previous (point)) (- (window-height) next-screen-context-lines))
458 ((beginning-of-buffer) nil))
459 (goto-char (window-start)))
460 (notmuch-show-previous-message))))
462 (defun notmuch-show-advance-marking-read-and-archiving ()
463 "Advance through buffer, marking read and archiving.
465 This command is intended to be one of the simplest ways to
466 process a thread of email. It does the following:
468 If the current message in the thread is not yet fully visible,
469 scroll by a near screenful to read more of the message.
471 Otherwise, (the end of the current message is already within the
472 current window), remove the \"unread\" tag (if present) from the
473 current message and advance to the next open message.
475 Finally, if there is no further message to advance to, and this
476 last message is already read, then archive the entire current
477 thread, (remove the \"inbox\" tag from each message). Also kill
478 this buffer, and display the next thread from the search from
479 which this thread was originally shown."
481 (let ((next (notmuch-show-find-next-message))
482 (unread (notmuch-show-message-unread-p)))
483 (if (> next (window-end))
485 (let ((last (notmuch-show-last-message-p)))
486 (notmuch-show-mark-read-then-next-open-message)
488 (notmuch-show-archive-thread))))))
490 (defun notmuch-show-next-button ()
491 "Advance point to the next button in the buffer."
493 (goto-char (button-start (next-button (point)))))
495 (defun notmuch-show-previous-button ()
496 "Move point back to the previous button in the buffer."
498 (goto-char (button-start (previous-button (point)))))
500 (defun notmuch-toggle-invisible-action (cite-button)
501 (let ((invis-spec (button-get button 'invisibility-spec)))
502 (if (invisible-p invis-spec)
503 (remove-from-invisibility-spec invis-spec)
504 (add-to-invisibility-spec invis-spec)
506 (force-window-update)
509 (define-button-type 'notmuch-button-invisibility-toggle-type 'action 'notmuch-toggle-invisible-action 'follow-link t)
510 (define-button-type 'notmuch-button-citation-toggle-type 'help-echo "mouse-1, RET: Show citation"
511 :supertype 'notmuch-button-invisibility-toggle-type)
512 (define-button-type 'notmuch-button-signature-toggle-type 'help-echo "mouse-1, RET: Show signature"
513 :supertype 'notmuch-button-invisibility-toggle-type)
514 (define-button-type 'notmuch-button-headers-toggle-type 'help-echo "mouse-1, RET: Show headers"
515 :supertype 'notmuch-button-invisibility-toggle-type)
516 (define-button-type 'notmuch-button-body-toggle-type 'help-echo "mouse-1, RET: Show message"
517 :supertype 'notmuch-button-invisibility-toggle-type)
519 (defun notmuch-show-markup-citations-region (beg end depth)
522 (while (< (point) end)
523 (let ((beg-sub (point-marker))
524 (indent (make-string depth ? ))
525 (citation "[[:space:]]*>"))
526 (if (looking-at citation)
528 (while (looking-at citation)
530 (let ((overlay (make-overlay beg-sub (point)))
531 (invis-spec (make-symbol "notmuch-citation-region")))
532 (add-to-invisibility-spec invis-spec)
533 (overlay-put overlay 'invisible invis-spec)
536 (concat "[" (number-to-string (count-lines beg-sub (point)))
537 "-line citation.]")))
538 (goto-char (- beg-sub 1))
539 (insert (concat "\n" indent))
540 (insert-button cite-button-text
541 'invisibility-spec invis-spec
542 :type 'notmuch-button-citation-toggle-type)
544 (goto-char (+ (length cite-button-text) p))
546 (move-to-column depth)
547 (if (looking-at notmuch-show-signature-regexp)
548 (let ((sig-lines (- (count-lines beg-sub end) 1)))
549 (if (<= sig-lines notmuch-show-signature-lines-max)
551 (let ((invis-spec (make-symbol "notmuch-signature-region")))
552 (add-to-invisibility-spec invis-spec)
553 (overlay-put (make-overlay beg-sub end)
554 'invisible invis-spec)
556 (goto-char (- beg-sub 1))
557 (insert (concat "\n" indent))
558 (let ((sig-button-text (concat "[" (number-to-string sig-lines)
559 "-line signature.]")))
560 (insert-button sig-button-text 'invisibility-spec invis-spec
561 :type 'notmuch-button-signature-toggle-type)
567 (defun notmuch-show-markup-part (beg end depth mime-message)
568 (if (re-search-forward notmuch-show-part-begin-regexp nil t)
570 (if (eq mime-message nil)
571 (let ((filename (notmuch-show-get-filename)))
573 (insert-file-contents filename nil nil nil t)
574 (setq mime-message (mm-dissect-buffer)))))
576 (let ((part-beg (point-marker)))
577 (re-search-forward notmuch-show-part-end-regexp)
579 (let ((part-end (copy-marker (match-beginning 0))))
583 (indent-rigidly part-beg part-end depth)
588 (let ((handle-type (mm-handle-type mime-message))
590 (if (sequencep (car handle-type))
591 (setq mime-type (car handle-type))
592 (setq mime-type (car (car (cdr handle-type))))
594 (if (equal mime-type "text/html")
595 (mm-display-part mime-message))))
597 (notmuch-show-markup-citations-region part-beg part-end depth)
598 ; Advance to the next part (if any) (so the outer loop can
599 ; determine whether we've left the current message.
600 (if (re-search-forward notmuch-show-part-begin-regexp nil t)
601 (beginning-of-line)))))
605 (defun notmuch-show-markup-parts-region (beg end depth)
609 (while (< (point) end)
611 (notmuch-show-markup-part
612 beg end depth mime-message))))))
614 (defun notmuch-show-markup-body (depth btn)
615 (re-search-forward notmuch-show-body-begin-regexp)
617 (let ((beg (point-marker)))
618 (re-search-forward notmuch-show-body-end-regexp)
619 (let ((end (copy-marker (match-beginning 0))))
620 (notmuch-show-markup-parts-region beg end depth)
621 (let ((invis-spec (make-symbol "notmuch-show-body-read")))
622 (overlay-put (make-overlay beg end)
623 'invisible invis-spec)
624 (button-put btn 'invisibility-spec invis-spec)
625 (if (not (notmuch-show-message-unread-p))
626 (add-to-invisibility-spec invis-spec)))
631 (defun notmuch-show-markup-header (depth)
632 (re-search-forward notmuch-show-header-begin-regexp)
634 (let ((beg (point-marker))
637 ; Inverse video for subject
638 (overlay-put (make-overlay beg (point)) 'face '(:inverse-video t))
639 (setq btn (make-button beg (point) :type 'notmuch-button-body-toggle-type))
642 (let ((beg-hidden (point-marker)))
643 (re-search-forward notmuch-show-header-end-regexp)
645 (let ((end (point-marker)))
648 (while (looking-at "[A-Za-z][-A-Za-z0-9]*:")
650 (overlay-put (make-overlay (point) (re-search-forward ":"))
654 (indent-rigidly beg end depth)
655 (let ((invis-spec (make-symbol "notmuch-show-header")))
656 (add-to-invisibility-spec (cons invis-spec t))
657 (overlay-put (make-overlay beg-hidden end)
658 'invisible invis-spec)
661 (make-button (line-beginning-position) (line-end-position)
662 'invisibility-spec (cons invis-spec t)
663 :type 'notmuch-button-headers-toggle-type))
667 (set-marker beg-hidden nil)
672 (defun notmuch-show-markup-message ()
673 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
675 (re-search-forward notmuch-show-depth-regexp)
676 (let ((depth (string-to-number (buffer-substring (match-beginning 1) (match-end 1))))
678 (setq btn (notmuch-show-markup-header depth))
679 (notmuch-show-markup-body depth btn)))
680 (goto-char (point-max))))
682 (defun notmuch-show-hide-markers ()
684 (goto-char (point-min))
686 (if (re-search-forward notmuch-show-marker-regexp nil t)
688 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
689 'invisible 'notmuch-show-marker))
690 (goto-char (point-max))))))
692 (defun notmuch-show-markup-messages ()
694 (goto-char (point-min))
696 (notmuch-show-markup-message)))
697 (notmuch-show-hide-markers))
700 (defun notmuch-show-mode ()
701 "Major mode for viewing a thread with notmuch.
703 This buffer contains the results of the \"notmuch show\" command
704 for displaying a single thread of email from your email archives.
706 By default, various components of email messages, (citations,
707 signatures, already-read messages), are invisible to help you
708 focus on the most important things, (new text from unread
709 messages). See the various commands below for toggling the
710 visibility of hidden components.
712 The `notmuch-show-next-message' and
713 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
714 default), allow you to navigate to the next and previous
715 messages. Each time you navigate away from a message with
716 `notmuch-show-next-message' the current message will have its
717 \"unread\" tag removed.
719 You can add or remove tags from the current message with '+' and
720 '-'. You can also archive all messages in the current
721 view, (remove the \"inbox\" tag from each), with
722 `notmuch-show-archive-thread' (bound to 'a' by default).
724 \\{notmuch-show-mode-map}"
726 (kill-all-local-variables)
727 (add-to-invisibility-spec 'notmuch-show-marker)
728 (use-local-map notmuch-show-mode-map)
729 (setq major-mode 'notmuch-show-mode
730 mode-name "notmuch-show")
731 (setq buffer-read-only t))
735 (defgroup notmuch nil
736 "Notmuch mail reader for Emacs."
739 (defcustom notmuch-show-hook nil
740 "List of functions to call when notmuch displays a message."
742 :options '(goto-address)
745 (defcustom notmuch-search-hook nil
746 "List of functions to call when notmuch displays the search results."
748 :options '(hl-line-mode)
751 ; Make show mode a bit prettier, highlighting URLs and using word wrap
753 (defun notmuch-show-pretty-hook ()
754 (goto-address-mode 1)
757 (add-hook 'notmuch-show-hook 'notmuch-show-pretty-hook)
758 (add-hook 'notmuch-search-hook
762 (defun notmuch-show (thread-id &optional parent-buffer)
763 "Run \"notmuch show\" with the given thread ID and display results.
765 The optional PARENT-BUFFER is the notmuch-search buffer from
766 which this notmuch-show command was executed, (so that the next
767 thread from that buffer can be show when done with this one)."
768 (interactive "sNotmuch show: ")
769 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
770 (switch-to-buffer buffer)
772 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
773 (let ((proc (get-buffer-process (current-buffer)))
774 (inhibit-read-only t))
776 (error "notmuch search process already running for query `%s'" thread-id)
779 (goto-char (point-min))
781 (call-process notmuch-command nil t nil "show" thread-id)
782 (notmuch-show-markup-messages)
784 (run-hooks 'notmuch-show-hook)
785 ; Move straight to the first unread message
786 (if (not (notmuch-show-message-unread-p))
788 (notmuch-show-next-unread-message)
789 ; But if there are no unread messages, go back to the
790 ; beginning of the buffer, and open up the bodies of all
792 (if (not (notmuch-show-message-unread-p))
794 (goto-char (point-min))
795 (let ((btn (forward-button 1)))
797 (if (button-has-type-p btn 'notmuch-button-body-toggle-type)
800 (setq btn (forward-button 1))
801 (error (setq btn nil)))
803 (beginning-of-buffer)
807 (defvar notmuch-search-authors-width 40
808 "Number of columns to use to display authors in a notmuch-search buffer.")
810 (defvar notmuch-search-mode-map
811 (let ((map (make-sparse-keymap)))
812 (define-key map "a" 'notmuch-search-archive-thread)
813 (define-key map "b" 'notmuch-search-scroll-down)
814 (define-key map "f" 'notmuch-search-filter)
815 (define-key map "m" 'message-mail)
816 (define-key map "n" 'next-line)
817 (define-key map "o" 'notmuch-search-toggle-order)
818 (define-key map "p" 'previous-line)
819 (define-key map "q" 'kill-this-buffer)
820 (define-key map "r" 'notmuch-search-reply-to-thread)
821 (define-key map "s" 'notmuch-search)
822 (define-key map "t" 'notmuch-search-filter-by-tag)
823 (define-key map "x" 'kill-this-buffer)
824 (define-key map (kbd "RET") 'notmuch-search-show-thread)
825 (define-key map [mouse-1] 'notmuch-search-show-thread)
826 (define-key map "+" 'notmuch-search-add-tag)
827 (define-key map "-" 'notmuch-search-remove-tag)
828 (define-key map "*" 'notmuch-search-operate-all)
829 (define-key map "<" 'beginning-of-buffer)
830 (define-key map ">" 'notmuch-search-goto-last-thread)
831 (define-key map "=" 'notmuch-search-refresh-view)
832 (define-key map "\M->" 'notmuch-search-goto-last-thread)
833 (define-key map " " 'notmuch-search-scroll-up)
834 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
835 (define-key map "?" 'describe-mode)
837 "Keymap for \"notmuch search\" buffers.")
838 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
840 (defvar notmuch-search-query-string)
841 (defvar notmuch-search-oldest-first t
842 "Show the oldest mail first in the search-mode")
845 (defun notmuch-search-scroll-up ()
846 "Scroll up, moving point to last message in thread if at end."
850 ((end-of-buffer) (notmuch-search-goto-last-thread))))
852 (defun notmuch-search-scroll-down ()
853 "Scroll down, moving point to first message in thread if at beginning."
855 ; I don't know why scroll-down doesn't signal beginning-of-buffer
856 ; the way that scroll-up signals end-of-buffer, but c'est la vie.
858 ; So instead of trapping a signal we instead check whether the
859 ; window begins on the first line of the buffer and if so, move
860 ; directly to that position. (We have to count lines since the
861 ; window-start position is not the same as point-min due to the
862 ; invisible thread-ID characters on the first line.
863 (if (equal (count-lines (point-min) (window-start)) 1)
864 (goto-char (window-start))
867 (defun notmuch-search-goto-last-thread ()
868 "Move point to the last thread in the buffer."
870 (goto-char (point-max))
874 (defun notmuch-search-mode ()
875 "Major mode for searching mail with notmuch.
877 This buffer contains the results of a \"notmuch search\" of your
878 email archives. Each line in the buffer represents a single
879 thread giving a relative date for the thread and a subject.
881 Pressing RET on any line displays that thread. The '+' and '-'
882 keys can be used to add or remove tags from a thread. The 'a' key
883 is a convenience key for archiving a thread (removing the
886 Other useful commands are `notmuch-search-filter' for filtering
887 the current search based on an additional query string,
888 `notmuch-search-filter-by-tag' for filtering to include only
889 messages with a given tag, and `notmuch-search' to execute a new,
892 \\{notmuch-search-mode-map}"
894 (kill-all-local-variables)
895 (make-local-variable 'notmuch-search-query-string)
896 (make-local-variable 'notmuch-search-oldest-first)
897 (set (make-local-variable 'scroll-preserve-screen-position) t)
898 (add-to-invisibility-spec 'notmuch-search)
899 (use-local-map notmuch-search-mode-map)
900 (setq truncate-lines t)
901 (setq major-mode 'notmuch-search-mode
902 mode-name "notmuch-search")
903 (setq buffer-read-only t))
905 (defun notmuch-search-find-thread-id ()
906 "Return the thread for the current thread"
907 (get-text-property (point) 'notmuch-search-thread-id))
909 (defun notmuch-search-show-thread ()
911 (let ((thread-id (notmuch-search-find-thread-id)))
912 (if (> (length thread-id) 0)
913 (notmuch-show thread-id (current-buffer))
914 (error "End of search results"))))
916 (defun notmuch-search-reply-to-thread ()
917 "Begin composing a reply to the entire current thread in a new buffer."
919 (let ((message-id (notmuch-search-find-thread-id)))
920 (notmuch-reply message-id)))
922 (defun notmuch-call-notmuch-process (&rest args)
923 "Synchronously invoke \"notmuch\" with the given list of arguments.
925 Output from the process will be presented to the user as an error
926 and will also appear in a buffer named \"*Notmuch errors*\"."
927 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
928 (with-current-buffer error-buffer
930 (if (eq (apply 'call-process notmuch-command nil error-buffer nil args) 0)
933 (with-current-buffer error-buffer
934 (let ((beg (point-min))
935 (end (- (point-max) 1)))
936 (error (buffer-substring beg end))
939 (defun notmuch-search-set-tags (tags)
942 (re-search-backward "(")
945 (inhibit-read-only t))
946 (re-search-forward ")")
949 (delete-region beg end)
950 (insert (mapconcat 'identity tags " "))))))
952 (defun notmuch-search-get-tags ()
955 (re-search-backward "(")
956 (let ((beg (+ (point) 1)))
957 (re-search-forward ")")
958 (let ((end (- (point) 1)))
959 (split-string (buffer-substring beg end))))))
961 (defun notmuch-search-add-tag (tag)
962 "Add a tag to messages in the current thread matching the
965 (list (notmuch-select-tag-with-completion "Tag to add: ")))
966 (notmuch-call-notmuch-process "tag" (concat "+" tag) (notmuch-search-find-thread-id) " and " notmuch-search-query-string)
967 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
969 (defun notmuch-search-remove-tag (tag)
970 "Remove a tag from messages in the current thread matching the
973 (list (notmuch-select-tag-with-completion "Tag to remove: " (notmuch-search-find-thread-id))))
974 (notmuch-call-notmuch-process "tag" (concat "-" tag) (notmuch-search-find-thread-id) " and " notmuch-search-query-string)
975 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
977 (defun notmuch-search-archive-thread ()
978 "Archive the current thread (remove its \"inbox\" tag).
980 This function advances point to the next line when finished."
982 (notmuch-search-remove-tag "inbox")
985 (defun notmuch-search-process-sentinel (proc msg)
986 "Add a message to let user know when \"notmuch search\" exits"
987 (let ((buffer (process-buffer proc))
988 (status (process-status proc))
989 (exit-status (process-exit-status proc)))
990 (if (memq status '(exit signal))
991 (if (buffer-live-p buffer)
992 (with-current-buffer buffer
994 (let ((inhibit-read-only t))
995 (goto-char (point-max))
996 (if (eq status 'signal)
997 (insert "Incomplete search results (search process was killed).\n"))
998 (if (eq status 'exit)
1000 (insert "End of search results.")
1001 (if (not (= exit-status 0))
1002 (insert (format " (process returned %d)" exit-status)))
1003 (insert "\n"))))))))))
1005 (defun notmuch-search-process-filter (proc string)
1006 "Process and filter the output of \"notmuch search\""
1007 (let ((buffer (process-buffer proc)))
1008 (if (buffer-live-p buffer)
1009 (with-current-buffer buffer
1013 (inhibit-read-only t))
1015 (if (string-match "^\\(thread:[0-9A-Fa-f]*\\) \\(.*\\) \\(\\[[0-9/]*\\]\\) \\([^:]*\\); \\(.*\\) (\\([^()]*\\))$" string line)
1016 (let* ((thread-id (match-string 1 string))
1017 (date (match-string 2 string))
1018 (count (match-string 3 string))
1019 (authors (match-string 4 string))
1020 (authors-length (length authors))
1021 (subject (match-string 5 string))
1022 (tags (match-string 6 string)))
1023 (if (> authors-length 40)
1024 (set 'authors (concat (substring authors 0 (- 40 3)) "...")))
1025 (goto-char (point-max))
1026 (let ((beg (point-marker)))
1027 (insert (format "%s %-7s %-40s %s (%s)\n" date count authors subject tags))
1028 (put-text-property beg (point-marker) 'notmuch-search-thread-id thread-id))
1029 (set 'line (match-end 0)))
1030 (set 'more nil))))))
1031 (delete-process proc))))
1033 (defun notmuch-search-operate-all (action)
1034 "Operate on all messages matching the current query. Any
1035 number of whitespace separated actions can be given. Each action
1036 must have one of the two forms
1038 +tagname Add the tag `tagname'
1039 -tagname Remove the tag `tagname'
1041 Each character of the tag name may consist of alphanumeric
1042 characters as well as `_.+-'.
1044 (interactive "sOperation (+add -drop): notmuch tag ")
1045 (let ((action-split (split-string action " +")))
1046 ;; Perform some validation
1047 (let ((words action-split))
1048 (when (null words) (error "No operation given"))
1050 (unless (string-match-p "^[\+\-][_\+\-\\w]+$" (car words))
1051 (error "Action must be of the form `+thistag -that_tag'"))
1052 (setq words (cdr words))))
1053 (apply 'notmuch-call-notmuch-process "tag"
1054 (append action-split (list notmuch-search-query-string) nil))))
1056 (defun notmuch-search (query &optional oldest-first)
1057 "Run \"notmuch search\" with the given query string and display results."
1058 (interactive "sNotmuch search: ")
1059 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
1060 (switch-to-buffer buffer)
1061 (notmuch-search-mode)
1062 (set 'notmuch-search-query-string query)
1063 (set 'notmuch-search-oldest-first oldest-first)
1064 (let ((proc (get-buffer-process (current-buffer)))
1065 (inhibit-read-only t))
1067 (error "notmuch search process already running for query `%s'" query)
1070 (goto-char (point-min))
1072 (let ((proc (start-process-shell-command
1073 "notmuch-search" buffer notmuch-command "search"
1074 (if oldest-first "--sort=oldest-first" "--sort=newest-first")
1075 (shell-quote-argument query))))
1076 (set-process-sentinel proc 'notmuch-search-process-sentinel)
1077 (set-process-filter proc 'notmuch-search-process-filter))))
1078 (run-hooks 'notmuch-search-hook)))
1080 (defun notmuch-search-refresh-view ()
1081 "Refresh the current view.
1083 Kills the current buffer and runs a new search with the same
1084 query string as the current search. If the current thread is in
1085 the new search results, then point will be placed on the same
1086 thread. Otherwise, point will be moved to attempt to be in the
1087 same relative position within the new buffer."
1089 (let ((here (point))
1090 (oldest-first notmuch-search-oldest-first)
1091 (thread (notmuch-search-find-thread-id))
1092 (query notmuch-search-query-string))
1094 (notmuch-search query oldest-first)
1095 (goto-char (point-min))
1096 (if (re-search-forward (concat "^" thread) nil t)
1100 (defun notmuch-search-toggle-order ()
1101 "Toggle the current search order.
1103 By default, the \"inbox\" view created by `notmuch' is displayed
1104 in chronological order (oldest thread at the beginning of the
1105 buffer), while any global searches created by `notmuch-search'
1106 are displayed in reverse-chronological order (newest thread at
1107 the beginning of the buffer).
1109 This command toggles the sort order for the current search.
1111 Note that any filtered searches created by
1112 `notmuch-search-filter' retain the search order of the parent
1115 (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
1116 (notmuch-search-refresh-view))
1118 (defun notmuch-search-filter (query)
1119 "Filter the current search results based on an additional query string.
1121 Runs a new search matching only messages that match both the
1122 current search results AND the additional query string provided."
1123 (interactive "sFilter search: ")
1124 (notmuch-search (concat notmuch-search-query-string " and " query) notmuch-search-oldest-first))
1126 (defun notmuch-search-filter-by-tag (tag)
1127 "Filter the current search results based on a single tag.
1129 Runs a new search matching only messages that match both the
1130 current search results AND that are tagged with the given tag."
1132 (list (notmuch-select-tag-with-completion "Filter by tag: ")))
1133 (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
1136 "Run notmuch to display all mail with tag of 'inbox'"
1138 (notmuch-search "tag:inbox" notmuch-search-oldest-first))
1140 (setq mail-user-agent 'message-user-agent)
1142 (defvar notmuch-folder-mode-map
1143 (let ((map (make-sparse-keymap)))
1144 (define-key map "n" 'next-line)
1145 (define-key map "p" 'previous-line)
1146 (define-key map "x" 'kill-this-buffer)
1147 (define-key map "q" 'kill-this-buffer)
1148 (define-key map "s" 'notmuch-search)
1149 (define-key map (kbd "RET") 'notmuch-folder-show-search)
1150 (define-key map "<" 'beginning-of-buffer)
1151 (define-key map "=" 'notmuch-folder)
1152 (define-key map "?" 'describe-mode)
1153 (define-key map [mouse-1] 'notmuch-folder-show-search)
1155 "Keymap for \"notmuch folder\" buffers.")
1157 (fset 'notmuch-folder-mode-map notmuch-folder-mode-map)
1159 (defcustom notmuch-folders (quote (("inbox" . "tag:inbox") ("unread" . "tag:unread")))
1160 "List of searches for the notmuch folder view"
1161 :type '(alist :key-type (string) :value-type (string))
1164 (defun notmuch-folder-mode ()
1165 "Major mode for showing notmuch 'folders'.
1167 This buffer contains a list of messages counts returned by a
1168 customizable set of searches of your email archives. Each line
1169 in the buffer shows the search terms and the resulting message count.
1171 Pressing RET on any line opens a search window containing the
1172 results for the search terms in that line.
1174 \\{notmuch-folder-mode-map}"
1176 (kill-all-local-variables)
1177 (use-local-map 'notmuch-folder-mode-map)
1178 (setq truncate-lines t)
1180 (setq major-mode 'notmuch-folder-mode
1181 mode-name "notmuch-folder")
1182 (setq buffer-read-only t))
1184 (defun notmuch-folder-add (folders)
1186 (let ((name (car (car folders)))
1187 (inhibit-read-only t)
1188 (search (cdr (car folders))))
1191 (call-process notmuch-command nil t nil "count" search)
1192 (notmuch-folder-add (cdr folders)))))
1194 (defun notmuch-folder-find-name ()
1197 (let ((beg (point)))
1199 (filter-buffer-substring beg (point)))))
1201 (defun notmuch-folder-show-search (&optional folder)
1202 "Show a search window for the search related to the specified folder."
1205 (setq folder (notmuch-folder-find-name)))
1206 (let ((search (assoc folder notmuch-folders)))
1208 (notmuch-search (cdr search) notmuch-search-oldest-first))))
1210 (defun notmuch-folder ()
1211 "Show the notmuch folder view and update the displayed counts."
1213 (let ((buffer (get-buffer-create "*notmuch-folders*")))
1214 (switch-to-buffer buffer)
1215 (let ((inhibit-read-only t)
1216 (n (line-number-at-pos)))
1218 (notmuch-folder-mode)
1219 (notmuch-folder-add notmuch-folders)
1220 (goto-char (point-min))