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)
37 (declare-function notmuch-search "notmuch" (&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 ;; the following variable is defined in notmuch.el
44 (defvar notmuch-search-query-string)
46 ;; this variable distinguishes the unthreaded display from the normal tree display
47 (defvar notmuch-tree-unthreaded nil
48 "A buffer local copy of argument unthreaded to the function notmuch-tree.")
49 (make-variable-buffer-local 'notmuch-tree-unthreaded)
51 (defgroup notmuch-tree nil
52 "Showing message and thread structure."
55 (defcustom notmuch-tree-show-out nil
56 "View selected messages in new window rather than split-pane."
60 (defcustom notmuch-unthreaded-show-out t
61 "View selected messages in new window rather than split-pane."
65 (defun notmuch-tree-show-out ()
66 (if notmuch-tree-unthreaded
67 notmuch-unthreaded-show-out
68 notmuch-tree-show-out))
70 (defcustom notmuch-tree-result-format
73 ((("tree" . "%s")("subject" . "%s")) ." %-54s ")
75 "Result formatting for tree view. Supported fields are: date,
76 authors, subject, tree, tags. Tree means the thread tree
77 box graphics. The field may also be a list in which case
78 the formatting rules are applied recursively and then the
79 output of all the fields in the list is inserted
80 according to format-string.
82 Note the author string should not contain
83 whitespace (put it in the neighbouring fields instead).
85 (setq notmuch-tree-result-format \(\(\"authors\" . \"%-40s\"\)
86 \(\"subject\" . \"%s\"\)\)\)"
87 :type '(alist :key-type (string) :value-type (string))
90 (defcustom notmuch-unthreaded-result-format
93 ((("subject" . "%s")) ." %-54s ")
95 "Result formatting for unthreaded tree view. Supported fields are: date,
96 authors, subject, tree, tags. Tree means the thread tree
97 box graphics. The field may also be a list in which case
98 the formatting rules are applied recursively and then the
99 output of all the fields in the list is inserted
100 according to format-string.
102 Note the author string should not contain
103 whitespace (put it in the neighbouring fields instead).
105 (setq notmuch-tree-result-format \(\(\"authors\" . \"%-40s\"\)
106 \(\"subject\" . \"%s\"\)\)\)"
107 :type '(alist :key-type (string) :value-type (string))
108 :group 'notmuch-tree)
110 (defun notmuch-tree-result-format ()
111 (if notmuch-tree-unthreaded
112 notmuch-unthreaded-result-format
113 notmuch-tree-result-format))
115 ;; Faces for messages that match the query.
116 (defface notmuch-tree-match-face
117 '((t :inherit default))
118 "Default face used in tree mode face for matching messages"
120 :group 'notmuch-faces)
122 (defface notmuch-tree-match-date-face
124 "Face used in tree mode for the date in messages matching the query."
126 :group 'notmuch-faces)
128 (defface notmuch-tree-match-author-face
131 (:foreground "OliveDrab1"))
134 (:foreground "dark blue"))
137 "Face used in tree mode for the date in messages matching the query."
139 :group 'notmuch-faces)
141 (defface notmuch-tree-match-subject-face
143 "Face used in tree mode for the subject in messages matching the query."
145 :group 'notmuch-faces)
147 (defface notmuch-tree-match-tree-face
149 "Face used in tree mode for the thread tree block graphics in messages matching the query."
151 :group 'notmuch-faces)
153 (defface notmuch-tree-match-tag-face
156 (:foreground "OliveDrab1"))
159 (:foreground "navy blue" :bold t))
162 "Face used in tree mode for tags in messages matching the query."
164 :group 'notmuch-faces)
166 ;; Faces for messages that do not match the query.
167 (defface notmuch-tree-no-match-face
168 '((t (:foreground "gray")))
169 "Default face used in tree mode face for non-matching messages."
171 :group 'notmuch-faces)
173 (defface notmuch-tree-no-match-date-face
175 "Face used in tree mode for non-matching dates."
177 :group 'notmuch-faces)
179 (defface notmuch-tree-no-match-subject-face
181 "Face used in tree mode for non-matching subjects."
183 :group 'notmuch-faces)
185 (defface notmuch-tree-no-match-tree-face
187 "Face used in tree mode for the thread tree block graphics in messages matching the query."
189 :group 'notmuch-faces)
191 (defface notmuch-tree-no-match-author-face
193 "Face used in tree mode for the date in messages matching the query."
195 :group 'notmuch-faces)
197 (defface notmuch-tree-no-match-tag-face
199 "Face used in tree mode face for non-matching tags."
201 :group 'notmuch-faces)
203 (defvar notmuch-tree-previous-subject
204 "The subject of the most recent result shown during the async display.")
205 (make-variable-buffer-local 'notmuch-tree-previous-subject)
207 (defvar notmuch-tree-basic-query nil
208 "A buffer local copy of argument query to the function notmuch-tree.")
209 (make-variable-buffer-local 'notmuch-tree-basic-query)
211 (defvar notmuch-tree-query-context nil
212 "A buffer local copy of argument query-context to the function notmuch-tree.")
213 (make-variable-buffer-local 'notmuch-tree-query-context)
215 (defvar notmuch-tree-target-msg nil
216 "A buffer local copy of argument target to the function notmuch-tree.")
217 (make-variable-buffer-local 'notmuch-tree-target-msg)
219 (defvar notmuch-tree-open-target nil
220 "A buffer local copy of argument open-target to the function notmuch-tree.")
221 (make-variable-buffer-local 'notmuch-tree-open-target)
223 (defvar notmuch-tree-message-window nil
224 "The window of the message pane.
226 It is set in both the tree buffer and the child show buffer. It
227 is used to try and close the message pane when quitting tree view
228 or the child show buffer.")
229 (make-variable-buffer-local 'notmuch-tree-message-window)
230 (put 'notmuch-tree-message-window 'permanent-local t)
232 (defvar notmuch-tree-message-buffer nil
233 "The buffer name of the show buffer in the message pane.
235 This is used to try and make sure we don't close the message pane
236 if the user has loaded a different buffer in that window.")
237 (make-variable-buffer-local 'notmuch-tree-message-buffer)
238 (put 'notmuch-tree-message-buffer 'permanent-local t)
240 (defun notmuch-tree-to-message-pane (func)
241 "Execute FUNC in message pane.
243 This function returns a function (so can be used as a keybinding)
244 which executes function FUNC in the message pane if it is
245 open (if the message pane is closed it does nothing)."
247 ,(concat "(In message pane) " (documentation func t))
249 (when (window-live-p notmuch-tree-message-window)
250 (with-selected-window notmuch-tree-message-window
251 (call-interactively #',func)))))
253 (defun notmuch-tree-inherit-from-message-pane (sym)
254 "Return value of SYM in message-pane if open, or tree-pane if not."
255 (if (window-live-p notmuch-tree-message-window)
256 (with-selected-window notmuch-tree-message-window
260 (defun notmuch-tree-button-activate (&optional button)
261 "Activate BUTTON or button at point.
263 This function does not give an error if there is no button."
265 (let ((button (or button (button-at (point)))))
266 (when button (button-activate button))))
268 (defun notmuch-tree-close-message-pane-and (func)
269 "Close message pane and execute FUNC.
271 This function returns a function (so can be used as a keybinding)
272 which closes the message pane if open and then executes function
275 ,(concat "(Close message pane and) " (documentation func t))
277 (let ((notmuch-show-process-crypto
278 (notmuch-tree-inherit-from-message-pane 'notmuch-show-process-crypto)))
279 (notmuch-tree-close-message-window)
280 (call-interactively #',func))))
282 (defvar notmuch-tree-mode-map
283 (let ((map (make-sparse-keymap)))
284 (set-keymap-parent map notmuch-common-keymap)
285 ;; The following override the global keymap.
286 ;; Override because we want to close message pane first.
287 (define-key map [remap notmuch-help] (notmuch-tree-close-message-pane-and #'notmuch-help))
288 ;; Override because we first close message pane and then close tree buffer.
289 (define-key map [remap notmuch-bury-or-kill-this-buffer] 'notmuch-tree-quit)
290 ;; Override because we close message pane after the search query is entered.
291 (define-key map [remap notmuch-search] 'notmuch-tree-to-search)
292 ;; Override because we want to close message pane first.
293 (define-key map [remap notmuch-mua-new-mail] (notmuch-tree-close-message-pane-and #'notmuch-mua-new-mail))
294 ;; Override because we want to close message pane first.
295 (define-key map [remap notmuch-jump-search] (notmuch-tree-close-message-pane-and #'notmuch-jump-search))
297 (define-key map "S" 'notmuch-search-from-tree-current-query)
298 (define-key map "U" 'notmuch-unthreaded-from-tree-current-query)
299 (define-key map "Z" 'notmuch-tree-from-unthreaded-current-query)
301 ;; these use notmuch-show functions directly
302 (define-key map "|" 'notmuch-show-pipe-message)
303 (define-key map "w" 'notmuch-show-save-attachments)
304 (define-key map "v" 'notmuch-show-view-all-mime-parts)
305 (define-key map "c" 'notmuch-show-stash-map)
306 (define-key map "b" 'notmuch-show-resend-message)
308 ;; these apply to the message pane
309 (define-key map (kbd "M-TAB") (notmuch-tree-to-message-pane #'notmuch-show-previous-button))
310 (define-key map (kbd "<backtab>") (notmuch-tree-to-message-pane #'notmuch-show-previous-button))
311 (define-key map (kbd "TAB") (notmuch-tree-to-message-pane #'notmuch-show-next-button))
312 (define-key map "$" (notmuch-tree-to-message-pane #'notmuch-show-toggle-process-crypto))
314 ;; bindings from show (or elsewhere) but we close the message pane first.
315 (define-key map "f" (notmuch-tree-close-message-pane-and #'notmuch-show-forward-message))
316 (define-key map "r" (notmuch-tree-close-message-pane-and #'notmuch-show-reply-sender))
317 (define-key map "R" (notmuch-tree-close-message-pane-and #'notmuch-show-reply))
318 (define-key map "V" (notmuch-tree-close-message-pane-and #'notmuch-show-view-raw-message))
320 ;; The main tree view bindings
321 (define-key map (kbd "RET") 'notmuch-tree-show-message)
322 (define-key map [mouse-1] 'notmuch-tree-show-message)
323 (define-key map "x" 'notmuch-tree-archive-message-then-next-or-exit)
324 (define-key map "X" 'notmuch-tree-archive-thread-then-exit)
325 (define-key map "A" 'notmuch-tree-archive-thread)
326 (define-key map "a" 'notmuch-tree-archive-message-then-next)
327 (define-key map "z" 'notmuch-tree-to-tree)
328 (define-key map "n" 'notmuch-tree-next-matching-message)
329 (define-key map "p" 'notmuch-tree-prev-matching-message)
330 (define-key map "N" 'notmuch-tree-next-message)
331 (define-key map "P" 'notmuch-tree-prev-message)
332 (define-key map (kbd "M-p") 'notmuch-tree-prev-thread)
333 (define-key map (kbd "M-n") 'notmuch-tree-next-thread)
334 (define-key map "k" 'notmuch-tag-jump)
335 (define-key map "-" 'notmuch-tree-remove-tag)
336 (define-key map "+" 'notmuch-tree-add-tag)
337 (define-key map "*" 'notmuch-tree-tag-thread)
338 (define-key map " " 'notmuch-tree-scroll-or-next)
339 (define-key map (kbd "DEL") 'notmuch-tree-scroll-message-window-back)
340 (define-key map "e" 'notmuch-tree-resume-message)
342 (fset 'notmuch-tree-mode-map notmuch-tree-mode-map)
344 (defun notmuch-tree-get-message-properties ()
345 "Return the properties of the current message as a plist.
347 Some useful entries are:
348 :headers - Property list containing the headers :Date, :Subject, :From, etc.
349 :tags - Tags for this message."
352 (get-text-property (point) :notmuch-message-properties)))
354 (defun notmuch-tree-set-message-properties (props)
357 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
359 (defun notmuch-tree-set-prop (prop val &optional props)
360 (let ((inhibit-read-only t)
362 (notmuch-tree-get-message-properties))))
363 (plist-put props prop val)
364 (notmuch-tree-set-message-properties props)))
366 (defun notmuch-tree-get-prop (prop &optional props)
367 (let ((props (or props
368 (notmuch-tree-get-message-properties))))
369 (plist-get props prop)))
371 (defun notmuch-tree-set-tags (tags)
372 "Set the tags of the current message."
373 (notmuch-tree-set-prop :tags tags))
375 (defun notmuch-tree-get-tags ()
376 "Return the tags of the current message."
377 (notmuch-tree-get-prop :tags))
379 (defun notmuch-tree-get-message-id (&optional bare)
380 "Return the message id of the current message."
381 (let ((id (notmuch-tree-get-prop :id)))
385 (notmuch-id-to-query id))
388 (defun notmuch-tree-get-match ()
389 "Return whether the current message is a match."
391 (notmuch-tree-get-prop :match))
393 (defun notmuch-tree-refresh-result ()
394 "Redisplay the current message line.
396 This redisplays the current line based on the messages
397 properties (as they are now). This is used when tags are
399 (let ((init-point (point))
400 (end (line-end-position))
401 (msg (notmuch-tree-get-message-properties))
402 (inhibit-read-only t))
404 ;; This is a little tricky: we override
405 ;; notmuch-tree-previous-subject to get the decision between
406 ;; ... and a subject right and it stops notmuch-tree-insert-msg
407 ;; from overwriting the buffer local copy of
408 ;; notmuch-tree-previous-subject if this is called while the
409 ;; buffer is displaying.
410 (let ((notmuch-tree-previous-subject (notmuch-tree-get-prop :previous-subject)))
411 (delete-region (point) (1+ (line-end-position)))
412 (notmuch-tree-insert-msg msg))
413 (let ((new-end (line-end-position)))
414 (goto-char (if (= init-point end)
416 (min init-point (- new-end 1)))))))
418 (defun notmuch-tree-tag-update-display (&optional tag-changes)
419 "Update display for TAG-CHANGES to current message.
421 Updates the message in the message pane if appropriate, but does
422 NOT change the database."
423 (let* ((current-tags (notmuch-tree-get-tags))
424 (new-tags (notmuch-update-tags current-tags tag-changes))
425 (tree-msg-id (notmuch-tree-get-message-id)))
426 (unless (equal current-tags new-tags)
427 (notmuch-tree-set-tags new-tags)
428 (notmuch-tree-refresh-result)
429 (when (window-live-p notmuch-tree-message-window)
430 (with-selected-window notmuch-tree-message-window
431 (when (string= tree-msg-id (notmuch-show-get-message-id))
432 (notmuch-show-update-tags new-tags)))))))
434 (defun notmuch-tree-tag (tag-changes)
435 "Change tags for the current message."
437 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message")))
438 (notmuch-tag (notmuch-tree-get-message-id) tag-changes)
439 (notmuch-tree-tag-update-display tag-changes))
441 (defun notmuch-tree-add-tag (tag-changes)
442 "Same as `notmuch-tree-tag' but sets initial input to '+'."
444 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "+")))
445 (notmuch-tree-tag tag-changes))
447 (defun notmuch-tree-remove-tag (tag-changes)
448 "Same as `notmuch-tree-tag' but sets initial input to '-'."
450 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "-")))
451 (notmuch-tree-tag tag-changes))
453 (defun notmuch-tree-resume-message ()
454 "Resume EDITING the current draft message."
456 (notmuch-tree-close-message-window)
457 (let ((id (notmuch-tree-get-message-id)))
459 (notmuch-draft-resume id)
460 (message "No message to resume!"))))
462 ;; The next two functions close the message window before calling
463 ;; notmuch-search or notmuch-tree but they do so after the user has
464 ;; entered the query (in case the user was basing the query on
465 ;; something in the message window).
467 (defun notmuch-tree-to-search ()
468 "Run \"notmuch search\" with the given `query' and display results."
470 (let ((query (notmuch-read-query "Notmuch search: ")))
471 (notmuch-tree-close-message-window)
472 (notmuch-search query)))
474 (defun notmuch-tree-to-tree ()
475 "Run a query and display results in tree view."
477 (let ((query (notmuch-read-query "Notmuch tree view search: ")))
478 (notmuch-tree-close-message-window)
479 (notmuch-tree query)))
481 (defun notmuch-unthreaded-from-tree-current-query ()
482 "Switch from tree view to unthreaded view."
484 (unless notmuch-tree-unthreaded
485 (notmuch-tree-refresh-view 'unthreaded)))
487 (defun notmuch-tree-from-unthreaded-current-query ()
488 "Switch from unthreaded view to tree view."
490 (when notmuch-tree-unthreaded
491 (notmuch-tree-refresh-view 'tree)))
493 (defun notmuch-search-from-tree-current-query ()
494 "Call notmuch search with the current query."
496 (notmuch-tree-close-message-window)
497 (notmuch-search (notmuch-tree-get-query)))
499 (defun notmuch-tree-message-window-kill-hook ()
500 "Close the message pane when exiting the show buffer."
501 (let ((buffer (current-buffer)))
502 (when (and (window-live-p notmuch-tree-message-window)
503 (eq (window-buffer notmuch-tree-message-window) buffer))
504 ;; We do not want an error if this is the sole window in the
505 ;; frame and I do not know how to test for that in emacs pre
506 ;; 24. Hence we just ignore-errors.
508 (delete-window notmuch-tree-message-window)))))
510 (defun notmuch-tree-command-hook ()
511 (when (eq major-mode 'notmuch-tree-mode)
512 ;; We just run the notmuch-show-command-hook on the message pane.
513 (when (buffer-live-p notmuch-tree-message-buffer)
514 (with-current-buffer notmuch-tree-message-buffer
515 (notmuch-show-command-hook)))))
517 (defun notmuch-tree-show-message-in ()
518 "Show the current message (in split-pane)."
520 (let ((id (notmuch-tree-get-message-id))
521 (inhibit-read-only t)
524 ;; We close and reopen the window to kill off un-needed buffers
525 ;; this might cause flickering but seems ok.
526 (notmuch-tree-close-message-window)
527 (setq notmuch-tree-message-window
528 (split-window-vertically (/ (window-height) 4)))
529 (with-selected-window notmuch-tree-message-window
530 ;; Since we are only displaying one message do not indent.
531 (let ((notmuch-show-indent-messages-width 0)
532 (notmuch-show-only-matching-messages t))
533 (setq buffer (notmuch-show id))))
534 ;; We need the `let' as notmuch-tree-message-window is buffer local.
535 (let ((window notmuch-tree-message-window))
536 (with-current-buffer buffer
537 (setq notmuch-tree-message-window window)
538 (add-hook 'kill-buffer-hook 'notmuch-tree-message-window-kill-hook)))
539 (when notmuch-show-mark-read-tags
540 (notmuch-tree-tag-update-display notmuch-show-mark-read-tags))
541 (setq notmuch-tree-message-buffer buffer))))
543 (defun notmuch-tree-show-message-out ()
544 "Show the current message (in whole window)."
546 (let ((id (notmuch-tree-get-message-id))
547 (inhibit-read-only t)
550 ;; We close the window to kill off un-needed buffers.
551 (notmuch-tree-close-message-window)
554 (defun notmuch-tree-show-message (arg)
555 "Show the current message.
557 Shows in split pane or whole window according to value of
558 `notmuch-tree-show-out'. A prefix argument reverses the choice."
560 (if (or (and (notmuch-tree-show-out) (not arg))
561 (and (not (notmuch-tree-show-out)) arg))
562 (notmuch-tree-show-message-out)
563 (notmuch-tree-show-message-in)))
565 (defun notmuch-tree-scroll-message-window ()
566 "Scroll the message window (if it exists)."
568 (when (window-live-p notmuch-tree-message-window)
569 (with-selected-window notmuch-tree-message-window
570 (if (pos-visible-in-window-p (point-max))
574 (defun notmuch-tree-scroll-message-window-back ()
575 "Scroll the message window back(if it exists)."
577 (when (window-live-p notmuch-tree-message-window)
578 (with-selected-window notmuch-tree-message-window
579 (if (pos-visible-in-window-p (point-min))
583 (defun notmuch-tree-scroll-or-next ()
584 "Scroll the message window. If it at end go to next message."
586 (when (notmuch-tree-scroll-message-window)
587 (notmuch-tree-next-matching-message)))
589 (defun notmuch-tree-quit (&optional kill-both)
590 "Close the split view or exit tree."
592 (when (or (not (notmuch-tree-close-message-window)) kill-both)
593 (kill-buffer (current-buffer))))
595 (defun notmuch-tree-close-message-window ()
596 "Close the message-window. Return t if close succeeds."
598 (when (and (window-live-p notmuch-tree-message-window)
599 (eq (window-buffer notmuch-tree-message-window) notmuch-tree-message-buffer))
600 (delete-window notmuch-tree-message-window)
601 (unless (get-buffer-window-list notmuch-tree-message-buffer)
602 (kill-buffer notmuch-tree-message-buffer))
605 (defun notmuch-tree-archive-message (&optional unarchive)
606 "Archive the current message.
608 Archive the current message by applying the tag changes in
609 `notmuch-archive-tags' to it. If a prefix argument is given, the
610 message will be \"unarchived\", i.e. the tag changes in
611 `notmuch-archive-tags' will be reversed."
613 (when notmuch-archive-tags
614 (notmuch-tree-tag (notmuch-tag-change-list notmuch-archive-tags unarchive))))
616 (defun notmuch-tree-archive-message-then-next (&optional unarchive)
617 "Archive the current message and move to next matching message."
619 (notmuch-tree-archive-message unarchive)
620 (notmuch-tree-next-matching-message))
622 (defun notmuch-tree-archive-thread-then-exit ()
623 "Archive all messages in the current buffer, then exit notmuch-tree."
625 (notmuch-tree-archive-thread)
626 (notmuch-tree-quit t))
628 (defun notmuch-tree-archive-message-then-next-or-exit ()
629 "Archive current message, then show next open message in current thread.
631 If at the last open message in the current thread, then exit back
634 (notmuch-tree-archive-message)
635 (notmuch-tree-next-matching-message t))
637 (defun notmuch-tree-next-message ()
638 "Move to next message."
641 (when (window-live-p notmuch-tree-message-window)
642 (notmuch-tree-show-message-in)))
644 (defun notmuch-tree-prev-message ()
645 "Move to previous message."
648 (when (window-live-p notmuch-tree-message-window)
649 (notmuch-tree-show-message-in)))
651 (defun notmuch-tree-goto-matching-message (&optional prev)
652 "Move to the next or previous matching message.
654 Returns t if there was a next matching message in the thread to show,
656 (let ((dir (if prev -1 nil))
657 (eobfn (if prev #'bobp #'eobp)))
658 (while (and (not (funcall eobfn))
659 (not (notmuch-tree-get-match)))
661 (not (funcall eobfn))))
663 (defun notmuch-tree-matching-message (&optional prev pop-at-end)
664 "Move to the next or previous matching message."
666 (forward-line (if prev -1 nil))
667 (if (and (not (notmuch-tree-goto-matching-message prev)) pop-at-end)
668 (notmuch-tree-quit pop-at-end)
669 (when (window-live-p notmuch-tree-message-window)
670 (notmuch-tree-show-message-in))))
672 (defun notmuch-tree-prev-matching-message (&optional pop-at-end)
673 "Move to previous matching message."
675 (notmuch-tree-matching-message t pop-at-end))
677 (defun notmuch-tree-next-matching-message (&optional pop-at-end)
678 "Move to next matching message."
680 (notmuch-tree-matching-message nil pop-at-end))
682 (defun notmuch-tree-refresh-view (&optional view)
685 (when (get-buffer-process (current-buffer))
686 (error "notmuch tree process already running for current buffer"))
687 (let ((inhibit-read-only t)
688 (basic-query notmuch-tree-basic-query)
689 (unthreaded (cond ((eq view 'unthreaded) t)
690 ((eq view 'tree) nil)
691 (t notmuch-tree-unthreaded)))
692 (query-context notmuch-tree-query-context)
693 (target (notmuch-tree-get-message-id)))
695 (notmuch-tree-worker basic-query
701 (defun notmuch-tree-thread-top ()
702 (when (notmuch-tree-get-message-properties)
703 (while (not (or (notmuch-tree-get-prop :first) (eobp)))
706 (defun notmuch-tree-prev-thread ()
709 (notmuch-tree-thread-top))
711 (defun notmuch-tree-next-thread ()
712 "Get the next thread in the current tree. Returns t if a thread was
713 found or nil if not."
716 (while (not (or (notmuch-tree-get-prop :first) (eobp)))
720 (defun notmuch-tree-thread-mapcar (function)
721 "Iterate through all messages in the current thread
722 and call FUNCTION for side effects."
724 (notmuch-tree-thread-top)
725 (cl-loop collect (funcall function)
727 while (and (notmuch-tree-get-message-properties)
728 (not (notmuch-tree-get-prop :first))))))
730 (defun notmuch-tree-get-messages-ids-thread-search ()
731 "Return a search string for all message ids of messages in the current thread."
733 (notmuch-tree-thread-mapcar 'notmuch-tree-get-message-id)
736 (defun notmuch-tree-tag-thread (tag-changes)
737 "Tag all messages in the current thread."
739 (let ((tags (apply #'append (notmuch-tree-thread-mapcar
740 (lambda () (notmuch-tree-get-tags))))))
741 (list (notmuch-read-tag-changes tags "Tag thread"))))
742 (when (notmuch-tree-get-message-properties)
743 (notmuch-tag (notmuch-tree-get-messages-ids-thread-search) tag-changes)
744 (notmuch-tree-thread-mapcar
745 (lambda () (notmuch-tree-tag-update-display tag-changes)))))
747 (defun notmuch-tree-archive-thread (&optional unarchive)
748 "Archive each message in thread.
750 Archive each message currently shown by applying the tag changes
751 in `notmuch-archive-tags' to each. If a prefix argument is given,
752 the messages will be \"unarchived\", i.e. the tag changes in
753 `notmuch-archive-tags' will be reversed.
755 Note: This command is safe from any race condition of new messages
756 being delivered to the same thread. It does not archive the
757 entire thread, but only the messages shown in the current
760 (when notmuch-archive-tags
761 (notmuch-tree-tag-thread
762 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
764 ;; Functions below here display the tree buffer itself.
766 (defun notmuch-tree-clean-address (address)
767 "Try to clean a single email ADDRESS for display. Return
768 AUTHOR_NAME if present, otherwise return AUTHOR_EMAIL. Return
769 unchanged ADDRESS if parsing fails."
770 (let* ((clean-address (notmuch-clean-address address))
771 (p-address (car clean-address))
772 (p-name (cdr clean-address)))
774 ;; If we have a name return that otherwise return the address.
775 (or p-name p-address)))
777 (defun notmuch-tree-format-field (field format-string msg)
778 "Format a FIELD of MSG according to FORMAT-STRING and return string."
779 (let* ((headers (plist-get msg :headers))
780 (match (plist-get msg :match)))
783 (format format-string (notmuch-tree-format-field-list field msg)))
785 ((string-equal field "date")
786 (let ((face (if match
787 'notmuch-tree-match-date-face
788 'notmuch-tree-no-match-date-face)))
789 (propertize (format format-string (plist-get msg :date_relative)) 'face face)))
791 ((string-equal field "tree")
792 (let ((tree-status (plist-get msg :tree-status))
794 'notmuch-tree-match-tree-face
795 'notmuch-tree-no-match-tree-face)))
797 (propertize (format format-string
798 (mapconcat #'identity (reverse tree-status) ""))
801 ((string-equal field "subject")
802 (let ((bare-subject (notmuch-show-strip-re (plist-get headers :Subject)))
803 (previous-subject notmuch-tree-previous-subject)
805 'notmuch-tree-match-subject-face
806 'notmuch-tree-no-match-subject-face)))
808 (setq notmuch-tree-previous-subject bare-subject)
809 (propertize (format format-string
810 (if (string= previous-subject bare-subject)
815 ((string-equal field "authors")
816 (let ((author (notmuch-tree-clean-address (plist-get headers :From)))
817 (len (length (format format-string "")))
819 'notmuch-tree-match-author-face
820 'notmuch-tree-no-match-author-face)))
821 (when (> (length author) len)
822 (setq author (substring author 0 len)))
823 (propertize (format format-string author) 'face face)))
825 ((string-equal field "tags")
826 (let ((tags (plist-get msg :tags))
827 (orig-tags (plist-get msg :orig-tags))
829 'notmuch-tree-match-tag-face
830 'notmuch-tree-no-match-tag-face)))
831 (format format-string (notmuch-tag-format-tags tags orig-tags face)))))))
833 (defun notmuch-tree-format-field-list (field-list msg)
834 "Format fields of MSG according to FIELD-LIST and return string."
835 (let ((face (if (plist-get msg :match)
836 'notmuch-tree-match-face
837 'notmuch-tree-no-match-face))
839 (dolist (spec field-list result-string)
840 (let ((field-string (notmuch-tree-format-field (car spec) (cdr spec) msg)))
841 (setq result-string (concat result-string field-string))))
842 (notmuch-apply-face result-string face t)))
844 (defun notmuch-tree-insert-msg (msg)
845 "Insert the message MSG according to notmuch-tree-result-format."
846 ;; We need to save the previous subject as it will get overwritten
847 ;; by the insert-field calls.
848 (let ((previous-subject notmuch-tree-previous-subject))
849 (insert (notmuch-tree-format-field-list (notmuch-tree-result-format) msg))
850 (notmuch-tree-set-message-properties msg)
851 (notmuch-tree-set-prop :previous-subject previous-subject)
854 (defun notmuch-tree-goto-and-insert-msg (msg)
855 "Insert msg at the end of the buffer. Move point to msg if it is the target."
857 (goto-char (point-max))
858 (notmuch-tree-insert-msg msg))
859 (let ((msg-id (notmuch-id-to-query (plist-get msg :id)))
860 (target notmuch-tree-target-msg))
861 (when (or (and (not target) (plist-get msg :match))
862 (string= msg-id target))
863 (setq notmuch-tree-target-msg "found")
864 (goto-char (point-max))
866 (when notmuch-tree-open-target
867 (notmuch-tree-show-message-in)))))
869 (defun notmuch-tree-insert-tree (tree depth tree-status first last)
870 "Insert the message tree TREE at depth DEPTH in the current thread.
872 A message tree is another name for a single sub-thread: i.e., a
873 message together with all its descendents."
874 (let ((msg (car tree))
875 (replies (cadr tree)))
878 ((and (< 0 depth) (not last))
879 (push "├" tree-status))
880 ((and (< 0 depth) last)
881 (push "╰" tree-status))
882 ((and (eq 0 depth) first last)
883 ;; (push "─" tree-status)) choice between this and next line is matter of taste.
884 (push " " tree-status))
885 ((and (eq 0 depth) first (not last))
886 (push "┬" tree-status))
887 ((and (eq 0 depth) (not first) last)
888 (push "╰" tree-status))
889 ((and (eq 0 depth) (not first) (not last))
890 (push "├" tree-status)))
892 (push (concat (if replies "┬" "─") "►") tree-status)
893 (setq msg (plist-put msg :first (and first (eq 0 depth))))
894 (setq msg (plist-put msg :tree-status tree-status))
895 (setq msg (plist-put msg :orig-tags (plist-get msg :tags)))
896 (notmuch-tree-goto-and-insert-msg msg)
901 (push " " tree-status)
902 (push "│" tree-status))
904 (notmuch-tree-insert-thread replies (1+ depth) tree-status)))
906 (defun notmuch-tree-insert-thread (thread depth tree-status)
907 "Insert the collection of sibling sub-threads THREAD at depth DEPTH in the current forest."
908 (let ((n (length thread)))
909 (cl-loop for tree in thread
910 for count from 1 to n
911 do (notmuch-tree-insert-tree tree depth tree-status
915 (defun notmuch-tree-insert-forest-thread (forest-thread)
916 "Insert a single complete thread."
918 ;; Reset at the start of each main thread.
919 (setq notmuch-tree-previous-subject nil)
920 (notmuch-tree-insert-thread forest-thread 0 tree-status)))
922 (defun notmuch-tree-insert-forest (forest)
923 "Insert a forest of threads.
925 This function inserts a collection of several complete threads as
926 passed to it by notmuch-tree-process-filter."
927 (mapc 'notmuch-tree-insert-forest-thread forest))
929 (define-derived-mode notmuch-tree-mode fundamental-mode "notmuch-tree"
930 "Major mode displaying messages (as opposed to threads) of a notmuch search.
932 This buffer contains the results of a \"notmuch tree\" of your
933 email archives. Each line in the buffer represents a single
934 message giving the relative date, the author, subject, and any
937 Pressing \\[notmuch-tree-show-message] on any line displays that message.
939 Complete list of currently available key bindings:
941 \\{notmuch-tree-mode-map}"
943 (setq notmuch-buffer-refresh-function #'notmuch-tree-refresh-view)
945 (setq buffer-read-only t
948 (defun notmuch-tree-process-sentinel (proc msg)
949 "Add a message to let user know when \"notmuch tree\" exits."
950 (let ((buffer (process-buffer proc))
951 (status (process-status proc))
952 (exit-status (process-exit-status proc))
953 (never-found-target-thread nil))
954 (when (memq status '(exit signal))
955 (kill-buffer (process-get proc 'parse-buf))
956 (if (buffer-live-p buffer)
957 (with-current-buffer buffer
959 (let ((inhibit-read-only t)
961 (goto-char (point-max))
962 (if (eq status 'signal)
963 (insert "Incomplete search results (tree view process was killed).\n"))
964 (when (eq status 'exit)
965 (insert "End of search results.")
966 (unless (= exit-status 0)
967 (insert (format " (process returned %d)" exit-status)))
968 (insert "\n")))))))))
970 (defun notmuch-tree-process-filter (proc string)
971 "Process and filter the output of \"notmuch show\" for tree view."
972 (let ((results-buf (process-buffer proc))
973 (parse-buf (process-get proc 'parse-buf))
974 (inhibit-read-only t)
976 (if (not (buffer-live-p results-buf))
977 (delete-process proc)
978 (with-current-buffer parse-buf
981 (goto-char (point-max))
983 (notmuch-sexp-parse-partial-list 'notmuch-tree-insert-forest-thread
986 (defun notmuch-tree-worker (basic-query &optional query-context target open-target unthreaded)
987 "Insert the tree view of the search in the current buffer.
989 This is is a helper function for notmuch-tree. The arguments are
990 the same as for the function notmuch-tree."
993 (add-hook 'post-command-hook #'notmuch-tree-command-hook t t)
994 (setq notmuch-tree-unthreaded unthreaded)
995 (setq notmuch-tree-basic-query basic-query)
996 (setq notmuch-tree-query-context (if (or (string= query-context "")
997 (string= query-context "*"))
999 (setq notmuch-tree-target-msg target)
1000 (setq notmuch-tree-open-target open-target)
1001 ;; Set the default value for `notmuch-show-process-crypto' in this
1002 ;; buffer. Although we don't use this some of the functions we call
1003 ;; (such as reply) do. It is a buffer local variable so setting it
1004 ;; will not affect genuine show buffers.
1005 (setq notmuch-show-process-crypto notmuch-crypto-process-mime)
1008 (goto-char (point-min))
1009 (let* ((search-args (concat basic-query
1010 (if query-context (concat " and (" query-context ")"))
1012 (message-arg (if unthreaded "--unthreaded" "--entire-thread")))
1013 (if (equal (car (process-lines notmuch-command "count" search-args)) "0")
1014 (setq search-args basic-query))
1015 (notmuch-tag-clear-cache)
1016 (let ((proc (notmuch-start-notmuch
1017 "notmuch-tree" (current-buffer) #'notmuch-tree-process-sentinel
1018 "show" "--body=false" "--format=sexp" "--format-version=4"
1019 message-arg search-args))
1020 ;; Use a scratch buffer to accumulate partial output.
1021 ;; This buffer will be killed by the sentinel, which
1022 ;; should be called no matter how the process dies.
1023 (parse-buf (generate-new-buffer " *notmuch tree parse*")))
1024 (process-put proc 'parse-buf parse-buf)
1025 (set-process-filter proc 'notmuch-tree-process-filter)
1026 (set-process-query-on-exit-flag proc nil))))
1028 (defun notmuch-tree-get-query ()
1029 "Return the current query in this tree buffer."
1030 (if notmuch-tree-query-context
1031 (concat notmuch-tree-basic-query
1033 notmuch-tree-query-context
1035 notmuch-tree-basic-query))
1037 (defun notmuch-tree (&optional query query-context target buffer-name open-target unthreaded)
1038 "Display threads matching QUERY in tree view.
1041 QUERY: the main query. This can be any query but in many cases will be
1042 a single thread. If nil this is read interactively from the minibuffer.
1043 QUERY-CONTEXT: is an additional term for the query. The query used
1044 is QUERY and QUERY-CONTEXT unless that does not match any messages
1045 in which case we fall back to just QUERY.
1046 TARGET: A message ID (with the id: prefix) that will be made
1047 current if it appears in the tree view results.
1048 BUFFER-NAME: the name of the buffer to display the tree view. If
1049 it is nil \"*notmuch-tree\" followed by QUERY is used.
1050 OPEN-TARGET: If TRUE open the target message in the message pane.
1051 UNTHREADED: If TRUE only show matching messages in an unthreaded view."
1054 (setq query (notmuch-read-query (concat "Notmuch "
1055 (if unthreaded "unthreaded " "tree ")
1057 (let ((buffer (get-buffer-create (generate-new-buffer-name
1060 (if unthreaded "unthreaded-" "tree-")
1062 (inhibit-read-only t))
1064 (switch-to-buffer buffer))
1065 ;; Don't track undo information for this buffer
1066 (set 'buffer-undo-list t)
1068 (notmuch-tree-worker query query-context target open-target unthreaded)
1070 (setq truncate-lines t))
1072 (defun notmuch-unthreaded (&optional query query-context target buffer-name open-target)
1074 (notmuch-tree query query-context target buffer-name open-target t))
1078 (provide 'notmuch-tree)
1080 ;;; notmuch-tree.el ends here