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