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-result-format
80 ((("tree" . "%s")("subject" . "%s")) ." %-54s ")
82 "Result formatting for tree view. Supported fields are: date,
83 authors, subject, tree, tags. Tree means the thread tree
84 box graphics. The field may also be a list in which case
85 the formatting rules are applied recursively and then the
86 output of all the fields in the list is inserted
87 according to format-string.
89 Note the author string should not contain
90 whitespace (put it in the neighbouring fields instead).
92 (setq notmuch-tree-result-format \(\(\"authors\" . \"%-40s\"\)
93 \(\"subject\" . \"%s\"\)\)\)"
94 :type '(alist :key-type (string) :value-type (string))
97 (defcustom notmuch-unthreaded-result-format
100 ((("subject" . "%s")) ." %-54s ")
102 "Result formatting for unthreaded tree view. Supported fields are: date,
103 authors, subject, tree, tags. Tree means the thread tree
104 box graphics. The field may also be a list in which case
105 the formatting rules are applied recursively and then the
106 output of all the fields in the list is inserted
107 according to format-string.
109 Note the author string should not contain
110 whitespace (put it in the neighbouring fields instead).
112 (setq notmuch-tree-result-format \(\(\"authors\" . \"%-40s\"\)
113 \(\"subject\" . \"%s\"\)\)\)"
114 :type '(alist :key-type (string) :value-type (string))
115 :group 'notmuch-tree)
117 (defun notmuch-tree-result-format ()
118 (if notmuch-tree-unthreaded
119 notmuch-unthreaded-result-format
120 notmuch-tree-result-format))
123 ;;;; Faces for messages that match the query
125 (defface notmuch-tree-match-face
126 '((t :inherit default))
127 "Default face used in tree mode face for matching messages"
129 :group 'notmuch-faces)
131 (defface notmuch-tree-match-date-face
133 "Face used in tree mode for the date in messages matching the query."
135 :group 'notmuch-faces)
137 (defface notmuch-tree-match-author-face
140 (:foreground "OliveDrab1"))
143 (:foreground "dark blue"))
146 "Face used in tree mode for the date in messages matching the query."
148 :group 'notmuch-faces)
150 (defface notmuch-tree-match-subject-face
152 "Face used in tree mode for the subject in messages matching the query."
154 :group 'notmuch-faces)
156 (defface notmuch-tree-match-tree-face
158 "Face used in tree mode for the thread tree block graphics in messages matching the query."
160 :group 'notmuch-faces)
162 (defface notmuch-tree-match-tag-face
165 (:foreground "OliveDrab1"))
168 (:foreground "navy blue" :bold t))
171 "Face used in tree mode for tags in messages matching the query."
173 :group 'notmuch-faces)
175 ;;;; Faces for messages that do not match the query
177 (defface notmuch-tree-no-match-face
178 '((t (:foreground "gray")))
179 "Default face used in tree mode face for non-matching messages."
181 :group 'notmuch-faces)
183 (defface notmuch-tree-no-match-date-face
185 "Face used in tree mode for non-matching dates."
187 :group 'notmuch-faces)
189 (defface notmuch-tree-no-match-subject-face
191 "Face used in tree mode for non-matching subjects."
193 :group 'notmuch-faces)
195 (defface notmuch-tree-no-match-tree-face
197 "Face used in tree mode for the thread tree block graphics in messages matching the query."
199 :group 'notmuch-faces)
201 (defface notmuch-tree-no-match-author-face
203 "Face used in tree mode for the date in messages matching the query."
205 :group 'notmuch-faces)
207 (defface notmuch-tree-no-match-tag-face
209 "Face used in tree mode face for non-matching tags."
211 :group 'notmuch-faces)
215 (defvar-local notmuch-tree-previous-subject
216 "The subject of the most recent result shown during the async display.")
218 (defvar-local notmuch-tree-basic-query nil
219 "A buffer local copy of argument query to the function notmuch-tree.")
221 (defvar-local notmuch-tree-query-context nil
222 "A buffer local copy of argument query-context to the function notmuch-tree.")
224 (defvar-local notmuch-tree-target-msg nil
225 "A buffer local copy of argument target to the function notmuch-tree.")
227 (defvar-local notmuch-tree-open-target nil
228 "A buffer local copy of argument open-target to the function notmuch-tree.")
230 (defvar-local notmuch-tree-parent-buffer nil)
232 (defvar-local notmuch-tree-message-window nil
233 "The window of the message pane.
235 It is set in both the tree buffer and the child show buffer. It
236 is used to try and close the message pane when quitting tree view
237 or the child show buffer.")
238 (put 'notmuch-tree-message-window 'permanent-local t)
240 (defvar-local notmuch-tree-message-buffer nil
241 "The buffer name of the show buffer in the message pane.
243 This is used to try and make sure we don't close the message pane
244 if the user has loaded a different buffer in that window.")
245 (put 'notmuch-tree-message-buffer 'permanent-local t)
247 ;;; Tree wrapper commands
249 (defmacro notmuch-tree--define-do-in-message-window (name cmd)
250 "Define NAME as a command that calls CMD interactively in the message window.
251 If the message pane is closed then this command does nothing.
252 Avoid using this macro in new code; it will be removed."
254 ,(concat "(In message window) " (documentation cmd t))
256 (when (window-live-p notmuch-tree-message-window)
257 (with-selected-window notmuch-tree-message-window
258 (call-interactively #',cmd)))))
260 (notmuch-tree--define-do-in-message-window
261 notmuch-tree-previous-message-button
262 notmuch-show-previous-button)
263 (notmuch-tree--define-do-in-message-window
264 notmuch-tree-next-message-button
265 notmuch-show-next-button)
266 (notmuch-tree--define-do-in-message-window
267 notmuch-tree-toggle-message-process-crypto
268 notmuch-show-toggle-process-crypto)
270 (defun notmuch-tree--message-process-crypto ()
271 "Return value of `notmuch-show-process-crypto' in the message window.
272 If that window isn't alive, then return the current value.
273 Avoid using this function in new code; it will be removed."
274 (if (window-live-p notmuch-tree-message-window)
275 (with-selected-window notmuch-tree-message-window
276 notmuch-show-process-crypto)
277 notmuch-show-process-crypto))
279 (defmacro notmuch-tree--define-close-message-window-and (name cmd)
280 "Define NAME as a variant of CMD.
282 NAME determines the value of `notmuch-show-process-crypto' in the
283 message window, closes the window, and then call CMD interactively
284 with that value let-bound. If the message window does not exist,
285 then NAME behaves like CMD."
287 ,(concat "(Close message pane and) " (documentation cmd t))
289 (let ((notmuch-show-process-crypto
290 (notmuch-tree--message-process-crypto)))
291 (notmuch-tree-close-message-window)
292 (call-interactively #',cmd))))
294 (notmuch-tree--define-close-message-window-and
297 (notmuch-tree--define-close-message-window-and
298 notmuch-tree-new-mail
299 notmuch-mua-new-mail)
300 (notmuch-tree--define-close-message-window-and
301 notmuch-tree-jump-search
303 (notmuch-tree--define-close-message-window-and
304 notmuch-tree-forward-message
305 notmuch-show-forward-message)
306 (notmuch-tree--define-close-message-window-and
307 notmuch-tree-reply-sender
308 notmuch-show-reply-sender)
309 (notmuch-tree--define-close-message-window-and
312 (notmuch-tree--define-close-message-window-and
313 notmuch-tree-view-raw-message
314 notmuch-show-view-raw-message)
318 (defvar notmuch-tree-mode-map
319 (let ((map (make-sparse-keymap)))
320 (set-keymap-parent map notmuch-common-keymap)
321 ;; These bindings shadow common bindings with variants
322 ;; that additionally close the message window.
323 (define-key map [remap notmuch-bury-or-kill-this-buffer] 'notmuch-tree-quit)
324 (define-key map [remap notmuch-search] 'notmuch-tree-to-search)
325 (define-key map [remap notmuch-help] 'notmuch-tree-help)
326 (define-key map [remap notmuch-mua-new-mail] 'notmuch-tree-new-mail)
327 (define-key map [remap notmuch-jump-search] 'notmuch-tree-jump-search)
329 (define-key map "o" 'notmuch-tree-toggle-order)
330 (define-key map "S" 'notmuch-search-from-tree-current-query)
331 (define-key map "U" 'notmuch-unthreaded-from-tree-current-query)
332 (define-key map "Z" 'notmuch-tree-from-unthreaded-current-query)
334 ;; these use notmuch-show functions directly
335 (define-key map "|" 'notmuch-show-pipe-message)
336 (define-key map "w" 'notmuch-show-save-attachments)
337 (define-key map "v" 'notmuch-show-view-all-mime-parts)
338 (define-key map "c" 'notmuch-show-stash-map)
339 (define-key map "b" 'notmuch-show-resend-message)
341 ;; these apply to the message pane
342 (define-key map (kbd "M-TAB") 'notmuch-tree-previous-message-button)
343 (define-key map (kbd "<backtab>") 'notmuch-tree-previous-message-button)
344 (define-key map (kbd "TAB") 'notmuch-tree-next-message-button)
345 (define-key map "$" 'notmuch-tree-toggle-message-process-crypto)
347 ;; bindings from show (or elsewhere) but we close the message pane first.
348 (define-key map "f" 'notmuch-tree-forward-message)
349 (define-key map "r" 'notmuch-tree-reply-sender)
350 (define-key map "R" 'notmuch-tree-reply)
351 (define-key map "V" 'notmuch-tree-view-raw-message)
353 ;; The main tree view bindings
354 (define-key map (kbd "RET") 'notmuch-tree-show-message)
355 (define-key map [mouse-1] 'notmuch-tree-show-message)
356 (define-key map "x" 'notmuch-tree-archive-message-then-next-or-exit)
357 (define-key map "X" 'notmuch-tree-archive-thread-then-exit)
358 (define-key map "A" 'notmuch-tree-archive-thread-then-next)
359 (define-key map "a" 'notmuch-tree-archive-message-then-next)
360 (define-key map "z" 'notmuch-tree-to-tree)
361 (define-key map "n" 'notmuch-tree-next-matching-message)
362 (define-key map "p" 'notmuch-tree-prev-matching-message)
363 (define-key map "N" 'notmuch-tree-next-message)
364 (define-key map "P" 'notmuch-tree-prev-message)
365 (define-key map (kbd "M-p") 'notmuch-tree-prev-thread)
366 (define-key map (kbd "M-n") 'notmuch-tree-next-thread)
367 (define-key map "k" 'notmuch-tag-jump)
368 (define-key map "-" 'notmuch-tree-remove-tag)
369 (define-key map "+" 'notmuch-tree-add-tag)
370 (define-key map "*" 'notmuch-tree-tag-thread)
371 (define-key map " " 'notmuch-tree-scroll-or-next)
372 (define-key map (kbd "DEL") 'notmuch-tree-scroll-message-window-back)
373 (define-key map "e" 'notmuch-tree-resume-message)
375 "Keymap for \"notmuch tree\" buffers.")
377 ;;; Message properties
379 (defun notmuch-tree-get-message-properties ()
380 "Return the properties of the current message as a plist.
382 Some useful entries are:
383 :headers - Property list containing the headers :Date, :Subject, :From, etc.
384 :tags - Tags for this message."
387 (get-text-property (point) :notmuch-message-properties)))
389 (defun notmuch-tree-set-message-properties (props)
392 (put-text-property (point)
394 :notmuch-message-properties props)))
396 (defun notmuch-tree-set-prop (prop val &optional props)
397 (let ((inhibit-read-only t)
399 (notmuch-tree-get-message-properties))))
400 (plist-put props prop val)
401 (notmuch-tree-set-message-properties props)))
403 (defun notmuch-tree-get-prop (prop &optional props)
404 (plist-get (or props (notmuch-tree-get-message-properties))
407 (defun notmuch-tree-set-tags (tags)
408 "Set the tags of the current message."
409 (notmuch-tree-set-prop :tags tags))
411 (defun notmuch-tree-get-tags ()
412 "Return the tags of the current message."
413 (notmuch-tree-get-prop :tags))
415 (defun notmuch-tree-get-message-id (&optional bare)
416 "Return the message id of the current message."
417 (let ((id (notmuch-tree-get-prop :id)))
421 (notmuch-id-to-query id))
424 (defun notmuch-tree-get-match ()
425 "Return whether the current message is a match."
426 (notmuch-tree-get-prop :match))
430 (defun notmuch-tree-refresh-result ()
431 "Redisplay the current message line.
433 This redisplays the current line based on the messages
434 properties (as they are now). This is used when tags are
436 (let ((init-point (point))
437 (end (line-end-position))
438 (msg (notmuch-tree-get-message-properties))
439 (inhibit-read-only t))
441 ;; This is a little tricky: we override
442 ;; notmuch-tree-previous-subject to get the decision between
443 ;; ... and a subject right and it stops notmuch-tree-insert-msg
444 ;; from overwriting the buffer local copy of
445 ;; notmuch-tree-previous-subject if this is called while the
446 ;; buffer is displaying.
447 (let ((notmuch-tree-previous-subject
448 (notmuch-tree-get-prop :previous-subject)))
449 (delete-region (point) (1+ (line-end-position)))
450 (notmuch-tree-insert-msg msg))
451 (let ((new-end (line-end-position)))
452 (goto-char (if (= init-point end)
454 (min init-point (- new-end 1)))))))
456 (defun notmuch-tree-tag-update-display (&optional tag-changes)
457 "Update display for TAG-CHANGES to current message.
459 Updates the message in the message pane if appropriate, but does
460 NOT change the database."
461 (let* ((current-tags (notmuch-tree-get-tags))
462 (new-tags (notmuch-update-tags current-tags tag-changes))
463 (tree-msg-id (notmuch-tree-get-message-id)))
464 (unless (equal current-tags new-tags)
465 (notmuch-tree-set-tags new-tags)
466 (notmuch-tree-refresh-result)
467 (when (window-live-p notmuch-tree-message-window)
468 (with-selected-window notmuch-tree-message-window
469 (when (string= tree-msg-id (notmuch-show-get-message-id))
470 (notmuch-show-update-tags new-tags)))))))
472 ;;; Commands (and some helper functions used by them)
474 (defun notmuch-tree-tag (tag-changes)
475 "Change tags for the current message."
477 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message")))
478 (notmuch-tag (notmuch-tree-get-message-id) tag-changes)
479 (notmuch-tree-tag-update-display tag-changes))
481 (defun notmuch-tree-add-tag (tag-changes)
482 "Same as `notmuch-tree-tag' but sets initial input to '+'."
484 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "+")))
485 (notmuch-tree-tag tag-changes))
487 (defun notmuch-tree-remove-tag (tag-changes)
488 "Same as `notmuch-tree-tag' but sets initial input to '-'."
490 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "-")))
491 (notmuch-tree-tag tag-changes))
493 (defun notmuch-tree-resume-message ()
494 "Resume EDITING the current draft message."
496 (notmuch-tree-close-message-window)
497 (let ((id (notmuch-tree-get-message-id)))
499 (notmuch-draft-resume id)
500 (message "No message to resume!"))))
502 ;; The next two functions close the message window before calling
503 ;; notmuch-search or notmuch-tree but they do so after the user has
504 ;; entered the query (in case the user was basing the query on
505 ;; something in the message window).
507 (defun notmuch-tree-to-search ()
508 "Run \"notmuch search\" with the given `query' and display results."
510 (let ((query (notmuch-read-query "Notmuch search: ")))
511 (notmuch-tree-close-message-window)
512 (notmuch-search query)))
514 (defun notmuch-tree-to-tree ()
515 "Run a query and display results in tree view."
517 (let ((query (notmuch-read-query "Notmuch tree view search: ")))
518 (notmuch-tree-close-message-window)
519 (notmuch-tree query)))
521 (defun notmuch-tree-archive-thread-then-next ()
522 "Archive all messages in the current buffer, then show next thread from search."
524 (notmuch-tree-archive-thread)
525 (notmuch-tree-next-thread))
527 (defun notmuch-unthreaded-from-tree-current-query ()
528 "Switch from tree view to unthreaded view."
530 (unless notmuch-tree-unthreaded
531 (notmuch-tree-refresh-view 'unthreaded)))
533 (defun notmuch-tree-from-unthreaded-current-query ()
534 "Switch from unthreaded view to tree view."
536 (when notmuch-tree-unthreaded
537 (notmuch-tree-refresh-view 'tree)))
539 (defun notmuch-search-from-tree-current-query ()
540 "Call notmuch search with the current query."
542 (notmuch-tree-close-message-window)
543 (notmuch-search (notmuch-tree-get-query)))
545 (defun notmuch-tree-message-window-kill-hook ()
546 "Close the message pane when exiting the show buffer."
547 (let ((buffer (current-buffer)))
548 (when (and (window-live-p notmuch-tree-message-window)
549 (eq (window-buffer notmuch-tree-message-window) buffer))
550 ;; We could check whether this is the only window in its frame,
551 ;; but simply ignoring the error that is thrown otherwise is
552 ;; what we had to do for Emacs 24 and we stick to that because
553 ;; it is still the simplest approach.
555 (delete-window notmuch-tree-message-window)))))
557 (defun notmuch-tree-command-hook ()
558 (when (eq major-mode 'notmuch-tree-mode)
559 ;; We just run the notmuch-show-command-hook on the message pane.
560 (when (buffer-live-p notmuch-tree-message-buffer)
561 (with-current-buffer notmuch-tree-message-buffer
562 (notmuch-show-command-hook)))))
564 (defun notmuch-tree-show-message-in ()
565 "Show the current message (in split-pane)."
567 (let ((id (notmuch-tree-get-message-id))
568 (inhibit-read-only t)
571 ;; We close and reopen the window to kill off un-needed buffers
572 ;; this might cause flickering but seems ok.
573 (notmuch-tree-close-message-window)
574 (setq notmuch-tree-message-window
575 (split-window-vertically (/ (window-height) 4)))
576 (with-selected-window notmuch-tree-message-window
577 (let (;; Since we are only displaying one message do not indent.
578 (notmuch-show-indent-messages-width 0)
579 (notmuch-show-single-message t)
580 ;; Ensure that `pop-to-buffer-same-window' uses the
581 ;; window we want it to use.
582 (display-buffer-overriding-action
583 '((display-buffer-same-window)
584 (inhibit-same-window . nil))))
585 (setq buffer (notmuch-show id))))
586 ;; We need the `let' as notmuch-tree-message-window is buffer local.
587 (let ((window notmuch-tree-message-window))
588 (with-current-buffer buffer
589 (setq notmuch-tree-message-window window)
590 (add-hook 'kill-buffer-hook 'notmuch-tree-message-window-kill-hook)))
591 (when notmuch-show-mark-read-tags
592 (notmuch-tree-tag-update-display notmuch-show-mark-read-tags))
593 (setq notmuch-tree-message-buffer buffer))))
595 (defun notmuch-tree-show-message-out ()
596 "Show the current message (in whole window)."
598 (let ((id (notmuch-tree-get-message-id))
599 (inhibit-read-only t))
601 ;; We close the window to kill off un-needed buffers.
602 (notmuch-tree-close-message-window)
603 ;; n-s-s-m is buffer local, so use inner let.
604 (let ((notmuch-show-single-message t))
605 (notmuch-show id)))))
607 (defun notmuch-tree-show-message (arg)
608 "Show the current message.
610 Shows in split pane or whole window according to value of
611 `notmuch-tree-show-out'. A prefix argument reverses the choice."
613 (if (or (and (notmuch-tree-show-out) (not arg))
614 (and (not (notmuch-tree-show-out)) arg))
615 (notmuch-tree-show-message-out)
616 (notmuch-tree-show-message-in)))
618 (defun notmuch-tree-scroll-message-window ()
619 "Scroll the message window (if it exists)."
621 (when (window-live-p notmuch-tree-message-window)
622 (with-selected-window notmuch-tree-message-window
623 (if (pos-visible-in-window-p (point-max))
627 (defun notmuch-tree-scroll-message-window-back ()
628 "Scroll the message window back (if it exists)."
630 (when (window-live-p notmuch-tree-message-window)
631 (with-selected-window notmuch-tree-message-window
632 (if (pos-visible-in-window-p (point-min))
636 (defun notmuch-tree-scroll-or-next ()
637 "Scroll the message window.
638 If it at end go to next message."
640 (when (notmuch-tree-scroll-message-window)
641 (notmuch-tree-next-matching-message)))
643 (defun notmuch-tree-quit (&optional kill-both)
644 "Close the split view or exit tree."
646 (when (or (not (notmuch-tree-close-message-window)) kill-both)
647 (kill-buffer (current-buffer))))
649 (defun notmuch-tree-close-message-window ()
650 "Close the message-window. Return t if close succeeds."
652 (when (and (window-live-p notmuch-tree-message-window)
653 (eq (window-buffer notmuch-tree-message-window)
654 notmuch-tree-message-buffer))
655 (delete-window notmuch-tree-message-window)
656 (unless (get-buffer-window-list notmuch-tree-message-buffer)
657 (kill-buffer notmuch-tree-message-buffer))
660 (defun notmuch-tree-archive-message (&optional unarchive)
661 "Archive the current message.
663 Archive the current message by applying the tag changes in
664 `notmuch-archive-tags' to it. If a prefix argument is given, the
665 message will be \"unarchived\", i.e. the tag changes in
666 `notmuch-archive-tags' will be reversed."
668 (when notmuch-archive-tags
670 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
672 (defun notmuch-tree-archive-message-then-next (&optional unarchive)
673 "Archive the current message and move to next matching message."
675 (notmuch-tree-archive-message unarchive)
676 (notmuch-tree-next-matching-message))
678 (defun notmuch-tree-archive-thread-then-exit ()
679 "Archive all messages in the current buffer, then exit notmuch-tree."
681 (notmuch-tree-archive-thread)
682 (notmuch-tree-quit t))
684 (defun notmuch-tree-archive-message-then-next-or-exit ()
685 "Archive current message, then show next open message in current thread.
687 If at the last open message in the current thread, then exit back
690 (notmuch-tree-archive-message)
691 (notmuch-tree-next-matching-message t))
693 (defun notmuch-tree-next-message ()
694 "Move to next message."
697 (when (window-live-p notmuch-tree-message-window)
698 (notmuch-tree-show-message-in)))
700 (defun notmuch-tree-prev-message ()
701 "Move to previous message."
704 (when (window-live-p notmuch-tree-message-window)
705 (notmuch-tree-show-message-in)))
707 (defun notmuch-tree-goto-matching-message (&optional prev)
708 "Move to the next or previous matching message.
710 Returns t if there was a next matching message in the thread to show,
712 (let ((dir (if prev -1 nil))
713 (eobfn (if prev #'bobp #'eobp)))
714 (while (and (not (funcall eobfn))
715 (not (notmuch-tree-get-match)))
717 (not (funcall eobfn))))
719 (defun notmuch-tree-matching-message (&optional prev pop-at-end)
720 "Move to the next or previous matching message."
722 (forward-line (if prev -1 nil))
723 (if (and (not (notmuch-tree-goto-matching-message prev)) pop-at-end)
724 (notmuch-tree-quit pop-at-end)
725 (when (window-live-p notmuch-tree-message-window)
726 (notmuch-tree-show-message-in))))
728 (defun notmuch-tree-prev-matching-message (&optional pop-at-end)
729 "Move to previous matching message."
731 (notmuch-tree-matching-message t pop-at-end))
733 (defun notmuch-tree-next-matching-message (&optional pop-at-end)
734 "Move to next matching message."
736 (notmuch-tree-matching-message nil pop-at-end))
738 (defun notmuch-tree-refresh-view (&optional view)
741 (when (get-buffer-process (current-buffer))
742 (error "notmuch tree process already running for current buffer"))
743 (let ((inhibit-read-only t)
744 (basic-query notmuch-tree-basic-query)
745 (unthreaded (cond ((eq view 'unthreaded) t)
746 ((eq view 'tree) nil)
747 (t notmuch-tree-unthreaded)))
748 (query-context notmuch-tree-query-context)
749 (target (notmuch-tree-get-message-id)))
751 (notmuch-tree-worker basic-query
756 notmuch-search-oldest-first)))
758 (defun notmuch-tree-thread-top ()
759 (when (notmuch-tree-get-message-properties)
760 (while (not (or (notmuch-tree-get-prop :first) (eobp)))
763 (defun notmuch-tree-prev-thread-in-tree ()
764 "Move to the previous thread in the current tree"
767 (notmuch-tree-thread-top)
770 (defun notmuch-tree-next-thread-in-tree ()
771 "Get the next thread in the current tree. Returns t if a thread was
772 found or nil if not."
775 (while (not (or (notmuch-tree-get-prop :first) (eobp)))
779 (defun notmuch-tree-next-thread-from-search (&optional previous)
780 "Move to the next thread in the parent search results, if any.
782 If PREVIOUS is non-nil, move to the previous item in the
783 search results instead."
785 (let ((parent-buffer notmuch-tree-parent-buffer))
786 (notmuch-tree-quit t)
787 (when (buffer-live-p parent-buffer)
788 (switch-to-buffer parent-buffer)
790 (notmuch-search-previous-thread)
791 (notmuch-search-next-thread))
792 (notmuch-tree-from-search-thread))))
794 (defun notmuch-tree-next-thread (&optional previous)
795 "Move to the next thread in the current tree or parent search results.
797 If PREVIOUS is non-nil, move to the previous thread in the tree or
798 search results instead."
800 (unless (if previous (notmuch-tree-prev-thread-in-tree)
801 (notmuch-tree-next-thread-in-tree))
802 (notmuch-tree-next-thread-from-search previous)))
804 (defun notmuch-tree-prev-thread ()
805 "Move to the previous thread in the current tree or parent search results."
807 (notmuch-tree-next-thread t))
809 (defun notmuch-tree-thread-mapcar (function)
810 "Call FUNCTION for each message in the current thread.
811 FUNCTION is called for side effects only."
813 (notmuch-tree-thread-top)
814 (cl-loop collect (funcall function)
816 while (and (notmuch-tree-get-message-properties)
817 (not (notmuch-tree-get-prop :first))))))
819 (defun notmuch-tree-get-messages-ids-thread-search ()
820 "Return a search string for all message ids of messages in the current thread."
822 (notmuch-tree-thread-mapcar 'notmuch-tree-get-message-id)
825 (defun notmuch-tree-tag-thread (tag-changes)
826 "Tag all messages in the current thread."
828 (let ((tags (apply #'append (notmuch-tree-thread-mapcar
829 (lambda () (notmuch-tree-get-tags))))))
830 (list (notmuch-read-tag-changes tags "Tag thread"))))
831 (when (notmuch-tree-get-message-properties)
832 (notmuch-tag (notmuch-tree-get-messages-ids-thread-search) tag-changes)
833 (notmuch-tree-thread-mapcar
834 (lambda () (notmuch-tree-tag-update-display tag-changes)))))
836 (defun notmuch-tree-archive-thread (&optional unarchive)
837 "Archive each message in thread.
839 Archive each message currently shown by applying the tag changes
840 in `notmuch-archive-tags' to each. If a prefix argument is given,
841 the messages will be \"unarchived\", i.e. the tag changes in
842 `notmuch-archive-tags' will be reversed.
844 Note: This command is safe from any race condition of new messages
845 being delivered to the same thread. It does not archive the
846 entire thread, but only the messages shown in the current
849 (when notmuch-archive-tags
850 (notmuch-tree-tag-thread
851 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
853 ;;; Functions for displaying the tree buffer itself
855 (defun notmuch-tree-clean-address (address)
856 "Try to clean a single email ADDRESS for display. Return
857 AUTHOR_NAME if present, otherwise return AUTHOR_EMAIL. Return
858 unchanged ADDRESS if parsing fails."
859 (let* ((clean-address (notmuch-clean-address address))
860 (p-address (car clean-address))
861 (p-name (cdr clean-address)))
863 ;; If we have a name return that otherwise return the address.
864 (or p-name p-address)))
866 (defun notmuch-tree-format-field (field format-string msg)
867 "Format a FIELD of MSG according to FORMAT-STRING and return string."
868 (let* ((headers (plist-get msg :headers))
869 (match (plist-get msg :match)))
872 (format format-string (notmuch-tree-format-field-list field msg)))
874 ((string-equal field "date")
875 (let ((face (if match
876 'notmuch-tree-match-date-face
877 'notmuch-tree-no-match-date-face)))
878 (propertize (format format-string (plist-get msg :date_relative))
881 ((string-equal field "tree")
882 (let ((tree-status (plist-get msg :tree-status))
884 'notmuch-tree-match-tree-face
885 'notmuch-tree-no-match-tree-face)))
887 (propertize (format format-string
888 (mapconcat #'identity (reverse tree-status) ""))
891 ((string-equal field "subject")
892 (let ((bare-subject (notmuch-show-strip-re (plist-get headers :Subject)))
893 (previous-subject notmuch-tree-previous-subject)
895 'notmuch-tree-match-subject-face
896 'notmuch-tree-no-match-subject-face)))
898 (setq notmuch-tree-previous-subject bare-subject)
899 (propertize (format format-string
900 (if (string= previous-subject bare-subject)
905 ((string-equal field "authors")
906 (let ((author (notmuch-tree-clean-address (plist-get headers :From)))
907 (len (length (format format-string "")))
909 'notmuch-tree-match-author-face
910 'notmuch-tree-no-match-author-face)))
911 (when (> (length author) len)
912 (setq author (substring author 0 len)))
913 (propertize (format format-string author) 'face face)))
915 ((string-equal field "tags")
916 (let ((tags (plist-get msg :tags))
917 (orig-tags (plist-get msg :orig-tags))
919 'notmuch-tree-match-tag-face
920 'notmuch-tree-no-match-tag-face)))
921 (format format-string (notmuch-tag-format-tags tags orig-tags face)))))))
923 (defun notmuch-tree-format-field-list (field-list msg)
924 "Format fields of MSG according to FIELD-LIST and return string."
925 (let ((face (if (plist-get msg :match)
926 'notmuch-tree-match-face
927 'notmuch-tree-no-match-face))
929 (dolist (spec field-list result-string)
930 (let ((field-string (notmuch-tree-format-field (car spec) (cdr spec) msg)))
931 (setq result-string (concat result-string field-string))))
932 (notmuch-apply-face result-string face t)))
934 (defun notmuch-tree-insert-msg (msg)
935 "Insert the message MSG according to notmuch-tree-result-format."
936 ;; We need to save the previous subject as it will get overwritten
937 ;; by the insert-field calls.
938 (let ((previous-subject notmuch-tree-previous-subject))
939 (insert (notmuch-tree-format-field-list (notmuch-tree-result-format) msg))
940 (notmuch-tree-set-message-properties msg)
941 (notmuch-tree-set-prop :previous-subject previous-subject)
944 (defun notmuch-tree-goto-and-insert-msg (msg)
945 "Insert msg at the end of the buffer. Move point to msg if it is the target."
947 (goto-char (point-max))
948 (notmuch-tree-insert-msg msg))
949 (let ((msg-id (notmuch-id-to-query (plist-get msg :id)))
950 (target notmuch-tree-target-msg))
951 (when (or (and (not target) (plist-get msg :match))
952 (string= msg-id target))
953 (setq notmuch-tree-target-msg "found")
954 (goto-char (point-max))
956 (when notmuch-tree-open-target
957 (notmuch-tree-show-message-in)
958 (notmuch-tree-command-hook)))))
960 (defun notmuch-tree-insert-tree (tree depth tree-status first last)
961 "Insert the message tree TREE at depth DEPTH in the current thread.
963 A message tree is another name for a single sub-thread: i.e., a
964 message together with all its descendents."
965 (let ((msg (car tree))
966 (replies (cadr tree)))
968 ((and (< 0 depth) (not last))
969 (push "├" tree-status))
970 ((and (< 0 depth) last)
971 (push "╰" tree-status))
972 ((and (eq 0 depth) first last)
973 ;; Choice between these two variants is a matter of taste.
974 ;; (push "─" tree-status))
975 (push " " tree-status))
976 ((and (eq 0 depth) first (not last))
977 (push "┬" tree-status))
978 ((and (eq 0 depth) (not first) last)
979 (push "╰" tree-status))
980 ((and (eq 0 depth) (not first) (not last))
981 (push "├" tree-status)))
982 (push (concat (if replies "┬" "─") "►") tree-status)
983 (setq msg (plist-put msg :first (and first (eq 0 depth))))
984 (setq msg (plist-put msg :tree-status tree-status))
985 (setq msg (plist-put msg :orig-tags (plist-get msg :tags)))
986 (notmuch-tree-goto-and-insert-msg msg)
990 (push " " tree-status)
991 (push "│" tree-status))
992 (notmuch-tree-insert-thread replies (1+ depth) tree-status)))
994 (defun notmuch-tree-insert-thread (thread depth tree-status)
995 "Insert the collection of sibling sub-threads THREAD at depth DEPTH in the current forest."
996 (let ((n (length thread)))
997 (cl-loop for tree in thread
998 for count from 1 to n
999 do (notmuch-tree-insert-tree tree depth tree-status
1003 (defun notmuch-tree-insert-forest-thread (forest-thread)
1004 "Insert a single complete thread."
1005 ;; Reset at the start of each main thread.
1006 (setq notmuch-tree-previous-subject nil)
1007 (notmuch-tree-insert-thread forest-thread 0 nil))
1009 (defun notmuch-tree-insert-forest (forest)
1010 "Insert a forest of threads.
1012 This function inserts a collection of several complete threads as
1013 passed to it by notmuch-tree-process-filter."
1014 (mapc 'notmuch-tree-insert-forest-thread forest))
1016 (define-derived-mode notmuch-tree-mode fundamental-mode "notmuch-tree"
1017 "Major mode displaying messages (as opposed to threads) of a notmuch search.
1019 This buffer contains the results of a \"notmuch tree\" of your
1020 email archives. Each line in the buffer represents a single
1021 message giving the relative date, the author, subject, and any
1024 Pressing \\[notmuch-tree-show-message] on any line displays that message.
1026 Complete list of currently available key bindings:
1028 \\{notmuch-tree-mode-map}"
1029 (setq notmuch-buffer-refresh-function #'notmuch-tree-refresh-view)
1031 (setq buffer-read-only t)
1032 (setq truncate-lines t))
1034 (defun notmuch-tree-process-sentinel (proc _msg)
1035 "Add a message to let user know when \"notmuch tree\" exits."
1036 (let ((buffer (process-buffer proc))
1037 (status (process-status proc))
1038 (exit-status (process-exit-status proc)))
1039 (when (memq status '(exit signal))
1040 (kill-buffer (process-get proc 'parse-buf))
1041 (when (buffer-live-p buffer)
1042 (with-current-buffer buffer
1044 (let ((inhibit-read-only t))
1045 (goto-char (point-max))
1046 (when (eq status 'signal)
1047 (insert "Incomplete search results (tree view process was killed).\n"))
1048 (when (eq status 'exit)
1049 (insert "End of search results.")
1050 (unless (= exit-status 0)
1051 (insert (format " (process returned %d)" exit-status)))
1052 (insert "\n")))))))))
1054 (defun notmuch-tree-process-filter (proc string)
1055 "Process and filter the output of \"notmuch show\" for tree view."
1056 (let ((results-buf (process-buffer proc))
1057 (parse-buf (process-get proc 'parse-buf))
1058 (inhibit-read-only t))
1059 (if (not (buffer-live-p results-buf))
1060 (delete-process proc)
1061 (with-current-buffer parse-buf
1064 (goto-char (point-max))
1066 (notmuch-sexp-parse-partial-list 'notmuch-tree-insert-forest-thread
1069 (defun notmuch-tree-worker (basic-query &optional query-context target
1070 open-target unthreaded oldest-first)
1071 "Insert the tree view of the search in the current buffer.
1073 This is is a helper function for notmuch-tree. The arguments are
1074 the same as for the function notmuch-tree."
1077 (add-hook 'post-command-hook #'notmuch-tree-command-hook t t)
1078 (setq notmuch-search-oldest-first oldest-first)
1079 (setq notmuch-tree-unthreaded unthreaded)
1080 (setq notmuch-tree-basic-query basic-query)
1081 (setq notmuch-tree-query-context (if (or (string= query-context "")
1082 (string= query-context "*"))
1085 (setq notmuch-tree-target-msg target)
1086 (setq notmuch-tree-open-target open-target)
1087 ;; Set the default value for `notmuch-show-process-crypto' in this
1088 ;; buffer. Although we don't use this some of the functions we call
1089 ;; (such as reply) do. It is a buffer local variable so setting it
1090 ;; will not affect genuine show buffers.
1091 (setq notmuch-show-process-crypto notmuch-crypto-process-mime)
1093 (goto-char (point-min))
1094 (let* ((search-args (concat basic-query
1096 (concat " and (" query-context ")"))))
1097 (sort-arg (if oldest-first "--sort=oldest-first" "--sort=newest-first"))
1098 (message-arg (if unthreaded "--unthreaded" "--entire-thread")))
1099 (when (equal (car (process-lines notmuch-command "count" search-args)) "0")
1100 (setq search-args basic-query))
1101 (notmuch-tag-clear-cache)
1102 (let ((proc (notmuch-start-notmuch
1103 "notmuch-tree" (current-buffer) #'notmuch-tree-process-sentinel
1104 "show" "--body=false" "--format=sexp" "--format-version=4"
1105 sort-arg message-arg search-args))
1106 ;; Use a scratch buffer to accumulate partial output.
1107 ;; This buffer will be killed by the sentinel, which
1108 ;; should be called no matter how the process dies.
1109 (parse-buf (generate-new-buffer " *notmuch tree parse*")))
1110 (process-put proc 'parse-buf parse-buf)
1111 (set-process-filter proc 'notmuch-tree-process-filter)
1112 (set-process-query-on-exit-flag proc nil))))
1114 (defun notmuch-tree-get-query ()
1115 "Return the current query in this tree buffer."
1116 (if notmuch-tree-query-context
1117 (concat notmuch-tree-basic-query
1119 notmuch-tree-query-context
1121 notmuch-tree-basic-query))
1123 (defun notmuch-tree-toggle-order ()
1124 "Toggle the current search order.
1126 This command toggles the sort order for the current search. The
1127 default sort order is defined by `notmuch-search-oldest-first'."
1129 (setq notmuch-search-oldest-first (not notmuch-search-oldest-first))
1130 (notmuch-tree-refresh-view))
1132 (defun notmuch-tree (&optional query query-context target buffer-name
1133 open-target unthreaded parent-buffer oldest-first)
1134 "Display threads matching QUERY in tree view.
1137 QUERY: the main query. This can be any query but in many cases will be
1138 a single thread. If nil this is read interactively from the minibuffer.
1139 QUERY-CONTEXT: is an additional term for the query. The query used
1140 is QUERY and QUERY-CONTEXT unless that does not match any messages
1141 in which case we fall back to just QUERY.
1142 TARGET: A message ID (with the id: prefix) that will be made
1143 current if it appears in the tree view results.
1144 BUFFER-NAME: the name of the buffer to display the tree view. If
1145 it is nil \"*notmuch-tree\" followed by QUERY is used.
1146 OPEN-TARGET: If TRUE open the target message in the message pane.
1147 UNTHREADED: If TRUE only show matching messages in an unthreaded view."
1150 (setq query (notmuch-read-query (concat "Notmuch "
1151 (if unthreaded "unthreaded " "tree ")
1153 (let ((buffer (get-buffer-create (generate-new-buffer-name
1156 (if unthreaded "unthreaded-" "tree-")
1158 (inhibit-read-only t))
1159 (pop-to-buffer-same-window buffer))
1160 ;; Don't track undo information for this buffer
1161 (setq buffer-undo-list t)
1162 (notmuch-tree-worker query query-context target open-target unthreaded oldest-first)
1163 (setq notmuch-tree-parent-buffer parent-buffer)
1164 (setq truncate-lines t))
1166 (defun notmuch-unthreaded (&optional query query-context target buffer-name
1169 (notmuch-tree query query-context target buffer-name open-target t))
1173 (provide 'notmuch-tree)
1175 ;;; notmuch-tree.el ends here