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>
25 (defvar notmuch-show-mode-map
26 (let ((map (make-sparse-keymap)))
27 ; I don't actually want all of these toggle commands occupying
28 ; keybindings. They steal valuable key-binding space, are hard
29 ; to remember, and act globally rather than locally.
31 ; Will be much preferable to switch to direct manipulation for
32 ; toggling visibility of these components. Probably using
33 ; overlays-at to query and manipulate the current overlay.
34 (define-key map "a" 'notmuch-show-archive-thread)
35 (define-key map "A" 'notmuch-show-mark-read-then-archive-thread)
36 (define-key map "b" 'notmuch-show-toggle-body-read-visible)
37 (define-key map "c" 'notmuch-show-toggle-citations-visible)
38 (define-key map "h" 'notmuch-show-toggle-headers-visible)
39 (define-key map "m" 'message-mail)
40 (define-key map "n" 'notmuch-show-next-message)
41 (define-key map "N" 'notmuch-show-mark-read-then-next-open-message)
42 (define-key map "p" 'notmuch-show-previous-message)
43 (define-key map (kbd "C-n") 'notmuch-show-next-line)
44 (define-key map (kbd "C-p") 'notmuch-show-previous-line)
45 (define-key map "q" 'kill-this-buffer)
46 (define-key map "r" 'notmuch-show-reply)
47 (define-key map "s" 'notmuch-show-toggle-signatures-visible)
48 (define-key map "v" 'notmuch-show-view-all-mime-parts)
49 (define-key map "w" 'notmuch-show-view-raw-message)
50 (define-key map "x" 'kill-this-buffer)
51 (define-key map "+" 'notmuch-show-add-tag)
52 (define-key map "-" 'notmuch-show-remove-tag)
53 (define-key map (kbd "DEL") 'notmuch-show-rewind)
54 (define-key map " " 'notmuch-show-advance-marking-read-and-archiving)
55 (define-key map "|" 'notmuch-show-pipe-message)
57 "Keymap for \"notmuch show\" buffers.")
58 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
60 (defvar notmuch-show-signature-lines-max 6
61 "Maximum length of signature that will be hidden by default.")
63 (set 'notmuch-show-message-begin-regexp "
\fmessage{")
64 (set 'notmuch-show-message-end-regexp "
\fmessage}")
65 (set 'notmuch-show-header-begin-regexp "
\fheader{")
66 (set 'notmuch-show-header-end-regexp "
\fheader}")
67 (set 'notmuch-show-body-begin-regexp "
\fbody{")
68 (set 'notmuch-show-body-end-regexp "
\fbody}")
69 (set 'notmuch-show-attachment-begin-regexp "
\fattachment{")
70 (set 'notmuch-show-attachment-end-regexp "
\fattachment}")
71 (set 'notmuch-show-part-begin-regexp "
\fpart{")
72 (set 'notmuch-show-part-end-regexp "
\fpart}")
73 (set 'notmuch-show-marker-regexp "
\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
75 (set 'notmuch-show-id-regexp "\\(id:[^ ]*\\)")
76 (set 'notmuch-show-filename-regexp "filename:\\(.*\\)$")
77 (set 'notmuch-show-tags-regexp "(\\([^)]*\\))$")
79 ; XXX: This should be a generic function in emacs somewhere, not here
80 (defun point-invisible-p ()
81 "Return whether the character at point is invisible.
83 Here visibility is determined by `buffer-invisibility-spec' and
84 the invisible property of any overlays for point. It doesn't have
85 anything to do with whether point is currently being displayed
86 within the current window."
87 (let ((prop (get-char-property (point) 'invisible)))
88 (if (eq buffer-invisibility-spec t)
90 (or (memq prop buffer-invisibility-spec)
91 (assq prop buffer-invisibility-spec)))))
93 (defun notmuch-show-next-line ()
94 "Like builtin `next-line' but ensuring we end on a visible character.
96 By advancing forward until reaching a visible character.
98 Unlike builtin `next-line' this version accepts no arguments."
100 (set 'this-command 'next-line)
101 (call-interactively 'next-line)
102 (while (point-invisible-p)
105 (defun notmuch-show-previous-line ()
106 "Like builtin `previous-line' but ensuring we end on a visible character.
108 By advancing forward until reaching a visible character.
110 Unlike builtin `next-line' this version accepts no arguments."
112 (set 'this-command 'previous-line)
113 (call-interactively 'previous-line)
114 (while (point-invisible-p)
117 (defun notmuch-show-get-message-id ()
120 (if (not (looking-at notmuch-show-message-begin-regexp))
121 (re-search-backward notmuch-show-message-begin-regexp))
122 (re-search-forward notmuch-show-id-regexp)
123 (buffer-substring (match-beginning 1) (match-end 1))))
125 (defun notmuch-show-get-filename ()
128 (if (not (looking-at notmuch-show-message-begin-regexp))
129 (re-search-backward notmuch-show-message-begin-regexp))
130 (re-search-forward notmuch-show-filename-regexp)
131 (buffer-substring (match-beginning 1) (match-end 1))))
133 (defun notmuch-show-set-tags (tags)
136 (if (not (looking-at notmuch-show-message-begin-regexp))
137 (re-search-backward notmuch-show-message-begin-regexp))
138 (re-search-forward notmuch-show-tags-regexp)
139 (let ((inhibit-read-only t)
140 (beg (match-beginning 1))
142 (delete-region beg end)
144 (insert (mapconcat 'identity tags " ")))))
146 (defun notmuch-show-get-tags ()
149 (if (not (looking-at notmuch-show-message-begin-regexp))
150 (re-search-backward notmuch-show-message-begin-regexp))
151 (re-search-forward notmuch-show-tags-regexp)
152 (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
154 (defun notmuch-show-add-tag (&rest toadd)
155 "Add a tag to the current message."
156 (interactive "sTag to add: ")
157 (apply 'notmuch-call-notmuch-process
159 (mapcar (lambda (s) (concat "+" s)) toadd))
160 (cons (notmuch-show-get-message-id) nil)))
161 (notmuch-show-set-tags (sort (union toadd (notmuch-show-get-tags) :test 'string=) 'string<)))
163 (defun notmuch-show-remove-tag (&rest toremove)
164 "Remove a tag from the current message."
165 (interactive "sTag to remove: ")
166 (let ((tags (notmuch-show-get-tags)))
167 (if (intersection tags toremove :test 'string=)
169 (apply 'notmuch-call-notmuch-process
171 (mapcar (lambda (s) (concat "-" s)) toremove))
172 (cons (notmuch-show-get-message-id) nil)))
173 (notmuch-show-set-tags (sort (set-difference tags toremove :test 'string=) 'string<))))))
175 (defun notmuch-show-archive-thread-maybe-mark-read (markread)
177 (goto-char (point-min))
180 (notmuch-show-remove-tag "unread" "inbox")
181 (notmuch-show-remove-tag "inbox"))
184 (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
185 (goto-char (point-max)))))
186 (let ((parent-buffer notmuch-show-parent-buffer))
190 (switch-to-buffer parent-buffer)
192 (notmuch-search-show-thread)))))
194 (defun notmuch-show-mark-read-then-archive-thread ()
195 "Remove \"unread\" tag from each message, then archive and show next thread.
197 Archive each message currrently shown by removing the \"unread\"
198 and \"inbox\" tag from each. Then kill this buffer and show the
199 next thread from the search from which this thread was originally
202 Note: This command is safe from any race condition of new messages
203 being delivered to the same thread. It does not archive the
204 entire thread, but only the messages shown in the current
207 (notmuch-show-archive-thread-maybe-mark-read t))
209 (defun notmuch-show-archive-thread ()
210 "Archive each message in thread, and show next thread from search.
212 Archive each message currrently shown by removing the \"inbox\"
213 tag from each. Then kill this buffer and show the next thread
214 from the search from which this thread was originally shown.
216 Note: This command is safe from any race condition of new messages
217 being delivered to the same thread. It does not archive the
218 entire thread, but only the messages shown in the current
221 (notmuch-show-archive-thread-maybe-mark-read nil))
223 (defun notmuch-show-view-raw-message ()
224 "View the raw email of the current message."
226 (view-file (notmuch-show-get-filename)))
228 (defun notmuch-show-view-all-mime-parts ()
229 "Use external viewers (according to mailcap) to view all MIME-encoded parts."
232 (let ((filename (notmuch-show-get-filename)))
233 (switch-to-buffer (generate-new-buffer (concat "*notmuch-mime-"
236 (insert-file-contents filename nil nil nil t)
237 (mm-display-parts (mm-dissect-buffer))
238 (kill-this-buffer))))
240 (defun notmuch-show-reply ()
241 "Begin composing a reply to the current message in a new buffer."
243 (let ((message-id (notmuch-show-get-message-id)))
244 (switch-to-buffer (generate-new-buffer "notmuch-draft"))
245 (call-process "notmuch" nil t nil "reply" message-id)
246 (goto-char (point-min))
247 (if (re-search-forward "^$" nil t)
249 (insert "--text follows this line--")
253 (defun notmuch-show-pipe-message (command)
254 "Pipe the contents of the current message to the given command.
256 The given command will be executed with the raw contents of the
257 current email message as stdin. Anything printed by the command
258 to stdout or stderr will appear in the *Messages* buffer."
259 (interactive "sPipe message to command: ")
260 (apply 'start-process-shell-command "notmuch-pipe-command" "*notmuch-pipe*" (split-string (concat command " < " (notmuch-show-get-filename)))))
262 (defun notmuch-show-move-to-current-message-summary-line ()
263 "Move to the beginning of the one-line summary of the current message.
265 This gives us a stable place to move to and work from since the
266 summary line is always visible. This is important since moving to
267 an invisible location is unreliable, (the main command loop moves
268 point either forward or backward to the next visible character
269 when a command ends with point on an invisible character).
271 Emits an error if point is not within a valid message, (that is
272 not pattern of `notmuch-show-message-begin-regexp' could be found
273 by searching backward)."
275 (if (not (looking-at notmuch-show-message-begin-regexp))
276 (if (re-search-backward notmuch-show-message-begin-regexp nil t)
278 (error "Not within a valid message."))
281 (defun notmuch-show-last-message-p ()
282 "Predicate testing whether point is within the last message."
283 (save-window-excursion
285 (notmuch-show-move-to-current-message-summary-line)
286 (not (re-search-forward notmuch-show-message-begin-regexp nil t)))))
288 (defun notmuch-show-message-unread-p ()
289 "Preficate testing whether current message is unread."
290 (member "unread" (notmuch-show-get-tags)))
292 (defun notmuch-show-next-message ()
293 "Advance to the beginning of the next message in the buffer.
295 Moves to the last visible character of the current message if
296 already on the last message in the buffer."
298 (notmuch-show-move-to-current-message-summary-line)
299 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
300 (notmuch-show-move-to-current-message-summary-line)
301 (goto-char (- (point-max) 1))
302 (while (point-invisible-p)
306 (defun notmuch-show-find-next-message ()
307 "Returns the position of the next message in the buffer.
309 Or the position of the last visible character of the current
310 message if already within the last message in the buffer."
311 ; save-excursion doesn't save our window position
312 ; save-window-excursion doesn't save point
313 ; Looks like we have to use both.
315 (save-window-excursion
316 (notmuch-show-next-message)
319 (defun notmuch-show-next-unread-message ()
320 "Advance to the beginning of the next unread message in the buffer.
322 Moves to the last visible character of the current message if
323 there are no more unread messages past the current point."
324 (notmuch-show-next-message)
325 (while (and (not (notmuch-show-last-message-p))
326 (not (notmuch-show-message-unread-p)))
327 (notmuch-show-next-message))
328 (if (not (notmuch-show-message-unread-p))
329 (notmuch-show-next-message)))
331 (defun notmuch-show-next-open-message ()
332 "Advance to the the next message which is not hidden.
334 If read messages are currently hidden, advance to the next unread
335 message. Otherwise, advance to the next message."
336 (if (or (memq 'notmuch-show-body-read buffer-invisibility-spec)
337 (assq 'notmuch-show-body-read buffer-invisibility-spec))
338 (notmuch-show-next-unread-message)
339 (notmuch-show-next-message)))
341 (defun notmuch-show-previous-message ()
342 "Backup to the beginning of the previous message in the buffer.
344 If within a message rather than at the beginning of it, then
345 simply move to the beginning of the current message."
347 (let ((start (point)))
348 (notmuch-show-move-to-current-message-summary-line)
349 (if (not (< (point) start))
350 ; Go backward twice to skip the current message's marker
352 (re-search-backward notmuch-show-message-begin-regexp nil t)
353 (re-search-backward notmuch-show-message-begin-regexp nil t)
354 (notmuch-show-move-to-current-message-summary-line)
358 (defun notmuch-show-find-previous-message ()
359 "Returns the position of the previous message in the buffer.
361 Or the position of the beginning of the current message if point
362 is originally within the message rather than at the beginning of
364 ; save-excursion doesn't save our window position
365 ; save-window-excursion doesn't save point
366 ; Looks like we have to use both.
368 (save-window-excursion
369 (notmuch-show-previous-message)
372 (defun notmuch-show-mark-read-then-next-open-message ()
373 "Remove unread tag from current message, then advance to next unread message."
375 (notmuch-show-remove-tag "unread")
376 (notmuch-show-next-open-message))
378 (defun notmuch-show-rewind ()
379 "Do reverse scrolling compared to `notmuch-show-advance-marking-read-and-archiving'
381 Specifically, if the beginning of the previous email is fewer
382 than `window-height' lines from the current point, move to it
383 just like `notmuch-show-previous-message'.
385 Otherwise, just scroll down a screenful of the current message.
387 This command does not modify any message tags, (it does not undo
388 any effects from previous calls to
389 `notmuch-show-advance-marking-read-and-archiving'."
391 (let ((previous (notmuch-show-find-previous-message)))
392 (if (> (count-lines previous (point)) (- (window-height) next-screen-context-lines))
396 ((beginning-of-buffer) nil))
397 (goto-char (window-start)))
398 (notmuch-show-previous-message))))
400 (defun notmuch-show-advance-marking-read-and-archiving ()
401 "Advance through buffer, marking read and archiving.
403 This command is intended to be one of the simplest ways to
404 process a thread of email. It does the following:
406 If the current message in the thread is not yet fully visible,
407 scroll by a near screenful to read more of the message.
409 Otherwise, (the end of the current message is already within the
410 current window), remove the \"unread\" tag (if present) from the
411 current message and advance to the next open message.
413 Finally, if there is no further message to advance to, and this
414 last message is already read, then archive the entire current
415 thread, (remove the \"inbox\" tag from each message). Also kill
416 this buffer, and display the next thread from the search from
417 which this thread was originally shown."
419 (let ((next (notmuch-show-find-next-message))
420 (unread (notmuch-show-message-unread-p)))
421 (if (> next (window-end))
423 (let ((last (notmuch-show-last-message-p)))
424 (notmuch-show-mark-read-then-next-open-message)
426 (notmuch-show-archive-thread))))))
428 (defun notmuch-show-markup-citations-region (beg end)
431 (while (< (point) end)
432 (let ((beg-sub (point)))
435 (while (looking-at ">")
437 (let ((overlay (make-overlay beg-sub (point))))
438 (overlay-put overlay 'invisible 'notmuch-show-citation)
439 (overlay-put overlay 'before-string
440 (concat "[" (number-to-string (count-lines beg-sub (point)))
441 "-line citation. Press 'c' to show.]\n")))))
442 (if (looking-at "--[ ]?$")
443 (let ((sig-lines (count-lines beg-sub end)))
444 (if (<= sig-lines notmuch-show-signature-lines-max)
446 (overlay-put (make-overlay beg-sub (+ beg-sub 1))
448 (concat "[" (number-to-string sig-lines)
449 "-line signature. Press 's' to show.]"))
450 (overlay-put (make-overlay (+ beg-sub 2) end)
451 'invisible 'notmuch-show-signature)
455 (defun notmuch-show-markup-body ()
456 (re-search-forward notmuch-show-body-begin-regexp)
460 (re-search-forward notmuch-show-body-end-regexp)
461 (let ((end (match-beginning 0)))
462 (notmuch-show-markup-citations-region beg end)
463 (if (not (notmuch-show-message-unread-p))
464 (overlay-put (make-overlay beg end)
465 'invisible 'notmuch-show-body-read)))))
467 (defun notmuch-show-markup-header ()
468 (re-search-forward notmuch-show-header-begin-regexp)
473 ; Inverse video for subject
474 (overlay-put (make-overlay beg (point)) 'face '((cons :inverse-video t)))
478 (re-search-forward notmuch-show-header-end-regexp)
479 (overlay-put (make-overlay beg (match-beginning 0))
480 'invisible 'notmuch-show-header)))
482 (defun notmuch-show-markup-message ()
483 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
485 (notmuch-show-markup-header)
486 (notmuch-show-markup-body))
487 (goto-char (point-max))))
489 (defun notmuch-show-hide-markers ()
491 (goto-char (point-min))
493 (if (re-search-forward notmuch-show-marker-regexp nil t)
495 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
496 'invisible 'notmuch-show-marker))
497 (goto-char (point-max))))))
499 (defun notmuch-show-markup-messages ()
501 (goto-char (point-min))
503 (notmuch-show-markup-message)))
504 (notmuch-show-hide-markers))
506 (defun notmuch-show-toggle-citations-visible ()
507 "Toggle visibility of citations"
509 (if notmuch-show-citations-visible
510 (add-to-invisibility-spec 'notmuch-show-citation)
511 (remove-from-invisibility-spec 'notmuch-show-citation))
512 (set 'notmuch-show-citations-visible (not notmuch-show-citations-visible))
513 ; Need to force the redisplay for some reason
514 (force-window-update)
517 (defun notmuch-show-toggle-signatures-visible ()
518 "Toggle visibility of signatures"
520 (if notmuch-show-signatures-visible
521 (add-to-invisibility-spec 'notmuch-show-signature)
522 (remove-from-invisibility-spec 'notmuch-show-signature))
523 (set 'notmuch-show-signatures-visible (not notmuch-show-signatures-visible))
524 ; Need to force the redisplay for some reason
525 (force-window-update)
528 (defun notmuch-show-toggle-headers-visible ()
529 "Toggle visibility of header fields"
531 (if notmuch-show-headers-visible
532 (add-to-invisibility-spec 'notmuch-show-header)
533 (remove-from-invisibility-spec 'notmuch-show-header))
534 (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
535 ; Need to force the redisplay for some reason
536 (force-window-update)
539 (defun notmuch-show-toggle-body-read-visible ()
540 "Toggle visibility of message bodies of read messages"
542 (if notmuch-show-body-read-visible
543 (add-to-invisibility-spec 'notmuch-show-body-read)
544 (remove-from-invisibility-spec 'notmuch-show-body-read))
545 (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
546 ; Need to force the redisplay for some reason
547 (force-window-update)
551 (defun notmuch-show-mode ()
552 "Major mode for viewing a thread with notmuch.
554 This buffer contains the results of the \"notmuch show\" command
555 for displaying a single thread of email from your email archives.
557 By default, various components of email messages, (citations,
558 signatures, already-read messages), are invisible to help you
559 focus on the most important things, (new text from unread
560 messages). See the various commands below for toggling the
561 visibility of hidden components.
563 The `notmuch-show-next-message' and
564 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
565 default), allow you to navigate to the next and previous
566 messages. Each time you navigate away from a message with
567 `notmuch-show-next-message' the current message will have its
568 \"unread\" tag removed.
570 You can add or remove tags from the current message with '+' and
571 '-'. You can also archive all messages in the current
572 view, (remove the \"inbox\" tag from each), with
573 `notmuch-show-archive-thread' (bound to 'a' by default).
575 \\{notmuch-show-mode-map}"
577 (kill-all-local-variables)
578 (set (make-local-variable 'notmuch-show-headers-visible) t)
579 (notmuch-show-toggle-headers-visible)
580 (set (make-local-variable 'notmuch-show-body-read-visible) t)
581 (notmuch-show-toggle-body-read-visible)
582 (set (make-local-variable 'notmuch-show-citations-visible) t)
583 (notmuch-show-toggle-citations-visible)
584 (set (make-local-variable 'notmuch-show-signatures-visible) t)
585 (notmuch-show-toggle-signatures-visible)
586 (add-to-invisibility-spec 'notmuch-show-marker)
587 (use-local-map notmuch-show-mode-map)
588 (setq major-mode 'notmuch-show-mode
589 mode-name "notmuch-show")
590 (setq buffer-read-only t))
592 (defun notmuch-show (thread-id &optional parent-buffer)
593 "Run \"notmuch show\" with the given thread ID and display results.
595 The optional PARENT-BUFFER is the notmuch-search buffer from
596 which this notmuch-show command was executed, (so that the next
597 thread from that buffer can be show when done with this one)."
598 (interactive "sNotmuch show: ")
599 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
600 (switch-to-buffer buffer)
602 (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
603 (let ((proc (get-buffer-process (current-buffer)))
604 (inhibit-read-only t))
606 (error "notmuch search process already running for query `%s'" query)
609 (goto-char (point-min))
611 (call-process "notmuch" nil t nil "show" thread-id)
612 (notmuch-show-markup-messages)
614 ; Move straight to the first unread message
615 (if (not (notmuch-show-message-unread-p))
617 (notmuch-show-next-unread-message)
618 ; But if there are no unread messages, go back to the
619 ; beginning of the buffer, and open up the bodies of all
621 (if (not (notmuch-show-message-unread-p))
623 (goto-char (point-min))
624 (notmuch-show-toggle-body-read-visible)))))
627 (defvar notmuch-search-mode-map
628 (let ((map (make-sparse-keymap)))
629 (define-key map "a" 'notmuch-search-archive-thread)
630 (define-key map "b" 'notmuch-search-scroll-down)
631 (define-key map "f" 'notmuch-search-filter)
632 (define-key map "m" 'message-mail)
633 (define-key map "n" 'next-line)
634 (define-key map "o" 'notmuch-search-toggle-order)
635 (define-key map "p" 'previous-line)
636 (define-key map "q" 'kill-this-buffer)
637 (define-key map "s" 'notmuch-search)
638 (define-key map "t" 'notmuch-search-filter-by-tag)
639 (define-key map "x" 'kill-this-buffer)
640 (define-key map (kbd "RET") 'notmuch-search-show-thread)
641 (define-key map "+" 'notmuch-search-add-tag)
642 (define-key map "-" 'notmuch-search-remove-tag)
643 (define-key map "<" 'beginning-of-buffer)
644 (define-key map ">" 'notmuch-search-goto-last-thread)
645 (define-key map "=" 'notmuch-search-refresh-view)
646 (define-key map "\M->" 'notmuch-search-goto-last-thread)
647 (define-key map " " 'notmuch-search-scroll-up)
648 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
650 "Keymap for \"notmuch search\" buffers.")
651 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
653 (defun notmuch-search-scroll-up ()
654 "Scroll up, moving point to last message in thread if at end."
658 ((end-of-buffer) (notmuch-search-goto-last-thread))))
660 (defun notmuch-search-scroll-down ()
661 "Scroll down, moving point to first message in thread if at beginning."
663 ; I don't know why scroll-down doesn't signal beginning-of-buffer
664 ; the way that scroll-up signals end-of-buffer, but c'est la vie.
666 ; So instead of trapping a signal we instead check whether the
667 ; window begins on the first line of the buffer and if so, move
668 ; directly to that position. (We have to count lines since the
669 ; window-start position is not the same as point-min due to the
670 ; invisible thread-ID characters on the first line.
671 (if (equal (count-lines (point-min) (window-start)) 1)
672 (goto-char (window-start))
675 (defun notmuch-search-goto-last-thread (&optional arg)
676 "Move point to the last thread in the buffer."
682 (defun notmuch-search-mode ()
683 "Major mode for searching mail with notmuch.
685 This buffer contains the results of a \"notmuch search\" of your
686 email archives. Each line in the buffer represents a single
687 thread giving a relative date for the thread and a subject.
689 Pressing RET on any line displays that thread. The '+' and '-'
690 keys can be used to add or remove tags from a thread. The 'a' key
691 is a convenience key for archiving a thread (removing the
694 Other useful commands are `notmuch-search-filter' for filtering
695 the current search based on an additional query string,
696 `notmuch-search-filter-by-tag' for filtering to include only
697 messages with a given tag, and `notmuch-search' to execute a new,
700 \\{notmuch-search-mode-map}"
702 (kill-all-local-variables)
703 (make-local-variable 'notmuch-search-query-string)
704 (make-local-variable 'notmuch-search-oldest-first)
705 (set (make-local-variable 'scroll-preserve-screen-position) t)
706 (add-to-invisibility-spec 'notmuch-search)
707 (use-local-map notmuch-search-mode-map)
708 (setq major-mode 'notmuch-search-mode
709 mode-name "notmuch-search")
710 (setq buffer-read-only t))
712 (defun notmuch-search-find-thread-id ()
716 (re-search-forward "thread:[a-fA-F0-9]*" nil t)
717 (filter-buffer-substring beg (point)))))
719 (defun notmuch-search-markup-this-thread-id ()
722 (if (re-search-forward "thread:[a-fA-F0-9]*" nil t)
725 (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)))))
727 (defun notmuch-search-markup-thread-ids ()
729 (goto-char (point-min))
731 (notmuch-search-markup-this-thread-id)
734 (defun notmuch-search-show-thread ()
736 (let ((thread-id (notmuch-search-find-thread-id)))
737 (if (> (length thread-id) 0)
738 (notmuch-show thread-id (current-buffer))
739 (error "End of search results"))))
741 (defun notmuch-call-notmuch-process (&rest args)
742 "Synchronously invoke \"notmuch\" with the given list of arguments.
744 Output from the process will be presented to the user as an error
745 and will also appear in a buffer named \"*Notmuch errors*\"."
746 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
747 (with-current-buffer error-buffer
749 (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
752 (with-current-buffer error-buffer
753 (let ((beg (point-min))
754 (end (- (point-max) 1)))
755 (error (buffer-substring beg end))
758 (defun notmuch-search-set-tags (tags)
761 (re-search-backward "(")
764 (inhibit-read-only t))
765 (re-search-forward ")")
768 (delete-region beg end)
769 (insert (mapconcat 'identity tags " "))))))
771 (defun notmuch-search-get-tags ()
774 (re-search-backward "(")
775 (let ((beg (+ (point) 1)))
776 (re-search-forward ")")
777 (let ((end (- (point) 1)))
778 (split-string (buffer-substring beg end))))))
780 (defun notmuch-search-add-tag (tag)
781 (interactive "sTag to add: ")
782 (notmuch-call-notmuch-process "tag" (concat "+" tag) (notmuch-search-find-thread-id))
783 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
785 (defun notmuch-search-remove-tag (tag)
786 (interactive "sTag to remove: ")
787 (notmuch-call-notmuch-process "tag" (concat "-" tag) (notmuch-search-find-thread-id))
788 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
790 (defun notmuch-search-archive-thread ()
791 "Archive the current thread (remove its \"inbox\" tag).
793 This function advances point to the next line when finished."
795 (notmuch-search-remove-tag "inbox")
798 (defun notmuch-search (query &optional oldest-first)
799 "Run \"notmuch search\" with the given query string and display results."
800 (interactive "sNotmuch search: ")
801 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
802 (switch-to-buffer buffer)
803 (notmuch-search-mode)
804 (set 'notmuch-search-query-string query)
805 (set 'notmuch-search-oldest-first oldest-first)
806 (let ((proc (get-buffer-process (current-buffer)))
807 (inhibit-read-only t))
809 (error "notmuch search process already running for query `%s'" query)
812 (goto-char (point-min))
815 (call-process "notmuch" nil t nil "search" query)
816 (call-process "notmuch" nil t nil "search" "--reverse" query))
817 (notmuch-search-markup-thread-ids)
820 (defun notmuch-search-refresh-view ()
821 "Refresh the current view.
823 Kills the current buffer and runs a new search with the same
824 query string as the current search. If the current thread is in
825 the new search results, then point will be placed on the same
826 thread. Otherwise, point will be moved to attempt to be in the
827 same relative position within the new buffer."
830 (oldest-first notmuch-search-oldest-first)
831 (thread (notmuch-search-find-thread-id))
832 (query notmuch-search-query-string))
834 (notmuch-search query oldest-first)
835 (goto-char (point-min))
836 (if (re-search-forward (concat "^" thread) nil t)
840 (defun notmuch-search-toggle-order ()
841 "Toggle the current search order.
843 By default, the \"inbox\" view created by `notmuch' is displayed
844 in chronological order (oldest thread at the beginning of the
845 buffer), while any global searches created by `notmuch-search'
846 are displayed in reverse-chronological order (newest thread at
847 the beginning of the buffer).
849 This command toggles the sort order for the current search.
851 Note that any fitlered searches created by
852 `notmuch-search-filter' retain the search order of the parent
855 (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
856 (notmuch-search-refresh-view))
858 (defun notmuch-search-filter (query)
859 "Filter the current search results based on an additional query string.
861 Runs a new search matching only messages that match both the
862 current search results AND the additional query string provided."
863 (interactive "sFilter search: ")
864 (notmuch-search (concat notmuch-search-query-string " and " query) notmuch-search-oldest-first))
866 (defun notmuch-search-filter-by-tag (tag)
867 "Filter the current search results based on a single tag.
869 Runs a new search matching only messages that match both the
870 current search results AND that are tagged with the given tag."
871 (interactive "sFilter by tag: ")
872 (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
875 "Run notmuch to display all mail with tag of 'inbox'"
877 (notmuch-search "tag:inbox" t))