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))
38 (declare-function notmuch-call-notmuch-process "notmuch" (&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-result-format
79 ((("tree" . "%s")("subject" . "%s")) ." %-54s ")
81 "Result formatting for tree view. Supported fields are: date,
82 authors, subject, tree, tags. Tree means the thread tree
83 box graphics. The field may also be a list in which case
84 the formatting rules are applied recursively and then the
85 output of all the fields in the list is inserted
86 according to format-string.
88 Note the author string should not contain
89 whitespace (put it in the neighbouring fields instead).
91 (setq notmuch-tree-result-format \(\(\"authors\" . \"%-40s\"\)
92 \(\"subject\" . \"%s\"\)\)\)"
93 :type '(alist :key-type (string) :value-type (string))
96 (defcustom notmuch-unthreaded-result-format
99 ((("subject" . "%s")) ." %-54s ")
101 "Result formatting for unthreaded tree view. Supported fields are: date,
102 authors, subject, tree, tags. Tree means the thread tree
103 box graphics. The field may also be a list in which case
104 the formatting rules are applied recursively and then the
105 output of all the fields in the list is inserted
106 according to format-string.
108 Note the author string should not contain
109 whitespace (put it in the neighbouring fields instead).
111 (setq notmuch-tree-result-format \(\(\"authors\" . \"%-40s\"\)
112 \(\"subject\" . \"%s\"\)\)\)"
113 :type '(alist :key-type (string) :value-type (string))
114 :group 'notmuch-tree)
116 (defun notmuch-tree-result-format ()
117 (if notmuch-tree-unthreaded
118 notmuch-unthreaded-result-format
119 notmuch-tree-result-format))
122 ;;;; Faces for messages that match the query
124 (defface notmuch-tree-match-face
125 '((t :inherit default))
126 "Default face used in tree mode face for matching messages"
128 :group 'notmuch-faces)
130 (defface notmuch-tree-match-date-face
132 "Face used in tree mode for the date in messages matching the query."
134 :group 'notmuch-faces)
136 (defface notmuch-tree-match-author-face
139 (:foreground "OliveDrab1"))
142 (:foreground "dark blue"))
145 "Face used in tree mode for the date in messages matching the query."
147 :group 'notmuch-faces)
149 (defface notmuch-tree-match-subject-face
151 "Face used in tree mode for the subject in messages matching the query."
153 :group 'notmuch-faces)
155 (defface notmuch-tree-match-tree-face
157 "Face used in tree mode for the thread tree block graphics in messages matching the query."
159 :group 'notmuch-faces)
161 (defface notmuch-tree-match-tag-face
164 (:foreground "OliveDrab1"))
167 (:foreground "navy blue" :bold t))
170 "Face used in tree mode for tags in messages matching the query."
172 :group 'notmuch-faces)
174 ;;;; Faces for messages that do not match the query
176 (defface notmuch-tree-no-match-face
177 '((t (:foreground "gray")))
178 "Default face used in tree mode face for non-matching messages."
180 :group 'notmuch-faces)
182 (defface notmuch-tree-no-match-date-face
184 "Face used in tree mode for non-matching dates."
186 :group 'notmuch-faces)
188 (defface notmuch-tree-no-match-subject-face
190 "Face used in tree mode for non-matching subjects."
192 :group 'notmuch-faces)
194 (defface notmuch-tree-no-match-tree-face
196 "Face used in tree mode for the thread tree block graphics in messages matching the query."
198 :group 'notmuch-faces)
200 (defface notmuch-tree-no-match-author-face
202 "Face used in tree mode for the date in messages matching the query."
204 :group 'notmuch-faces)
206 (defface notmuch-tree-no-match-tag-face
208 "Face used in tree mode face for non-matching tags."
210 :group 'notmuch-faces)
214 (defvar-local notmuch-tree-previous-subject
215 "The subject of the most recent result shown during the async display.")
217 (defvar-local notmuch-tree-basic-query nil
218 "A buffer local copy of argument query to the function notmuch-tree.")
220 (defvar-local notmuch-tree-query-context nil
221 "A buffer local copy of argument query-context to the function notmuch-tree.")
223 (defvar-local notmuch-tree-target-msg nil
224 "A buffer local copy of argument target to the function notmuch-tree.")
226 (defvar-local notmuch-tree-open-target nil
227 "A buffer local copy of argument open-target to the function notmuch-tree.")
229 (defvar-local notmuch-tree-parent-buffer nil)
231 (defvar-local notmuch-tree-message-window nil
232 "The window of the message pane.
234 It is set in both the tree buffer and the child show buffer. It
235 is used to try and close the message pane when quitting tree view
236 or the child show buffer.")
237 (put 'notmuch-tree-message-window 'permanent-local t)
239 (defvar-local notmuch-tree-message-buffer nil
240 "The buffer name of the show buffer in the message pane.
242 This is used to try and make sure we don't close the message pane
243 if the user has loaded a different buffer in that window.")
244 (put 'notmuch-tree-message-buffer 'permanent-local t)
246 ;;; Tree wrapper commands
248 (defmacro notmuch-tree--define-do-in-message-window (name cmd)
249 "Define NAME as a command that calls CMD interactively in the message window.
250 If the message pane is closed then this command does nothing.
251 Avoid using this macro in new code; it will be removed."
253 ,(concat "(In message window) " (documentation cmd t))
255 (when (window-live-p notmuch-tree-message-window)
256 (with-selected-window notmuch-tree-message-window
257 (call-interactively #',cmd)))))
259 (notmuch-tree--define-do-in-message-window
260 notmuch-tree-previous-message-button
261 notmuch-show-previous-button)
262 (notmuch-tree--define-do-in-message-window
263 notmuch-tree-next-message-button
264 notmuch-show-next-button)
265 (notmuch-tree--define-do-in-message-window
266 notmuch-tree-toggle-message-process-crypto
267 notmuch-show-toggle-process-crypto)
269 (defun notmuch-tree--message-process-crypto ()
270 "Return value of `notmuch-show-process-crypto' in the message window.
271 If that window isn't alive, then return the current value.
272 Avoid using this function in new code; it will be removed."
273 (if (window-live-p notmuch-tree-message-window)
274 (with-selected-window notmuch-tree-message-window
275 notmuch-show-process-crypto)
276 notmuch-show-process-crypto))
278 (defmacro notmuch-tree--define-close-message-window-and (name cmd)
279 "Define NAME as a variant of CMD.
281 NAME determines the value of `notmuch-show-process-crypto' in the
282 message window, closes the window, and then call CMD interactively
283 with that value let-bound. If the message window does not exist,
284 then NAME behaves like CMD."
286 ,(concat "(Close message pane and) " (documentation cmd t))
288 (let ((notmuch-show-process-crypto
289 (notmuch-tree--message-process-crypto)))
290 (notmuch-tree-close-message-window)
291 (call-interactively #',cmd))))
293 (notmuch-tree--define-close-message-window-and
296 (notmuch-tree--define-close-message-window-and
297 notmuch-tree-new-mail
298 notmuch-mua-new-mail)
299 (notmuch-tree--define-close-message-window-and
300 notmuch-tree-jump-search
302 (notmuch-tree--define-close-message-window-and
303 notmuch-tree-forward-message
304 notmuch-show-forward-message)
305 (notmuch-tree--define-close-message-window-and
306 notmuch-tree-reply-sender
307 notmuch-show-reply-sender)
308 (notmuch-tree--define-close-message-window-and
311 (notmuch-tree--define-close-message-window-and
312 notmuch-tree-view-raw-message
313 notmuch-show-view-raw-message)
317 (defvar notmuch-tree-mode-map
318 (let ((map (make-sparse-keymap)))
319 (set-keymap-parent map notmuch-common-keymap)
320 ;; These bindings shadow common bindings with variants
321 ;; that additionally close the message window.
322 (define-key map [remap notmuch-bury-or-kill-this-buffer] 'notmuch-tree-quit)
323 (define-key map [remap notmuch-search] 'notmuch-tree-to-search)
324 (define-key map [remap notmuch-help] 'notmuch-tree-help)
325 (define-key map [remap notmuch-mua-new-mail] 'notmuch-tree-new-mail)
326 (define-key map [remap notmuch-jump-search] 'notmuch-tree-jump-search)
328 (define-key map "S" 'notmuch-search-from-tree-current-query)
329 (define-key map "U" 'notmuch-unthreaded-from-tree-current-query)
330 (define-key map "Z" 'notmuch-tree-from-unthreaded-current-query)
332 ;; these use notmuch-show functions directly
333 (define-key map "|" 'notmuch-show-pipe-message)
334 (define-key map "w" 'notmuch-show-save-attachments)
335 (define-key map "v" 'notmuch-show-view-all-mime-parts)
336 (define-key map "c" 'notmuch-show-stash-map)
337 (define-key map "b" 'notmuch-show-resend-message)
339 ;; these apply to the message pane
340 (define-key map (kbd "M-TAB") 'notmuch-tree-previous-message-button)
341 (define-key map (kbd "<backtab>") 'notmuch-tree-previous-message-button)
342 (define-key map (kbd "TAB") 'notmuch-tree-next-message-button)
343 (define-key map "$" 'notmuch-tree-toggle-message-process-crypto)
345 ;; bindings from show (or elsewhere) but we close the message pane first.
346 (define-key map "f" 'notmuch-tree-forward-message)
347 (define-key map "r" 'notmuch-tree-reply-sender)
348 (define-key map "R" 'notmuch-tree-reply)
349 (define-key map "V" 'notmuch-tree-view-raw-message)
351 ;; The main tree view bindings
352 (define-key map (kbd "RET") 'notmuch-tree-show-message)
353 (define-key map [mouse-1] 'notmuch-tree-show-message)
354 (define-key map "x" 'notmuch-tree-archive-message-then-next-or-exit)
355 (define-key map "X" 'notmuch-tree-archive-thread-then-exit)
356 (define-key map "A" 'notmuch-tree-archive-thread-then-next)
357 (define-key map "a" 'notmuch-tree-archive-message-then-next)
358 (define-key map "z" 'notmuch-tree-to-tree)
359 (define-key map "n" 'notmuch-tree-next-matching-message)
360 (define-key map "p" 'notmuch-tree-prev-matching-message)
361 (define-key map "N" 'notmuch-tree-next-message)
362 (define-key map "P" 'notmuch-tree-prev-message)
363 (define-key map (kbd "M-p") 'notmuch-tree-prev-thread)
364 (define-key map (kbd "M-n") 'notmuch-tree-next-thread)
365 (define-key map "k" 'notmuch-tag-jump)
366 (define-key map "-" 'notmuch-tree-remove-tag)
367 (define-key map "+" 'notmuch-tree-add-tag)
368 (define-key map "*" 'notmuch-tree-tag-thread)
369 (define-key map " " 'notmuch-tree-scroll-or-next)
370 (define-key map (kbd "DEL") 'notmuch-tree-scroll-message-window-back)
371 (define-key map "e" 'notmuch-tree-resume-message)
373 "Keymap for \"notmuch tree\" buffers.")
375 ;;; Message properties
377 (defun notmuch-tree-get-message-properties ()
378 "Return the properties of the current message as a plist.
380 Some useful entries are:
381 :headers - Property list containing the headers :Date, :Subject, :From, etc.
382 :tags - Tags for this message."
385 (get-text-property (point) :notmuch-message-properties)))
387 (defun notmuch-tree-set-message-properties (props)
390 (put-text-property (point)
392 :notmuch-message-properties props)))
394 (defun notmuch-tree-set-prop (prop val &optional props)
395 (let ((inhibit-read-only t)
397 (notmuch-tree-get-message-properties))))
398 (plist-put props prop val)
399 (notmuch-tree-set-message-properties props)))
401 (defun notmuch-tree-get-prop (prop &optional props)
402 (plist-get (or props (notmuch-tree-get-message-properties))
405 (defun notmuch-tree-set-tags (tags)
406 "Set the tags of the current message."
407 (notmuch-tree-set-prop :tags tags))
409 (defun notmuch-tree-get-tags ()
410 "Return the tags of the current message."
411 (notmuch-tree-get-prop :tags))
413 (defun notmuch-tree-get-message-id (&optional bare)
414 "Return the message id of the current message."
415 (let ((id (notmuch-tree-get-prop :id)))
419 (notmuch-id-to-query id))
422 (defun notmuch-tree-get-match ()
423 "Return whether the current message is a match."
424 (notmuch-tree-get-prop :match))
428 (defun notmuch-tree-refresh-result ()
429 "Redisplay the current message line.
431 This redisplays the current line based on the messages
432 properties (as they are now). This is used when tags are
434 (let ((init-point (point))
435 (end (line-end-position))
436 (msg (notmuch-tree-get-message-properties))
437 (inhibit-read-only t))
439 ;; This is a little tricky: we override
440 ;; notmuch-tree-previous-subject to get the decision between
441 ;; ... and a subject right and it stops notmuch-tree-insert-msg
442 ;; from overwriting the buffer local copy of
443 ;; notmuch-tree-previous-subject if this is called while the
444 ;; buffer is displaying.
445 (let ((notmuch-tree-previous-subject
446 (notmuch-tree-get-prop :previous-subject)))
447 (delete-region (point) (1+ (line-end-position)))
448 (notmuch-tree-insert-msg msg))
449 (let ((new-end (line-end-position)))
450 (goto-char (if (= init-point end)
452 (min init-point (- new-end 1)))))))
454 (defun notmuch-tree-tag-update-display (&optional tag-changes)
455 "Update display for TAG-CHANGES to current message.
457 Updates the message in the message pane if appropriate, but does
458 NOT change the database."
459 (let* ((current-tags (notmuch-tree-get-tags))
460 (new-tags (notmuch-update-tags current-tags tag-changes))
461 (tree-msg-id (notmuch-tree-get-message-id)))
462 (unless (equal current-tags new-tags)
463 (notmuch-tree-set-tags new-tags)
464 (notmuch-tree-refresh-result)
465 (when (window-live-p notmuch-tree-message-window)
466 (with-selected-window notmuch-tree-message-window
467 (when (string= tree-msg-id (notmuch-show-get-message-id))
468 (notmuch-show-update-tags new-tags)))))))
470 ;;; Commands (and some helper functions used by them)
472 (defun notmuch-tree-tag (tag-changes)
473 "Change tags for the current message."
475 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message")))
476 (notmuch-tag (notmuch-tree-get-message-id) tag-changes)
477 (notmuch-tree-tag-update-display tag-changes))
479 (defun notmuch-tree-add-tag (tag-changes)
480 "Same as `notmuch-tree-tag' but sets initial input to '+'."
482 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "+")))
483 (notmuch-tree-tag tag-changes))
485 (defun notmuch-tree-remove-tag (tag-changes)
486 "Same as `notmuch-tree-tag' but sets initial input to '-'."
488 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "-")))
489 (notmuch-tree-tag tag-changes))
491 (defun notmuch-tree-resume-message ()
492 "Resume EDITING the current draft message."
494 (notmuch-tree-close-message-window)
495 (let ((id (notmuch-tree-get-message-id)))
497 (notmuch-draft-resume id)
498 (message "No message to resume!"))))
500 ;; The next two functions close the message window before calling
501 ;; notmuch-search or notmuch-tree but they do so after the user has
502 ;; entered the query (in case the user was basing the query on
503 ;; something in the message window).
505 (defun notmuch-tree-to-search ()
506 "Run \"notmuch search\" with the given `query' and display results."
508 (let ((query (notmuch-read-query "Notmuch search: ")))
509 (notmuch-tree-close-message-window)
510 (notmuch-search query)))
512 (defun notmuch-tree-to-tree ()
513 "Run a query and display results in tree view."
515 (let ((query (notmuch-read-query "Notmuch tree view search: ")))
516 (notmuch-tree-close-message-window)
517 (notmuch-tree query)))
519 (defun notmuch-tree-archive-thread-then-next ()
520 "Archive all messages in the current buffer, then show next thread from search."
522 (notmuch-tree-archive-thread)
523 (notmuch-tree-next-thread))
525 (defun notmuch-unthreaded-from-tree-current-query ()
526 "Switch from tree view to unthreaded view."
528 (unless notmuch-tree-unthreaded
529 (notmuch-tree-refresh-view 'unthreaded)))
531 (defun notmuch-tree-from-unthreaded-current-query ()
532 "Switch from unthreaded view to tree view."
534 (when notmuch-tree-unthreaded
535 (notmuch-tree-refresh-view 'tree)))
537 (defun notmuch-search-from-tree-current-query ()
538 "Call notmuch search with the current query."
540 (notmuch-tree-close-message-window)
541 (notmuch-search (notmuch-tree-get-query)))
543 (defun notmuch-tree-message-window-kill-hook ()
544 "Close the message pane when exiting the show buffer."
545 (let ((buffer (current-buffer)))
546 (when (and (window-live-p notmuch-tree-message-window)
547 (eq (window-buffer notmuch-tree-message-window) buffer))
548 ;; We could check whether this is the only window in its frame,
549 ;; but simply ignoring the error that is thrown otherwise is
550 ;; what we had to do for Emacs 24 and we stick to that because
551 ;; it is still the simplest approach.
553 (delete-window notmuch-tree-message-window)))))
555 (defun notmuch-tree-command-hook ()
556 (when (eq major-mode 'notmuch-tree-mode)
557 ;; We just run the notmuch-show-command-hook on the message pane.
558 (when (buffer-live-p notmuch-tree-message-buffer)
559 (with-current-buffer notmuch-tree-message-buffer
560 (notmuch-show-command-hook)))))
562 (defun notmuch-tree-show-message-in ()
563 "Show the current message (in split-pane)."
565 (let ((id (notmuch-tree-get-message-id))
566 (inhibit-read-only t)
569 ;; We close and reopen the window to kill off un-needed buffers
570 ;; this might cause flickering but seems ok.
571 (notmuch-tree-close-message-window)
572 (setq notmuch-tree-message-window
573 (split-window-vertically (/ (window-height) 4)))
574 (with-selected-window notmuch-tree-message-window
575 (let (;; Since we are only displaying one message do not indent.
576 (notmuch-show-indent-messages-width 0)
577 (notmuch-show-only-matching-messages t)
578 ;; Ensure that `pop-to-buffer-same-window' uses the
579 ;; window we want it to use.
580 (display-buffer-overriding-action
581 '((display-buffer-same-window)
582 (inhibit-same-window . nil))))
583 (setq buffer (notmuch-show id))))
584 ;; We need the `let' as notmuch-tree-message-window is buffer local.
585 (let ((window notmuch-tree-message-window))
586 (with-current-buffer buffer
587 (setq notmuch-tree-message-window window)
588 (add-hook 'kill-buffer-hook 'notmuch-tree-message-window-kill-hook)))
589 (when notmuch-show-mark-read-tags
590 (notmuch-tree-tag-update-display notmuch-show-mark-read-tags))
591 (setq notmuch-tree-message-buffer buffer))))
593 (defun notmuch-tree-show-message-out ()
594 "Show the current message (in whole window)."
596 (let ((id (notmuch-tree-get-message-id))
597 (inhibit-read-only t))
599 ;; We close the window to kill off un-needed buffers.
600 (notmuch-tree-close-message-window)
603 (defun notmuch-tree-show-message (arg)
604 "Show the current message.
606 Shows in split pane or whole window according to value of
607 `notmuch-tree-show-out'. A prefix argument reverses the choice."
609 (if (or (and (notmuch-tree-show-out) (not arg))
610 (and (not (notmuch-tree-show-out)) arg))
611 (notmuch-tree-show-message-out)
612 (notmuch-tree-show-message-in)))
614 (defun notmuch-tree-scroll-message-window ()
615 "Scroll the message window (if it exists)."
617 (when (window-live-p notmuch-tree-message-window)
618 (with-selected-window notmuch-tree-message-window
619 (if (pos-visible-in-window-p (point-max))
623 (defun notmuch-tree-scroll-message-window-back ()
624 "Scroll the message window back (if it exists)."
626 (when (window-live-p notmuch-tree-message-window)
627 (with-selected-window notmuch-tree-message-window
628 (if (pos-visible-in-window-p (point-min))
632 (defun notmuch-tree-scroll-or-next ()
633 "Scroll the message window.
634 If it at end go to next message."
636 (when (notmuch-tree-scroll-message-window)
637 (notmuch-tree-next-matching-message)))
639 (defun notmuch-tree-quit (&optional kill-both)
640 "Close the split view or exit tree."
642 (when (or (not (notmuch-tree-close-message-window)) kill-both)
643 (kill-buffer (current-buffer))))
645 (defun notmuch-tree-close-message-window ()
646 "Close the message-window. Return t if close succeeds."
648 (when (and (window-live-p notmuch-tree-message-window)
649 (eq (window-buffer notmuch-tree-message-window)
650 notmuch-tree-message-buffer))
651 (delete-window notmuch-tree-message-window)
652 (unless (get-buffer-window-list notmuch-tree-message-buffer)
653 (kill-buffer notmuch-tree-message-buffer))
656 (defun notmuch-tree-archive-message (&optional unarchive)
657 "Archive the current message.
659 Archive the current message by applying the tag changes in
660 `notmuch-archive-tags' to it. If a prefix argument is given, the
661 message will be \"unarchived\", i.e. the tag changes in
662 `notmuch-archive-tags' will be reversed."
664 (when notmuch-archive-tags
666 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
668 (defun notmuch-tree-archive-message-then-next (&optional unarchive)
669 "Archive the current message and move to next matching message."
671 (notmuch-tree-archive-message unarchive)
672 (notmuch-tree-next-matching-message))
674 (defun notmuch-tree-archive-thread-then-exit ()
675 "Archive all messages in the current buffer, then exit notmuch-tree."
677 (notmuch-tree-archive-thread)
678 (notmuch-tree-quit t))
680 (defun notmuch-tree-archive-message-then-next-or-exit ()
681 "Archive current message, then show next open message in current thread.
683 If at the last open message in the current thread, then exit back
686 (notmuch-tree-archive-message)
687 (notmuch-tree-next-matching-message t))
689 (defun notmuch-tree-next-message ()
690 "Move to next message."
693 (when (window-live-p notmuch-tree-message-window)
694 (notmuch-tree-show-message-in)))
696 (defun notmuch-tree-prev-message ()
697 "Move to previous message."
700 (when (window-live-p notmuch-tree-message-window)
701 (notmuch-tree-show-message-in)))
703 (defun notmuch-tree-goto-matching-message (&optional prev)
704 "Move to the next or previous matching message.
706 Returns t if there was a next matching message in the thread to show,
708 (let ((dir (if prev -1 nil))
709 (eobfn (if prev #'bobp #'eobp)))
710 (while (and (not (funcall eobfn))
711 (not (notmuch-tree-get-match)))
713 (not (funcall eobfn))))
715 (defun notmuch-tree-matching-message (&optional prev pop-at-end)
716 "Move to the next or previous matching message."
718 (forward-line (if prev -1 nil))
719 (if (and (not (notmuch-tree-goto-matching-message prev)) pop-at-end)
720 (notmuch-tree-quit pop-at-end)
721 (when (window-live-p notmuch-tree-message-window)
722 (notmuch-tree-show-message-in))))
724 (defun notmuch-tree-prev-matching-message (&optional pop-at-end)
725 "Move to previous matching message."
727 (notmuch-tree-matching-message t pop-at-end))
729 (defun notmuch-tree-next-matching-message (&optional pop-at-end)
730 "Move to next matching message."
732 (notmuch-tree-matching-message nil pop-at-end))
734 (defun notmuch-tree-refresh-view (&optional view)
737 (when (get-buffer-process (current-buffer))
738 (error "notmuch tree process already running for current buffer"))
739 (let ((inhibit-read-only t)
740 (basic-query notmuch-tree-basic-query)
741 (unthreaded (cond ((eq view 'unthreaded) t)
742 ((eq view 'tree) nil)
743 (t notmuch-tree-unthreaded)))
744 (query-context notmuch-tree-query-context)
745 (target (notmuch-tree-get-message-id)))
747 (notmuch-tree-worker basic-query
753 (defun notmuch-tree-thread-top ()
754 (when (notmuch-tree-get-message-properties)
755 (while (not (or (notmuch-tree-get-prop :first) (eobp)))
758 (defun notmuch-tree-prev-thread-in-tree ()
759 "Move to the previous thread in the current tree"
762 (notmuch-tree-thread-top)
765 (defun notmuch-tree-next-thread-in-tree ()
766 "Get the next thread in the current tree. Returns t if a thread was
767 found or nil if not."
770 (while (not (or (notmuch-tree-get-prop :first) (eobp)))
774 (defun notmuch-tree-next-thread-from-search (&optional previous)
775 "Move to the next thread in the parent search results, if any.
777 If PREVIOUS is non-nil, move to the previous item in the
778 search results instead."
780 (let ((parent-buffer notmuch-tree-parent-buffer))
781 (notmuch-tree-quit t)
782 (when (buffer-live-p parent-buffer)
783 (switch-to-buffer parent-buffer)
785 (notmuch-search-previous-thread)
786 (notmuch-search-next-thread))
787 (notmuch-tree-from-search-thread))))
789 (defun notmuch-tree-next-thread (&optional previous)
790 "Move to the next thread in the current tree or parent search results.
792 If PREVIOUS is non-nil, move to the previous thread in the tree or
793 search results instead."
795 (unless (if previous (notmuch-tree-prev-thread-in-tree)
796 (notmuch-tree-next-thread-in-tree))
797 (notmuch-tree-next-thread-from-search previous)))
799 (defun notmuch-tree-prev-thread ()
800 "Move to the previous thread in the current tree or parent search results."
802 (notmuch-tree-next-thread t))
804 (defun notmuch-tree-thread-mapcar (function)
805 "Call FUNCTION for each message in the current thread.
806 FUNCTION is called for side effects only."
808 (notmuch-tree-thread-top)
809 (cl-loop collect (funcall function)
811 while (and (notmuch-tree-get-message-properties)
812 (not (notmuch-tree-get-prop :first))))))
814 (defun notmuch-tree-get-messages-ids-thread-search ()
815 "Return a search string for all message ids of messages in the current thread."
817 (notmuch-tree-thread-mapcar 'notmuch-tree-get-message-id)
820 (defun notmuch-tree-tag-thread (tag-changes)
821 "Tag all messages in the current thread."
823 (let ((tags (apply #'append (notmuch-tree-thread-mapcar
824 (lambda () (notmuch-tree-get-tags))))))
825 (list (notmuch-read-tag-changes tags "Tag thread"))))
826 (when (notmuch-tree-get-message-properties)
827 (notmuch-tag (notmuch-tree-get-messages-ids-thread-search) tag-changes)
828 (notmuch-tree-thread-mapcar
829 (lambda () (notmuch-tree-tag-update-display tag-changes)))))
831 (defun notmuch-tree-archive-thread (&optional unarchive)
832 "Archive each message in thread.
834 Archive each message currently shown by applying the tag changes
835 in `notmuch-archive-tags' to each. If a prefix argument is given,
836 the messages will be \"unarchived\", i.e. the tag changes in
837 `notmuch-archive-tags' will be reversed.
839 Note: This command is safe from any race condition of new messages
840 being delivered to the same thread. It does not archive the
841 entire thread, but only the messages shown in the current
844 (when notmuch-archive-tags
845 (notmuch-tree-tag-thread
846 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
848 ;;; Functions for displaying the tree buffer itself
850 (defun notmuch-tree-clean-address (address)
851 "Try to clean a single email ADDRESS for display. Return
852 AUTHOR_NAME if present, otherwise return AUTHOR_EMAIL. Return
853 unchanged ADDRESS if parsing fails."
854 (let* ((clean-address (notmuch-clean-address address))
855 (p-address (car clean-address))
856 (p-name (cdr clean-address)))
858 ;; If we have a name return that otherwise return the address.
859 (or p-name p-address)))
861 (defun notmuch-tree-format-field (field format-string msg)
862 "Format a FIELD of MSG according to FORMAT-STRING and return string."
863 (let* ((headers (plist-get msg :headers))
864 (match (plist-get msg :match)))
867 (format format-string (notmuch-tree-format-field-list field msg)))
869 ((string-equal field "date")
870 (let ((face (if match
871 'notmuch-tree-match-date-face
872 'notmuch-tree-no-match-date-face)))
873 (propertize (format format-string (plist-get msg :date_relative))
876 ((string-equal field "tree")
877 (let ((tree-status (plist-get msg :tree-status))
879 'notmuch-tree-match-tree-face
880 'notmuch-tree-no-match-tree-face)))
882 (propertize (format format-string
883 (mapconcat #'identity (reverse tree-status) ""))
886 ((string-equal field "subject")
887 (let ((bare-subject (notmuch-show-strip-re (plist-get headers :Subject)))
888 (previous-subject notmuch-tree-previous-subject)
890 'notmuch-tree-match-subject-face
891 'notmuch-tree-no-match-subject-face)))
893 (setq notmuch-tree-previous-subject bare-subject)
894 (propertize (format format-string
895 (if (string= previous-subject bare-subject)
900 ((string-equal field "authors")
901 (let ((author (notmuch-tree-clean-address (plist-get headers :From)))
902 (len (length (format format-string "")))
904 'notmuch-tree-match-author-face
905 'notmuch-tree-no-match-author-face)))
906 (when (> (length author) len)
907 (setq author (substring author 0 len)))
908 (propertize (format format-string author) 'face face)))
910 ((string-equal field "tags")
911 (let ((tags (plist-get msg :tags))
912 (orig-tags (plist-get msg :orig-tags))
914 'notmuch-tree-match-tag-face
915 'notmuch-tree-no-match-tag-face)))
916 (format format-string (notmuch-tag-format-tags tags orig-tags face)))))))
918 (defun notmuch-tree-format-field-list (field-list msg)
919 "Format fields of MSG according to FIELD-LIST and return string."
920 (let ((face (if (plist-get msg :match)
921 'notmuch-tree-match-face
922 'notmuch-tree-no-match-face))
924 (dolist (spec field-list result-string)
925 (let ((field-string (notmuch-tree-format-field (car spec) (cdr spec) msg)))
926 (setq result-string (concat result-string field-string))))
927 (notmuch-apply-face result-string face t)))
929 (defun notmuch-tree-insert-msg (msg)
930 "Insert the message MSG according to notmuch-tree-result-format."
931 ;; We need to save the previous subject as it will get overwritten
932 ;; by the insert-field calls.
933 (let ((previous-subject notmuch-tree-previous-subject))
934 (insert (notmuch-tree-format-field-list (notmuch-tree-result-format) msg))
935 (notmuch-tree-set-message-properties msg)
936 (notmuch-tree-set-prop :previous-subject previous-subject)
939 (defun notmuch-tree-goto-and-insert-msg (msg)
940 "Insert msg at the end of the buffer. Move point to msg if it is the target."
942 (goto-char (point-max))
943 (notmuch-tree-insert-msg msg))
944 (let ((msg-id (notmuch-id-to-query (plist-get msg :id)))
945 (target notmuch-tree-target-msg))
946 (when (or (and (not target) (plist-get msg :match))
947 (string= msg-id target))
948 (setq notmuch-tree-target-msg "found")
949 (goto-char (point-max))
951 (when notmuch-tree-open-target
952 (notmuch-tree-show-message-in)))))
954 (defun notmuch-tree-insert-tree (tree depth tree-status first last)
955 "Insert the message tree TREE at depth DEPTH in the current thread.
957 A message tree is another name for a single sub-thread: i.e., a
958 message together with all its descendents."
959 (let ((msg (car tree))
960 (replies (cadr tree)))
962 ((and (< 0 depth) (not last))
963 (push "├" tree-status))
964 ((and (< 0 depth) last)
965 (push "╰" tree-status))
966 ((and (eq 0 depth) first last)
967 ;; Choice between these two variants is a matter of taste.
968 ;; (push "─" tree-status))
969 (push " " tree-status))
970 ((and (eq 0 depth) first (not last))
971 (push "┬" tree-status))
972 ((and (eq 0 depth) (not first) last)
973 (push "╰" tree-status))
974 ((and (eq 0 depth) (not first) (not last))
975 (push "├" tree-status)))
976 (push (concat (if replies "┬" "─") "►") tree-status)
977 (setq msg (plist-put msg :first (and first (eq 0 depth))))
978 (setq msg (plist-put msg :tree-status tree-status))
979 (setq msg (plist-put msg :orig-tags (plist-get msg :tags)))
980 (notmuch-tree-goto-and-insert-msg msg)
984 (push " " tree-status)
985 (push "│" tree-status))
986 (notmuch-tree-insert-thread replies (1+ depth) tree-status)))
988 (defun notmuch-tree-insert-thread (thread depth tree-status)
989 "Insert the collection of sibling sub-threads THREAD at depth DEPTH in the current forest."
990 (let ((n (length thread)))
991 (cl-loop for tree in thread
992 for count from 1 to n
993 do (notmuch-tree-insert-tree tree depth tree-status
997 (defun notmuch-tree-insert-forest-thread (forest-thread)
998 "Insert a single complete thread."
1000 ;; Reset at the start of each main thread.
1001 (setq notmuch-tree-previous-subject nil)
1002 (notmuch-tree-insert-thread forest-thread 0 tree-status)))
1004 (defun notmuch-tree-insert-forest (forest)
1005 "Insert a forest of threads.
1007 This function inserts a collection of several complete threads as
1008 passed to it by notmuch-tree-process-filter."
1009 (mapc 'notmuch-tree-insert-forest-thread forest))
1011 (define-derived-mode notmuch-tree-mode fundamental-mode "notmuch-tree"
1012 "Major mode displaying messages (as opposed to threads) of a notmuch search.
1014 This buffer contains the results of a \"notmuch tree\" of your
1015 email archives. Each line in the buffer represents a single
1016 message giving the relative date, the author, subject, and any
1019 Pressing \\[notmuch-tree-show-message] on any line displays that message.
1021 Complete list of currently available key bindings:
1023 \\{notmuch-tree-mode-map}"
1024 (setq notmuch-buffer-refresh-function #'notmuch-tree-refresh-view)
1026 (setq buffer-read-only t)
1027 (setq truncate-lines t))
1029 (defun notmuch-tree-process-sentinel (proc _msg)
1030 "Add a message to let user know when \"notmuch tree\" exits."
1031 (let ((buffer (process-buffer proc))
1032 (status (process-status proc))
1033 (exit-status (process-exit-status proc)))
1034 (when (memq status '(exit signal))
1035 (kill-buffer (process-get proc 'parse-buf))
1036 (when (buffer-live-p buffer)
1037 (with-current-buffer buffer
1039 (let ((inhibit-read-only t))
1040 (goto-char (point-max))
1041 (when (eq status 'signal)
1042 (insert "Incomplete search results (tree view process was killed).\n"))
1043 (when (eq status 'exit)
1044 (insert "End of search results.")
1045 (unless (= exit-status 0)
1046 (insert (format " (process returned %d)" exit-status)))
1047 (insert "\n")))))))))
1049 (defun notmuch-tree-process-filter (proc string)
1050 "Process and filter the output of \"notmuch show\" for tree view."
1051 (let ((results-buf (process-buffer proc))
1052 (parse-buf (process-get proc 'parse-buf))
1053 (inhibit-read-only t))
1054 (if (not (buffer-live-p results-buf))
1055 (delete-process proc)
1056 (with-current-buffer parse-buf
1059 (goto-char (point-max))
1061 (notmuch-sexp-parse-partial-list 'notmuch-tree-insert-forest-thread
1064 (defun notmuch-tree-worker (basic-query &optional query-context target open-target unthreaded)
1065 "Insert the tree view of the search in the current buffer.
1067 This is is a helper function for notmuch-tree. The arguments are
1068 the same as for the function notmuch-tree."
1071 (add-hook 'post-command-hook #'notmuch-tree-command-hook t t)
1072 (setq notmuch-tree-unthreaded unthreaded)
1073 (setq notmuch-tree-basic-query basic-query)
1074 (setq notmuch-tree-query-context (if (or (string= query-context "")
1075 (string= query-context "*"))
1078 (setq notmuch-tree-target-msg target)
1079 (setq notmuch-tree-open-target open-target)
1080 ;; Set the default value for `notmuch-show-process-crypto' in this
1081 ;; buffer. Although we don't use this some of the functions we call
1082 ;; (such as reply) do. It is a buffer local variable so setting it
1083 ;; will not affect genuine show buffers.
1084 (setq notmuch-show-process-crypto notmuch-crypto-process-mime)
1086 (goto-char (point-min))
1087 (let* ((search-args (concat basic-query
1089 (concat " and (" query-context ")"))))
1090 (message-arg (if unthreaded "--unthreaded" "--entire-thread")))
1091 (when (equal (car (process-lines notmuch-command "count" search-args)) "0")
1092 (setq search-args basic-query))
1093 (notmuch-tag-clear-cache)
1094 (let ((proc (notmuch-start-notmuch
1095 "notmuch-tree" (current-buffer) #'notmuch-tree-process-sentinel
1096 "show" "--body=false" "--format=sexp" "--format-version=4"
1097 message-arg search-args))
1098 ;; Use a scratch buffer to accumulate partial output.
1099 ;; This buffer will be killed by the sentinel, which
1100 ;; should be called no matter how the process dies.
1101 (parse-buf (generate-new-buffer " *notmuch tree parse*")))
1102 (process-put proc 'parse-buf parse-buf)
1103 (set-process-filter proc 'notmuch-tree-process-filter)
1104 (set-process-query-on-exit-flag proc nil))))
1106 (defun notmuch-tree-get-query ()
1107 "Return the current query in this tree buffer."
1108 (if notmuch-tree-query-context
1109 (concat notmuch-tree-basic-query
1111 notmuch-tree-query-context
1113 notmuch-tree-basic-query))
1115 (defun notmuch-tree (&optional query query-context target buffer-name open-target unthreaded parent-buffer)
1116 "Display threads matching QUERY in tree view.
1119 QUERY: the main query. This can be any query but in many cases will be
1120 a single thread. If nil this is read interactively from the minibuffer.
1121 QUERY-CONTEXT: is an additional term for the query. The query used
1122 is QUERY and QUERY-CONTEXT unless that does not match any messages
1123 in which case we fall back to just QUERY.
1124 TARGET: A message ID (with the id: prefix) that will be made
1125 current if it appears in the tree view results.
1126 BUFFER-NAME: the name of the buffer to display the tree view. If
1127 it is nil \"*notmuch-tree\" followed by QUERY is used.
1128 OPEN-TARGET: If TRUE open the target message in the message pane.
1129 UNTHREADED: If TRUE only show matching messages in an unthreaded view."
1132 (setq query (notmuch-read-query (concat "Notmuch "
1133 (if unthreaded "unthreaded " "tree ")
1135 (let ((buffer (get-buffer-create (generate-new-buffer-name
1138 (if unthreaded "unthreaded-" "tree-")
1140 (inhibit-read-only t))
1141 (pop-to-buffer-same-window buffer))
1142 ;; Don't track undo information for this buffer
1143 (setq buffer-undo-list t)
1144 (notmuch-tree-worker query query-context target open-target unthreaded)
1145 (setq notmuch-tree-parent-buffer parent-buffer)
1146 (setq truncate-lines t))
1148 (defun notmuch-unthreaded (&optional query query-context target buffer-name open-target)
1150 (notmuch-tree query query-context target buffer-name open-target t))
1154 (provide 'notmuch-tree)
1156 ;;; notmuch-tree.el ends here