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