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 (defvar notmuch-show-mode-map
23 (let ((map (make-sparse-keymap)))
24 ; I don't actually want all of these toggle commands occupying
25 ; keybindings. They steal valuable key-binding space, are hard
26 ; to remember, and act globally rather than locally.
28 ; Will be much preferable to switch to direct manipulation for
29 ; toggling visibility of these components. Probably using
30 ; overlays-at to query and manipulate the current overlay.
31 (define-key map "a" 'notmuch-show-archive-thread)
32 (define-key map "b" 'notmuch-show-toggle-body-read-visible)
33 (define-key map "c" 'notmuch-show-toggle-citations-visible)
34 (define-key map "h" 'notmuch-show-toggle-headers-visible)
35 (define-key map "n" 'notmuch-show-next-message)
36 (define-key map "p" 'notmuch-show-previous-message)
37 (define-key map "q" 'kill-this-buffer)
38 (define-key map "s" 'notmuch-show-toggle-signatures-visible)
39 (define-key map "x" 'kill-this-buffer)
40 (define-key map "+" 'notmuch-show-add-tag)
41 (define-key map "-" 'notmuch-show-remove-tag)
42 (define-key map " " 'notmuch-show-advance-marking-read-and-archiving)
44 "Keymap for \"notmuch show\" buffers.")
45 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
47 (defvar notmuch-show-message-begin-regexp "
\fmessage{")
48 (defvar notmuch-show-message-end-regexp "
\fmessage}")
49 (defvar notmuch-show-header-begin-regexp "
\fheader{")
50 (defvar notmuch-show-header-end-regexp "
\fheader}")
51 (defvar notmuch-show-body-begin-regexp "
\fbody{")
52 (defvar notmuch-show-body-end-regexp "
\fbody}")
53 (defvar notmuch-show-attachment-begin-regexp "
\fattachment{")
54 (defvar notmuch-show-attachment-end-regexp "
\fattachment}")
55 (defvar notmuch-show-part-begin-regexp "
\fpart{")
56 (defvar notmuch-show-part-end-regexp "
\fpart}")
57 (defvar notmuch-show-marker-regexp "
\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
59 (defvar notmuch-show-id-regexp "ID: \\(.*\\)$")
60 (defvar notmuch-show-tags-regexp "(\\([^)]*\\))$")
62 (defun notmuch-show-get-message-id ()
65 (if (not (looking-at notmuch-show-message-begin-regexp))
66 (re-search-backward notmuch-show-message-begin-regexp))
67 (re-search-forward notmuch-show-id-regexp)
68 (buffer-substring (match-beginning 1) (match-end 1))))
70 (defun notmuch-show-set-tags (tags)
73 (if (not (looking-at notmuch-show-message-begin-regexp))
74 (re-search-backward notmuch-show-message-begin-regexp))
75 (re-search-forward notmuch-show-tags-regexp)
76 (let ((inhibit-read-only t)
77 (beg (match-beginning 1))
79 (delete-region beg end)
81 (insert (mapconcat 'identity tags " ")))))
83 (defun notmuch-show-get-tags ()
86 (if (not (looking-at notmuch-show-message-begin-regexp))
87 (re-search-backward notmuch-show-message-begin-regexp))
88 (re-search-forward notmuch-show-tags-regexp)
89 (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
91 (defun notmuch-show-add-tag (tag)
92 (interactive "sTag to add: ")
93 (notmuch-call-notmuch-process "tag" (concat "+" tag) (concat "id:" (notmuch-show-get-message-id)))
94 (notmuch-show-set-tags (delete-dups (sort (cons tag (notmuch-show-get-tags)) 'string<))))
96 (defun notmuch-show-remove-tag (tag)
97 (interactive "sTag to remove: ")
98 (let ((tags (notmuch-show-get-tags)))
101 (notmuch-call-notmuch-process "tag" (concat "-" tag) (concat "id:" (notmuch-show-get-message-id)))
102 (notmuch-show-set-tags (delete tag tags))))))
104 (defun notmuch-show-archive-thread ()
105 "Archive each message in thread, and show next thread from search.
107 Archive each message currrently shown by removing the \"inbox\"
108 tag from each. Then kill this buffer and show the next thread
109 from the search from which this thread was originally shown.
111 Note: This command is safe from any race condition of new messages
112 being delivered to the same thread. It does not archive the
113 entire thread, but only the messages shown in the current
117 (goto-char (point-min))
119 (notmuch-show-remove-tag "inbox")
122 (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
123 (goto-char (point-max)))))
124 (let ((parent-buffer notmuch-show-parent-buffer))
128 (switch-to-buffer parent-buffer)
129 (notmuch-search-show-thread)))))
131 (defun notmuch-show-move-to-current-message-summary-line ()
132 "Move to the beginning of the one-line summary of the current message.
134 This gives us a stable place to move to and work from since the
135 summary line is always visible. This is important since moving to
136 an invisible location is unreliable, (the main command loop moves
137 point either forward or backward to the next visible character
138 when a command ends with point on an invisible character).
140 Emits an error if point is not within a valid message, (that is
141 not pattern of `notmuch-show-message-begin-regexp' could be found
142 by searching backward)."
144 (if (not (looking-at notmuch-show-message-begin-regexp))
145 (if (re-search-backward notmuch-show-message-begin-regexp nil t)
147 (error "Not within a valid message."))
150 (defun notmuch-show-next-message ()
151 "Advance to the beginning of the next message in the buffer.
153 Moves to the end of the buffer if already on the last message in
156 (notmuch-show-move-to-current-message-summary-line)
157 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
158 (notmuch-show-move-to-current-message-summary-line)
159 (goto-char (point-max)))
162 (defun notmuch-show-find-next-message ()
163 "Returns the position of the next message in the buffer.
165 Or the end of the buffer if already within the last message in
167 ; save-excursion doesn't save our window position
168 ; save-window-excursion doesn't save point
169 ; Looks like we have to use both.
171 (save-window-excursion
172 (notmuch-show-next-message)
175 (defun notmuch-show-next-unread-message ()
176 "Advance to the beginning of the next unread message in the buffer.
178 Does nothing if there are no more unread messages past the
180 (while (and (not (eobp))
181 (not (member "unread" (notmuch-show-get-tags))))
182 (notmuch-show-next-message)))
184 (defun notmuch-show-find-next-unread-message ()
185 "Returns the position of the next message in the buffer.
187 Returns the current point if there are no more unread messages
188 past the current point."
189 ; save-excursion doesn't save our window position
190 ; save-window-excursion doesn't save point
191 ; Looks like we have to use both.
193 (save-window-excursion
194 (notmuch-show-next-unread-message)
197 (defun notmuch-show-previous-message ()
198 "Backup to the beginning of the previous message in the buffer.
200 If within a message rather than at the beginning of it, then
201 simply move to the beginning of the current message."
203 (let ((start (point)))
204 (notmuch-show-move-to-current-message-summary-line)
205 (if (not (< (point) start))
206 ; Go backward twice to skip the current message's marker
208 (re-search-backward notmuch-show-message-begin-regexp nil t)
209 (re-search-backward notmuch-show-message-begin-regexp nil t)
210 (notmuch-show-move-to-current-message-summary-line)
214 (defun notmuch-show-mark-read-then-next-unread-message ()
215 "Remove unread tag from current message, then advance to next unread message."
217 (notmuch-show-remove-tag "unread")
218 (notmuch-show-next-unread-message))
220 (defun notmuch-show-advance-marking-read-and-archiving ()
221 "Advance through buffer, marking read and archiving.
223 This command is intended to be one of the simplest ways to
224 process a thread of email. It does the following:
226 If the current message in the thread is not yet fully visible,
227 scroll by a near screenful to read more of the message.
229 Otherwise, (the end of the current message is already within the
230 current window), remove the \"unread\" tag from the current
231 message and advance to the next message.
233 Finally, if there is no further message to advance to, and this
234 last message is already read, then archive the entire current
235 thread, (remove the \"inbox\" tag from each message). Also kill
236 this buffer, and display the next thread from the search from
237 which this thread was originally shown."
239 (let ((next (notmuch-show-find-next-unread-message))
240 (unread (member "unread" (notmuch-show-get-tags))))
241 (if (and (not unread)
242 (equal next (point)))
243 (notmuch-show-archive-thread)
244 (if (and (> next (window-end))
245 (< next (point-max)))
247 (notmuch-show-mark-read-then-next-unread-message)))))
249 (defun notmuch-show-markup-citations-region (beg end)
252 (while (< (point) end)
253 (let ((beg-sub (point)))
256 (while (looking-at ">")
258 (let ((overlay (make-overlay beg-sub (point))))
259 (overlay-put overlay 'invisible 'notmuch-show-citation)
260 (overlay-put overlay 'before-string
261 (concat "[" (number-to-string (count-lines beg-sub (point)))
262 " quoted lines.]")))))
263 (if (looking-at "--[ ]?$")
264 (let ((overlay (make-overlay beg-sub end)))
265 (overlay-put overlay 'invisible 'notmuch-show-signature)
266 (overlay-put overlay 'before-string
267 (concat "[" (number-to-string (count-lines beg-sub (point)))
268 "-line signature.]"))
272 (defun notmuch-show-markup-body ()
273 (re-search-forward notmuch-show-body-begin-regexp)
277 (re-search-forward notmuch-show-body-end-regexp)
278 (let ((end (match-beginning 0)))
279 (notmuch-show-markup-citations-region beg end)
280 (if (not (member "unread" (notmuch-show-get-tags)))
281 (overlay-put (make-overlay beg end)
282 'invisible 'notmuch-show-body-read)))))
284 (defun notmuch-show-markup-header ()
285 (re-search-forward notmuch-show-header-begin-regexp)
289 (re-search-forward notmuch-show-header-end-regexp)
290 (overlay-put (make-overlay beg (match-beginning 0))
291 'invisible 'notmuch-show-header)))
293 (defun notmuch-show-markup-message ()
294 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
296 (notmuch-show-markup-header)
297 (notmuch-show-markup-body))
298 (goto-char (point-max))))
300 (defun notmuch-show-hide-markers ()
302 (goto-char (point-min))
304 (if (re-search-forward notmuch-show-marker-regexp nil t)
306 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
307 'invisible 'notmuch-show-marker))
308 (goto-char (point-max))))))
310 (defun notmuch-show-markup-messages ()
312 (goto-char (point-min))
314 (notmuch-show-markup-message)))
315 (notmuch-show-hide-markers))
317 (defun notmuch-show-toggle-citations-visible ()
318 "Toggle visibility of citations"
320 (if notmuch-show-citations-visible
321 (add-to-invisibility-spec 'notmuch-show-citation)
322 (remove-from-invisibility-spec 'notmuch-show-citation))
323 (set 'notmuch-show-citations-visible (not notmuch-show-citations-visible))
324 ; Need to force the redisplay for some reason
325 (force-window-update)
328 (defun notmuch-show-toggle-signatures-visible ()
329 "Toggle visibility of signatures"
331 (if notmuch-show-signatures-visible
332 (add-to-invisibility-spec 'notmuch-show-signature)
333 (remove-from-invisibility-spec 'notmuch-show-signature))
334 (set 'notmuch-show-signatures-visible (not notmuch-show-signatures-visible))
335 ; Need to force the redisplay for some reason
336 (force-window-update)
339 (defun notmuch-show-toggle-headers-visible ()
340 "Toggle visibility of header fields"
342 (if notmuch-show-headers-visible
343 (add-to-invisibility-spec 'notmuch-show-header)
344 (remove-from-invisibility-spec 'notmuch-show-header))
345 (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
346 ; Need to force the redisplay for some reason
347 (force-window-update)
350 (defun notmuch-show-toggle-body-read-visible ()
351 "Toggle visibility of message bodies of read messages"
353 (if notmuch-show-body-read-visible
354 (add-to-invisibility-spec 'notmuch-show-body-read)
355 (remove-from-invisibility-spec 'notmuch-show-body-read))
356 (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
357 ; Need to force the redisplay for some reason
358 (force-window-update)
362 (defun notmuch-show-mode ()
363 "Major mode for viewing a thread with notmuch.
365 This buffer contains the results of the \"notmuch show\" command
366 for displaying a single thread of email from your email archives.
368 By default, various components of email messages, (citations,
369 signatures, already-read messages), are invisible to help you
370 focus on the most important things, (new text from unread
371 messages). See the various commands below for toggling the
372 visibility of hidden components.
374 The `notmuch-show-next-message' and
375 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
376 default), allow you to navigate to the next and previous
377 messages. Each time you navigate away from a message with
378 `notmuch-show-next-message' the current message will have its
379 \"unread\" tag removed.
381 You can add or remove tags from the current message with '+' and
382 '-'. You can also archive all messages in the current
383 view, (remove the \"inbox\" tag from each), with
384 `notmuch-show-archive-thread' (bound to 'a' by default).
386 \\{notmuch-show-mode-map}"
388 (kill-all-local-variables)
389 (set (make-local-variable 'notmuch-show-headers-visible) t)
390 (notmuch-show-toggle-headers-visible)
391 (set (make-local-variable 'notmuch-show-body-read-visible) t)
392 (notmuch-show-toggle-body-read-visible)
393 (set (make-local-variable 'notmuch-show-citations-visible) t)
394 (notmuch-show-toggle-citations-visible)
395 (set (make-local-variable 'notmuch-show-signatures-visible) t)
396 (notmuch-show-toggle-signatures-visible)
397 (add-to-invisibility-spec 'notmuch-show-marker)
398 (use-local-map notmuch-show-mode-map)
399 (setq major-mode 'notmuch-show-mode
400 mode-name "notmuch-show")
401 (setq buffer-read-only t))
403 (defun notmuch-show (thread-id &optional parent-buffer)
404 "Run \"notmuch show\" with the given thread ID and display results.
406 The optional PARENT-BUFFER is the notmuch-search buffer from
407 which this notmuch-show command was executed, (so that the next
408 thread from that buffer can be show when done with this one)."
409 (interactive "sNotmuch show: ")
410 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
411 (switch-to-buffer buffer)
413 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
414 (let ((proc (get-buffer-process (current-buffer)))
415 (inhibit-read-only t))
417 (error "notmuch search process already running for query `%s'" query)
420 (goto-char (point-min))
422 (call-process "notmuch" nil t nil "show" thread-id)
423 (notmuch-show-markup-messages)
425 (notmuch-show-next-unread-message)
428 (defvar notmuch-search-mode-map
429 (let ((map (make-sparse-keymap)))
430 (define-key map "a" 'notmuch-search-archive-thread)
431 (define-key map "b" 'scroll-down)
432 (define-key map "f" 'notmuch-search-filter)
433 (define-key map "n" 'next-line)
434 (define-key map "p" 'previous-line)
435 (define-key map "q" 'kill-this-buffer)
436 (define-key map "s" 'notmuch-search)
437 (define-key map "t" 'notmuch-search-filter-by-tag)
438 (define-key map "x" 'kill-this-buffer)
439 (define-key map (kbd "RET") 'notmuch-search-show-thread)
440 (define-key map "+" 'notmuch-search-add-tag)
441 (define-key map "-" 'notmuch-search-remove-tag)
442 (define-key map "<" 'beginning-of-buffer)
443 (define-key map ">" 'notmuch-search-goto-last-thread)
444 (define-key map "=" 'notmuch-search-refresh-view)
445 (define-key map "\M->" 'notmuch-search-goto-last-thread)
446 (define-key map " " 'scroll-up)
447 (define-key map (kbd "<DEL>") 'scroll-down)
449 "Keymap for \"notmuch search\" buffers.")
450 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
452 (defun notmuch-search-goto-last-thread (&optional arg)
453 "Move point to the last thread in the buffer."
459 (defun notmuch-search-mode ()
460 "Major mode for searching mail with notmuch.
462 This buffer contains the results of a \"notmuch search\" of your
463 email archives. Each line in the buffer represents a single
464 thread giving a relative date for the thread and a subject.
466 Pressing RET on any line displays that thread. The '+' and '-'
467 keys can be used to add or remove tags from a thread. The 'a' key
468 is a convenience key for archiving a thread (removing the
471 Other useful commands are `notmuch-search-filter' for filtering
472 the current search based on an additional query string,
473 `notmuch-search-filter-by-tag' for filtering to include only
474 messages with a given tag, and `notmuch-search' to execute a new,
477 \\{notmuch-search-mode-map}"
479 (kill-all-local-variables)
480 (make-local-variable 'notmuch-search-query-string)
481 (set (make-local-variable 'scroll-preserve-screen-position) t)
482 (add-to-invisibility-spec 'notmuch-search)
483 (use-local-map notmuch-search-mode-map)
484 (setq major-mode 'notmuch-search-mode
485 mode-name "notmuch-search")
486 (setq buffer-read-only t))
488 (defun notmuch-search-find-thread-id ()
492 (re-search-forward "[a-fA-F0-9]*")
493 (filter-buffer-substring beg (point)))))
495 (defun notmuch-search-markup-this-thread-id ()
498 (re-search-forward "[a-fA-F0-9]*")
500 (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)))
502 (defun notmuch-search-markup-thread-ids ()
504 (goto-char (point-min))
506 (notmuch-search-markup-this-thread-id)
509 (defun notmuch-search-hide-thread-ids ()
511 (add-to-invisibility-spec 'notmuch-search)
512 (force-window-update)
515 (defun notmuch-search-show-thread-ids ()
517 (remove-from-invisibility-spec 'notmuch-search)
518 (force-window-update)
521 (defun notmuch-search-show-thread ()
523 (let ((thread-id (notmuch-search-find-thread-id)))
525 (if (> (length thread-id) 0)
526 (notmuch-show thread-id (current-buffer))
527 (error "End of search results"))))
529 (defun notmuch-call-notmuch-process (&rest args)
530 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
531 (with-current-buffer error-buffer
533 (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
536 (with-current-buffer error-buffer
537 (let ((beg (point-min))
538 (end (- (point-max) 1)))
539 (error (buffer-substring beg end))
542 (defun notmuch-search-set-tags (tags)
545 (re-search-backward "(")
548 (inhibit-read-only t))
549 (re-search-forward ")")
552 (delete-region beg end)
553 (insert (mapconcat 'identity tags " "))))))
555 (defun notmuch-search-get-tags ()
558 (re-search-backward "(")
559 (let ((beg (+ (point) 1)))
560 (re-search-forward ")")
561 (let ((end (- (point) 1)))
562 (split-string (buffer-substring beg end))))))
564 (defun notmuch-search-add-tag (tag)
565 (interactive "sTag to add: ")
566 (notmuch-call-notmuch-process "tag" (concat "+" tag) (concat "thread:" (notmuch-search-find-thread-id)))
567 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
569 (defun notmuch-search-remove-tag (tag)
570 (interactive "sTag to remove: ")
571 (notmuch-call-notmuch-process "tag" (concat "-" tag) (concat "thread:" (notmuch-search-find-thread-id)))
572 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
574 (defun notmuch-search-archive-thread ()
575 "Archive the current thread (remove its \"inbox\" tag).
577 This function advances point to the next line when finished."
579 (notmuch-search-remove-tag "inbox")
582 (defun notmuch-search (query)
583 "Run \"notmuch search\" with the given query string and display results."
584 (interactive "sNotmuch search: ")
585 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
586 (switch-to-buffer buffer)
587 (notmuch-search-mode)
588 (set 'notmuch-search-query-string query)
589 (let ((proc (get-buffer-process (current-buffer)))
590 (inhibit-read-only t))
592 (error "notmuch search process already running for query `%s'" query)
595 (goto-char (point-min))
597 (call-process "notmuch" nil t nil "search" query)
598 (notmuch-search-markup-thread-ids)
601 (defun notmuch-search-refresh-view ()
602 "Refresh the current view.
604 Kills the current buffer and runs a new search with the same
605 query string as the current search. If the current thread is in
606 the new search results, then point will be placed on the same
607 thread. Otherwise, point will be moved to attempt to be in the
608 same relative position within the new buffer."
611 (thread (notmuch-search-find-thread-id))
612 (query notmuch-search-query-string))
614 (notmuch-search query)
615 (goto-char (point-min))
616 (if (re-search-forward (concat "^" thread) nil t)
620 (defun notmuch-search-filter (query)
621 "Filter the current search results based on an additional query string.
623 Runs a new search matching only messages that match both the
624 current search results AND the additional query string provided."
625 (interactive "sFilter search: ")
626 (notmuch-search (concat notmuch-search-query-string " and " query)))
628 (defun notmuch-search-filter-by-tag (tag)
629 "Filter the current search results based on a single tag.
631 Runs a new search matching only messages that match both the
632 current search results AND that are tagged with the given tag."
633 (interactive "sFilter by tag: ")
634 (notmuch-search (concat notmuch-search-query-string " and tag:" tag)))
637 "Run notmuch to display all mail with tag of 'inbox'"
639 (notmuch-search "tag:inbox"))