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