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)
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 (notmuch-call-notmuch-process "tag" (concat "-" tag) (concat "id:" (notmuch-show-get-message-id)))
99 (notmuch-show-set-tags (delete tag (notmuch-show-get-tags))))
101 (defun notmuch-show-archive-thread ()
102 "Archive each message currrently shown by removing the \"inbox\" tag from each.
104 This command is safe from any race condition of new messages
105 being delivered to the same thread. It does not archive the
106 entire thread, but only the messages shown in the current
110 (goto-char (point-min))
112 (notmuch-show-remove-tag "inbox")
115 (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
116 (goto-char (point-max))))))
118 (defun notmuch-show-move-to-current-message-summary-line ()
119 "Move to the beginning of the one-line summary of the current message.
121 This gives us a stable place to move to and work from since the
122 summary line is always visible. This is important since moving to
123 an invisible location is unreliable, (the main command loop moves
124 point either forward or backward to the next visible character
125 when a command ends with point on an invisible character).
127 Emits an error if point is not within a valid message, (that is
128 not pattern of `notmuch-show-message-begin-regexp' could be found
129 by searching backward)."
131 (if (not (looking-at notmuch-show-message-begin-regexp))
132 (if (re-search-backward notmuch-show-message-begin-regexp nil t)
134 (error "Not within a valid message."))
137 (defun notmuch-show-next-message ()
138 "Advance to the beginning of the next message in the buffer.
140 Moves to the beginning of the current message if already on the
141 last message in the buffer."
143 (notmuch-show-move-to-current-message-summary-line)
144 (re-search-forward notmuch-show-message-begin-regexp nil t)
145 (notmuch-show-move-to-current-message-summary-line)
148 (defun notmuch-show-find-next-message ()
149 "Returns the position of the next message in the buffer.
151 Or the beginning of the current message if already within the last
152 message in the buffer."
153 ; save-excursion doesn't save our window position
154 ; save-window-excursion doesn't save point
155 ; Looks like we have to use both.
157 (save-window-excursion
158 (notmuch-show-next-message)
161 (defun notmuch-show-previous-message ()
162 "Backup to the beginning of the previous message in the buffer.
164 If within a message rather than at the beginning of it, then
165 simply move to the beginning of the current message."
167 (let ((start (point)))
168 (notmuch-show-move-to-current-message-summary-line)
169 (if (not (< (point) start))
170 ; Go backward twice to skip the current message's marker
172 (re-search-backward notmuch-show-message-begin-regexp nil t)
173 (re-search-backward notmuch-show-message-begin-regexp nil t)
174 (notmuch-show-move-to-current-message-summary-line)
178 (defun notmuch-show-mark-read-then-next-message ()
179 "Remove unread tag from current message, then advance to next message."
181 (if (member "unread" (notmuch-show-get-tags))
182 (notmuch-show-remove-tag "unread"))
183 (notmuch-show-next-message))
185 (defun notmuch-show-advance-marking-read ()
186 "Advance through buffer, marking messages as read.
188 This command is intended to be one of the simplest ways to
189 process a thread of email. It does the following:
191 If the current message in the thread is not yet fully visible,
192 scroll by a near screenful to read more of the message.
194 Otherwise, (the end of the current message is already within the
195 current window), remove the \"unread\" tag from the current
196 message and advance to the next message."
198 (if (< (notmuch-show-find-next-message) (window-end))
199 (notmuch-show-mark-read-then-next-message)
202 (defun notmuch-show-markup-citations-region (beg end)
205 (while (< (point) end)
206 (let ((beg-sub (point)))
209 (while (looking-at ">")
211 (let ((overlay (make-overlay beg-sub (point))))
212 (overlay-put overlay 'invisible 'notmuch-show-citation)
213 (overlay-put overlay 'before-string
214 (concat "[" (number-to-string (count-lines beg-sub (point)))
215 " quoted lines.]")))))
216 (if (looking-at "--[ ]?$")
217 (let ((overlay (make-overlay beg-sub end)))
218 (overlay-put overlay 'invisible 'notmuch-show-signature)
219 (overlay-put overlay 'before-string
220 (concat "[" (number-to-string (count-lines beg-sub (point)))
221 "-line signature.]"))
225 (defun notmuch-show-markup-body ()
226 (re-search-forward notmuch-show-body-begin-regexp)
230 (re-search-forward notmuch-show-body-end-regexp)
231 (let ((end (match-beginning 0)))
232 (notmuch-show-markup-citations-region beg end)
233 (if (not (member "unread" (notmuch-show-get-tags)))
234 (overlay-put (make-overlay beg end)
235 'invisible 'notmuch-show-body-read)))))
237 (defun notmuch-show-markup-header ()
238 (re-search-forward notmuch-show-header-begin-regexp)
242 (re-search-forward notmuch-show-header-end-regexp)
243 (overlay-put (make-overlay beg (match-beginning 0))
244 'invisible 'notmuch-show-header)))
246 (defun notmuch-show-markup-message ()
247 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
249 (notmuch-show-markup-header)
250 (notmuch-show-markup-body))
251 (goto-char (point-max))))
253 (defun notmuch-show-hide-markers ()
255 (goto-char (point-min))
257 (if (re-search-forward notmuch-show-marker-regexp nil t)
259 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
260 'invisible 'notmuch-show-marker))
261 (goto-char (point-max))))))
263 (defun notmuch-show-markup-messages ()
265 (goto-char (point-min))
267 (notmuch-show-markup-message)))
268 (notmuch-show-hide-markers))
270 (defun notmuch-show-toggle-citations-visible ()
271 "Toggle visibility of citations"
273 (if notmuch-show-citations-visible
274 (add-to-invisibility-spec 'notmuch-show-citation)
275 (remove-from-invisibility-spec 'notmuch-show-citation))
276 (set 'notmuch-show-citations-visible (not notmuch-show-citations-visible))
277 ; Need to force the redisplay for some reason
278 (force-window-update)
281 (defun notmuch-show-toggle-signatures-visible ()
282 "Toggle visibility of signatures"
284 (if notmuch-show-signatures-visible
285 (add-to-invisibility-spec 'notmuch-show-signature)
286 (remove-from-invisibility-spec 'notmuch-show-signature))
287 (set 'notmuch-show-signatures-visible (not notmuch-show-signatures-visible))
288 ; Need to force the redisplay for some reason
289 (force-window-update)
292 (defun notmuch-show-toggle-headers-visible ()
293 "Toggle visibility of header fields"
295 (if notmuch-show-headers-visible
296 (add-to-invisibility-spec 'notmuch-show-header)
297 (remove-from-invisibility-spec 'notmuch-show-header))
298 (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
299 ; Need to force the redisplay for some reason
300 (force-window-update)
303 (defun notmuch-show-toggle-body-read-visible ()
304 "Toggle visibility of message bodies of read messages"
306 (if notmuch-show-body-read-visible
307 (add-to-invisibility-spec 'notmuch-show-body-read)
308 (remove-from-invisibility-spec 'notmuch-show-body-read))
309 (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
310 ; Need to force the redisplay for some reason
311 (force-window-update)
315 (defun notmuch-show-mode ()
316 "Major mode for viewing a thread with notmuch.
318 This buffer contains the results of the \"notmuch show\" command
319 for displaying a single thread of email from your email archives.
321 By default, various components of email messages, (citations,
322 signatures, already-read messages), are invisible to help you
323 focus on the most important things, (new text from unread
324 messages). See the various commands below for toggling the
325 visibility of hidden components.
327 The `notmuch-show-next-message' and
328 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
329 default), allow you to navigate to the next and previous
330 messages. Each time you navigate away from a message with
331 `notmuch-show-next-message' the current message will have its
332 \"unread\" tag removed.
334 You can add or remove tags from the current message with '+' and
335 '-'. You can also archive all messages in the current
336 view, (remove the \"inbox\" tag from each), with
337 `notmuch-show-archive-thread' (bound to 'a' by default).
339 \\{notmuch-show-mode-map}"
341 (kill-all-local-variables)
342 (set (make-local-variable 'notmuch-show-headers-visible) t)
343 (notmuch-show-toggle-headers-visible)
344 (set (make-local-variable 'notmuch-show-body-read-visible) t)
345 (notmuch-show-toggle-body-read-visible)
346 (set (make-local-variable 'notmuch-show-citations-visible) t)
347 (notmuch-show-toggle-citations-visible)
348 (set (make-local-variable 'notmuch-show-signatures-visible) t)
349 (notmuch-show-toggle-signatures-visible)
350 (add-to-invisibility-spec 'notmuch-show-marker)
351 (use-local-map notmuch-show-mode-map)
352 (setq major-mode 'notmuch-show-mode
353 mode-name "notmuch-show")
354 (setq buffer-read-only t))
356 (defun notmuch-show (thread-id)
357 "Run \"notmuch show\" with the given thread ID and display results."
358 (interactive "sNotmuch show: ")
359 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
360 (switch-to-buffer buffer)
362 (let ((proc (get-buffer-process (current-buffer)))
363 (inhibit-read-only t))
365 (error "notmuch search process already running for query `%s'" query)
368 (goto-char (point-min))
370 (call-process "notmuch" nil t nil "show" thread-id)
371 (notmuch-show-markup-messages)
375 (defvar notmuch-search-mode-map
376 (let ((map (make-sparse-keymap)))
377 (define-key map "a" 'notmuch-search-archive-thread)
378 (define-key map "b" 'scroll-down)
379 (define-key map "f" 'notmuch-search-filter)
380 (define-key map "n" 'next-line)
381 (define-key map "p" 'previous-line)
382 (define-key map "q" 'kill-this-buffer)
383 (define-key map "s" 'notmuch-search)
384 (define-key map "t" 'notmuch-search-filter-by-tag)
385 (define-key map "x" 'kill-this-buffer)
386 (define-key map "\r" 'notmuch-search-show-thread)
387 (define-key map "+" 'notmuch-search-add-tag)
388 (define-key map "-" 'notmuch-search-remove-tag)
389 (define-key map "<" 'beginning-of-buffer)
390 (define-key map ">" 'notmuch-search-goto-last-thread)
391 (define-key map "=" 'notmuch-search-refresh-view)
392 (define-key map "\M->" 'notmuch-search-goto-last-thread)
393 (define-key map " " 'scroll-up)
394 (define-key map (kbd "<DEL>") 'scroll-down)
396 "Keymap for \"notmuch search\" buffers.")
397 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
399 (defun notmuch-search-goto-last-thread (&optional arg)
400 "Move point to the last thread in the buffer."
406 (defun notmuch-search-mode ()
407 "Major mode for searching mail with notmuch.
409 This buffer contains the results of a \"notmuch search\" of your
410 email archives. Each line in the buffer represents a single
411 thread giving a relative date for the thread and a subject.
413 Pressing RET on any line displays that thread. The '+' and '-'
414 keys can be used to add or remove tags from a thread. The 'a' key
415 is a convenience key for archiving a thread (removing the
418 Other useful commands are `notmuch-search-filter' for filtering
419 the current search based on an additional query string,
420 `notmuch-search-filter-by-tag' for filtering to include only
421 messages with a given tag, and `notmuch-search' to execute a new,
424 \\{notmuch-search-mode-map}"
426 (kill-all-local-variables)
427 (make-local-variable 'notmuch-search-query-string)
428 (set (make-local-variable 'scroll-preserve-screen-position) t)
429 (add-to-invisibility-spec 'notmuch-search)
430 (use-local-map notmuch-search-mode-map)
431 (setq major-mode 'notmuch-search-mode
432 mode-name "notmuch-search")
433 (setq buffer-read-only t))
435 (defun notmuch-search-find-thread-id ()
439 (re-search-forward "[a-fA-F0-9]*")
440 (filter-buffer-substring beg (point)))))
442 (defun notmuch-search-markup-this-thread-id ()
445 (re-search-forward "[a-fA-F0-9]*")
447 (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)))
449 (defun notmuch-search-markup-thread-ids ()
451 (goto-char (point-min))
453 (notmuch-search-markup-this-thread-id)
456 (defun notmuch-search-hide-thread-ids ()
458 (add-to-invisibility-spec 'notmuch-search)
459 (force-window-update)
462 (defun notmuch-search-show-thread-ids ()
464 (remove-from-invisibility-spec 'notmuch-search)
465 (force-window-update)
468 (defun notmuch-search-show-thread ()
470 (notmuch-show (notmuch-search-find-thread-id)))
472 (defun notmuch-call-notmuch-process (&rest args)
473 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
474 (with-current-buffer error-buffer
476 (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
479 (with-current-buffer error-buffer
480 (let ((beg (point-min))
481 (end (- (point-max) 1)))
482 (error (buffer-substring beg end))
485 (defun notmuch-search-set-tags (tags)
488 (re-search-backward "(")
491 (inhibit-read-only t))
492 (re-search-forward ")")
495 (delete-region beg end)
496 (insert (mapconcat 'identity tags " "))))))
498 (defun notmuch-search-get-tags ()
501 (re-search-backward "(")
502 (let ((beg (+ (point) 1)))
503 (re-search-forward ")")
504 (let ((end (- (point) 1)))
505 (split-string (buffer-substring beg end))))))
507 (defun notmuch-search-add-tag (tag)
508 (interactive "sTag to add: ")
509 (notmuch-call-notmuch-process "tag" (concat "+" tag) (concat "thread:" (notmuch-search-find-thread-id)))
510 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
512 (defun notmuch-search-remove-tag (tag)
513 (interactive "sTag to remove: ")
514 (notmuch-call-notmuch-process "tag" (concat "-" tag) (concat "thread:" (notmuch-search-find-thread-id)))
515 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
517 (defun notmuch-search-archive-thread ()
518 "Archive the current thread (remove its \"inbox\" tag).
520 This function advances point to the next line when finished."
522 (notmuch-search-remove-tag "inbox")
525 (defun notmuch-search (query)
526 "Run \"notmuch search\" with the given query string and display results."
527 (interactive "sNotmuch search: ")
528 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
529 (switch-to-buffer buffer)
530 (notmuch-search-mode)
531 (set 'notmuch-search-query-string query)
532 (let ((proc (get-buffer-process (current-buffer)))
533 (inhibit-read-only t))
535 (error "notmuch search process already running for query `%s'" query)
538 (goto-char (point-min))
540 (call-process "notmuch" nil t nil "search" query)
541 (notmuch-search-markup-thread-ids)
542 ; A well-behaved program ends its output with a newline, but we
543 ; don't actually want the blank line at the end of the file.
544 (goto-char (point-max))
545 (if (looking-at "^$")
546 (delete-backward-char 1)
550 (defun notmuch-search-refresh-view ()
551 "Refresh the current view.
553 Kills the current buffer and runs a new search with the same
554 query string as the current search. If the current thread is in
555 the new search results, then point will be placed on the same
556 thread. Otherwise, point will be moved to attempt to be in the
557 same relative position within the new buffer."
560 (thread (notmuch-search-find-thread-id))
561 (query notmuch-search-query-string))
563 (notmuch-search query)
564 (goto-char (point-min))
565 (if (re-search-forward (concat "^" thread) nil t)
569 (defun notmuch-search-filter (query)
570 "Filter the current search results based on an additional query string.
572 Runs a new search matching only messages that match both the
573 current search results AND the additional query string provided."
574 (interactive "sFilter search: ")
575 (notmuch-search (concat notmuch-search-query-string " and " query)))
577 (defun notmuch-search-filter-by-tag (tag)
578 "Filter the current search results based on a single tag.
580 Runs a new search matching only messages that match both the
581 current search results AND that are tagged with the given tag."
582 (interactive "sFilter by tag: ")
583 (notmuch-search (concat notmuch-search-query-string " and tag:" tag)))
586 "Run notmuch to display all mail with tag of 'inbox'"
588 (notmuch-search "tag:inbox"))