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 ;; This is an emacs-based interface to the notmuch mail system.
24 ;; You will first need to have the notmuch program installed and have a
25 ;; notmuch database built in order to use this. See
26 ;; http://notmuchmail.org for details.
28 ;; To install this software, copy it to a directory that is on the
29 ;; `load-path' variable within emacs (a good candidate is
30 ;; /usr/local/share/emacs/site-lisp). If you are viewing this from the
31 ;; notmuch source distribution then you can simply run:
33 ;; sudo make install-emacs
37 ;; Then, to actually run it, add:
41 ;; to your ~/.emacs file, and then run "M-x notmuch" from within emacs,
46 ;; Have fun, and let us know if you have any comment, questions, or
47 ;; kudos: Notmuch list <notmuch@notmuchmail.org> (subscription is not
48 ;; required, but is available from http://notmuchmail.org).
50 (eval-when-compile (require 'cl))
54 (require 'notmuch-lib)
55 (require 'notmuch-tag)
56 (require 'notmuch-show)
57 (require 'notmuch-mua)
58 (require 'notmuch-hello)
59 (require 'notmuch-maildir-fcc)
60 (require 'notmuch-message)
61 (require 'notmuch-parser)
63 (defcustom notmuch-search-result-format
66 ("authors" . "%-20s ")
69 "Search result formatting. Supported fields are:
70 date, count, authors, subject, tags
72 (setq notmuch-search-result-format \(\(\"authors\" . \"%-40s\"\)
73 \(\"subject\" . \"%s\"\)\)\)
74 Line breaks are permitted in format strings (though this is
75 currently experimental). Note that a line break at the end of an
76 \"authors\" field will get elided if the authors list is long;
77 place it instead at the beginning of the following field. To
78 enter a line break when setting this variable with setq, use \\n.
79 To enter a line break in customize, press \\[quoted-insert] C-j."
80 :type '(alist :key-type (string) :value-type (string))
81 :group 'notmuch-search)
83 (defvar notmuch-query-history nil
84 "Variable to store minibuffer history for notmuch queries")
86 (defun notmuch-foreach-mime-part (function mm-handle)
87 (cond ((stringp (car mm-handle))
88 (dolist (part (cdr mm-handle))
89 (notmuch-foreach-mime-part function part)))
90 ((bufferp (car mm-handle))
91 (funcall function mm-handle))
92 (t (dolist (part mm-handle)
93 (notmuch-foreach-mime-part function part)))))
95 (defun notmuch-count-attachments (mm-handle)
97 (notmuch-foreach-mime-part
99 (let ((disposition (mm-handle-disposition p)))
100 (and (listp disposition)
101 (or (equal (car disposition) "attachment")
102 (and (equal (car disposition) "inline")
103 (assq 'filename disposition)))
108 (defun notmuch-save-attachments (mm-handle &optional queryp)
109 (notmuch-foreach-mime-part
111 (let ((disposition (mm-handle-disposition p)))
112 (and (listp disposition)
113 (or (equal (car disposition) "attachment")
114 (and (equal (car disposition) "inline")
115 (assq 'filename disposition)))
118 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
122 (defun notmuch-documentation-first-line (symbol)
123 "Return the first line of the documentation string for SYMBOL."
124 (let ((doc (documentation symbol)))
127 (insert (documentation symbol t))
128 (goto-char (point-min))
131 (buffer-substring beg (point))))
134 (defun notmuch-prefix-key-description (key)
135 "Given a prefix key code, return a human-readable string representation.
137 This is basically just `format-kbd-macro' but we also convert ESC to M-."
138 (let ((desc (format-kbd-macro (vector key))))
139 (if (string= desc "ESC")
143 ;; I would think that emacs would have code handy for walking a keymap
144 ;; and generating strings for each key, and I would prefer to just call
145 ;; that. But I couldn't find any (could be all implemented in C I
146 ;; suppose), so I wrote my own here.
147 (defun notmuch-substitute-one-command-key-with-prefix (prefix binding)
148 "For a key binding, return a string showing a human-readable
149 representation of the prefixed key as well as the first line of
150 documentation from the bound function.
152 For a mouse binding, return nil."
153 (let ((key (car binding))
154 (action (cdr binding)))
155 (if (mouse-event-p key)
158 (let ((substitute (apply-partially 'notmuch-substitute-one-command-key-with-prefix (notmuch-prefix-key-description key)))
160 (map-keymap (lambda (a b)
161 (push (cons a b) as-list))
163 (mapconcat substitute as-list "\n"))
164 (concat prefix (format-kbd-macro (vector key))
166 (notmuch-documentation-first-line action))))))
168 (defun notmuch-substitute-command-keys-one (key)
169 ;; A `keymap' key indicates inheritance from a parent keymap - the
170 ;; inherited mappings follow, so there is nothing to print for
172 (when (not (eq key 'keymap))
173 (notmuch-substitute-one-command-key-with-prefix nil key)))
175 (defun notmuch-substitute-command-keys (doc)
176 "Like `substitute-command-keys' but with documentation, not function names."
178 (while (string-match "\\\\{\\([^}[:space:]]*\\)}" doc beg)
179 (let* ((keymap-name (substring doc (match-beginning 1) (match-end 1)))
180 (keymap (symbol-value (intern keymap-name))))
181 (setq doc (replace-match
182 (mapconcat #'notmuch-substitute-command-keys-one
185 (setq beg (match-end 0)))
188 (defun notmuch-help ()
189 "Display help for the current notmuch mode."
191 (let* ((mode major-mode)
192 (doc (substitute-command-keys (notmuch-substitute-command-keys (documentation mode t)))))
193 (with-current-buffer (generate-new-buffer "*notmuch-help*")
195 (goto-char (point-min))
196 (set-buffer-modified-p nil)
197 (view-buffer (current-buffer) 'kill-buffer-if-not-modified))))
201 (defun notmuch-hl-line-mode ()
202 (prog1 (hl-line-mode)
203 (when hl-line-overlay
204 (overlay-put hl-line-overlay 'priority 1))))
206 (defcustom notmuch-search-hook '(notmuch-hl-line-mode)
207 "List of functions to call when notmuch displays the search results."
209 :options '(notmuch-hl-line-mode)
210 :group 'notmuch-search
211 :group 'notmuch-hooks)
213 (defvar notmuch-search-mode-map
214 (let ((map (make-sparse-keymap)))
215 (define-key map "?" 'notmuch-help)
216 (define-key map "q" 'notmuch-search-quit)
217 (define-key map "x" 'notmuch-search-quit)
218 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
219 (define-key map "b" 'notmuch-search-scroll-down)
220 (define-key map " " 'notmuch-search-scroll-up)
221 (define-key map "<" 'notmuch-search-first-thread)
222 (define-key map ">" 'notmuch-search-last-thread)
223 (define-key map "p" 'notmuch-search-previous-thread)
224 (define-key map "n" 'notmuch-search-next-thread)
225 (define-key map "r" 'notmuch-search-reply-to-thread-sender)
226 (define-key map "R" 'notmuch-search-reply-to-thread)
227 (define-key map "m" 'notmuch-mua-new-mail)
228 (define-key map "s" 'notmuch-search)
229 (define-key map "o" 'notmuch-search-toggle-order)
230 (define-key map "c" 'notmuch-search-stash-map)
231 (define-key map "=" 'notmuch-search-refresh-view)
232 (define-key map "G" 'notmuch-search-poll-and-refresh-view)
233 (define-key map "t" 'notmuch-search-filter-by-tag)
234 (define-key map "f" 'notmuch-search-filter)
235 (define-key map [mouse-1] 'notmuch-search-show-thread)
236 (define-key map "*" 'notmuch-search-tag-all)
237 (define-key map "a" 'notmuch-search-archive-thread)
238 (define-key map "-" 'notmuch-search-remove-tag)
239 (define-key map "+" 'notmuch-search-add-tag)
240 (define-key map (kbd "RET") 'notmuch-search-show-thread)
242 "Keymap for \"notmuch search\" buffers.")
243 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
245 (defvar notmuch-search-stash-map
246 (let ((map (make-sparse-keymap)))
247 (define-key map "i" 'notmuch-search-stash-thread-id)
249 "Submap for stash commands")
250 (fset 'notmuch-search-stash-map notmuch-search-stash-map)
252 (defun notmuch-search-stash-thread-id ()
253 "Copy thread ID of current thread to kill-ring."
255 (notmuch-common-do-stash (notmuch-search-find-thread-id)))
257 (defvar notmuch-search-query-string)
258 (defvar notmuch-search-target-thread)
259 (defvar notmuch-search-target-line)
260 (defvar notmuch-search-continuation)
262 (defvar notmuch-search-disjunctive-regexp "\\<[oO][rR]\\>")
264 (defun notmuch-search-quit ()
265 "Exit the search buffer, calling any defined continuation function."
267 (let ((continuation notmuch-search-continuation))
268 (notmuch-kill-this-buffer)
270 (funcall continuation))))
272 (defun notmuch-search-scroll-up ()
273 "Move forward through search results by one window's worth."
277 ((end-of-buffer) (notmuch-search-last-thread))))
279 (defun notmuch-search-scroll-down ()
280 "Move backward through the search results by one window's worth."
282 ;; I don't know why scroll-down doesn't signal beginning-of-buffer
283 ;; the way that scroll-up signals end-of-buffer, but c'est la vie.
285 ;; So instead of trapping a signal we instead check whether the
286 ;; window begins on the first line of the buffer and if so, move
287 ;; directly to that position. (We have to count lines since the
288 ;; window-start position is not the same as point-min due to the
289 ;; invisible thread-ID characters on the first line.
290 (if (equal (count-lines (point-min) (window-start)) 0)
291 (goto-char (point-min))
294 (defun notmuch-search-next-thread ()
295 "Select the next thread in the search results."
297 (when (notmuch-search-get-result)
298 (goto-char (notmuch-search-result-end))))
300 (defun notmuch-search-previous-thread ()
301 "Select the previous thread in the search results."
303 (if (notmuch-search-get-result)
305 (goto-char (notmuch-search-result-beginning (- (point) 1))))
306 ;; We must be past the end; jump to the last result
307 (notmuch-search-last-thread)))
309 (defun notmuch-search-last-thread ()
310 "Select the last thread in the search results."
312 (goto-char (point-max))
314 (let ((beg (notmuch-search-result-beginning)))
315 (when beg (goto-char beg))))
317 (defun notmuch-search-first-thread ()
318 "Select the first thread in the search results."
320 (goto-char (point-min)))
322 (defface notmuch-message-summary-face
323 '((((class color) (background light)) (:background "#f0f0f0"))
324 (((class color) (background dark)) (:background "#303030")))
325 "Face for the single-line message summary in notmuch-show-mode."
327 :group 'notmuch-faces)
329 (defface notmuch-search-date
330 '((t :inherit default))
331 "Face used in search mode for dates."
332 :group 'notmuch-search
333 :group 'notmuch-faces)
335 (defface notmuch-search-count
336 '((t :inherit default))
337 "Face used in search mode for the count matching the query."
338 :group 'notmuch-search
339 :group 'notmuch-faces)
341 (defface notmuch-search-subject
342 '((t :inherit default))
343 "Face used in search mode for subjects."
344 :group 'notmuch-search
345 :group 'notmuch-faces)
347 (defface notmuch-search-matching-authors
348 '((t :inherit default))
349 "Face used in search mode for authors matching the query."
350 :group 'notmuch-search
351 :group 'notmuch-faces)
353 (defface notmuch-search-non-matching-authors
356 (:foreground "grey30"))
359 (:foreground "grey60"))
362 "Face used in search mode for authors not matching the query."
363 :group 'notmuch-search
364 :group 'notmuch-faces)
366 (defface notmuch-tag-face
369 (:foreground "OliveDrab1"))
372 (:foreground "navy blue" :bold t))
375 "Face used in search mode face for tags."
376 :group 'notmuch-search
377 :group 'notmuch-faces)
379 (defun notmuch-search-mode ()
380 "Major mode displaying results of a notmuch search.
382 This buffer contains the results of a \"notmuch search\" of your
383 email archives. Each line in the buffer represents a single
384 thread giving a summary of the thread (a relative date, the
385 number of matched messages and total messages in the thread,
386 participants in the thread, a representative subject line, and
389 Pressing \\[notmuch-search-show-thread] on any line displays that thread. The '\\[notmuch-search-add-tag]' and '\\[notmuch-search-remove-tag]'
390 keys can be used to add or remove tags from a thread. The '\\[notmuch-search-archive-thread]' key
391 is a convenience for archiving a thread (removing the \"inbox\"
392 tag). The '\\[notmuch-search-tag-all]' key can be used to add and/or remove tags from all
393 messages (as opposed to threads) that match the current query. Use with caution, as this
394 will also tag matching messages that arrived *after* constructing the buffer.
396 Other useful commands are '\\[notmuch-search-filter]' for filtering the current search
397 based on an additional query string, '\\[notmuch-search-filter-by-tag]' for filtering to include
398 only messages with a given tag, and '\\[notmuch-search]' to execute a new, global
401 Complete list of currently available key bindings:
403 \\{notmuch-search-mode-map}"
405 (kill-all-local-variables)
406 (make-local-variable 'notmuch-search-query-string)
407 (make-local-variable 'notmuch-search-oldest-first)
408 (make-local-variable 'notmuch-search-target-thread)
409 (make-local-variable 'notmuch-search-target-line)
410 (set (make-local-variable 'notmuch-search-continuation) nil)
411 (set (make-local-variable 'scroll-preserve-screen-position) t)
412 (add-to-invisibility-spec (cons 'ellipsis t))
413 (use-local-map notmuch-search-mode-map)
414 (setq truncate-lines t)
415 (setq major-mode 'notmuch-search-mode
416 mode-name "notmuch-search")
417 (setq buffer-read-only t))
419 (defun notmuch-search-get-result (&optional pos)
420 "Return the result object for the thread at POS (or point).
422 If there is no thread at POS (or point), returns nil."
423 (get-text-property (or pos (point)) 'notmuch-search-result))
425 (defun notmuch-search-result-beginning (&optional pos)
426 "Return the point at the beginning of the thread at POS (or point).
428 If there is no thread at POS (or point), returns nil."
429 (when (notmuch-search-get-result pos)
430 ;; We pass 1+point because previous-single-property-change starts
431 ;; searching one before the position we give it.
432 (previous-single-property-change (1+ (or pos (point)))
433 'notmuch-search-result nil (point-min))))
435 (defun notmuch-search-result-end (&optional pos)
436 "Return the point at the end of the thread at POS (or point).
438 The returned point will be just after the newline character that
439 ends the result line. If there is no thread at POS (or point),
441 (when (notmuch-search-get-result pos)
442 (next-single-property-change (or pos (point)) 'notmuch-search-result
445 (defun notmuch-search-foreach-result (beg end function)
446 "Invoke FUNCTION for each result between BEG and END.
448 FUNCTION should take one argument. It will be applied to the
449 character position of the beginning of each result that overlaps
450 the region between points BEG and END. As a special case, if (=
451 BEG END), FUNCTION will be applied to the result containing point
454 (lexical-let ((pos (notmuch-search-result-beginning beg))
455 ;; End must be a marker in case function changes the
457 (end (copy-marker end))
458 ;; Make sure we examine at least one result, even if
461 ;; We have to be careful if the region extends beyond the results.
462 ;; In this case, pos could be null or there could be no result at
464 (while (and pos (or (< pos end) first))
465 (when (notmuch-search-get-result pos)
466 (funcall function pos))
467 (setq pos (notmuch-search-result-end pos)
469 ;; Unindent the function argument of notmuch-search-foreach-result so
470 ;; the indentation of callers doesn't get out of hand.
471 (put 'notmuch-search-foreach-result 'lisp-indent-function 2)
473 (defun notmuch-search-properties-in-region (property beg end)
475 (notmuch-search-foreach-result beg end
477 (push (plist-get (notmuch-search-get-result pos) property) output)))
480 (defun notmuch-search-find-thread-id (&optional bare)
481 "Return the thread for the current thread
483 If BARE is set then do not prefix with \"thread:\""
484 (let ((thread (plist-get (notmuch-search-get-result) :thread)))
485 (when thread (concat (unless bare "thread:") thread))))
487 (defun notmuch-search-find-thread-id-region (beg end)
488 "Return a list of threads for the current region"
489 (mapcar (lambda (thread) (concat "thread:" thread))
490 (notmuch-search-properties-in-region :thread beg end)))
492 (defun notmuch-search-find-thread-id-region-search (beg end)
493 "Return a search string for threads for the current region"
494 (mapconcat 'identity (notmuch-search-find-thread-id-region beg end) " or "))
496 (defun notmuch-search-find-authors ()
497 "Return the authors for the current thread"
498 (plist-get (notmuch-search-get-result) :authors))
500 (defun notmuch-search-find-authors-region (beg end)
501 "Return a list of authors for the current region"
502 (notmuch-search-properties-in-region :authors beg end))
504 (defun notmuch-search-find-subject ()
505 "Return the subject for the current thread"
506 (plist-get (notmuch-search-get-result) :subject))
508 (defun notmuch-search-find-subject-region (beg end)
509 "Return a list of authors for the current region"
510 (notmuch-search-properties-in-region :subject beg end))
512 (defun notmuch-search-show-thread ()
513 "Display the currently selected thread."
515 (let ((thread-id (notmuch-search-find-thread-id))
516 (subject (notmuch-search-find-subject)))
517 (if (> (length thread-id) 0)
518 (notmuch-show thread-id
520 notmuch-search-query-string
521 ;; Name the buffer based on the subject.
522 (concat "*" (truncate-string-to-width subject 30 nil nil t) "*"))
523 (message "End of search results."))))
525 (defun notmuch-search-reply-to-thread (&optional prompt-for-sender)
526 "Begin composing a reply-all to the entire current thread in a new buffer."
528 (let ((message-id (notmuch-search-find-thread-id)))
529 (notmuch-mua-new-reply message-id prompt-for-sender t)))
531 (defun notmuch-search-reply-to-thread-sender (&optional prompt-for-sender)
532 "Begin composing a reply to the entire current thread in a new buffer."
534 (let ((message-id (notmuch-search-find-thread-id)))
535 (notmuch-mua-new-reply message-id prompt-for-sender nil)))
537 (defun notmuch-call-notmuch-process (&rest args)
538 "Synchronously invoke \"notmuch\" with the given list of arguments.
540 If notmuch exits with a non-zero status, output from the process
541 will appear in a buffer named \"*Notmuch errors*\" and an error
544 (let ((status (apply #'call-process notmuch-command nil t nil args)))
545 (notmuch-check-exit-status status (cons notmuch-command args)
548 (defun notmuch-search-set-tags (tags &optional pos)
549 (let ((new-result (plist-put (notmuch-search-get-result pos) :tags tags)))
550 (notmuch-search-update-result new-result pos)))
552 (defun notmuch-search-get-tags (&optional pos)
553 (plist-get (notmuch-search-get-result pos) :tags))
555 (defun notmuch-search-get-tags-region (beg end)
557 (notmuch-search-foreach-result beg end
559 (setq output (append output (notmuch-search-get-tags pos)))))
562 (defun notmuch-search-tag-region (beg end &optional tag-changes)
563 "Change tags for threads in the given region."
564 (let ((search-string (notmuch-search-find-thread-id-region-search beg end)))
565 (setq tag-changes (funcall 'notmuch-tag search-string tag-changes))
566 (notmuch-search-foreach-result beg end
568 (notmuch-search-set-tags
569 (notmuch-update-tags (notmuch-search-get-tags pos) tag-changes)
572 (defun notmuch-search-tag (&optional tag-changes)
573 "Change tags for the currently selected thread or region.
575 See `notmuch-tag' for information on the format of TAG-CHANGES."
577 (let* ((beg (if (region-active-p) (region-beginning) (point)))
578 (end (if (region-active-p) (region-end) (point))))
579 (funcall 'notmuch-search-tag-region beg end tag-changes)))
581 (defun notmuch-search-add-tag ()
582 "Same as `notmuch-search-tag' but sets initial input to '+'."
584 (notmuch-search-tag "+"))
586 (defun notmuch-search-remove-tag ()
587 "Same as `notmuch-search-tag' but sets initial input to '-'."
589 (notmuch-search-tag "-"))
591 (defun notmuch-search-archive-thread (&optional unarchive)
592 "Archive the currently selected thread.
594 Archive each message in the currently selected thread by applying
595 the tag changes in `notmuch-archive-tags' to each (remove the
596 \"inbox\" tag by default). If a prefix argument is given, the
597 messages will be \"unarchived\" (i.e. the tag changes in
598 `notmuch-archive-tags' will be reversed).
600 This function advances the next thread when finished."
602 (when notmuch-archive-tags
604 (notmuch-tag-change-list notmuch-archive-tags unarchive)))
605 (notmuch-search-next-thread))
607 (defun notmuch-search-update-result (result &optional pos)
608 "Replace the result object of the thread at POS (or point) by
609 RESULT and redraw it.
611 This will keep point in a reasonable location. However, if there
612 are enclosing save-excursions and the saved point is in the
613 result being updated, the point will be restored to the beginning
615 (let ((start (notmuch-search-result-beginning pos))
616 (end (notmuch-search-result-end pos))
618 (inhibit-read-only t))
619 ;; Delete the current thread
620 (delete-region start end)
621 ;; Insert the updated thread
622 (notmuch-search-show-result result start)
623 ;; If point was inside the old result, make an educated guess
624 ;; about where to place it now. Unfortunately, this won't work
625 ;; with save-excursion (or any other markers that would be nice to
626 ;; preserve, such as the window start), but there's nothing we can
627 ;; do about that without a way to retrieve markers in a region.
628 (when (and (>= init-point start) (<= init-point end))
629 (let* ((new-end (notmuch-search-result-end start))
630 (new-point (if (= init-point end)
632 (min init-point (- new-end 1)))))
633 (goto-char new-point)))))
635 (defun notmuch-search-process-sentinel (proc msg)
636 "Add a message to let user know when \"notmuch search\" exits"
637 (let ((buffer (process-buffer proc))
638 (status (process-status proc))
639 (exit-status (process-exit-status proc))
640 (never-found-target-thread nil))
641 (when (memq status '(exit signal))
643 (kill-buffer (process-get proc 'parse-buf))
644 (if (buffer-live-p buffer)
645 (with-current-buffer buffer
647 (let ((inhibit-read-only t)
649 (goto-char (point-max))
650 (if (eq status 'signal)
651 (insert "Incomplete search results (search process was killed).\n"))
652 (when (eq status 'exit)
653 (insert "End of search results.\n")
654 ;; For version mismatch, there's no point in
655 ;; showing the search buffer
656 (when (or (= exit-status 20) (= exit-status 21))
660 (not (string= notmuch-search-target-thread "found")))
661 (set 'never-found-target-thread t)))))
662 (when (and never-found-target-thread
663 notmuch-search-target-line)
664 (goto-char (point-min))
665 (forward-line (1- notmuch-search-target-line)))))))))
667 (defcustom notmuch-search-line-faces '(("unread" :weight bold)
668 ("flagged" :foreground "blue"))
669 "Tag/face mapping for line highlighting in notmuch-search.
671 Here is an example of how to color search results based on tags.
672 (the following text would be placed in your ~/.emacs file):
674 (setq notmuch-search-line-faces '((\"deleted\" . (:foreground \"red\"
675 :background \"blue\"))
676 (\"unread\" . (:foreground \"green\"))))
678 The attributes defined for matching tags are merged, with later
679 attributes overriding earlier. A message having both \"deleted\"
680 and \"unread\" tags with the above settings would have a green
681 foreground and blue background."
682 :type '(alist :key-type (string) :value-type (custom-face-edit))
683 :group 'notmuch-search
684 :group 'notmuch-faces)
686 (defun notmuch-search-color-line (start end line-tag-list)
687 "Colorize lines in `notmuch-show' based on tags."
689 (let ((tag (car elem))
690 (attributes (cdr elem)))
691 (when (member tag line-tag-list)
692 (notmuch-combine-face-text-property start end attributes))))
693 ;; Reverse the list so earlier entries take precedence
694 (reverse notmuch-search-line-faces)))
696 (defun notmuch-search-author-propertize (authors)
697 "Split `authors' into matching and non-matching authors and
698 propertize appropriately. If no boundary between authors and
699 non-authors is found, assume that all of the authors match."
700 (if (string-match "\\(.*\\)|\\(.*\\)" authors)
701 (concat (propertize (concat (match-string 1 authors) ",")
702 'face 'notmuch-search-matching-authors)
703 (propertize (match-string 2 authors)
704 'face 'notmuch-search-non-matching-authors))
705 (propertize authors 'face 'notmuch-search-matching-authors)))
707 (defun notmuch-search-insert-authors (format-string authors)
708 ;; Save the match data to avoid interfering with
709 ;; `notmuch-search-process-filter'.
711 (let* ((formatted-authors (format format-string authors))
712 (formatted-sample (format format-string ""))
713 (visible-string formatted-authors)
714 (invisible-string "")
717 ;; Truncate the author string to fit the specification.
718 (if (> (length formatted-authors)
719 (length formatted-sample))
720 (let ((visible-length (- (length formatted-sample)
722 ;; Truncate the visible string according to the width of
723 ;; the display string.
724 (setq visible-string (substring formatted-authors 0 visible-length)
725 invisible-string (substring formatted-authors visible-length))
726 ;; If possible, truncate the visible string at a natural
727 ;; break (comma or pipe), as incremental search doesn't
728 ;; match across the visible/invisible border.
729 (when (string-match "\\(.*\\)\\([,|] \\)\\([^,|]*\\)" visible-string)
730 ;; Second clause is destructive on `visible-string', so
731 ;; order is important.
732 (setq invisible-string (concat (match-string 3 visible-string)
734 visible-string (concat (match-string 1 visible-string)
735 (match-string 2 visible-string))))
736 ;; `visible-string' may be shorter than the space allowed
737 ;; by `format-string'. If so we must insert some padding
738 ;; after `invisible-string'.
739 (setq padding (make-string (- (length formatted-sample)
740 (length visible-string)
744 ;; Use different faces to show matching and non-matching authors.
745 (if (string-match "\\(.*\\)|\\(.*\\)" visible-string)
746 ;; The visible string contains both matching and
747 ;; non-matching authors.
748 (setq visible-string (notmuch-search-author-propertize visible-string)
749 ;; The invisible string must contain only non-matching
750 ;; authors, as the visible-string contains both.
751 invisible-string (propertize invisible-string
752 'face 'notmuch-search-non-matching-authors))
753 ;; The visible string contains only matching authors.
754 (setq visible-string (propertize visible-string
755 'face 'notmuch-search-matching-authors)
756 ;; The invisible string may contain both matching and
757 ;; non-matching authors.
758 invisible-string (notmuch-search-author-propertize invisible-string)))
760 ;; If there is any invisible text, add it as a tooltip to the
762 (when (not (string= invisible-string ""))
763 (setq visible-string (propertize visible-string 'help-echo (concat "..." invisible-string))))
765 ;; Insert the visible and, if present, invisible author strings.
766 (insert visible-string)
767 (when (not (string= invisible-string ""))
768 (let ((start (point))
770 (insert invisible-string)
771 (setq overlay (make-overlay start (point)))
772 (overlay-put overlay 'invisible 'ellipsis)
773 (overlay-put overlay 'isearch-open-invisible #'delete-overlay)))
776 (defun notmuch-search-insert-field (field format-string result)
778 ((string-equal field "date")
779 (insert (propertize (format format-string (plist-get result :date_relative))
780 'face 'notmuch-search-date)))
781 ((string-equal field "count")
782 (insert (propertize (format format-string
783 (format "[%s/%s]" (plist-get result :matched)
784 (plist-get result :total)))
785 'face 'notmuch-search-count)))
786 ((string-equal field "subject")
787 (insert (propertize (format format-string (plist-get result :subject))
788 'face 'notmuch-search-subject)))
790 ((string-equal field "authors")
791 (notmuch-search-insert-authors format-string (plist-get result :authors)))
793 ((string-equal field "tags")
794 (let ((tags (plist-get result :tags)))
795 (insert (format format-string (notmuch-tag-format-tags tags)))))))
797 (defun notmuch-search-show-result (result &optional pos)
798 "Insert RESULT at POS or the end of the buffer if POS is null."
799 ;; Ignore excluded matches
800 (unless (= (plist-get result :matched) 0)
801 (let ((beg (or pos (point-max))))
804 (dolist (spec notmuch-search-result-format)
805 (notmuch-search-insert-field (car spec) (cdr spec) result))
807 (notmuch-search-color-line beg (point) (plist-get result :tags))
808 (put-text-property beg (point) 'notmuch-search-result result))
809 (when (string= (plist-get result :thread) notmuch-search-target-thread)
810 (setq notmuch-search-target-thread "found")
813 (defun notmuch-search-process-filter (proc string)
814 "Process and filter the output of \"notmuch search\""
815 (let ((results-buf (process-buffer proc))
816 (parse-buf (process-get proc 'parse-buf))
817 (inhibit-read-only t)
819 (if (not (buffer-live-p results-buf))
820 (delete-process proc)
821 (with-current-buffer parse-buf
824 (goto-char (point-max))
826 (notmuch-sexp-parse-partial-list 'notmuch-search-show-result
829 (defun notmuch-search-tag-all (&optional tag-changes)
830 "Add/remove tags from all messages in current search buffer.
832 See `notmuch-tag' for information on the format of TAG-CHANGES."
834 (apply 'notmuch-tag notmuch-search-query-string tag-changes))
836 (defun notmuch-search-buffer-title (query)
837 "Returns the title for a buffer with notmuch search results."
841 (loop for tuple in notmuch-saved-searches
842 if (let ((quoted-query (regexp-quote (cdr tuple))))
843 (and (string-match (concat "^" quoted-query) query)
844 (> (length (match-string 0 query))
846 do (setq longest tuple))
848 (saved-search-name (car saved-search))
849 (saved-search-query (cdr saved-search)))
850 (cond ((and saved-search (equal saved-search-query query))
851 ;; Query is the same as saved search (ignoring case)
852 (concat "*notmuch-saved-search-" saved-search-name "*"))
854 (concat "*notmuch-search-"
855 (replace-regexp-in-string (concat "^" (regexp-quote saved-search-query))
856 (concat "[ " saved-search-name " ]")
860 (concat "*notmuch-search-" query "*"))
863 (defun notmuch-read-query (prompt)
864 "Read a notmuch-query from the minibuffer with completion.
866 PROMPT is the string to prompt with."
869 (append (list "folder:" "thread:" "id:" "date:" "from:" "to:"
870 "subject:" "attachment:")
871 (mapcar (lambda (tag)
872 (concat "tag:" (notmuch-escape-boolean-term tag)))
873 (process-lines notmuch-command "search" "--output=tags" "*")))))
874 (let ((keymap (copy-keymap minibuffer-local-map))
875 (minibuffer-completion-table
876 (completion-table-dynamic
878 ;; generate a list of possible completions for the current input
880 ;; this ugly regexp is used to get the last word of the input
881 ;; possibly preceded by a '('
882 ((string-match "\\(^\\|.* (?\\)\\([^ ]*\\)$" string)
883 (mapcar (lambda (compl)
884 (concat (match-string-no-properties 1 string) compl))
885 (all-completions (match-string-no-properties 2 string)
887 (t (list string)))))))
888 ;; this was simpler than convincing completing-read to accept spaces:
889 (define-key keymap (kbd "TAB") 'minibuffer-complete)
890 (let ((history-delete-duplicates t))
891 (read-from-minibuffer prompt nil keymap nil
892 'notmuch-search-history nil nil)))))
895 (defun notmuch-search (&optional query oldest-first target-thread target-line continuation)
896 "Run \"notmuch search\" with the given `query' and display results.
898 If `query' is nil, it is read interactively from the minibuffer.
899 Other optional parameters are used as follows:
901 oldest-first: A Boolean controlling the sort order of returned threads
902 target-thread: A thread ID (without the thread: prefix) that will be made
903 current if it appears in the search results.
904 target-line: The line number to move to if the target thread does not
905 appear in the search results."
908 (setq query (notmuch-read-query "Notmuch search: ")))
909 (let ((buffer (get-buffer-create (notmuch-search-buffer-title query))))
910 (switch-to-buffer buffer)
911 (notmuch-search-mode)
912 ;; Don't track undo information for this buffer
913 (set 'buffer-undo-list t)
914 (set 'notmuch-search-query-string query)
915 (set 'notmuch-search-oldest-first oldest-first)
916 (set 'notmuch-search-target-thread target-thread)
917 (set 'notmuch-search-target-line target-line)
918 (set 'notmuch-search-continuation continuation)
919 (let ((proc (get-buffer-process (current-buffer)))
920 (inhibit-read-only t))
922 (error "notmuch search process already running for query `%s'" query)
925 (goto-char (point-min))
927 (let ((proc (notmuch-start-notmuch
928 "notmuch-search" buffer #'notmuch-search-process-sentinel
929 "search" "--format=sexp" "--format-version=1"
931 "--sort=oldest-first"
932 "--sort=newest-first")
934 ;; Use a scratch buffer to accumulate partial output.
935 ;; This buffer will be killed by the sentinel, which
936 ;; should be called no matter how the process dies.
937 (parse-buf (generate-new-buffer " *notmuch search parse*")))
938 (process-put proc 'parse-buf parse-buf)
939 (set-process-filter proc 'notmuch-search-process-filter)
940 (set-process-query-on-exit-flag proc nil))))
941 (run-hooks 'notmuch-search-hook)))
943 (defun notmuch-search-refresh-view ()
944 "Refresh the current view.
946 Kills the current buffer and runs a new search with the same
947 query string as the current search. If the current thread is in
948 the new search results, then point will be placed on the same
949 thread. Otherwise, point will be moved to attempt to be in the
950 same relative position within the new buffer."
952 (let ((target-line (line-number-at-pos))
953 (oldest-first notmuch-search-oldest-first)
954 (target-thread (notmuch-search-find-thread-id 'bare))
955 (query notmuch-search-query-string)
956 (continuation notmuch-search-continuation))
957 (notmuch-kill-this-buffer)
958 (notmuch-search query oldest-first target-thread target-line continuation)
959 (goto-char (point-min))))
961 (defcustom notmuch-poll-script nil
962 "An external script to incorporate new mail into the notmuch database.
964 This variable controls the action invoked by
965 `notmuch-search-poll-and-refresh-view' and
966 `notmuch-hello-poll-and-update' (each have a default keybinding
967 of 'G') to incorporate new mail into the notmuch database.
969 If set to nil (the default), new mail is processed by invoking
970 \"notmuch new\". Otherwise, this should be set to a string that
971 gives the name of an external script that processes new mail. If
972 set to the empty string, no command will be run.
974 The external script could do any of the following depending on
977 1. Invoke a program to transfer mail to the local mail store
978 2. Invoke \"notmuch new\" to incorporate the new mail
979 3. Invoke one or more \"notmuch tag\" commands to classify the mail
981 Note that the recommended way of achieving the same is using
982 \"notmuch new\" hooks."
983 :type '(choice (const :tag "notmuch new" nil)
984 (const :tag "Disabled" "")
985 (string :tag "Custom script"))
986 :group 'notmuch-external)
988 (defun notmuch-poll ()
989 "Run \"notmuch new\" or an external script to import mail.
991 Invokes `notmuch-poll-script', \"notmuch new\", or does nothing
992 depending on the value of `notmuch-poll-script'."
994 (if (stringp notmuch-poll-script)
995 (unless (string= notmuch-poll-script "")
996 (call-process notmuch-poll-script nil nil))
997 (call-process notmuch-command nil nil nil "new")))
999 (defun notmuch-search-poll-and-refresh-view ()
1000 "Invoke `notmuch-poll' to import mail, then refresh the current view."
1003 (notmuch-search-refresh-view))
1005 (defun notmuch-search-toggle-order ()
1006 "Toggle the current search order.
1008 By default, the \"inbox\" view created by `notmuch' is displayed
1009 in chronological order (oldest thread at the beginning of the
1010 buffer), while any global searches created by `notmuch-search'
1011 are displayed in reverse-chronological order (newest thread at
1012 the beginning of the buffer).
1014 This command toggles the sort order for the current search.
1016 Note that any filtered searches created by
1017 `notmuch-search-filter' retain the search order of the parent
1020 (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
1021 (notmuch-search-refresh-view))
1023 (defun notmuch-search-filter (query)
1024 "Filter the current search results based on an additional query string.
1026 Runs a new search matching only messages that match both the
1027 current search results AND the additional query string provided."
1028 (interactive (list (notmuch-read-query "Filter search: ")))
1029 (let ((grouped-query (if (string-match-p notmuch-search-disjunctive-regexp query)
1030 (concat "( " query " )")
1032 (notmuch-search (if (string= notmuch-search-query-string "*")
1034 (concat notmuch-search-query-string " and " grouped-query)) notmuch-search-oldest-first)))
1036 (defun notmuch-search-filter-by-tag (tag)
1037 "Filter the current search results based on a single tag.
1039 Runs a new search matching only messages that match both the
1040 current search results AND that are tagged with the given tag."
1042 (list (notmuch-select-tag-with-completion "Filter by tag: ")))
1043 (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
1047 "Run notmuch and display saved searches, known tags, etc."
1051 (defun notmuch-interesting-buffer (b)
1052 "Is the current buffer of interest to a notmuch user?"
1053 (with-current-buffer b
1054 (memq major-mode '(notmuch-show-mode
1060 (defun notmuch-cycle-notmuch-buffers ()
1061 "Cycle through any existing notmuch buffers (search, show or hello).
1063 If the current buffer is the only notmuch buffer, bury it. If no
1064 notmuch buffers exist, run `notmuch'."
1068 ;; If the current buffer is a notmuch buffer, remember it and then
1070 (when (notmuch-interesting-buffer (current-buffer))
1071 (setq start (current-buffer))
1074 ;; Find the first notmuch buffer.
1075 (setq first (loop for buffer in (buffer-list)
1076 if (notmuch-interesting-buffer buffer)
1080 ;; If the first one we found is any other than the starting
1081 ;; buffer, switch to it.
1082 (unless (eq first start)
1083 (switch-to-buffer first))
1086 (setq mail-user-agent 'notmuch-user-agent)