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 <https://www.gnu.org/licenses/>.
20 ;; Authors: Carl Worth <cworth@cworth.org>
21 ;; Homepage: https://notmuchmail.org/
25 ;; This is an emacs-based interface to the notmuch mail system.
27 ;; You will first need to have the notmuch program installed and have a
28 ;; notmuch database built in order to use this. See
29 ;; https://notmuchmail.org for details.
31 ;; To install this software, copy it to a directory that is on the
32 ;; `load-path' variable within emacs (a good candidate is
33 ;; /usr/local/share/emacs/site-lisp). If you are viewing this from the
34 ;; notmuch source distribution then you can simply run:
36 ;; sudo make install-emacs
40 ;; Then, to actually run it, add:
42 ;; (autoload 'notmuch "notmuch" "Notmuch mail" t)
44 ;; to your ~/.emacs file, and then run "M-x notmuch" from within emacs,
49 ;; Have fun, and let us know if you have any comment, questions, or
50 ;; kudos: Notmuch list <notmuch@notmuchmail.org> (subscription is not
51 ;; required, but is available from https://notmuchmail.org).
53 ;; Note for MELPA users (and others tracking the development version
56 ;; This emacs package needs a fairly closely matched version of the
57 ;; notmuch program. If you use the MELPA version of notmuch.el (as
58 ;; opposed to MELPA stable), you should be prepared to track the
59 ;; master development branch (i.e. build from git) for the notmuch
60 ;; program as well. Upgrading notmuch-emacs too far beyond the notmuch
61 ;; program can CAUSE YOUR EMAIL TO STOP WORKING.
63 ;; TL;DR: notmuch-emacs from MELPA and notmuch from distro packages is
68 (eval-when-compile (require 'cl))
72 (require 'notmuch-lib)
73 (require 'notmuch-tag)
74 (require 'notmuch-show)
75 (require 'notmuch-tree)
76 (require 'notmuch-mua)
77 (require 'notmuch-hello)
78 (require 'notmuch-maildir-fcc)
79 (require 'notmuch-message)
80 (require 'notmuch-parser)
82 (defcustom notmuch-search-result-format
85 ("authors" . "%-20s ")
88 "Search result formatting. Supported fields are:
89 date, count, authors, subject, tags
91 (setq notmuch-search-result-format \(\(\"authors\" . \"%-40s\"\)
92 \(\"subject\" . \"%s\"\)\)\)
93 Line breaks are permitted in format strings (though this is
94 currently experimental). Note that a line break at the end of an
95 \"authors\" field will get elided if the authors list is long;
96 place it instead at the beginning of the following field. To
97 enter a line break when setting this variable with setq, use \\n.
98 To enter a line break in customize, press \\[quoted-insert] C-j."
99 :type '(alist :key-type (string) :value-type (string))
100 :group 'notmuch-search)
102 ;; The name of this variable `notmuch-init-file' is consistent with the
103 ;; convention used in e.g. emacs and gnus. The value, `notmuch-config[.el[c]]'
104 ;; is consistent with notmuch cli configuration file `~/.notmuch-config'.
105 (defcustom notmuch-init-file (locate-user-emacs-file "notmuch-config")
106 "Your Notmuch Emacs-Lisp configuration file name.
107 If a file with one of the suffixes defined by `get-load-suffixes' exists,
108 it will be read instead.
109 This file is read once when notmuch is loaded; the notmuch hooks added
110 there will be called at other points of notmuch execution."
114 (defvar notmuch-query-history nil
115 "Variable to store minibuffer history for notmuch queries")
117 (defun notmuch-foreach-mime-part (function mm-handle)
118 (cond ((stringp (car mm-handle))
119 (dolist (part (cdr mm-handle))
120 (notmuch-foreach-mime-part function part)))
121 ((bufferp (car mm-handle))
122 (funcall function mm-handle))
123 (t (dolist (part mm-handle)
124 (notmuch-foreach-mime-part function part)))))
126 (defun notmuch-count-attachments (mm-handle)
128 (notmuch-foreach-mime-part
130 (let ((disposition (mm-handle-disposition p)))
131 (and (listp disposition)
132 (or (equal (car disposition) "attachment")
133 (and (equal (car disposition) "inline")
134 (assq 'filename disposition)))
139 (defun notmuch-save-attachments (mm-handle &optional queryp)
140 (notmuch-foreach-mime-part
142 (let ((disposition (mm-handle-disposition p)))
143 (and (listp disposition)
144 (or (equal (car disposition) "attachment")
145 (and (equal (car disposition) "inline")
146 (assq 'filename disposition)))
149 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
155 (defun notmuch-hl-line-mode ()
156 (prog1 (hl-line-mode)
157 (when hl-line-overlay
158 (overlay-put hl-line-overlay 'priority 1))))
160 (defcustom notmuch-search-hook '(notmuch-hl-line-mode)
161 "List of functions to call when notmuch displays the search results."
163 :options '(notmuch-hl-line-mode)
164 :group 'notmuch-search
165 :group 'notmuch-hooks)
167 (defvar notmuch-search-mode-map
168 (let ((map (make-sparse-keymap)))
169 (set-keymap-parent map notmuch-common-keymap)
170 (define-key map "x" 'notmuch-bury-or-kill-this-buffer)
171 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
172 (define-key map "b" 'notmuch-search-scroll-down)
173 (define-key map " " 'notmuch-search-scroll-up)
174 (define-key map "<" 'notmuch-search-first-thread)
175 (define-key map ">" 'notmuch-search-last-thread)
176 (define-key map "p" 'notmuch-search-previous-thread)
177 (define-key map "n" 'notmuch-search-next-thread)
178 (define-key map "r" 'notmuch-search-reply-to-thread-sender)
179 (define-key map "R" 'notmuch-search-reply-to-thread)
180 (define-key map "o" 'notmuch-search-toggle-order)
181 (define-key map "c" 'notmuch-search-stash-map)
182 (define-key map "t" 'notmuch-search-filter-by-tag)
183 (define-key map "l" 'notmuch-search-filter)
184 (define-key map [mouse-1] 'notmuch-search-show-thread)
185 (define-key map "k" 'notmuch-tag-jump)
186 (define-key map "*" 'notmuch-search-tag-all)
187 (define-key map "a" 'notmuch-search-archive-thread)
188 (define-key map "-" 'notmuch-search-remove-tag)
189 (define-key map "+" 'notmuch-search-add-tag)
190 (define-key map (kbd "RET") 'notmuch-search-show-thread)
191 (define-key map (kbd "M-RET") 'notmuch-tree-from-search-thread)
192 (define-key map "Z" 'notmuch-tree-from-search-current-query)
193 (define-key map "U" 'notmuch-unthreaded-from-search-current-query)
195 "Keymap for \"notmuch search\" buffers.")
196 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
198 (defvar notmuch-search-stash-map
199 (let ((map (make-sparse-keymap)))
200 (define-key map "i" 'notmuch-search-stash-thread-id)
201 (define-key map "q" 'notmuch-stash-query)
202 (define-key map "?" 'notmuch-subkeymap-help)
204 "Submap for stash commands")
205 (fset 'notmuch-search-stash-map notmuch-search-stash-map)
207 (defun notmuch-search-stash-thread-id ()
208 "Copy thread ID of current thread to kill-ring."
210 (notmuch-common-do-stash (notmuch-search-find-thread-id)))
212 (defun notmuch-stash-query ()
213 "Copy current query to kill-ring."
215 (notmuch-common-do-stash (notmuch-search-get-query)))
217 (defvar notmuch-search-query-string)
218 (defvar notmuch-search-target-thread)
219 (defvar notmuch-search-target-line)
221 (defvar notmuch-search-disjunctive-regexp "\\<[oO][rR]\\>")
223 (defun notmuch-search-scroll-up ()
224 "Move forward through search results by one window's worth."
228 ((end-of-buffer) (notmuch-search-last-thread))))
230 (defun notmuch-search-scroll-down ()
231 "Move backward through the search results by one window's worth."
233 ;; I don't know why scroll-down doesn't signal beginning-of-buffer
234 ;; the way that scroll-up signals end-of-buffer, but c'est la vie.
236 ;; So instead of trapping a signal we instead check whether the
237 ;; window begins on the first line of the buffer and if so, move
238 ;; directly to that position. (We have to count lines since the
239 ;; window-start position is not the same as point-min due to the
240 ;; invisible thread-ID characters on the first line.
241 (if (equal (count-lines (point-min) (window-start)) 0)
242 (goto-char (point-min))
245 (defun notmuch-search-next-thread ()
246 "Select the next thread in the search results."
248 (when (notmuch-search-get-result)
249 (goto-char (notmuch-search-result-end))))
251 (defun notmuch-search-previous-thread ()
252 "Select the previous thread in the search results."
254 (if (notmuch-search-get-result)
256 (goto-char (notmuch-search-result-beginning (- (point) 1))))
257 ;; We must be past the end; jump to the last result
258 (notmuch-search-last-thread)))
260 (defun notmuch-search-last-thread ()
261 "Select the last thread in the search results."
263 (goto-char (point-max))
265 (let ((beg (notmuch-search-result-beginning)))
266 (when beg (goto-char beg))))
268 (defun notmuch-search-first-thread ()
269 "Select the first thread in the search results."
271 (goto-char (point-min)))
273 (defface notmuch-message-summary-face
274 '((((class color) (background light)) (:background "#f0f0f0"))
275 (((class color) (background dark)) (:background "#303030")))
276 "Face for the single-line message summary in notmuch-show-mode."
278 :group 'notmuch-faces)
280 (defface notmuch-search-date
281 '((t :inherit default))
282 "Face used in search mode for dates."
283 :group 'notmuch-search
284 :group 'notmuch-faces)
286 (defface notmuch-search-count
287 '((t :inherit default))
288 "Face used in search mode for the count matching the query."
289 :group 'notmuch-search
290 :group 'notmuch-faces)
292 (defface notmuch-search-subject
293 '((t :inherit default))
294 "Face used in search mode for subjects."
295 :group 'notmuch-search
296 :group 'notmuch-faces)
298 (defface notmuch-search-matching-authors
299 '((t :inherit default))
300 "Face used in search mode for authors matching the query."
301 :group 'notmuch-search
302 :group 'notmuch-faces)
304 (defface notmuch-search-non-matching-authors
307 (:foreground "grey30"))
310 (:foreground "grey60"))
313 "Face used in search mode for authors not matching the query."
314 :group 'notmuch-search
315 :group 'notmuch-faces)
317 (defface notmuch-tag-face
320 (:foreground "OliveDrab1"))
323 (:foreground "navy blue" :bold t))
326 "Face used in search mode face for tags."
327 :group 'notmuch-search
328 :group 'notmuch-faces)
330 (defface notmuch-search-flagged-face
333 (:foreground "LightBlue1"))
336 (:foreground "blue")))
337 "Face used in search mode face for flagged threads.
339 This face is the default value for the \"flagged\" tag in
340 `notmuch-search-line-faces`."
341 :group 'notmuch-search
342 :group 'notmuch-faces)
344 (defface notmuch-search-unread-face
347 "Face used in search mode for unread threads.
349 This face is the default value for the \"unread\" tag in
350 `notmuch-search-line-faces`."
351 :group 'notmuch-search
352 :group 'notmuch-faces)
354 (define-derived-mode notmuch-search-mode fundamental-mode "notmuch-search"
355 "Major mode displaying results of a notmuch search.
357 This buffer contains the results of a \"notmuch search\" of your
358 email archives. Each line in the buffer represents a single
359 thread giving a summary of the thread (a relative date, the
360 number of matched messages and total messages in the thread,
361 participants in the thread, a representative subject line, and
364 Pressing \\[notmuch-search-show-thread] on any line displays that
365 thread. The '\\[notmuch-search-add-tag]' and
366 '\\[notmuch-search-remove-tag]' keys can be used to add or remove
367 tags from a thread. The '\\[notmuch-search-archive-thread]' key
368 is a convenience for archiving a thread (applying changes in
369 `notmuch-archive-tags'). The '\\[notmuch-search-tag-all]' key can
370 be used to add and/or remove tags from all messages (as opposed
371 to threads) that match the current query. Use with caution, as
372 this will also tag matching messages that arrived *after*
373 constructing the buffer.
375 Other useful commands are '\\[notmuch-search-filter]' for
376 filtering the current search based on an additional query string,
377 '\\[notmuch-search-filter-by-tag]' for filtering to include only
378 messages with a given tag, and '\\[notmuch-search]' to execute a
381 Complete list of currently available key bindings:
383 \\{notmuch-search-mode-map}"
384 (make-local-variable 'notmuch-search-query-string)
385 (make-local-variable 'notmuch-search-oldest-first)
386 (make-local-variable 'notmuch-search-target-thread)
387 (make-local-variable 'notmuch-search-target-line)
388 (setq notmuch-buffer-refresh-function #'notmuch-search-refresh-view)
389 (set (make-local-variable 'scroll-preserve-screen-position) t)
390 (add-to-invisibility-spec (cons 'ellipsis t))
391 (setq truncate-lines t)
392 (setq buffer-read-only t)
393 (setq imenu-prev-index-position-function
394 #'notmuch-search-imenu-prev-index-position-function)
395 (setq imenu-extract-index-name-function
396 #'notmuch-search-imenu-extract-index-name-function))
398 (defun notmuch-search-get-result (&optional pos)
399 "Return the result object for the thread at POS (or point).
401 If there is no thread at POS (or point), returns nil."
402 (get-text-property (or pos (point)) 'notmuch-search-result))
404 (defun notmuch-search-result-beginning (&optional pos)
405 "Return the point at the beginning of the thread at POS (or point).
407 If there is no thread at POS (or point), returns nil."
408 (when (notmuch-search-get-result pos)
409 ;; We pass 1+point because previous-single-property-change starts
410 ;; searching one before the position we give it.
411 (previous-single-property-change (1+ (or pos (point)))
412 'notmuch-search-result nil (point-min))))
414 (defun notmuch-search-result-end (&optional pos)
415 "Return the point at the end of the thread at POS (or point).
417 The returned point will be just after the newline character that
418 ends the result line. If there is no thread at POS (or point),
420 (when (notmuch-search-get-result pos)
421 (next-single-property-change (or pos (point)) 'notmuch-search-result
424 (defun notmuch-search-foreach-result (beg end fn)
425 "Invoke FN for each result between BEG and END.
427 FN should take one argument. It will be applied to the
428 character position of the beginning of each result that overlaps
429 the region between points BEG and END. As a special case, if (=
430 BEG END), FN will be applied to the result containing point
433 (lexical-let ((pos (notmuch-search-result-beginning beg))
434 ;; End must be a marker in case fn changes the
436 (end (copy-marker end))
437 ;; Make sure we examine at least one result, even if
440 ;; We have to be careful if the region extends beyond the results.
441 ;; In this case, pos could be null or there could be no result at
443 (while (and pos (or (< pos end) first))
444 (when (notmuch-search-get-result pos)
446 (setq pos (notmuch-search-result-end pos)
448 ;; Unindent the function argument of notmuch-search-foreach-result so
449 ;; the indentation of callers doesn't get out of hand.
450 (put 'notmuch-search-foreach-result 'lisp-indent-function 2)
452 (defun notmuch-search-properties-in-region (property beg end)
454 (notmuch-search-foreach-result beg end
456 (push (plist-get (notmuch-search-get-result pos) property) output)))
459 (defun notmuch-search-find-thread-id (&optional bare)
460 "Return the thread for the current thread
462 If BARE is set then do not prefix with \"thread:\""
463 (let ((thread (plist-get (notmuch-search-get-result) :thread)))
464 (when thread (concat (unless bare "thread:") thread))))
466 (defun notmuch-search-find-stable-query ()
467 "Return the stable queries for the current thread.
469 This returns a list (MATCHED-QUERY UNMATCHED-QUERY) for the
470 matched and unmatched messages in the current thread."
471 (plist-get (notmuch-search-get-result) :query))
473 (defun notmuch-search-find-stable-query-region (beg end &optional only-matched)
474 "Return the stable query for the current region.
476 If ONLY-MATCHED is non-nil, include only matched messages. If it
477 is nil, include both matched and unmatched messages. If there are
478 no messages in the region then return nil."
479 (let ((query-list nil) (all (not only-matched)))
480 (dolist (queries (notmuch-search-properties-in-region :query beg end))
481 (when (first queries)
482 (push (first queries) query-list))
483 (when (and all (second queries))
484 (push (second queries) query-list)))
486 (concat "(" (mapconcat 'identity query-list ") or (") ")"))))
488 (defun notmuch-search-find-authors ()
489 "Return the authors for the current thread"
490 (plist-get (notmuch-search-get-result) :authors))
492 (defun notmuch-search-find-authors-region (beg end)
493 "Return a list of authors for the current region"
494 (notmuch-search-properties-in-region :authors beg end))
496 (defun notmuch-search-find-subject ()
497 "Return the subject for the current thread"
498 (plist-get (notmuch-search-get-result) :subject))
500 (defun notmuch-search-find-subject-region (beg end)
501 "Return a list of authors for the current region"
502 (notmuch-search-properties-in-region :subject beg end))
504 (defun notmuch-search-show-thread (&optional elide-toggle)
505 "Display the currently selected thread.
507 With a prefix argument, invert the default value of
508 `notmuch-show-only-matching-messages' when displaying the
511 (let ((thread-id (notmuch-search-find-thread-id))
512 (subject (notmuch-search-find-subject)))
513 (if (> (length thread-id) 0)
514 (notmuch-show thread-id
517 notmuch-search-query-string
518 ;; Name the buffer based on the subject.
519 (concat "*" (truncate-string-to-width subject 30 nil nil t) "*"))
520 (message "End of search results."))))
522 (defun notmuch-tree-from-search-current-query ()
523 "Call notmuch tree with the current query"
525 (notmuch-tree notmuch-search-query-string))
527 (defun notmuch-unthreaded-from-search-current-query ()
528 "Call notmuch tree with the current query"
530 (notmuch-unthreaded notmuch-search-query-string))
532 (defun notmuch-tree-from-search-thread ()
533 "Show the selected thread with notmuch-tree"
535 (notmuch-tree (notmuch-search-find-thread-id)
536 notmuch-search-query-string
538 (notmuch-prettify-subject (notmuch-search-find-subject))
541 (defun notmuch-search-reply-to-thread (&optional prompt-for-sender)
542 "Begin composing a reply-all to the entire current thread in a new buffer."
544 (let ((message-id (notmuch-search-find-thread-id)))
545 (notmuch-mua-new-reply message-id prompt-for-sender t)))
547 (defun notmuch-search-reply-to-thread-sender (&optional prompt-for-sender)
548 "Begin composing a reply to the entire current thread in a new buffer."
550 (let ((message-id (notmuch-search-find-thread-id)))
551 (notmuch-mua-new-reply message-id prompt-for-sender nil)))
553 (defun notmuch-search-set-tags (tags &optional pos)
554 (let ((new-result (plist-put (notmuch-search-get-result pos) :tags tags)))
555 (notmuch-search-update-result new-result pos)))
557 (defun notmuch-search-get-tags (&optional pos)
558 (plist-get (notmuch-search-get-result pos) :tags))
560 (defun notmuch-search-get-tags-region (beg end)
562 (notmuch-search-foreach-result beg end
564 (setq output (append output (notmuch-search-get-tags pos)))))
567 (defun notmuch-search-interactive-tag-changes (&optional initial-input)
568 "Prompt for tag changes for the current thread or region.
570 Returns (TAG-CHANGES REGION-BEGIN REGION-END)."
571 (let* ((region (notmuch-interactive-region))
572 (beg (first region)) (end (second region))
573 (prompt (if (= beg end) "Tag thread" "Tag region")))
574 (cons (notmuch-read-tag-changes
575 (notmuch-search-get-tags-region beg end) prompt initial-input)
578 (defun notmuch-search-tag (tag-changes &optional beg end only-matched)
579 "Change tags for the currently selected thread or region.
581 See `notmuch-tag' for information on the format of TAG-CHANGES.
582 When called interactively, this uses the region if the region is
583 active. When called directly, BEG and END provide the region.
584 If these are nil or not provided, then, if the region is active
585 this applied to all threads meeting the region, and if the region
586 is inactive this applies to the thread at point.
588 If ONLY-MATCHED is non-nil, only tag matched messages."
589 (interactive (notmuch-search-interactive-tag-changes))
590 (unless (and beg end)
591 (setq beg (car (notmuch-interactive-region))
592 end (cadr (notmuch-interactive-region))))
593 (let ((search-string (notmuch-search-find-stable-query-region
594 beg end only-matched)))
595 (notmuch-tag search-string tag-changes)
596 (notmuch-search-foreach-result beg end
598 (notmuch-search-set-tags
599 (notmuch-update-tags (notmuch-search-get-tags pos) tag-changes)
602 (defun notmuch-search-add-tag (tag-changes &optional beg end)
603 "Change tags for the current thread or region (defaulting to add).
605 Same as `notmuch-search-tag' but sets initial input to '+'."
606 (interactive (notmuch-search-interactive-tag-changes "+"))
607 (notmuch-search-tag tag-changes beg end))
609 (defun notmuch-search-remove-tag (tag-changes &optional beg end)
610 "Change tags for the current thread or region (defaulting to remove).
612 Same as `notmuch-search-tag' but sets initial input to '-'."
613 (interactive (notmuch-search-interactive-tag-changes "-"))
614 (notmuch-search-tag tag-changes beg end))
616 (put 'notmuch-search-archive-thread 'notmuch-prefix-doc
617 "Un-archive the currently selected thread.")
618 (defun notmuch-search-archive-thread (&optional unarchive beg end)
619 "Archive the currently selected thread or region.
621 Archive each message in the currently selected thread by applying
622 the tag changes in `notmuch-archive-tags' to each (remove the
623 \"inbox\" tag by default). If a prefix argument is given, the
624 messages will be \"unarchived\" (i.e. the tag changes in
625 `notmuch-archive-tags' will be reversed).
627 This function advances the next thread when finished."
628 (interactive (cons current-prefix-arg (notmuch-interactive-region)))
629 (when notmuch-archive-tags
631 (notmuch-tag-change-list notmuch-archive-tags unarchive) beg end))
633 (notmuch-search-next-thread)))
635 (defun notmuch-search-update-result (result &optional pos)
636 "Replace the result object of the thread at POS (or point) by
637 RESULT and redraw it.
639 This will keep point in a reasonable location. However, if there
640 are enclosing save-excursions and the saved point is in the
641 result being updated, the point will be restored to the beginning
643 (let ((start (notmuch-search-result-beginning pos))
644 (end (notmuch-search-result-end pos))
646 (inhibit-read-only t))
647 ;; Delete the current thread
648 (delete-region start end)
649 ;; Insert the updated thread
650 (notmuch-search-show-result result start)
651 ;; If point was inside the old result, make an educated guess
652 ;; about where to place it now. Unfortunately, this won't work
653 ;; with save-excursion (or any other markers that would be nice to
654 ;; preserve, such as the window start), but there's nothing we can
655 ;; do about that without a way to retrieve markers in a region.
656 (when (and (>= init-point start) (<= init-point end))
657 (let* ((new-end (notmuch-search-result-end start))
658 (new-point (if (= init-point end)
660 (min init-point (- new-end 1)))))
661 (goto-char new-point)))))
663 (defun notmuch-search-process-sentinel (proc msg)
664 "Add a message to let user know when \"notmuch search\" exits"
665 (let ((buffer (process-buffer proc))
666 (status (process-status proc))
667 (exit-status (process-exit-status proc))
668 (never-found-target-thread nil))
669 (when (memq status '(exit signal))
671 (kill-buffer (process-get proc 'parse-buf))
672 (if (buffer-live-p buffer)
673 (with-current-buffer buffer
675 (let ((inhibit-read-only t)
677 (goto-char (point-max))
678 (if (eq status 'signal)
679 (insert "Incomplete search results (search process was killed).\n"))
680 (when (eq status 'exit)
681 (insert "End of search results.\n")
682 ;; For version mismatch, there's no point in
683 ;; showing the search buffer
684 (when (or (= exit-status 20) (= exit-status 21))
688 (not (string= notmuch-search-target-thread "found")))
689 (set 'never-found-target-thread t)))))
690 (when (and never-found-target-thread
691 notmuch-search-target-line)
692 (goto-char (point-min))
693 (forward-line (1- notmuch-search-target-line)))))))))
695 (define-widget 'notmuch--custom-face-edit 'lazy
696 "Custom face edit with a tag Edit Face"
697 ;; I could not persuage custom-face-edit to respect the :tag
698 ;; property so create a widget specially
699 :tag "Manually specify face"
700 :type 'custom-face-edit)
702 (defcustom notmuch-search-line-faces
703 '(("unread" . notmuch-search-unread-face)
704 ("flagged" . notmuch-search-flagged-face))
705 "Alist of tags to faces for line highlighting in notmuch-search.
706 Each element looks like (TAG . FACE).
707 A thread with TAG will have FACE applied.
709 Here is an example of how to color search results based on tags.
710 (the following text would be placed in your ~/.emacs file):
712 (setq notmuch-search-line-faces \\='((\"unread\" . (:foreground \"green\"))
713 (\"deleted\" . (:foreground \"red\"
714 :background \"blue\"))))
716 The FACE must be a face name (a symbol or string), a property
717 list of face attributes, or a list of these. The faces for
718 matching tags are merged, with earlier attributes overriding
719 later. A message having both \"deleted\" and \"unread\" tags with
720 the above settings would have a green foreground and blue
722 :type '(alist :key-type (string)
723 :value-type (radio (face :tag "Face name")
724 (notmuch--custom-face-edit)))
725 :group 'notmuch-search
726 :group 'notmuch-faces)
728 (defun notmuch-search-color-line (start end line-tag-list)
729 "Colorize lines in `notmuch-show' based on tags."
730 ;; Reverse the list so earlier entries take precedence
731 (dolist (elem (reverse notmuch-search-line-faces))
732 (let ((tag (car elem))
734 (when (member tag line-tag-list)
735 (notmuch-apply-face nil face nil start end)))))
737 (defun notmuch-search-author-propertize (authors)
738 "Split `authors' into matching and non-matching authors and
739 propertize appropriately. If no boundary between authors and
740 non-authors is found, assume that all of the authors match."
741 (if (string-match "\\(.*\\)|\\(.*\\)" authors)
742 (concat (propertize (concat (match-string 1 authors) ",")
743 'face 'notmuch-search-matching-authors)
744 (propertize (match-string 2 authors)
745 'face 'notmuch-search-non-matching-authors))
746 (propertize authors 'face 'notmuch-search-matching-authors)))
748 (defun notmuch-search-insert-authors (format-string authors)
749 ;; Save the match data to avoid interfering with
750 ;; `notmuch-search-process-filter'.
752 (let* ((formatted-authors (format format-string authors))
753 (formatted-sample (format format-string ""))
754 (visible-string formatted-authors)
755 (invisible-string "")
758 ;; Truncate the author string to fit the specification.
759 (if (> (length formatted-authors)
760 (length formatted-sample))
761 (let ((visible-length (- (length formatted-sample)
763 ;; Truncate the visible string according to the width of
764 ;; the display string.
765 (setq visible-string (substring formatted-authors 0 visible-length)
766 invisible-string (substring formatted-authors visible-length))
767 ;; If possible, truncate the visible string at a natural
768 ;; break (comma or pipe), as incremental search doesn't
769 ;; match across the visible/invisible border.
770 (when (string-match "\\(.*\\)\\([,|] \\)\\([^,|]*\\)" visible-string)
771 ;; Second clause is destructive on `visible-string', so
772 ;; order is important.
773 (setq invisible-string (concat (match-string 3 visible-string)
775 visible-string (concat (match-string 1 visible-string)
776 (match-string 2 visible-string))))
777 ;; `visible-string' may be shorter than the space allowed
778 ;; by `format-string'. If so we must insert some padding
779 ;; after `invisible-string'.
780 (setq padding (make-string (- (length formatted-sample)
781 (length visible-string)
785 ;; Use different faces to show matching and non-matching authors.
786 (if (string-match "\\(.*\\)|\\(.*\\)" visible-string)
787 ;; The visible string contains both matching and
788 ;; non-matching authors.
789 (setq visible-string (notmuch-search-author-propertize visible-string)
790 ;; The invisible string must contain only non-matching
791 ;; authors, as the visible-string contains both.
792 invisible-string (propertize invisible-string
793 'face 'notmuch-search-non-matching-authors))
794 ;; The visible string contains only matching authors.
795 (setq visible-string (propertize visible-string
796 'face 'notmuch-search-matching-authors)
797 ;; The invisible string may contain both matching and
798 ;; non-matching authors.
799 invisible-string (notmuch-search-author-propertize invisible-string)))
801 ;; If there is any invisible text, add it as a tooltip to the
803 (when (not (string= invisible-string ""))
804 (setq visible-string (propertize visible-string 'help-echo (concat "..." invisible-string))))
806 ;; Insert the visible and, if present, invisible author strings.
807 (insert visible-string)
808 (when (not (string= invisible-string ""))
809 (let ((start (point))
811 (insert invisible-string)
812 (setq overlay (make-overlay start (point)))
813 (overlay-put overlay 'invisible 'ellipsis)
814 (overlay-put overlay 'isearch-open-invisible #'delete-overlay)))
817 (defun notmuch-search-insert-field (field format-string result)
819 ((string-equal field "date")
820 (insert (propertize (format format-string (plist-get result :date_relative))
821 'face 'notmuch-search-date)))
822 ((string-equal field "count")
823 (insert (propertize (format format-string
824 (format "[%s/%s]" (plist-get result :matched)
825 (plist-get result :total)))
826 'face 'notmuch-search-count)))
827 ((string-equal field "subject")
828 (insert (propertize (format format-string
829 (notmuch-sanitize (plist-get result :subject)))
830 'face 'notmuch-search-subject)))
832 ((string-equal field "authors")
833 (notmuch-search-insert-authors
834 format-string (notmuch-sanitize (plist-get result :authors))))
836 ((string-equal field "tags")
837 (let ((tags (plist-get result :tags))
838 (orig-tags (plist-get result :orig-tags)))
839 (insert (format format-string (notmuch-tag-format-tags tags orig-tags)))))))
841 (defun notmuch-search-show-result (result pos)
842 "Insert RESULT at POS."
843 ;; Ignore excluded matches
844 (unless (= (plist-get result :matched) 0)
847 (dolist (spec notmuch-search-result-format)
848 (notmuch-search-insert-field (car spec) (cdr spec) result))
850 (notmuch-search-color-line pos (point) (plist-get result :tags))
851 (put-text-property pos (point) 'notmuch-search-result result))))
853 (defun notmuch-search-append-result (result)
854 "Insert RESULT at the end of the buffer.
856 This is only called when a result is first inserted so it also
857 sets the :orig-tag property."
858 (let ((new-result (plist-put result :orig-tags (plist-get result :tags)))
860 (notmuch-search-show-result new-result pos)
861 (when (string= (plist-get result :thread) notmuch-search-target-thread)
862 (setq notmuch-search-target-thread "found")
865 (defun notmuch-search-process-filter (proc string)
866 "Process and filter the output of \"notmuch search\""
867 (let ((results-buf (process-buffer proc))
868 (parse-buf (process-get proc 'parse-buf))
869 (inhibit-read-only t)
871 (when (buffer-live-p results-buf)
872 (with-current-buffer parse-buf
875 (goto-char (point-max))
877 (notmuch-sexp-parse-partial-list 'notmuch-search-append-result
880 (defun notmuch-search-tag-all (tag-changes)
881 "Add/remove tags from all messages in current search buffer.
883 See `notmuch-tag' for information on the format of TAG-CHANGES."
885 (list (notmuch-read-tag-changes
886 (notmuch-search-get-tags-region (point-min) (point-max)) "Tag all")))
887 (notmuch-search-tag tag-changes (point-min) (point-max) t))
889 (defun notmuch-search-buffer-title (query)
890 "Returns the title for a buffer with notmuch search results."
894 (loop for tuple in notmuch-saved-searches
895 if (let ((quoted-query (regexp-quote (notmuch-saved-search-get tuple :query))))
896 (and (string-match (concat "^" quoted-query) query)
897 (> (length (match-string 0 query))
899 do (setq longest tuple))
901 (saved-search-name (notmuch-saved-search-get saved-search :name))
902 (saved-search-query (notmuch-saved-search-get saved-search :query)))
903 (cond ((and saved-search (equal saved-search-query query))
904 ;; Query is the same as saved search (ignoring case)
905 (concat "*notmuch-saved-search-" saved-search-name "*"))
907 (concat "*notmuch-search-"
908 (replace-regexp-in-string (concat "^" (regexp-quote saved-search-query))
909 (concat "[ " saved-search-name " ]")
913 (concat "*notmuch-search-" query "*"))
916 (defun notmuch-read-query (prompt)
917 "Read a notmuch-query from the minibuffer with completion.
919 PROMPT is the string to prompt with."
922 (mapcar (lambda (tag) (notmuch-escape-boolean-term tag))
923 (process-lines notmuch-command "search" "--output=tags" "*")))
925 (append (list "folder:" "path:" "thread:" "id:" "date:" "from:" "to:"
926 "subject:" "attachment:")
927 (mapcar (lambda (tag) (concat "tag:" tag)) all-tags)
928 (mapcar (lambda (tag) (concat "is:" tag)) all-tags)
929 (mapcar (lambda (mimetype) (concat "mimetype:" mimetype)) (mailcap-mime-types)))))
930 (let ((keymap (copy-keymap minibuffer-local-map))
931 (current-query (case major-mode
932 (notmuch-search-mode (notmuch-search-get-query))
933 (notmuch-show-mode (notmuch-show-get-query))
934 (notmuch-tree-mode (notmuch-tree-get-query))))
935 (minibuffer-completion-table
936 (completion-table-dynamic
938 ;; generate a list of possible completions for the current input
940 ;; this ugly regexp is used to get the last word of the input
941 ;; possibly preceded by a '('
942 ((string-match "\\(^\\|.* (?\\)\\([^ ]*\\)$" string)
943 (mapcar (lambda (compl)
944 (concat (match-string-no-properties 1 string) compl))
945 (all-completions (match-string-no-properties 2 string)
947 (t (list string)))))))
948 ;; this was simpler than convincing completing-read to accept spaces:
949 (define-key keymap (kbd "TAB") 'minibuffer-complete)
950 (let ((history-delete-duplicates t))
951 (read-from-minibuffer prompt nil keymap nil
952 'notmuch-search-history current-query nil)))))
954 (defun notmuch-search-get-query ()
955 "Return the current query in this search buffer"
956 notmuch-search-query-string)
958 (put 'notmuch-search 'notmuch-doc "Search for messages.")
960 (defun notmuch-search (&optional query oldest-first target-thread target-line no-display)
961 "Display threads matching QUERY in a notmuch-search buffer.
963 If QUERY is nil, it is read interactively from the minibuffer.
964 Other optional parameters are used as follows:
966 OLDEST-FIRST: A Boolean controlling the sort order of returned threads
967 TARGET-THREAD: A thread ID (without the thread: prefix) that will be made
968 current if it appears in the search results.
969 TARGET-LINE: The line number to move to if the target thread does not
970 appear in the search results.
971 NO-DISPLAY: Do not try to foreground the search results buffer. If it is
972 already foregrounded i.e. displayed in a window, this has no
973 effect, meaning the buffer will remain visible.
975 When called interactively, this will prompt for a query and use
976 the configured default sort order."
979 ;; Prompt for a query
981 ;; Use the default search order (if we're doing a search from a
982 ;; search buffer, ignore any buffer-local overrides)
983 (default-value 'notmuch-search-oldest-first)))
985 (let* ((query (or query (notmuch-read-query "Notmuch search: ")))
986 (buffer (get-buffer-create (notmuch-search-buffer-title query))))
989 (switch-to-buffer buffer))
990 (notmuch-search-mode)
991 ;; Don't track undo information for this buffer
992 (set 'buffer-undo-list t)
993 (set 'notmuch-search-query-string query)
994 (set 'notmuch-search-oldest-first oldest-first)
995 (set 'notmuch-search-target-thread target-thread)
996 (set 'notmuch-search-target-line target-line)
997 (notmuch-tag-clear-cache)
998 (let ((proc (get-buffer-process (current-buffer)))
999 (inhibit-read-only t))
1001 (error "notmuch search process already running for query `%s'" query)
1004 (goto-char (point-min))
1006 (let ((proc (notmuch-start-notmuch
1007 "notmuch-search" buffer #'notmuch-search-process-sentinel
1008 "search" "--format=sexp" "--format-version=4"
1010 "--sort=oldest-first"
1011 "--sort=newest-first")
1013 ;; Use a scratch buffer to accumulate partial output.
1014 ;; This buffer will be killed by the sentinel, which
1015 ;; should be called no matter how the process dies.
1016 (parse-buf (generate-new-buffer " *notmuch search parse*")))
1017 (process-put proc 'parse-buf parse-buf)
1018 (set-process-filter proc 'notmuch-search-process-filter)
1019 (set-process-query-on-exit-flag proc nil))))
1020 (run-hooks 'notmuch-search-hook)))
1022 (defun notmuch-search-refresh-view ()
1023 "Refresh the current view.
1025 Erases the current buffer and runs a new search with the same
1026 query string as the current search. If the current thread is in
1027 the new search results, then point will be placed on the same
1028 thread. Otherwise, point will be moved to attempt to be in the
1029 same relative position within the new buffer."
1031 (let ((target-line (line-number-at-pos))
1032 (oldest-first notmuch-search-oldest-first)
1033 (target-thread (notmuch-search-find-thread-id 'bare))
1034 (query notmuch-search-query-string))
1035 ;; notmuch-search erases the current buffer.
1036 (notmuch-search query oldest-first target-thread target-line t)
1037 (goto-char (point-min))))
1039 (defun notmuch-search-toggle-order ()
1040 "Toggle the current search order.
1042 This command toggles the sort order for the current search. The
1043 default sort order is defined by `notmuch-search-oldest-first'."
1045 (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
1046 (notmuch-search-refresh-view))
1048 (defun notmuch-group-disjunctive-query-string (query-string)
1049 "Group query if it contains a complex expression.
1051 Enclose QUERY-STRING in parentheses if it matches
1052 `notmuch-search-disjunctive-regexp'."
1053 (if (string-match-p notmuch-search-disjunctive-regexp query-string)
1054 (concat "( " query-string " )")
1057 (defun notmuch-search-filter (query)
1058 "Filter or LIMIT the current search results based on an additional query string.
1060 Runs a new search matching only messages that match both the
1061 current search results AND the additional query string provided."
1062 (interactive (list (notmuch-read-query "Filter search: ")))
1063 (let ((grouped-query (notmuch-group-disjunctive-query-string query))
1064 (grouped-original-query (notmuch-group-disjunctive-query-string
1065 notmuch-search-query-string)))
1066 (notmuch-search (if (string= grouped-original-query "*")
1068 (concat grouped-original-query " and " grouped-query))
1069 notmuch-search-oldest-first)))
1071 (defun notmuch-search-filter-by-tag (tag)
1072 "Filter the current search results based on a single tag.
1074 Runs a new search matching only messages that match both the
1075 current search results AND that are tagged with the given tag."
1077 (list (notmuch-select-tag-with-completion "Filter by tag: " notmuch-search-query-string)))
1078 (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
1082 "Run notmuch and display saved searches, known tags, etc."
1086 (defun notmuch-interesting-buffer (b)
1087 "Is the current buffer of interest to a notmuch user?"
1088 (with-current-buffer b
1089 (memq major-mode '(notmuch-show-mode
1093 notmuch-message-mode))))
1096 (defun notmuch-cycle-notmuch-buffers ()
1097 "Cycle through any existing notmuch buffers (search, show or hello).
1099 If the current buffer is the only notmuch buffer, bury it. If no
1100 notmuch buffers exist, run `notmuch'."
1104 ;; If the current buffer is a notmuch buffer, remember it and then
1106 (when (notmuch-interesting-buffer (current-buffer))
1107 (setq start (current-buffer))
1110 ;; Find the first notmuch buffer.
1111 (setq first (loop for buffer in (buffer-list)
1112 if (notmuch-interesting-buffer buffer)
1116 ;; If the first one we found is any other than the starting
1117 ;; buffer, switch to it.
1118 (unless (eq first start)
1119 (switch-to-buffer first))
1124 (defun notmuch-search-imenu-prev-index-position-function ()
1125 "Move point to previous message in notmuch-search buffer.
1126 This function is used as a value for
1127 `imenu-prev-index-position-function'."
1128 (notmuch-search-previous-thread))
1130 (defun notmuch-search-imenu-extract-index-name-function ()
1131 "Return imenu name for line at point.
1132 This function is used as a value for
1133 `imenu-extract-index-name-function'. Point should be at the
1134 beginning of the line."
1135 (let ((subject (notmuch-search-find-subject))
1136 (author (notmuch-search-find-authors)))
1137 (format "%s (%s)" subject author)))
1139 (setq mail-user-agent 'notmuch-user-agent)
1143 ;; After provide to avoid loops if notmuch was require'd via notmuch-init-file.
1144 (if init-file-user ; don't load init file if the -q option was used.
1145 (let ((init-file (locate-file notmuch-init-file '("/")
1146 (get-load-suffixes))))
1147 (if init-file (load init-file nil t t))))
1149 ;;; notmuch.el ends here