1 ;;; notmuch-tree.el --- displaying notmuch forests
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>
27 (eval-when-compile (require 'cl-lib))
31 (require 'notmuch-lib)
32 (require 'notmuch-query)
33 (require 'notmuch-show)
34 (require 'notmuch-tag)
35 (require 'notmuch-parser)
36 (require 'notmuch-jump)
38 (declare-function notmuch-search "notmuch"
39 (&optional query oldest-first target-thread target-line))
40 (declare-function notmuch-call-notmuch-process "notmuch" (&rest args))
41 (declare-function notmuch-read-query "notmuch" (prompt))
42 (declare-function notmuch-search-find-thread-id "notmuch" (&optional bare))
43 (declare-function notmuch-search-find-subject "notmuch" ())
45 ;; the following variable is defined in notmuch.el
46 (defvar notmuch-search-query-string)
48 ;; this variable distinguishes the unthreaded display from the normal tree display
49 (defvar notmuch-tree-unthreaded nil
50 "A buffer local copy of argument unthreaded to the function notmuch-tree.")
51 (make-variable-buffer-local 'notmuch-tree-unthreaded)
53 (defgroup notmuch-tree nil
54 "Showing message and thread structure."
57 (defcustom notmuch-tree-show-out nil
58 "View selected messages in new window rather than split-pane."
62 (defcustom notmuch-unthreaded-show-out t
63 "View selected messages in new window rather than split-pane."
67 (defun notmuch-tree-show-out ()
68 (if notmuch-tree-unthreaded
69 notmuch-unthreaded-show-out
70 notmuch-tree-show-out))
72 (defcustom notmuch-tree-result-format
75 ((("tree" . "%s")("subject" . "%s")) ." %-54s ")
77 "Result formatting for tree view. Supported fields are: date,
78 authors, subject, tree, tags. Tree means the thread tree
79 box graphics. The field may also be a list in which case
80 the formatting rules are applied recursively and then the
81 output of all the fields in the list is inserted
82 according to format-string.
84 Note the author string should not contain
85 whitespace (put it in the neighbouring fields instead).
87 (setq notmuch-tree-result-format \(\(\"authors\" . \"%-40s\"\)
88 \(\"subject\" . \"%s\"\)\)\)"
89 :type '(alist :key-type (string) :value-type (string))
92 (defcustom notmuch-unthreaded-result-format
95 ((("subject" . "%s")) ." %-54s ")
97 "Result formatting for unthreaded tree view. Supported fields are: date,
98 authors, subject, tree, tags. Tree means the thread tree
99 box graphics. The field may also be a list in which case
100 the formatting rules are applied recursively and then the
101 output of all the fields in the list is inserted
102 according to format-string.
104 Note the author string should not contain
105 whitespace (put it in the neighbouring fields instead).
107 (setq notmuch-tree-result-format \(\(\"authors\" . \"%-40s\"\)
108 \(\"subject\" . \"%s\"\)\)\)"
109 :type '(alist :key-type (string) :value-type (string))
110 :group 'notmuch-tree)
112 (defun notmuch-tree-result-format ()
113 (if notmuch-tree-unthreaded
114 notmuch-unthreaded-result-format
115 notmuch-tree-result-format))
117 ;; Faces for messages that match the query.
118 (defface notmuch-tree-match-face
119 '((t :inherit default))
120 "Default face used in tree mode face for matching messages"
122 :group 'notmuch-faces)
124 (defface notmuch-tree-match-date-face
126 "Face used in tree mode for the date in messages matching the query."
128 :group 'notmuch-faces)
130 (defface notmuch-tree-match-author-face
133 (:foreground "OliveDrab1"))
136 (:foreground "dark blue"))
139 "Face used in tree mode for the date in messages matching the query."
141 :group 'notmuch-faces)
143 (defface notmuch-tree-match-subject-face
145 "Face used in tree mode for the subject in messages matching the query."
147 :group 'notmuch-faces)
149 (defface notmuch-tree-match-tree-face
151 "Face used in tree mode for the thread tree block graphics in messages matching the query."
153 :group 'notmuch-faces)
155 (defface notmuch-tree-match-tag-face
158 (:foreground "OliveDrab1"))
161 (:foreground "navy blue" :bold t))
164 "Face used in tree mode for tags in messages matching the query."
166 :group 'notmuch-faces)
168 ;; Faces for messages that do not match the query.
169 (defface notmuch-tree-no-match-face
170 '((t (:foreground "gray")))
171 "Default face used in tree mode face for non-matching messages."
173 :group 'notmuch-faces)
175 (defface notmuch-tree-no-match-date-face
177 "Face used in tree mode for non-matching dates."
179 :group 'notmuch-faces)
181 (defface notmuch-tree-no-match-subject-face
183 "Face used in tree mode for non-matching subjects."
185 :group 'notmuch-faces)
187 (defface notmuch-tree-no-match-tree-face
189 "Face used in tree mode for the thread tree block graphics in messages matching the query."
191 :group 'notmuch-faces)
193 (defface notmuch-tree-no-match-author-face
195 "Face used in tree mode for the date in messages matching the query."
197 :group 'notmuch-faces)
199 (defface notmuch-tree-no-match-tag-face
201 "Face used in tree mode face for non-matching tags."
203 :group 'notmuch-faces)
205 (defvar notmuch-tree-previous-subject
206 "The subject of the most recent result shown during the async display.")
207 (make-variable-buffer-local 'notmuch-tree-previous-subject)
209 (defvar notmuch-tree-basic-query nil
210 "A buffer local copy of argument query to the function notmuch-tree.")
211 (make-variable-buffer-local 'notmuch-tree-basic-query)
213 (defvar notmuch-tree-query-context nil
214 "A buffer local copy of argument query-context to the function notmuch-tree.")
215 (make-variable-buffer-local 'notmuch-tree-query-context)
217 (defvar notmuch-tree-target-msg nil
218 "A buffer local copy of argument target to the function notmuch-tree.")
219 (make-variable-buffer-local 'notmuch-tree-target-msg)
221 (defvar notmuch-tree-open-target nil
222 "A buffer local copy of argument open-target to the function notmuch-tree.")
223 (make-variable-buffer-local 'notmuch-tree-open-target)
225 (defvar notmuch-tree-parent-buffer nil)
226 (make-variable-buffer-local 'notmuch-tree-parent-buffer)
228 (defvar notmuch-tree-message-window nil
229 "The window of the message pane.
231 It is set in both the tree buffer and the child show buffer. It
232 is used to try and close the message pane when quitting tree view
233 or the child show buffer.")
234 (make-variable-buffer-local 'notmuch-tree-message-window)
235 (put 'notmuch-tree-message-window 'permanent-local t)
237 (defvar notmuch-tree-message-buffer nil
238 "The buffer name of the show buffer in the message pane.
240 This is used to try and make sure we don't close the message pane
241 if the user has loaded a different buffer in that window.")
242 (make-variable-buffer-local 'notmuch-tree-message-buffer)
243 (put 'notmuch-tree-message-buffer 'permanent-local t)
245 (defun notmuch-tree-to-message-pane (func)
246 "Execute FUNC in message pane.
248 This function returns a function (so can be used as a keybinding)
249 which executes function FUNC in the message pane if it is
250 open (if the message pane is closed it does nothing)."
252 ,(concat "(In message pane) " (documentation func t))
254 (when (window-live-p notmuch-tree-message-window)
255 (with-selected-window notmuch-tree-message-window
256 (call-interactively #',func)))))
258 (defun notmuch-tree-inherit-from-message-pane (sym)
259 "Return value of SYM in message-pane if open, or tree-pane if not."
260 (if (window-live-p notmuch-tree-message-window)
261 (with-selected-window notmuch-tree-message-window
265 (defun notmuch-tree-button-activate (&optional button)
266 "Activate BUTTON or button at point.
268 This function does not give an error if there is no button."
270 (let ((button (or button (button-at (point)))))
271 (when button (button-activate button))))
273 (defun notmuch-tree-close-message-pane-and (func)
274 "Close message pane and execute FUNC.
276 This function returns a function (so can be used as a keybinding)
277 which closes the message pane if open and then executes function
280 ,(concat "(Close message pane and) " (documentation func t))
282 (let ((notmuch-show-process-crypto
283 (notmuch-tree-inherit-from-message-pane 'notmuch-show-process-crypto)))
284 (notmuch-tree-close-message-window)
285 (call-interactively #',func))))
287 (defvar notmuch-tree-mode-map
288 (let ((map (make-sparse-keymap)))
289 (set-keymap-parent map notmuch-common-keymap)
290 ;; The following override the global keymap.
291 ;; Override because we want to close message pane first.
292 (define-key map [remap notmuch-help]
293 (notmuch-tree-close-message-pane-and #'notmuch-help))
294 ;; Override because we first close message pane and then close tree buffer.
295 (define-key map [remap notmuch-bury-or-kill-this-buffer] 'notmuch-tree-quit)
296 ;; Override because we close message pane after the search query is entered.
297 (define-key map [remap notmuch-search] 'notmuch-tree-to-search)
298 ;; Override because we want to close message pane first.
299 (define-key map [remap notmuch-mua-new-mail]
300 (notmuch-tree-close-message-pane-and #'notmuch-mua-new-mail))
301 ;; Override because we want to close message pane first.
302 (define-key map [remap notmuch-jump-search]
303 (notmuch-tree-close-message-pane-and #'notmuch-jump-search))
305 (define-key map "S" 'notmuch-search-from-tree-current-query)
306 (define-key map "U" 'notmuch-unthreaded-from-tree-current-query)
307 (define-key map "Z" 'notmuch-tree-from-unthreaded-current-query)
309 ;; these use notmuch-show functions directly
310 (define-key map "|" 'notmuch-show-pipe-message)
311 (define-key map "w" 'notmuch-show-save-attachments)
312 (define-key map "v" 'notmuch-show-view-all-mime-parts)
313 (define-key map "c" 'notmuch-show-stash-map)
314 (define-key map "b" 'notmuch-show-resend-message)
316 ;; these apply to the message pane
317 (define-key map (kbd "M-TAB")
318 (notmuch-tree-to-message-pane #'notmuch-show-previous-button))
319 (define-key map (kbd "<backtab>")
320 (notmuch-tree-to-message-pane #'notmuch-show-previous-button))
321 (define-key map (kbd "TAB")
322 (notmuch-tree-to-message-pane #'notmuch-show-next-button))
324 (notmuch-tree-to-message-pane #'notmuch-show-toggle-process-crypto))
326 ;; bindings from show (or elsewhere) but we close the message pane first.
328 (notmuch-tree-close-message-pane-and #'notmuch-show-forward-message))
330 (notmuch-tree-close-message-pane-and #'notmuch-show-reply-sender))
332 (notmuch-tree-close-message-pane-and #'notmuch-show-reply))
334 (notmuch-tree-close-message-pane-and #'notmuch-show-view-raw-message))
336 ;; The main tree view bindings
337 (define-key map (kbd "RET") 'notmuch-tree-show-message)
338 (define-key map [mouse-1] 'notmuch-tree-show-message)
339 (define-key map "x" 'notmuch-tree-archive-message-then-next-or-exit)
340 (define-key map "X" 'notmuch-tree-archive-thread-then-exit)
341 (define-key map "A" 'notmuch-tree-archive-thread-then-next)
342 (define-key map "a" 'notmuch-tree-archive-message-then-next)
343 (define-key map "z" 'notmuch-tree-to-tree)
344 (define-key map "n" 'notmuch-tree-next-matching-message)
345 (define-key map "p" 'notmuch-tree-prev-matching-message)
346 (define-key map "N" 'notmuch-tree-next-message)
347 (define-key map "P" 'notmuch-tree-prev-message)
348 (define-key map (kbd "M-p") 'notmuch-tree-prev-thread)
349 (define-key map (kbd "M-n") 'notmuch-tree-next-thread)
350 (define-key map "k" 'notmuch-tag-jump)
351 (define-key map "-" 'notmuch-tree-remove-tag)
352 (define-key map "+" 'notmuch-tree-add-tag)
353 (define-key map "*" 'notmuch-tree-tag-thread)
354 (define-key map " " 'notmuch-tree-scroll-or-next)
355 (define-key map (kbd "DEL") 'notmuch-tree-scroll-message-window-back)
356 (define-key map "e" 'notmuch-tree-resume-message)
358 (fset 'notmuch-tree-mode-map notmuch-tree-mode-map)
360 (defun notmuch-tree-get-message-properties ()
361 "Return the properties of the current message as a plist.
363 Some useful entries are:
364 :headers - Property list containing the headers :Date, :Subject, :From, etc.
365 :tags - Tags for this message."
368 (get-text-property (point) :notmuch-message-properties)))
370 (defun notmuch-tree-set-message-properties (props)
373 (put-text-property (point)
375 :notmuch-message-properties props)))
377 (defun notmuch-tree-set-prop (prop val &optional props)
378 (let ((inhibit-read-only t)
380 (notmuch-tree-get-message-properties))))
381 (plist-put props prop val)
382 (notmuch-tree-set-message-properties props)))
384 (defun notmuch-tree-get-prop (prop &optional props)
385 (let ((props (or props
386 (notmuch-tree-get-message-properties))))
387 (plist-get props prop)))
389 (defun notmuch-tree-set-tags (tags)
390 "Set the tags of the current message."
391 (notmuch-tree-set-prop :tags tags))
393 (defun notmuch-tree-get-tags ()
394 "Return the tags of the current message."
395 (notmuch-tree-get-prop :tags))
397 (defun notmuch-tree-get-message-id (&optional bare)
398 "Return the message id of the current message."
399 (let ((id (notmuch-tree-get-prop :id)))
403 (notmuch-id-to-query id))
406 (defun notmuch-tree-get-match ()
407 "Return whether the current message is a match."
409 (notmuch-tree-get-prop :match))
411 (defun notmuch-tree-refresh-result ()
412 "Redisplay the current message line.
414 This redisplays the current line based on the messages
415 properties (as they are now). This is used when tags are
417 (let ((init-point (point))
418 (end (line-end-position))
419 (msg (notmuch-tree-get-message-properties))
420 (inhibit-read-only t))
422 ;; This is a little tricky: we override
423 ;; notmuch-tree-previous-subject to get the decision between
424 ;; ... and a subject right and it stops notmuch-tree-insert-msg
425 ;; from overwriting the buffer local copy of
426 ;; notmuch-tree-previous-subject if this is called while the
427 ;; buffer is displaying.
428 (let ((notmuch-tree-previous-subject
429 (notmuch-tree-get-prop :previous-subject)))
430 (delete-region (point) (1+ (line-end-position)))
431 (notmuch-tree-insert-msg msg))
432 (let ((new-end (line-end-position)))
433 (goto-char (if (= init-point end)
435 (min init-point (- new-end 1)))))))
437 (defun notmuch-tree-tag-update-display (&optional tag-changes)
438 "Update display for TAG-CHANGES to current message.
440 Updates the message in the message pane if appropriate, but does
441 NOT change the database."
442 (let* ((current-tags (notmuch-tree-get-tags))
443 (new-tags (notmuch-update-tags current-tags tag-changes))
444 (tree-msg-id (notmuch-tree-get-message-id)))
445 (unless (equal current-tags new-tags)
446 (notmuch-tree-set-tags new-tags)
447 (notmuch-tree-refresh-result)
448 (when (window-live-p notmuch-tree-message-window)
449 (with-selected-window notmuch-tree-message-window
450 (when (string= tree-msg-id (notmuch-show-get-message-id))
451 (notmuch-show-update-tags new-tags)))))))
453 (defun notmuch-tree-tag (tag-changes)
454 "Change tags for the current message."
456 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message")))
457 (notmuch-tag (notmuch-tree-get-message-id) tag-changes)
458 (notmuch-tree-tag-update-display tag-changes))
460 (defun notmuch-tree-add-tag (tag-changes)
461 "Same as `notmuch-tree-tag' but sets initial input to '+'."
463 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "+")))
464 (notmuch-tree-tag tag-changes))
466 (defun notmuch-tree-remove-tag (tag-changes)
467 "Same as `notmuch-tree-tag' but sets initial input to '-'."
469 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "-")))
470 (notmuch-tree-tag tag-changes))
472 (defun notmuch-tree-resume-message ()
473 "Resume EDITING the current draft message."
475 (notmuch-tree-close-message-window)
476 (let ((id (notmuch-tree-get-message-id)))
478 (notmuch-draft-resume id)
479 (message "No message to resume!"))))
481 ;; The next two functions close the message window before calling
482 ;; notmuch-search or notmuch-tree but they do so after the user has
483 ;; entered the query (in case the user was basing the query on
484 ;; something in the message window).
486 (defun notmuch-tree-to-search ()
487 "Run \"notmuch search\" with the given `query' and display results."
489 (let ((query (notmuch-read-query "Notmuch search: ")))
490 (notmuch-tree-close-message-window)
491 (notmuch-search query)))
493 (defun notmuch-tree-to-tree ()
494 "Run a query and display results in tree view."
496 (let ((query (notmuch-read-query "Notmuch tree view search: ")))
497 (notmuch-tree-close-message-window)
498 (notmuch-tree query)))
500 (defun notmuch-tree-archive-thread-then-next ()
501 "Archive all messages in the current buffer, then show next thread from search."
503 (notmuch-tree-archive-thread)
504 (notmuch-tree-next-thread))
506 (defun notmuch-unthreaded-from-tree-current-query ()
507 "Switch from tree view to unthreaded view."
509 (unless notmuch-tree-unthreaded
510 (notmuch-tree-refresh-view 'unthreaded)))
512 (defun notmuch-tree-from-unthreaded-current-query ()
513 "Switch from unthreaded view to tree view."
515 (when notmuch-tree-unthreaded
516 (notmuch-tree-refresh-view 'tree)))
518 (defun notmuch-search-from-tree-current-query ()
519 "Call notmuch search with the current query."
521 (notmuch-tree-close-message-window)
522 (notmuch-search (notmuch-tree-get-query)))
524 (defun notmuch-tree-message-window-kill-hook ()
525 "Close the message pane when exiting the show buffer."
526 (let ((buffer (current-buffer)))
527 (when (and (window-live-p notmuch-tree-message-window)
528 (eq (window-buffer notmuch-tree-message-window) buffer))
529 ;; We do not want an error if this is the sole window in the
530 ;; frame and I do not know how to test for that in emacs pre
531 ;; 24. Hence we just ignore-errors.
533 (delete-window notmuch-tree-message-window)))))
535 (defun notmuch-tree-command-hook ()
536 (when (eq major-mode 'notmuch-tree-mode)
537 ;; We just run the notmuch-show-command-hook on the message pane.
538 (when (buffer-live-p notmuch-tree-message-buffer)
539 (with-current-buffer notmuch-tree-message-buffer
540 (notmuch-show-command-hook)))))
542 (defun notmuch-tree-show-message-in ()
543 "Show the current message (in split-pane)."
545 (let ((id (notmuch-tree-get-message-id))
546 (inhibit-read-only t)
549 ;; We close and reopen the window to kill off un-needed buffers
550 ;; this might cause flickering but seems ok.
551 (notmuch-tree-close-message-window)
552 (setq notmuch-tree-message-window
553 (split-window-vertically (/ (window-height) 4)))
554 (with-selected-window notmuch-tree-message-window
555 ;; Since we are only displaying one message do not indent.
556 (let ((notmuch-show-indent-messages-width 0)
557 (notmuch-show-only-matching-messages t))
558 (setq buffer (notmuch-show id))))
559 ;; We need the `let' as notmuch-tree-message-window is buffer local.
560 (let ((window notmuch-tree-message-window))
561 (with-current-buffer buffer
562 (setq notmuch-tree-message-window window)
563 (add-hook 'kill-buffer-hook 'notmuch-tree-message-window-kill-hook)))
564 (when notmuch-show-mark-read-tags
565 (notmuch-tree-tag-update-display notmuch-show-mark-read-tags))
566 (setq notmuch-tree-message-buffer buffer))))
568 (defun notmuch-tree-show-message-out ()
569 "Show the current message (in whole window)."
571 (let ((id (notmuch-tree-get-message-id))
572 (inhibit-read-only t)
575 ;; We close the window to kill off un-needed buffers.
576 (notmuch-tree-close-message-window)
579 (defun notmuch-tree-show-message (arg)
580 "Show the current message.
582 Shows in split pane or whole window according to value of
583 `notmuch-tree-show-out'. A prefix argument reverses the choice."
585 (if (or (and (notmuch-tree-show-out) (not arg))
586 (and (not (notmuch-tree-show-out)) arg))
587 (notmuch-tree-show-message-out)
588 (notmuch-tree-show-message-in)))
590 (defun notmuch-tree-scroll-message-window ()
591 "Scroll the message window (if it exists)."
593 (when (window-live-p notmuch-tree-message-window)
594 (with-selected-window notmuch-tree-message-window
595 (if (pos-visible-in-window-p (point-max))
599 (defun notmuch-tree-scroll-message-window-back ()
600 "Scroll the message window back (if it exists)."
602 (when (window-live-p notmuch-tree-message-window)
603 (with-selected-window notmuch-tree-message-window
604 (if (pos-visible-in-window-p (point-min))
608 (defun notmuch-tree-scroll-or-next ()
609 "Scroll the message window.
610 If it at end go to next message."
612 (when (notmuch-tree-scroll-message-window)
613 (notmuch-tree-next-matching-message)))
615 (defun notmuch-tree-quit (&optional kill-both)
616 "Close the split view or exit tree."
618 (when (or (not (notmuch-tree-close-message-window)) kill-both)
619 (kill-buffer (current-buffer))))
621 (defun notmuch-tree-close-message-window ()
622 "Close the message-window. Return t if close succeeds."
624 (when (and (window-live-p notmuch-tree-message-window)
625 (eq (window-buffer notmuch-tree-message-window)
626 notmuch-tree-message-buffer))
627 (delete-window notmuch-tree-message-window)
628 (unless (get-buffer-window-list notmuch-tree-message-buffer)
629 (kill-buffer notmuch-tree-message-buffer))
632 (defun notmuch-tree-archive-message (&optional unarchive)
633 "Archive the current message.
635 Archive the current message by applying the tag changes in
636 `notmuch-archive-tags' to it. If a prefix argument is given, the
637 message will be \"unarchived\", i.e. the tag changes in
638 `notmuch-archive-tags' will be reversed."
640 (when notmuch-archive-tags
642 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
644 (defun notmuch-tree-archive-message-then-next (&optional unarchive)
645 "Archive the current message and move to next matching message."
647 (notmuch-tree-archive-message unarchive)
648 (notmuch-tree-next-matching-message))
650 (defun notmuch-tree-archive-thread-then-exit ()
651 "Archive all messages in the current buffer, then exit notmuch-tree."
653 (notmuch-tree-archive-thread)
654 (notmuch-tree-quit t))
656 (defun notmuch-tree-archive-message-then-next-or-exit ()
657 "Archive current message, then show next open message in current thread.
659 If at the last open message in the current thread, then exit back
662 (notmuch-tree-archive-message)
663 (notmuch-tree-next-matching-message t))
665 (defun notmuch-tree-next-message ()
666 "Move to next message."
669 (when (window-live-p notmuch-tree-message-window)
670 (notmuch-tree-show-message-in)))
672 (defun notmuch-tree-prev-message ()
673 "Move to previous message."
676 (when (window-live-p notmuch-tree-message-window)
677 (notmuch-tree-show-message-in)))
679 (defun notmuch-tree-goto-matching-message (&optional prev)
680 "Move to the next or previous matching message.
682 Returns t if there was a next matching message in the thread to show,
684 (let ((dir (if prev -1 nil))
685 (eobfn (if prev #'bobp #'eobp)))
686 (while (and (not (funcall eobfn))
687 (not (notmuch-tree-get-match)))
689 (not (funcall eobfn))))
691 (defun notmuch-tree-matching-message (&optional prev pop-at-end)
692 "Move to the next or previous matching message."
694 (forward-line (if prev -1 nil))
695 (if (and (not (notmuch-tree-goto-matching-message prev)) pop-at-end)
696 (notmuch-tree-quit pop-at-end)
697 (when (window-live-p notmuch-tree-message-window)
698 (notmuch-tree-show-message-in))))
700 (defun notmuch-tree-prev-matching-message (&optional pop-at-end)
701 "Move to previous matching message."
703 (notmuch-tree-matching-message t pop-at-end))
705 (defun notmuch-tree-next-matching-message (&optional pop-at-end)
706 "Move to next matching message."
708 (notmuch-tree-matching-message nil pop-at-end))
710 (defun notmuch-tree-refresh-view (&optional view)
713 (when (get-buffer-process (current-buffer))
714 (error "notmuch tree process already running for current buffer"))
715 (let ((inhibit-read-only t)
716 (basic-query notmuch-tree-basic-query)
717 (unthreaded (cond ((eq view 'unthreaded) t)
718 ((eq view 'tree) nil)
719 (t notmuch-tree-unthreaded)))
720 (query-context notmuch-tree-query-context)
721 (target (notmuch-tree-get-message-id)))
723 (notmuch-tree-worker basic-query
729 (defun notmuch-tree-thread-top ()
730 (when (notmuch-tree-get-message-properties)
731 (while (not (or (notmuch-tree-get-prop :first) (eobp)))
734 (defun notmuch-tree-prev-thread-in-tree ()
735 "Move to the previous thread in the current tree"
738 (notmuch-tree-thread-top)
741 (defun notmuch-tree-next-thread-in-tree ()
742 "Get the next thread in the current tree. Returns t if a thread was
743 found or nil if not."
746 (while (not (or (notmuch-tree-get-prop :first) (eobp)))
750 (defun notmuch-tree-next-thread-from-search (&optional previous)
751 "Move to the next thread in the parent search results, if any.
753 If PREVIOUS is non-nil, move to the previous item in the
754 search results instead."
756 (let ((parent-buffer notmuch-tree-parent-buffer))
757 (notmuch-tree-quit t)
758 (when (buffer-live-p parent-buffer)
759 (switch-to-buffer parent-buffer)
761 (notmuch-search-previous-thread)
762 (notmuch-search-next-thread))
763 (notmuch-tree-from-search-thread))))
765 (defun notmuch-tree-next-thread (&optional previous)
766 "Move to the next thread in the current tree or parent search
769 If PREVIOUS is non-nil, move to the previous thread in the tree or
770 search results instead."
772 (unless (if previous (notmuch-tree-prev-thread-in-tree)
773 (notmuch-tree-next-thread-in-tree))
774 (notmuch-tree-next-thread-from-search previous)))
776 (defun notmuch-tree-prev-thread ()
777 "Move to the previous thread in the current tree or parent search
780 (notmuch-tree-next-thread t))
782 (defun notmuch-tree-thread-mapcar (function)
783 "Iterate through all messages in the current thread
784 and call FUNCTION for side effects."
786 (notmuch-tree-thread-top)
787 (cl-loop collect (funcall function)
789 while (and (notmuch-tree-get-message-properties)
790 (not (notmuch-tree-get-prop :first))))))
792 (defun notmuch-tree-get-messages-ids-thread-search ()
793 "Return a search string for all message ids of messages in the current thread."
795 (notmuch-tree-thread-mapcar 'notmuch-tree-get-message-id)
798 (defun notmuch-tree-tag-thread (tag-changes)
799 "Tag all messages in the current thread."
801 (let ((tags (apply #'append (notmuch-tree-thread-mapcar
802 (lambda () (notmuch-tree-get-tags))))))
803 (list (notmuch-read-tag-changes tags "Tag thread"))))
804 (when (notmuch-tree-get-message-properties)
805 (notmuch-tag (notmuch-tree-get-messages-ids-thread-search) tag-changes)
806 (notmuch-tree-thread-mapcar
807 (lambda () (notmuch-tree-tag-update-display tag-changes)))))
809 (defun notmuch-tree-archive-thread (&optional unarchive)
810 "Archive each message in thread.
812 Archive each message currently shown by applying the tag changes
813 in `notmuch-archive-tags' to each. If a prefix argument is given,
814 the messages will be \"unarchived\", i.e. the tag changes in
815 `notmuch-archive-tags' will be reversed.
817 Note: This command is safe from any race condition of new messages
818 being delivered to the same thread. It does not archive the
819 entire thread, but only the messages shown in the current
822 (when notmuch-archive-tags
823 (notmuch-tree-tag-thread
824 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
826 ;; Functions below here display the tree buffer itself.
828 (defun notmuch-tree-clean-address (address)
829 "Try to clean a single email ADDRESS for display. Return
830 AUTHOR_NAME if present, otherwise return AUTHOR_EMAIL. Return
831 unchanged ADDRESS if parsing fails."
832 (let* ((clean-address (notmuch-clean-address address))
833 (p-address (car clean-address))
834 (p-name (cdr clean-address)))
836 ;; If we have a name return that otherwise return the address.
837 (or p-name p-address)))
839 (defun notmuch-tree-format-field (field format-string msg)
840 "Format a FIELD of MSG according to FORMAT-STRING and return string."
841 (let* ((headers (plist-get msg :headers))
842 (match (plist-get msg :match)))
845 (format format-string (notmuch-tree-format-field-list field msg)))
847 ((string-equal field "date")
848 (let ((face (if match
849 'notmuch-tree-match-date-face
850 'notmuch-tree-no-match-date-face)))
851 (propertize (format format-string (plist-get msg :date_relative))
854 ((string-equal field "tree")
855 (let ((tree-status (plist-get msg :tree-status))
857 'notmuch-tree-match-tree-face
858 'notmuch-tree-no-match-tree-face)))
860 (propertize (format format-string
861 (mapconcat #'identity (reverse tree-status) ""))
864 ((string-equal field "subject")
865 (let ((bare-subject (notmuch-show-strip-re (plist-get headers :Subject)))
866 (previous-subject notmuch-tree-previous-subject)
868 'notmuch-tree-match-subject-face
869 'notmuch-tree-no-match-subject-face)))
871 (setq notmuch-tree-previous-subject bare-subject)
872 (propertize (format format-string
873 (if (string= previous-subject bare-subject)
878 ((string-equal field "authors")
879 (let ((author (notmuch-tree-clean-address (plist-get headers :From)))
880 (len (length (format format-string "")))
882 'notmuch-tree-match-author-face
883 'notmuch-tree-no-match-author-face)))
884 (when (> (length author) len)
885 (setq author (substring author 0 len)))
886 (propertize (format format-string author) 'face face)))
888 ((string-equal field "tags")
889 (let ((tags (plist-get msg :tags))
890 (orig-tags (plist-get msg :orig-tags))
892 'notmuch-tree-match-tag-face
893 'notmuch-tree-no-match-tag-face)))
894 (format format-string (notmuch-tag-format-tags tags orig-tags face)))))))
896 (defun notmuch-tree-format-field-list (field-list msg)
897 "Format fields of MSG according to FIELD-LIST and return string."
898 (let ((face (if (plist-get msg :match)
899 'notmuch-tree-match-face
900 'notmuch-tree-no-match-face))
902 (dolist (spec field-list result-string)
903 (let ((field-string (notmuch-tree-format-field (car spec) (cdr spec) msg)))
904 (setq result-string (concat result-string field-string))))
905 (notmuch-apply-face result-string face t)))
907 (defun notmuch-tree-insert-msg (msg)
908 "Insert the message MSG according to notmuch-tree-result-format."
909 ;; We need to save the previous subject as it will get overwritten
910 ;; by the insert-field calls.
911 (let ((previous-subject notmuch-tree-previous-subject))
912 (insert (notmuch-tree-format-field-list (notmuch-tree-result-format) msg))
913 (notmuch-tree-set-message-properties msg)
914 (notmuch-tree-set-prop :previous-subject previous-subject)
917 (defun notmuch-tree-goto-and-insert-msg (msg)
918 "Insert msg at the end of the buffer. Move point to msg if it is the target."
920 (goto-char (point-max))
921 (notmuch-tree-insert-msg msg))
922 (let ((msg-id (notmuch-id-to-query (plist-get msg :id)))
923 (target notmuch-tree-target-msg))
924 (when (or (and (not target) (plist-get msg :match))
925 (string= msg-id target))
926 (setq notmuch-tree-target-msg "found")
927 (goto-char (point-max))
929 (when notmuch-tree-open-target
930 (notmuch-tree-show-message-in)))))
932 (defun notmuch-tree-insert-tree (tree depth tree-status first last)
933 "Insert the message tree TREE at depth DEPTH in the current thread.
935 A message tree is another name for a single sub-thread: i.e., a
936 message together with all its descendents."
937 (let ((msg (car tree))
938 (replies (cadr tree)))
940 ((and (< 0 depth) (not last))
941 (push "├" tree-status))
942 ((and (< 0 depth) last)
943 (push "╰" tree-status))
944 ((and (eq 0 depth) first last)
945 ;; Choice between these two variants is a matter of taste.
946 ;; (push "─" tree-status))
947 (push " " tree-status))
948 ((and (eq 0 depth) first (not last))
949 (push "┬" tree-status))
950 ((and (eq 0 depth) (not first) last)
951 (push "╰" tree-status))
952 ((and (eq 0 depth) (not first) (not last))
953 (push "├" tree-status)))
954 (push (concat (if replies "┬" "─") "►") tree-status)
955 (setq msg (plist-put msg :first (and first (eq 0 depth))))
956 (setq msg (plist-put msg :tree-status tree-status))
957 (setq msg (plist-put msg :orig-tags (plist-get msg :tags)))
958 (notmuch-tree-goto-and-insert-msg msg)
962 (push " " tree-status)
963 (push "│" tree-status))
964 (notmuch-tree-insert-thread replies (1+ depth) tree-status)))
966 (defun notmuch-tree-insert-thread (thread depth tree-status)
967 "Insert the collection of sibling sub-threads THREAD at depth DEPTH in the current forest."
968 (let ((n (length thread)))
969 (cl-loop for tree in thread
970 for count from 1 to n
971 do (notmuch-tree-insert-tree tree depth tree-status
975 (defun notmuch-tree-insert-forest-thread (forest-thread)
976 "Insert a single complete thread."
978 ;; Reset at the start of each main thread.
979 (setq notmuch-tree-previous-subject nil)
980 (notmuch-tree-insert-thread forest-thread 0 tree-status)))
982 (defun notmuch-tree-insert-forest (forest)
983 "Insert a forest of threads.
985 This function inserts a collection of several complete threads as
986 passed to it by notmuch-tree-process-filter."
987 (mapc 'notmuch-tree-insert-forest-thread forest))
989 (define-derived-mode notmuch-tree-mode fundamental-mode "notmuch-tree"
990 "Major mode displaying messages (as opposed to threads) of a notmuch search.
992 This buffer contains the results of a \"notmuch tree\" of your
993 email archives. Each line in the buffer represents a single
994 message giving the relative date, the author, subject, and any
997 Pressing \\[notmuch-tree-show-message] on any line displays that message.
999 Complete list of currently available key bindings:
1001 \\{notmuch-tree-mode-map}"
1002 (setq notmuch-buffer-refresh-function #'notmuch-tree-refresh-view)
1004 (setq buffer-read-only t)
1005 (setq truncate-lines t))
1007 (defun notmuch-tree-process-sentinel (proc msg)
1008 "Add a message to let user know when \"notmuch tree\" exits."
1009 (let ((buffer (process-buffer proc))
1010 (status (process-status proc))
1011 (exit-status (process-exit-status proc))
1012 (never-found-target-thread nil))
1013 (when (memq status '(exit signal))
1014 (kill-buffer (process-get proc 'parse-buf))
1015 (when (buffer-live-p buffer)
1016 (with-current-buffer buffer
1018 (let ((inhibit-read-only t)
1020 (goto-char (point-max))
1021 (when (eq status 'signal)
1022 (insert "Incomplete search results (tree view process was killed).\n"))
1023 (when (eq status 'exit)
1024 (insert "End of search results.")
1025 (unless (= exit-status 0)
1026 (insert (format " (process returned %d)" exit-status)))
1027 (insert "\n")))))))))
1029 (defun notmuch-tree-process-filter (proc string)
1030 "Process and filter the output of \"notmuch show\" for tree view."
1031 (let ((results-buf (process-buffer proc))
1032 (parse-buf (process-get proc 'parse-buf))
1033 (inhibit-read-only t)
1035 (if (not (buffer-live-p results-buf))
1036 (delete-process proc)
1037 (with-current-buffer parse-buf
1040 (goto-char (point-max))
1042 (notmuch-sexp-parse-partial-list 'notmuch-tree-insert-forest-thread
1045 (defun notmuch-tree-worker (basic-query &optional query-context target open-target unthreaded)
1046 "Insert the tree view of the search in the current buffer.
1048 This is is a helper function for notmuch-tree. The arguments are
1049 the same as for the function notmuch-tree."
1052 (add-hook 'post-command-hook #'notmuch-tree-command-hook t t)
1053 (setq notmuch-tree-unthreaded unthreaded)
1054 (setq notmuch-tree-basic-query basic-query)
1055 (setq notmuch-tree-query-context (if (or (string= query-context "")
1056 (string= query-context "*"))
1059 (setq notmuch-tree-target-msg target)
1060 (setq notmuch-tree-open-target open-target)
1061 ;; Set the default value for `notmuch-show-process-crypto' in this
1062 ;; buffer. Although we don't use this some of the functions we call
1063 ;; (such as reply) do. It is a buffer local variable so setting it
1064 ;; will not affect genuine show buffers.
1065 (setq notmuch-show-process-crypto notmuch-crypto-process-mime)
1067 (goto-char (point-min))
1068 (let* ((search-args (concat basic-query
1070 (concat " and (" query-context ")"))))
1071 (message-arg (if unthreaded "--unthreaded" "--entire-thread")))
1072 (when (equal (car (process-lines notmuch-command "count" search-args)) "0")
1073 (setq search-args basic-query))
1074 (notmuch-tag-clear-cache)
1075 (let ((proc (notmuch-start-notmuch
1076 "notmuch-tree" (current-buffer) #'notmuch-tree-process-sentinel
1077 "show" "--body=false" "--format=sexp" "--format-version=4"
1078 message-arg search-args))
1079 ;; Use a scratch buffer to accumulate partial output.
1080 ;; This buffer will be killed by the sentinel, which
1081 ;; should be called no matter how the process dies.
1082 (parse-buf (generate-new-buffer " *notmuch tree parse*")))
1083 (process-put proc 'parse-buf parse-buf)
1084 (set-process-filter proc 'notmuch-tree-process-filter)
1085 (set-process-query-on-exit-flag proc nil))))
1087 (defun notmuch-tree-get-query ()
1088 "Return the current query in this tree buffer."
1089 (if notmuch-tree-query-context
1090 (concat notmuch-tree-basic-query
1092 notmuch-tree-query-context
1094 notmuch-tree-basic-query))
1096 (defun notmuch-tree (&optional query query-context target buffer-name open-target unthreaded parent-buffer)
1097 "Display threads matching QUERY in tree view.
1100 QUERY: the main query. This can be any query but in many cases will be
1101 a single thread. If nil this is read interactively from the minibuffer.
1102 QUERY-CONTEXT: is an additional term for the query. The query used
1103 is QUERY and QUERY-CONTEXT unless that does not match any messages
1104 in which case we fall back to just QUERY.
1105 TARGET: A message ID (with the id: prefix) that will be made
1106 current if it appears in the tree view results.
1107 BUFFER-NAME: the name of the buffer to display the tree view. If
1108 it is nil \"*notmuch-tree\" followed by QUERY is used.
1109 OPEN-TARGET: If TRUE open the target message in the message pane.
1110 UNTHREADED: If TRUE only show matching messages in an unthreaded view."
1113 (setq query (notmuch-read-query (concat "Notmuch "
1114 (if unthreaded "unthreaded " "tree ")
1116 (let ((buffer (get-buffer-create (generate-new-buffer-name
1119 (if unthreaded "unthreaded-" "tree-")
1121 (inhibit-read-only t))
1122 (switch-to-buffer buffer))
1123 ;; Don't track undo information for this buffer
1124 (set 'buffer-undo-list t)
1125 (notmuch-tree-worker query query-context target open-target unthreaded)
1126 (setq notmuch-tree-parent-buffer parent-buffer)
1127 (setq truncate-lines t))
1129 (defun notmuch-unthreaded (&optional query query-context target buffer-name open-target)
1131 (notmuch-tree query query-context target buffer-name open-target t))
1135 (provide 'notmuch-tree)
1137 ;;; notmuch-tree.el ends here