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-mark-read-then-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 beginning of the current message if already within the last
166 message in the buffer."
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-previous-message ()
185 "Backup to the beginning of the previous message in the buffer.
187 If within a message rather than at the beginning of it, then
188 simply move to the beginning of the current message."
190 (let ((start (point)))
191 (notmuch-show-move-to-current-message-summary-line)
192 (if (not (< (point) start))
193 ; Go backward twice to skip the current message's marker
195 (re-search-backward notmuch-show-message-begin-regexp nil t)
196 (re-search-backward notmuch-show-message-begin-regexp nil t)
197 (notmuch-show-move-to-current-message-summary-line)
201 (defun notmuch-show-mark-read-then-next-message ()
202 "Remove unread tag from current message, then advance to next message."
204 (notmuch-show-remove-tag "unread")
205 (notmuch-show-next-message))
207 (defun notmuch-show-advance-marking-read-and-archiving ()
208 "Advance through buffer, marking read and archiving.
210 This command is intended to be one of the simplest ways to
211 process a thread of email. It does the following:
213 If the current message in the thread is not yet fully visible,
214 scroll by a near screenful to read more of the message.
216 Otherwise, (the end of the current message is already within the
217 current window), remove the \"unread\" tag from the current
218 message and advance to the next message.
220 Finally, if there is no further message to advance to, and this
221 last message is already read, then archive the entire current
222 thread, (remove the \"inbox\" tag from each message). Also kill
223 this buffer, and display the next thread from the search from
224 which this thread was originally shown."
226 (let ((next (notmuch-show-find-next-message))
227 (unread (member "unread" (notmuch-show-get-tags))))
228 (if (and (not unread)
229 (equal next (point)))
230 (notmuch-show-archive-thread)
231 (if (and (> next (window-end))
232 (< next (point-max)))
234 (notmuch-show-mark-read-then-next-message)))))
236 (defun notmuch-show-markup-citations-region (beg end)
239 (while (< (point) end)
240 (let ((beg-sub (point)))
243 (while (looking-at ">")
245 (let ((overlay (make-overlay beg-sub (point))))
246 (overlay-put overlay 'invisible 'notmuch-show-citation)
247 (overlay-put overlay 'before-string
248 (concat "[" (number-to-string (count-lines beg-sub (point)))
249 " quoted lines.]")))))
250 (if (looking-at "--[ ]?$")
251 (let ((overlay (make-overlay beg-sub end)))
252 (overlay-put overlay 'invisible 'notmuch-show-signature)
253 (overlay-put overlay 'before-string
254 (concat "[" (number-to-string (count-lines beg-sub (point)))
255 "-line signature.]"))
259 (defun notmuch-show-markup-body ()
260 (re-search-forward notmuch-show-body-begin-regexp)
264 (re-search-forward notmuch-show-body-end-regexp)
265 (let ((end (match-beginning 0)))
266 (notmuch-show-markup-citations-region beg end)
267 (if (not (member "unread" (notmuch-show-get-tags)))
268 (overlay-put (make-overlay beg end)
269 'invisible 'notmuch-show-body-read)))))
271 (defun notmuch-show-markup-header ()
272 (re-search-forward notmuch-show-header-begin-regexp)
276 (re-search-forward notmuch-show-header-end-regexp)
277 (overlay-put (make-overlay beg (match-beginning 0))
278 'invisible 'notmuch-show-header)))
280 (defun notmuch-show-markup-message ()
281 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
283 (notmuch-show-markup-header)
284 (notmuch-show-markup-body))
285 (goto-char (point-max))))
287 (defun notmuch-show-hide-markers ()
289 (goto-char (point-min))
291 (if (re-search-forward notmuch-show-marker-regexp nil t)
293 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
294 'invisible 'notmuch-show-marker))
295 (goto-char (point-max))))))
297 (defun notmuch-show-markup-messages ()
299 (goto-char (point-min))
301 (notmuch-show-markup-message)))
302 (notmuch-show-hide-markers))
304 (defun notmuch-show-toggle-citations-visible ()
305 "Toggle visibility of citations"
307 (if notmuch-show-citations-visible
308 (add-to-invisibility-spec 'notmuch-show-citation)
309 (remove-from-invisibility-spec 'notmuch-show-citation))
310 (set 'notmuch-show-citations-visible (not notmuch-show-citations-visible))
311 ; Need to force the redisplay for some reason
312 (force-window-update)
315 (defun notmuch-show-toggle-signatures-visible ()
316 "Toggle visibility of signatures"
318 (if notmuch-show-signatures-visible
319 (add-to-invisibility-spec 'notmuch-show-signature)
320 (remove-from-invisibility-spec 'notmuch-show-signature))
321 (set 'notmuch-show-signatures-visible (not notmuch-show-signatures-visible))
322 ; Need to force the redisplay for some reason
323 (force-window-update)
326 (defun notmuch-show-toggle-headers-visible ()
327 "Toggle visibility of header fields"
329 (if notmuch-show-headers-visible
330 (add-to-invisibility-spec 'notmuch-show-header)
331 (remove-from-invisibility-spec 'notmuch-show-header))
332 (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
333 ; Need to force the redisplay for some reason
334 (force-window-update)
337 (defun notmuch-show-toggle-body-read-visible ()
338 "Toggle visibility of message bodies of read messages"
340 (if notmuch-show-body-read-visible
341 (add-to-invisibility-spec 'notmuch-show-body-read)
342 (remove-from-invisibility-spec 'notmuch-show-body-read))
343 (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
344 ; Need to force the redisplay for some reason
345 (force-window-update)
349 (defun notmuch-show-mode ()
350 "Major mode for viewing a thread with notmuch.
352 This buffer contains the results of the \"notmuch show\" command
353 for displaying a single thread of email from your email archives.
355 By default, various components of email messages, (citations,
356 signatures, already-read messages), are invisible to help you
357 focus on the most important things, (new text from unread
358 messages). See the various commands below for toggling the
359 visibility of hidden components.
361 The `notmuch-show-next-message' and
362 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
363 default), allow you to navigate to the next and previous
364 messages. Each time you navigate away from a message with
365 `notmuch-show-next-message' the current message will have its
366 \"unread\" tag removed.
368 You can add or remove tags from the current message with '+' and
369 '-'. You can also archive all messages in the current
370 view, (remove the \"inbox\" tag from each), with
371 `notmuch-show-archive-thread' (bound to 'a' by default).
373 \\{notmuch-show-mode-map}"
375 (kill-all-local-variables)
376 (set (make-local-variable 'notmuch-show-headers-visible) t)
377 (notmuch-show-toggle-headers-visible)
378 (set (make-local-variable 'notmuch-show-body-read-visible) t)
379 (notmuch-show-toggle-body-read-visible)
380 (set (make-local-variable 'notmuch-show-citations-visible) t)
381 (notmuch-show-toggle-citations-visible)
382 (set (make-local-variable 'notmuch-show-signatures-visible) t)
383 (notmuch-show-toggle-signatures-visible)
384 (add-to-invisibility-spec 'notmuch-show-marker)
385 (use-local-map notmuch-show-mode-map)
386 (setq major-mode 'notmuch-show-mode
387 mode-name "notmuch-show")
388 (setq buffer-read-only t))
390 (defun notmuch-show (thread-id &optional parent-buffer)
391 "Run \"notmuch show\" with the given thread ID and display results.
393 The optional PARENT-BUFFER is the notmuch-search buffer from
394 which this notmuch-show command was executed, (so that the next
395 thread from that buffer can be show when done with this one)."
396 (interactive "sNotmuch show: ")
397 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
398 (switch-to-buffer buffer)
400 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
401 (let ((proc (get-buffer-process (current-buffer)))
402 (inhibit-read-only t))
404 (error "notmuch search process already running for query `%s'" query)
407 (goto-char (point-min))
409 (call-process "notmuch" nil t nil "show" thread-id)
410 (notmuch-show-markup-messages)
412 (notmuch-show-next-unread-message)
415 (defvar notmuch-search-mode-map
416 (let ((map (make-sparse-keymap)))
417 (define-key map "a" 'notmuch-search-archive-thread)
418 (define-key map "b" 'scroll-down)
419 (define-key map "f" 'notmuch-search-filter)
420 (define-key map "n" 'next-line)
421 (define-key map "p" 'previous-line)
422 (define-key map "q" 'kill-this-buffer)
423 (define-key map "s" 'notmuch-search)
424 (define-key map "t" 'notmuch-search-filter-by-tag)
425 (define-key map "x" 'kill-this-buffer)
426 (define-key map (kbd "RET") 'notmuch-search-show-thread)
427 (define-key map "+" 'notmuch-search-add-tag)
428 (define-key map "-" 'notmuch-search-remove-tag)
429 (define-key map "<" 'beginning-of-buffer)
430 (define-key map ">" 'notmuch-search-goto-last-thread)
431 (define-key map "=" 'notmuch-search-refresh-view)
432 (define-key map "\M->" 'notmuch-search-goto-last-thread)
433 (define-key map " " 'scroll-up)
434 (define-key map (kbd "<DEL>") 'scroll-down)
436 "Keymap for \"notmuch search\" buffers.")
437 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
439 (defun notmuch-search-goto-last-thread (&optional arg)
440 "Move point to the last thread in the buffer."
446 (defun notmuch-search-mode ()
447 "Major mode for searching mail with notmuch.
449 This buffer contains the results of a \"notmuch search\" of your
450 email archives. Each line in the buffer represents a single
451 thread giving a relative date for the thread and a subject.
453 Pressing RET on any line displays that thread. The '+' and '-'
454 keys can be used to add or remove tags from a thread. The 'a' key
455 is a convenience key for archiving a thread (removing the
458 Other useful commands are `notmuch-search-filter' for filtering
459 the current search based on an additional query string,
460 `notmuch-search-filter-by-tag' for filtering to include only
461 messages with a given tag, and `notmuch-search' to execute a new,
464 \\{notmuch-search-mode-map}"
466 (kill-all-local-variables)
467 (make-local-variable 'notmuch-search-query-string)
468 (set (make-local-variable 'scroll-preserve-screen-position) t)
469 (add-to-invisibility-spec 'notmuch-search)
470 (use-local-map notmuch-search-mode-map)
471 (setq major-mode 'notmuch-search-mode
472 mode-name "notmuch-search")
473 (setq buffer-read-only t))
475 (defun notmuch-search-find-thread-id ()
479 (re-search-forward "[a-fA-F0-9]*")
480 (filter-buffer-substring beg (point)))))
482 (defun notmuch-search-markup-this-thread-id ()
485 (re-search-forward "[a-fA-F0-9]*")
487 (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)))
489 (defun notmuch-search-markup-thread-ids ()
491 (goto-char (point-min))
493 (notmuch-search-markup-this-thread-id)
496 (defun notmuch-search-hide-thread-ids ()
498 (add-to-invisibility-spec 'notmuch-search)
499 (force-window-update)
502 (defun notmuch-search-show-thread-ids ()
504 (remove-from-invisibility-spec 'notmuch-search)
505 (force-window-update)
508 (defun notmuch-search-show-thread ()
510 (let ((thread-id (notmuch-search-find-thread-id)))
512 (if (> (length thread-id) 0)
513 (notmuch-show thread-id (current-buffer))
514 (error "End of search results"))))
516 (defun notmuch-call-notmuch-process (&rest args)
517 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
518 (with-current-buffer error-buffer
520 (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
523 (with-current-buffer error-buffer
524 (let ((beg (point-min))
525 (end (- (point-max) 1)))
526 (error (buffer-substring beg end))
529 (defun notmuch-search-set-tags (tags)
532 (re-search-backward "(")
535 (inhibit-read-only t))
536 (re-search-forward ")")
539 (delete-region beg end)
540 (insert (mapconcat 'identity tags " "))))))
542 (defun notmuch-search-get-tags ()
545 (re-search-backward "(")
546 (let ((beg (+ (point) 1)))
547 (re-search-forward ")")
548 (let ((end (- (point) 1)))
549 (split-string (buffer-substring beg end))))))
551 (defun notmuch-search-add-tag (tag)
552 (interactive "sTag to add: ")
553 (notmuch-call-notmuch-process "tag" (concat "+" tag) (concat "thread:" (notmuch-search-find-thread-id)))
554 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
556 (defun notmuch-search-remove-tag (tag)
557 (interactive "sTag to remove: ")
558 (notmuch-call-notmuch-process "tag" (concat "-" tag) (concat "thread:" (notmuch-search-find-thread-id)))
559 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
561 (defun notmuch-search-archive-thread ()
562 "Archive the current thread (remove its \"inbox\" tag).
564 This function advances point to the next line when finished."
566 (notmuch-search-remove-tag "inbox")
569 (defun notmuch-search (query)
570 "Run \"notmuch search\" with the given query string and display results."
571 (interactive "sNotmuch search: ")
572 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
573 (switch-to-buffer buffer)
574 (notmuch-search-mode)
575 (set 'notmuch-search-query-string query)
576 (let ((proc (get-buffer-process (current-buffer)))
577 (inhibit-read-only t))
579 (error "notmuch search process already running for query `%s'" query)
582 (goto-char (point-min))
584 (call-process "notmuch" nil t nil "search" query)
585 (notmuch-search-markup-thread-ids)
588 (defun notmuch-search-refresh-view ()
589 "Refresh the current view.
591 Kills the current buffer and runs a new search with the same
592 query string as the current search. If the current thread is in
593 the new search results, then point will be placed on the same
594 thread. Otherwise, point will be moved to attempt to be in the
595 same relative position within the new buffer."
598 (thread (notmuch-search-find-thread-id))
599 (query notmuch-search-query-string))
601 (notmuch-search query)
602 (goto-char (point-min))
603 (if (re-search-forward (concat "^" thread) nil t)
607 (defun notmuch-search-filter (query)
608 "Filter the current search results based on an additional query string.
610 Runs a new search matching only messages that match both the
611 current search results AND the additional query string provided."
612 (interactive "sFilter search: ")
613 (notmuch-search (concat notmuch-search-query-string " and " query)))
615 (defun notmuch-search-filter-by-tag (tag)
616 "Filter the current search results based on a single tag.
618 Runs a new search matching only messages that match both the
619 current search results AND that are tagged with the given tag."
620 (interactive "sFilter by tag: ")
621 (notmuch-search (concat notmuch-search-query-string " and tag:" tag)))
624 "Run notmuch to display all mail with tag of 'inbox'"
626 (notmuch-search "tag:inbox"))