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 "s" 'notmuch-search)
74 (define-key map "v" 'notmuch-show-view-all-mime-parts)
75 (define-key map "w" 'notmuch-show-view-raw-message)
76 (define-key map "x" 'kill-this-buffer)
77 (define-key map "+" 'notmuch-show-add-tag)
78 (define-key map "-" 'notmuch-show-remove-tag)
79 (define-key map (kbd "DEL") 'notmuch-show-rewind)
80 (define-key map " " 'notmuch-show-advance-marking-read-and-archiving)
81 (define-key map "|" 'notmuch-show-pipe-message)
82 (define-key map "?" 'describe-mode)
83 (define-key map (kbd "TAB") 'notmuch-show-next-button)
84 (define-key map (kbd "M-TAB") 'notmuch-show-previous-button)
86 "Keymap for \"notmuch show\" buffers.")
87 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
89 (defvar notmuch-show-signature-regexp "\\(-- ?\\|_+\\)$"
90 "Pattern to match a line that separates content from signature.
92 The regexp can (and should) include $ to match the end of the
93 line, but should not include ^ to match the beginning of the
94 line. This is because notmuch may have inserted additional space
95 for indentation at the beginning of the line. But notmuch will
96 move past the indentation when testing this pattern, (so that the
97 pattern can still test against the entire line).")
99 (defvar notmuch-show-signature-lines-max 12
100 "Maximum length of signature that will be hidden by default.")
102 (defvar notmuch-command "notmuch"
103 "Command to run the notmuch binary.")
105 (defvar notmuch-show-message-begin-regexp "\fmessage{")
106 (defvar notmuch-show-message-end-regexp "\fmessage}")
107 (defvar notmuch-show-header-begin-regexp "\fheader{")
108 (defvar notmuch-show-header-end-regexp "\fheader}")
109 (defvar notmuch-show-body-begin-regexp "\fbody{")
110 (defvar notmuch-show-body-end-regexp "\fbody}")
111 (defvar notmuch-show-attachment-begin-regexp "\fattachment{")
112 (defvar notmuch-show-attachment-end-regexp "\fattachment}")
113 (defvar notmuch-show-part-begin-regexp "\fpart{")
114 (defvar notmuch-show-part-end-regexp "\fpart}")
115 (defvar notmuch-show-marker-regexp "\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
117 (defvar notmuch-show-id-regexp "\\(id:[^ ]*\\)")
118 (defvar notmuch-show-depth-regexp " depth:\\([0-9]*\\) ")
119 (defvar notmuch-show-filename-regexp "filename:\\(.*\\)$")
120 (defvar notmuch-show-tags-regexp "(\\([^)]*\\))$")
122 (defvar notmuch-show-parent-buffer nil)
123 (defvar notmuch-show-body-read-visible nil)
124 (defvar notmuch-show-citations-visible nil)
125 (defvar notmuch-show-signatures-visible nil)
126 (defvar notmuch-show-headers-visible nil)
128 ; XXX: This should be a generic function in emacs somewhere, not here
129 (defun point-invisible-p ()
130 "Return whether the character at point is invisible.
132 Here visibility is determined by `buffer-invisibility-spec' and
133 the invisible property of any overlays for point. It doesn't have
134 anything to do with whether point is currently being displayed
135 within the current window."
136 (let ((prop (get-char-property (point) 'invisible)))
137 (if (eq buffer-invisibility-spec t)
139 (or (memq prop buffer-invisibility-spec)
140 (assq prop buffer-invisibility-spec)))))
142 (defun notmuch-select-tag-with-completion (prompt &rest search-terms)
144 (with-output-to-string
145 (with-current-buffer standard-output
146 (apply 'call-process notmuch-command nil t nil "search-tags" search-terms)))))
147 (completing-read prompt (split-string tag-list "\n+" t) nil nil nil)))
149 (defun notmuch-show-next-line ()
150 "Like builtin `next-line' but ensuring we end on a visible character.
152 By advancing forward until reaching a visible character.
154 Unlike builtin `next-line' this version accepts no arguments."
156 (set 'this-command 'next-line)
157 (call-interactively 'next-line)
158 (while (point-invisible-p)
161 (defun notmuch-show-previous-line ()
162 "Like builtin `previous-line' but ensuring we end on a visible character.
164 By advancing forward until reaching a visible character.
166 Unlike builtin `next-line' this version accepts no arguments."
168 (set 'this-command 'previous-line)
169 (call-interactively 'previous-line)
170 (while (point-invisible-p)
173 (defun notmuch-show-get-message-id ()
176 (if (not (looking-at notmuch-show-message-begin-regexp))
177 (re-search-backward notmuch-show-message-begin-regexp))
178 (re-search-forward notmuch-show-id-regexp)
179 (buffer-substring-no-properties (match-beginning 1) (match-end 1))))
181 (defun notmuch-show-get-filename ()
184 (if (not (looking-at notmuch-show-message-begin-regexp))
185 (re-search-backward notmuch-show-message-begin-regexp))
186 (re-search-forward notmuch-show-filename-regexp)
187 (buffer-substring-no-properties (match-beginning 1) (match-end 1))))
189 (defun notmuch-show-set-tags (tags)
192 (if (not (looking-at notmuch-show-message-begin-regexp))
193 (re-search-backward notmuch-show-message-begin-regexp))
194 (re-search-forward notmuch-show-tags-regexp)
195 (let ((inhibit-read-only t)
196 (beg (match-beginning 1))
198 (delete-region beg end)
200 (insert (mapconcat 'identity tags " ")))))
202 (defun notmuch-show-get-tags ()
205 (if (not (looking-at notmuch-show-message-begin-regexp))
206 (re-search-backward notmuch-show-message-begin-regexp))
207 (re-search-forward notmuch-show-tags-regexp)
208 (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
210 (defun notmuch-show-add-tag (&rest toadd)
211 "Add a tag to the current message."
213 (list (notmuch-select-tag-with-completion "Tag to add: ")))
214 (apply 'notmuch-call-notmuch-process
216 (mapcar (lambda (s) (concat "+" s)) toadd))
217 (cons (notmuch-show-get-message-id) nil)))
218 (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
220 (defun notmuch-show-remove-tag (&rest toremove)
221 "Remove a tag from the current message."
223 (list (notmuch-select-tag-with-completion "Tag to remove: " (notmuch-show-get-message-id))))
224 (let ((tags (notmuch-show-get-tags)))
225 (if (intersection tags toremove :test 'string=)
227 (apply 'notmuch-call-notmuch-process
229 (mapcar (lambda (s) (concat "-" s)) toremove))
230 (cons (notmuch-show-get-message-id) nil)))
231 (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
233 (defun notmuch-show-archive-thread-maybe-mark-read (markread)
235 (goto-char (point-min))
238 (notmuch-show-remove-tag "unread" "inbox")
239 (notmuch-show-remove-tag "inbox"))
242 (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
243 (goto-char (point-max)))))
244 (let ((parent-buffer notmuch-show-parent-buffer))
248 (switch-to-buffer parent-buffer)
250 (notmuch-search-show-thread)))))
252 (defun notmuch-show-mark-read-then-archive-thread ()
253 "Remove \"unread\" tag from each message, then archive and show next thread.
255 Archive each message currently shown by removing the \"unread\"
256 and \"inbox\" tag from each. Then kill this buffer and show the
257 next thread from the search from which this thread was originally
260 Note: This command is safe from any race condition of new messages
261 being delivered to the same thread. It does not archive the
262 entire thread, but only the messages shown in the current
265 (notmuch-show-archive-thread-maybe-mark-read t))
267 (defun notmuch-show-archive-thread ()
268 "Archive each message in thread, and show next thread from search.
270 Archive each message currently shown by removing the \"inbox\"
271 tag from each. Then kill this buffer and show the next thread
272 from the search from which this thread was originally shown.
274 Note: This command is safe from any race condition of new messages
275 being delivered to the same thread. It does not archive the
276 entire thread, but only the messages shown in the current
279 (notmuch-show-archive-thread-maybe-mark-read nil))
281 (defun notmuch-show-view-raw-message ()
282 "View the raw email of the current message."
284 (view-file (notmuch-show-get-filename)))
286 (defun notmuch-show-view-all-mime-parts ()
287 "Use external viewers (according to mailcap) to view all MIME-encoded parts."
290 (let ((filename (notmuch-show-get-filename)))
291 (switch-to-buffer (generate-new-buffer (concat "*notmuch-mime-"
294 (insert-file-contents filename nil nil nil t)
295 (mm-display-parts (mm-dissect-buffer))
296 (kill-this-buffer))))
298 (defun notmuch-reply (query-string)
299 (switch-to-buffer (generate-new-buffer "notmuch-draft"))
300 (call-process notmuch-command nil t nil "reply" query-string)
301 (message-insert-signature)
302 (goto-char (point-min))
303 (if (re-search-forward "^$" nil t)
305 (insert "--text follows this line--")
309 (defun notmuch-show-reply ()
310 "Begin composing a reply to the current message in a new buffer."
312 (let ((message-id (notmuch-show-get-message-id)))
313 (notmuch-reply message-id)))
315 (defun notmuch-show-pipe-message (command)
316 "Pipe the contents of the current message to the given command.
318 The given command will be executed with the raw contents of the
319 current email message as stdin. Anything printed by the command
320 to stdout or stderr will appear in the *Messages* buffer."
321 (interactive "sPipe message to command: ")
322 (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*"
323 (list command " < " (shell-quote-argument (notmuch-show-get-filename)))))
325 (defun notmuch-show-move-to-current-message-summary-line ()
326 "Move to the beginning of the one-line summary of the current message.
328 This gives us a stable place to move to and work from since the
329 summary line is always visible. This is important since moving to
330 an invisible location is unreliable, (the main command loop moves
331 point either forward or backward to the next visible character
332 when a command ends with point on an invisible character).
334 Emits an error if point is not within a valid message, (that is
335 not pattern of `notmuch-show-message-begin-regexp' could be found
336 by searching backward)."
338 (if (not (looking-at notmuch-show-message-begin-regexp))
339 (if (re-search-backward notmuch-show-message-begin-regexp nil t)
341 (error "Not within a valid message."))
344 (defun notmuch-show-last-message-p ()
345 "Predicate testing whether point is within the last message."
346 (save-window-excursion
348 (notmuch-show-move-to-current-message-summary-line)
349 (not (re-search-forward notmuch-show-message-begin-regexp nil t)))))
351 (defun notmuch-show-message-unread-p ()
352 "Preficate testing whether current message is unread."
353 (member "unread" (notmuch-show-get-tags)))
355 (defun notmuch-show-next-message ()
356 "Advance to the beginning of the next message in the buffer.
358 Moves to the last visible character of the current message if
359 already on the last message in the buffer."
361 (notmuch-show-move-to-current-message-summary-line)
362 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
363 (notmuch-show-move-to-current-message-summary-line)
364 (goto-char (- (point-max) 1))
365 (while (point-invisible-p)
369 (defun notmuch-show-find-next-message ()
370 "Returns the position of the next message in the buffer.
372 Or the position of the last visible character of the current
373 message if already within the last message in the buffer."
374 ; save-excursion doesn't save our window position
375 ; save-window-excursion doesn't save point
376 ; Looks like we have to use both.
378 (save-window-excursion
379 (notmuch-show-next-message)
382 (defun notmuch-show-next-unread-message ()
383 "Advance to the beginning of the next unread message in the buffer.
385 Moves to the last visible character of the current message if
386 there are no more unread messages past the current point."
387 (notmuch-show-next-message)
388 (while (and (not (notmuch-show-last-message-p))
389 (not (notmuch-show-message-unread-p)))
390 (notmuch-show-next-message))
391 (if (not (notmuch-show-message-unread-p))
392 (notmuch-show-next-message)))
394 (defun notmuch-show-next-open-message ()
395 "Advance to the next message which is not hidden.
397 If read messages are currently hidden, advance to the next unread
398 message. Otherwise, advance to the next message."
399 (if (or (memq 'notmuch-show-body-read buffer-invisibility-spec)
400 (assq 'notmuch-show-body-read buffer-invisibility-spec))
401 (notmuch-show-next-unread-message)
402 (notmuch-show-next-message)))
404 (defun notmuch-show-previous-message ()
405 "Backup to the beginning of the previous message in the buffer.
407 If within a message rather than at the beginning of it, then
408 simply move to the beginning of the current message."
410 (let ((start (point)))
411 (notmuch-show-move-to-current-message-summary-line)
412 (if (not (< (point) start))
413 ; Go backward twice to skip the current message's marker
415 (re-search-backward notmuch-show-message-begin-regexp nil t)
416 (re-search-backward notmuch-show-message-begin-regexp nil t)
417 (notmuch-show-move-to-current-message-summary-line)
421 (defun notmuch-show-find-previous-message ()
422 "Returns the position of the previous message in the buffer.
424 Or the position of the beginning of the current message if point
425 is originally within the message rather than at the beginning of
427 ; save-excursion doesn't save our window position
428 ; save-window-excursion doesn't save point
429 ; Looks like we have to use both.
431 (save-window-excursion
432 (notmuch-show-previous-message)
435 (defun notmuch-show-mark-read-then-next-open-message ()
436 "Remove unread tag from current message, then advance to next unread message."
438 (notmuch-show-remove-tag "unread")
439 (notmuch-show-next-open-message))
441 (defun notmuch-show-rewind ()
442 "Do reverse scrolling compared to `notmuch-show-advance-marking-read-and-archiving'
444 Specifically, if the beginning of the previous email is fewer
445 than `window-height' lines from the current point, move to it
446 just like `notmuch-show-previous-message'.
448 Otherwise, just scroll down a screenful of the current message.
450 This command does not modify any message tags, (it does not undo
451 any effects from previous calls to
452 `notmuch-show-advance-marking-read-and-archiving'."
454 (let ((previous (notmuch-show-find-previous-message)))
455 (if (> (count-lines previous (point)) (- (window-height) next-screen-context-lines))
459 ((beginning-of-buffer) nil))
460 (goto-char (window-start)))
461 (notmuch-show-previous-message))))
463 (defun notmuch-show-advance-marking-read-and-archiving ()
464 "Advance through buffer, marking read and archiving.
466 This command is intended to be one of the simplest ways to
467 process a thread of email. It does the following:
469 If the current message in the thread is not yet fully visible,
470 scroll by a near screenful to read more of the message.
472 Otherwise, (the end of the current message is already within the
473 current window), remove the \"unread\" tag (if present) from the
474 current message and advance to the next open message.
476 Finally, if there is no further message to advance to, and this
477 last message is already read, then archive the entire current
478 thread, (remove the \"inbox\" tag from each message). Also kill
479 this buffer, and display the next thread from the search from
480 which this thread was originally shown."
482 (let ((next (notmuch-show-find-next-message))
483 (unread (notmuch-show-message-unread-p)))
484 (if (> next (window-end))
486 (let ((last (notmuch-show-last-message-p)))
487 (notmuch-show-mark-read-then-next-open-message)
489 (notmuch-show-archive-thread))))))
491 (defun notmuch-show-next-button ()
492 "Advance point to the next button in the buffer."
494 (goto-char (button-start (next-button (point)))))
496 (defun notmuch-show-previous-button ()
497 "Move point back to the previous button in the buffer."
499 (goto-char (button-start (previous-button (point)))))
501 (defun notmuch-toggle-invisible-action (cite-button)
502 (let ((invis-spec (button-get button 'invisibility-spec)))
503 (if (invisible-p invis-spec)
504 (remove-from-invisibility-spec invis-spec)
505 (add-to-invisibility-spec invis-spec)
507 (force-window-update)
510 (define-button-type 'notmuch-button-invisibility-toggle-type 'action 'notmuch-toggle-invisible-action 'follow-link t)
511 (define-button-type 'notmuch-button-citation-toggle-type 'help-echo "mouse-1, RET: Show citation"
512 :supertype 'notmuch-button-invisibility-toggle-type)
513 (define-button-type 'notmuch-button-signature-toggle-type 'help-echo "mouse-1, RET: Show signature"
514 :supertype 'notmuch-button-invisibility-toggle-type)
515 (define-button-type 'notmuch-button-headers-toggle-type 'help-echo "mouse-1, RET: Show headers"
516 :supertype 'notmuch-button-invisibility-toggle-type)
517 (define-button-type 'notmuch-button-body-toggle-type 'help-echo "mouse-1, RET: Show message"
518 :supertype 'notmuch-button-invisibility-toggle-type)
520 (defun notmuch-show-markup-citations-region (beg end depth)
523 (while (< (point) end)
524 (let ((beg-sub (point-marker))
525 (indent (make-string depth ? ))
526 (citation "[[:space:]]*>"))
527 (if (looking-at citation)
529 (while (looking-at citation)
531 (let ((overlay (make-overlay beg-sub (point)))
532 (invis-spec (make-symbol "notmuch-citation-region")))
533 (add-to-invisibility-spec invis-spec)
534 (overlay-put overlay 'invisible invis-spec)
537 (concat "[" (number-to-string (count-lines beg-sub (point)))
538 "-line citation.]")))
539 (goto-char (- beg-sub 1))
540 (insert (concat "\n" indent))
541 (insert-button cite-button-text
542 'invisibility-spec invis-spec
543 :type 'notmuch-button-citation-toggle-type)
545 (goto-char (+ (length cite-button-text) p))
547 (move-to-column depth)
548 (if (looking-at notmuch-show-signature-regexp)
549 (let ((sig-lines (- (count-lines beg-sub end) 1)))
550 (if (<= sig-lines notmuch-show-signature-lines-max)
552 (let ((invis-spec (make-symbol "notmuch-signature-region")))
553 (add-to-invisibility-spec invis-spec)
554 (overlay-put (make-overlay beg-sub end)
555 'invisible invis-spec)
557 (goto-char (- beg-sub 1))
558 (insert (concat "\n" indent))
559 (let ((sig-button-text (concat "[" (number-to-string sig-lines)
560 "-line signature.]")))
561 (insert-button sig-button-text 'invisibility-spec invis-spec
562 :type 'notmuch-button-signature-toggle-type)
568 (defun notmuch-show-markup-part (beg end depth mime-message)
569 (if (re-search-forward notmuch-show-part-begin-regexp nil t)
571 (if (eq mime-message nil)
572 (let ((filename (notmuch-show-get-filename)))
574 (insert-file-contents filename nil nil nil t)
575 (setq mime-message (mm-dissect-buffer)))))
577 (let ((part-beg (point-marker)))
578 (re-search-forward notmuch-show-part-end-regexp)
580 (let ((part-end (copy-marker (match-beginning 0))))
584 (indent-rigidly part-beg part-end depth)
589 (let ((handle-type (mm-handle-type mime-message))
591 (if (sequencep (car handle-type))
592 (setq mime-type (car handle-type))
593 (setq mime-type (car (car (cdr handle-type))))
595 (if (equal mime-type "text/html")
596 (mm-display-part mime-message))))
598 (notmuch-show-markup-citations-region part-beg part-end depth)
599 ; Advance to the next part (if any) (so the outer loop can
600 ; determine whether we've left the current message.
601 (if (re-search-forward notmuch-show-part-begin-regexp nil t)
602 (beginning-of-line)))))
606 (defun notmuch-show-markup-parts-region (beg end depth)
610 (while (< (point) end)
612 (notmuch-show-markup-part
613 beg end depth mime-message))))))
615 (defun notmuch-show-markup-body (depth btn)
616 (re-search-forward notmuch-show-body-begin-regexp)
618 (let ((beg (point-marker)))
619 (re-search-forward notmuch-show-body-end-regexp)
620 (let ((end (copy-marker (match-beginning 0))))
621 (notmuch-show-markup-parts-region beg end depth)
622 (let ((invis-spec (make-symbol "notmuch-show-body-read")))
623 (overlay-put (make-overlay beg end)
624 'invisible invis-spec)
625 (button-put btn 'invisibility-spec invis-spec)
626 (if (not (notmuch-show-message-unread-p))
627 (add-to-invisibility-spec invis-spec)))
631 (defun notmuch-fontify-headers ()
633 (if (looking-at "[Tt]o:")
635 (overlay-put (make-overlay (point) (re-search-forward ":"))
636 'face 'message-header-name)
637 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
638 'face 'message-header-to))
639 (if (looking-at "[B]?[Cc][Cc]:")
641 (overlay-put (make-overlay (point) (re-search-forward ":"))
642 'face 'message-header-name)
643 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
644 'face 'message-header-cc))
645 (if (looking-at "[Ss]ubject:")
647 (overlay-put (make-overlay (point) (re-search-forward ":"))
648 'face 'message-header-name)
649 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
650 'face 'message-header-subject))
651 (if (looking-at "[Ff]rom:")
653 (overlay-put (make-overlay (point) (re-search-forward ":"))
654 'face 'message-header-name)
655 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
656 'face 'message-header-other))))))))
658 (defun notmuch-show-markup-header (depth)
659 (re-search-forward notmuch-show-header-begin-regexp)
661 (let ((beg (point-marker))
664 ; Inverse video for subject
665 (overlay-put (make-overlay beg (point)) 'face '(:inverse-video t))
666 (setq btn (make-button beg (point) :type 'notmuch-button-body-toggle-type))
669 (let ((beg-hidden (point-marker)))
670 (re-search-forward notmuch-show-header-end-regexp)
672 (let ((end (point-marker)))
675 (while (looking-at "[A-Za-z][-A-Za-z0-9]*:")
677 (notmuch-fontify-headers)
680 (indent-rigidly beg end depth)
681 (let ((invis-spec (make-symbol "notmuch-show-header")))
682 (add-to-invisibility-spec (cons invis-spec t))
683 (overlay-put (make-overlay beg-hidden end)
684 'invisible invis-spec)
687 (make-button (line-beginning-position) (line-end-position)
688 'invisibility-spec (cons invis-spec t)
689 :type 'notmuch-button-headers-toggle-type))
693 (set-marker beg-hidden nil)
698 (defun notmuch-show-markup-message ()
699 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
701 (re-search-forward notmuch-show-depth-regexp)
702 (let ((depth (string-to-number (buffer-substring (match-beginning 1) (match-end 1))))
704 (setq btn (notmuch-show-markup-header depth))
705 (notmuch-show-markup-body depth btn)))
706 (goto-char (point-max))))
708 (defun notmuch-show-hide-markers ()
710 (goto-char (point-min))
712 (if (re-search-forward notmuch-show-marker-regexp nil t)
714 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
715 'invisible 'notmuch-show-marker))
716 (goto-char (point-max))))))
718 (defun notmuch-show-markup-messages ()
720 (goto-char (point-min))
722 (notmuch-show-markup-message)))
723 (notmuch-show-hide-markers))
726 (defun notmuch-show-mode ()
727 "Major mode for viewing a thread with notmuch.
729 This buffer contains the results of the \"notmuch show\" command
730 for displaying a single thread of email from your email archives.
732 By default, various components of email messages, (citations,
733 signatures, already-read messages), are invisible to help you
734 focus on the most important things, (new text from unread
735 messages). See the various commands below for toggling the
736 visibility of hidden components.
738 The `notmuch-show-next-message' and
739 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
740 default), allow you to navigate to the next and previous
741 messages. Each time you navigate away from a message with
742 `notmuch-show-next-message' the current message will have its
743 \"unread\" tag removed.
745 You can add or remove tags from the current message with '+' and
746 '-'. You can also archive all messages in the current
747 view, (remove the \"inbox\" tag from each), with
748 `notmuch-show-archive-thread' (bound to 'a' by default).
750 \\{notmuch-show-mode-map}"
752 (kill-all-local-variables)
753 (add-to-invisibility-spec 'notmuch-show-marker)
754 (use-local-map notmuch-show-mode-map)
755 (setq major-mode 'notmuch-show-mode
756 mode-name "notmuch-show")
757 (setq buffer-read-only t))
761 (defgroup notmuch nil
762 "Notmuch mail reader for Emacs."
765 (defcustom notmuch-show-hook nil
766 "List of functions to call when notmuch displays a message."
768 :options '(goto-address)
771 (defcustom notmuch-search-hook nil
772 "List of functions to call when notmuch displays the search results."
774 :options '(hl-line-mode)
777 ; Make show mode a bit prettier, highlighting URLs and using word wrap
779 (defun notmuch-show-pretty-hook ()
780 (goto-address-mode 1)
783 (add-hook 'notmuch-show-hook 'notmuch-show-pretty-hook)
784 (add-hook 'notmuch-search-hook
788 (defun notmuch-show (thread-id &optional parent-buffer)
789 "Run \"notmuch show\" with the given thread ID and display results.
791 The optional PARENT-BUFFER is the notmuch-search buffer from
792 which this notmuch-show command was executed, (so that the next
793 thread from that buffer can be show when done with this one)."
794 (interactive "sNotmuch show: ")
795 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
796 (switch-to-buffer buffer)
798 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
799 (let ((proc (get-buffer-process (current-buffer)))
800 (inhibit-read-only t))
802 (error "notmuch search process already running for query `%s'" thread-id)
805 (goto-char (point-min))
807 (call-process notmuch-command nil t nil "show" thread-id)
808 (notmuch-show-markup-messages)
810 (run-hooks 'notmuch-show-hook)
811 ; Move straight to the first unread message
812 (if (not (notmuch-show-message-unread-p))
814 (notmuch-show-next-unread-message)
815 ; But if there are no unread messages, go back to the
816 ; beginning of the buffer, and open up the bodies of all
818 (if (not (notmuch-show-message-unread-p))
820 (goto-char (point-min))
821 (let ((btn (forward-button 1)))
823 (if (button-has-type-p btn 'notmuch-button-body-toggle-type)
826 (setq btn (forward-button 1))
827 (error (setq btn nil)))
829 (beginning-of-buffer)
833 (defvar notmuch-search-authors-width 40
834 "Number of columns to use to display authors in a notmuch-search buffer.")
836 (defvar notmuch-search-mode-map
837 (let ((map (make-sparse-keymap)))
838 (define-key map "a" 'notmuch-search-archive-thread)
839 (define-key map "b" 'notmuch-search-scroll-down)
840 (define-key map "f" 'notmuch-search-filter)
841 (define-key map "m" 'message-mail)
842 (define-key map "n" 'next-line)
843 (define-key map "o" 'notmuch-search-toggle-order)
844 (define-key map "p" 'previous-line)
845 (define-key map "q" 'kill-this-buffer)
846 (define-key map "r" 'notmuch-search-reply-to-thread)
847 (define-key map "s" 'notmuch-search)
848 (define-key map "t" 'notmuch-search-filter-by-tag)
849 (define-key map "x" 'kill-this-buffer)
850 (define-key map (kbd "RET") 'notmuch-search-show-thread)
851 (define-key map [mouse-1] 'notmuch-search-show-thread)
852 (define-key map "+" 'notmuch-search-add-tag)
853 (define-key map "-" 'notmuch-search-remove-tag)
854 (define-key map "*" 'notmuch-search-operate-all)
855 (define-key map "<" 'beginning-of-buffer)
856 (define-key map ">" 'notmuch-search-goto-last-thread)
857 (define-key map "=" 'notmuch-search-refresh-view)
858 (define-key map "\M->" 'notmuch-search-goto-last-thread)
859 (define-key map " " 'notmuch-search-scroll-up)
860 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
861 (define-key map "?" 'describe-mode)
863 "Keymap for \"notmuch search\" buffers.")
864 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
866 (defvar notmuch-search-query-string)
867 (defvar notmuch-search-oldest-first t
868 "Show the oldest mail first in the search-mode")
871 (defun notmuch-search-scroll-up ()
872 "Scroll up, moving point to last message in thread if at end."
876 ((end-of-buffer) (notmuch-search-goto-last-thread))))
878 (defun notmuch-search-scroll-down ()
879 "Scroll down, moving point to first message in thread if at beginning."
881 ; I don't know why scroll-down doesn't signal beginning-of-buffer
882 ; the way that scroll-up signals end-of-buffer, but c'est la vie.
884 ; So instead of trapping a signal we instead check whether the
885 ; window begins on the first line of the buffer and if so, move
886 ; directly to that position. (We have to count lines since the
887 ; window-start position is not the same as point-min due to the
888 ; invisible thread-ID characters on the first line.
889 (if (equal (count-lines (point-min) (window-start)) 1)
890 (goto-char (window-start))
893 (defun notmuch-search-goto-last-thread ()
894 "Move point to the last thread in the buffer."
896 (goto-char (point-max))
900 (defun notmuch-search-mode ()
901 "Major mode for searching mail with notmuch.
903 This buffer contains the results of a \"notmuch search\" of your
904 email archives. Each line in the buffer represents a single
905 thread giving a relative date for the thread and a subject.
907 Pressing RET on any line displays that thread. The '+' and '-'
908 keys can be used to add or remove tags from a thread. The 'a' key
909 is a convenience key for archiving a thread (removing the
912 Other useful commands are `notmuch-search-filter' for filtering
913 the current search based on an additional query string,
914 `notmuch-search-filter-by-tag' for filtering to include only
915 messages with a given tag, and `notmuch-search' to execute a new,
918 \\{notmuch-search-mode-map}"
920 (kill-all-local-variables)
921 (make-local-variable 'notmuch-search-query-string)
922 (make-local-variable 'notmuch-search-oldest-first)
923 (set (make-local-variable 'scroll-preserve-screen-position) t)
924 (add-to-invisibility-spec 'notmuch-search)
925 (use-local-map notmuch-search-mode-map)
926 (setq truncate-lines t)
927 (setq major-mode 'notmuch-search-mode
928 mode-name "notmuch-search")
929 (setq buffer-read-only t))
931 (defun notmuch-search-find-thread-id ()
932 "Return the thread for the current thread"
933 (get-text-property (point) 'notmuch-search-thread-id))
935 (defun notmuch-search-show-thread ()
937 (let ((thread-id (notmuch-search-find-thread-id)))
938 (if (> (length thread-id) 0)
939 (notmuch-show thread-id (current-buffer))
940 (error "End of search results"))))
942 (defun notmuch-search-reply-to-thread ()
943 "Begin composing a reply to the entire current thread in a new buffer."
945 (let ((message-id (notmuch-search-find-thread-id)))
946 (notmuch-reply message-id)))
948 (defun notmuch-call-notmuch-process (&rest args)
949 "Synchronously invoke \"notmuch\" with the given list of arguments.
951 Output from the process will be presented to the user as an error
952 and will also appear in a buffer named \"*Notmuch errors*\"."
953 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
954 (with-current-buffer error-buffer
956 (if (eq (apply 'call-process notmuch-command nil error-buffer nil args) 0)
959 (with-current-buffer error-buffer
960 (let ((beg (point-min))
961 (end (- (point-max) 1)))
962 (error (buffer-substring beg end))
965 (defun notmuch-search-set-tags (tags)
968 (re-search-backward "(")
971 (inhibit-read-only t))
972 (re-search-forward ")")
975 (delete-region beg end)
976 (insert (mapconcat 'identity tags " "))))))
978 (defun notmuch-search-get-tags ()
981 (re-search-backward "(")
982 (let ((beg (+ (point) 1)))
983 (re-search-forward ")")
984 (let ((end (- (point) 1)))
985 (split-string (buffer-substring beg end))))))
987 (defun notmuch-search-add-tag (tag)
988 "Add a tag to messages in the current thread matching the
991 (list (notmuch-select-tag-with-completion "Tag to add: ")))
992 (notmuch-call-notmuch-process "tag" (concat "+" tag) (notmuch-search-find-thread-id) " and " notmuch-search-query-string)
993 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
995 (defun notmuch-search-remove-tag (tag)
996 "Remove a tag from messages in the current thread matching the
999 (list (notmuch-select-tag-with-completion "Tag to remove: " (notmuch-search-find-thread-id))))
1000 (notmuch-call-notmuch-process "tag" (concat "-" tag) (notmuch-search-find-thread-id) " and " notmuch-search-query-string)
1001 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
1003 (defun notmuch-search-archive-thread ()
1004 "Archive the current thread (remove its \"inbox\" tag).
1006 This function advances point to the next line when finished."
1008 (notmuch-search-remove-tag "inbox")
1011 (defun notmuch-search-process-sentinel (proc msg)
1012 "Add a message to let user know when \"notmuch search\" exits"
1013 (let ((buffer (process-buffer proc))
1014 (status (process-status proc))
1015 (exit-status (process-exit-status proc)))
1016 (if (memq status '(exit signal))
1017 (if (buffer-live-p buffer)
1018 (with-current-buffer buffer
1020 (let ((inhibit-read-only t))
1021 (goto-char (point-max))
1022 (if (eq status 'signal)
1023 (insert "Incomplete search results (search process was killed).\n"))
1024 (if (eq status 'exit)
1026 (insert "End of search results.")
1027 (if (not (= exit-status 0))
1028 (insert (format " (process returned %d)" exit-status)))
1029 (insert "\n"))))))))))
1031 (defun notmuch-search-process-filter (proc string)
1032 "Process and filter the output of \"notmuch search\""
1033 (let ((buffer (process-buffer proc)))
1034 (if (buffer-live-p buffer)
1035 (with-current-buffer buffer
1039 (inhibit-read-only t))
1041 (if (string-match "^\\(thread:[0-9A-Fa-f]*\\) \\(.*\\) \\(\\[[0-9/]*\\]\\) \\([^:]*\\); \\(.*\\) (\\([^()]*\\))$" string line)
1042 (let* ((thread-id (match-string 1 string))
1043 (date (match-string 2 string))
1044 (count (match-string 3 string))
1045 (authors (match-string 4 string))
1046 (authors-length (length authors))
1047 (subject (match-string 5 string))
1048 (tags (match-string 6 string)))
1049 (if (> authors-length 40)
1050 (set 'authors (concat (substring authors 0 (- 40 3)) "...")))
1051 (goto-char (point-max))
1052 (let ((beg (point-marker)))
1053 (insert (format "%s %-7s %-40s %s (%s)\n" date count authors subject tags))
1054 (put-text-property beg (point-marker) 'notmuch-search-thread-id thread-id))
1055 (set 'line (match-end 0)))
1056 (set 'more nil))))))
1057 (delete-process proc))))
1059 (defun notmuch-search-operate-all (action)
1060 "Operate on all messages matching the current query. Any
1061 number of whitespace separated actions can be given. Each action
1062 must have one of the two forms
1064 +tagname Add the tag `tagname'
1065 -tagname Remove the tag `tagname'
1067 Each character of the tag name may consist of alphanumeric
1068 characters as well as `_.+-'.
1070 (interactive "sOperation (+add -drop): notmuch tag ")
1071 (let ((action-split (split-string action " +")))
1072 ;; Perform some validation
1073 (let ((words action-split))
1074 (when (null words) (error "No operation given"))
1076 (unless (string-match-p "^[\+\-][_\+\-\\w]+$" (car words))
1077 (error "Action must be of the form `+thistag -that_tag'"))
1078 (setq words (cdr words))))
1079 (apply 'notmuch-call-notmuch-process "tag"
1080 (append action-split (list notmuch-search-query-string) nil))))
1082 (defun notmuch-search (query &optional oldest-first)
1083 "Run \"notmuch search\" with the given query string and display results."
1084 (interactive "sNotmuch search: ")
1085 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
1086 (switch-to-buffer buffer)
1087 (notmuch-search-mode)
1088 (set 'notmuch-search-query-string query)
1089 (set 'notmuch-search-oldest-first oldest-first)
1090 (let ((proc (get-buffer-process (current-buffer)))
1091 (inhibit-read-only t))
1093 (error "notmuch search process already running for query `%s'" query)
1096 (goto-char (point-min))
1098 (let ((proc (start-process-shell-command
1099 "notmuch-search" buffer notmuch-command "search"
1100 (if oldest-first "--sort=oldest-first" "--sort=newest-first")
1101 (shell-quote-argument query))))
1102 (set-process-sentinel proc 'notmuch-search-process-sentinel)
1103 (set-process-filter proc 'notmuch-search-process-filter))))
1104 (run-hooks 'notmuch-search-hook)))
1106 (defun notmuch-search-refresh-view ()
1107 "Refresh the current view.
1109 Kills the current buffer and runs a new search with the same
1110 query string as the current search. If the current thread is in
1111 the new search results, then point will be placed on the same
1112 thread. Otherwise, point will be moved to attempt to be in the
1113 same relative position within the new buffer."
1115 (let ((here (point))
1116 (oldest-first notmuch-search-oldest-first)
1117 (thread (notmuch-search-find-thread-id))
1118 (query notmuch-search-query-string))
1120 (notmuch-search query oldest-first)
1121 (goto-char (point-min))
1122 (if (re-search-forward (concat "^" thread) nil t)
1126 (defun notmuch-search-toggle-order ()
1127 "Toggle the current search order.
1129 By default, the \"inbox\" view created by `notmuch' is displayed
1130 in chronological order (oldest thread at the beginning of the
1131 buffer), while any global searches created by `notmuch-search'
1132 are displayed in reverse-chronological order (newest thread at
1133 the beginning of the buffer).
1135 This command toggles the sort order for the current search.
1137 Note that any filtered searches created by
1138 `notmuch-search-filter' retain the search order of the parent
1141 (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
1142 (notmuch-search-refresh-view))
1144 (defun notmuch-search-filter (query)
1145 "Filter the current search results based on an additional query string.
1147 Runs a new search matching only messages that match both the
1148 current search results AND the additional query string provided."
1149 (interactive "sFilter search: ")
1150 (notmuch-search (concat notmuch-search-query-string " and " query) notmuch-search-oldest-first))
1152 (defun notmuch-search-filter-by-tag (tag)
1153 "Filter the current search results based on a single tag.
1155 Runs a new search matching only messages that match both the
1156 current search results AND that are tagged with the given tag."
1158 (list (notmuch-select-tag-with-completion "Filter by tag: ")))
1159 (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
1162 "Run notmuch to display all mail with tag of 'inbox'"
1164 (notmuch-search "tag:inbox" notmuch-search-oldest-first))
1166 (setq mail-user-agent 'message-user-agent)
1168 (defvar notmuch-folder-mode-map
1169 (let ((map (make-sparse-keymap)))
1170 (define-key map "n" 'next-line)
1171 (define-key map "p" 'previous-line)
1172 (define-key map "x" 'kill-this-buffer)
1173 (define-key map "q" 'kill-this-buffer)
1174 (define-key map "s" 'notmuch-search)
1175 (define-key map (kbd "RET") 'notmuch-folder-show-search)
1176 (define-key map "<" 'beginning-of-buffer)
1177 (define-key map "=" 'notmuch-folder)
1178 (define-key map "?" 'describe-mode)
1179 (define-key map [mouse-1] 'notmuch-folder-show-search)
1181 "Keymap for \"notmuch folder\" buffers.")
1183 (fset 'notmuch-folder-mode-map notmuch-folder-mode-map)
1185 (defcustom notmuch-folders (quote (("inbox" . "tag:inbox") ("unread" . "tag:unread")))
1186 "List of searches for the notmuch folder view"
1187 :type '(alist :key-type (string) :value-type (string))
1190 (defun notmuch-folder-mode ()
1191 "Major mode for showing notmuch 'folders'.
1193 This buffer contains a list of messages counts returned by a
1194 customizable set of searches of your email archives. Each line
1195 in the buffer shows the search terms and the resulting message count.
1197 Pressing RET on any line opens a search window containing the
1198 results for the search terms in that line.
1200 \\{notmuch-folder-mode-map}"
1202 (kill-all-local-variables)
1203 (use-local-map 'notmuch-folder-mode-map)
1204 (setq truncate-lines t)
1206 (setq major-mode 'notmuch-folder-mode
1207 mode-name "notmuch-folder")
1208 (setq buffer-read-only t))
1210 (defun notmuch-folder-add (folders)
1212 (let ((name (car (car folders)))
1213 (inhibit-read-only t)
1214 (search (cdr (car folders))))
1217 (call-process notmuch-command nil t nil "count" search)
1218 (notmuch-folder-add (cdr folders)))))
1220 (defun notmuch-folder-find-name ()
1223 (let ((beg (point)))
1225 (filter-buffer-substring beg (point)))))
1227 (defun notmuch-folder-show-search (&optional folder)
1228 "Show a search window for the search related to the specified folder."
1231 (setq folder (notmuch-folder-find-name)))
1232 (let ((search (assoc folder notmuch-folders)))
1234 (notmuch-search (cdr search) notmuch-search-oldest-first))))
1236 (defun notmuch-folder ()
1237 "Show the notmuch folder view and update the displayed counts."
1239 (let ((buffer (get-buffer-create "*notmuch-folders*")))
1240 (switch-to-buffer buffer)
1241 (let ((inhibit-read-only t)
1242 (n (line-number-at-pos)))
1244 (notmuch-folder-mode)
1245 (notmuch-folder-add notmuch-folders)
1246 (goto-char (point-min))