]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-tree.el
CLI: define and use format version 5
[notmuch] / emacs / notmuch-tree.el
1 ;;; notmuch-tree.el --- displaying notmuch forests  -*- lexical-binding: t -*-
2 ;;
3 ;; Copyright © Carl Worth
4 ;; Copyright © David Edmondson
5 ;; Copyright © Mark Walters
6 ;;
7 ;; This file is part of Notmuch.
8 ;;
9 ;; Notmuch is free software: you can redistribute it and/or modify it
10 ;; under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13 ;;
14 ;; Notmuch is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 ;; General Public License for more details.
18 ;;
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with Notmuch.  If not, see <https://www.gnu.org/licenses/>.
21 ;;
22 ;; Authors: David Edmondson <dme@dme.org>
23 ;;          Mark Walters <markwalters1009@gmail.com>
24
25 ;;; Code:
26
27 (require 'mail-parse)
28
29 (require 'notmuch-lib)
30 (require 'notmuch-query)
31 (require 'notmuch-show)
32 (require 'notmuch-tag)
33 (require 'notmuch-parser)
34 (require 'notmuch-jump)
35
36 (declare-function notmuch-search "notmuch"
37                   (&optional query oldest-first target-thread target-line
38                              no-display))
39 (declare-function notmuch-call-notmuch-process "notmuch-lib" (&rest args))
40 (declare-function notmuch-read-query "notmuch" (prompt))
41 (declare-function notmuch-search-find-thread-id "notmuch" (&optional bare))
42 (declare-function notmuch-search-find-subject "notmuch" ())
43
44 ;; For `notmuch-tree-next-thread-from-search'.
45 (declare-function notmuch-search-next-thread "notmuch" ())
46 (declare-function notmuch-search-previous-thread "notmuch" ())
47 (declare-function notmuch-tree-from-search-thread "notmuch" ())
48
49 ;; the following variable is defined in notmuch.el
50 (defvar notmuch-search-query-string)
51
52 ;; this variable distinguishes the unthreaded display from the normal tree display
53 (defvar-local notmuch-tree-unthreaded nil
54   "A buffer local copy of argument unthreaded to the function notmuch-tree.")
55
56 ;;; Options
57
58 (defgroup notmuch-tree nil
59   "Showing message and thread structure."
60   :group 'notmuch)
61
62 (defcustom notmuch-tree-show-out nil
63   "View selected messages in new window rather than split-pane."
64   :type 'boolean
65   :group 'notmuch-tree)
66
67 (defcustom notmuch-unthreaded-show-out t
68   "View selected messages in new window rather than split-pane."
69   :type 'boolean
70   :group 'notmuch-tree)
71
72 (defun notmuch-tree-show-out ()
73   (if notmuch-tree-unthreaded
74       notmuch-unthreaded-show-out
75     notmuch-tree-show-out))
76
77 (defcustom notmuch-tree-result-format
78   `(("date" . "%12s  ")
79     ("authors" . "%-20s")
80     ((("tree" . "%s")("subject" . "%s")) ." %-54s ")
81     ("tags" . "(%s)"))
82   "Result formatting for tree view. Supported fields are: date,
83 authors, subject, tree, tags.  Tree means the thread tree
84 box graphics. The field may also be a list in which case
85 the formatting rules are applied recursively and then the
86 output of all the fields in the list is inserted
87 according to format-string.
88
89 Note the author string should not contain
90 whitespace (put it in the neighbouring fields instead).
91 For example:
92         (setq notmuch-tree-result-format \(\(\"authors\" . \"%-40s\"\)
93                                           \(\"subject\" . \"%s\"\)\)\)"
94   :type '(alist :key-type (string) :value-type (string))
95   :group 'notmuch-tree)
96
97 (defcustom notmuch-unthreaded-result-format
98   `(("date" . "%12s  ")
99     ("authors" . "%-20s")
100     ((("subject" . "%s")) ." %-54s ")
101     ("tags" . "(%s)"))
102   "Result formatting for unthreaded tree view. Supported fields are: date,
103 authors, subject, tree, tags.  Tree means the thread tree
104 box graphics. The field may also be a list in which case
105 the formatting rules are applied recursively and then the
106 output of all the fields in the list is inserted
107 according to format-string.
108
109 Note the author string should not contain
110 whitespace (put it in the neighbouring fields instead).
111 For example:
112         (setq notmuch-tree-result-format \(\(\"authors\" . \"%-40s\"\)
113                                           \(\"subject\" . \"%s\"\)\)\)"
114   :type '(alist :key-type (string) :value-type (string))
115   :group 'notmuch-tree)
116
117 (defun notmuch-tree-result-format ()
118   (if notmuch-tree-unthreaded
119       notmuch-unthreaded-result-format
120     notmuch-tree-result-format))
121
122 ;;; Faces
123 ;;;; Faces for messages that match the query
124
125 (defface notmuch-tree-match-face
126   '((t :inherit default))
127   "Default face used in tree mode face for matching messages"
128   :group 'notmuch-tree
129   :group 'notmuch-faces)
130
131 (defface notmuch-tree-match-date-face
132   nil
133   "Face used in tree mode for the date in messages matching the query."
134   :group 'notmuch-tree
135   :group 'notmuch-faces)
136
137 (defface notmuch-tree-match-author-face
138   '((((class color)
139       (background dark))
140      (:foreground "OliveDrab1"))
141     (((class color)
142       (background light))
143      (:foreground "dark blue"))
144     (t
145      (:bold t)))
146   "Face used in tree mode for the date in messages matching the query."
147   :group 'notmuch-tree
148   :group 'notmuch-faces)
149
150 (defface notmuch-tree-match-subject-face
151   nil
152   "Face used in tree mode for the subject in messages matching the query."
153   :group 'notmuch-tree
154   :group 'notmuch-faces)
155
156 (defface notmuch-tree-match-tree-face
157   nil
158   "Face used in tree mode for the thread tree block graphics in messages matching the query."
159   :group 'notmuch-tree
160   :group 'notmuch-faces)
161
162 (defface notmuch-tree-match-tag-face
163   '((((class color)
164       (background dark))
165      (:foreground "OliveDrab1"))
166     (((class color)
167       (background light))
168      (:foreground "navy blue" :bold t))
169     (t
170      (:bold t)))
171   "Face used in tree mode for tags in messages matching the query."
172   :group 'notmuch-tree
173   :group 'notmuch-faces)
174
175 ;;;; Faces for messages that do not match the query
176
177 (defface notmuch-tree-no-match-face
178   '((t (:foreground "gray")))
179   "Default face used in tree mode face for non-matching messages."
180   :group 'notmuch-tree
181   :group 'notmuch-faces)
182
183 (defface notmuch-tree-no-match-date-face
184   nil
185   "Face used in tree mode for non-matching dates."
186   :group 'notmuch-tree
187   :group 'notmuch-faces)
188
189 (defface notmuch-tree-no-match-subject-face
190   nil
191   "Face used in tree mode for non-matching subjects."
192   :group 'notmuch-tree
193   :group 'notmuch-faces)
194
195 (defface notmuch-tree-no-match-tree-face
196   nil
197   "Face used in tree mode for the thread tree block graphics in messages matching the query."
198   :group 'notmuch-tree
199   :group 'notmuch-faces)
200
201 (defface notmuch-tree-no-match-author-face
202   nil
203   "Face used in tree mode for the date in messages matching the query."
204   :group 'notmuch-tree
205   :group 'notmuch-faces)
206
207 (defface notmuch-tree-no-match-tag-face
208   nil
209   "Face used in tree mode face for non-matching tags."
210   :group 'notmuch-tree
211   :group 'notmuch-faces)
212
213 ;;; Variables
214
215 (defvar-local notmuch-tree-previous-subject
216   "The subject of the most recent result shown during the async display.")
217
218 (defvar-local notmuch-tree-basic-query nil
219   "A buffer local copy of argument query to the function notmuch-tree.")
220
221 (defvar-local notmuch-tree-query-context nil
222   "A buffer local copy of argument query-context to the function notmuch-tree.")
223
224 (defvar-local notmuch-tree-target-msg nil
225   "A buffer local copy of argument target to the function notmuch-tree.")
226
227 (defvar-local notmuch-tree-open-target nil
228   "A buffer local copy of argument open-target to the function notmuch-tree.")
229
230 (defvar-local notmuch-tree-parent-buffer nil)
231
232 (defvar-local notmuch-tree-message-window nil
233   "The window of the message pane.
234
235 It is set in both the tree buffer and the child show buffer. It
236 is used to try and close the message pane when quitting tree view
237 or the child show buffer.")
238 (put 'notmuch-tree-message-window 'permanent-local t)
239
240 (defvar-local notmuch-tree-message-buffer nil
241   "The buffer name of the show buffer in the message pane.
242
243 This is used to try and make sure we don't close the message pane
244 if the user has loaded a different buffer in that window.")
245 (put 'notmuch-tree-message-buffer 'permanent-local t)
246
247 ;;; Tree wrapper commands
248
249 (defmacro notmuch-tree--define-do-in-message-window (name cmd)
250   "Define NAME as a command that calls CMD interactively in the message window.
251 If the message pane is closed then this command does nothing.
252 Avoid using this macro in new code; it will be removed."
253   `(defun ,name ()
254      ,(concat "(In message window) " (documentation cmd t))
255      (interactive)
256      (when (window-live-p notmuch-tree-message-window)
257        (with-selected-window notmuch-tree-message-window
258          (call-interactively #',cmd)))))
259
260 (notmuch-tree--define-do-in-message-window
261  notmuch-tree-previous-message-button
262  notmuch-show-previous-button)
263 (notmuch-tree--define-do-in-message-window
264  notmuch-tree-next-message-button
265  notmuch-show-next-button)
266 (notmuch-tree--define-do-in-message-window
267  notmuch-tree-toggle-message-process-crypto
268  notmuch-show-toggle-process-crypto)
269
270 (defun notmuch-tree--message-process-crypto ()
271   "Return value of `notmuch-show-process-crypto' in the message window.
272 If that window isn't alive, then return the current value.
273 Avoid using this function in new code; it will be removed."
274   (if (window-live-p notmuch-tree-message-window)
275       (with-selected-window notmuch-tree-message-window
276         notmuch-show-process-crypto)
277     notmuch-show-process-crypto))
278
279 (defmacro notmuch-tree--define-close-message-window-and (name cmd)
280   "Define NAME as a variant of CMD.
281
282 NAME determines the value of `notmuch-show-process-crypto' in the
283 message window, closes the window, and then call CMD interactively
284 with that value let-bound.  If the message window does not exist,
285 then NAME behaves like CMD."
286   `(defun ,name ()
287      ,(concat "(Close message pane and) " (documentation cmd t))
288      (interactive)
289      (let ((notmuch-show-process-crypto
290             (notmuch-tree--message-process-crypto)))
291        (notmuch-tree-close-message-window)
292        (call-interactively #',cmd))))
293
294 (notmuch-tree--define-close-message-window-and
295  notmuch-tree-help
296  notmuch-help)
297 (notmuch-tree--define-close-message-window-and
298  notmuch-tree-new-mail
299  notmuch-mua-new-mail)
300 (notmuch-tree--define-close-message-window-and
301  notmuch-tree-jump-search
302  notmuch-jump-search)
303 (notmuch-tree--define-close-message-window-and
304  notmuch-tree-forward-message
305  notmuch-show-forward-message)
306 (notmuch-tree--define-close-message-window-and
307  notmuch-tree-reply-sender
308  notmuch-show-reply-sender)
309 (notmuch-tree--define-close-message-window-and
310  notmuch-tree-reply
311  notmuch-show-reply)
312 (notmuch-tree--define-close-message-window-and
313  notmuch-tree-view-raw-message
314  notmuch-show-view-raw-message)
315
316 ;;; Keymap
317
318 (defvar notmuch-tree-mode-map
319   (let ((map (make-sparse-keymap)))
320     (set-keymap-parent map notmuch-common-keymap)
321     ;; These bindings shadow common bindings with variants
322     ;; that additionally close the message window.
323     (define-key map [remap notmuch-bury-or-kill-this-buffer] 'notmuch-tree-quit)
324     (define-key map [remap notmuch-search]        'notmuch-tree-to-search)
325     (define-key map [remap notmuch-help]          'notmuch-tree-help)
326     (define-key map [remap notmuch-mua-new-mail]  'notmuch-tree-new-mail)
327     (define-key map [remap notmuch-jump-search]   'notmuch-tree-jump-search)
328
329     (define-key map "o" 'notmuch-tree-toggle-order)
330     (define-key map "S" 'notmuch-search-from-tree-current-query)
331     (define-key map "U" 'notmuch-unthreaded-from-tree-current-query)
332     (define-key map "Z" 'notmuch-tree-from-unthreaded-current-query)
333
334     ;; these use notmuch-show functions directly
335     (define-key map "|" 'notmuch-show-pipe-message)
336     (define-key map "w" 'notmuch-show-save-attachments)
337     (define-key map "v" 'notmuch-show-view-all-mime-parts)
338     (define-key map "c" 'notmuch-show-stash-map)
339     (define-key map "b" 'notmuch-show-resend-message)
340
341     ;; these apply to the message pane
342     (define-key map (kbd "M-TAB")     'notmuch-tree-previous-message-button)
343     (define-key map (kbd "<backtab>") 'notmuch-tree-previous-message-button)
344     (define-key map (kbd "TAB")       'notmuch-tree-next-message-button)
345     (define-key map "$" 'notmuch-tree-toggle-message-process-crypto)
346
347     ;; bindings from show (or elsewhere) but we close the message pane first.
348     (define-key map "f" 'notmuch-tree-forward-message)
349     (define-key map "r" 'notmuch-tree-reply-sender)
350     (define-key map "R" 'notmuch-tree-reply)
351     (define-key map "V" 'notmuch-tree-view-raw-message)
352     (define-key map "l" 'notmuch-tree-filter)
353     (define-key map "t" 'notmuch-tree-filter-by-tag)
354
355     ;; The main tree view bindings
356     (define-key map (kbd "RET") 'notmuch-tree-show-message)
357     (define-key map [mouse-1] 'notmuch-tree-show-message)
358     (define-key map "x" 'notmuch-tree-archive-message-then-next-or-exit)
359     (define-key map "X" 'notmuch-tree-archive-thread-then-exit)
360     (define-key map "A" 'notmuch-tree-archive-thread-then-next)
361     (define-key map "a" 'notmuch-tree-archive-message-then-next)
362     (define-key map "z" 'notmuch-tree-to-tree)
363     (define-key map "n" 'notmuch-tree-next-matching-message)
364     (define-key map "p" 'notmuch-tree-prev-matching-message)
365     (define-key map "N" 'notmuch-tree-next-message)
366     (define-key map "P" 'notmuch-tree-prev-message)
367     (define-key map (kbd "M-p") 'notmuch-tree-prev-thread)
368     (define-key map (kbd "M-n") 'notmuch-tree-next-thread)
369     (define-key map "k" 'notmuch-tag-jump)
370     (define-key map "-" 'notmuch-tree-remove-tag)
371     (define-key map "+" 'notmuch-tree-add-tag)
372     (define-key map "*" 'notmuch-tree-tag-thread)
373     (define-key map " " 'notmuch-tree-scroll-or-next)
374     (define-key map (kbd "DEL") 'notmuch-tree-scroll-message-window-back)
375     (define-key map "e" 'notmuch-tree-resume-message)
376     map)
377   "Keymap for \"notmuch tree\" buffers.")
378
379 ;;; Message properties
380
381 (defun notmuch-tree-get-message-properties ()
382   "Return the properties of the current message as a plist.
383
384 Some useful entries are:
385 :headers - Property list containing the headers :Date, :Subject, :From, etc.
386 :tags - Tags for this message."
387   (save-excursion
388     (beginning-of-line)
389     (get-text-property (point) :notmuch-message-properties)))
390
391 (defun notmuch-tree-set-message-properties (props)
392   (save-excursion
393     (beginning-of-line)
394     (put-text-property (point)
395                        (+ (point) 1)
396                        :notmuch-message-properties props)))
397
398 (defun notmuch-tree-set-prop (prop val &optional props)
399   (let ((inhibit-read-only t)
400         (props (or props
401                    (notmuch-tree-get-message-properties))))
402     (plist-put props prop val)
403     (notmuch-tree-set-message-properties props)))
404
405 (defun notmuch-tree-get-prop (prop &optional props)
406   (plist-get (or props (notmuch-tree-get-message-properties))
407              prop))
408
409 (defun notmuch-tree-set-tags (tags)
410   "Set the tags of the current message."
411   (notmuch-tree-set-prop :tags tags))
412
413 (defun notmuch-tree-get-tags ()
414   "Return the tags of the current message."
415   (notmuch-tree-get-prop :tags))
416
417 (defun notmuch-tree-get-message-id (&optional bare)
418   "Return the message id of the current message."
419   (let ((id (notmuch-tree-get-prop :id)))
420     (if id
421         (if bare
422             id
423           (notmuch-id-to-query id))
424       nil)))
425
426 (defun notmuch-tree-get-match ()
427   "Return whether the current message is a match."
428   (notmuch-tree-get-prop :match))
429
430 ;;; Update display
431
432 (defun notmuch-tree-refresh-result ()
433   "Redisplay the current message line.
434
435 This redisplays the current line based on the messages
436 properties (as they are now). This is used when tags are
437 updated."
438   (let ((init-point (point))
439         (end (line-end-position))
440         (msg (notmuch-tree-get-message-properties))
441         (inhibit-read-only t))
442     (beginning-of-line)
443     ;; This is a little tricky: we override
444     ;; notmuch-tree-previous-subject to get the decision between
445     ;; ... and a subject right and it stops notmuch-tree-insert-msg
446     ;; from overwriting the buffer local copy of
447     ;; notmuch-tree-previous-subject if this is called while the
448     ;; buffer is displaying.
449     (let ((notmuch-tree-previous-subject
450            (notmuch-tree-get-prop :previous-subject)))
451       (delete-region (point) (1+ (line-end-position)))
452       (notmuch-tree-insert-msg msg))
453     (let ((new-end (line-end-position)))
454       (goto-char (if (= init-point end)
455                      new-end
456                    (min init-point (- new-end 1)))))))
457
458 (defun notmuch-tree-tag-update-display (&optional tag-changes)
459   "Update display for TAG-CHANGES to current message.
460
461 Updates the message in the message pane if appropriate, but does
462 NOT change the database."
463   (let* ((current-tags (notmuch-tree-get-tags))
464          (new-tags (notmuch-update-tags current-tags tag-changes))
465          (tree-msg-id (notmuch-tree-get-message-id)))
466     (unless (equal current-tags new-tags)
467       (notmuch-tree-set-tags new-tags)
468       (notmuch-tree-refresh-result)
469       (when (window-live-p notmuch-tree-message-window)
470         (with-selected-window notmuch-tree-message-window
471           (when (string= tree-msg-id (notmuch-show-get-message-id))
472             (notmuch-show-update-tags new-tags)))))))
473
474 ;;; Commands (and some helper functions used by them)
475
476 (defun notmuch-tree-tag (tag-changes)
477   "Change tags for the current message."
478   (interactive
479    (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message")))
480   (notmuch-tag (notmuch-tree-get-message-id) tag-changes)
481   (notmuch-tree-tag-update-display tag-changes))
482
483 (defun notmuch-tree-add-tag (tag-changes)
484   "Same as `notmuch-tree-tag' but sets initial input to '+'."
485   (interactive
486    (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "+")))
487   (notmuch-tree-tag tag-changes))
488
489 (defun notmuch-tree-remove-tag (tag-changes)
490   "Same as `notmuch-tree-tag' but sets initial input to '-'."
491   (interactive
492    (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "-")))
493   (notmuch-tree-tag tag-changes))
494
495 (defun notmuch-tree-resume-message ()
496   "Resume EDITING the current draft message."
497   (interactive)
498   (notmuch-tree-close-message-window)
499   (let ((id (notmuch-tree-get-message-id)))
500     (if id
501         (notmuch-draft-resume id)
502       (message "No message to resume!"))))
503
504 ;; The next two functions close the message window before calling
505 ;; notmuch-search or notmuch-tree but they do so after the user has
506 ;; entered the query (in case the user was basing the query on
507 ;; something in the message window).
508
509 (defun notmuch-tree-to-search ()
510   "Run \"notmuch search\" with the given `query' and display results."
511   (interactive)
512   (let ((query (notmuch-read-query "Notmuch search: ")))
513     (notmuch-tree-close-message-window)
514     (notmuch-search query)))
515
516 (defun notmuch-tree-to-tree ()
517   "Run a query and display results in tree view."
518   (interactive)
519   (let ((query (notmuch-read-query "Notmuch tree view search: ")))
520     (notmuch-tree-close-message-window)
521     (notmuch-tree query)))
522
523 (defun notmuch-tree-archive-thread-then-next ()
524   "Archive all messages in the current buffer, then show next thread from search."
525   (interactive)
526   (notmuch-tree-archive-thread)
527   (notmuch-tree-next-thread))
528
529 (defun notmuch-unthreaded-from-tree-current-query ()
530   "Switch from tree view to unthreaded view."
531   (interactive)
532   (unless notmuch-tree-unthreaded
533     (notmuch-tree-refresh-view 'unthreaded)))
534
535 (defun notmuch-tree-from-unthreaded-current-query ()
536   "Switch from unthreaded view to tree view."
537   (interactive)
538   (when notmuch-tree-unthreaded
539     (notmuch-tree-refresh-view 'tree)))
540
541 (defun notmuch-search-from-tree-current-query ()
542   "Call notmuch search with the current query."
543   (interactive)
544   (notmuch-tree-close-message-window)
545   (notmuch-search (notmuch-tree-get-query)))
546
547 (defun notmuch-tree-message-window-kill-hook ()
548   "Close the message pane when exiting the show buffer."
549   (let ((buffer (current-buffer)))
550     (when (and (window-live-p notmuch-tree-message-window)
551                (eq (window-buffer notmuch-tree-message-window) buffer))
552       ;; We could check whether this is the only window in its frame,
553       ;; but simply ignoring the error that is thrown otherwise is
554       ;; what we had to do for Emacs 24 and we stick to that because
555       ;; it is still the simplest approach.
556       (ignore-errors
557         (delete-window notmuch-tree-message-window)))))
558
559 (defun notmuch-tree-command-hook ()
560   (when (eq major-mode 'notmuch-tree-mode)
561     ;; We just run the notmuch-show-command-hook on the message pane.
562     (when (buffer-live-p notmuch-tree-message-buffer)
563       (with-current-buffer notmuch-tree-message-buffer
564         (notmuch-show-command-hook)))))
565
566 (defun notmuch-tree-show-message-in ()
567   "Show the current message (in split-pane)."
568   (interactive)
569   (let ((id (notmuch-tree-get-message-id))
570         (inhibit-read-only t)
571         buffer)
572     (when id
573       ;; We close and reopen the window to kill off un-needed buffers
574       ;; this might cause flickering but seems ok.
575       (notmuch-tree-close-message-window)
576       (setq notmuch-tree-message-window
577             (split-window-vertically (/ (window-height) 4)))
578       (with-selected-window notmuch-tree-message-window
579         (let (;; Since we are only displaying one message do not indent.
580               (notmuch-show-indent-messages-width 0)
581               (notmuch-show-single-message t)
582               ;; Ensure that `pop-to-buffer-same-window' uses the
583               ;; window we want it to use.
584               (display-buffer-overriding-action
585                  '((display-buffer-same-window)
586                    (inhibit-same-window . nil))))
587           (setq buffer (notmuch-show id))))
588       ;; We need the `let' as notmuch-tree-message-window is buffer local.
589       (let ((window notmuch-tree-message-window))
590         (with-current-buffer buffer
591           (setq notmuch-tree-message-window window)
592           (add-hook 'kill-buffer-hook 'notmuch-tree-message-window-kill-hook)))
593       (when notmuch-show-mark-read-tags
594         (notmuch-tree-tag-update-display notmuch-show-mark-read-tags))
595       (setq notmuch-tree-message-buffer buffer))))
596
597 (defun notmuch-tree-show-message-out ()
598   "Show the current message (in whole window)."
599   (interactive)
600   (let ((id (notmuch-tree-get-message-id))
601         (inhibit-read-only t))
602     (when id
603       ;; We close the window to kill off un-needed buffers.
604       (notmuch-tree-close-message-window)
605       ;; n-s-s-m is buffer local, so use inner let.
606       (let ((notmuch-show-single-message t))
607         (notmuch-show id)))))
608
609 (defun notmuch-tree-show-message (arg)
610   "Show the current message.
611
612 Shows in split pane or whole window according to value of
613 `notmuch-tree-show-out'. A prefix argument reverses the choice."
614   (interactive "P")
615   (if (or (and (notmuch-tree-show-out) (not arg))
616           (and (not (notmuch-tree-show-out)) arg))
617       (notmuch-tree-show-message-out)
618     (notmuch-tree-show-message-in)))
619
620 (defun notmuch-tree-scroll-message-window ()
621   "Scroll the message window (if it exists)."
622   (interactive)
623   (when (window-live-p notmuch-tree-message-window)
624     (with-selected-window notmuch-tree-message-window
625       (if (pos-visible-in-window-p (point-max))
626           t
627         (scroll-up)))))
628
629 (defun notmuch-tree-scroll-message-window-back ()
630   "Scroll the message window back (if it exists)."
631   (interactive)
632   (when (window-live-p notmuch-tree-message-window)
633     (with-selected-window notmuch-tree-message-window
634       (if (pos-visible-in-window-p (point-min))
635           t
636         (scroll-down)))))
637
638 (defun notmuch-tree-scroll-or-next ()
639   "Scroll the message window.
640 If it at end go to next message."
641   (interactive)
642   (when (notmuch-tree-scroll-message-window)
643     (notmuch-tree-next-matching-message)))
644
645 (defun notmuch-tree-quit (&optional kill-both)
646   "Close the split view or exit tree."
647   (interactive "P")
648   (when (or (not (notmuch-tree-close-message-window)) kill-both)
649     (kill-buffer (current-buffer))))
650
651 (defun notmuch-tree-close-message-window ()
652   "Close the message-window. Return t if close succeeds."
653   (interactive)
654   (when (and (window-live-p notmuch-tree-message-window)
655              (eq (window-buffer notmuch-tree-message-window)
656                  notmuch-tree-message-buffer))
657     (delete-window notmuch-tree-message-window)
658     (unless (get-buffer-window-list notmuch-tree-message-buffer)
659       (kill-buffer notmuch-tree-message-buffer))
660     t))
661
662 (defun notmuch-tree-archive-message (&optional unarchive)
663   "Archive the current message.
664
665 Archive the current message by applying the tag changes in
666 `notmuch-archive-tags' to it. If a prefix argument is given, the
667 message will be \"unarchived\", i.e. the tag changes in
668 `notmuch-archive-tags' will be reversed."
669   (interactive "P")
670   (when notmuch-archive-tags
671     (notmuch-tree-tag
672      (notmuch-tag-change-list notmuch-archive-tags unarchive))))
673
674 (defun notmuch-tree-archive-message-then-next (&optional unarchive)
675   "Archive the current message and move to next matching message."
676   (interactive "P")
677   (notmuch-tree-archive-message unarchive)
678   (notmuch-tree-next-matching-message))
679
680 (defun notmuch-tree-archive-thread-then-exit ()
681   "Archive all messages in the current buffer, then exit notmuch-tree."
682   (interactive)
683   (notmuch-tree-archive-thread)
684   (notmuch-tree-quit t))
685
686 (defun notmuch-tree-archive-message-then-next-or-exit ()
687   "Archive current message, then show next open message in current thread.
688
689 If at the last open message in the current thread, then exit back
690 to search results."
691   (interactive)
692   (notmuch-tree-archive-message)
693   (notmuch-tree-next-matching-message t))
694
695 (defun notmuch-tree-next-message ()
696   "Move to next message."
697   (interactive)
698   (forward-line)
699   (when (window-live-p notmuch-tree-message-window)
700     (notmuch-tree-show-message-in)))
701
702 (defun notmuch-tree-prev-message ()
703   "Move to previous message."
704   (interactive)
705   (forward-line -1)
706   (when (window-live-p notmuch-tree-message-window)
707     (notmuch-tree-show-message-in)))
708
709 (defun notmuch-tree-goto-matching-message (&optional prev)
710   "Move to the next or previous matching message.
711
712 Returns t if there was a next matching message in the thread to show,
713 nil otherwise."
714   (let ((dir (if prev -1 nil))
715         (eobfn (if prev #'bobp #'eobp)))
716     (while (and (not (funcall eobfn))
717                 (not (notmuch-tree-get-match)))
718       (forward-line dir))
719     (not (funcall eobfn))))
720
721 (defun notmuch-tree-matching-message (&optional prev pop-at-end)
722   "Move to the next or previous matching message."
723   (interactive "P")
724   (forward-line (if prev -1 nil))
725   (if (and (not (notmuch-tree-goto-matching-message prev)) pop-at-end)
726       (notmuch-tree-quit pop-at-end)
727     (when (window-live-p notmuch-tree-message-window)
728       (notmuch-tree-show-message-in))))
729
730 (defun notmuch-tree-prev-matching-message (&optional pop-at-end)
731   "Move to previous matching message."
732   (interactive "P")
733   (notmuch-tree-matching-message t pop-at-end))
734
735 (defun notmuch-tree-next-matching-message (&optional pop-at-end)
736   "Move to next matching message."
737   (interactive "P")
738   (notmuch-tree-matching-message nil pop-at-end))
739
740 (defun notmuch-tree-refresh-view (&optional view)
741   "Refresh view."
742   (interactive)
743   (when (get-buffer-process (current-buffer))
744     (error "notmuch tree process already running for current buffer"))
745   (let ((inhibit-read-only t)
746         (basic-query notmuch-tree-basic-query)
747         (unthreaded (cond ((eq view 'unthreaded) t)
748                           ((eq view 'tree) nil)
749                           (t notmuch-tree-unthreaded)))
750         (query-context notmuch-tree-query-context)
751         (target (notmuch-tree-get-message-id)))
752     (erase-buffer)
753     (notmuch-tree-worker basic-query
754                          query-context
755                          target
756                          nil
757                          unthreaded
758                          notmuch-search-oldest-first)))
759
760 (defun notmuch-tree-thread-top ()
761   (when (notmuch-tree-get-message-properties)
762     (while (not (or (notmuch-tree-get-prop :first) (eobp)))
763       (forward-line -1))))
764
765 (defun notmuch-tree-prev-thread-in-tree ()
766   "Move to the previous thread in the current tree"
767   (interactive)
768   (forward-line -1)
769   (notmuch-tree-thread-top)
770   (not (bobp)))
771
772 (defun notmuch-tree-next-thread-in-tree ()
773   "Get the next thread in the current tree. Returns t if a thread was
774 found or nil if not."
775   (interactive)
776   (forward-line 1)
777   (while (not (or (notmuch-tree-get-prop :first) (eobp)))
778     (forward-line 1))
779   (not (eobp)))
780
781 (defun notmuch-tree-next-thread-from-search (&optional previous)
782   "Move to the next thread in the parent search results, if any.
783
784 If PREVIOUS is non-nil, move to the previous item in the
785 search results instead."
786   (interactive "P")
787   (let ((parent-buffer notmuch-tree-parent-buffer))
788     (notmuch-tree-quit t)
789     (when (buffer-live-p parent-buffer)
790       (switch-to-buffer parent-buffer)
791       (if previous
792           (notmuch-search-previous-thread)
793         (notmuch-search-next-thread))
794       (notmuch-tree-from-search-thread))))
795
796 (defun notmuch-tree-next-thread (&optional previous)
797   "Move to the next thread in the current tree or parent search results.
798
799 If PREVIOUS is non-nil, move to the previous thread in the tree or
800 search results instead."
801   (interactive)
802   (unless (if previous (notmuch-tree-prev-thread-in-tree)
803             (notmuch-tree-next-thread-in-tree))
804     (notmuch-tree-next-thread-from-search previous)))
805
806 (defun notmuch-tree-prev-thread ()
807   "Move to the previous thread in the current tree or parent search results."
808   (interactive)
809   (notmuch-tree-next-thread t))
810
811 (defun notmuch-tree-thread-mapcar (function)
812   "Call FUNCTION for each message in the current thread.
813 FUNCTION is called for side effects only."
814   (save-excursion
815     (notmuch-tree-thread-top)
816     (cl-loop collect (funcall function)
817              do (forward-line)
818              while (and (notmuch-tree-get-message-properties)
819                         (not (notmuch-tree-get-prop :first))))))
820
821 (defun notmuch-tree-get-messages-ids-thread-search ()
822   "Return a search string for all message ids of messages in the current thread."
823   (mapconcat 'identity
824              (notmuch-tree-thread-mapcar 'notmuch-tree-get-message-id)
825              " or "))
826
827 (defun notmuch-tree-tag-thread (tag-changes)
828   "Tag all messages in the current thread."
829   (interactive
830    (let ((tags (apply #'append (notmuch-tree-thread-mapcar
831                                 (lambda () (notmuch-tree-get-tags))))))
832      (list (notmuch-read-tag-changes tags "Tag thread"))))
833   (when (notmuch-tree-get-message-properties)
834     (notmuch-tag (notmuch-tree-get-messages-ids-thread-search) tag-changes)
835     (notmuch-tree-thread-mapcar
836      (lambda () (notmuch-tree-tag-update-display tag-changes)))))
837
838 (defun notmuch-tree-archive-thread (&optional unarchive)
839   "Archive each message in thread.
840
841 Archive each message currently shown by applying the tag changes
842 in `notmuch-archive-tags' to each. If a prefix argument is given,
843 the messages will be \"unarchived\", i.e. the tag changes in
844 `notmuch-archive-tags' will be reversed.
845
846 Note: This command is safe from any race condition of new messages
847 being delivered to the same thread. It does not archive the
848 entire thread, but only the messages shown in the current
849 buffer."
850   (interactive "P")
851   (when notmuch-archive-tags
852     (notmuch-tree-tag-thread
853      (notmuch-tag-change-list notmuch-archive-tags unarchive))))
854
855 ;;; Functions for displaying the tree buffer itself
856
857 (defun notmuch-tree-clean-address (address)
858   "Try to clean a single email ADDRESS for display. Return
859 AUTHOR_NAME if present, otherwise return AUTHOR_EMAIL. Return
860 unchanged ADDRESS if parsing fails."
861   (let* ((clean-address (notmuch-clean-address address))
862          (p-address (car clean-address))
863          (p-name (cdr clean-address)))
864
865     ;; If we have a name return that otherwise return the address.
866     (or p-name p-address)))
867
868 (defun notmuch-tree-format-field (field format-string msg)
869   "Format a FIELD of MSG according to FORMAT-STRING and return string."
870   (let* ((headers (plist-get msg :headers))
871          (match (plist-get msg :match)))
872     (cond
873      ((listp field)
874       (format format-string (notmuch-tree-format-field-list field msg)))
875
876      ((string-equal field "date")
877       (let ((face (if match
878                       'notmuch-tree-match-date-face
879                     'notmuch-tree-no-match-date-face)))
880         (propertize (format format-string (plist-get msg :date_relative))
881                     'face face)))
882
883      ((string-equal field "tree")
884       (let ((tree-status (plist-get msg :tree-status))
885             (face (if match
886                       'notmuch-tree-match-tree-face
887                     'notmuch-tree-no-match-tree-face)))
888
889         (propertize (format format-string
890                             (mapconcat #'identity (reverse tree-status) ""))
891                     'face face)))
892
893      ((string-equal field "subject")
894       (let ((bare-subject (notmuch-show-strip-re (plist-get headers :Subject)))
895             (previous-subject notmuch-tree-previous-subject)
896             (face (if match
897                       'notmuch-tree-match-subject-face
898                     'notmuch-tree-no-match-subject-face)))
899
900         (setq notmuch-tree-previous-subject bare-subject)
901         (propertize (format format-string
902                             (if (string= previous-subject bare-subject)
903                                 " ..."
904                               bare-subject))
905                     'face face)))
906
907      ((string-equal field "authors")
908       (let ((author (notmuch-tree-clean-address (plist-get headers :From)))
909             (len (length (format format-string "")))
910             (face (if match
911                       'notmuch-tree-match-author-face
912                     'notmuch-tree-no-match-author-face)))
913         (when (> (length author) len)
914           (setq author (substring author 0 len)))
915         (propertize (format format-string author) 'face face)))
916
917      ((string-equal field "tags")
918       (let ((tags (plist-get msg :tags))
919             (orig-tags (plist-get msg :orig-tags))
920             (face (if match
921                       'notmuch-tree-match-tag-face
922                     'notmuch-tree-no-match-tag-face)))
923         (format format-string (notmuch-tag-format-tags tags orig-tags face)))))))
924
925 (defun notmuch-tree-format-field-list (field-list msg)
926   "Format fields of MSG according to FIELD-LIST and return string."
927   (let ((face (if (plist-get msg :match)
928                   'notmuch-tree-match-face
929                 'notmuch-tree-no-match-face))
930         (result-string))
931     (dolist (spec field-list result-string)
932       (let ((field-string (notmuch-tree-format-field (car spec) (cdr spec) msg)))
933         (setq result-string (concat result-string field-string))))
934     (notmuch-apply-face result-string face t)))
935
936 (defun notmuch-tree-insert-msg (msg)
937   "Insert the message MSG according to notmuch-tree-result-format."
938   ;; We need to save the previous subject as it will get overwritten
939   ;; by the insert-field calls.
940   (let ((previous-subject notmuch-tree-previous-subject))
941     (insert (notmuch-tree-format-field-list (notmuch-tree-result-format) msg))
942     (notmuch-tree-set-message-properties msg)
943     (notmuch-tree-set-prop :previous-subject previous-subject)
944     (insert "\n")))
945
946 (defun notmuch-tree-goto-and-insert-msg (msg)
947   "Insert msg at the end of the buffer. Move point to msg if it is the target."
948   (save-excursion
949     (goto-char (point-max))
950     (notmuch-tree-insert-msg msg))
951   (let ((msg-id (notmuch-id-to-query (plist-get msg :id)))
952         (target notmuch-tree-target-msg))
953     (when (or (and (not target) (plist-get msg :match))
954               (string= msg-id target))
955       (setq notmuch-tree-target-msg "found")
956       (goto-char (point-max))
957       (forward-line -1)
958       (when notmuch-tree-open-target
959         (notmuch-tree-show-message-in)
960         (notmuch-tree-command-hook)))))
961
962 (defun notmuch-tree-insert-tree (tree depth tree-status first last)
963   "Insert the message tree TREE at depth DEPTH in the current thread.
964
965 A message tree is another name for a single sub-thread: i.e., a
966 message together with all its descendents."
967   (let ((msg (car tree))
968         (replies (cadr tree)))
969     (cond
970      ((and (< 0 depth) (not last))
971       (push "├" tree-status))
972      ((and (< 0 depth) last)
973       (push "╰" tree-status))
974      ((and (eq 0 depth) first last)
975       ;; Choice between these two variants is a matter of taste.
976       ;; (push "─" tree-status))
977       (push " " tree-status))
978      ((and (eq 0 depth) first (not last))
979       (push "┬" tree-status))
980      ((and (eq 0 depth) (not first) last)
981       (push "╰" tree-status))
982      ((and (eq 0 depth) (not first) (not last))
983       (push "├" tree-status)))
984     (push (concat (if replies "┬" "─") "►") tree-status)
985     (setq msg (plist-put msg :first (and first (eq 0 depth))))
986     (setq msg (plist-put msg :tree-status tree-status))
987     (setq msg (plist-put msg :orig-tags (plist-get msg :tags)))
988     (notmuch-tree-goto-and-insert-msg msg)
989     (pop tree-status)
990     (pop tree-status)
991     (if last
992         (push " " tree-status)
993       (push "│" tree-status))
994     (notmuch-tree-insert-thread replies (1+ depth) tree-status)))
995
996 (defun notmuch-tree-insert-thread (thread depth tree-status)
997   "Insert the collection of sibling sub-threads THREAD at depth DEPTH in the current forest."
998   (let ((n (length thread)))
999     (cl-loop for tree in thread
1000              for count from 1 to n
1001              do (notmuch-tree-insert-tree tree depth tree-status
1002                                           (eq count 1)
1003                                           (eq count n)))))
1004
1005 (defun notmuch-tree-insert-forest-thread (forest-thread)
1006   "Insert a single complete thread."
1007   ;; Reset at the start of each main thread.
1008   (setq notmuch-tree-previous-subject nil)
1009   (notmuch-tree-insert-thread forest-thread 0 nil))
1010
1011 (defun notmuch-tree-insert-forest (forest)
1012   "Insert a forest of threads.
1013
1014 This function inserts a collection of several complete threads as
1015 passed to it by notmuch-tree-process-filter."
1016   (mapc 'notmuch-tree-insert-forest-thread forest))
1017
1018 (define-derived-mode notmuch-tree-mode fundamental-mode "notmuch-tree"
1019   "Major mode displaying messages (as opposed to threads) of a notmuch search.
1020
1021 This buffer contains the results of a \"notmuch tree\" of your
1022 email archives. Each line in the buffer represents a single
1023 message giving the relative date, the author, subject, and any
1024 tags.
1025
1026 Pressing \\[notmuch-tree-show-message] on any line displays that message.
1027
1028 Complete list of currently available key bindings:
1029
1030 \\{notmuch-tree-mode-map}"
1031   (setq notmuch-buffer-refresh-function #'notmuch-tree-refresh-view)
1032   (hl-line-mode 1)
1033   (setq buffer-read-only t)
1034   (setq truncate-lines t))
1035
1036 (defun notmuch-tree-process-sentinel (proc _msg)
1037   "Add a message to let user know when \"notmuch tree\" exits."
1038   (let ((buffer (process-buffer proc))
1039         (status (process-status proc))
1040         (exit-status (process-exit-status proc)))
1041     (when (memq status '(exit signal))
1042       (kill-buffer (process-get proc 'parse-buf))
1043       (when (buffer-live-p buffer)
1044         (with-current-buffer buffer
1045           (save-excursion
1046             (let ((inhibit-read-only t))
1047               (goto-char (point-max))
1048               (when (eq status 'signal)
1049                 (insert "Incomplete search results (tree view process was killed).\n"))
1050               (when (eq status 'exit)
1051                 (insert "End of search results.")
1052                 (unless (= exit-status 0)
1053                   (insert (format " (process returned %d)" exit-status)))
1054                 (insert "\n")))))))))
1055
1056 (defun notmuch-tree-process-filter (proc string)
1057   "Process and filter the output of \"notmuch show\" for tree view."
1058   (let ((results-buf (process-buffer proc))
1059         (parse-buf (process-get proc 'parse-buf))
1060         (inhibit-read-only t))
1061     (if (not (buffer-live-p results-buf))
1062         (delete-process proc)
1063       (with-current-buffer parse-buf
1064         ;; Insert new data
1065         (save-excursion
1066           (goto-char (point-max))
1067           (insert string))
1068         (notmuch-sexp-parse-partial-list 'notmuch-tree-insert-forest-thread
1069                                          results-buf)))))
1070
1071 (defun notmuch-tree-worker (basic-query &optional query-context target
1072                                         open-target unthreaded oldest-first)
1073   "Insert the tree view of the search in the current buffer.
1074
1075 This is is a helper function for notmuch-tree. The arguments are
1076 the same as for the function notmuch-tree."
1077   (interactive)
1078   (notmuch-tree-mode)
1079   (add-hook 'post-command-hook #'notmuch-tree-command-hook t t)
1080   (setq notmuch-search-oldest-first oldest-first)
1081   (setq notmuch-tree-unthreaded unthreaded)
1082   (setq notmuch-tree-basic-query basic-query)
1083   (setq notmuch-tree-query-context (if (or (string= query-context "")
1084                                            (string= query-context "*"))
1085                                        nil
1086                                      query-context))
1087   (setq notmuch-tree-target-msg target)
1088   (setq notmuch-tree-open-target open-target)
1089   ;; Set the default value for `notmuch-show-process-crypto' in this
1090   ;; buffer. Although we don't use this some of the functions we call
1091   ;; (such as reply) do. It is a buffer local variable so setting it
1092   ;; will not affect genuine show buffers.
1093   (setq notmuch-show-process-crypto notmuch-crypto-process-mime)
1094   (erase-buffer)
1095   (goto-char (point-min))
1096   (let* ((search-args (concat basic-query
1097                               (and query-context
1098                                    (concat " and (" query-context ")"))))
1099          (sort-arg (if oldest-first "--sort=oldest-first" "--sort=newest-first"))
1100          (message-arg (if unthreaded "--unthreaded" "--entire-thread")))
1101     (when (equal (car (process-lines notmuch-command "count" search-args)) "0")
1102       (setq search-args basic-query))
1103     (notmuch-tag-clear-cache)
1104     (let ((proc (notmuch-start-notmuch
1105                  "notmuch-tree" (current-buffer) #'notmuch-tree-process-sentinel
1106                  "show" "--body=false" "--format=sexp" "--format-version=5"
1107                  sort-arg message-arg search-args))
1108           ;; Use a scratch buffer to accumulate partial output.
1109           ;; This buffer will be killed by the sentinel, which
1110           ;; should be called no matter how the process dies.
1111           (parse-buf (generate-new-buffer " *notmuch tree parse*")))
1112       (process-put proc 'parse-buf parse-buf)
1113       (set-process-filter proc 'notmuch-tree-process-filter)
1114       (set-process-query-on-exit-flag proc nil))))
1115
1116 (defun notmuch-tree-get-query ()
1117   "Return the current query in this tree buffer."
1118   (if notmuch-tree-query-context
1119       (concat notmuch-tree-basic-query
1120               " and ("
1121               notmuch-tree-query-context
1122               ")")
1123     notmuch-tree-basic-query))
1124
1125 (defun notmuch-tree-toggle-order ()
1126   "Toggle the current search order.
1127
1128 This command toggles the sort order for the current search. The
1129 default sort order is defined by `notmuch-search-oldest-first'."
1130   (interactive)
1131   (setq notmuch-search-oldest-first (not notmuch-search-oldest-first))
1132   (notmuch-tree-refresh-view))
1133
1134 (defun notmuch-tree (&optional query query-context target buffer-name
1135                                open-target unthreaded parent-buffer oldest-first)
1136   "Display threads matching QUERY in tree view.
1137
1138 The arguments are:
1139   QUERY: the main query. This can be any query but in many cases will be
1140       a single thread. If nil this is read interactively from the minibuffer.
1141   QUERY-CONTEXT: is an additional term for the query. The query used
1142       is QUERY and QUERY-CONTEXT unless that does not match any messages
1143       in which case we fall back to just QUERY.
1144   TARGET: A message ID (with the id: prefix) that will be made
1145       current if it appears in the tree view results.
1146   BUFFER-NAME: the name of the buffer to display the tree view. If
1147       it is nil \"*notmuch-tree\" followed by QUERY is used.
1148   OPEN-TARGET: If TRUE open the target message in the message pane.
1149   UNTHREADED: If TRUE only show matching messages in an unthreaded view."
1150   (interactive)
1151   (unless query
1152     (setq query (notmuch-read-query (concat "Notmuch "
1153                                             (if unthreaded "unthreaded " "tree ")
1154                                             "view search: "))))
1155   (let ((buffer (get-buffer-create (generate-new-buffer-name
1156                                     (or buffer-name
1157                                         (concat "*notmuch-"
1158                                                 (if unthreaded "unthreaded-" "tree-")
1159                                                 query "*")))))
1160         (inhibit-read-only t))
1161     (pop-to-buffer-same-window buffer))
1162   ;; Don't track undo information for this buffer
1163   (setq buffer-undo-list t)
1164   (notmuch-tree-worker query query-context target open-target unthreaded oldest-first)
1165   (setq notmuch-tree-parent-buffer parent-buffer)
1166   (setq truncate-lines t))
1167
1168 (defun notmuch-unthreaded (&optional query query-context target buffer-name
1169                                      open-target)
1170   (interactive)
1171   (notmuch-tree query query-context target buffer-name open-target t))
1172
1173 (defun notmuch-tree-filter (query)
1174   "Filter or LIMIT the current search results based on an additional query string.
1175
1176 Runs a new tree search matching only messages that match both the
1177 current search results AND the additional query string provided."
1178   (interactive (list (notmuch-read-query "Filter search: ")))
1179   (let ((notmuch-show-process-crypto (notmuch-tree--message-process-crypto))
1180         (grouped-query (notmuch-group-disjunctive-query-string query))
1181         (grouped-original-query (notmuch-group-disjunctive-query-string
1182                                  (notmuch-tree-get-query))))
1183     (notmuch-tree-close-message-window)
1184     (notmuch-tree (if (string= grouped-original-query "*")
1185                       grouped-query
1186                     (concat grouped-original-query " and " grouped-query)))))
1187
1188 (defun notmuch-tree-filter-by-tag (tag)
1189   "Filter the current search results based on a single TAG.
1190
1191 Run a new search matching only messages that match the current
1192 search results and that are also tagged with the given TAG."
1193   (interactive
1194    (list (notmuch-select-tag-with-completion "Filter by tag: "
1195                                              notmuch-tree-basic-query)))
1196   (let ((notmuch-show-process-crypto (notmuch-tree--message-process-crypto)))
1197     (notmuch-tree-close-message-window)
1198     (notmuch-tree (concat notmuch-tree-basic-query " and tag:" tag)
1199                   notmuch-tree-query-context
1200                   nil
1201                   nil
1202                   nil
1203                   notmuch-tree-unthreaded
1204                   nil
1205                   notmuch-search-oldest-first)))
1206
1207 ;;; _
1208
1209 (provide 'notmuch-tree)
1210
1211 ;;; notmuch-tree.el ends here