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 ;; the following variable is defined in notmuch.el
49 (defvar notmuch-search-query-string)
51 ;; this variable distinguishes the unthreaded display from the normal tree display
52 (defvar-local notmuch-tree-unthreaded nil
53 "A buffer local copy of argument unthreaded to the function notmuch-tree.")
57 (defgroup notmuch-tree nil
58 "Showing message and thread structure."
61 (defcustom notmuch-tree-show-out nil
62 "View selected messages in new window rather than split-pane."
66 (defcustom notmuch-unthreaded-show-out t
67 "View selected messages in new window rather than split-pane."
71 (defun notmuch-tree-show-out ()
72 (if notmuch-tree-unthreaded
73 notmuch-unthreaded-show-out
74 notmuch-tree-show-out))
76 (defcustom notmuch-tree-thread-symbols
84 "Strings used to draw trees in notmuch tree results.
85 Symbol keys denote where the corresponding string value is used:
86 `prefix' is used at the top of the tree, followed by `top' if it
87 has no children or `top-tee' if it does; `vertical' is a bar
88 connecting with a response down the list skipping the current
89 one, while `vertical-tee' marks the current message as a reply to
90 the previous one; `bottom' is used at the bottom of threads.
91 Finally, the `arrrow' string in the list is used as a pointer to
94 Common customizations include setting `prefix' to \"-\", to see
95 equal-length prefixes, and `arrow' to an empty string or to a
96 different kind of arrow point."
97 :type '(alist :key-type symbol :value-type string)
100 (defconst notmuch-tree--field-names
101 '(choice :tag "Field"
102 (const :tag "Date" "date")
103 (const :tag "Authors" "authors")
104 (const :tag "Subject" "subject")
105 (const :tag "Tree" "tree")
106 (const :tag "Tags" "tags")
109 (defcustom notmuch-tree-result-format
111 ("authors" . "%-20s")
116 "Result formatting for tree view.
118 List of pairs of (field . format-string). Supported field
119 strings are: \"date\", \"authors\", \"subject\", \"tree\",
120 \"tags\". It is also supported to pass a function in place of a
121 field-name. In this case the function is passed the thread
122 object (plist) and format string.
124 Tree means the thread tree box graphics. The field may
125 also be a list in which case the formatting rules are
126 applied recursively and then the output of all the fields
127 in the list is inserted according to format-string.
129 Note that the author string should not contain whitespace
130 \(put it in the neighbouring fields instead)."
132 :type `(alist :key-type (choice ,notmuch-tree--field-names
133 (alist :key-type ,notmuch-tree--field-names
134 :value-type (string :tag "Format")))
135 :value-type (string :tag "Format"))
136 :group 'notmuch-tree)
138 (defcustom notmuch-unthreaded-result-format
140 ("authors" . "%-20s")
141 ((("subject" . "%s")) ." %-54s ")
143 "Result formatting for unthreaded tree view.
145 List of pairs of (field . format-string). Supported field
146 strings are: \"date\", \"authors\", \"subject\", \"tree\",
147 \"tags\". It is also supported to pass a function in place of a
148 field-name. In this case the function is passed the thread
149 object (plist) and format string.
151 Tree means the thread tree box graphics. The field may
152 also be a list in which case the formatting rules are
153 applied recursively and then the output of all the fields
154 in the list is inserted according to format-string.
156 Note that the author string should not contain whitespace
157 \(put it in the neighbouring fields instead)."
159 :type `(alist :key-type (choice ,notmuch-tree--field-names
160 (alist :key-type ,notmuch-tree--field-names
161 :value-type (string :tag "Format")))
162 :value-type (string :tag "Format"))
163 :group 'notmuch-tree)
165 (defun notmuch-tree-result-format ()
166 (if notmuch-tree-unthreaded
167 notmuch-unthreaded-result-format
168 notmuch-tree-result-format))
171 ;;;; Faces for messages that match the query
173 (defface notmuch-tree-match-face
174 '((t :inherit default))
175 "Default face used in tree mode face for matching messages"
177 :group 'notmuch-faces)
179 (defface notmuch-tree-match-date-face
181 "Face used in tree mode for the date in messages matching the query."
183 :group 'notmuch-faces)
185 (defface notmuch-tree-match-author-face
188 (:foreground "OliveDrab1"))
191 (:foreground "dark blue"))
194 "Face used in tree mode for the author in messages matching the query."
196 :group 'notmuch-faces)
198 (defface notmuch-tree-match-subject-face
200 "Face used in tree mode for the subject in messages matching the query."
202 :group 'notmuch-faces)
204 (defface notmuch-tree-match-tree-face
206 "Face used in tree mode for the thread tree block graphics in messages matching the query."
208 :group 'notmuch-faces)
210 (defface notmuch-tree-match-tag-face
213 (:foreground "OliveDrab1"))
216 (:foreground "navy blue" :bold t))
219 "Face used in tree mode for tags in messages matching the query."
221 :group 'notmuch-faces)
223 ;;;; Faces for messages that do not match the query
225 (defface notmuch-tree-no-match-face
226 '((t (:foreground "gray")))
227 "Default face used in tree mode face for non-matching messages."
229 :group 'notmuch-faces)
231 (defface notmuch-tree-no-match-date-face
233 "Face used in tree mode for non-matching dates."
235 :group 'notmuch-faces)
237 (defface notmuch-tree-no-match-subject-face
239 "Face used in tree mode for non-matching subjects."
241 :group 'notmuch-faces)
243 (defface notmuch-tree-no-match-tree-face
245 "Face used in tree mode for the thread tree block graphics in messages matching the query."
247 :group 'notmuch-faces)
249 (defface notmuch-tree-no-match-author-face
251 "Face used in tree mode for non-matching authors."
253 :group 'notmuch-faces)
255 (defface notmuch-tree-no-match-tag-face
257 "Face used in tree mode face for non-matching tags."
259 :group 'notmuch-faces)
263 (defvar-local notmuch-tree-previous-subject
264 "The subject of the most recent result shown during the async display.")
266 (defvar-local notmuch-tree-basic-query nil
267 "A buffer local copy of argument query to the function notmuch-tree.")
269 (defvar-local notmuch-tree-query-context nil
270 "A buffer local copy of argument query-context to the function notmuch-tree.")
272 (defvar-local notmuch-tree-target-msg nil
273 "A buffer local copy of argument target to the function notmuch-tree.")
275 (defvar-local notmuch-tree-open-target nil
276 "A buffer local copy of argument open-target to the function notmuch-tree.")
278 (defvar-local notmuch-tree-parent-buffer nil)
280 (defvar-local notmuch-tree-message-window nil
281 "The window of the message pane.
283 It is set in both the tree buffer and the child show buffer. It
284 is used to try and close the message pane when quitting tree view
285 or the child show buffer.")
286 (put 'notmuch-tree-message-window 'permanent-local t)
288 (defvar-local notmuch-tree-message-buffer nil
289 "The buffer name of the show buffer in the message pane.
291 This is used to try and make sure we don't close the message pane
292 if the user has loaded a different buffer in that window.")
293 (put 'notmuch-tree-message-buffer 'permanent-local t)
295 ;;; Tree wrapper commands
297 (defmacro notmuch-tree--define-do-in-message-window (name cmd)
298 "Define NAME as a command that calls CMD interactively in the message window.
299 If the message pane is closed then this command does nothing.
300 Avoid using this macro in new code; it will be removed."
302 ,(concat "(In message window) " (documentation cmd t))
304 (when (window-live-p notmuch-tree-message-window)
305 (with-selected-window notmuch-tree-message-window
306 (call-interactively #',cmd)))))
308 (notmuch-tree--define-do-in-message-window
309 notmuch-tree-previous-message-button
310 notmuch-show-previous-button)
311 (notmuch-tree--define-do-in-message-window
312 notmuch-tree-next-message-button
313 notmuch-show-next-button)
314 (notmuch-tree--define-do-in-message-window
315 notmuch-tree-toggle-message-process-crypto
316 notmuch-show-toggle-process-crypto)
318 (defun notmuch-tree--message-process-crypto ()
319 "Return value of `notmuch-show-process-crypto' in the message window.
320 If that window isn't alive, then return the current value.
321 Avoid using this function in new code; it will be removed."
322 (if (window-live-p notmuch-tree-message-window)
323 (with-selected-window notmuch-tree-message-window
324 notmuch-show-process-crypto)
325 notmuch-show-process-crypto))
327 (defmacro notmuch-tree--define-close-message-window-and (name cmd)
328 "Define NAME as a variant of CMD.
330 NAME determines the value of `notmuch-show-process-crypto' in the
331 message window, closes the window, and then call CMD interactively
332 with that value let-bound. If the message window does not exist,
333 then NAME behaves like CMD."
335 ,(concat "(Close message pane and) " (documentation cmd t))
337 (let ((notmuch-show-process-crypto
338 (notmuch-tree--message-process-crypto)))
339 (notmuch-tree-close-message-window)
340 (call-interactively #',cmd))))
342 (notmuch-tree--define-close-message-window-and
345 (notmuch-tree--define-close-message-window-and
346 notmuch-tree-new-mail
347 notmuch-mua-new-mail)
348 (notmuch-tree--define-close-message-window-and
349 notmuch-tree-jump-search
351 (notmuch-tree--define-close-message-window-and
352 notmuch-tree-forward-message
353 notmuch-show-forward-message)
354 (notmuch-tree--define-close-message-window-and
355 notmuch-tree-reply-sender
356 notmuch-show-reply-sender)
357 (notmuch-tree--define-close-message-window-and
360 (notmuch-tree--define-close-message-window-and
361 notmuch-tree-view-raw-message
362 notmuch-show-view-raw-message)
366 (defvar notmuch-tree-mode-map
367 (let ((map (make-sparse-keymap)))
368 (set-keymap-parent map notmuch-common-keymap)
369 ;; These bindings shadow common bindings with variants
370 ;; that additionally close the message window.
371 (define-key map [remap notmuch-bury-or-kill-this-buffer] 'notmuch-tree-quit)
372 (define-key map [remap notmuch-search] 'notmuch-tree-to-search)
373 (define-key map [remap notmuch-help] 'notmuch-tree-help)
374 (define-key map [remap notmuch-mua-new-mail] 'notmuch-tree-new-mail)
375 (define-key map [remap notmuch-jump-search] 'notmuch-tree-jump-search)
377 (define-key map "o" 'notmuch-tree-toggle-order)
378 (define-key map "S" 'notmuch-search-from-tree-current-query)
379 (define-key map "U" 'notmuch-unthreaded-from-tree-current-query)
380 (define-key map "Z" 'notmuch-tree-from-unthreaded-current-query)
382 ;; these use notmuch-show functions directly
383 (define-key map "|" 'notmuch-show-pipe-message)
384 (define-key map "w" 'notmuch-show-save-attachments)
385 (define-key map "v" 'notmuch-show-view-all-mime-parts)
386 (define-key map "c" 'notmuch-show-stash-map)
387 (define-key map "b" 'notmuch-show-resend-message)
389 ;; these apply to the message pane
390 (define-key map (kbd "M-TAB") 'notmuch-tree-previous-message-button)
391 (define-key map (kbd "<backtab>") 'notmuch-tree-previous-message-button)
392 (define-key map (kbd "TAB") 'notmuch-tree-next-message-button)
393 (define-key map "$" 'notmuch-tree-toggle-message-process-crypto)
395 ;; bindings from show (or elsewhere) but we close the message pane first.
396 (define-key map "f" 'notmuch-tree-forward-message)
397 (define-key map "r" 'notmuch-tree-reply-sender)
398 (define-key map "R" 'notmuch-tree-reply)
399 (define-key map "V" 'notmuch-tree-view-raw-message)
400 (define-key map "l" 'notmuch-tree-filter)
401 (define-key map "t" 'notmuch-tree-filter-by-tag)
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)))
1021 ((and (< 0 depth) (not last))
1022 (push (alist-get 'vertical-tee notmuch-tree-thread-symbols) tree-status))
1023 ((and (< 0 depth) last)
1024 (push (alist-get 'bottom notmuch-tree-thread-symbols) tree-status))
1025 ((and (eq 0 depth) first last)
1026 (push (alist-get 'prefix notmuch-tree-thread-symbols) tree-status))
1027 ((and (eq 0 depth) first (not last))
1028 (push (alist-get 'top-tee notmuch-tree-thread-symbols) tree-status))
1029 ((and (eq 0 depth) (not first) last)
1030 (push (alist-get 'bottom notmuch-tree-thread-symbols) tree-status))
1031 ((and (eq 0 depth) (not first) (not last))
1032 (push (alist-get 'vertical-tee notmuch-tree-thread-symbols) tree-status)))
1033 (push (concat (alist-get (if replies 'top-tee 'top) notmuch-tree-thread-symbols)
1034 (alist-get 'arrow notmuch-tree-thread-symbols))
1036 (setq msg (plist-put msg :first (and first (eq 0 depth))))
1037 (setq msg (plist-put msg :tree-status tree-status))
1038 (setq msg (plist-put msg :orig-tags (plist-get msg :tags)))
1039 (notmuch-tree-goto-and-insert-msg msg)
1043 (push " " tree-status)
1044 (push (alist-get 'vertical notmuch-tree-thread-symbols) tree-status))
1045 (notmuch-tree-insert-thread replies (1+ depth) tree-status)))
1047 (defun notmuch-tree-insert-thread (thread depth tree-status)
1048 "Insert the collection of sibling sub-threads THREAD at depth DEPTH in the current forest."
1049 (let ((n (length thread)))
1050 (cl-loop for tree in thread
1051 for count from 1 to n
1052 do (notmuch-tree-insert-tree tree depth tree-status
1056 (defun notmuch-tree-insert-forest-thread (forest-thread)
1057 "Insert a single complete thread."
1058 ;; Reset at the start of each main thread.
1059 (setq notmuch-tree-previous-subject nil)
1060 (notmuch-tree-insert-thread forest-thread 0 nil))
1062 (defun notmuch-tree-insert-forest (forest)
1063 "Insert a forest of threads.
1065 This function inserts a collection of several complete threads as
1066 passed to it by notmuch-tree-process-filter."
1067 (mapc 'notmuch-tree-insert-forest-thread forest))
1069 (define-derived-mode notmuch-tree-mode fundamental-mode "notmuch-tree"
1070 "Major mode displaying messages (as opposed to threads) of a notmuch search.
1072 This buffer contains the results of a \"notmuch tree\" of your
1073 email archives. Each line in the buffer represents a single
1074 message giving the relative date, the author, subject, and any
1077 Pressing \\[notmuch-tree-show-message] on any line displays that message.
1079 Complete list of currently available key bindings:
1081 \\{notmuch-tree-mode-map}"
1082 (setq notmuch-buffer-refresh-function #'notmuch-tree-refresh-view)
1084 (setq buffer-read-only t)
1085 (setq truncate-lines t))
1087 (defun notmuch-tree-process-sentinel (proc _msg)
1088 "Add a message to let user know when \"notmuch tree\" exits."
1089 (let ((buffer (process-buffer proc))
1090 (status (process-status proc))
1091 (exit-status (process-exit-status proc)))
1092 (when (memq status '(exit signal))
1093 (kill-buffer (process-get proc 'parse-buf))
1094 (when (buffer-live-p buffer)
1095 (with-current-buffer buffer
1097 (let ((inhibit-read-only t))
1098 (goto-char (point-max))
1099 (when (eq status 'signal)
1100 (insert "Incomplete search results (tree view process was killed).\n"))
1101 (when (eq status 'exit)
1102 (insert "End of search results.")
1103 (unless (= exit-status 0)
1104 (insert (format " (process returned %d)" exit-status)))
1105 (insert "\n")))))))))
1107 (defun notmuch-tree-process-filter (proc string)
1108 "Process and filter the output of \"notmuch show\" for tree view."
1109 (let ((results-buf (process-buffer proc))
1110 (parse-buf (process-get proc 'parse-buf))
1111 (inhibit-read-only t))
1112 (if (not (buffer-live-p results-buf))
1113 (delete-process proc)
1114 (with-current-buffer parse-buf
1117 (goto-char (point-max))
1119 (notmuch-sexp-parse-partial-list 'notmuch-tree-insert-forest-thread
1122 (defun notmuch-tree-worker (basic-query &optional query-context target
1123 open-target unthreaded oldest-first)
1124 "Insert the tree view of the search in the current buffer.
1126 This is is a helper function for notmuch-tree. The arguments are
1127 the same as for the function notmuch-tree."
1130 (add-hook 'post-command-hook #'notmuch-tree-command-hook t t)
1131 (setq notmuch-search-oldest-first oldest-first)
1132 (setq notmuch-tree-unthreaded unthreaded)
1133 (setq notmuch-tree-basic-query basic-query)
1134 (setq notmuch-tree-query-context (if (or (string= query-context "")
1135 (string= query-context "*"))
1138 (setq notmuch-tree-target-msg target)
1139 (setq notmuch-tree-open-target open-target)
1140 ;; Set the default value for `notmuch-show-process-crypto' in this
1141 ;; buffer. Although we don't use this some of the functions we call
1142 ;; (such as reply) do. It is a buffer local variable so setting it
1143 ;; will not affect genuine show buffers.
1144 (setq notmuch-show-process-crypto notmuch-crypto-process-mime)
1146 (goto-char (point-min))
1147 (let* ((search-args (concat basic-query
1149 (concat " and (" query-context ")"))))
1150 (sort-arg (if oldest-first "--sort=oldest-first" "--sort=newest-first"))
1151 (message-arg (if unthreaded "--unthreaded" "--entire-thread")))
1152 (when (equal (car (notmuch--process-lines notmuch-command "count" search-args)) "0")
1153 (setq search-args basic-query))
1154 (notmuch-tag-clear-cache)
1155 (let ((proc (notmuch-start-notmuch
1156 "notmuch-tree" (current-buffer) #'notmuch-tree-process-sentinel
1157 "show" "--body=false" "--format=sexp" "--format-version=5"
1158 sort-arg message-arg search-args))
1159 ;; Use a scratch buffer to accumulate partial output.
1160 ;; This buffer will be killed by the sentinel, which
1161 ;; should be called no matter how the process dies.
1162 (parse-buf (generate-new-buffer " *notmuch tree parse*")))
1163 (process-put proc 'parse-buf parse-buf)
1164 (set-process-filter proc 'notmuch-tree-process-filter)
1165 (set-process-query-on-exit-flag proc nil))))
1167 (defun notmuch-tree-get-query ()
1168 "Return the current query in this tree buffer."
1169 (if notmuch-tree-query-context
1170 (concat notmuch-tree-basic-query
1172 notmuch-tree-query-context
1174 notmuch-tree-basic-query))
1176 (defun notmuch-tree-toggle-order ()
1177 "Toggle the current search order.
1179 This command toggles the sort order for the current search. The
1180 default sort order is defined by `notmuch-search-oldest-first'."
1182 (setq notmuch-search-oldest-first (not notmuch-search-oldest-first))
1183 (notmuch-tree-refresh-view))
1185 (defun notmuch-tree (&optional query query-context target buffer-name
1186 open-target unthreaded parent-buffer oldest-first)
1187 "Display threads matching QUERY in tree view.
1190 QUERY: the main query. This can be any query but in many cases will be
1191 a single thread. If nil this is read interactively from the minibuffer.
1192 QUERY-CONTEXT: is an additional term for the query. The query used
1193 is QUERY and QUERY-CONTEXT unless that does not match any messages
1194 in which case we fall back to just QUERY.
1195 TARGET: A message ID (with the id: prefix) that will be made
1196 current if it appears in the tree view results.
1197 BUFFER-NAME: the name of the buffer to display the tree view. If
1198 it is nil \"*notmuch-tree\" followed by QUERY is used.
1199 OPEN-TARGET: If TRUE open the target message in the message pane.
1200 UNTHREADED: If TRUE only show matching messages in an unthreaded view."
1203 (setq query (notmuch-read-query (concat "Notmuch "
1204 (if unthreaded "unthreaded " "tree ")
1208 (notmuch-search-buffer-title query
1209 (if unthreaded "unthreaded" "tree"))))
1210 (buffer (get-buffer-create (generate-new-buffer-name name)))
1211 (inhibit-read-only t))
1212 (pop-to-buffer-same-window buffer))
1213 ;; Don't track undo information for this buffer
1214 (setq buffer-undo-list t)
1215 (notmuch-tree-worker query query-context target open-target unthreaded oldest-first)
1216 (setq notmuch-tree-parent-buffer parent-buffer)
1217 (setq truncate-lines t))
1219 (defun notmuch-unthreaded (&optional query query-context target buffer-name
1221 "Display threads matching QUERY in unthreaded view.
1223 See function NOTMUCH-TREE for documentation of the arguments"
1225 (notmuch-tree query query-context target buffer-name open-target t))
1227 (defun notmuch-tree-filter (query)
1228 "Filter or LIMIT the current search results based on an additional query string.
1230 Runs a new tree search matching only messages that match both the
1231 current search results AND the additional query string provided."
1232 (interactive (list (notmuch-read-query "Filter search: ")))
1233 (let ((notmuch-show-process-crypto (notmuch-tree--message-process-crypto))
1234 (grouped-query (notmuch-group-disjunctive-query-string query))
1235 (grouped-original-query (notmuch-group-disjunctive-query-string
1236 (notmuch-tree-get-query))))
1237 (notmuch-tree-close-message-window)
1238 (notmuch-tree (if (string= grouped-original-query "*")
1240 (concat grouped-original-query " and " grouped-query)))))
1242 (defun notmuch-tree-filter-by-tag (tag)
1243 "Filter the current search results based on a single TAG.
1245 Run a new search matching only messages that match the current
1246 search results and that are also tagged with the given TAG."
1248 (list (notmuch-select-tag-with-completion "Filter by tag: "
1249 notmuch-tree-basic-query)))
1250 (let ((notmuch-show-process-crypto (notmuch-tree--message-process-crypto)))
1251 (notmuch-tree-close-message-window)
1252 (notmuch-tree (concat notmuch-tree-basic-query " and tag:" tag)
1253 notmuch-tree-query-context
1257 notmuch-tree-unthreaded
1259 notmuch-search-oldest-first)))
1263 (provide 'notmuch-tree)
1265 ;;; notmuch-tree.el ends here