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)
43 "Keymap for \"notmuch show\" buffers.")
44 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
46 (defvar notmuch-show-message-begin-regexp "
\fmessage{")
47 (defvar notmuch-show-message-end-regexp "
\fmessage}")
48 (defvar notmuch-show-header-begin-regexp "
\fheader{")
49 (defvar notmuch-show-header-end-regexp "
\fheader}")
50 (defvar notmuch-show-body-begin-regexp "
\fbody{")
51 (defvar notmuch-show-body-end-regexp "
\fbody}")
52 (defvar notmuch-show-attachment-begin-regexp "
\fattachment{")
53 (defvar notmuch-show-attachment-end-regexp "
\fattachment}")
54 (defvar notmuch-show-part-begin-regexp "
\fpart{")
55 (defvar notmuch-show-part-end-regexp "
\fpart}")
56 (defvar notmuch-show-marker-regexp "
\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
58 (defvar notmuch-show-id-regexp "ID: \\(.*\\)$")
59 (defvar notmuch-show-tags-regexp "(\\([^)]*\\))$")
61 (defun notmuch-show-get-message-id ()
64 (if (not (looking-at notmuch-show-message-begin-regexp))
65 (re-search-backward notmuch-show-message-begin-regexp))
66 (re-search-forward notmuch-show-id-regexp)
67 (buffer-substring (match-beginning 1) (match-end 1))))
69 (defun notmuch-show-set-tags (tags)
72 (if (not (looking-at notmuch-show-message-begin-regexp))
73 (re-search-backward notmuch-show-message-begin-regexp))
74 (re-search-forward notmuch-show-tags-regexp)
75 (let ((inhibit-read-only t)
76 (beg (match-beginning 1))
78 (delete-region beg end)
80 (insert (mapconcat 'identity tags " ")))))
82 (defun notmuch-show-get-tags ()
85 (if (not (looking-at notmuch-show-message-begin-regexp))
86 (re-search-backward notmuch-show-message-begin-regexp))
87 (re-search-forward notmuch-show-tags-regexp)
88 (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
90 (defun notmuch-show-add-tag (tag)
91 (interactive "sTag to add: ")
92 (notmuch-call-notmuch-process "tag" (concat "+" tag) (concat "id:" (notmuch-show-get-message-id)))
93 (notmuch-show-set-tags (delete-dups (sort (cons tag (notmuch-show-get-tags)) 'string<))))
95 (defun notmuch-show-remove-tag (tag)
96 (interactive "sTag to remove: ")
97 (notmuch-call-notmuch-process "tag" (concat "-" tag) (concat "id:" (notmuch-show-get-message-id)))
98 (notmuch-show-set-tags (delete tag (notmuch-show-get-tags))))
100 (defun notmuch-show-archive-thread ()
101 "Archive each message currrently shown by removing the \"inbox\" tag from each.
103 This command is safe from any race condition of new messages
104 being delivered to the same thread. It does not archive the
105 entire thread, but only the messages shown in the current
109 (goto-char (point-min))
111 (notmuch-show-remove-tag "inbox")
114 (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
115 (goto-char (point-max))))))
117 (defun notmuch-show-move-to-current-message-summary-line ()
118 "Move to the beginning of the one-line summary of the current message.
120 This gives us a stable place to move to and work from since the
121 summary line is always visible. This is important since moving to
122 an invisible location is unreliable, (the main command loop moves
123 point either forward or backward to the next visible character
124 when a command ends with point on an invisible character).
126 Emits an error if point is not within a valid message, (that is
127 not pattern of `notmuch-show-message-begin-regexp' could be found
128 by searching backward)."
130 (if (not (looking-at notmuch-show-message-begin-regexp))
131 (if (re-search-backward notmuch-show-message-begin-regexp nil t)
133 (error "Not within a valid message."))
136 (defun notmuch-show-next-message ()
137 "Advance to the beginning of the next message in the buffer.
139 Moves to the beginning of the current message if already on the
140 last message in the buffer."
142 (notmuch-show-move-to-current-message-summary-line)
143 (re-search-forward notmuch-show-message-begin-regexp nil t)
144 (notmuch-show-move-to-current-message-summary-line)
147 (defun notmuch-show-previous-message ()
148 "Backup to the beginning of the previous message in the buffer.
150 If within a message rather than at the beginning of it, then
151 simply move to the beginning of the current message."
153 (let ((start (point)))
154 (notmuch-show-move-to-current-message-summary-line)
155 (if (not (< (point) start))
156 ; Go backward twice to skip the current message's marker
158 (re-search-backward notmuch-show-message-begin-regexp nil t)
159 (re-search-backward notmuch-show-message-begin-regexp nil t)
160 (notmuch-show-move-to-current-message-summary-line)
164 (defun notmuch-show-mark-read-then-next-message ()
165 "Remove uread tag from current message, then advance to next message."
167 (if (member "unread" (notmuch-show-get-tags))
168 (notmuch-show-remove-tag "unread"))
169 (notmuch-show-next-message))
171 (defun notmuch-show-markup-citations-region (beg end)
174 (while (< (point) end)
175 (let ((beg-sub (point)))
178 (while (looking-at ">")
180 (let ((overlay (make-overlay beg-sub (point))))
181 (overlay-put overlay 'invisible 'notmuch-show-citation)
182 (overlay-put overlay 'before-string
183 (concat "[" (number-to-string (count-lines beg-sub (point)))
184 " quoted lines.]")))))
185 (if (looking-at "--[ ]?$")
186 (let ((overlay (make-overlay beg-sub end)))
187 (overlay-put overlay 'invisible 'notmuch-show-signature)
188 (overlay-put overlay 'before-string
189 (concat "[" (number-to-string (count-lines beg-sub (point)))
190 "-line signature.]"))
194 (defun notmuch-show-markup-body ()
195 (re-search-forward notmuch-show-body-begin-regexp)
199 (re-search-forward notmuch-show-body-end-regexp)
200 (let ((end (match-beginning 0)))
201 (notmuch-show-markup-citations-region beg end)
202 (if (not (member "unread" (notmuch-show-get-tags)))
203 (overlay-put (make-overlay beg end)
204 'invisible 'notmuch-show-body-read)))))
206 (defun notmuch-show-markup-header ()
207 (re-search-forward notmuch-show-header-begin-regexp)
211 (re-search-forward notmuch-show-header-end-regexp)
212 (overlay-put (make-overlay beg (match-beginning 0))
213 'invisible 'notmuch-show-header)))
215 (defun notmuch-show-markup-message ()
216 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
218 (notmuch-show-markup-header)
219 (notmuch-show-markup-body))
220 (goto-char (point-max))))
222 (defun notmuch-show-hide-markers ()
224 (goto-char (point-min))
226 (if (re-search-forward notmuch-show-marker-regexp nil t)
228 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
229 'invisible 'notmuch-show-marker))
230 (goto-char (point-max))))))
232 (defun notmuch-show-markup-messages ()
234 (goto-char (point-min))
236 (notmuch-show-markup-message)))
237 (notmuch-show-hide-markers))
239 (defun notmuch-show-toggle-citations-visible ()
240 "Toggle visibility of citations"
242 (if notmuch-show-citations-visible
243 (add-to-invisibility-spec 'notmuch-show-citation)
244 (remove-from-invisibility-spec 'notmuch-show-citation))
245 (set 'notmuch-show-citations-visible (not notmuch-show-citations-visible))
246 ; Need to force the redisplay for some reason
247 (force-window-update)
250 (defun notmuch-show-toggle-signatures-visible ()
251 "Toggle visibility of signatures"
253 (if notmuch-show-signatures-visible
254 (add-to-invisibility-spec 'notmuch-show-signature)
255 (remove-from-invisibility-spec 'notmuch-show-signature))
256 (set 'notmuch-show-signatures-visible (not notmuch-show-signatures-visible))
257 ; Need to force the redisplay for some reason
258 (force-window-update)
261 (defun notmuch-show-toggle-headers-visible ()
262 "Toggle visibility of header fields"
264 (if notmuch-show-headers-visible
265 (add-to-invisibility-spec 'notmuch-show-header)
266 (remove-from-invisibility-spec 'notmuch-show-header))
267 (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
268 ; Need to force the redisplay for some reason
269 (force-window-update)
272 (defun notmuch-show-toggle-body-read-visible ()
273 "Toggle visibility of message bodies of read messages"
275 (if notmuch-show-body-read-visible
276 (add-to-invisibility-spec 'notmuch-show-body-read)
277 (remove-from-invisibility-spec 'notmuch-show-body-read))
278 (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
279 ; Need to force the redisplay for some reason
280 (force-window-update)
284 (defun notmuch-show-mode ()
285 "Major mode for viewing a thread with notmuch.
287 This buffer contains the results of the \"notmuch show\" command
288 for displaying a single thread of email from your email archives.
290 By default, various components of email messages, (citations,
291 signatures, already-read messages), are invisible to help you
292 focus on the most important things, (new text from unread
293 messages). See the various commands below for toggling the
294 visibility of hidden components.
296 The `notmuch-show-next-message' and
297 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
298 default), allow you to navigate to the next and previous
299 messages. Each time you navigate away from a message with
300 `notmuch-show-next-message' the current message will have its
301 \"unread\" tag removed.
303 You can add or remove tags from the current message with '+' and
304 '-'. You can also archive all messages in the current
305 view, (remove the \"inbox\" tag from each), with
306 `notmuch-show-archive-thread' (bound to 'a' by default).
308 \\{notmuch-show-mode-map}"
310 (kill-all-local-variables)
311 (set (make-local-variable 'notmuch-show-headers-visible) t)
312 (notmuch-show-toggle-headers-visible)
313 (set (make-local-variable 'notmuch-show-body-read-visible) t)
314 (notmuch-show-toggle-body-read-visible)
315 (set (make-local-variable 'notmuch-show-citations-visible) t)
316 (notmuch-show-toggle-citations-visible)
317 (set (make-local-variable 'notmuch-show-signatures-visible) t)
318 (notmuch-show-toggle-signatures-visible)
319 (add-to-invisibility-spec 'notmuch-show-marker)
320 (use-local-map notmuch-show-mode-map)
321 (setq major-mode 'notmuch-show-mode
322 mode-name "notmuch-show")
323 (setq buffer-read-only t))
325 (defun notmuch-show (thread-id)
326 "Run \"notmuch show\" with the given thread ID and display results."
327 (interactive "sNotmuch show: ")
328 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
329 (switch-to-buffer buffer)
331 (let ((proc (get-buffer-process (current-buffer)))
332 (inhibit-read-only t))
334 (error "notmuch search process already running for query `%s'" query)
337 (goto-char (point-min))
339 (call-process "notmuch" nil t nil "show" thread-id)
340 (notmuch-show-markup-messages)
344 (defvar notmuch-search-mode-map
345 (let ((map (make-sparse-keymap)))
346 (define-key map "a" 'notmuch-search-archive-thread)
347 (define-key map "b" 'scroll-down)
348 (define-key map "f" 'notmuch-search-filter)
349 (define-key map "n" 'next-line)
350 (define-key map "p" 'previous-line)
351 (define-key map "q" 'kill-this-buffer)
352 (define-key map "s" 'notmuch-search)
353 (define-key map "t" 'notmuch-search-filter-by-tag)
354 (define-key map "x" 'kill-this-buffer)
355 (define-key map "\r" 'notmuch-search-show-thread)
356 (define-key map "+" 'notmuch-search-add-tag)
357 (define-key map "-" 'notmuch-search-remove-tag)
358 (define-key map "<" 'beginning-of-buffer)
359 (define-key map ">" 'notmuch-search-goto-last-thread)
360 (define-key map "=" 'notmuch-search-refresh-view)
361 (define-key map "\M->" 'notmuch-search-goto-last-thread)
362 (define-key map " " 'scroll-up)
363 (define-key map (kbd "<DEL>") 'scroll-down)
365 "Keymap for \"notmuch search\" buffers.")
366 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
368 (defun notmuch-search-goto-last-thread (&optional arg)
369 "Move point to the last thread in the buffer."
375 (defun notmuch-search-mode ()
376 "Major mode for searching mail with notmuch.
378 This buffer contains the results of a \"notmuch search\" of your
379 email archives. Each line in the buffer represents a single
380 thread giving a relative date for the thread and a subject.
382 Pressing RET on any line displays that thread. The '+' and '-'
383 keys can be used to add or remove tags from a thread. The 'a' key
384 is a convenience key for archiving a thread (removing the
387 Other useful commands are `notmuch-search-filter' for filtering
388 the current search based on an additional query string,
389 `notmuch-search-filter-by-tag' for filtering to include only
390 messages with a given tag, and `notmuch-search' to execute a new,
393 \\{notmuch-search-mode-map}"
395 (kill-all-local-variables)
396 (make-local-variable 'notmuch-search-query-string)
397 (set (make-local-variable 'scroll-preserve-screen-position) t)
398 (add-to-invisibility-spec 'notmuch-search)
399 (use-local-map notmuch-search-mode-map)
400 (setq major-mode 'notmuch-search-mode
401 mode-name "notmuch-search")
402 (setq buffer-read-only t))
404 (defun notmuch-search-find-thread-id ()
408 (re-search-forward "[a-fA-F0-9]*")
409 (filter-buffer-substring beg (point)))))
411 (defun notmuch-search-markup-this-thread-id ()
414 (re-search-forward "[a-fA-F0-9]*")
416 (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)))
418 (defun notmuch-search-markup-thread-ids ()
420 (goto-char (point-min))
422 (notmuch-search-markup-this-thread-id)
425 (defun notmuch-search-hide-thread-ids ()
427 (add-to-invisibility-spec 'notmuch-search)
428 (force-window-update)
431 (defun notmuch-search-show-thread-ids ()
433 (remove-from-invisibility-spec 'notmuch-search)
434 (force-window-update)
437 (defun notmuch-search-show-thread ()
439 (notmuch-show (notmuch-search-find-thread-id)))
441 (defun notmuch-call-notmuch-process (&rest args)
442 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
443 (with-current-buffer error-buffer
445 (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
448 (with-current-buffer error-buffer
449 (let ((beg (point-min))
450 (end (- (point-max) 1)))
451 (error (buffer-substring beg end))
454 (defun notmuch-search-set-tags (tags)
457 (re-search-backward "(")
460 (inhibit-read-only t))
461 (re-search-forward ")")
464 (delete-region beg end)
465 (insert (mapconcat 'identity tags " "))))))
467 (defun notmuch-search-get-tags ()
470 (re-search-backward "(")
471 (let ((beg (+ (point) 1)))
472 (re-search-forward ")")
473 (let ((end (- (point) 1)))
474 (split-string (buffer-substring beg end))))))
476 (defun notmuch-search-add-tag (tag)
477 (interactive "sTag to add: ")
478 (notmuch-call-notmuch-process "tag" (concat "+" tag) (concat "thread:" (notmuch-search-find-thread-id)))
479 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
481 (defun notmuch-search-remove-tag (tag)
482 (interactive "sTag to remove: ")
483 (notmuch-call-notmuch-process "tag" (concat "-" tag) (concat "thread:" (notmuch-search-find-thread-id)))
484 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
486 (defun notmuch-search-archive-thread ()
487 "Archive the current thread (remove its \"inbox\" tag).
489 This function advances point to the next line when finished."
491 (notmuch-search-remove-tag "inbox")
494 (defun notmuch-search (query)
495 "Run \"notmuch search\" with the given query string and display results."
496 (interactive "sNotmuch search: ")
497 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
498 (switch-to-buffer buffer)
499 (notmuch-search-mode)
500 (set 'notmuch-search-query-string query)
501 (let ((proc (get-buffer-process (current-buffer)))
502 (inhibit-read-only t))
504 (error "notmuch search process already running for query `%s'" query)
507 (goto-char (point-min))
509 (call-process "notmuch" nil t nil "search" query)
510 (notmuch-search-markup-thread-ids)
511 ; A well-behaved program ends its output with a newline, but we
512 ; don't actually want the blank line at the end of the file.
513 (goto-char (point-max))
514 (if (looking-at "^$")
515 (delete-backward-char 1)
519 (defun notmuch-search-refresh-view ()
520 "Refresh the current view.
522 Kills the current buffer and runs a new search with the same
523 query string as the current search. If the current thread is in
524 the new search results, then point will be placed on the same
525 thread. Otherwise, point will be moved to attempt to be in the
526 same relative position within the new buffer."
529 (thread (notmuch-search-find-thread-id))
530 (query notmuch-search-query-string))
532 (notmuch-search query)
533 (goto-char (point-min))
534 (if (re-search-forward (concat "^" thread) nil t)
538 (defun notmuch-search-filter (query)
539 "Filter the current search results based on an additional query string.
541 Runs a new search matching only messages that match both the
542 current search results AND the additional query string provided."
543 (interactive "sFilter search: ")
544 (notmuch-search (concat notmuch-search-query-string " and " query)))
546 (defun notmuch-search-filter-by-tag (tag)
547 "Filter the current search results based on a single tag.
549 Runs a new search matching only messages that match both the
550 current search results AND that are tagged with the given tag."
551 (interactive "sFilter by tag: ")
552 (notmuch-search (concat notmuch-search-query-string " and tag:" tag)))
555 "Run notmuch to display all mail with tag of 'inbox'"
557 (notmuch-search "tag:inbox"))