1 ;;; notmuch-tree.el --- displaying notmuch forests -*- lexical-binding: t -*-
3 ;; Copyright © Carl Worth
4 ;; Copyright © David Edmondson
5 ;; Copyright © Mark Walters
7 ;; This file is part of Notmuch.
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.
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.
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/>.
22 ;; Authors: David Edmondson <dme@dme.org>
23 ;; Mark Walters <markwalters1009@gmail.com>
29 (require 'notmuch-lib)
30 (require 'notmuch-show)
31 (require 'notmuch-tag)
32 (require 'notmuch-parser)
33 (require 'notmuch-jump)
35 (declare-function notmuch-search "notmuch"
36 (&optional query oldest-first target-thread target-line
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" ())
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" ())
48 ;; this variable distinguishes the unthreaded display from the normal tree display
49 (defvar-local notmuch-tree-unthreaded nil
50 "A buffer local copy of argument unthreaded to the function notmuch-tree.")
54 (defgroup notmuch-tree nil
55 "Showing message and thread structure."
58 (defcustom notmuch-tree-show-out nil
59 "View selected messages in new window rather than split-pane."
63 (defcustom notmuch-unthreaded-show-out t
64 "View selected messages in new window rather than split-pane."
68 (defun notmuch-tree-show-out ()
69 (if notmuch-tree-unthreaded
70 notmuch-unthreaded-show-out
71 notmuch-tree-show-out))
73 (defcustom notmuch-tree-thread-symbols
81 "Strings used to draw trees in notmuch tree results.
82 Symbol keys denote where the corresponding string value is used:
83 `prefix' is used at the top of the tree, followed by `top' if it
84 has no children or `top-tee' if it does; `vertical' is a bar
85 connecting with a response down the list skipping the current
86 one, while `vertical-tee' marks the current message as a reply to
87 the previous one; `bottom' is used at the bottom of threads.
88 Finally, the `arrrow' string in the list is used as a pointer to
91 Common customizations include setting `prefix' to \"-\", to see
92 equal-length prefixes, and `arrow' to an empty string or to a
93 different kind of arrow point."
94 :type '(alist :key-type symbol :value-type string)
97 (defconst notmuch-tree--field-names
99 (const :tag "Date" "date")
100 (const :tag "Authors" "authors")
101 (const :tag "Subject" "subject")
102 (const :tag "Tree" "tree")
103 (const :tag "Tags" "tags")
106 (defcustom notmuch-tree-result-format
108 ("authors" . "%-20s")
113 "Result formatting for tree view.
115 List of pairs of (field . format-string). Supported field
116 strings are: \"date\", \"authors\", \"subject\", \"tree\",
117 \"tags\". It is also supported to pass a function in place of a
118 field-name. In this case the function is passed the thread
119 object (plist) and format string.
121 Tree means the thread tree box graphics. The field may
122 also be a list in which case the formatting rules are
123 applied recursively and then the output of all the fields
124 in the list is inserted according to format-string.
126 Note that the author string should not contain whitespace
127 \(put it in the neighbouring fields instead)."
129 :type `(alist :key-type (choice ,notmuch-tree--field-names
130 (alist :key-type ,notmuch-tree--field-names
131 :value-type (string :tag "Format")))
132 :value-type (string :tag "Format"))
133 :group 'notmuch-tree)
135 (defcustom notmuch-unthreaded-result-format
137 ("authors" . "%-20s")
138 ((("subject" . "%s")) ." %-54s ")
140 "Result formatting for unthreaded tree view.
142 List of pairs of (field . format-string). Supported field
143 strings are: \"date\", \"authors\", \"subject\", \"tree\",
144 \"tags\". It is also supported to pass a function in place of a
145 field-name. In this case the function is passed the thread
146 object (plist) and format string.
148 Tree means the thread tree box graphics. The field may
149 also be a list in which case the formatting rules are
150 applied recursively and then the output of all the fields
151 in the list is inserted according to format-string.
153 Note that the author string should not contain whitespace
154 \(put it in the neighbouring fields instead)."
156 :type `(alist :key-type (choice ,notmuch-tree--field-names
157 (alist :key-type ,notmuch-tree--field-names
158 :value-type (string :tag "Format")))
159 :value-type (string :tag "Format"))
160 :group 'notmuch-tree)
162 (defun notmuch-tree-result-format ()
163 (if notmuch-tree-unthreaded
164 notmuch-unthreaded-result-format
165 notmuch-tree-result-format))
168 ;;;; Faces for messages that match the query
170 (defface notmuch-tree-match-face
171 '((t :inherit default))
172 "Default face used in tree mode face for matching messages"
174 :group 'notmuch-faces)
176 (defface notmuch-tree-match-date-face
178 "Face used in tree mode for the date in messages matching the query."
180 :group 'notmuch-faces)
182 (defface notmuch-tree-match-author-face
185 (:foreground "OliveDrab1"))
188 (:foreground "dark blue"))
191 "Face used in tree mode for the author in messages matching the query."
193 :group 'notmuch-faces)
195 (defface notmuch-tree-match-subject-face
197 "Face used in tree mode for the subject in messages matching the query."
199 :group 'notmuch-faces)
201 (defface notmuch-tree-match-tree-face
203 "Face used in tree mode for the thread tree block graphics in messages matching the query."
205 :group 'notmuch-faces)
207 (defface notmuch-tree-match-tag-face
210 (:foreground "OliveDrab1"))
213 (:foreground "navy blue" :bold t))
216 "Face used in tree mode for tags in messages matching the query."
218 :group 'notmuch-faces)
220 ;;;; Faces for messages that do not match the query
222 (defface notmuch-tree-no-match-face
223 '((t (:foreground "gray")))
224 "Default face used in tree mode face for non-matching messages."
226 :group 'notmuch-faces)
228 (defface notmuch-tree-no-match-date-face
230 "Face used in tree mode for non-matching dates."
232 :group 'notmuch-faces)
234 (defface notmuch-tree-no-match-subject-face
236 "Face used in tree mode for non-matching subjects."
238 :group 'notmuch-faces)
240 (defface notmuch-tree-no-match-tree-face
242 "Face used in tree mode for the thread tree block graphics in messages matching the query."
244 :group 'notmuch-faces)
246 (defface notmuch-tree-no-match-author-face
248 "Face used in tree mode for non-matching authors."
250 :group 'notmuch-faces)
252 (defface notmuch-tree-no-match-tag-face
254 "Face used in tree mode face for non-matching tags."
256 :group 'notmuch-faces)
260 (defvar-local notmuch-tree-previous-subject
261 "The subject of the most recent result shown during the async display.")
263 (defvar-local notmuch-tree-basic-query nil
264 "A buffer local copy of argument query to the function notmuch-tree.")
266 (defvar-local notmuch-tree-query-context nil
267 "A buffer local copy of argument query-context to the function notmuch-tree.")
269 (defvar-local notmuch-tree-target-msg nil
270 "A buffer local copy of argument target to the function notmuch-tree.")
272 (defvar-local notmuch-tree-open-target nil
273 "A buffer local copy of argument open-target to the function notmuch-tree.")
275 (defvar-local notmuch-tree-parent-buffer nil)
277 (defvar-local notmuch-tree-message-window nil
278 "The window of the message pane.
280 It is set in both the tree buffer and the child show buffer. It
281 is used to try and close the message pane when quitting tree view
282 or the child show buffer.")
283 (put 'notmuch-tree-message-window 'permanent-local t)
285 (defvar-local notmuch-tree-message-buffer nil
286 "The buffer name of the show buffer in the message pane.
288 This is used to try and make sure we don't close the message pane
289 if the user has loaded a different buffer in that window.")
290 (put 'notmuch-tree-message-buffer 'permanent-local t)
292 ;;; Tree wrapper commands
294 (defmacro notmuch-tree--define-do-in-message-window (name cmd)
295 "Define NAME as a command that calls CMD interactively in the message window.
296 If the message pane is closed then this command does nothing.
297 Avoid using this macro in new code; it will be removed."
299 ,(concat "(In message window) " (documentation cmd t))
301 (when (window-live-p notmuch-tree-message-window)
302 (with-selected-window notmuch-tree-message-window
303 (call-interactively #',cmd)))))
305 (notmuch-tree--define-do-in-message-window
306 notmuch-tree-previous-message-button
307 notmuch-show-previous-button)
308 (notmuch-tree--define-do-in-message-window
309 notmuch-tree-next-message-button
310 notmuch-show-next-button)
311 (notmuch-tree--define-do-in-message-window
312 notmuch-tree-toggle-message-process-crypto
313 notmuch-show-toggle-process-crypto)
315 (defun notmuch-tree--message-process-crypto ()
316 "Return value of `notmuch-show-process-crypto' in the message window.
317 If that window isn't alive, then return the current value.
318 Avoid using this function in new code; it will be removed."
319 (if (window-live-p notmuch-tree-message-window)
320 (with-selected-window notmuch-tree-message-window
321 notmuch-show-process-crypto)
322 notmuch-show-process-crypto))
324 (defmacro notmuch-tree--define-close-message-window-and (name cmd)
325 "Define NAME as a variant of CMD.
327 NAME determines the value of `notmuch-show-process-crypto' in the
328 message window, closes the window, and then call CMD interactively
329 with that value let-bound. If the message window does not exist,
330 then NAME behaves like CMD."
332 ,(concat "(Close message pane and) " (documentation cmd t))
334 (let ((notmuch-show-process-crypto
335 (notmuch-tree--message-process-crypto)))
336 (notmuch-tree-close-message-window)
337 (call-interactively #',cmd))))
339 (notmuch-tree--define-close-message-window-and
342 (notmuch-tree--define-close-message-window-and
343 notmuch-tree-new-mail
344 notmuch-mua-new-mail)
345 (notmuch-tree--define-close-message-window-and
346 notmuch-tree-jump-search
348 (notmuch-tree--define-close-message-window-and
349 notmuch-tree-forward-message
350 notmuch-show-forward-message)
351 (notmuch-tree--define-close-message-window-and
352 notmuch-tree-reply-sender
353 notmuch-show-reply-sender)
354 (notmuch-tree--define-close-message-window-and
357 (notmuch-tree--define-close-message-window-and
358 notmuch-tree-view-raw-message
359 notmuch-show-view-raw-message)
363 (defvar notmuch-tree-mode-map
364 (let ((map (make-sparse-keymap)))
365 (set-keymap-parent map notmuch-common-keymap)
366 ;; These bindings shadow common bindings with variants
367 ;; that additionally close the message window.
368 (define-key map [remap notmuch-bury-or-kill-this-buffer] 'notmuch-tree-quit)
369 (define-key map [remap notmuch-search] 'notmuch-tree-to-search)
370 (define-key map [remap notmuch-help] 'notmuch-tree-help)
371 (define-key map [remap notmuch-mua-new-mail] 'notmuch-tree-new-mail)
372 (define-key map [remap notmuch-jump-search] 'notmuch-tree-jump-search)
374 (define-key map "o" 'notmuch-tree-toggle-order)
375 (define-key map "S" 'notmuch-search-from-tree-current-query)
376 (define-key map "U" 'notmuch-unthreaded-from-tree-current-query)
377 (define-key map "Z" 'notmuch-tree-from-unthreaded-current-query)
379 ;; these use notmuch-show functions directly
380 (define-key map "|" 'notmuch-show-pipe-message)
381 (define-key map "w" 'notmuch-show-save-attachments)
382 (define-key map "v" 'notmuch-show-view-all-mime-parts)
383 (define-key map "c" 'notmuch-show-stash-map)
384 (define-key map "b" 'notmuch-show-resend-message)
386 ;; these apply to the message pane
387 (define-key map (kbd "M-TAB") 'notmuch-tree-previous-message-button)
388 (define-key map (kbd "<backtab>") 'notmuch-tree-previous-message-button)
389 (define-key map (kbd "TAB") 'notmuch-tree-next-message-button)
390 (define-key map "$" 'notmuch-tree-toggle-message-process-crypto)
392 ;; bindings from show (or elsewhere) but we close the message pane first.
393 (define-key map "f" 'notmuch-tree-forward-message)
394 (define-key map "r" 'notmuch-tree-reply-sender)
395 (define-key map "R" 'notmuch-tree-reply)
396 (define-key map "V" 'notmuch-tree-view-raw-message)
397 (define-key map "l" 'notmuch-tree-filter)
398 (define-key map "t" 'notmuch-tree-filter-by-tag)
399 (define-key map "E" 'notmuch-tree-edit-search)
401 ;; The main tree view bindings
402 (define-key map (kbd "RET") 'notmuch-tree-show-message)
403 (define-key map [mouse-1] 'notmuch-tree-show-message)
404 (define-key map "x" 'notmuch-tree-archive-message-then-next-or-exit)
405 (define-key map "X" 'notmuch-tree-archive-thread-then-exit)
406 (define-key map "A" 'notmuch-tree-archive-thread-then-next)
407 (define-key map "a" 'notmuch-tree-archive-message-then-next)
408 (define-key map "z" 'notmuch-tree-to-tree)
409 (define-key map "n" 'notmuch-tree-next-matching-message)
410 (define-key map "p" 'notmuch-tree-prev-matching-message)
411 (define-key map "N" 'notmuch-tree-next-message)
412 (define-key map "P" 'notmuch-tree-prev-message)
413 (define-key map (kbd "M-p") 'notmuch-tree-prev-thread)
414 (define-key map (kbd "M-n") 'notmuch-tree-next-thread)
415 (define-key map "k" 'notmuch-tag-jump)
416 (define-key map "-" 'notmuch-tree-remove-tag)
417 (define-key map "+" 'notmuch-tree-add-tag)
418 (define-key map "*" 'notmuch-tree-tag-thread)
419 (define-key map " " 'notmuch-tree-scroll-or-next)
420 (define-key map (kbd "DEL") 'notmuch-tree-scroll-message-window-back)
421 (define-key map "e" 'notmuch-tree-resume-message)
423 "Keymap for \"notmuch tree\" buffers.")
425 ;;; Message properties
427 (defun notmuch-tree-get-message-properties ()
428 "Return the properties of the current message as a plist.
430 Some useful entries are:
431 :headers - Property list containing the headers :Date, :Subject, :From, etc.
432 :tags - Tags for this message."
435 (get-text-property (point) :notmuch-message-properties)))
437 (defun notmuch-tree-set-message-properties (props)
440 (put-text-property (point)
442 :notmuch-message-properties props)))
444 (defun notmuch-tree-set-prop (prop val &optional props)
445 (let ((inhibit-read-only t)
447 (notmuch-tree-get-message-properties))))
448 (plist-put props prop val)
449 (notmuch-tree-set-message-properties props)))
451 (defun notmuch-tree-get-prop (prop &optional props)
452 (plist-get (or props (notmuch-tree-get-message-properties))
455 (defun notmuch-tree-set-tags (tags)
456 "Set the tags of the current message."
457 (notmuch-tree-set-prop :tags tags))
459 (defun notmuch-tree-get-tags ()
460 "Return the tags of the current message."
461 (notmuch-tree-get-prop :tags))
463 (defun notmuch-tree-get-message-id (&optional bare)
464 "Return the message id of the current message."
465 (let ((id (notmuch-tree-get-prop :id)))
469 (notmuch-id-to-query id))
472 (defun notmuch-tree-get-match ()
473 "Return whether the current message is a match."
474 (notmuch-tree-get-prop :match))
478 (defun notmuch-tree-refresh-result ()
479 "Redisplay the current message line.
481 This redisplays the current line based on the messages
482 properties (as they are now). This is used when tags are
484 (let ((init-point (point))
485 (end (line-end-position))
486 (msg (notmuch-tree-get-message-properties))
487 (inhibit-read-only t))
489 ;; This is a little tricky: we override
490 ;; notmuch-tree-previous-subject to get the decision between
491 ;; ... and a subject right and it stops notmuch-tree-insert-msg
492 ;; from overwriting the buffer local copy of
493 ;; notmuch-tree-previous-subject if this is called while the
494 ;; buffer is displaying.
495 (let ((notmuch-tree-previous-subject
496 (notmuch-tree-get-prop :previous-subject)))
497 (delete-region (point) (1+ (line-end-position)))
498 (notmuch-tree-insert-msg msg))
499 (let ((new-end (line-end-position)))
500 (goto-char (if (= init-point end)
502 (min init-point (- new-end 1)))))))
504 (defun notmuch-tree-tag-update-display (&optional tag-changes)
505 "Update display for TAG-CHANGES to current message.
507 Updates the message in the message pane if appropriate, but does
508 NOT change the database."
509 (let* ((current-tags (notmuch-tree-get-tags))
510 (new-tags (notmuch-update-tags current-tags tag-changes))
511 (tree-msg-id (notmuch-tree-get-message-id)))
512 (unless (equal current-tags new-tags)
513 (notmuch-tree-set-tags new-tags)
514 (notmuch-tree-refresh-result)
515 (when (window-live-p notmuch-tree-message-window)
516 (with-selected-window notmuch-tree-message-window
517 (when (string= tree-msg-id (notmuch-show-get-message-id))
518 (notmuch-show-update-tags new-tags)))))))
520 ;;; Commands (and some helper functions used by them)
522 (defun notmuch-tree-tag (tag-changes)
523 "Change tags for the current message."
525 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message")))
526 (notmuch-tag (notmuch-tree-get-message-id) tag-changes)
527 (notmuch-tree-tag-update-display tag-changes))
529 (defun notmuch-tree-add-tag (tag-changes)
530 "Same as `notmuch-tree-tag' but sets initial input to '+'."
532 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "+")))
533 (notmuch-tree-tag tag-changes))
535 (defun notmuch-tree-remove-tag (tag-changes)
536 "Same as `notmuch-tree-tag' but sets initial input to '-'."
538 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "-")))
539 (notmuch-tree-tag tag-changes))
541 (defun notmuch-tree-resume-message ()
542 "Resume EDITING the current draft message."
544 (notmuch-tree-close-message-window)
545 (let ((id (notmuch-tree-get-message-id)))
547 (notmuch-draft-resume id)
548 (message "No message to resume!"))))
550 ;; The next two functions close the message window before calling
551 ;; notmuch-search or notmuch-tree but they do so after the user has
552 ;; entered the query (in case the user was basing the query on
553 ;; something in the message window).
555 (defun notmuch-tree-to-search ()
556 "Run \"notmuch search\" with the given `query' and display results."
558 (let ((query (notmuch-read-query "Notmuch search: ")))
559 (notmuch-tree-close-message-window)
560 (notmuch-search query)))
562 (defun notmuch-tree-to-tree ()
563 "Run a query and display results in tree view."
565 (let ((query (notmuch-read-query "Notmuch tree view search: ")))
566 (notmuch-tree-close-message-window)
567 (notmuch-tree query)))
569 (defun notmuch-tree-archive-thread-then-next ()
570 "Archive all messages in the current buffer, then show next thread from search."
572 (notmuch-tree-archive-thread)
573 (notmuch-tree-next-thread))
575 (defun notmuch-unthreaded-from-tree-current-query ()
576 "Switch from tree view to unthreaded view."
578 (unless notmuch-tree-unthreaded
579 (notmuch-tree-refresh-view 'unthreaded)))
581 (defun notmuch-tree-from-unthreaded-current-query ()
582 "Switch from unthreaded view to tree view."
584 (when notmuch-tree-unthreaded
585 (notmuch-tree-refresh-view 'tree)))
587 (defun notmuch-search-from-tree-current-query ()
588 "Call notmuch search with the current query."
590 (notmuch-tree-close-message-window)
591 (notmuch-search (notmuch-tree-get-query)))
593 (defun notmuch-tree-message-window-kill-hook ()
594 "Close the message pane when exiting the show buffer."
595 (let ((buffer (current-buffer)))
596 (when (and (window-live-p notmuch-tree-message-window)
597 (eq (window-buffer notmuch-tree-message-window) buffer))
598 ;; We could check whether this is the only window in its frame,
599 ;; but simply ignoring the error that is thrown otherwise is
600 ;; what we had to do for Emacs 24 and we stick to that because
601 ;; it is still the simplest approach.
603 (delete-window notmuch-tree-message-window)))))
605 (defun notmuch-tree-command-hook ()
606 (when (eq major-mode 'notmuch-tree-mode)
607 ;; We just run the notmuch-show-command-hook on the message pane.
608 (when (buffer-live-p notmuch-tree-message-buffer)
609 (with-current-buffer notmuch-tree-message-buffer
610 (notmuch-show-command-hook)))))
612 (defun notmuch-tree-show-message-in ()
613 "Show the current message (in split-pane)."
615 (let ((id (notmuch-tree-get-message-id))
616 (inhibit-read-only t)
619 ;; We close and reopen the window to kill off un-needed buffers
620 ;; this might cause flickering but seems ok.
621 (notmuch-tree-close-message-window)
622 (setq notmuch-tree-message-window
623 (split-window-vertically (/ (window-height) 4)))
624 (with-selected-window notmuch-tree-message-window
625 (let (;; Since we are only displaying one message do not indent.
626 (notmuch-show-indent-messages-width 0)
627 (notmuch-show-single-message t)
628 ;; Ensure that `pop-to-buffer-same-window' uses the
629 ;; window we want it to use.
630 (display-buffer-overriding-action
631 '((display-buffer-same-window)
632 (inhibit-same-window . nil))))
633 (setq buffer (notmuch-show id))))
634 ;; We need the `let' as notmuch-tree-message-window is buffer local.
635 (let ((window notmuch-tree-message-window))
636 (with-current-buffer buffer
637 (setq notmuch-tree-message-window window)
638 (add-hook 'kill-buffer-hook 'notmuch-tree-message-window-kill-hook)))
639 (when notmuch-show-mark-read-tags
640 (notmuch-tree-tag-update-display notmuch-show-mark-read-tags))
641 (setq notmuch-tree-message-buffer buffer))))
643 (defun notmuch-tree-show-message-out ()
644 "Show the current message (in whole window)."
646 (let ((id (notmuch-tree-get-message-id))
647 (inhibit-read-only t))
649 ;; We close the window to kill off un-needed buffers.
650 (notmuch-tree-close-message-window)
651 ;; n-s-s-m is buffer local, so use inner let.
652 (let ((notmuch-show-single-message t))
653 (notmuch-show id)))))
655 (defun notmuch-tree-show-message (arg)
656 "Show the current message.
658 Shows in split pane or whole window according to value of
659 `notmuch-tree-show-out'. A prefix argument reverses the choice."
661 (if (or (and (notmuch-tree-show-out) (not arg))
662 (and (not (notmuch-tree-show-out)) arg))
663 (notmuch-tree-show-message-out)
664 (notmuch-tree-show-message-in)))
666 (defun notmuch-tree-scroll-message-window ()
667 "Scroll the message window (if it exists)."
669 (when (window-live-p notmuch-tree-message-window)
670 (with-selected-window notmuch-tree-message-window
671 (if (pos-visible-in-window-p (point-max))
675 (defun notmuch-tree-scroll-message-window-back ()
676 "Scroll the message window back (if it exists)."
678 (when (window-live-p notmuch-tree-message-window)
679 (with-selected-window notmuch-tree-message-window
680 (if (pos-visible-in-window-p (point-min))
684 (defun notmuch-tree-scroll-or-next ()
685 "Scroll the message window.
686 If it at end go to next message."
688 (when (notmuch-tree-scroll-message-window)
689 (notmuch-tree-next-matching-message)))
691 (defun notmuch-tree-quit (&optional kill-both)
692 "Close the split view or exit tree."
694 (when (or (not (notmuch-tree-close-message-window)) kill-both)
695 (kill-buffer (current-buffer))))
697 (defun notmuch-tree-close-message-window ()
698 "Close the message-window. Return t if close succeeds."
700 (when (and (window-live-p notmuch-tree-message-window)
701 (eq (window-buffer notmuch-tree-message-window)
702 notmuch-tree-message-buffer))
703 (delete-window notmuch-tree-message-window)
704 (unless (get-buffer-window-list notmuch-tree-message-buffer)
705 (kill-buffer notmuch-tree-message-buffer))
708 (defun notmuch-tree-archive-message (&optional unarchive)
709 "Archive the current message.
711 Archive the current message by applying the tag changes in
712 `notmuch-archive-tags' to it. If a prefix argument is given, the
713 message will be \"unarchived\", i.e. the tag changes in
714 `notmuch-archive-tags' will be reversed."
716 (when notmuch-archive-tags
718 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
720 (defun notmuch-tree-archive-message-then-next (&optional unarchive)
721 "Archive the current message and move to next matching message."
723 (notmuch-tree-archive-message unarchive)
724 (notmuch-tree-next-matching-message))
726 (defun notmuch-tree-archive-thread-then-exit ()
727 "Archive all messages in the current buffer, then exit notmuch-tree."
729 (notmuch-tree-archive-thread)
730 (notmuch-tree-quit t))
732 (defun notmuch-tree-archive-message-then-next-or-exit ()
733 "Archive current message, then show next open message in current thread.
735 If at the last open message in the current thread, then exit back
738 (notmuch-tree-archive-message)
739 (notmuch-tree-next-matching-message t))
741 (defun notmuch-tree-next-message ()
742 "Move to next message."
745 (when (window-live-p notmuch-tree-message-window)
746 (notmuch-tree-show-message-in)))
748 (defun notmuch-tree-prev-message ()
749 "Move to previous message."
752 (when (window-live-p notmuch-tree-message-window)
753 (notmuch-tree-show-message-in)))
755 (defun notmuch-tree-goto-matching-message (&optional prev)
756 "Move to the next or previous matching message.
758 Returns t if there was a next matching message in the thread to show,
760 (let ((dir (if prev -1 nil))
761 (eobfn (if prev #'bobp #'eobp)))
762 (while (and (not (funcall eobfn))
763 (not (notmuch-tree-get-match)))
765 (not (funcall eobfn))))
767 (defun notmuch-tree-matching-message (&optional prev pop-at-end)
768 "Move to the next or previous matching message."
770 (forward-line (if prev -1 nil))
771 (if (and (not (notmuch-tree-goto-matching-message prev)) pop-at-end)
772 (notmuch-tree-quit pop-at-end)
773 (when (window-live-p notmuch-tree-message-window)
774 (notmuch-tree-show-message-in))))
776 (defun notmuch-tree-prev-matching-message (&optional pop-at-end)
777 "Move to previous matching message."
779 (notmuch-tree-matching-message t pop-at-end))
781 (defun notmuch-tree-next-matching-message (&optional pop-at-end)
782 "Move to next matching message."
784 (notmuch-tree-matching-message nil pop-at-end))
786 (defun notmuch-tree-refresh-view (&optional view)
789 (when (get-buffer-process (current-buffer))
790 (error "notmuch tree process already running for current buffer"))
791 (let ((inhibit-read-only t)
792 (basic-query notmuch-tree-basic-query)
793 (unthreaded (cond ((eq view 'unthreaded) t)
794 ((eq view 'tree) nil)
795 (t notmuch-tree-unthreaded)))
796 (query-context notmuch-tree-query-context)
797 (target (notmuch-tree-get-message-id)))
799 (notmuch-tree-worker basic-query
804 notmuch-search-oldest-first)))
806 (defun notmuch-tree-thread-top ()
807 (when (notmuch-tree-get-message-properties)
808 (while (not (or (notmuch-tree-get-prop :first) (eobp)))
811 (defun notmuch-tree-prev-thread-in-tree ()
812 "Move to the previous thread in the current tree"
815 (notmuch-tree-thread-top)
818 (defun notmuch-tree-next-thread-in-tree ()
819 "Get the next thread in the current tree. Returns t if a thread was
820 found or nil if not."
823 (while (not (or (notmuch-tree-get-prop :first) (eobp)))
827 (defun notmuch-tree-next-thread-from-search (&optional previous)
828 "Move to the next thread in the parent search results, if any.
830 If PREVIOUS is non-nil, move to the previous item in the
831 search results instead."
833 (let ((parent-buffer notmuch-tree-parent-buffer))
834 (notmuch-tree-quit t)
835 (when (buffer-live-p parent-buffer)
836 (switch-to-buffer parent-buffer)
838 (notmuch-search-previous-thread)
839 (notmuch-search-next-thread))
840 (notmuch-tree-from-search-thread))))
842 (defun notmuch-tree-next-thread (&optional previous)
843 "Move to the next thread in the current tree or parent search results.
845 If PREVIOUS is non-nil, move to the previous thread in the tree or
846 search results instead."
848 (unless (if previous (notmuch-tree-prev-thread-in-tree)
849 (notmuch-tree-next-thread-in-tree))
850 (notmuch-tree-next-thread-from-search previous)))
852 (defun notmuch-tree-prev-thread ()
853 "Move to the previous thread in the current tree or parent search results."
855 (notmuch-tree-next-thread t))
857 (defun notmuch-tree-thread-mapcar (function)
858 "Call FUNCTION for each message in the current thread.
859 FUNCTION is called for side effects only."
861 (notmuch-tree-thread-top)
862 (cl-loop collect (funcall function)
864 while (and (notmuch-tree-get-message-properties)
865 (not (notmuch-tree-get-prop :first))))))
867 (defun notmuch-tree-get-messages-ids-thread-search ()
868 "Return a search string for all message ids of messages in the current thread."
870 (notmuch-tree-thread-mapcar 'notmuch-tree-get-message-id)
873 (defun notmuch-tree-tag-thread (tag-changes)
874 "Tag all messages in the current thread."
876 (let ((tags (apply #'append (notmuch-tree-thread-mapcar
877 (lambda () (notmuch-tree-get-tags))))))
878 (list (notmuch-read-tag-changes tags "Tag thread"))))
879 (when (notmuch-tree-get-message-properties)
880 (notmuch-tag (notmuch-tree-get-messages-ids-thread-search) tag-changes)
881 (notmuch-tree-thread-mapcar
882 (lambda () (notmuch-tree-tag-update-display tag-changes)))))
884 (defun notmuch-tree-archive-thread (&optional unarchive)
885 "Archive each message in thread.
887 Archive each message currently shown by applying the tag changes
888 in `notmuch-archive-tags' to each. If a prefix argument is given,
889 the messages will be \"unarchived\", i.e. the tag changes in
890 `notmuch-archive-tags' will be reversed.
892 Note: This command is safe from any race condition of new messages
893 being delivered to the same thread. It does not archive the
894 entire thread, but only the messages shown in the current
897 (when notmuch-archive-tags
898 (notmuch-tree-tag-thread
899 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
901 ;;; Functions for displaying the tree buffer itself
903 (defun notmuch-tree-clean-address (address)
904 "Try to clean a single email ADDRESS for display. Return
905 AUTHOR_NAME if present, otherwise return AUTHOR_EMAIL. Return
906 unchanged ADDRESS if parsing fails."
907 (let* ((clean-address (notmuch-clean-address address))
908 (p-address (car clean-address))
909 (p-name (cdr clean-address)))
911 ;; If we have a name return that otherwise return the address.
912 (or p-name p-address)))
914 (defun notmuch-tree-format-field (field format-string msg)
915 "Format a FIELD of MSG according to FORMAT-STRING and return string."
916 (let* ((headers (plist-get msg :headers))
917 (match (plist-get msg :match)))
920 (format format-string (notmuch-tree-format-field-list field msg)))
923 (funcall field format-string msg))
925 ((string-equal field "date")
926 (let ((face (if match
927 'notmuch-tree-match-date-face
928 'notmuch-tree-no-match-date-face)))
929 (propertize (format format-string (plist-get msg :date_relative))
932 ((string-equal field "tree")
933 (let ((tree-status (plist-get msg :tree-status))
935 'notmuch-tree-match-tree-face
936 'notmuch-tree-no-match-tree-face)))
938 (propertize (format format-string
939 (mapconcat #'identity (reverse tree-status) ""))
942 ((string-equal field "subject")
943 (let ((bare-subject (notmuch-show-strip-re (plist-get headers :Subject)))
944 (previous-subject notmuch-tree-previous-subject)
946 'notmuch-tree-match-subject-face
947 'notmuch-tree-no-match-subject-face)))
949 (setq notmuch-tree-previous-subject bare-subject)
950 (propertize (format format-string
951 (if (string= previous-subject bare-subject)
956 ((string-equal field "authors")
957 (let ((author (notmuch-tree-clean-address (plist-get headers :From)))
958 (len (length (format format-string "")))
960 'notmuch-tree-match-author-face
961 'notmuch-tree-no-match-author-face)))
962 (when (> (length author) len)
963 (setq author (substring author 0 len)))
964 (propertize (format format-string author) 'face face)))
966 ((string-equal field "tags")
967 (let ((tags (plist-get msg :tags))
968 (orig-tags (plist-get msg :orig-tags))
970 'notmuch-tree-match-tag-face
971 'notmuch-tree-no-match-tag-face)))
972 (format format-string (notmuch-tag-format-tags tags orig-tags face)))))))
974 (defun notmuch-tree-format-field-list (field-list msg)
975 "Format fields of MSG according to FIELD-LIST and return string."
976 (let ((face (if (plist-get msg :match)
977 'notmuch-tree-match-face
978 'notmuch-tree-no-match-face))
980 (dolist (spec field-list result-string)
981 (let ((field-string (notmuch-tree-format-field (car spec) (cdr spec) msg)))
982 (setq result-string (concat result-string field-string))))
983 (notmuch-apply-face result-string face t)))
985 (defun notmuch-tree-insert-msg (msg)
986 "Insert the message MSG according to notmuch-tree-result-format."
987 ;; We need to save the previous subject as it will get overwritten
988 ;; by the insert-field calls.
989 (let ((previous-subject notmuch-tree-previous-subject))
990 (insert (notmuch-tree-format-field-list (notmuch-tree-result-format) msg))
991 (notmuch-tree-set-message-properties msg)
992 (notmuch-tree-set-prop :previous-subject previous-subject)
995 (defun notmuch-tree-goto-and-insert-msg (msg)
996 "Insert msg at the end of the buffer. Move point to msg if it is the target."
998 (goto-char (point-max))
999 (notmuch-tree-insert-msg msg))
1000 (let ((msg-id (notmuch-id-to-query (plist-get msg :id)))
1001 (target notmuch-tree-target-msg))
1002 (when (or (and (not target) (plist-get msg :match))
1003 (string= msg-id target))
1004 (setq notmuch-tree-target-msg "found")
1005 (goto-char (point-max))
1007 (when notmuch-tree-open-target
1008 (notmuch-tree-show-message-in)
1009 (notmuch-tree-command-hook)))))
1011 (defun notmuch-tree-insert-tree (tree depth tree-status first last)
1012 "Insert the message tree TREE at depth DEPTH in the current thread.
1014 A message tree is another name for a single sub-thread: i.e., a
1015 message together with all its descendents."
1016 (let ((msg (car tree))
1017 (replies (cadr tree))
1018 ;; outline level, computed from the message's depth and
1019 ;; whether or not it's the first message in the tree.
1020 (level (1+ (if (and (eq 0 depth) (not first)) 1 depth))))
1022 ((and (< 0 depth) (not last))
1023 (push (alist-get 'vertical-tee notmuch-tree-thread-symbols) tree-status))
1024 ((and (< 0 depth) last)
1025 (push (alist-get 'bottom notmuch-tree-thread-symbols) tree-status))
1026 ((and (eq 0 depth) first last)
1027 (push (alist-get 'prefix notmuch-tree-thread-symbols) tree-status))
1028 ((and (eq 0 depth) first (not last))
1029 (push (alist-get 'top-tee notmuch-tree-thread-symbols) tree-status))
1030 ((and (eq 0 depth) (not first) last)
1031 (push (alist-get 'bottom notmuch-tree-thread-symbols) tree-status))
1032 ((and (eq 0 depth) (not first) (not last))
1033 (push (alist-get 'vertical-tee notmuch-tree-thread-symbols) tree-status)))
1034 (push (concat (alist-get (if replies 'top-tee 'top) notmuch-tree-thread-symbols)
1035 (alist-get 'arrow notmuch-tree-thread-symbols))
1037 (setq msg (plist-put msg :first (and first (eq 0 depth))))
1038 (setq msg (plist-put msg :tree-status tree-status))
1039 (setq msg (plist-put msg :orig-tags (plist-get msg :tags)))
1040 (setq msg (plist-put msg :level level))
1041 (notmuch-tree-goto-and-insert-msg msg)
1045 (push " " tree-status)
1046 (push (alist-get 'vertical notmuch-tree-thread-symbols) tree-status))
1047 (notmuch-tree-insert-thread replies (1+ depth) tree-status)))
1049 (defun notmuch-tree-insert-thread (thread depth tree-status)
1050 "Insert the collection of sibling sub-threads THREAD at depth DEPTH in the current forest."
1051 (let ((n (length thread)))
1052 (cl-loop for tree in thread
1053 for count from 1 to n
1054 do (notmuch-tree-insert-tree tree depth tree-status
1058 (defun notmuch-tree-insert-forest-thread (forest-thread)
1059 "Insert a single complete thread."
1060 ;; Reset at the start of each main thread.
1061 (setq notmuch-tree-previous-subject nil)
1062 (notmuch-tree-insert-thread forest-thread 0 nil))
1064 (defun notmuch-tree-insert-forest (forest)
1065 "Insert a forest of threads.
1067 This function inserts a collection of several complete threads as
1068 passed to it by notmuch-tree-process-filter."
1069 (mapc 'notmuch-tree-insert-forest-thread forest))
1071 (define-derived-mode notmuch-tree-mode fundamental-mode "notmuch-tree"
1072 "Major mode displaying messages (as opposed to threads) of a notmuch search.
1074 This buffer contains the results of a \"notmuch tree\" of your
1075 email archives. Each line in the buffer represents a single
1076 message giving the relative date, the author, subject, and any
1079 Pressing \\[notmuch-tree-show-message] on any line displays that message.
1081 Complete list of currently available key bindings:
1083 \\{notmuch-tree-mode-map}"
1084 (setq notmuch-buffer-refresh-function #'notmuch-tree-refresh-view)
1086 (setq buffer-read-only t)
1087 (setq truncate-lines t)
1088 (when notmuch-tree-outline-enabled (notmuch-tree-outline-mode 1)))
1090 (defvar notmuch-tree-process-exit-functions nil
1091 "Functions called when the process inserting a tree of results finishes.
1093 Functions in this list are called with one argument, the process
1094 object, and with the tree results buffer as the current buffer.")
1096 (defun notmuch-tree-process-sentinel (proc _msg)
1097 "Add a message to let user know when \"notmuch tree\" exits."
1098 (let ((buffer (process-buffer proc))
1099 (status (process-status proc))
1100 (exit-status (process-exit-status proc)))
1101 (when (memq status '(exit signal))
1102 (kill-buffer (process-get proc 'parse-buf))
1103 (when (buffer-live-p buffer)
1104 (with-current-buffer buffer
1106 (let ((inhibit-read-only t))
1107 (goto-char (point-max))
1108 (when (eq status 'signal)
1109 (insert "Incomplete search results (tree view process was killed).\n"))
1110 (when (eq status 'exit)
1111 (insert "End of search results.")
1112 (unless (= exit-status 0)
1113 (insert (format " (process returned %d)" exit-status)))
1115 (run-hook-with-args 'notmuch-tree-process-exit-functions proc))))))
1117 (defun notmuch-tree-process-filter (proc string)
1118 "Process and filter the output of \"notmuch show\" for tree view."
1119 (let ((results-buf (process-buffer proc))
1120 (parse-buf (process-get proc 'parse-buf))
1121 (inhibit-read-only t))
1122 (if (not (buffer-live-p results-buf))
1123 (delete-process proc)
1124 (with-current-buffer parse-buf
1127 (goto-char (point-max))
1129 (notmuch-sexp-parse-partial-list 'notmuch-tree-insert-forest-thread
1132 (defun notmuch-tree-worker (basic-query &optional query-context target
1133 open-target unthreaded oldest-first)
1134 "Insert the tree view of the search in the current buffer.
1136 This is is a helper function for notmuch-tree. The arguments are
1137 the same as for the function notmuch-tree."
1140 (add-hook 'post-command-hook #'notmuch-tree-command-hook t t)
1141 (setq notmuch-search-oldest-first oldest-first)
1142 (setq notmuch-tree-unthreaded unthreaded)
1143 (setq notmuch-tree-basic-query basic-query)
1144 (setq notmuch-tree-query-context (if (or (string= query-context "")
1145 (string= query-context "*"))
1148 (setq notmuch-tree-target-msg target)
1149 (setq notmuch-tree-open-target open-target)
1150 ;; Set the default value for `notmuch-show-process-crypto' in this
1151 ;; buffer. Although we don't use this some of the functions we call
1152 ;; (such as reply) do. It is a buffer local variable so setting it
1153 ;; will not affect genuine show buffers.
1154 (setq notmuch-show-process-crypto notmuch-crypto-process-mime)
1156 (goto-char (point-min))
1157 (let* ((search-args (concat basic-query
1159 (concat " and (" query-context ")"))))
1160 (sort-arg (if oldest-first "--sort=oldest-first" "--sort=newest-first"))
1161 (message-arg (if unthreaded "--unthreaded" "--entire-thread")))
1162 (when (equal (car (notmuch--process-lines notmuch-command "count" search-args)) "0")
1163 (setq search-args basic-query))
1164 (notmuch-tag-clear-cache)
1165 (let ((proc (notmuch-start-notmuch
1166 "notmuch-tree" (current-buffer) #'notmuch-tree-process-sentinel
1167 "show" "--body=false" "--format=sexp" "--format-version=5"
1168 sort-arg message-arg search-args))
1169 ;; Use a scratch buffer to accumulate partial output.
1170 ;; This buffer will be killed by the sentinel, which
1171 ;; should be called no matter how the process dies.
1172 (parse-buf (generate-new-buffer " *notmuch tree parse*")))
1173 (process-put proc 'parse-buf parse-buf)
1174 (set-process-filter proc 'notmuch-tree-process-filter)
1175 (set-process-query-on-exit-flag proc nil))))
1177 (defun notmuch-tree-get-query ()
1178 "Return the current query in this tree buffer."
1179 (if notmuch-tree-query-context
1180 (concat notmuch-tree-basic-query
1182 notmuch-tree-query-context
1184 notmuch-tree-basic-query))
1186 (defun notmuch-tree-toggle-order ()
1187 "Toggle the current search order.
1189 This command toggles the sort order for the current search. The
1190 default sort order is defined by `notmuch-search-oldest-first'."
1192 (setq notmuch-search-oldest-first (not notmuch-search-oldest-first))
1193 (notmuch-tree-refresh-view))
1195 (defun notmuch-tree (&optional query query-context target buffer-name
1196 open-target unthreaded parent-buffer oldest-first)
1197 "Display threads matching QUERY in tree view.
1200 QUERY: the main query. This can be any query but in many cases will be
1201 a single thread. If nil this is read interactively from the minibuffer.
1202 QUERY-CONTEXT: is an additional term for the query. The query used
1203 is QUERY and QUERY-CONTEXT unless that does not match any messages
1204 in which case we fall back to just QUERY.
1205 TARGET: A message ID (with the id: prefix) that will be made
1206 current if it appears in the tree view results.
1207 BUFFER-NAME: the name of the buffer to display the tree view. If
1208 it is nil \"*notmuch-tree\" followed by QUERY is used.
1209 OPEN-TARGET: If TRUE open the target message in the message pane.
1210 UNTHREADED: If TRUE only show matching messages in an unthreaded view."
1213 (setq query (notmuch-read-query (concat "Notmuch "
1214 (if unthreaded "unthreaded " "tree ")
1218 (notmuch-search-buffer-title query
1219 (if unthreaded "unthreaded" "tree"))))
1220 (buffer (get-buffer-create (generate-new-buffer-name name)))
1221 (inhibit-read-only t))
1222 (pop-to-buffer-same-window buffer))
1223 ;; Don't track undo information for this buffer
1224 (setq buffer-undo-list t)
1225 (notmuch-tree-worker query query-context target open-target unthreaded oldest-first)
1226 (setq notmuch-tree-parent-buffer parent-buffer)
1227 (setq truncate-lines t))
1229 (defun notmuch-unthreaded (&optional query query-context target buffer-name
1231 "Display threads matching QUERY in unthreaded view.
1233 See function NOTMUCH-TREE for documentation of the arguments"
1235 (notmuch-tree query query-context target buffer-name open-target t))
1237 (defun notmuch-tree-filter (query)
1238 "Filter or LIMIT the current search results based on an additional query string.
1240 Runs a new tree search matching only messages that match both the
1241 current search results AND the additional query string provided."
1242 (interactive (list (notmuch-read-query "Filter search: ")))
1243 (let ((notmuch-show-process-crypto (notmuch-tree--message-process-crypto))
1244 (grouped-query (notmuch-group-disjunctive-query-string query))
1245 (grouped-original-query (notmuch-group-disjunctive-query-string
1246 (notmuch-tree-get-query))))
1247 (notmuch-tree-close-message-window)
1248 (notmuch-tree (if (string= grouped-original-query "*")
1250 (concat grouped-original-query " and " grouped-query)))))
1252 (defun notmuch-tree-filter-by-tag (tag)
1253 "Filter the current search results based on a single TAG.
1255 Run a new search matching only messages that match the current
1256 search results and that are also tagged with the given TAG."
1258 (list (notmuch-select-tag-with-completion "Filter by tag: "
1259 notmuch-tree-basic-query)))
1260 (let ((notmuch-show-process-crypto (notmuch-tree--message-process-crypto)))
1261 (notmuch-tree-close-message-window)
1262 (notmuch-tree (concat notmuch-tree-basic-query " and tag:" tag)
1263 notmuch-tree-query-context
1267 notmuch-tree-unthreaded
1269 notmuch-search-oldest-first)))
1271 (defun notmuch-tree-edit-search (query)
1272 "Edit the current search"
1273 (interactive (list (read-from-minibuffer "Edit search: "
1274 notmuch-tree-basic-query)))
1275 (let ((notmuch-show-process-crypto (notmuch-tree--message-process-crypto)))
1276 (notmuch-tree-close-message-window)
1278 notmuch-tree-query-context
1282 notmuch-tree-unthreaded
1284 notmuch-search-oldest-first)))
1286 ;;; Tree outline mode
1287 ;;;; Custom variables
1288 (defcustom notmuch-tree-outline-enabled nil
1289 "Whether to automatically activate `notmuch-tree-outline-mode' in tree views."
1292 (defcustom notmuch-tree-outline-visibility 'hide-others
1293 "Default state of the forest outline for `notmuch-tree-outline-mode'.
1295 This variable controls the state of a forest initially and after
1296 a movement command. If set to nil, all trees are displayed while
1297 the symbol hide-all indicates that all trees in the forest should
1298 be folded and hide-other that only the first one should be
1300 :type '(choice (const :tag "Show all" nil)
1301 (const :tag "Hide others" hide-others)
1302 (const :tag "Hide all" hide-all)))
1304 (defcustom notmuch-tree-outline-auto-close nil
1305 "Close message and tree windows when moving past the last message."
1308 (defcustom notmuch-tree-outline-open-on-next nil
1309 "Open new messages under point if they are closed when moving to next one.
1311 When this flag is set, using the command
1312 `notmuch-tree-outline-next' with point on a header for a new
1313 message that is not shown will open its `notmuch-show' buffer
1314 instead of moving point to next matching message."
1317 ;;;; Helper functions
1318 (defsubst notmuch-tree-outline--pop-at-end (pop-at-end)
1319 (if notmuch-tree-outline-auto-close (not pop-at-end) pop-at-end))
1321 (defun notmuch-tree-outline--set-visibility ()
1322 (when (and notmuch-tree-outline-mode (> (point-max) (point-min)))
1323 (cl-case notmuch-tree-outline-visibility
1324 (hide-others (notmuch-tree-outline-hide-others))
1325 (hide-all (outline-hide-body)))))
1327 (defun notmuch-tree-outline--on-exit (proc)
1328 (when (eq (process-status proc) 'exit)
1329 (notmuch-tree-outline--set-visibility)))
1331 (add-hook 'notmuch-tree-process-exit-functions #'notmuch-tree-outline--on-exit)
1333 (defsubst notmuch-tree-outline--level (&optional props)
1334 (or (plist-get (or props (notmuch-tree-get-message-properties)) :level) 0))
1336 (defsubst notmuch-tree-outline--message-open-p ()
1337 (and (buffer-live-p notmuch-tree-message-buffer)
1338 (get-buffer-window notmuch-tree-message-buffer)
1339 (let ((id (notmuch-tree-get-message-id)))
1341 (with-current-buffer notmuch-tree-message-buffer
1342 (string= (notmuch-show-get-message-id) id))))))
1344 (defsubst notmuch-tree-outline--at-original-match-p ()
1345 (and (notmuch-tree-get-prop :match)
1346 (equal (notmuch-tree-get-prop :orig-tags)
1347 (notmuch-tree-get-prop :tags))))
1349 (defun notmuch-tree-outline--next (prev thread pop-at-end &optional open-new)
1351 (notmuch-tree-thread-top)
1353 (outline-backward-same-level 1)
1354 (outline-forward-same-level 1))
1355 (when (> (notmuch-tree-outline--level) 0) (outline-show-branches))
1356 (notmuch-tree-outline--next nil nil pop-at-end t))
1357 ((and (or open-new notmuch-tree-outline-open-on-next)
1358 (notmuch-tree-outline--at-original-match-p)
1359 (not (notmuch-tree-outline--message-open-p)))
1360 (notmuch-tree-outline-hide-others t))
1361 (t (outline-next-visible-heading (if prev -1 1))
1362 (unless (notmuch-tree-get-prop :match)
1363 (notmuch-tree-matching-message prev pop-at-end))
1364 (notmuch-tree-outline-hide-others t))))
1367 (defun notmuch-tree-outline-hide-others (&optional and-show)
1368 "Fold all threads except the one around point.
1369 If AND-SHOW is t, make the current message visible if it's not."
1372 (while (and (not (bobp)) (> (notmuch-tree-outline--level) 1))
1373 (outline-previous-heading))
1374 (outline-hide-sublevels 1))
1375 (when (> (notmuch-tree-outline--level) 0)
1376 (outline-show-subtree)
1377 (when and-show (notmuch-tree-show-message nil))))
1379 (defun notmuch-tree-outline-next (&optional pop-at-end)
1380 "Next matching message in a forest, taking care of thread visibility.
1381 A prefix argument reverses the meaning of `notmuch-tree-outline-auto-close'."
1383 (let ((pop (notmuch-tree-outline--pop-at-end pop-at-end)))
1384 (if (null notmuch-tree-outline-visibility)
1385 (notmuch-tree-matching-message nil pop)
1386 (notmuch-tree-outline--next nil nil pop))))
1388 (defun notmuch-tree-outline-previous (&optional pop-at-end)
1389 "Previous matching message in forest, taking care of thread visibility.
1390 With prefix, quit the tree view if there is no previous message."
1392 (if (null notmuch-tree-outline-visibility)
1393 (notmuch-tree-prev-matching-message pop-at-end)
1394 (notmuch-tree-outline--next t nil pop-at-end)))
1396 (defun notmuch-tree-outline-next-thread ()
1397 "Next matching thread in forest, taking care of thread visibility."
1399 (if (null notmuch-tree-outline-visibility)
1400 (notmuch-tree-next-thread)
1401 (notmuch-tree-outline--next nil t nil)))
1403 (defun notmuch-tree-outline-previous-thread ()
1404 "Previous matching thread in forest, taking care of thread visibility."
1406 (if (null notmuch-tree-outline-visibility)
1407 (notmuch-tree-prev-thread)
1408 (notmuch-tree-outline--next t t nil)))
1410 ;;;; Mode definition
1411 (defvar notmuch-tree-outline-mode-lighter nil
1412 "The lighter mark for notmuch-tree-outline mode.
1413 Usually empty since outline-minor-mode's lighter will be active.")
1415 (define-minor-mode notmuch-tree-outline-mode
1416 "Minor mode allowing message trees to be folded as outlines.
1418 When this mode is set, each thread and subthread in the results
1419 list is treated as a foldable section, with its first message as
1422 The mode just makes available in the tree buffer all the
1423 keybindings in `outline-minor-mode', and binds the following
1426 \\{notmuch-tree-outline-mode-map}
1428 The customizable variable `notmuch-tree-outline-visibility'
1429 controls how navigation in the buffer is affected by this mode:
1431 - If it is set to nil, `notmuch-tree-outline-previous',
1432 `notmuch-tree-outline-next', and their thread counterparts
1433 behave just as the corresponding notmuch-tree navigation keys
1434 when this mode is not enabled.
1436 - If, on the other hand, `notmuch-tree-outline-visibility' is
1437 set to a non-nil value, these commands hiding the outlines of
1438 the trees you are not reading as you move to new messages.
1440 To enable notmuch-tree-outline-mode by default in all
1441 notmuch-tree buffers, just set
1442 `notmuch-tree-outline-mode-enabled' to t."
1443 :lighter notmuch-tree-outline-mode-lighter
1444 :keymap `((,(kbd "TAB") . outline-cycle)
1445 (,(kbd "M-TAB") . outline-cycle-buffer)
1446 ("n" . notmuch-tree-outline-next)
1447 ("p" . notmuch-tree-outline-previous)
1448 (,(kbd "M-n") . notmuch-tree-outline-next-thread)
1449 (,(kbd "M-p") . notmuch-tree-outline-previous-thread))
1450 (outline-minor-mode notmuch-tree-outline-mode)
1451 (unless (derived-mode-p 'notmuch-tree-mode)
1452 (user-error "notmuch-tree-outline-mode is only meaningful for notmuch trees!"))
1453 (if notmuch-tree-outline-mode
1454 (progn (setq-local outline-regexp "^[^\n]+")
1455 (setq-local outline-level #'notmuch-tree-outline--level)
1456 (notmuch-tree-outline--set-visibility))
1457 (setq-local outline-regexp (default-value 'outline-regexp))
1458 (setq-local outline-level (default-value 'outline-level))))
1462 (provide 'notmuch-tree)
1464 ;;; notmuch-tree.el ends here