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
204 messages matching the query."
206 :group 'notmuch-faces)
208 (defface notmuch-tree-match-tag-face
211 (:foreground "OliveDrab1"))
214 (:foreground "navy blue" :bold t))
217 "Face used in tree mode for tags in messages matching the query."
219 :group 'notmuch-faces)
221 ;;;; Faces for messages that do not match the query
223 (defface notmuch-tree-no-match-face
224 '((t (:foreground "gray")))
225 "Default face used in tree mode face for non-matching messages."
227 :group 'notmuch-faces)
229 (defface notmuch-tree-no-match-date-face
231 "Face used in tree mode for non-matching dates."
233 :group 'notmuch-faces)
235 (defface notmuch-tree-no-match-subject-face
237 "Face used in tree mode for non-matching subjects."
239 :group 'notmuch-faces)
241 (defface notmuch-tree-no-match-tree-face
243 "Face used in tree mode for the thread tree block graphics in
244 messages matching the query."
246 :group 'notmuch-faces)
248 (defface notmuch-tree-no-match-author-face
250 "Face used in tree mode for non-matching authors."
252 :group 'notmuch-faces)
254 (defface notmuch-tree-no-match-tag-face
256 "Face used in tree mode face for non-matching tags."
258 :group 'notmuch-faces)
262 (defvar-local notmuch-tree-previous-subject
263 "The subject of the most recent result shown during the async display.")
265 (defvar-local notmuch-tree-basic-query nil
266 "A buffer local copy of argument query to the function notmuch-tree.")
268 (defvar-local notmuch-tree-query-context nil
269 "A buffer local copy of argument query-context to the function notmuch-tree.")
271 (defvar-local notmuch-tree-target-msg nil
272 "A buffer local copy of argument target to the function notmuch-tree.")
274 (defvar-local notmuch-tree-open-target nil
275 "A buffer local copy of argument open-target to the function notmuch-tree.")
277 (defvar-local notmuch-tree-parent-buffer nil)
279 (defvar-local notmuch-tree-message-window nil
280 "The window of the message pane.
282 It is set in both the tree buffer and the child show buffer. It
283 is used to try and close the message pane when quitting tree view
284 or the child show buffer.")
285 (put 'notmuch-tree-message-window 'permanent-local t)
287 (defvar-local notmuch-tree-message-buffer nil
288 "The buffer name of the show buffer in the message pane.
290 This is used to try and make sure we don't close the message pane
291 if the user has loaded a different buffer in that window.")
292 (put 'notmuch-tree-message-buffer 'permanent-local t)
294 ;;; Tree wrapper commands
296 (defmacro notmuch-tree--define-do-in-message-window (name cmd)
297 "Define NAME as a command that calls CMD interactively in the message window.
298 If the message pane is closed then this command does nothing.
299 Avoid using this macro in new code; it will be removed."
301 ,(concat "(In message window) " (documentation cmd t))
303 (when (window-live-p notmuch-tree-message-window)
304 (with-selected-window notmuch-tree-message-window
305 (call-interactively #',cmd)))))
307 (notmuch-tree--define-do-in-message-window
308 notmuch-tree-previous-message-button
309 notmuch-show-previous-button)
310 (notmuch-tree--define-do-in-message-window
311 notmuch-tree-next-message-button
312 notmuch-show-next-button)
313 (notmuch-tree--define-do-in-message-window
314 notmuch-tree-toggle-message-process-crypto
315 notmuch-show-toggle-process-crypto)
317 (defun notmuch-tree--message-process-crypto ()
318 "Return value of `notmuch-show-process-crypto' in the message window.
319 If that window isn't alive, then return the current value.
320 Avoid using this function in new code; it will be removed."
321 (if (window-live-p notmuch-tree-message-window)
322 (with-selected-window notmuch-tree-message-window
323 notmuch-show-process-crypto)
324 notmuch-show-process-crypto))
326 (defmacro notmuch-tree--define-close-message-window-and (name cmd)
327 "Define NAME as a variant of CMD.
329 NAME determines the value of `notmuch-show-process-crypto' in the
330 message window, closes the window, and then call CMD interactively
331 with that value let-bound. If the message window does not exist,
332 then NAME behaves like CMD."
334 ,(concat "(Close message pane and) " (documentation cmd t))
336 (let ((notmuch-show-process-crypto
337 (notmuch-tree--message-process-crypto)))
338 (notmuch-tree-close-message-window)
339 (call-interactively #',cmd))))
341 (notmuch-tree--define-close-message-window-and
344 (notmuch-tree--define-close-message-window-and
345 notmuch-tree-new-mail
346 notmuch-mua-new-mail)
347 (notmuch-tree--define-close-message-window-and
348 notmuch-tree-jump-search
350 (notmuch-tree--define-close-message-window-and
351 notmuch-tree-forward-message
352 notmuch-show-forward-message)
353 (notmuch-tree--define-close-message-window-and
354 notmuch-tree-reply-sender
355 notmuch-show-reply-sender)
356 (notmuch-tree--define-close-message-window-and
359 (notmuch-tree--define-close-message-window-and
360 notmuch-tree-view-raw-message
361 notmuch-show-view-raw-message)
365 (defvar notmuch-tree-mode-map
366 (let ((map (make-sparse-keymap)))
367 (set-keymap-parent map notmuch-common-keymap)
368 ;; These bindings shadow common bindings with variants
369 ;; that additionally close the message window.
370 (define-key map [remap notmuch-bury-or-kill-this-buffer] 'notmuch-tree-quit)
371 (define-key map [remap notmuch-search] 'notmuch-tree-to-search)
372 (define-key map [remap notmuch-help] 'notmuch-tree-help)
373 (define-key map [remap notmuch-mua-new-mail] 'notmuch-tree-new-mail)
374 (define-key map [remap notmuch-jump-search] 'notmuch-tree-jump-search)
376 (define-key map "o" 'notmuch-tree-toggle-order)
377 (define-key map "S" 'notmuch-search-from-tree-current-query)
378 (define-key map "U" 'notmuch-unthreaded-from-tree-current-query)
379 (define-key map "Z" 'notmuch-tree-from-unthreaded-current-query)
381 ;; these use notmuch-show functions directly
382 (define-key map "|" 'notmuch-show-pipe-message)
383 (define-key map "w" 'notmuch-show-save-attachments)
384 (define-key map "v" 'notmuch-show-view-all-mime-parts)
385 (define-key map "c" 'notmuch-show-stash-map)
386 (define-key map "b" 'notmuch-show-resend-message)
388 ;; these apply to the message pane
389 (define-key map (kbd "M-TAB") 'notmuch-tree-previous-message-button)
390 (define-key map (kbd "<backtab>") 'notmuch-tree-previous-message-button)
391 (define-key map (kbd "TAB") 'notmuch-tree-next-message-button)
392 (define-key map "$" 'notmuch-tree-toggle-message-process-crypto)
394 ;; bindings from show (or elsewhere) but we close the message pane first.
395 (define-key map "f" 'notmuch-tree-forward-message)
396 (define-key map "r" 'notmuch-tree-reply-sender)
397 (define-key map "R" 'notmuch-tree-reply)
398 (define-key map "V" 'notmuch-tree-view-raw-message)
399 (define-key map "l" 'notmuch-tree-filter)
400 (define-key map "t" 'notmuch-tree-filter-by-tag)
401 (define-key map "E" 'notmuch-tree-edit-search)
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)
425 "Keymap for \"notmuch tree\" buffers.")
427 ;;; Message properties
429 (defun notmuch-tree-get-message-properties ()
430 "Return the properties of the current message as a plist.
432 Some useful entries are:
433 :headers - Property list containing the headers :Date, :Subject, :From, etc.
434 :tags - Tags for this message."
437 (get-text-property (point) :notmuch-message-properties)))
439 (defun notmuch-tree-set-message-properties (props)
442 (put-text-property (point)
444 :notmuch-message-properties props)))
446 (defun notmuch-tree-set-prop (prop val &optional props)
447 (let ((inhibit-read-only t)
449 (notmuch-tree-get-message-properties))))
450 (plist-put props prop val)
451 (notmuch-tree-set-message-properties props)))
453 (defun notmuch-tree-get-prop (prop &optional props)
454 (plist-get (or props (notmuch-tree-get-message-properties))
457 (defun notmuch-tree-set-tags (tags)
458 "Set the tags of the current message."
459 (notmuch-tree-set-prop :tags tags))
461 (defun notmuch-tree-get-tags ()
462 "Return the tags of the current message."
463 (notmuch-tree-get-prop :tags))
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)))
471 (notmuch-id-to-query id))
474 (defun notmuch-tree-get-match ()
475 "Return whether the current message is a match."
476 (notmuch-tree-get-prop :match))
480 (defun notmuch-tree-refresh-result ()
481 "Redisplay the current message line.
483 This redisplays the current line based on the messages
484 properties (as they are now). This is used when tags are
486 (let ((init-point (point))
487 (end (line-end-position))
488 (msg (notmuch-tree-get-message-properties))
489 (inhibit-read-only t))
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)
504 (min init-point (- new-end 1)))))))
506 (defun notmuch-tree-tag-update-display (&optional tag-changes)
507 "Update display for TAG-CHANGES to current message.
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)))))))
522 ;;; Commands (and some helper functions used by them)
524 (defun notmuch-tree-tag (tag-changes)
525 "Change tags for the current message."
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))
531 (defun notmuch-tree-add-tag (tag-changes)
532 "Same as `notmuch-tree-tag' but sets initial input to '+'."
534 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "+")))
535 (notmuch-tree-tag tag-changes))
537 (defun notmuch-tree-remove-tag (tag-changes)
538 "Same as `notmuch-tree-tag' but sets initial input to '-'."
540 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "-")))
541 (notmuch-tree-tag tag-changes))
543 (defun notmuch-tree-resume-message ()
544 "Resume EDITING the current draft message."
546 (notmuch-tree-close-message-window)
547 (let ((id (notmuch-tree-get-message-id)))
549 (notmuch-draft-resume id)
550 (message "No message to resume!"))))
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).
557 (defun notmuch-tree-to-search ()
558 "Run \"notmuch search\" with the given `query' and display results."
560 (let ((query (notmuch-read-query "Notmuch search: ")))
561 (notmuch-tree-close-message-window)
562 (notmuch-search query)))
564 (defun notmuch-tree-to-tree ()
565 "Run a query and display results in tree view."
567 (let ((query (notmuch-read-query "Notmuch tree view search: ")))
568 (notmuch-tree-close-message-window)
569 (notmuch-tree query)))
571 (defun notmuch-tree-archive-thread-then-next ()
572 "Archive all messages in the current buffer, then show next thread from search."
574 (notmuch-tree-archive-thread)
575 (notmuch-tree-next-thread))
577 (defun notmuch-unthreaded-from-tree-current-query ()
578 "Switch from tree view to unthreaded view."
580 (unless notmuch-tree-unthreaded
581 (notmuch-tree-refresh-view 'unthreaded)))
583 (defun notmuch-tree-from-unthreaded-current-query ()
584 "Switch from unthreaded view to tree view."
586 (when notmuch-tree-unthreaded
587 (notmuch-tree-refresh-view 'tree)))
589 (defun notmuch-search-from-tree-current-query ()
590 "Call notmuch search with the current query."
592 (notmuch-tree-close-message-window)
593 (notmuch-search (notmuch-tree-get-query)))
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.
605 (delete-window notmuch-tree-message-window)))))
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)))))
614 (defun notmuch-tree-show-message-in ()
615 "Show the current message (in split-pane)."
617 (let ((id (notmuch-tree-get-message-id))
618 (inhibit-read-only t)
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))))
645 (defun notmuch-tree-show-message-out ()
646 "Show the current message (in whole window)."
648 (let ((id (notmuch-tree-get-message-id))
649 (inhibit-read-only t))
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)))))
657 (defun notmuch-tree-show-message (arg)
658 "Show the current message.
660 Shows in split pane or whole window according to value of
661 `notmuch-tree-show-out'. A prefix argument reverses the choice."
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)))
668 (defun notmuch-tree-scroll-message-window ()
669 "Scroll the message window (if it exists)."
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))
677 (defun notmuch-tree-scroll-message-window-back ()
678 "Scroll the message window back (if it exists)."
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))
686 (defun notmuch-tree-scroll-or-next ()
687 "Scroll the message window.
688 If it at end go to next message."
690 (when (notmuch-tree-scroll-message-window)
691 (notmuch-tree-next-matching-message)))
693 (defun notmuch-tree-quit (&optional kill-both)
694 "Close the split view or exit tree."
696 (when (or (not (notmuch-tree-close-message-window)) kill-both)
697 (kill-buffer (current-buffer))))
699 (defun notmuch-tree-close-message-window ()
700 "Close the message-window. Return t if close succeeds."
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))
710 (defun notmuch-tree-archive-message (&optional unarchive)
711 "Archive the current message.
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."
718 (when notmuch-archive-tags
720 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
722 (defun notmuch-tree-archive-message-then-next (&optional unarchive)
723 "Archive the current message and move to next matching message."
725 (notmuch-tree-archive-message unarchive)
726 (notmuch-tree-next-matching-message))
728 (defun notmuch-tree-archive-thread-then-exit ()
729 "Archive all messages in the current buffer, then exit notmuch-tree."
731 (notmuch-tree-archive-thread)
732 (notmuch-tree-quit t))
734 (defun notmuch-tree-archive-message-then-next-or-exit ()
735 "Archive current message, then show next open message in current thread.
737 If at the last open message in the current thread, then exit back
740 (notmuch-tree-archive-message)
741 (notmuch-tree-next-matching-message t))
743 (defun notmuch-tree-next-message ()
744 "Move to next message."
747 (when (window-live-p notmuch-tree-message-window)
748 (notmuch-tree-show-message-in)))
750 (defun notmuch-tree-prev-message ()
751 "Move to previous message."
754 (when (window-live-p notmuch-tree-message-window)
755 (notmuch-tree-show-message-in)))
757 (defun notmuch-tree-goto-matching-message (&optional prev)
758 "Move to the next or previous matching message.
760 Returns t if there was a next matching message in the thread to show,
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)))
767 (not (funcall eobfn))))
769 (defun notmuch-tree-matching-message (&optional prev pop-at-end)
770 "Move to the next or previous matching message."
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))))
778 (defun notmuch-tree-prev-matching-message (&optional pop-at-end)
779 "Move to previous matching message."
781 (notmuch-tree-matching-message t pop-at-end))
783 (defun notmuch-tree-next-matching-message (&optional pop-at-end)
784 "Move to next matching message."
786 (notmuch-tree-matching-message nil pop-at-end))
788 (defun notmuch-tree-refresh-view (&optional view)
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)))
801 (notmuch-tree-worker basic-query
806 notmuch-search-oldest-first)))
808 (defun notmuch-tree-thread-top ()
809 (when (notmuch-tree-get-message-properties)
810 (while (not (or (notmuch-tree-get-prop :first) (eobp)))
813 (defun notmuch-tree-prev-thread-in-tree ()
814 "Move to the previous thread in the current tree"
817 (notmuch-tree-thread-top)
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."
825 (while (not (or (notmuch-tree-get-prop :first) (eobp)))
829 (defun notmuch-tree-next-thread-from-search (&optional previous)
830 "Move to the next thread in the parent search results, if any.
832 If PREVIOUS is non-nil, move to the previous item in the
833 search results instead."
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)
840 (notmuch-search-previous-thread)
841 (notmuch-search-next-thread))
842 (notmuch-tree-from-search-thread))))
844 (defun notmuch-tree-next-thread (&optional previous)
845 "Move to the next thread in the current tree or parent search results.
847 If PREVIOUS is non-nil, move to the previous thread in the tree or
848 search results instead."
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)))
854 (defun notmuch-tree-prev-thread ()
855 "Move to the previous thread in the current tree or parent search results."
857 (notmuch-tree-next-thread t))
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."
863 (notmuch-tree-thread-top)
864 (cl-loop collect (funcall function)
866 while (and (notmuch-tree-get-message-properties)
867 (not (notmuch-tree-get-prop :first))))))
869 (defun notmuch-tree-get-messages-ids-thread-search ()
870 "Return a search string for all message ids of messages in the current thread."
872 (notmuch-tree-thread-mapcar 'notmuch-tree-get-message-id)
875 (defun notmuch-tree-tag-thread (tag-changes)
876 "Tag all messages in the current thread."
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)))))
886 (defun notmuch-tree-archive-thread (&optional unarchive)
887 "Archive each message in thread.
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.
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
899 (when notmuch-archive-tags
900 (notmuch-tree-tag-thread
901 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
903 ;;; Functions for displaying the tree buffer itself
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)))
913 ;; If we have a name return that otherwise return the address.
914 (or p-name p-address)))
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)))
922 (format format-string (notmuch-tree-format-field-list field msg)))
925 (funcall field format-string msg))
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))
934 ((string-equal field "tree")
935 (let ((tree-status (plist-get msg :tree-status))
937 'notmuch-tree-match-tree-face
938 'notmuch-tree-no-match-tree-face)))
940 (propertize (format format-string
941 (mapconcat #'identity (reverse tree-status) ""))
944 ((string-equal field "subject")
945 (let ((bare-subject (notmuch-show-strip-re (plist-get headers :Subject)))
946 (previous-subject notmuch-tree-previous-subject)
948 'notmuch-tree-match-subject-face
949 'notmuch-tree-no-match-subject-face)))
951 (setq notmuch-tree-previous-subject bare-subject)
952 (propertize (format format-string
953 (if (string= previous-subject bare-subject)
958 ((string-equal field "authors")
959 (let ((author (notmuch-tree-clean-address (plist-get headers :From)))
960 (len (length (format format-string "")))
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)))
968 ((string-equal field "tags")
969 (let ((tags (plist-get msg :tags))
970 (orig-tags (plist-get msg :orig-tags))
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)))))))
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))
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)))
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)
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."
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))
1009 (when notmuch-tree-open-target
1010 (notmuch-tree-show-message-in)
1011 (notmuch-tree-command-hook)))))
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.
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 ;; outline level, computed from the message's depth and
1021 ;; whether or not it's the first message in the tree.
1022 (level (1+ (if (and (eq 0 depth) (not first)) 1 depth))))
1024 ((and (< 0 depth) (not last))
1025 (push (alist-get 'vertical-tee notmuch-tree-thread-symbols) tree-status))
1026 ((and (< 0 depth) last)
1027 (push (alist-get 'bottom notmuch-tree-thread-symbols) tree-status))
1028 ((and (eq 0 depth) first last)
1029 (push (alist-get 'prefix notmuch-tree-thread-symbols) tree-status))
1030 ((and (eq 0 depth) first (not last))
1031 (push (alist-get 'top-tee notmuch-tree-thread-symbols) tree-status))
1032 ((and (eq 0 depth) (not first) last)
1033 (push (alist-get 'bottom notmuch-tree-thread-symbols) tree-status))
1034 ((and (eq 0 depth) (not first) (not last))
1035 (push (alist-get 'vertical-tee notmuch-tree-thread-symbols) tree-status)))
1036 (push (concat (alist-get (if replies 'top-tee 'top) notmuch-tree-thread-symbols)
1037 (alist-get 'arrow notmuch-tree-thread-symbols))
1039 (setq msg (plist-put msg :first (and first (eq 0 depth))))
1040 (setq msg (plist-put msg :tree-status tree-status))
1041 (setq msg (plist-put msg :orig-tags (plist-get msg :tags)))
1042 (setq msg (plist-put msg :level level))
1043 (notmuch-tree-goto-and-insert-msg msg)
1047 (push " " tree-status)
1048 (push (alist-get 'vertical notmuch-tree-thread-symbols) tree-status))
1049 (notmuch-tree-insert-thread replies (1+ depth) tree-status)))
1051 (defun notmuch-tree-insert-thread (thread depth tree-status)
1052 "Insert the collection of sibling sub-threads THREAD at depth
1053 DEPTH in the current forest."
1054 (let ((n (length thread)))
1055 (cl-loop for tree in thread
1056 for count from 1 to n
1057 do (notmuch-tree-insert-tree tree depth tree-status
1061 (defun notmuch-tree-insert-forest-thread (forest-thread)
1062 "Insert a single complete thread."
1063 ;; Reset at the start of each main thread.
1064 (setq notmuch-tree-previous-subject nil)
1065 (notmuch-tree-insert-thread forest-thread 0 nil))
1067 (defun notmuch-tree-insert-forest (forest)
1068 "Insert a forest of threads.
1070 This function inserts a collection of several complete threads as
1071 passed to it by notmuch-tree-process-filter."
1072 (mapc 'notmuch-tree-insert-forest-thread forest))
1074 (define-derived-mode notmuch-tree-mode fundamental-mode "notmuch-tree"
1075 "Major mode displaying messages (as opposed to threads) of a notmuch search.
1077 This buffer contains the results of a \"notmuch tree\" of your
1078 email archives. Each line in the buffer represents a single
1079 message giving the relative date, the author, subject, and any
1082 Pressing \\[notmuch-tree-show-message] on any line displays that message.
1084 Complete list of currently available key bindings:
1086 \\{notmuch-tree-mode-map}"
1087 (setq notmuch-buffer-refresh-function #'notmuch-tree-refresh-view)
1089 (setq buffer-read-only t)
1090 (setq truncate-lines t)
1091 (when notmuch-tree-outline-enabled (notmuch-tree-outline-mode 1)))
1093 (defvar notmuch-tree-process-exit-functions nil
1094 "Functions called when the process inserting a tree of results finishes.
1096 Functions in this list are called with one argument, the process
1097 object, and with the tree results buffer as the current buffer.")
1099 (defun notmuch-tree-process-sentinel (proc _msg)
1100 "Add a message to let user know when \"notmuch tree\" exits."
1101 (let ((buffer (process-buffer proc))
1102 (status (process-status proc))
1103 (exit-status (process-exit-status proc)))
1104 (when (memq status '(exit signal))
1105 (kill-buffer (process-get proc 'parse-buf))
1106 (when (buffer-live-p buffer)
1107 (with-current-buffer buffer
1109 (let ((inhibit-read-only t))
1110 (goto-char (point-max))
1111 (when (eq status 'signal)
1112 (insert "Incomplete search results (tree view process was killed).\n"))
1113 (when (eq status 'exit)
1114 (insert "End of search results.")
1115 (unless (= exit-status 0)
1116 (insert (format " (process returned %d)" exit-status)))
1118 (run-hook-with-args 'notmuch-tree-process-exit-functions proc))))))
1120 (defun notmuch-tree-process-filter (proc string)
1121 "Process and filter the output of \"notmuch show\" for tree view."
1122 (let ((results-buf (process-buffer proc))
1123 (parse-buf (process-get proc 'parse-buf))
1124 (inhibit-read-only t))
1125 (if (not (buffer-live-p results-buf))
1126 (delete-process proc)
1127 (with-current-buffer parse-buf
1130 (goto-char (point-max))
1132 (notmuch-sexp-parse-partial-list 'notmuch-tree-insert-forest-thread
1135 (defun notmuch-tree-worker (basic-query &optional query-context target
1136 open-target unthreaded oldest-first)
1137 "Insert the tree view of the search in the current buffer.
1139 This is is a helper function for notmuch-tree. The arguments are
1140 the same as for the function notmuch-tree."
1143 (add-hook 'post-command-hook #'notmuch-tree-command-hook t t)
1144 (setq notmuch-search-oldest-first oldest-first)
1145 (setq notmuch-tree-unthreaded unthreaded)
1146 (setq notmuch-tree-basic-query basic-query)
1147 (setq notmuch-tree-query-context (if (or (string= query-context "")
1148 (string= query-context "*"))
1151 (setq notmuch-tree-target-msg target)
1152 (setq notmuch-tree-open-target open-target)
1153 ;; Set the default value for `notmuch-show-process-crypto' in this
1154 ;; buffer. Although we don't use this some of the functions we call
1155 ;; (such as reply) do. It is a buffer local variable so setting it
1156 ;; will not affect genuine show buffers.
1157 (setq notmuch-show-process-crypto notmuch-crypto-process-mime)
1159 (goto-char (point-min))
1160 (let* ((search-args (concat basic-query
1162 (concat " and (" query-context ")"))))
1163 (sort-arg (if oldest-first "--sort=oldest-first" "--sort=newest-first"))
1164 (message-arg (if unthreaded "--unthreaded" "--entire-thread")))
1165 (when (equal (car (notmuch--process-lines notmuch-command "count" search-args)) "0")
1166 (setq search-args basic-query))
1167 (notmuch-tag-clear-cache)
1168 (let ((proc (notmuch-start-notmuch
1169 "notmuch-tree" (current-buffer) #'notmuch-tree-process-sentinel
1170 "show" "--body=false" "--format=sexp" "--format-version=5"
1171 sort-arg message-arg search-args))
1172 ;; Use a scratch buffer to accumulate partial output.
1173 ;; This buffer will be killed by the sentinel, which
1174 ;; should be called no matter how the process dies.
1175 (parse-buf (generate-new-buffer " *notmuch tree parse*")))
1176 (process-put proc 'parse-buf parse-buf)
1177 (set-process-filter proc 'notmuch-tree-process-filter)
1178 (set-process-query-on-exit-flag proc nil))))
1180 (defun notmuch-tree-get-query ()
1181 "Return the current query in this tree buffer."
1182 (if notmuch-tree-query-context
1183 (concat notmuch-tree-basic-query
1185 notmuch-tree-query-context
1187 notmuch-tree-basic-query))
1189 (defun notmuch-tree-toggle-order ()
1190 "Toggle the current search order.
1192 This command toggles the sort order for the current search. The
1193 default sort order is defined by `notmuch-search-oldest-first'."
1195 (setq notmuch-search-oldest-first (not notmuch-search-oldest-first))
1196 (notmuch-tree-refresh-view))
1198 (defun notmuch-tree (&optional query query-context target buffer-name
1199 open-target unthreaded parent-buffer oldest-first)
1200 "Display threads matching QUERY in tree view.
1203 QUERY: the main query. This can be any query but in many cases will be
1204 a single thread. If nil this is read interactively from the minibuffer.
1205 QUERY-CONTEXT: is an additional term for the query. The query used
1206 is QUERY and QUERY-CONTEXT unless that does not match any messages
1207 in which case we fall back to just QUERY.
1208 TARGET: A message ID (with the id: prefix) that will be made
1209 current if it appears in the tree view results.
1210 BUFFER-NAME: the name of the buffer to display the tree view. If
1211 it is nil \"*notmuch-tree\" followed by QUERY is used.
1212 OPEN-TARGET: If TRUE open the target message in the message pane.
1213 UNTHREADED: If TRUE only show matching messages in an unthreaded view."
1216 (setq query (notmuch-read-query (concat "Notmuch "
1217 (if unthreaded "unthreaded " "tree ")
1221 (notmuch-search-buffer-title query
1222 (if unthreaded "unthreaded" "tree"))))
1223 (buffer (get-buffer-create (generate-new-buffer-name name)))
1224 (inhibit-read-only t))
1225 (pop-to-buffer-same-window buffer))
1226 ;; Don't track undo information for this buffer
1227 (setq buffer-undo-list t)
1228 (notmuch-tree-worker query query-context target open-target unthreaded oldest-first)
1229 (setq notmuch-tree-parent-buffer parent-buffer)
1230 (setq truncate-lines t))
1232 (defun notmuch-unthreaded (&optional query query-context target buffer-name
1234 "Display threads matching QUERY in unthreaded view.
1236 See function NOTMUCH-TREE for documentation of the arguments"
1238 (notmuch-tree query query-context target buffer-name open-target t))
1240 (defun notmuch-tree-filter (query)
1241 "Filter or LIMIT the current search results based on an additional query string.
1243 Runs a new tree search matching only messages that match both the
1244 current search results AND the additional query string provided."
1245 (interactive (list (notmuch-read-query "Filter search: ")))
1246 (let ((notmuch-show-process-crypto (notmuch-tree--message-process-crypto))
1247 (grouped-query (notmuch-group-disjunctive-query-string query))
1248 (grouped-original-query (notmuch-group-disjunctive-query-string
1249 (notmuch-tree-get-query))))
1250 (notmuch-tree-close-message-window)
1251 (notmuch-tree (if (string= grouped-original-query "*")
1253 (concat grouped-original-query " and " grouped-query)))))
1255 (defun notmuch-tree-filter-by-tag (tag)
1256 "Filter the current search results based on a single TAG.
1258 Run a new search matching only messages that match the current
1259 search results and that are also tagged with the given TAG."
1261 (list (notmuch-select-tag-with-completion "Filter by tag: "
1262 notmuch-tree-basic-query)))
1263 (let ((notmuch-show-process-crypto (notmuch-tree--message-process-crypto)))
1264 (notmuch-tree-close-message-window)
1265 (notmuch-tree (concat notmuch-tree-basic-query " and tag:" tag)
1266 notmuch-tree-query-context
1270 notmuch-tree-unthreaded
1272 notmuch-search-oldest-first)))
1274 (defun notmuch-tree-edit-search (query)
1275 "Edit the current search"
1276 (interactive (list (read-from-minibuffer "Edit search: "
1277 notmuch-tree-basic-query)))
1278 (let ((notmuch-show-process-crypto (notmuch-tree--message-process-crypto)))
1279 (notmuch-tree-close-message-window)
1281 notmuch-tree-query-context
1285 notmuch-tree-unthreaded
1287 notmuch-search-oldest-first)))
1289 ;;; Tree outline mode
1290 ;;;; Custom variables
1291 (defcustom notmuch-tree-outline-enabled nil
1292 "Whether to automatically activate `notmuch-tree-outline-mode' in tree views."
1295 (defcustom notmuch-tree-outline-visibility 'hide-others
1296 "Default state of the forest outline for `notmuch-tree-outline-mode'.
1298 This variable controls the state of a forest initially and after
1299 a movement command. If set to nil, all trees are displayed while
1300 the symbol hide-all indicates that all trees in the forest should
1301 be folded and hide-other that only the first one should be
1303 :type '(choice (const :tag "Show all" nil)
1304 (const :tag "Hide others" hide-others)
1305 (const :tag "Hide all" hide-all)))
1307 (defcustom notmuch-tree-outline-auto-close nil
1308 "Close message and tree windows when moving past the last message."
1311 (defcustom notmuch-tree-outline-open-on-next nil
1312 "Open new messages under point if they are closed when moving to next one.
1314 When this flag is set, using the command
1315 `notmuch-tree-outline-next' with point on a header for a new
1316 message that is not shown will open its `notmuch-show' buffer
1317 instead of moving point to next matching message."
1320 ;;;; Helper functions
1321 (defsubst notmuch-tree-outline--pop-at-end (pop-at-end)
1322 (if notmuch-tree-outline-auto-close (not pop-at-end) pop-at-end))
1324 (defun notmuch-tree-outline--set-visibility ()
1325 (when (and notmuch-tree-outline-mode (> (point-max) (point-min)))
1326 (cl-case notmuch-tree-outline-visibility
1327 (hide-others (notmuch-tree-outline-hide-others))
1328 (hide-all (outline-hide-body)))))
1330 (defun notmuch-tree-outline--on-exit (proc)
1331 (when (eq (process-status proc) 'exit)
1332 (notmuch-tree-outline--set-visibility)))
1334 (add-hook 'notmuch-tree-process-exit-functions #'notmuch-tree-outline--on-exit)
1336 (defsubst notmuch-tree-outline--level (&optional props)
1337 (or (plist-get (or props (notmuch-tree-get-message-properties)) :level) 0))
1339 (defsubst notmuch-tree-outline--message-open-p ()
1340 (and (buffer-live-p notmuch-tree-message-buffer)
1341 (get-buffer-window notmuch-tree-message-buffer)
1342 (let ((id (notmuch-tree-get-message-id)))
1344 (with-current-buffer notmuch-tree-message-buffer
1345 (string= (notmuch-show-get-message-id) id))))))
1347 (defsubst notmuch-tree-outline--at-original-match-p ()
1348 (and (notmuch-tree-get-prop :match)
1349 (equal (notmuch-tree-get-prop :orig-tags)
1350 (notmuch-tree-get-prop :tags))))
1352 (defun notmuch-tree-outline--next (prev thread pop-at-end &optional open-new)
1354 (notmuch-tree-thread-top)
1356 (outline-backward-same-level 1)
1357 (outline-forward-same-level 1))
1358 (when (> (notmuch-tree-outline--level) 0) (outline-show-branches))
1359 (notmuch-tree-outline--next nil nil pop-at-end t))
1360 ((and (or open-new notmuch-tree-outline-open-on-next)
1361 (notmuch-tree-outline--at-original-match-p)
1362 (not (notmuch-tree-outline--message-open-p)))
1363 (notmuch-tree-outline-hide-others t))
1364 (t (outline-next-visible-heading (if prev -1 1))
1365 (unless (notmuch-tree-get-prop :match)
1366 (notmuch-tree-matching-message prev pop-at-end))
1367 (notmuch-tree-outline-hide-others t))))
1370 (defun notmuch-tree-outline-hide-others (&optional and-show)
1371 "Fold all threads except the one around point.
1372 If AND-SHOW is t, make the current message visible if it's not."
1375 (while (and (not (bobp)) (> (notmuch-tree-outline--level) 1))
1376 (outline-previous-heading))
1377 (outline-hide-sublevels 1))
1378 (when (> (notmuch-tree-outline--level) 0)
1379 (outline-show-subtree)
1380 (when and-show (notmuch-tree-show-message nil))))
1382 (defun notmuch-tree-outline-next (&optional pop-at-end)
1383 "Next matching message in a forest, taking care of thread visibility.
1384 A prefix argument reverses the meaning of `notmuch-tree-outline-auto-close'."
1386 (let ((pop (notmuch-tree-outline--pop-at-end pop-at-end)))
1387 (if (null notmuch-tree-outline-visibility)
1388 (notmuch-tree-matching-message nil pop)
1389 (notmuch-tree-outline--next nil nil pop))))
1391 (defun notmuch-tree-outline-previous (&optional pop-at-end)
1392 "Previous matching message in forest, taking care of thread visibility.
1393 With prefix, quit the tree view if there is no previous message."
1395 (if (null notmuch-tree-outline-visibility)
1396 (notmuch-tree-prev-matching-message pop-at-end)
1397 (notmuch-tree-outline--next t nil pop-at-end)))
1399 (defun notmuch-tree-outline-next-thread ()
1400 "Next matching thread in forest, taking care of thread visibility."
1402 (if (null notmuch-tree-outline-visibility)
1403 (notmuch-tree-next-thread)
1404 (notmuch-tree-outline--next nil t nil)))
1406 (defun notmuch-tree-outline-previous-thread ()
1407 "Previous matching thread in forest, taking care of thread visibility."
1409 (if (null notmuch-tree-outline-visibility)
1410 (notmuch-tree-prev-thread)
1411 (notmuch-tree-outline--next t t nil)))
1413 ;;;; Mode definition
1414 (defvar notmuch-tree-outline-mode-lighter nil
1415 "The lighter mark for notmuch-tree-outline mode.
1416 Usually empty since outline-minor-mode's lighter will be active.")
1418 (define-minor-mode notmuch-tree-outline-mode
1419 "Minor mode allowing message trees to be folded as outlines.
1421 When this mode is set, each thread and subthread in the results
1422 list is treated as a foldable section, with its first message as
1425 The mode just makes available in the tree buffer all the
1426 keybindings in `outline-minor-mode', and binds the following
1429 \\{notmuch-tree-outline-mode-map}
1431 The customizable variable `notmuch-tree-outline-visibility'
1432 controls how navigation in the buffer is affected by this mode:
1434 - If it is set to nil, `notmuch-tree-outline-previous',
1435 `notmuch-tree-outline-next', and their thread counterparts
1436 behave just as the corresponding notmuch-tree navigation keys
1437 when this mode is not enabled.
1439 - If, on the other hand, `notmuch-tree-outline-visibility' is
1440 set to a non-nil value, these commands hiding the outlines of
1441 the trees you are not reading as you move to new messages.
1443 To enable notmuch-tree-outline-mode by default in all
1444 notmuch-tree buffers, just set
1445 `notmuch-tree-outline-mode-enabled' to t."
1446 :lighter notmuch-tree-outline-mode-lighter
1447 :keymap `((,(kbd "TAB") . outline-cycle)
1448 (,(kbd "M-TAB") . outline-cycle-buffer)
1449 ("n" . notmuch-tree-outline-next)
1450 ("p" . notmuch-tree-outline-previous)
1451 (,(kbd "M-n") . notmuch-tree-outline-next-thread)
1452 (,(kbd "M-p") . notmuch-tree-outline-previous-thread))
1453 (outline-minor-mode notmuch-tree-outline-mode)
1454 (unless (derived-mode-p 'notmuch-tree-mode)
1455 (user-error "notmuch-tree-outline-mode is only meaningful for notmuch trees!"))
1456 (if notmuch-tree-outline-mode
1457 (progn (setq-local outline-regexp "^[^\n]+")
1458 (setq-local outline-level #'notmuch-tree-outline--level)
1459 (notmuch-tree-outline--set-visibility))
1460 (setq-local outline-regexp (default-value 'outline-regexp))
1461 (setq-local outline-level (default-value 'outline-level))))
1465 (provide 'notmuch-tree)
1467 ;;; notmuch-tree.el ends here