]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch.el
emacs: update defcustom for notmuch-search-result-format.
[notmuch] / emacs / notmuch.el
1 ;;; notmuch.el --- run notmuch within emacs  -*- lexical-binding: t -*-
2 ;;
3 ;; Copyright © Carl Worth
4 ;;
5 ;; This file is part of Notmuch.
6 ;;
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.
11 ;;
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.
16 ;;
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/>.
19 ;;
20 ;; Authors: Carl Worth <cworth@cworth.org>
21 ;; Homepage: https://notmuchmail.org
22
23 ;;; Commentary:
24
25 ;; This is an emacs-based interface to the notmuch mail system.
26 ;;
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.
30 ;;
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:
35 ;;
36 ;;      sudo make install-emacs
37 ;;
38 ;; to install it.
39 ;;
40 ;; Then, to actually run it, add:
41 ;;
42 ;;      (autoload 'notmuch "notmuch" "Notmuch mail" t)
43 ;;
44 ;; to your ~/.emacs file, and then run "M-x notmuch" from within emacs,
45 ;; or run:
46 ;;
47 ;;      emacs -f notmuch
48 ;;
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).
52 ;;
53 ;; Note for MELPA users (and others tracking the development version
54 ;; of notmuch-emacs):
55 ;;
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.
62 ;;
63 ;; TL;DR: notmuch-emacs from MELPA and notmuch from distro packages is
64 ;; NOT SUPPORTED.
65
66 ;;; Code:
67
68 (require 'mm-view)
69 (require 'message)
70
71 (require 'hl-line)
72
73 (require 'notmuch-lib)
74 (require 'notmuch-tag)
75 (require 'notmuch-show)
76 (require 'notmuch-tree)
77 (require 'notmuch-mua)
78 (require 'notmuch-hello)
79 (require 'notmuch-maildir-fcc)
80 (require 'notmuch-message)
81 (require 'notmuch-parser)
82
83 ;;; Options
84
85 (defcustom notmuch-search-result-format
86   `(("date" . "%12s ")
87     ("count" . "%-7s ")
88     ("authors" . "%-20s ")
89     ("subject" . "%s ")
90     ("tags" . "(%s)"))
91   "Search result formatting.
92
93 List of pairs of (field . format-string).  Supported field
94 strings are: \"date\", \"count\", \"authors\", \"subject\",
95 \"tags\".  It is also supported to pass a function in place of a
96 field name. In this case the function is passed the thread
97 object (plist) and format string.
98
99 Line breaks are permitted in format strings (though this is
100 currently experimental).  Note that a line break at the end of an
101 \"authors\" field will get elided if the authors list is long;
102 place it instead at the beginning of the following field.  To
103 enter a line break when setting this variable with setq, use \\n.
104 To enter a line break in customize, press \\[quoted-insert] C-j."
105   :type '(alist
106           :key-type
107           (choice
108            (const :tag "Date" "date")
109            (const :tag "Count" "count")
110            (const :tag "Authors" "authors")
111            (const :tag "Subject" "subject")
112            (const :tag "Tags" "tags")
113            function)
114           :value-type (string :tag "Format"))
115   :group 'notmuch-search)
116
117 ;; The name of this variable `notmuch-init-file' is consistent with the
118 ;; convention used in e.g. emacs and gnus. The value, `notmuch-config[.el[c]]'
119 ;; is consistent with notmuch cli configuration file `~/.notmuch-config'.
120 (defcustom notmuch-init-file (locate-user-emacs-file "notmuch-config")
121   "Your Notmuch Emacs-Lisp configuration file name.
122 If a file with one of the suffixes defined by `get-load-suffixes' exists,
123 it will be read instead.
124 This file is read once when notmuch is loaded; the notmuch hooks added
125 there will be called at other points of notmuch execution."
126   :type 'file
127   :group 'notmuch)
128
129 (defcustom notmuch-search-hook '(notmuch-hl-line-mode)
130   "List of functions to call when notmuch displays the search results."
131   :type 'hook
132   :options '(notmuch-hl-line-mode)
133   :group 'notmuch-search
134   :group 'notmuch-hooks)
135
136 ;;; Mime Utilities
137
138 (defun notmuch-foreach-mime-part (function mm-handle)
139   (cond ((stringp (car mm-handle))
140          (dolist (part (cdr mm-handle))
141            (notmuch-foreach-mime-part function part)))
142         ((bufferp (car mm-handle))
143          (funcall function mm-handle))
144         (t (dolist (part mm-handle)
145              (notmuch-foreach-mime-part function part)))))
146
147 (defun notmuch-count-attachments (mm-handle)
148   (let ((count 0))
149     (notmuch-foreach-mime-part
150      (lambda (p)
151        (let ((disposition (mm-handle-disposition p)))
152          (and (listp disposition)
153               (or (equal (car disposition) "attachment")
154                   (and (equal (car disposition) "inline")
155                        (assq 'filename disposition)))
156               (cl-incf count))))
157      mm-handle)
158     count))
159
160 (defun notmuch-save-attachments (mm-handle &optional queryp)
161   (notmuch-foreach-mime-part
162    (lambda (p)
163      (let ((disposition (mm-handle-disposition p)))
164        (and (listp disposition)
165             (or (equal (car disposition) "attachment")
166                 (and (equal (car disposition) "inline")
167                      (assq 'filename disposition)))
168             (or (not queryp)
169                 (y-or-n-p
170                  (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
171             (mm-save-part p))))
172    mm-handle))
173
174 ;;; Keymap
175
176 (defvar notmuch-search-mode-map
177   (let ((map (make-sparse-keymap)))
178     (set-keymap-parent map notmuch-common-keymap)
179     (define-key map "x" 'notmuch-bury-or-kill-this-buffer)
180     (define-key map (kbd "DEL") 'notmuch-search-scroll-down)
181     (define-key map "b" 'notmuch-search-scroll-down)
182     (define-key map " " 'notmuch-search-scroll-up)
183     (define-key map "<" 'notmuch-search-first-thread)
184     (define-key map ">" 'notmuch-search-last-thread)
185     (define-key map "p" 'notmuch-search-previous-thread)
186     (define-key map "n" 'notmuch-search-next-thread)
187     (define-key map "r" 'notmuch-search-reply-to-thread-sender)
188     (define-key map "R" 'notmuch-search-reply-to-thread)
189     (define-key map "o" 'notmuch-search-toggle-order)
190     (define-key map "c" 'notmuch-search-stash-map)
191     (define-key map "t" 'notmuch-search-filter-by-tag)
192     (define-key map "l" 'notmuch-search-filter)
193     (define-key map [mouse-1] 'notmuch-search-show-thread)
194     (define-key map "k" 'notmuch-tag-jump)
195     (define-key map "*" 'notmuch-search-tag-all)
196     (define-key map "a" 'notmuch-search-archive-thread)
197     (define-key map "-" 'notmuch-search-remove-tag)
198     (define-key map "+" 'notmuch-search-add-tag)
199     (define-key map (kbd "RET") 'notmuch-search-show-thread)
200     (define-key map (kbd "M-RET") 'notmuch-tree-from-search-thread)
201     (define-key map "Z" 'notmuch-tree-from-search-current-query)
202     (define-key map "U" 'notmuch-unthreaded-from-search-current-query)
203     map)
204   "Keymap for \"notmuch search\" buffers.")
205
206 ;;; Internal Variables
207
208 (defvar notmuch-query-history nil
209   "Variable to store minibuffer history for notmuch queries.")
210
211 (defvar-local notmuch-search-query-string nil)
212 (defvar-local notmuch-search-target-thread nil)
213 (defvar-local notmuch-search-target-line nil)
214
215 ;;; Stashing
216
217 (defvar notmuch-search-stash-map
218   (let ((map (make-sparse-keymap)))
219     (define-key map "i" 'notmuch-search-stash-thread-id)
220     (define-key map "q" 'notmuch-stash-query)
221     (define-key map "?" 'notmuch-subkeymap-help)
222     map)
223   "Submap for stash commands.")
224 (fset 'notmuch-search-stash-map notmuch-search-stash-map)
225
226 (defun notmuch-search-stash-thread-id ()
227   "Copy thread ID of current thread to kill-ring."
228   (interactive)
229   (notmuch-common-do-stash (notmuch-search-find-thread-id)))
230
231 (defun notmuch-stash-query ()
232   "Copy current query to kill-ring."
233   (interactive)
234   (notmuch-common-do-stash notmuch-search-query-string))
235
236 ;;; Movement
237
238 (defun notmuch-search-scroll-up ()
239   "Move forward through search results by one window's worth."
240   (interactive)
241   (condition-case nil
242       (scroll-up nil)
243     ((end-of-buffer) (notmuch-search-last-thread))))
244
245 (defun notmuch-search-scroll-down ()
246   "Move backward through the search results by one window's worth."
247   (interactive)
248   ;; I don't know why scroll-down doesn't signal beginning-of-buffer
249   ;; the way that scroll-up signals end-of-buffer, but c'est la vie.
250   ;;
251   ;; So instead of trapping a signal we instead check whether the
252   ;; window begins on the first line of the buffer and if so, move
253   ;; directly to that position. (We have to count lines since the
254   ;; window-start position is not the same as point-min due to the
255   ;; invisible thread-ID characters on the first line.
256   (if (equal (count-lines (point-min) (window-start)) 0)
257       (goto-char (point-min))
258     (scroll-down nil)))
259
260 (defun notmuch-search-next-thread ()
261   "Select the next thread in the search results."
262   (interactive)
263   (when (notmuch-search-get-result)
264     (goto-char (notmuch-search-result-end))))
265
266 (defun notmuch-search-previous-thread ()
267   "Select the previous thread in the search results."
268   (interactive)
269   (if (notmuch-search-get-result)
270       (unless (bobp)
271         (goto-char (notmuch-search-result-beginning (- (point) 1))))
272     ;; We must be past the end; jump to the last result
273     (notmuch-search-last-thread)))
274
275 (defun notmuch-search-last-thread ()
276   "Select the last thread in the search results."
277   (interactive)
278   (goto-char (point-max))
279   (forward-line -2)
280   (let ((beg (notmuch-search-result-beginning)))
281     (when beg
282       (goto-char beg))))
283
284 (defun notmuch-search-first-thread ()
285   "Select the first thread in the search results."
286   (interactive)
287   (goto-char (point-min)))
288
289 ;;; Faces
290
291 (defface notmuch-message-summary-face
292   `((((class color) (background light))
293      ,@(and (>= emacs-major-version 27) '(:extend t))
294      :background "#f0f0f0")
295     (((class color) (background dark))
296      ,@(and (>= emacs-major-version 27) '(:extend t))
297      :background "#303030"))
298   "Face for the single-line message summary in notmuch-show-mode."
299   :group 'notmuch-show
300   :group 'notmuch-faces)
301
302 (defface notmuch-search-date
303   '((t :inherit default))
304   "Face used in search mode for dates."
305   :group 'notmuch-search
306   :group 'notmuch-faces)
307
308 (defface notmuch-search-count
309   '((t :inherit default))
310   "Face used in search mode for the count matching the query."
311   :group 'notmuch-search
312   :group 'notmuch-faces)
313
314 (defface notmuch-search-subject
315   '((t :inherit default))
316   "Face used in search mode for subjects."
317   :group 'notmuch-search
318   :group 'notmuch-faces)
319
320 (defface notmuch-search-matching-authors
321   '((t :inherit default))
322   "Face used in search mode for authors matching the query."
323   :group 'notmuch-search
324   :group 'notmuch-faces)
325
326 (defface notmuch-search-non-matching-authors
327   '((((class color)
328       (background dark))
329      (:foreground "grey30"))
330     (((class color)
331       (background light))
332      (:foreground "grey60"))
333     (t
334      (:italic t)))
335   "Face used in search mode for authors not matching the query."
336   :group 'notmuch-search
337   :group 'notmuch-faces)
338
339 (defface notmuch-tag-face
340   '((((class color)
341       (background dark))
342      (:foreground "OliveDrab1"))
343     (((class color)
344       (background light))
345      (:foreground "navy blue" :bold t))
346     (t
347      (:bold t)))
348   "Face used in search mode face for tags."
349   :group 'notmuch-search
350   :group 'notmuch-faces)
351
352 (defface notmuch-search-flagged-face
353   '((((class color)
354       (background dark))
355      (:foreground "LightBlue1"))
356     (((class color)
357       (background light))
358      (:foreground "blue")))
359   "Face used in search mode face for flagged threads.
360
361 This face is the default value for the \"flagged\" tag in
362 `notmuch-search-line-faces'."
363   :group 'notmuch-search
364   :group 'notmuch-faces)
365
366 (defface notmuch-search-unread-face
367   '((t
368      (:weight bold)))
369   "Face used in search mode for unread threads.
370
371 This face is the default value for the \"unread\" tag in
372 `notmuch-search-line-faces'."
373   :group 'notmuch-search
374   :group 'notmuch-faces)
375
376 ;;; Mode
377
378 (define-derived-mode notmuch-search-mode fundamental-mode "notmuch-search"
379   "Major mode displaying results of a notmuch search.
380
381 This buffer contains the results of a \"notmuch search\" of your
382 email archives. Each line in the buffer represents a single
383 thread giving a summary of the thread (a relative date, the
384 number of matched messages and total messages in the thread,
385 participants in the thread, a representative subject line, and
386 any tags).
387
388 Pressing \\[notmuch-search-show-thread] on any line displays that
389 thread. The '\\[notmuch-search-add-tag]' and
390 '\\[notmuch-search-remove-tag]' keys can be used to add or remove
391 tags from a thread. The '\\[notmuch-search-archive-thread]' key
392 is a convenience for archiving a thread (applying changes in
393 `notmuch-archive-tags'). The '\\[notmuch-search-tag-all]' key can
394 be used to add and/or remove tags from all messages (as opposed
395 to threads) that match the current query.  Use with caution, as
396 this will also tag matching messages that arrived *after*
397 constructing the buffer.
398
399 Other useful commands are '\\[notmuch-search-filter]' for
400 filtering the current search based on an additional query string,
401 '\\[notmuch-search-filter-by-tag]' for filtering to include only
402 messages with a given tag, and '\\[notmuch-search]' to execute a
403 new, global search.
404
405 Complete list of currently available key bindings:
406
407 \\{notmuch-search-mode-map}"
408   (setq notmuch-buffer-refresh-function #'notmuch-search-refresh-view)
409   (setq-local scroll-preserve-screen-position t)
410   (add-to-invisibility-spec (cons 'ellipsis t))
411   (setq truncate-lines t)
412   (setq buffer-read-only t)
413   (setq imenu-prev-index-position-function
414         #'notmuch-search-imenu-prev-index-position-function)
415   (setq imenu-extract-index-name-function
416         #'notmuch-search-imenu-extract-index-name-function))
417
418 ;;; Search Results
419
420 (defun notmuch-search-get-result (&optional pos)
421   "Return the result object for the thread at POS (or point).
422
423 If there is no thread at POS (or point), returns nil."
424   (get-text-property (or pos (point)) 'notmuch-search-result))
425
426 (defun notmuch-search-result-beginning (&optional pos)
427   "Return the point at the beginning of the thread at POS (or point).
428
429 If there is no thread at POS (or point), returns nil."
430   (and (notmuch-search-get-result pos)
431        ;; We pass 1+point because previous-single-property-change starts
432        ;; searching one before the position we give it.
433        (previous-single-property-change (1+ (or pos (point)))
434                                         'notmuch-search-result nil
435                                         (point-min))))
436
437 (defun notmuch-search-result-end (&optional pos)
438   "Return the point at the end of the thread at POS (or point).
439
440 The returned point will be just after the newline character that
441 ends the result line.  If there is no thread at POS (or point),
442 returns nil."
443   (and (notmuch-search-get-result pos)
444        (next-single-property-change (or pos (point))
445                                     'notmuch-search-result nil
446                                     (point-max))))
447
448 (defun notmuch-search-foreach-result (beg end fn)
449   "Invoke FN for each result between BEG and END.
450
451 FN should take one argument.  It will be applied to the character
452 position of the beginning of each result that overlaps the region
453 between points BEG and END.  As a special case, if (= BEG END),
454 FN will be applied to the result containing point BEG."
455   (let ((pos (notmuch-search-result-beginning beg))
456         ;; End must be a marker in case fn changes the
457         ;; text.
458         (end (copy-marker end))
459         ;; Make sure we examine at least one result, even if
460         ;; (= beg end).
461         (first t))
462     ;; We have to be careful if the region extends beyond the results.
463     ;; In this case, pos could be null or there could be no result at
464     ;; pos.
465     (while (and pos (or (< pos end) first))
466       (when (notmuch-search-get-result pos)
467         (funcall fn pos))
468       (setq pos (notmuch-search-result-end pos))
469       (setq first nil))))
470 ;; Unindent the function argument of notmuch-search-foreach-result so
471 ;; the indentation of callers doesn't get out of hand.
472 (put 'notmuch-search-foreach-result 'lisp-indent-function 2)
473
474 (defun notmuch-search-properties-in-region (property beg end)
475   (let (output)
476     (notmuch-search-foreach-result beg end
477       (lambda (pos)
478         (push (plist-get (notmuch-search-get-result pos) property) output)))
479     output))
480
481 (defun notmuch-search-find-thread-id (&optional bare)
482   "Return the thread for the current thread.
483
484 If BARE is set then do not prefix with \"thread:\"."
485   (let ((thread (plist-get (notmuch-search-get-result) :thread)))
486     (when thread
487       (concat (and (not bare) "thread:") thread))))
488
489 (defun notmuch-search-find-stable-query ()
490   "Return the stable queries for the current thread.
491
492 Return a list (MATCHED-QUERY UNMATCHED-QUERY) for the
493 matched and unmatched messages in the current thread."
494   (plist-get (notmuch-search-get-result) :query))
495
496 (defun notmuch-search-find-stable-query-region (beg end &optional only-matched)
497   "Return the stable query for the current region.
498
499 If ONLY-MATCHED is non-nil, include only matched messages.  If it
500 is nil, include both matched and unmatched messages. If there are
501 no messages in the region then return nil."
502   (let ((query-list nil) (all (not only-matched)))
503     (dolist (queries (notmuch-search-properties-in-region :query beg end))
504       (when (car queries)
505         (push (car queries) query-list))
506       (when (and all (cadr queries))
507         (push (cadr queries) query-list)))
508     (and query-list
509          (concat "(" (mapconcat 'identity query-list ") or (") ")"))))
510
511 (defun notmuch-search-find-authors ()
512   "Return the authors for the current thread."
513   (plist-get (notmuch-search-get-result) :authors))
514
515 (defun notmuch-search-find-authors-region (beg end)
516   "Return a list of authors for the current region."
517   (notmuch-search-properties-in-region :authors beg end))
518
519 (defun notmuch-search-find-subject ()
520   "Return the subject for the current thread."
521   (plist-get (notmuch-search-get-result) :subject))
522
523 (defun notmuch-search-find-subject-region (beg end)
524   "Return a list of authors for the current region."
525   (notmuch-search-properties-in-region :subject beg end))
526
527 (defun notmuch-search-show-thread (&optional elide-toggle)
528   "Display the currently selected thread.
529
530 With a prefix argument, invert the default value of
531 `notmuch-show-only-matching-messages' when displaying the
532 thread.
533
534 Return non-nil on success."
535   (interactive "P")
536   (let ((thread-id (notmuch-search-find-thread-id)))
537     (if thread-id
538         (notmuch-show thread-id
539                       elide-toggle
540                       (current-buffer)
541                       notmuch-search-query-string
542                       ;; Name the buffer based on the subject.
543                       (format "*%s*" (truncate-string-to-width
544                                       (notmuch-search-find-subject)
545                                       30 nil nil t)))
546       (message "End of search results.")
547       nil)))
548
549 (defun notmuch-tree-from-search-current-query ()
550   "Tree view of current query."
551   (interactive)
552   (notmuch-tree notmuch-search-query-string))
553
554 (defun notmuch-unthreaded-from-search-current-query ()
555   "Unthreaded view of current query."
556   (interactive)
557   (notmuch-unthreaded notmuch-search-query-string))
558
559 (defun notmuch-tree-from-search-thread ()
560   "Show the selected thread with notmuch-tree."
561   (interactive)
562   (notmuch-tree (notmuch-search-find-thread-id)
563                 notmuch-search-query-string
564                 nil
565                 (notmuch-prettify-subject (notmuch-search-find-subject))
566                 t nil (current-buffer)))
567
568 (defun notmuch-search-reply-to-thread (&optional prompt-for-sender)
569   "Begin composing a reply-all to the entire current thread in a new buffer."
570   (interactive "P")
571   (notmuch-mua-new-reply (notmuch-search-find-thread-id)
572                          prompt-for-sender t))
573
574 (defun notmuch-search-reply-to-thread-sender (&optional prompt-for-sender)
575   "Begin composing a reply to the entire current thread in a new buffer."
576   (interactive "P")
577   (notmuch-mua-new-reply (notmuch-search-find-thread-id)
578                          prompt-for-sender nil))
579
580 ;;; Tags
581
582 (defun notmuch-search-set-tags (tags &optional pos)
583   (notmuch-search-update-result
584    (plist-put (notmuch-search-get-result pos) :tags tags)
585    pos))
586
587 (defun notmuch-search-get-tags (&optional pos)
588   (plist-get (notmuch-search-get-result pos) :tags))
589
590 (defun notmuch-search-get-tags-region (beg end)
591   (let (output)
592     (notmuch-search-foreach-result beg end
593       (lambda (pos)
594         (setq output (append output (notmuch-search-get-tags pos)))))
595     (delete-dups output)))
596
597 (defun notmuch-search-interactive-tag-changes (&optional initial-input)
598   "Prompt for tag changes for the current thread or region.
599
600 Return (TAG-CHANGES REGION-BEGIN REGION-END)."
601   (pcase-let ((`(,beg ,end) (notmuch-interactive-region)))
602     (list (notmuch-read-tag-changes (notmuch-search-get-tags-region beg end)
603                                     (if (= beg end) "Tag thread" "Tag region")
604                                     initial-input)
605           beg end)))
606
607 (defun notmuch-search-tag (tag-changes &optional beg end only-matched)
608   "Change tags for the currently selected thread or region.
609
610 See `notmuch-tag' for information on the format of TAG-CHANGES.
611 When called interactively, this uses the region if the region is
612 active.  When called directly, BEG and END provide the region.
613 If these are nil or not provided, then, if the region is active
614 this applied to all threads meeting the region, and if the region
615 is inactive this applies to the thread at point.
616
617 If ONLY-MATCHED is non-nil, only tag matched messages."
618   (interactive (notmuch-search-interactive-tag-changes))
619   (unless (and beg end)
620     (setq beg (car (notmuch-interactive-region)))
621     (setq end (cadr (notmuch-interactive-region))))
622   (let ((search-string (notmuch-search-find-stable-query-region
623                         beg end only-matched)))
624     (notmuch-tag search-string tag-changes)
625     (notmuch-search-foreach-result beg end
626       (lambda (pos)
627         (notmuch-search-set-tags
628          (notmuch-update-tags (notmuch-search-get-tags pos) tag-changes)
629          pos)))))
630
631 (defun notmuch-search-add-tag (tag-changes &optional beg end)
632   "Change tags for the current thread or region (defaulting to add).
633
634 Same as `notmuch-search-tag' but sets initial input to '+'."
635   (interactive (notmuch-search-interactive-tag-changes "+"))
636   (notmuch-search-tag tag-changes beg end))
637
638 (defun notmuch-search-remove-tag (tag-changes &optional beg end)
639   "Change tags for the current thread or region (defaulting to remove).
640
641 Same as `notmuch-search-tag' but sets initial input to '-'."
642   (interactive (notmuch-search-interactive-tag-changes "-"))
643   (notmuch-search-tag tag-changes beg end))
644
645 (put 'notmuch-search-archive-thread 'notmuch-prefix-doc
646      "Un-archive the currently selected thread.")
647 (defun notmuch-search-archive-thread (&optional unarchive beg end)
648   "Archive the currently selected thread or region.
649
650 Archive each message in the currently selected thread by applying
651 the tag changes in `notmuch-archive-tags' to each (remove the
652 \"inbox\" tag by default). If a prefix argument is given, the
653 messages will be \"unarchived\" (i.e. the tag changes in
654 `notmuch-archive-tags' will be reversed).
655
656 This function advances the next thread when finished."
657   (interactive (cons current-prefix-arg (notmuch-interactive-region)))
658   (when notmuch-archive-tags
659     (notmuch-search-tag
660      (notmuch-tag-change-list notmuch-archive-tags unarchive) beg end))
661   (when (eq beg end)
662     (notmuch-search-next-thread)))
663
664 ;;; Search Results
665
666 (defun notmuch-search-update-result (result &optional pos)
667   "Replace the result object of the thread at POS (or point) by
668 RESULT and redraw it.
669
670 This will keep point in a reasonable location.  However, if there
671 are enclosing save-excursions and the saved point is in the
672 result being updated, the point will be restored to the beginning
673 of the result."
674   (let ((start (notmuch-search-result-beginning pos))
675         (end (notmuch-search-result-end pos))
676         (init-point (point))
677         (inhibit-read-only t))
678     ;; Delete the current thread
679     (delete-region start end)
680     ;; Insert the updated thread
681     (notmuch-search-show-result result start)
682     ;; If point was inside the old result, make an educated guess
683     ;; about where to place it now.  Unfortunately, this won't work
684     ;; with save-excursion (or any other markers that would be nice to
685     ;; preserve, such as the window start), but there's nothing we can
686     ;; do about that without a way to retrieve markers in a region.
687     (when (and (>= init-point start) (<= init-point end))
688       (let* ((new-end (notmuch-search-result-end start))
689              (new-point (if (= init-point end)
690                             new-end
691                           (min init-point (- new-end 1)))))
692         (goto-char new-point)))))
693
694 (defun notmuch-search-process-sentinel (proc _msg)
695   "Add a message to let user know when \"notmuch search\" exits."
696   (let ((buffer (process-buffer proc))
697         (status (process-status proc))
698         (exit-status (process-exit-status proc))
699         (never-found-target-thread nil))
700     (when (memq status '(exit signal))
701       (catch 'return
702         (kill-buffer (process-get proc 'parse-buf))
703         (when (buffer-live-p buffer)
704           (with-current-buffer buffer
705             (save-excursion
706               (let ((inhibit-read-only t)
707                     (atbob (bobp)))
708                 (goto-char (point-max))
709                 (when (eq status 'signal)
710                   (insert "Incomplete search results (search process was killed).\n"))
711                 (when (eq status 'exit)
712                   (insert "End of search results.\n")
713                   ;; For version mismatch, there's no point in
714                   ;; showing the search buffer
715                   (when (or (= exit-status 20) (= exit-status 21))
716                     (kill-buffer)
717                     (throw 'return nil))
718                   (when (and atbob
719                              (not (string= notmuch-search-target-thread "found")))
720                     (setq never-found-target-thread t)))))
721             (when (and never-found-target-thread
722                        notmuch-search-target-line)
723               (goto-char (point-min))
724               (forward-line (1- notmuch-search-target-line)))))))))
725
726 (define-widget 'notmuch--custom-face-edit 'lazy
727   "Custom face edit with a tag Edit Face"
728   ;; I could not persuage custom-face-edit to respect the :tag
729   ;; property so create a widget specially
730   :tag "Manually specify face"
731   :type 'custom-face-edit)
732
733 (defcustom notmuch-search-line-faces
734   '(("unread" . notmuch-search-unread-face)
735     ("flagged" . notmuch-search-flagged-face))
736   "Alist of tags to faces for line highlighting in notmuch-search.
737 Each element looks like (TAG . FACE).
738 A thread with TAG will have FACE applied.
739
740 Here is an example of how to color search results based on tags.
741  (the following text would be placed in your ~/.emacs file):
742
743  (setq notmuch-search-line-faces \\='((\"unread\" . (:foreground \"green\"))
744                                    (\"deleted\" . (:foreground \"red\"
745                                                   :background \"blue\"))))
746
747 The FACE must be a face name (a symbol or string), a property
748 list of face attributes, or a list of these.  The faces for
749 matching tags are merged, with earlier attributes overriding
750 later. A message having both \"deleted\" and \"unread\" tags with
751 the above settings would have a green foreground and blue
752 background."
753   :type '(alist :key-type (string)
754                 :value-type (radio (face :tag "Face name")
755                                    (notmuch--custom-face-edit)))
756   :group 'notmuch-search
757   :group 'notmuch-faces)
758
759 (defun notmuch-search-color-line (start end line-tag-list)
760   "Colorize lines in `notmuch-show' based on tags."
761   ;; Reverse the list so earlier entries take precedence
762   (dolist (elem (reverse notmuch-search-line-faces))
763     (let ((tag (car elem))
764           (face (cdr elem)))
765       (when (member tag line-tag-list)
766         (notmuch-apply-face nil face nil start end)))))
767
768 (defun notmuch-search-author-propertize (authors)
769   "Split `authors' into matching and non-matching authors and
770 propertize appropriately. If no boundary between authors and
771 non-authors is found, assume that all of the authors match."
772   (if (string-match "\\(.*\\)|\\(.*\\)" authors)
773       (concat (propertize (concat (match-string 1 authors) ",")
774                           'face 'notmuch-search-matching-authors)
775               (propertize (match-string 2 authors)
776                           'face 'notmuch-search-non-matching-authors))
777     (propertize authors 'face 'notmuch-search-matching-authors)))
778
779 (defun notmuch-search-insert-authors (format-string authors)
780   ;; Save the match data to avoid interfering with
781   ;; `notmuch-search-process-filter'.
782   (save-match-data
783     (let* ((formatted-authors (format format-string authors))
784            (formatted-sample (format format-string ""))
785            (visible-string formatted-authors)
786            (invisible-string "")
787            (padding ""))
788       ;; Truncate the author string to fit the specification.
789       (when (> (length formatted-authors)
790                (length formatted-sample))
791         (let ((visible-length (- (length formatted-sample)
792                                  (length "... "))))
793           ;; Truncate the visible string according to the width of
794           ;; the display string.
795           (setq visible-string (substring formatted-authors 0 visible-length))
796           (setq invisible-string (substring formatted-authors visible-length))
797           ;; If possible, truncate the visible string at a natural
798           ;; break (comma or pipe), as incremental search doesn't
799           ;; match across the visible/invisible border.
800           (when (string-match "\\(.*\\)\\([,|] \\)\\([^,|]*\\)" visible-string)
801             ;; Second clause is destructive on `visible-string', so
802             ;; order is important.
803             (setq invisible-string (concat (match-string 3 visible-string)
804                                            invisible-string))
805             (setq visible-string (concat (match-string 1 visible-string)
806                                          (match-string 2 visible-string))))
807           ;; `visible-string' may be shorter than the space allowed
808           ;; by `format-string'. If so we must insert some padding
809           ;; after `invisible-string'.
810           (setq padding (make-string (- (length formatted-sample)
811                                         (length visible-string)
812                                         (length "..."))
813                                      ? ))))
814       ;; Use different faces to show matching and non-matching authors.
815       (if (string-match "\\(.*\\)|\\(.*\\)" visible-string)
816           ;; The visible string contains both matching and
817           ;; non-matching authors.
818           (progn
819             (setq visible-string (notmuch-search-author-propertize visible-string))
820             ;; The invisible string must contain only non-matching
821             ;; authors, as the visible-string contains both.
822             (setq invisible-string (propertize invisible-string
823                                                'face 'notmuch-search-non-matching-authors)))
824         ;; The visible string contains only matching authors.
825         (setq visible-string (propertize visible-string
826                                          'face 'notmuch-search-matching-authors))
827         ;; The invisible string may contain both matching and
828         ;; non-matching authors.
829         (setq invisible-string (notmuch-search-author-propertize invisible-string)))
830       ;; If there is any invisible text, add it as a tooltip to the
831       ;; visible text.
832       (unless (string-empty-p invisible-string)
833         (setq visible-string
834               (propertize visible-string
835                           'help-echo (concat "..." invisible-string))))
836       ;; Insert the visible and, if present, invisible author strings.
837       (insert visible-string)
838       (unless (string-empty-p invisible-string)
839         (let ((start (point))
840               overlay)
841           (insert invisible-string)
842           (setq overlay (make-overlay start (point)))
843           (overlay-put overlay 'invisible 'ellipsis)
844           (overlay-put overlay 'isearch-open-invisible #'delete-overlay)))
845       (insert padding))))
846
847 (defun notmuch-search-insert-field (field format-string result)
848   (pcase field
849     ((pred functionp)
850      (insert (funcall field format-string result)))
851     ("date"
852      (insert (propertize (format format-string (plist-get result :date_relative))
853                          'face 'notmuch-search-date)))
854     ("count"
855      (insert (propertize (format format-string
856                                  (format "[%s/%s]" (plist-get result :matched)
857                                          (plist-get result :total)))
858                          'face 'notmuch-search-count)))
859     ("subject"
860      (insert (propertize (format format-string
861                                  (notmuch-sanitize (plist-get result :subject)))
862                          'face 'notmuch-search-subject)))
863     ("authors"
864      (notmuch-search-insert-authors format-string
865                                     (notmuch-sanitize (plist-get result :authors))))
866     ("tags"
867      (let ((tags (plist-get result :tags))
868            (orig-tags (plist-get result :orig-tags)))
869        (insert (format format-string (notmuch-tag-format-tags tags orig-tags)))))))
870
871 (defun notmuch-search-show-result (result pos)
872   "Insert RESULT at POS."
873   ;; Ignore excluded matches
874   (unless (= (plist-get result :matched) 0)
875     (save-excursion
876       (goto-char pos)
877       (dolist (spec notmuch-search-result-format)
878         (notmuch-search-insert-field (car spec) (cdr spec) result))
879       (insert "\n")
880       (notmuch-search-color-line pos (point) (plist-get result :tags))
881       (put-text-property pos (point) 'notmuch-search-result result))))
882
883 (defun notmuch-search-append-result (result)
884   "Insert RESULT at the end of the buffer.
885
886 This is only called when a result is first inserted so it also
887 sets the :orig-tag property."
888   (let ((new-result (plist-put result :orig-tags (plist-get result :tags)))
889         (pos (point-max)))
890     (notmuch-search-show-result new-result pos)
891     (when (string= (plist-get result :thread) notmuch-search-target-thread)
892       (setq notmuch-search-target-thread "found")
893       (goto-char pos))))
894
895 (defvar-local notmuch--search-hook-run nil
896   "Flag used to ensure the notmuch-search-hook is only run once per buffer")
897
898 (defun notmuch--search-hook-wrapper ()
899   (unless notmuch--search-hook-run
900     (setq notmuch--search-hook-run t)
901     (run-hooks 'notmuch-search-hook)))
902
903 (defun notmuch-search-process-filter (proc string)
904   "Process and filter the output of \"notmuch search\"."
905   (let ((results-buf (process-buffer proc))
906         (parse-buf (process-get proc 'parse-buf))
907         (inhibit-read-only t))
908     (when (buffer-live-p results-buf)
909       (with-current-buffer parse-buf
910         ;; Insert new data
911         (save-excursion
912           (goto-char (point-max))
913           (insert string))
914         (notmuch-sexp-parse-partial-list 'notmuch-search-append-result
915                                          results-buf))
916       (with-current-buffer results-buf
917         (notmuch--search-hook-wrapper)))))
918
919 ;;; Commands (and some helper functions used by them)
920
921 (defun notmuch-search-tag-all (tag-changes)
922   "Add/remove tags from all messages in current search buffer.
923
924 See `notmuch-tag' for information on the format of TAG-CHANGES."
925   (interactive
926    (list (notmuch-read-tag-changes
927           (notmuch-search-get-tags-region (point-min) (point-max)) "Tag all")))
928   (notmuch-search-tag tag-changes (point-min) (point-max) t))
929
930 (defcustom notmuch-search-buffer-name-format "*notmuch-%t-%s*"
931   "Format for the name of search results buffers.
932
933 In this spec, %s will be replaced by a description of the search
934 query and %t by its type (search, tree or unthreaded).  The
935 buffer name is formatted using `format-spec': see its docstring
936 for additional parameters for the s and t format specifiers.
937
938 See also `notmuch-saved-search-buffer-name-format'"
939   :type 'string
940   :group 'notmuch-search)
941
942 (defcustom notmuch-saved-search-buffer-name-format "*notmuch-saved-%t-%s*"
943   "Format for the name of search results buffers for saved searches.
944
945 In this spec, %s will be replaced by the saved search name and %t
946 by its type (search, tree or unthreaded).  The buffer name is
947 formatted using `format-spec': see its docstring for additional
948 parameters for the s and t format specifiers.
949
950 See also `notmuch-search-buffer-name-format'"
951   :type 'string
952   :group 'notmuch-search)
953
954 (defun notmuch-search-format-buffer-name (query type saved)
955   "Compose a buffer name for the given QUERY, TYPE (search, tree,
956 unthreaded) and whether it's SAVED (t or nil)."
957   (let ((fmt (if saved
958                  notmuch-saved-search-buffer-name-format
959                notmuch-search-buffer-name-format)))
960     (format-spec fmt `((?t . ,(or type "search")) (?s . ,query)))))
961
962 (defun notmuch-search-buffer-title (query &optional type)
963   "Returns the title for a buffer with notmuch search results."
964   (let* ((saved-search
965           (let (longest
966                 (longest-length 0))
967             (cl-loop for tuple in notmuch-saved-searches
968                      if (let ((quoted-query
969                                (regexp-quote
970                                 (notmuch-saved-search-get tuple :query))))
971                           (and (string-match (concat "^" quoted-query) query)
972                                (> (length (match-string 0 query))
973                                   longest-length)))
974                      do (setq longest tuple))
975             longest))
976          (saved-search-name (notmuch-saved-search-get saved-search :name))
977          (saved-search-type (notmuch-saved-search-get saved-search :search-type))
978          (saved-search-query (notmuch-saved-search-get saved-search :query)))
979     (cond ((and saved-search (equal saved-search-query query))
980            ;; Query is the same as saved search (ignoring case)
981            (notmuch-search-format-buffer-name saved-search-name
982                                               saved-search-type
983                                               t))
984           (saved-search
985            (let ((query (replace-regexp-in-string
986                          (concat "^" (regexp-quote saved-search-query))
987                          (concat "[ " saved-search-name " ]")
988                          query)))
989              (notmuch-search-format-buffer-name query saved-search-type t)))
990           (t (notmuch-search-format-buffer-name query type nil)))))
991
992 (defun notmuch-read-query (prompt)
993   "Read a notmuch-query from the minibuffer with completion.
994
995 PROMPT is the string to prompt with."
996   (let* ((all-tags
997           (mapcar (lambda (tag) (notmuch-escape-boolean-term tag))
998                   (notmuch--process-lines notmuch-command "search" "--output=tags" "*")))
999          (completions
1000           (append (list "folder:" "path:" "thread:" "id:" "date:" "from:" "to:"
1001                         "subject:" "attachment:")
1002                   (mapcar (lambda (tag) (concat "tag:" tag)) all-tags)
1003                   (mapcar (lambda (tag) (concat "is:" tag)) all-tags)
1004                   (mapcar (lambda (mimetype) (concat "mimetype:" mimetype))
1005                           (mailcap-mime-types))))
1006          (keymap (copy-keymap minibuffer-local-map))
1007          (current-query (cl-case major-mode
1008                           (notmuch-search-mode (notmuch-search-get-query))
1009                           (notmuch-show-mode (notmuch-show-get-query))
1010                           (notmuch-tree-mode (notmuch-tree-get-query))))
1011          (minibuffer-completion-table
1012           (completion-table-dynamic
1013            (lambda (string)
1014              ;; Generate a list of possible completions for the current input.
1015              (cond
1016               ;; This ugly regexp is used to get the last word of the input
1017               ;; possibly preceded by a '('.
1018               ((string-match "\\(^\\|.* (?\\)\\([^ ]*\\)$" string)
1019                (mapcar (lambda (compl)
1020                          (concat (match-string-no-properties 1 string) compl))
1021                        (all-completions (match-string-no-properties 2 string)
1022                                         completions)))
1023               (t (list string)))))))
1024     ;; This was simpler than convincing completing-read to accept spaces:
1025     (define-key keymap (kbd "TAB") 'minibuffer-complete)
1026     (let ((history-delete-duplicates t))
1027       (read-from-minibuffer prompt nil keymap nil
1028                             'notmuch-search-history current-query nil))))
1029
1030 (defun notmuch-search-get-query ()
1031   "Return the current query in this search buffer."
1032   notmuch-search-query-string)
1033
1034 (put 'notmuch-search 'notmuch-doc "Search for messages.")
1035 ;;;###autoload
1036 (defun notmuch-search (&optional query oldest-first target-thread target-line
1037                                  no-display)
1038   "Display threads matching QUERY in a notmuch-search buffer.
1039
1040 If QUERY is nil, it is read interactively from the minibuffer.
1041 Other optional parameters are used as follows:
1042
1043   OLDEST-FIRST: A Boolean controlling the sort order of returned threads
1044   TARGET-THREAD: A thread ID (without the thread: prefix) that will be made
1045                  current if it appears in the search results.
1046   TARGET-LINE: The line number to move to if the target thread does not
1047                appear in the search results.
1048   NO-DISPLAY: Do not try to foreground the search results buffer. If it is
1049               already foregrounded i.e. displayed in a window, this has no
1050               effect, meaning the buffer will remain visible.
1051
1052 When called interactively, this will prompt for a query and use
1053 the configured default sort order."
1054   (interactive
1055    (list
1056     ;; Prompt for a query
1057     nil
1058     ;; Use the default search order (if we're doing a search from a
1059     ;; search buffer, ignore any buffer-local overrides)
1060     (default-value 'notmuch-search-oldest-first)))
1061
1062   (let* ((query (or query (notmuch-read-query "Notmuch search: ")))
1063          (buffer (get-buffer-create (notmuch-search-buffer-title query))))
1064     (if no-display
1065         (set-buffer buffer)
1066       (pop-to-buffer-same-window buffer))
1067     (notmuch-search-mode)
1068     ;; Don't track undo information for this buffer
1069     (setq buffer-undo-list t)
1070     (setq notmuch-search-query-string query)
1071     (setq notmuch-search-oldest-first oldest-first)
1072     (setq notmuch-search-target-thread target-thread)
1073     (setq notmuch-search-target-line target-line)
1074     (notmuch-tag-clear-cache)
1075     (when (get-buffer-process buffer)
1076       (error "notmuch search process already running for query `%s'" query))
1077     (let ((inhibit-read-only t))
1078       (erase-buffer)
1079       (goto-char (point-min))
1080       (save-excursion
1081         (let ((proc (notmuch-start-notmuch
1082                      "notmuch-search" buffer #'notmuch-search-process-sentinel
1083                      "search" "--format=sexp" "--format-version=5"
1084                      (if oldest-first
1085                          "--sort=oldest-first"
1086                        "--sort=newest-first")
1087                      query)))
1088           ;; Use a scratch buffer to accumulate partial output.
1089           ;; This buffer will be killed by the sentinel, which
1090           ;; should be called no matter how the process dies.
1091           (process-put proc 'parse-buf
1092                        (generate-new-buffer " *notmuch search parse*"))
1093           (set-process-filter proc 'notmuch-search-process-filter)
1094           (set-process-query-on-exit-flag proc nil))))))
1095
1096 (defun notmuch-search-refresh-view ()
1097   "Refresh the current view.
1098
1099 Erases the current buffer and runs a new search with the same
1100 query string as the current search. If the current thread is in
1101 the new search results, then point will be placed on the same
1102 thread. Otherwise, point will be moved to attempt to be in the
1103 same relative position within the new buffer."
1104   (interactive)
1105   (notmuch-search notmuch-search-query-string
1106                   notmuch-search-oldest-first
1107                   (notmuch-search-find-thread-id 'bare)
1108                   (line-number-at-pos)
1109                   t)
1110   (goto-char (point-min)))
1111
1112 (defun notmuch-search-toggle-order ()
1113   "Toggle the current search order.
1114
1115 This command toggles the sort order for the current search. The
1116 default sort order is defined by `notmuch-search-oldest-first'."
1117   (interactive)
1118   (setq notmuch-search-oldest-first (not notmuch-search-oldest-first))
1119   (notmuch-search-refresh-view))
1120
1121 (defun notmuch-group-disjunctive-query-string (query-string)
1122   "Group query if it contains a complex expression.
1123 Enclose QUERY-STRING in parentheses if contains \"OR\" operators."
1124   (if (string-match-p "\\<[oO][rR]\\>" query-string)
1125       (concat "( " query-string " )")
1126     query-string))
1127
1128 (defun notmuch-search-filter (query)
1129   "Filter or LIMIT the current search results based on an additional query string.
1130
1131 Runs a new search matching only messages that match both the
1132 current search results AND the additional query string provided."
1133   (interactive (list (notmuch-read-query "Filter search: ")))
1134   (let ((grouped-query (notmuch-group-disjunctive-query-string query))
1135         (grouped-original-query (notmuch-group-disjunctive-query-string
1136                                  notmuch-search-query-string)))
1137     (notmuch-search (if (string= grouped-original-query "*")
1138                         grouped-query
1139                       (concat grouped-original-query " and " grouped-query))
1140                     notmuch-search-oldest-first)))
1141
1142 (defun notmuch-search-filter-by-tag (tag)
1143   "Filter the current search results based on a single TAG.
1144
1145 Run a new search matching only messages that match the current
1146 search results and that are also tagged with the given TAG."
1147   (interactive
1148    (list (notmuch-select-tag-with-completion "Filter by tag: "
1149                                              notmuch-search-query-string)))
1150   (notmuch-search (concat notmuch-search-query-string " and tag:" tag)
1151                   notmuch-search-oldest-first))
1152
1153 (defun notmuch-search-by-tag (tag)
1154   "Display threads matching TAG in a notmuch-search buffer."
1155   (interactive
1156    (list (notmuch-select-tag-with-completion "Notmuch search tag: ")))
1157   (notmuch-search (concat "tag:" tag)))
1158
1159 ;;;###autoload
1160 (defun notmuch ()
1161   "Run notmuch and display saved searches, known tags, etc."
1162   (interactive)
1163   (notmuch-hello))
1164
1165 (defun notmuch-interesting-buffer (b)
1166   "Whether the current buffer's major-mode is a notmuch mode."
1167   (with-current-buffer b
1168     (memq major-mode '(notmuch-show-mode
1169                        notmuch-search-mode
1170                        notmuch-tree-mode
1171                        notmuch-hello-mode
1172                        notmuch-message-mode))))
1173
1174 ;;;###autoload
1175 (defun notmuch-cycle-notmuch-buffers ()
1176   "Cycle through any existing notmuch buffers (search, show or hello).
1177
1178 If the current buffer is the only notmuch buffer, bury it.
1179 If no notmuch buffers exist, run `notmuch'."
1180   (interactive)
1181   (let (start first)
1182     ;; If the current buffer is a notmuch buffer, remember it and then
1183     ;; bury it.
1184     (when (notmuch-interesting-buffer (current-buffer))
1185       (setq start (current-buffer))
1186       (bury-buffer))
1187
1188     ;; Find the first notmuch buffer.
1189     (setq first (cl-loop for buffer in (buffer-list)
1190                          if (notmuch-interesting-buffer buffer)
1191                          return buffer))
1192
1193     (if first
1194         ;; If the first one we found is any other than the starting
1195         ;; buffer, switch to it.
1196         (unless (eq first start)
1197           (pop-to-buffer-same-window first))
1198       (notmuch))))
1199
1200 ;;; Integrations
1201 ;;;; Hl-line Support
1202
1203 (defun notmuch-hl-line-mode ()
1204   (prog1 (hl-line-mode)
1205     (when hl-line-overlay
1206       (overlay-put hl-line-overlay 'priority 1))))
1207
1208 ;;;; Imenu Support
1209
1210 (defun notmuch-search-imenu-prev-index-position-function ()
1211   "Move point to previous message in notmuch-search buffer.
1212 Used as`imenu-prev-index-position-function' in notmuch buffers."
1213   (notmuch-search-previous-thread))
1214
1215 (defun notmuch-search-imenu-extract-index-name-function ()
1216   "Return imenu name for line at point.
1217 Used as `imenu-extract-index-name-function' in notmuch buffers.
1218 Point should be at the beginning of the line."
1219   (format "%s (%s)"
1220           (notmuch-search-find-subject)
1221           (notmuch-search-find-authors)))
1222
1223 ;;; _
1224
1225 (provide 'notmuch)
1226
1227 ;; After provide to avoid loops if notmuch was require'd via notmuch-init-file.
1228 (when init-file-user ; don't load init file if the -q option was used.
1229   (load notmuch-init-file t t nil t))
1230
1231 ;;; notmuch.el ends here