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-query)
31 (require 'notmuch-show)
32 (require 'notmuch-tag)
33 (require 'notmuch-parser)
34 (require 'notmuch-jump)
36 (declare-function notmuch-search "notmuch"
37 (&optional query oldest-first target-thread target-line
39 (declare-function notmuch-call-notmuch-process "notmuch-lib" (&rest args))
40 (declare-function notmuch-read-query "notmuch" (prompt))
41 (declare-function notmuch-search-find-thread-id "notmuch" (&optional bare))
42 (declare-function notmuch-search-find-subject "notmuch" ())
44 ;; For `notmuch-tree-next-thread-from-search'.
45 (declare-function notmuch-search-next-thread "notmuch" ())
46 (declare-function notmuch-search-previous-thread "notmuch" ())
47 (declare-function notmuch-tree-from-search-thread "notmuch" ())
49 ;; the following variable is defined in notmuch.el
50 (defvar notmuch-search-query-string)
52 ;; this variable distinguishes the unthreaded display from the normal tree display
53 (defvar-local notmuch-tree-unthreaded nil
54 "A buffer local copy of argument unthreaded to the function notmuch-tree.")
58 (defgroup notmuch-tree nil
59 "Showing message and thread structure."
62 (defcustom notmuch-tree-show-out nil
63 "View selected messages in new window rather than split-pane."
67 (defcustom notmuch-unthreaded-show-out t
68 "View selected messages in new window rather than split-pane."
72 (defun notmuch-tree-show-out ()
73 (if notmuch-tree-unthreaded
74 notmuch-unthreaded-show-out
75 notmuch-tree-show-out))
77 (defcustom notmuch-tree-thread-symbols
85 "Strings used to draw trees in notmuch tree results.
86 Symbol keys denote where the corresponding string value is used:
87 `prefix' is used at the top of the tree, followed by `top' if it
88 has no children or `top-tee' if it does; `vertical' is a bar
89 connecting with a response down the list skipping the current
90 one, while `vertical-tee' marks the current message as a reply to
91 the previous one; `bottom' is used at the bottom of threads.
92 Finally, the `arrrow' string in the list is used as a pointer to
95 Common customizations include setting `prefix' to \"-\", to see
96 equal-length prefixes, and `arrow' to an empty string or to a
97 different kind of arrow point."
98 :type '(alist :key-type symbol :value-type string)
101 (defcustom notmuch-tree-result-format
103 ("authors" . "%-20s")
108 "Result formatting for tree view.
110 Supported fields are: date, authors, subject, tree, tags.
112 Tree means the thread tree box graphics. The field may
113 also be a list in which case the formatting rules are
114 applied recursively and then the output of all the fields
115 in the list is inserted according to format-string.
117 Note that the author string should not contain whitespace
118 \(put it in the neighbouring fields instead). For example:
119 (setq notmuch-tree-result-format
120 '((\"authors\" . \"%-40s\")
121 (\"subject\" . \"%s\")))"
122 :type '(alist :key-type (choice string
123 (alist :key-type string
126 :group 'notmuch-tree)
128 (defcustom notmuch-unthreaded-result-format
130 ("authors" . "%-20s")
131 ((("subject" . "%s")) ." %-54s ")
133 "Result formatting for unthreaded tree view.
135 Supported fields are: date, authors, subject, tree, tags.
137 Tree means the thread tree box graphics. The field may
138 also be a list in which case the formatting rules are
139 applied recursively and then the output of all the fields
140 in the list is inserted according to format-string.
142 Note that the author string should not contain whitespace
143 \(put it in the neighbouring fields instead). For example:
144 (setq notmuch-unthreaded-result-format
145 '((\"authors\" . \"%-40s\")
146 (\"subject\" . \"%s\")))"
147 :type '(alist :key-type (choice string
148 (alist :key-type string
151 :group 'notmuch-tree)
153 (defun notmuch-tree-result-format ()
154 (if notmuch-tree-unthreaded
155 notmuch-unthreaded-result-format
156 notmuch-tree-result-format))
159 ;;;; Faces for messages that match the query
161 (defface notmuch-tree-match-face
162 '((t :inherit default))
163 "Default face used in tree mode face for matching messages"
165 :group 'notmuch-faces)
167 (defface notmuch-tree-match-date-face
169 "Face used in tree mode for the date in messages matching the query."
171 :group 'notmuch-faces)
173 (defface notmuch-tree-match-author-face
176 (:foreground "OliveDrab1"))
179 (:foreground "dark blue"))
182 "Face used in tree mode for the author in messages matching the query."
184 :group 'notmuch-faces)
186 (defface notmuch-tree-match-subject-face
188 "Face used in tree mode for the subject in messages matching the query."
190 :group 'notmuch-faces)
192 (defface notmuch-tree-match-tree-face
194 "Face used in tree mode for the thread tree block graphics in messages matching the query."
196 :group 'notmuch-faces)
198 (defface notmuch-tree-match-tag-face
201 (:foreground "OliveDrab1"))
204 (:foreground "navy blue" :bold t))
207 "Face used in tree mode for tags in messages matching the query."
209 :group 'notmuch-faces)
211 ;;;; Faces for messages that do not match the query
213 (defface notmuch-tree-no-match-face
214 '((t (:foreground "gray")))
215 "Default face used in tree mode face for non-matching messages."
217 :group 'notmuch-faces)
219 (defface notmuch-tree-no-match-date-face
221 "Face used in tree mode for non-matching dates."
223 :group 'notmuch-faces)
225 (defface notmuch-tree-no-match-subject-face
227 "Face used in tree mode for non-matching subjects."
229 :group 'notmuch-faces)
231 (defface notmuch-tree-no-match-tree-face
233 "Face used in tree mode for the thread tree block graphics in messages matching the query."
235 :group 'notmuch-faces)
237 (defface notmuch-tree-no-match-author-face
239 "Face used in tree mode for non-matching authors."
241 :group 'notmuch-faces)
243 (defface notmuch-tree-no-match-tag-face
245 "Face used in tree mode face for non-matching tags."
247 :group 'notmuch-faces)
251 (defvar-local notmuch-tree-previous-subject
252 "The subject of the most recent result shown during the async display.")
254 (defvar-local notmuch-tree-basic-query nil
255 "A buffer local copy of argument query to the function notmuch-tree.")
257 (defvar-local notmuch-tree-query-context nil
258 "A buffer local copy of argument query-context to the function notmuch-tree.")
260 (defvar-local notmuch-tree-target-msg nil
261 "A buffer local copy of argument target to the function notmuch-tree.")
263 (defvar-local notmuch-tree-open-target nil
264 "A buffer local copy of argument open-target to the function notmuch-tree.")
266 (defvar-local notmuch-tree-parent-buffer nil)
268 (defvar-local notmuch-tree-message-window nil
269 "The window of the message pane.
271 It is set in both the tree buffer and the child show buffer. It
272 is used to try and close the message pane when quitting tree view
273 or the child show buffer.")
274 (put 'notmuch-tree-message-window 'permanent-local t)
276 (defvar-local notmuch-tree-message-buffer nil
277 "The buffer name of the show buffer in the message pane.
279 This is used to try and make sure we don't close the message pane
280 if the user has loaded a different buffer in that window.")
281 (put 'notmuch-tree-message-buffer 'permanent-local t)
283 ;;; Tree wrapper commands
285 (defmacro notmuch-tree--define-do-in-message-window (name cmd)
286 "Define NAME as a command that calls CMD interactively in the message window.
287 If the message pane is closed then this command does nothing.
288 Avoid using this macro in new code; it will be removed."
290 ,(concat "(In message window) " (documentation cmd t))
292 (when (window-live-p notmuch-tree-message-window)
293 (with-selected-window notmuch-tree-message-window
294 (call-interactively #',cmd)))))
296 (notmuch-tree--define-do-in-message-window
297 notmuch-tree-previous-message-button
298 notmuch-show-previous-button)
299 (notmuch-tree--define-do-in-message-window
300 notmuch-tree-next-message-button
301 notmuch-show-next-button)
302 (notmuch-tree--define-do-in-message-window
303 notmuch-tree-toggle-message-process-crypto
304 notmuch-show-toggle-process-crypto)
306 (defun notmuch-tree--message-process-crypto ()
307 "Return value of `notmuch-show-process-crypto' in the message window.
308 If that window isn't alive, then return the current value.
309 Avoid using this function in new code; it will be removed."
310 (if (window-live-p notmuch-tree-message-window)
311 (with-selected-window notmuch-tree-message-window
312 notmuch-show-process-crypto)
313 notmuch-show-process-crypto))
315 (defmacro notmuch-tree--define-close-message-window-and (name cmd)
316 "Define NAME as a variant of CMD.
318 NAME determines the value of `notmuch-show-process-crypto' in the
319 message window, closes the window, and then call CMD interactively
320 with that value let-bound. If the message window does not exist,
321 then NAME behaves like CMD."
323 ,(concat "(Close message pane and) " (documentation cmd t))
325 (let ((notmuch-show-process-crypto
326 (notmuch-tree--message-process-crypto)))
327 (notmuch-tree-close-message-window)
328 (call-interactively #',cmd))))
330 (notmuch-tree--define-close-message-window-and
333 (notmuch-tree--define-close-message-window-and
334 notmuch-tree-new-mail
335 notmuch-mua-new-mail)
336 (notmuch-tree--define-close-message-window-and
337 notmuch-tree-jump-search
339 (notmuch-tree--define-close-message-window-and
340 notmuch-tree-forward-message
341 notmuch-show-forward-message)
342 (notmuch-tree--define-close-message-window-and
343 notmuch-tree-reply-sender
344 notmuch-show-reply-sender)
345 (notmuch-tree--define-close-message-window-and
348 (notmuch-tree--define-close-message-window-and
349 notmuch-tree-view-raw-message
350 notmuch-show-view-raw-message)
354 (defvar notmuch-tree-mode-map
355 (let ((map (make-sparse-keymap)))
356 (set-keymap-parent map notmuch-common-keymap)
357 ;; These bindings shadow common bindings with variants
358 ;; that additionally close the message window.
359 (define-key map [remap notmuch-bury-or-kill-this-buffer] 'notmuch-tree-quit)
360 (define-key map [remap notmuch-search] 'notmuch-tree-to-search)
361 (define-key map [remap notmuch-help] 'notmuch-tree-help)
362 (define-key map [remap notmuch-mua-new-mail] 'notmuch-tree-new-mail)
363 (define-key map [remap notmuch-jump-search] 'notmuch-tree-jump-search)
365 (define-key map "o" 'notmuch-tree-toggle-order)
366 (define-key map "S" 'notmuch-search-from-tree-current-query)
367 (define-key map "U" 'notmuch-unthreaded-from-tree-current-query)
368 (define-key map "Z" 'notmuch-tree-from-unthreaded-current-query)
370 ;; these use notmuch-show functions directly
371 (define-key map "|" 'notmuch-show-pipe-message)
372 (define-key map "w" 'notmuch-show-save-attachments)
373 (define-key map "v" 'notmuch-show-view-all-mime-parts)
374 (define-key map "c" 'notmuch-show-stash-map)
375 (define-key map "b" 'notmuch-show-resend-message)
377 ;; these apply to the message pane
378 (define-key map (kbd "M-TAB") 'notmuch-tree-previous-message-button)
379 (define-key map (kbd "<backtab>") 'notmuch-tree-previous-message-button)
380 (define-key map (kbd "TAB") 'notmuch-tree-next-message-button)
381 (define-key map "$" 'notmuch-tree-toggle-message-process-crypto)
383 ;; bindings from show (or elsewhere) but we close the message pane first.
384 (define-key map "f" 'notmuch-tree-forward-message)
385 (define-key map "r" 'notmuch-tree-reply-sender)
386 (define-key map "R" 'notmuch-tree-reply)
387 (define-key map "V" 'notmuch-tree-view-raw-message)
388 (define-key map "l" 'notmuch-tree-filter)
389 (define-key map "t" 'notmuch-tree-filter-by-tag)
391 ;; The main tree view bindings
392 (define-key map (kbd "RET") 'notmuch-tree-show-message)
393 (define-key map [mouse-1] 'notmuch-tree-show-message)
394 (define-key map "x" 'notmuch-tree-archive-message-then-next-or-exit)
395 (define-key map "X" 'notmuch-tree-archive-thread-then-exit)
396 (define-key map "A" 'notmuch-tree-archive-thread-then-next)
397 (define-key map "a" 'notmuch-tree-archive-message-then-next)
398 (define-key map "z" 'notmuch-tree-to-tree)
399 (define-key map "n" 'notmuch-tree-next-matching-message)
400 (define-key map "p" 'notmuch-tree-prev-matching-message)
401 (define-key map "N" 'notmuch-tree-next-message)
402 (define-key map "P" 'notmuch-tree-prev-message)
403 (define-key map (kbd "M-p") 'notmuch-tree-prev-thread)
404 (define-key map (kbd "M-n") 'notmuch-tree-next-thread)
405 (define-key map "k" 'notmuch-tag-jump)
406 (define-key map "-" 'notmuch-tree-remove-tag)
407 (define-key map "+" 'notmuch-tree-add-tag)
408 (define-key map "*" 'notmuch-tree-tag-thread)
409 (define-key map " " 'notmuch-tree-scroll-or-next)
410 (define-key map (kbd "DEL") 'notmuch-tree-scroll-message-window-back)
411 (define-key map "e" 'notmuch-tree-resume-message)
413 "Keymap for \"notmuch tree\" buffers.")
415 ;;; Message properties
417 (defun notmuch-tree-get-message-properties ()
418 "Return the properties of the current message as a plist.
420 Some useful entries are:
421 :headers - Property list containing the headers :Date, :Subject, :From, etc.
422 :tags - Tags for this message."
425 (get-text-property (point) :notmuch-message-properties)))
427 (defun notmuch-tree-set-message-properties (props)
430 (put-text-property (point)
432 :notmuch-message-properties props)))
434 (defun notmuch-tree-set-prop (prop val &optional props)
435 (let ((inhibit-read-only t)
437 (notmuch-tree-get-message-properties))))
438 (plist-put props prop val)
439 (notmuch-tree-set-message-properties props)))
441 (defun notmuch-tree-get-prop (prop &optional props)
442 (plist-get (or props (notmuch-tree-get-message-properties))
445 (defun notmuch-tree-set-tags (tags)
446 "Set the tags of the current message."
447 (notmuch-tree-set-prop :tags tags))
449 (defun notmuch-tree-get-tags ()
450 "Return the tags of the current message."
451 (notmuch-tree-get-prop :tags))
453 (defun notmuch-tree-get-message-id (&optional bare)
454 "Return the message id of the current message."
455 (let ((id (notmuch-tree-get-prop :id)))
459 (notmuch-id-to-query id))
462 (defun notmuch-tree-get-match ()
463 "Return whether the current message is a match."
464 (notmuch-tree-get-prop :match))
468 (defun notmuch-tree-refresh-result ()
469 "Redisplay the current message line.
471 This redisplays the current line based on the messages
472 properties (as they are now). This is used when tags are
474 (let ((init-point (point))
475 (end (line-end-position))
476 (msg (notmuch-tree-get-message-properties))
477 (inhibit-read-only t))
479 ;; This is a little tricky: we override
480 ;; notmuch-tree-previous-subject to get the decision between
481 ;; ... and a subject right and it stops notmuch-tree-insert-msg
482 ;; from overwriting the buffer local copy of
483 ;; notmuch-tree-previous-subject if this is called while the
484 ;; buffer is displaying.
485 (let ((notmuch-tree-previous-subject
486 (notmuch-tree-get-prop :previous-subject)))
487 (delete-region (point) (1+ (line-end-position)))
488 (notmuch-tree-insert-msg msg))
489 (let ((new-end (line-end-position)))
490 (goto-char (if (= init-point end)
492 (min init-point (- new-end 1)))))))
494 (defun notmuch-tree-tag-update-display (&optional tag-changes)
495 "Update display for TAG-CHANGES to current message.
497 Updates the message in the message pane if appropriate, but does
498 NOT change the database."
499 (let* ((current-tags (notmuch-tree-get-tags))
500 (new-tags (notmuch-update-tags current-tags tag-changes))
501 (tree-msg-id (notmuch-tree-get-message-id)))
502 (unless (equal current-tags new-tags)
503 (notmuch-tree-set-tags new-tags)
504 (notmuch-tree-refresh-result)
505 (when (window-live-p notmuch-tree-message-window)
506 (with-selected-window notmuch-tree-message-window
507 (when (string= tree-msg-id (notmuch-show-get-message-id))
508 (notmuch-show-update-tags new-tags)))))))
510 ;;; Commands (and some helper functions used by them)
512 (defun notmuch-tree-tag (tag-changes)
513 "Change tags for the current message."
515 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message")))
516 (notmuch-tag (notmuch-tree-get-message-id) tag-changes)
517 (notmuch-tree-tag-update-display tag-changes))
519 (defun notmuch-tree-add-tag (tag-changes)
520 "Same as `notmuch-tree-tag' but sets initial input to '+'."
522 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "+")))
523 (notmuch-tree-tag tag-changes))
525 (defun notmuch-tree-remove-tag (tag-changes)
526 "Same as `notmuch-tree-tag' but sets initial input to '-'."
528 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "-")))
529 (notmuch-tree-tag tag-changes))
531 (defun notmuch-tree-resume-message ()
532 "Resume EDITING the current draft message."
534 (notmuch-tree-close-message-window)
535 (let ((id (notmuch-tree-get-message-id)))
537 (notmuch-draft-resume id)
538 (message "No message to resume!"))))
540 ;; The next two functions close the message window before calling
541 ;; notmuch-search or notmuch-tree but they do so after the user has
542 ;; entered the query (in case the user was basing the query on
543 ;; something in the message window).
545 (defun notmuch-tree-to-search ()
546 "Run \"notmuch search\" with the given `query' and display results."
548 (let ((query (notmuch-read-query "Notmuch search: ")))
549 (notmuch-tree-close-message-window)
550 (notmuch-search query)))
552 (defun notmuch-tree-to-tree ()
553 "Run a query and display results in tree view."
555 (let ((query (notmuch-read-query "Notmuch tree view search: ")))
556 (notmuch-tree-close-message-window)
557 (notmuch-tree query)))
559 (defun notmuch-tree-archive-thread-then-next ()
560 "Archive all messages in the current buffer, then show next thread from search."
562 (notmuch-tree-archive-thread)
563 (notmuch-tree-next-thread))
565 (defun notmuch-unthreaded-from-tree-current-query ()
566 "Switch from tree view to unthreaded view."
568 (unless notmuch-tree-unthreaded
569 (notmuch-tree-refresh-view 'unthreaded)))
571 (defun notmuch-tree-from-unthreaded-current-query ()
572 "Switch from unthreaded view to tree view."
574 (when notmuch-tree-unthreaded
575 (notmuch-tree-refresh-view 'tree)))
577 (defun notmuch-search-from-tree-current-query ()
578 "Call notmuch search with the current query."
580 (notmuch-tree-close-message-window)
581 (notmuch-search (notmuch-tree-get-query)))
583 (defun notmuch-tree-message-window-kill-hook ()
584 "Close the message pane when exiting the show buffer."
585 (let ((buffer (current-buffer)))
586 (when (and (window-live-p notmuch-tree-message-window)
587 (eq (window-buffer notmuch-tree-message-window) buffer))
588 ;; We could check whether this is the only window in its frame,
589 ;; but simply ignoring the error that is thrown otherwise is
590 ;; what we had to do for Emacs 24 and we stick to that because
591 ;; it is still the simplest approach.
593 (delete-window notmuch-tree-message-window)))))
595 (defun notmuch-tree-command-hook ()
596 (when (eq major-mode 'notmuch-tree-mode)
597 ;; We just run the notmuch-show-command-hook on the message pane.
598 (when (buffer-live-p notmuch-tree-message-buffer)
599 (with-current-buffer notmuch-tree-message-buffer
600 (notmuch-show-command-hook)))))
602 (defun notmuch-tree-show-message-in ()
603 "Show the current message (in split-pane)."
605 (let ((id (notmuch-tree-get-message-id))
606 (inhibit-read-only t)
609 ;; We close and reopen the window to kill off un-needed buffers
610 ;; this might cause flickering but seems ok.
611 (notmuch-tree-close-message-window)
612 (setq notmuch-tree-message-window
613 (split-window-vertically (/ (window-height) 4)))
614 (with-selected-window notmuch-tree-message-window
615 (let (;; Since we are only displaying one message do not indent.
616 (notmuch-show-indent-messages-width 0)
617 (notmuch-show-single-message t)
618 ;; Ensure that `pop-to-buffer-same-window' uses the
619 ;; window we want it to use.
620 (display-buffer-overriding-action
621 '((display-buffer-same-window)
622 (inhibit-same-window . nil))))
623 (setq buffer (notmuch-show id))))
624 ;; We need the `let' as notmuch-tree-message-window is buffer local.
625 (let ((window notmuch-tree-message-window))
626 (with-current-buffer buffer
627 (setq notmuch-tree-message-window window)
628 (add-hook 'kill-buffer-hook 'notmuch-tree-message-window-kill-hook)))
629 (when notmuch-show-mark-read-tags
630 (notmuch-tree-tag-update-display notmuch-show-mark-read-tags))
631 (setq notmuch-tree-message-buffer buffer))))
633 (defun notmuch-tree-show-message-out ()
634 "Show the current message (in whole window)."
636 (let ((id (notmuch-tree-get-message-id))
637 (inhibit-read-only t))
639 ;; We close the window to kill off un-needed buffers.
640 (notmuch-tree-close-message-window)
641 ;; n-s-s-m is buffer local, so use inner let.
642 (let ((notmuch-show-single-message t))
643 (notmuch-show id)))))
645 (defun notmuch-tree-show-message (arg)
646 "Show the current message.
648 Shows in split pane or whole window according to value of
649 `notmuch-tree-show-out'. A prefix argument reverses the choice."
651 (if (or (and (notmuch-tree-show-out) (not arg))
652 (and (not (notmuch-tree-show-out)) arg))
653 (notmuch-tree-show-message-out)
654 (notmuch-tree-show-message-in)))
656 (defun notmuch-tree-scroll-message-window ()
657 "Scroll the message window (if it exists)."
659 (when (window-live-p notmuch-tree-message-window)
660 (with-selected-window notmuch-tree-message-window
661 (if (pos-visible-in-window-p (point-max))
665 (defun notmuch-tree-scroll-message-window-back ()
666 "Scroll the message window back (if it exists)."
668 (when (window-live-p notmuch-tree-message-window)
669 (with-selected-window notmuch-tree-message-window
670 (if (pos-visible-in-window-p (point-min))
674 (defun notmuch-tree-scroll-or-next ()
675 "Scroll the message window.
676 If it at end go to next message."
678 (when (notmuch-tree-scroll-message-window)
679 (notmuch-tree-next-matching-message)))
681 (defun notmuch-tree-quit (&optional kill-both)
682 "Close the split view or exit tree."
684 (when (or (not (notmuch-tree-close-message-window)) kill-both)
685 (kill-buffer (current-buffer))))
687 (defun notmuch-tree-close-message-window ()
688 "Close the message-window. Return t if close succeeds."
690 (when (and (window-live-p notmuch-tree-message-window)
691 (eq (window-buffer notmuch-tree-message-window)
692 notmuch-tree-message-buffer))
693 (delete-window notmuch-tree-message-window)
694 (unless (get-buffer-window-list notmuch-tree-message-buffer)
695 (kill-buffer notmuch-tree-message-buffer))
698 (defun notmuch-tree-archive-message (&optional unarchive)
699 "Archive the current message.
701 Archive the current message by applying the tag changes in
702 `notmuch-archive-tags' to it. If a prefix argument is given, the
703 message will be \"unarchived\", i.e. the tag changes in
704 `notmuch-archive-tags' will be reversed."
706 (when notmuch-archive-tags
708 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
710 (defun notmuch-tree-archive-message-then-next (&optional unarchive)
711 "Archive the current message and move to next matching message."
713 (notmuch-tree-archive-message unarchive)
714 (notmuch-tree-next-matching-message))
716 (defun notmuch-tree-archive-thread-then-exit ()
717 "Archive all messages in the current buffer, then exit notmuch-tree."
719 (notmuch-tree-archive-thread)
720 (notmuch-tree-quit t))
722 (defun notmuch-tree-archive-message-then-next-or-exit ()
723 "Archive current message, then show next open message in current thread.
725 If at the last open message in the current thread, then exit back
728 (notmuch-tree-archive-message)
729 (notmuch-tree-next-matching-message t))
731 (defun notmuch-tree-next-message ()
732 "Move to next message."
735 (when (window-live-p notmuch-tree-message-window)
736 (notmuch-tree-show-message-in)))
738 (defun notmuch-tree-prev-message ()
739 "Move to previous message."
742 (when (window-live-p notmuch-tree-message-window)
743 (notmuch-tree-show-message-in)))
745 (defun notmuch-tree-goto-matching-message (&optional prev)
746 "Move to the next or previous matching message.
748 Returns t if there was a next matching message in the thread to show,
750 (let ((dir (if prev -1 nil))
751 (eobfn (if prev #'bobp #'eobp)))
752 (while (and (not (funcall eobfn))
753 (not (notmuch-tree-get-match)))
755 (not (funcall eobfn))))
757 (defun notmuch-tree-matching-message (&optional prev pop-at-end)
758 "Move to the next or previous matching message."
760 (forward-line (if prev -1 nil))
761 (if (and (not (notmuch-tree-goto-matching-message prev)) pop-at-end)
762 (notmuch-tree-quit pop-at-end)
763 (when (window-live-p notmuch-tree-message-window)
764 (notmuch-tree-show-message-in))))
766 (defun notmuch-tree-prev-matching-message (&optional pop-at-end)
767 "Move to previous matching message."
769 (notmuch-tree-matching-message t pop-at-end))
771 (defun notmuch-tree-next-matching-message (&optional pop-at-end)
772 "Move to next matching message."
774 (notmuch-tree-matching-message nil pop-at-end))
776 (defun notmuch-tree-refresh-view (&optional view)
779 (when (get-buffer-process (current-buffer))
780 (error "notmuch tree process already running for current buffer"))
781 (let ((inhibit-read-only t)
782 (basic-query notmuch-tree-basic-query)
783 (unthreaded (cond ((eq view 'unthreaded) t)
784 ((eq view 'tree) nil)
785 (t notmuch-tree-unthreaded)))
786 (query-context notmuch-tree-query-context)
787 (target (notmuch-tree-get-message-id)))
789 (notmuch-tree-worker basic-query
794 notmuch-search-oldest-first)))
796 (defun notmuch-tree-thread-top ()
797 (when (notmuch-tree-get-message-properties)
798 (while (not (or (notmuch-tree-get-prop :first) (eobp)))
801 (defun notmuch-tree-prev-thread-in-tree ()
802 "Move to the previous thread in the current tree"
805 (notmuch-tree-thread-top)
808 (defun notmuch-tree-next-thread-in-tree ()
809 "Get the next thread in the current tree. Returns t if a thread was
810 found or nil if not."
813 (while (not (or (notmuch-tree-get-prop :first) (eobp)))
817 (defun notmuch-tree-next-thread-from-search (&optional previous)
818 "Move to the next thread in the parent search results, if any.
820 If PREVIOUS is non-nil, move to the previous item in the
821 search results instead."
823 (let ((parent-buffer notmuch-tree-parent-buffer))
824 (notmuch-tree-quit t)
825 (when (buffer-live-p parent-buffer)
826 (switch-to-buffer parent-buffer)
828 (notmuch-search-previous-thread)
829 (notmuch-search-next-thread))
830 (notmuch-tree-from-search-thread))))
832 (defun notmuch-tree-next-thread (&optional previous)
833 "Move to the next thread in the current tree or parent search results.
835 If PREVIOUS is non-nil, move to the previous thread in the tree or
836 search results instead."
838 (unless (if previous (notmuch-tree-prev-thread-in-tree)
839 (notmuch-tree-next-thread-in-tree))
840 (notmuch-tree-next-thread-from-search previous)))
842 (defun notmuch-tree-prev-thread ()
843 "Move to the previous thread in the current tree or parent search results."
845 (notmuch-tree-next-thread t))
847 (defun notmuch-tree-thread-mapcar (function)
848 "Call FUNCTION for each message in the current thread.
849 FUNCTION is called for side effects only."
851 (notmuch-tree-thread-top)
852 (cl-loop collect (funcall function)
854 while (and (notmuch-tree-get-message-properties)
855 (not (notmuch-tree-get-prop :first))))))
857 (defun notmuch-tree-get-messages-ids-thread-search ()
858 "Return a search string for all message ids of messages in the current thread."
860 (notmuch-tree-thread-mapcar 'notmuch-tree-get-message-id)
863 (defun notmuch-tree-tag-thread (tag-changes)
864 "Tag all messages in the current thread."
866 (let ((tags (apply #'append (notmuch-tree-thread-mapcar
867 (lambda () (notmuch-tree-get-tags))))))
868 (list (notmuch-read-tag-changes tags "Tag thread"))))
869 (when (notmuch-tree-get-message-properties)
870 (notmuch-tag (notmuch-tree-get-messages-ids-thread-search) tag-changes)
871 (notmuch-tree-thread-mapcar
872 (lambda () (notmuch-tree-tag-update-display tag-changes)))))
874 (defun notmuch-tree-archive-thread (&optional unarchive)
875 "Archive each message in thread.
877 Archive each message currently shown by applying the tag changes
878 in `notmuch-archive-tags' to each. If a prefix argument is given,
879 the messages will be \"unarchived\", i.e. the tag changes in
880 `notmuch-archive-tags' will be reversed.
882 Note: This command is safe from any race condition of new messages
883 being delivered to the same thread. It does not archive the
884 entire thread, but only the messages shown in the current
887 (when notmuch-archive-tags
888 (notmuch-tree-tag-thread
889 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
891 ;;; Functions for displaying the tree buffer itself
893 (defun notmuch-tree-clean-address (address)
894 "Try to clean a single email ADDRESS for display. Return
895 AUTHOR_NAME if present, otherwise return AUTHOR_EMAIL. Return
896 unchanged ADDRESS if parsing fails."
897 (let* ((clean-address (notmuch-clean-address address))
898 (p-address (car clean-address))
899 (p-name (cdr clean-address)))
901 ;; If we have a name return that otherwise return the address.
902 (or p-name p-address)))
904 (defun notmuch-tree-format-field (field format-string msg)
905 "Format a FIELD of MSG according to FORMAT-STRING and return string."
906 (let* ((headers (plist-get msg :headers))
907 (match (plist-get msg :match)))
910 (format format-string (notmuch-tree-format-field-list field msg)))
913 (funcall field format-string msg))
915 ((string-equal field "date")
916 (let ((face (if match
917 'notmuch-tree-match-date-face
918 'notmuch-tree-no-match-date-face)))
919 (propertize (format format-string (plist-get msg :date_relative))
922 ((string-equal field "tree")
923 (let ((tree-status (plist-get msg :tree-status))
925 'notmuch-tree-match-tree-face
926 'notmuch-tree-no-match-tree-face)))
928 (propertize (format format-string
929 (mapconcat #'identity (reverse tree-status) ""))
932 ((string-equal field "subject")
933 (let ((bare-subject (notmuch-show-strip-re (plist-get headers :Subject)))
934 (previous-subject notmuch-tree-previous-subject)
936 'notmuch-tree-match-subject-face
937 'notmuch-tree-no-match-subject-face)))
939 (setq notmuch-tree-previous-subject bare-subject)
940 (propertize (format format-string
941 (if (string= previous-subject bare-subject)
946 ((string-equal field "authors")
947 (let ((author (notmuch-tree-clean-address (plist-get headers :From)))
948 (len (length (format format-string "")))
950 'notmuch-tree-match-author-face
951 'notmuch-tree-no-match-author-face)))
952 (when (> (length author) len)
953 (setq author (substring author 0 len)))
954 (propertize (format format-string author) 'face face)))
956 ((string-equal field "tags")
957 (let ((tags (plist-get msg :tags))
958 (orig-tags (plist-get msg :orig-tags))
960 'notmuch-tree-match-tag-face
961 'notmuch-tree-no-match-tag-face)))
962 (format format-string (notmuch-tag-format-tags tags orig-tags face)))))))
964 (defun notmuch-tree-format-field-list (field-list msg)
965 "Format fields of MSG according to FIELD-LIST and return string."
966 (let ((face (if (plist-get msg :match)
967 'notmuch-tree-match-face
968 'notmuch-tree-no-match-face))
970 (dolist (spec field-list result-string)
971 (let ((field-string (notmuch-tree-format-field (car spec) (cdr spec) msg)))
972 (setq result-string (concat result-string field-string))))
973 (notmuch-apply-face result-string face t)))
975 (defun notmuch-tree-insert-msg (msg)
976 "Insert the message MSG according to notmuch-tree-result-format."
977 ;; We need to save the previous subject as it will get overwritten
978 ;; by the insert-field calls.
979 (let ((previous-subject notmuch-tree-previous-subject))
980 (insert (notmuch-tree-format-field-list (notmuch-tree-result-format) msg))
981 (notmuch-tree-set-message-properties msg)
982 (notmuch-tree-set-prop :previous-subject previous-subject)
985 (defun notmuch-tree-goto-and-insert-msg (msg)
986 "Insert msg at the end of the buffer. Move point to msg if it is the target."
988 (goto-char (point-max))
989 (notmuch-tree-insert-msg msg))
990 (let ((msg-id (notmuch-id-to-query (plist-get msg :id)))
991 (target notmuch-tree-target-msg))
992 (when (or (and (not target) (plist-get msg :match))
993 (string= msg-id target))
994 (setq notmuch-tree-target-msg "found")
995 (goto-char (point-max))
997 (when notmuch-tree-open-target
998 (notmuch-tree-show-message-in)
999 (notmuch-tree-command-hook)))))
1001 (defun notmuch-tree-insert-tree (tree depth tree-status first last)
1002 "Insert the message tree TREE at depth DEPTH in the current thread.
1004 A message tree is another name for a single sub-thread: i.e., a
1005 message together with all its descendents."
1006 (let ((msg (car tree))
1007 (replies (cadr tree)))
1009 ((and (< 0 depth) (not last))
1010 (push (alist-get 'vertical-tee notmuch-tree-thread-symbols) tree-status))
1011 ((and (< 0 depth) last)
1012 (push (alist-get 'bottom notmuch-tree-thread-symbols) tree-status))
1013 ((and (eq 0 depth) first last)
1014 (push (alist-get 'prefix notmuch-tree-thread-symbols) tree-status))
1015 ((and (eq 0 depth) first (not last))
1016 (push (alist-get 'top-tee notmuch-tree-thread-symbols) tree-status))
1017 ((and (eq 0 depth) (not first) last)
1018 (push (alist-get 'bottom notmuch-tree-thread-symbols) tree-status))
1019 ((and (eq 0 depth) (not first) (not last))
1020 (push (alist-get 'vertical-tee notmuch-tree-thread-symbols) tree-status)))
1021 (push (concat (alist-get (if replies 'top-tee 'top) notmuch-tree-thread-symbols)
1022 (alist-get 'arrow notmuch-tree-thread-symbols))
1024 (setq msg (plist-put msg :first (and first (eq 0 depth))))
1025 (setq msg (plist-put msg :tree-status tree-status))
1026 (setq msg (plist-put msg :orig-tags (plist-get msg :tags)))
1027 (notmuch-tree-goto-and-insert-msg msg)
1031 (push " " tree-status)
1032 (push (alist-get 'vertical notmuch-tree-thread-symbols) tree-status))
1033 (notmuch-tree-insert-thread replies (1+ depth) tree-status)))
1035 (defun notmuch-tree-insert-thread (thread depth tree-status)
1036 "Insert the collection of sibling sub-threads THREAD at depth DEPTH in the current forest."
1037 (let ((n (length thread)))
1038 (cl-loop for tree in thread
1039 for count from 1 to n
1040 do (notmuch-tree-insert-tree tree depth tree-status
1044 (defun notmuch-tree-insert-forest-thread (forest-thread)
1045 "Insert a single complete thread."
1046 ;; Reset at the start of each main thread.
1047 (setq notmuch-tree-previous-subject nil)
1048 (notmuch-tree-insert-thread forest-thread 0 nil))
1050 (defun notmuch-tree-insert-forest (forest)
1051 "Insert a forest of threads.
1053 This function inserts a collection of several complete threads as
1054 passed to it by notmuch-tree-process-filter."
1055 (mapc 'notmuch-tree-insert-forest-thread forest))
1057 (define-derived-mode notmuch-tree-mode fundamental-mode "notmuch-tree"
1058 "Major mode displaying messages (as opposed to threads) of a notmuch search.
1060 This buffer contains the results of a \"notmuch tree\" of your
1061 email archives. Each line in the buffer represents a single
1062 message giving the relative date, the author, subject, and any
1065 Pressing \\[notmuch-tree-show-message] on any line displays that message.
1067 Complete list of currently available key bindings:
1069 \\{notmuch-tree-mode-map}"
1070 (setq notmuch-buffer-refresh-function #'notmuch-tree-refresh-view)
1072 (setq buffer-read-only t)
1073 (setq truncate-lines t))
1075 (defun notmuch-tree-process-sentinel (proc _msg)
1076 "Add a message to let user know when \"notmuch tree\" exits."
1077 (let ((buffer (process-buffer proc))
1078 (status (process-status proc))
1079 (exit-status (process-exit-status proc)))
1080 (when (memq status '(exit signal))
1081 (kill-buffer (process-get proc 'parse-buf))
1082 (when (buffer-live-p buffer)
1083 (with-current-buffer buffer
1085 (let ((inhibit-read-only t))
1086 (goto-char (point-max))
1087 (when (eq status 'signal)
1088 (insert "Incomplete search results (tree view process was killed).\n"))
1089 (when (eq status 'exit)
1090 (insert "End of search results.")
1091 (unless (= exit-status 0)
1092 (insert (format " (process returned %d)" exit-status)))
1093 (insert "\n")))))))))
1095 (defun notmuch-tree-process-filter (proc string)
1096 "Process and filter the output of \"notmuch show\" for tree view."
1097 (let ((results-buf (process-buffer proc))
1098 (parse-buf (process-get proc 'parse-buf))
1099 (inhibit-read-only t))
1100 (if (not (buffer-live-p results-buf))
1101 (delete-process proc)
1102 (with-current-buffer parse-buf
1105 (goto-char (point-max))
1107 (notmuch-sexp-parse-partial-list 'notmuch-tree-insert-forest-thread
1110 (defun notmuch-tree-worker (basic-query &optional query-context target
1111 open-target unthreaded oldest-first)
1112 "Insert the tree view of the search in the current buffer.
1114 This is is a helper function for notmuch-tree. The arguments are
1115 the same as for the function notmuch-tree."
1118 (add-hook 'post-command-hook #'notmuch-tree-command-hook t t)
1119 (setq notmuch-search-oldest-first oldest-first)
1120 (setq notmuch-tree-unthreaded unthreaded)
1121 (setq notmuch-tree-basic-query basic-query)
1122 (setq notmuch-tree-query-context (if (or (string= query-context "")
1123 (string= query-context "*"))
1126 (setq notmuch-tree-target-msg target)
1127 (setq notmuch-tree-open-target open-target)
1128 ;; Set the default value for `notmuch-show-process-crypto' in this
1129 ;; buffer. Although we don't use this some of the functions we call
1130 ;; (such as reply) do. It is a buffer local variable so setting it
1131 ;; will not affect genuine show buffers.
1132 (setq notmuch-show-process-crypto notmuch-crypto-process-mime)
1134 (goto-char (point-min))
1135 (let* ((search-args (concat basic-query
1137 (concat " and (" query-context ")"))))
1138 (sort-arg (if oldest-first "--sort=oldest-first" "--sort=newest-first"))
1139 (message-arg (if unthreaded "--unthreaded" "--entire-thread")))
1140 (when (equal (car (notmuch--process-lines notmuch-command "count" search-args)) "0")
1141 (setq search-args basic-query))
1142 (notmuch-tag-clear-cache)
1143 (let ((proc (notmuch-start-notmuch
1144 "notmuch-tree" (current-buffer) #'notmuch-tree-process-sentinel
1145 "show" "--body=false" "--format=sexp" "--format-version=5"
1146 sort-arg message-arg search-args))
1147 ;; Use a scratch buffer to accumulate partial output.
1148 ;; This buffer will be killed by the sentinel, which
1149 ;; should be called no matter how the process dies.
1150 (parse-buf (generate-new-buffer " *notmuch tree parse*")))
1151 (process-put proc 'parse-buf parse-buf)
1152 (set-process-filter proc 'notmuch-tree-process-filter)
1153 (set-process-query-on-exit-flag proc nil))))
1155 (defun notmuch-tree-get-query ()
1156 "Return the current query in this tree buffer."
1157 (if notmuch-tree-query-context
1158 (concat notmuch-tree-basic-query
1160 notmuch-tree-query-context
1162 notmuch-tree-basic-query))
1164 (defun notmuch-tree-toggle-order ()
1165 "Toggle the current search order.
1167 This command toggles the sort order for the current search. The
1168 default sort order is defined by `notmuch-search-oldest-first'."
1170 (setq notmuch-search-oldest-first (not notmuch-search-oldest-first))
1171 (notmuch-tree-refresh-view))
1173 (defun notmuch-tree (&optional query query-context target buffer-name
1174 open-target unthreaded parent-buffer oldest-first)
1175 "Display threads matching QUERY in tree view.
1178 QUERY: the main query. This can be any query but in many cases will be
1179 a single thread. If nil this is read interactively from the minibuffer.
1180 QUERY-CONTEXT: is an additional term for the query. The query used
1181 is QUERY and QUERY-CONTEXT unless that does not match any messages
1182 in which case we fall back to just QUERY.
1183 TARGET: A message ID (with the id: prefix) that will be made
1184 current if it appears in the tree view results.
1185 BUFFER-NAME: the name of the buffer to display the tree view. If
1186 it is nil \"*notmuch-tree\" followed by QUERY is used.
1187 OPEN-TARGET: If TRUE open the target message in the message pane.
1188 UNTHREADED: If TRUE only show matching messages in an unthreaded view."
1191 (setq query (notmuch-read-query (concat "Notmuch "
1192 (if unthreaded "unthreaded " "tree ")
1196 (notmuch-search-buffer-title query
1197 (if unthreaded "unthreaded" "tree"))))
1198 (buffer (get-buffer-create (generate-new-buffer-name name)))
1199 (inhibit-read-only t))
1200 (pop-to-buffer-same-window buffer))
1201 ;; Don't track undo information for this buffer
1202 (setq buffer-undo-list t)
1203 (notmuch-tree-worker query query-context target open-target unthreaded oldest-first)
1204 (setq notmuch-tree-parent-buffer parent-buffer)
1205 (setq truncate-lines t))
1207 (defun notmuch-unthreaded (&optional query query-context target buffer-name
1209 "Display threads matching QUERY in unthreaded view.
1211 See function NOTMUCH-TREE for documentation of the arguments"
1213 (notmuch-tree query query-context target buffer-name open-target t))
1215 (defun notmuch-tree-filter (query)
1216 "Filter or LIMIT the current search results based on an additional query string.
1218 Runs a new tree search matching only messages that match both the
1219 current search results AND the additional query string provided."
1220 (interactive (list (notmuch-read-query "Filter search: ")))
1221 (let ((notmuch-show-process-crypto (notmuch-tree--message-process-crypto))
1222 (grouped-query (notmuch-group-disjunctive-query-string query))
1223 (grouped-original-query (notmuch-group-disjunctive-query-string
1224 (notmuch-tree-get-query))))
1225 (notmuch-tree-close-message-window)
1226 (notmuch-tree (if (string= grouped-original-query "*")
1228 (concat grouped-original-query " and " grouped-query)))))
1230 (defun notmuch-tree-filter-by-tag (tag)
1231 "Filter the current search results based on a single TAG.
1233 Run a new search matching only messages that match the current
1234 search results and that are also tagged with the given TAG."
1236 (list (notmuch-select-tag-with-completion "Filter by tag: "
1237 notmuch-tree-basic-query)))
1238 (let ((notmuch-show-process-crypto (notmuch-tree--message-process-crypto)))
1239 (notmuch-tree-close-message-window)
1240 (notmuch-tree (concat notmuch-tree-basic-query " and tag:" tag)
1241 notmuch-tree-query-context
1245 notmuch-tree-unthreaded
1247 notmuch-search-oldest-first)))
1251 (provide 'notmuch-tree)
1253 ;;; notmuch-tree.el ends here