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 (defcustom notmuch-tree-result-format
102 ("authors" . "%-20s")
107 "Result formatting for tree view.
109 Supported fields are: date, authors, subject, tree, tags.
111 Tree means the thread tree box graphics. The field may
112 also be a list in which case the formatting rules are
113 applied recursively and then the output of all the fields
114 in the list is inserted according to format-string.
116 Note that the author string should not contain whitespace
117 \(put it in the neighbouring fields instead). For example:
118 (setq notmuch-tree-result-format
119 '((\"authors\" . \"%-40s\")
120 (\"subject\" . \"%s\")))"
121 :type '(alist :key-type (choice string
122 (alist :key-type string
125 :group 'notmuch-tree)
127 (defcustom notmuch-unthreaded-result-format
129 ("authors" . "%-20s")
130 ((("subject" . "%s")) ." %-54s ")
132 "Result formatting for unthreaded tree view.
134 Supported fields are: date, authors, subject, tree, tags.
136 Tree means the thread tree box graphics. The field may
137 also be a list in which case the formatting rules are
138 applied recursively and then the output of all the fields
139 in the list is inserted according to format-string.
141 Note that the author string should not contain whitespace
142 \(put it in the neighbouring fields instead). For example:
143 (setq notmuch-unthreaded-result-format
144 '((\"authors\" . \"%-40s\")
145 (\"subject\" . \"%s\")))"
146 :type '(alist :key-type (choice string
147 (alist :key-type string
150 :group 'notmuch-tree)
152 (defun notmuch-tree-result-format ()
153 (if notmuch-tree-unthreaded
154 notmuch-unthreaded-result-format
155 notmuch-tree-result-format))
158 ;;;; Faces for messages that match the query
160 (defface notmuch-tree-match-face
161 '((t :inherit default))
162 "Default face used in tree mode face for matching messages"
164 :group 'notmuch-faces)
166 (defface notmuch-tree-match-date-face
168 "Face used in tree mode for the date in messages matching the query."
170 :group 'notmuch-faces)
172 (defface notmuch-tree-match-author-face
175 (:foreground "OliveDrab1"))
178 (:foreground "dark blue"))
181 "Face used in tree mode for the author in messages matching the query."
183 :group 'notmuch-faces)
185 (defface notmuch-tree-match-subject-face
187 "Face used in tree mode for the subject in messages matching the query."
189 :group 'notmuch-faces)
191 (defface notmuch-tree-match-tree-face
193 "Face used in tree mode for the thread tree block graphics in messages matching the query."
195 :group 'notmuch-faces)
197 (defface notmuch-tree-match-tag-face
200 (:foreground "OliveDrab1"))
203 (:foreground "navy blue" :bold t))
206 "Face used in tree mode for tags in messages matching the query."
208 :group 'notmuch-faces)
210 ;;;; Faces for messages that do not match the query
212 (defface notmuch-tree-no-match-face
213 '((t (:foreground "gray")))
214 "Default face used in tree mode face for non-matching messages."
216 :group 'notmuch-faces)
218 (defface notmuch-tree-no-match-date-face
220 "Face used in tree mode for non-matching dates."
222 :group 'notmuch-faces)
224 (defface notmuch-tree-no-match-subject-face
226 "Face used in tree mode for non-matching subjects."
228 :group 'notmuch-faces)
230 (defface notmuch-tree-no-match-tree-face
232 "Face used in tree mode for the thread tree block graphics in messages matching the query."
234 :group 'notmuch-faces)
236 (defface notmuch-tree-no-match-author-face
238 "Face used in tree mode for non-matching authors."
240 :group 'notmuch-faces)
242 (defface notmuch-tree-no-match-tag-face
244 "Face used in tree mode face for non-matching tags."
246 :group 'notmuch-faces)
250 (defvar-local notmuch-tree-previous-subject
251 "The subject of the most recent result shown during the async display.")
253 (defvar-local notmuch-tree-basic-query nil
254 "A buffer local copy of argument query to the function notmuch-tree.")
256 (defvar-local notmuch-tree-query-context nil
257 "A buffer local copy of argument query-context to the function notmuch-tree.")
259 (defvar-local notmuch-tree-target-msg nil
260 "A buffer local copy of argument target to the function notmuch-tree.")
262 (defvar-local notmuch-tree-open-target nil
263 "A buffer local copy of argument open-target to the function notmuch-tree.")
265 (defvar-local notmuch-tree-parent-buffer nil)
267 (defvar-local notmuch-tree-message-window nil
268 "The window of the message pane.
270 It is set in both the tree buffer and the child show buffer. It
271 is used to try and close the message pane when quitting tree view
272 or the child show buffer.")
273 (put 'notmuch-tree-message-window 'permanent-local t)
275 (defvar-local notmuch-tree-message-buffer nil
276 "The buffer name of the show buffer in the message pane.
278 This is used to try and make sure we don't close the message pane
279 if the user has loaded a different buffer in that window.")
280 (put 'notmuch-tree-message-buffer 'permanent-local t)
282 ;;; Tree wrapper commands
284 (defmacro notmuch-tree--define-do-in-message-window (name cmd)
285 "Define NAME as a command that calls CMD interactively in the message window.
286 If the message pane is closed then this command does nothing.
287 Avoid using this macro in new code; it will be removed."
289 ,(concat "(In message window) " (documentation cmd t))
291 (when (window-live-p notmuch-tree-message-window)
292 (with-selected-window notmuch-tree-message-window
293 (call-interactively #',cmd)))))
295 (notmuch-tree--define-do-in-message-window
296 notmuch-tree-previous-message-button
297 notmuch-show-previous-button)
298 (notmuch-tree--define-do-in-message-window
299 notmuch-tree-next-message-button
300 notmuch-show-next-button)
301 (notmuch-tree--define-do-in-message-window
302 notmuch-tree-toggle-message-process-crypto
303 notmuch-show-toggle-process-crypto)
305 (defun notmuch-tree--message-process-crypto ()
306 "Return value of `notmuch-show-process-crypto' in the message window.
307 If that window isn't alive, then return the current value.
308 Avoid using this function in new code; it will be removed."
309 (if (window-live-p notmuch-tree-message-window)
310 (with-selected-window notmuch-tree-message-window
311 notmuch-show-process-crypto)
312 notmuch-show-process-crypto))
314 (defmacro notmuch-tree--define-close-message-window-and (name cmd)
315 "Define NAME as a variant of CMD.
317 NAME determines the value of `notmuch-show-process-crypto' in the
318 message window, closes the window, and then call CMD interactively
319 with that value let-bound. If the message window does not exist,
320 then NAME behaves like CMD."
322 ,(concat "(Close message pane and) " (documentation cmd t))
324 (let ((notmuch-show-process-crypto
325 (notmuch-tree--message-process-crypto)))
326 (notmuch-tree-close-message-window)
327 (call-interactively #',cmd))))
329 (notmuch-tree--define-close-message-window-and
332 (notmuch-tree--define-close-message-window-and
333 notmuch-tree-new-mail
334 notmuch-mua-new-mail)
335 (notmuch-tree--define-close-message-window-and
336 notmuch-tree-jump-search
338 (notmuch-tree--define-close-message-window-and
339 notmuch-tree-forward-message
340 notmuch-show-forward-message)
341 (notmuch-tree--define-close-message-window-and
342 notmuch-tree-reply-sender
343 notmuch-show-reply-sender)
344 (notmuch-tree--define-close-message-window-and
347 (notmuch-tree--define-close-message-window-and
348 notmuch-tree-view-raw-message
349 notmuch-show-view-raw-message)
353 (defvar notmuch-tree-mode-map
354 (let ((map (make-sparse-keymap)))
355 (set-keymap-parent map notmuch-common-keymap)
356 ;; These bindings shadow common bindings with variants
357 ;; that additionally close the message window.
358 (define-key map [remap notmuch-bury-or-kill-this-buffer] 'notmuch-tree-quit)
359 (define-key map [remap notmuch-search] 'notmuch-tree-to-search)
360 (define-key map [remap notmuch-help] 'notmuch-tree-help)
361 (define-key map [remap notmuch-mua-new-mail] 'notmuch-tree-new-mail)
362 (define-key map [remap notmuch-jump-search] 'notmuch-tree-jump-search)
364 (define-key map "o" 'notmuch-tree-toggle-order)
365 (define-key map "S" 'notmuch-search-from-tree-current-query)
366 (define-key map "U" 'notmuch-unthreaded-from-tree-current-query)
367 (define-key map "Z" 'notmuch-tree-from-unthreaded-current-query)
369 ;; these use notmuch-show functions directly
370 (define-key map "|" 'notmuch-show-pipe-message)
371 (define-key map "w" 'notmuch-show-save-attachments)
372 (define-key map "v" 'notmuch-show-view-all-mime-parts)
373 (define-key map "c" 'notmuch-show-stash-map)
374 (define-key map "b" 'notmuch-show-resend-message)
376 ;; these apply to the message pane
377 (define-key map (kbd "M-TAB") 'notmuch-tree-previous-message-button)
378 (define-key map (kbd "<backtab>") 'notmuch-tree-previous-message-button)
379 (define-key map (kbd "TAB") 'notmuch-tree-next-message-button)
380 (define-key map "$" 'notmuch-tree-toggle-message-process-crypto)
382 ;; bindings from show (or elsewhere) but we close the message pane first.
383 (define-key map "f" 'notmuch-tree-forward-message)
384 (define-key map "r" 'notmuch-tree-reply-sender)
385 (define-key map "R" 'notmuch-tree-reply)
386 (define-key map "V" 'notmuch-tree-view-raw-message)
387 (define-key map "l" 'notmuch-tree-filter)
388 (define-key map "t" 'notmuch-tree-filter-by-tag)
390 ;; The main tree view bindings
391 (define-key map (kbd "RET") 'notmuch-tree-show-message)
392 (define-key map [mouse-1] 'notmuch-tree-show-message)
393 (define-key map "x" 'notmuch-tree-archive-message-then-next-or-exit)
394 (define-key map "X" 'notmuch-tree-archive-thread-then-exit)
395 (define-key map "A" 'notmuch-tree-archive-thread-then-next)
396 (define-key map "a" 'notmuch-tree-archive-message-then-next)
397 (define-key map "z" 'notmuch-tree-to-tree)
398 (define-key map "n" 'notmuch-tree-next-matching-message)
399 (define-key map "p" 'notmuch-tree-prev-matching-message)
400 (define-key map "N" 'notmuch-tree-next-message)
401 (define-key map "P" 'notmuch-tree-prev-message)
402 (define-key map (kbd "M-p") 'notmuch-tree-prev-thread)
403 (define-key map (kbd "M-n") 'notmuch-tree-next-thread)
404 (define-key map "k" 'notmuch-tag-jump)
405 (define-key map "-" 'notmuch-tree-remove-tag)
406 (define-key map "+" 'notmuch-tree-add-tag)
407 (define-key map "*" 'notmuch-tree-tag-thread)
408 (define-key map " " 'notmuch-tree-scroll-or-next)
409 (define-key map (kbd "DEL") 'notmuch-tree-scroll-message-window-back)
410 (define-key map "e" 'notmuch-tree-resume-message)
412 "Keymap for \"notmuch tree\" buffers.")
414 ;;; Message properties
416 (defun notmuch-tree-get-message-properties ()
417 "Return the properties of the current message as a plist.
419 Some useful entries are:
420 :headers - Property list containing the headers :Date, :Subject, :From, etc.
421 :tags - Tags for this message."
424 (get-text-property (point) :notmuch-message-properties)))
426 (defun notmuch-tree-set-message-properties (props)
429 (put-text-property (point)
431 :notmuch-message-properties props)))
433 (defun notmuch-tree-set-prop (prop val &optional props)
434 (let ((inhibit-read-only t)
436 (notmuch-tree-get-message-properties))))
437 (plist-put props prop val)
438 (notmuch-tree-set-message-properties props)))
440 (defun notmuch-tree-get-prop (prop &optional props)
441 (plist-get (or props (notmuch-tree-get-message-properties))
444 (defun notmuch-tree-set-tags (tags)
445 "Set the tags of the current message."
446 (notmuch-tree-set-prop :tags tags))
448 (defun notmuch-tree-get-tags ()
449 "Return the tags of the current message."
450 (notmuch-tree-get-prop :tags))
452 (defun notmuch-tree-get-message-id (&optional bare)
453 "Return the message id of the current message."
454 (let ((id (notmuch-tree-get-prop :id)))
458 (notmuch-id-to-query id))
461 (defun notmuch-tree-get-match ()
462 "Return whether the current message is a match."
463 (notmuch-tree-get-prop :match))
467 (defun notmuch-tree-refresh-result ()
468 "Redisplay the current message line.
470 This redisplays the current line based on the messages
471 properties (as they are now). This is used when tags are
473 (let ((init-point (point))
474 (end (line-end-position))
475 (msg (notmuch-tree-get-message-properties))
476 (inhibit-read-only t))
478 ;; This is a little tricky: we override
479 ;; notmuch-tree-previous-subject to get the decision between
480 ;; ... and a subject right and it stops notmuch-tree-insert-msg
481 ;; from overwriting the buffer local copy of
482 ;; notmuch-tree-previous-subject if this is called while the
483 ;; buffer is displaying.
484 (let ((notmuch-tree-previous-subject
485 (notmuch-tree-get-prop :previous-subject)))
486 (delete-region (point) (1+ (line-end-position)))
487 (notmuch-tree-insert-msg msg))
488 (let ((new-end (line-end-position)))
489 (goto-char (if (= init-point end)
491 (min init-point (- new-end 1)))))))
493 (defun notmuch-tree-tag-update-display (&optional tag-changes)
494 "Update display for TAG-CHANGES to current message.
496 Updates the message in the message pane if appropriate, but does
497 NOT change the database."
498 (let* ((current-tags (notmuch-tree-get-tags))
499 (new-tags (notmuch-update-tags current-tags tag-changes))
500 (tree-msg-id (notmuch-tree-get-message-id)))
501 (unless (equal current-tags new-tags)
502 (notmuch-tree-set-tags new-tags)
503 (notmuch-tree-refresh-result)
504 (when (window-live-p notmuch-tree-message-window)
505 (with-selected-window notmuch-tree-message-window
506 (when (string= tree-msg-id (notmuch-show-get-message-id))
507 (notmuch-show-update-tags new-tags)))))))
509 ;;; Commands (and some helper functions used by them)
511 (defun notmuch-tree-tag (tag-changes)
512 "Change tags for the current message."
514 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message")))
515 (notmuch-tag (notmuch-tree-get-message-id) tag-changes)
516 (notmuch-tree-tag-update-display tag-changes))
518 (defun notmuch-tree-add-tag (tag-changes)
519 "Same as `notmuch-tree-tag' but sets initial input to '+'."
521 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "+")))
522 (notmuch-tree-tag tag-changes))
524 (defun notmuch-tree-remove-tag (tag-changes)
525 "Same as `notmuch-tree-tag' but sets initial input to '-'."
527 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "-")))
528 (notmuch-tree-tag tag-changes))
530 (defun notmuch-tree-resume-message ()
531 "Resume EDITING the current draft message."
533 (notmuch-tree-close-message-window)
534 (let ((id (notmuch-tree-get-message-id)))
536 (notmuch-draft-resume id)
537 (message "No message to resume!"))))
539 ;; The next two functions close the message window before calling
540 ;; notmuch-search or notmuch-tree but they do so after the user has
541 ;; entered the query (in case the user was basing the query on
542 ;; something in the message window).
544 (defun notmuch-tree-to-search ()
545 "Run \"notmuch search\" with the given `query' and display results."
547 (let ((query (notmuch-read-query "Notmuch search: ")))
548 (notmuch-tree-close-message-window)
549 (notmuch-search query)))
551 (defun notmuch-tree-to-tree ()
552 "Run a query and display results in tree view."
554 (let ((query (notmuch-read-query "Notmuch tree view search: ")))
555 (notmuch-tree-close-message-window)
556 (notmuch-tree query)))
558 (defun notmuch-tree-archive-thread-then-next ()
559 "Archive all messages in the current buffer, then show next thread from search."
561 (notmuch-tree-archive-thread)
562 (notmuch-tree-next-thread))
564 (defun notmuch-unthreaded-from-tree-current-query ()
565 "Switch from tree view to unthreaded view."
567 (unless notmuch-tree-unthreaded
568 (notmuch-tree-refresh-view 'unthreaded)))
570 (defun notmuch-tree-from-unthreaded-current-query ()
571 "Switch from unthreaded view to tree view."
573 (when notmuch-tree-unthreaded
574 (notmuch-tree-refresh-view 'tree)))
576 (defun notmuch-search-from-tree-current-query ()
577 "Call notmuch search with the current query."
579 (notmuch-tree-close-message-window)
580 (notmuch-search (notmuch-tree-get-query)))
582 (defun notmuch-tree-message-window-kill-hook ()
583 "Close the message pane when exiting the show buffer."
584 (let ((buffer (current-buffer)))
585 (when (and (window-live-p notmuch-tree-message-window)
586 (eq (window-buffer notmuch-tree-message-window) buffer))
587 ;; We could check whether this is the only window in its frame,
588 ;; but simply ignoring the error that is thrown otherwise is
589 ;; what we had to do for Emacs 24 and we stick to that because
590 ;; it is still the simplest approach.
592 (delete-window notmuch-tree-message-window)))))
594 (defun notmuch-tree-command-hook ()
595 (when (eq major-mode 'notmuch-tree-mode)
596 ;; We just run the notmuch-show-command-hook on the message pane.
597 (when (buffer-live-p notmuch-tree-message-buffer)
598 (with-current-buffer notmuch-tree-message-buffer
599 (notmuch-show-command-hook)))))
601 (defun notmuch-tree-show-message-in ()
602 "Show the current message (in split-pane)."
604 (let ((id (notmuch-tree-get-message-id))
605 (inhibit-read-only t)
608 ;; We close and reopen the window to kill off un-needed buffers
609 ;; this might cause flickering but seems ok.
610 (notmuch-tree-close-message-window)
611 (setq notmuch-tree-message-window
612 (split-window-vertically (/ (window-height) 4)))
613 (with-selected-window notmuch-tree-message-window
614 (let (;; Since we are only displaying one message do not indent.
615 (notmuch-show-indent-messages-width 0)
616 (notmuch-show-single-message t)
617 ;; Ensure that `pop-to-buffer-same-window' uses the
618 ;; window we want it to use.
619 (display-buffer-overriding-action
620 '((display-buffer-same-window)
621 (inhibit-same-window . nil))))
622 (setq buffer (notmuch-show id))))
623 ;; We need the `let' as notmuch-tree-message-window is buffer local.
624 (let ((window notmuch-tree-message-window))
625 (with-current-buffer buffer
626 (setq notmuch-tree-message-window window)
627 (add-hook 'kill-buffer-hook 'notmuch-tree-message-window-kill-hook)))
628 (when notmuch-show-mark-read-tags
629 (notmuch-tree-tag-update-display notmuch-show-mark-read-tags))
630 (setq notmuch-tree-message-buffer buffer))))
632 (defun notmuch-tree-show-message-out ()
633 "Show the current message (in whole window)."
635 (let ((id (notmuch-tree-get-message-id))
636 (inhibit-read-only t))
638 ;; We close the window to kill off un-needed buffers.
639 (notmuch-tree-close-message-window)
640 ;; n-s-s-m is buffer local, so use inner let.
641 (let ((notmuch-show-single-message t))
642 (notmuch-show id)))))
644 (defun notmuch-tree-show-message (arg)
645 "Show the current message.
647 Shows in split pane or whole window according to value of
648 `notmuch-tree-show-out'. A prefix argument reverses the choice."
650 (if (or (and (notmuch-tree-show-out) (not arg))
651 (and (not (notmuch-tree-show-out)) arg))
652 (notmuch-tree-show-message-out)
653 (notmuch-tree-show-message-in)))
655 (defun notmuch-tree-scroll-message-window ()
656 "Scroll the message window (if it exists)."
658 (when (window-live-p notmuch-tree-message-window)
659 (with-selected-window notmuch-tree-message-window
660 (if (pos-visible-in-window-p (point-max))
664 (defun notmuch-tree-scroll-message-window-back ()
665 "Scroll the message window back (if it exists)."
667 (when (window-live-p notmuch-tree-message-window)
668 (with-selected-window notmuch-tree-message-window
669 (if (pos-visible-in-window-p (point-min))
673 (defun notmuch-tree-scroll-or-next ()
674 "Scroll the message window.
675 If it at end go to next message."
677 (when (notmuch-tree-scroll-message-window)
678 (notmuch-tree-next-matching-message)))
680 (defun notmuch-tree-quit (&optional kill-both)
681 "Close the split view or exit tree."
683 (when (or (not (notmuch-tree-close-message-window)) kill-both)
684 (kill-buffer (current-buffer))))
686 (defun notmuch-tree-close-message-window ()
687 "Close the message-window. Return t if close succeeds."
689 (when (and (window-live-p notmuch-tree-message-window)
690 (eq (window-buffer notmuch-tree-message-window)
691 notmuch-tree-message-buffer))
692 (delete-window notmuch-tree-message-window)
693 (unless (get-buffer-window-list notmuch-tree-message-buffer)
694 (kill-buffer notmuch-tree-message-buffer))
697 (defun notmuch-tree-archive-message (&optional unarchive)
698 "Archive the current message.
700 Archive the current message by applying the tag changes in
701 `notmuch-archive-tags' to it. If a prefix argument is given, the
702 message will be \"unarchived\", i.e. the tag changes in
703 `notmuch-archive-tags' will be reversed."
705 (when notmuch-archive-tags
707 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
709 (defun notmuch-tree-archive-message-then-next (&optional unarchive)
710 "Archive the current message and move to next matching message."
712 (notmuch-tree-archive-message unarchive)
713 (notmuch-tree-next-matching-message))
715 (defun notmuch-tree-archive-thread-then-exit ()
716 "Archive all messages in the current buffer, then exit notmuch-tree."
718 (notmuch-tree-archive-thread)
719 (notmuch-tree-quit t))
721 (defun notmuch-tree-archive-message-then-next-or-exit ()
722 "Archive current message, then show next open message in current thread.
724 If at the last open message in the current thread, then exit back
727 (notmuch-tree-archive-message)
728 (notmuch-tree-next-matching-message t))
730 (defun notmuch-tree-next-message ()
731 "Move to next message."
734 (when (window-live-p notmuch-tree-message-window)
735 (notmuch-tree-show-message-in)))
737 (defun notmuch-tree-prev-message ()
738 "Move to previous message."
741 (when (window-live-p notmuch-tree-message-window)
742 (notmuch-tree-show-message-in)))
744 (defun notmuch-tree-goto-matching-message (&optional prev)
745 "Move to the next or previous matching message.
747 Returns t if there was a next matching message in the thread to show,
749 (let ((dir (if prev -1 nil))
750 (eobfn (if prev #'bobp #'eobp)))
751 (while (and (not (funcall eobfn))
752 (not (notmuch-tree-get-match)))
754 (not (funcall eobfn))))
756 (defun notmuch-tree-matching-message (&optional prev pop-at-end)
757 "Move to the next or previous matching message."
759 (forward-line (if prev -1 nil))
760 (if (and (not (notmuch-tree-goto-matching-message prev)) pop-at-end)
761 (notmuch-tree-quit pop-at-end)
762 (when (window-live-p notmuch-tree-message-window)
763 (notmuch-tree-show-message-in))))
765 (defun notmuch-tree-prev-matching-message (&optional pop-at-end)
766 "Move to previous matching message."
768 (notmuch-tree-matching-message t pop-at-end))
770 (defun notmuch-tree-next-matching-message (&optional pop-at-end)
771 "Move to next matching message."
773 (notmuch-tree-matching-message nil pop-at-end))
775 (defun notmuch-tree-refresh-view (&optional view)
778 (when (get-buffer-process (current-buffer))
779 (error "notmuch tree process already running for current buffer"))
780 (let ((inhibit-read-only t)
781 (basic-query notmuch-tree-basic-query)
782 (unthreaded (cond ((eq view 'unthreaded) t)
783 ((eq view 'tree) nil)
784 (t notmuch-tree-unthreaded)))
785 (query-context notmuch-tree-query-context)
786 (target (notmuch-tree-get-message-id)))
788 (notmuch-tree-worker basic-query
793 notmuch-search-oldest-first)))
795 (defun notmuch-tree-thread-top ()
796 (when (notmuch-tree-get-message-properties)
797 (while (not (or (notmuch-tree-get-prop :first) (eobp)))
800 (defun notmuch-tree-prev-thread-in-tree ()
801 "Move to the previous thread in the current tree"
804 (notmuch-tree-thread-top)
807 (defun notmuch-tree-next-thread-in-tree ()
808 "Get the next thread in the current tree. Returns t if a thread was
809 found or nil if not."
812 (while (not (or (notmuch-tree-get-prop :first) (eobp)))
816 (defun notmuch-tree-next-thread-from-search (&optional previous)
817 "Move to the next thread in the parent search results, if any.
819 If PREVIOUS is non-nil, move to the previous item in the
820 search results instead."
822 (let ((parent-buffer notmuch-tree-parent-buffer))
823 (notmuch-tree-quit t)
824 (when (buffer-live-p parent-buffer)
825 (switch-to-buffer parent-buffer)
827 (notmuch-search-previous-thread)
828 (notmuch-search-next-thread))
829 (notmuch-tree-from-search-thread))))
831 (defun notmuch-tree-next-thread (&optional previous)
832 "Move to the next thread in the current tree or parent search results.
834 If PREVIOUS is non-nil, move to the previous thread in the tree or
835 search results instead."
837 (unless (if previous (notmuch-tree-prev-thread-in-tree)
838 (notmuch-tree-next-thread-in-tree))
839 (notmuch-tree-next-thread-from-search previous)))
841 (defun notmuch-tree-prev-thread ()
842 "Move to the previous thread in the current tree or parent search results."
844 (notmuch-tree-next-thread t))
846 (defun notmuch-tree-thread-mapcar (function)
847 "Call FUNCTION for each message in the current thread.
848 FUNCTION is called for side effects only."
850 (notmuch-tree-thread-top)
851 (cl-loop collect (funcall function)
853 while (and (notmuch-tree-get-message-properties)
854 (not (notmuch-tree-get-prop :first))))))
856 (defun notmuch-tree-get-messages-ids-thread-search ()
857 "Return a search string for all message ids of messages in the current thread."
859 (notmuch-tree-thread-mapcar 'notmuch-tree-get-message-id)
862 (defun notmuch-tree-tag-thread (tag-changes)
863 "Tag all messages in the current thread."
865 (let ((tags (apply #'append (notmuch-tree-thread-mapcar
866 (lambda () (notmuch-tree-get-tags))))))
867 (list (notmuch-read-tag-changes tags "Tag thread"))))
868 (when (notmuch-tree-get-message-properties)
869 (notmuch-tag (notmuch-tree-get-messages-ids-thread-search) tag-changes)
870 (notmuch-tree-thread-mapcar
871 (lambda () (notmuch-tree-tag-update-display tag-changes)))))
873 (defun notmuch-tree-archive-thread (&optional unarchive)
874 "Archive each message in thread.
876 Archive each message currently shown by applying the tag changes
877 in `notmuch-archive-tags' to each. If a prefix argument is given,
878 the messages will be \"unarchived\", i.e. the tag changes in
879 `notmuch-archive-tags' will be reversed.
881 Note: This command is safe from any race condition of new messages
882 being delivered to the same thread. It does not archive the
883 entire thread, but only the messages shown in the current
886 (when notmuch-archive-tags
887 (notmuch-tree-tag-thread
888 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
890 ;;; Functions for displaying the tree buffer itself
892 (defun notmuch-tree-clean-address (address)
893 "Try to clean a single email ADDRESS for display. Return
894 AUTHOR_NAME if present, otherwise return AUTHOR_EMAIL. Return
895 unchanged ADDRESS if parsing fails."
896 (let* ((clean-address (notmuch-clean-address address))
897 (p-address (car clean-address))
898 (p-name (cdr clean-address)))
900 ;; If we have a name return that otherwise return the address.
901 (or p-name p-address)))
903 (defun notmuch-tree-format-field (field format-string msg)
904 "Format a FIELD of MSG according to FORMAT-STRING and return string."
905 (let* ((headers (plist-get msg :headers))
906 (match (plist-get msg :match)))
909 (format format-string (notmuch-tree-format-field-list field msg)))
912 (funcall field format-string msg))
914 ((string-equal field "date")
915 (let ((face (if match
916 'notmuch-tree-match-date-face
917 'notmuch-tree-no-match-date-face)))
918 (propertize (format format-string (plist-get msg :date_relative))
921 ((string-equal field "tree")
922 (let ((tree-status (plist-get msg :tree-status))
924 'notmuch-tree-match-tree-face
925 'notmuch-tree-no-match-tree-face)))
927 (propertize (format format-string
928 (mapconcat #'identity (reverse tree-status) ""))
931 ((string-equal field "subject")
932 (let ((bare-subject (notmuch-show-strip-re (plist-get headers :Subject)))
933 (previous-subject notmuch-tree-previous-subject)
935 'notmuch-tree-match-subject-face
936 'notmuch-tree-no-match-subject-face)))
938 (setq notmuch-tree-previous-subject bare-subject)
939 (propertize (format format-string
940 (if (string= previous-subject bare-subject)
945 ((string-equal field "authors")
946 (let ((author (notmuch-tree-clean-address (plist-get headers :From)))
947 (len (length (format format-string "")))
949 'notmuch-tree-match-author-face
950 'notmuch-tree-no-match-author-face)))
951 (when (> (length author) len)
952 (setq author (substring author 0 len)))
953 (propertize (format format-string author) 'face face)))
955 ((string-equal field "tags")
956 (let ((tags (plist-get msg :tags))
957 (orig-tags (plist-get msg :orig-tags))
959 'notmuch-tree-match-tag-face
960 'notmuch-tree-no-match-tag-face)))
961 (format format-string (notmuch-tag-format-tags tags orig-tags face)))))))
963 (defun notmuch-tree-format-field-list (field-list msg)
964 "Format fields of MSG according to FIELD-LIST and return string."
965 (let ((face (if (plist-get msg :match)
966 'notmuch-tree-match-face
967 'notmuch-tree-no-match-face))
969 (dolist (spec field-list result-string)
970 (let ((field-string (notmuch-tree-format-field (car spec) (cdr spec) msg)))
971 (setq result-string (concat result-string field-string))))
972 (notmuch-apply-face result-string face t)))
974 (defun notmuch-tree-insert-msg (msg)
975 "Insert the message MSG according to notmuch-tree-result-format."
976 ;; We need to save the previous subject as it will get overwritten
977 ;; by the insert-field calls.
978 (let ((previous-subject notmuch-tree-previous-subject))
979 (insert (notmuch-tree-format-field-list (notmuch-tree-result-format) msg))
980 (notmuch-tree-set-message-properties msg)
981 (notmuch-tree-set-prop :previous-subject previous-subject)
984 (defun notmuch-tree-goto-and-insert-msg (msg)
985 "Insert msg at the end of the buffer. Move point to msg if it is the target."
987 (goto-char (point-max))
988 (notmuch-tree-insert-msg msg))
989 (let ((msg-id (notmuch-id-to-query (plist-get msg :id)))
990 (target notmuch-tree-target-msg))
991 (when (or (and (not target) (plist-get msg :match))
992 (string= msg-id target))
993 (setq notmuch-tree-target-msg "found")
994 (goto-char (point-max))
996 (when notmuch-tree-open-target
997 (notmuch-tree-show-message-in)
998 (notmuch-tree-command-hook)))))
1000 (defun notmuch-tree-insert-tree (tree depth tree-status first last)
1001 "Insert the message tree TREE at depth DEPTH in the current thread.
1003 A message tree is another name for a single sub-thread: i.e., a
1004 message together with all its descendents."
1005 (let ((msg (car tree))
1006 (replies (cadr tree)))
1008 ((and (< 0 depth) (not last))
1009 (push (alist-get 'vertical-tee notmuch-tree-thread-symbols) tree-status))
1010 ((and (< 0 depth) last)
1011 (push (alist-get 'bottom notmuch-tree-thread-symbols) tree-status))
1012 ((and (eq 0 depth) first last)
1013 (push (alist-get 'prefix notmuch-tree-thread-symbols) tree-status))
1014 ((and (eq 0 depth) first (not last))
1015 (push (alist-get 'top-tee notmuch-tree-thread-symbols) tree-status))
1016 ((and (eq 0 depth) (not first) last)
1017 (push (alist-get 'bottom notmuch-tree-thread-symbols) tree-status))
1018 ((and (eq 0 depth) (not first) (not last))
1019 (push (alist-get 'vertical-tee notmuch-tree-thread-symbols) tree-status)))
1020 (push (concat (alist-get (if replies 'top-tee 'top) notmuch-tree-thread-symbols)
1021 (alist-get 'arrow notmuch-tree-thread-symbols))
1023 (setq msg (plist-put msg :first (and first (eq 0 depth))))
1024 (setq msg (plist-put msg :tree-status tree-status))
1025 (setq msg (plist-put msg :orig-tags (plist-get msg :tags)))
1026 (notmuch-tree-goto-and-insert-msg msg)
1030 (push " " tree-status)
1031 (push (alist-get 'vertical notmuch-tree-thread-symbols) tree-status))
1032 (notmuch-tree-insert-thread replies (1+ depth) tree-status)))
1034 (defun notmuch-tree-insert-thread (thread depth tree-status)
1035 "Insert the collection of sibling sub-threads THREAD at depth DEPTH in the current forest."
1036 (let ((n (length thread)))
1037 (cl-loop for tree in thread
1038 for count from 1 to n
1039 do (notmuch-tree-insert-tree tree depth tree-status
1043 (defun notmuch-tree-insert-forest-thread (forest-thread)
1044 "Insert a single complete thread."
1045 ;; Reset at the start of each main thread.
1046 (setq notmuch-tree-previous-subject nil)
1047 (notmuch-tree-insert-thread forest-thread 0 nil))
1049 (defun notmuch-tree-insert-forest (forest)
1050 "Insert a forest of threads.
1052 This function inserts a collection of several complete threads as
1053 passed to it by notmuch-tree-process-filter."
1054 (mapc 'notmuch-tree-insert-forest-thread forest))
1056 (define-derived-mode notmuch-tree-mode fundamental-mode "notmuch-tree"
1057 "Major mode displaying messages (as opposed to threads) of a notmuch search.
1059 This buffer contains the results of a \"notmuch tree\" of your
1060 email archives. Each line in the buffer represents a single
1061 message giving the relative date, the author, subject, and any
1064 Pressing \\[notmuch-tree-show-message] on any line displays that message.
1066 Complete list of currently available key bindings:
1068 \\{notmuch-tree-mode-map}"
1069 (setq notmuch-buffer-refresh-function #'notmuch-tree-refresh-view)
1071 (setq buffer-read-only t)
1072 (setq truncate-lines t))
1074 (defun notmuch-tree-process-sentinel (proc _msg)
1075 "Add a message to let user know when \"notmuch tree\" exits."
1076 (let ((buffer (process-buffer proc))
1077 (status (process-status proc))
1078 (exit-status (process-exit-status proc)))
1079 (when (memq status '(exit signal))
1080 (kill-buffer (process-get proc 'parse-buf))
1081 (when (buffer-live-p buffer)
1082 (with-current-buffer buffer
1084 (let ((inhibit-read-only t))
1085 (goto-char (point-max))
1086 (when (eq status 'signal)
1087 (insert "Incomplete search results (tree view process was killed).\n"))
1088 (when (eq status 'exit)
1089 (insert "End of search results.")
1090 (unless (= exit-status 0)
1091 (insert (format " (process returned %d)" exit-status)))
1092 (insert "\n")))))))))
1094 (defun notmuch-tree-process-filter (proc string)
1095 "Process and filter the output of \"notmuch show\" for tree view."
1096 (let ((results-buf (process-buffer proc))
1097 (parse-buf (process-get proc 'parse-buf))
1098 (inhibit-read-only t))
1099 (if (not (buffer-live-p results-buf))
1100 (delete-process proc)
1101 (with-current-buffer parse-buf
1104 (goto-char (point-max))
1106 (notmuch-sexp-parse-partial-list 'notmuch-tree-insert-forest-thread
1109 (defun notmuch-tree-worker (basic-query &optional query-context target
1110 open-target unthreaded oldest-first)
1111 "Insert the tree view of the search in the current buffer.
1113 This is is a helper function for notmuch-tree. The arguments are
1114 the same as for the function notmuch-tree."
1117 (add-hook 'post-command-hook #'notmuch-tree-command-hook t t)
1118 (setq notmuch-search-oldest-first oldest-first)
1119 (setq notmuch-tree-unthreaded unthreaded)
1120 (setq notmuch-tree-basic-query basic-query)
1121 (setq notmuch-tree-query-context (if (or (string= query-context "")
1122 (string= query-context "*"))
1125 (setq notmuch-tree-target-msg target)
1126 (setq notmuch-tree-open-target open-target)
1127 ;; Set the default value for `notmuch-show-process-crypto' in this
1128 ;; buffer. Although we don't use this some of the functions we call
1129 ;; (such as reply) do. It is a buffer local variable so setting it
1130 ;; will not affect genuine show buffers.
1131 (setq notmuch-show-process-crypto notmuch-crypto-process-mime)
1133 (goto-char (point-min))
1134 (let* ((search-args (concat basic-query
1136 (concat " and (" query-context ")"))))
1137 (sort-arg (if oldest-first "--sort=oldest-first" "--sort=newest-first"))
1138 (message-arg (if unthreaded "--unthreaded" "--entire-thread")))
1139 (when (equal (car (notmuch--process-lines notmuch-command "count" search-args)) "0")
1140 (setq search-args basic-query))
1141 (notmuch-tag-clear-cache)
1142 (let ((proc (notmuch-start-notmuch
1143 "notmuch-tree" (current-buffer) #'notmuch-tree-process-sentinel
1144 "show" "--body=false" "--format=sexp" "--format-version=5"
1145 sort-arg message-arg search-args))
1146 ;; Use a scratch buffer to accumulate partial output.
1147 ;; This buffer will be killed by the sentinel, which
1148 ;; should be called no matter how the process dies.
1149 (parse-buf (generate-new-buffer " *notmuch tree parse*")))
1150 (process-put proc 'parse-buf parse-buf)
1151 (set-process-filter proc 'notmuch-tree-process-filter)
1152 (set-process-query-on-exit-flag proc nil))))
1154 (defun notmuch-tree-get-query ()
1155 "Return the current query in this tree buffer."
1156 (if notmuch-tree-query-context
1157 (concat notmuch-tree-basic-query
1159 notmuch-tree-query-context
1161 notmuch-tree-basic-query))
1163 (defun notmuch-tree-toggle-order ()
1164 "Toggle the current search order.
1166 This command toggles the sort order for the current search. The
1167 default sort order is defined by `notmuch-search-oldest-first'."
1169 (setq notmuch-search-oldest-first (not notmuch-search-oldest-first))
1170 (notmuch-tree-refresh-view))
1172 (defun notmuch-tree (&optional query query-context target buffer-name
1173 open-target unthreaded parent-buffer oldest-first)
1174 "Display threads matching QUERY in tree view.
1177 QUERY: the main query. This can be any query but in many cases will be
1178 a single thread. If nil this is read interactively from the minibuffer.
1179 QUERY-CONTEXT: is an additional term for the query. The query used
1180 is QUERY and QUERY-CONTEXT unless that does not match any messages
1181 in which case we fall back to just QUERY.
1182 TARGET: A message ID (with the id: prefix) that will be made
1183 current if it appears in the tree view results.
1184 BUFFER-NAME: the name of the buffer to display the tree view. If
1185 it is nil \"*notmuch-tree\" followed by QUERY is used.
1186 OPEN-TARGET: If TRUE open the target message in the message pane.
1187 UNTHREADED: If TRUE only show matching messages in an unthreaded view."
1190 (setq query (notmuch-read-query (concat "Notmuch "
1191 (if unthreaded "unthreaded " "tree ")
1195 (notmuch-search-buffer-title query
1196 (if unthreaded "unthreaded" "tree"))))
1197 (buffer (get-buffer-create (generate-new-buffer-name name)))
1198 (inhibit-read-only t))
1199 (pop-to-buffer-same-window buffer))
1200 ;; Don't track undo information for this buffer
1201 (setq buffer-undo-list t)
1202 (notmuch-tree-worker query query-context target open-target unthreaded oldest-first)
1203 (setq notmuch-tree-parent-buffer parent-buffer)
1204 (setq truncate-lines t))
1206 (defun notmuch-unthreaded (&optional query query-context target buffer-name
1208 "Display threads matching QUERY in unthreaded view.
1210 See function NOTMUCH-TREE for documentation of the arguments"
1212 (notmuch-tree query query-context target buffer-name open-target t))
1214 (defun notmuch-tree-filter (query)
1215 "Filter or LIMIT the current search results based on an additional query string.
1217 Runs a new tree search matching only messages that match both the
1218 current search results AND the additional query string provided."
1219 (interactive (list (notmuch-read-query "Filter search: ")))
1220 (let ((notmuch-show-process-crypto (notmuch-tree--message-process-crypto))
1221 (grouped-query (notmuch-group-disjunctive-query-string query))
1222 (grouped-original-query (notmuch-group-disjunctive-query-string
1223 (notmuch-tree-get-query))))
1224 (notmuch-tree-close-message-window)
1225 (notmuch-tree (if (string= grouped-original-query "*")
1227 (concat grouped-original-query " and " grouped-query)))))
1229 (defun notmuch-tree-filter-by-tag (tag)
1230 "Filter the current search results based on a single TAG.
1232 Run a new search matching only messages that match the current
1233 search results and that are also tagged with the given TAG."
1235 (list (notmuch-select-tag-with-completion "Filter by tag: "
1236 notmuch-tree-basic-query)))
1237 (let ((notmuch-show-process-crypto (notmuch-tree--message-process-crypto)))
1238 (notmuch-tree-close-message-window)
1239 (notmuch-tree (concat notmuch-tree-basic-query " and tag:" tag)
1240 notmuch-tree-query-context
1244 notmuch-tree-unthreaded
1246 notmuch-search-oldest-first)))
1250 (provide 'notmuch-tree)
1252 ;;; notmuch-tree.el ends here