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>
29 (require 'notmuch-lib)
30 (require 'notmuch-query)
31 (require 'notmuch-show)
32 (require 'notmuch-tag)
33 (require 'notmuch-parser)
35 (eval-when-compile (require 'cl))
36 (declare-function notmuch-search "notmuch" (&optional query oldest-first target-thread target-line))
37 (declare-function notmuch-call-notmuch-process "notmuch" (&rest args))
38 (declare-function notmuch-read-query "notmuch" (prompt))
39 (declare-function notmuch-search-find-thread-id "notmuch" (&optional bare))
40 (declare-function notmuch-search-find-subject "notmuch" ())
42 ;; the following variable is defined in notmuch.el
43 (defvar notmuch-search-query-string)
45 ;; this variable distinguishes the unthreaded display from the normal tree display
46 (defvar notmuch-tree-unthreaded nil
47 "A buffer local copy of argument unthreaded to the function notmuch-tree")
48 (make-variable-buffer-local 'notmuch-tree-unthreaded)
50 (defgroup notmuch-tree nil
51 "Showing message and thread structure."
54 (defcustom notmuch-tree-show-out nil
55 "View selected messages in new window rather than split-pane."
59 (defcustom notmuch-unthreaded-show-out t
60 "View selected messages in new window rather than split-pane."
64 (defun notmuch-tree-show-out ()
65 (if notmuch-tree-unthreaded
66 notmuch-unthreaded-show-out
67 notmuch-tree-show-out))
69 (defcustom notmuch-tree-result-format
72 ((("tree" . "%s")("subject" . "%s")) ." %-54s ")
74 "Result formatting for Tree view. Supported fields are: date,
75 authors, subject, tree, tags. Tree means the thread tree
76 box graphics. The field may also be a list in which case
77 the formatting rules are applied recursively and then the
78 output of all the fields in the list is inserted
79 according to format-string.
81 Note the author string should not contain
82 whitespace (put it in the neighbouring fields instead).
84 (setq notmuch-tree-result-format \(\(\"authors\" . \"%-40s\"\)
85 \(\"subject\" . \"%s\"\)\)\)"
86 :type '(alist :key-type (string) :value-type (string))
89 (defcustom notmuch-unthreaded-result-format
92 ((("subject" . "%s")) ." %-54s ")
94 "Result formatting for unthreaded Tree view. Supported fields are: date,
95 authors, subject, tree, tags. Tree means the thread tree
96 box graphics. The field may also be a list in which case
97 the formatting rules are applied recursively and then the
98 output of all the fields in the list is inserted
99 according to format-string.
101 Note the author string should not contain
102 whitespace (put it in the neighbouring fields instead).
104 (setq notmuch-tree-result-format \(\(\"authors\" . \"%-40s\"\)
105 \(\"subject\" . \"%s\"\)\)\)"
106 :type '(alist :key-type (string) :value-type (string))
107 :group 'notmuch-tree)
109 (defun notmuch-tree-result-format ()
110 (if notmuch-tree-unthreaded
111 notmuch-unthreaded-result-format
112 notmuch-tree-result-format))
114 ;; Faces for messages that match the query.
115 (defface notmuch-tree-match-face
116 '((t :inherit default))
117 "Default face used in tree mode face for matching messages"
119 :group 'notmuch-faces)
121 (defface notmuch-tree-match-date-face
123 "Face used in tree mode for the date in messages matching the query."
125 :group 'notmuch-faces)
127 (defface notmuch-tree-match-author-face
130 (:foreground "OliveDrab1"))
133 (:foreground "dark blue"))
136 "Face used in tree mode for the date in messages matching the query."
138 :group 'notmuch-faces)
140 (defface notmuch-tree-match-subject-face
142 "Face used in tree mode for the subject in messages matching the query."
144 :group 'notmuch-faces)
146 (defface notmuch-tree-match-tree-face
148 "Face used in tree mode for the thread tree block graphics in messages matching the query."
150 :group 'notmuch-faces)
152 (defface notmuch-tree-match-tag-face
155 (:foreground "OliveDrab1"))
158 (:foreground "navy blue" :bold t))
161 "Face used in tree mode for tags in messages matching the query."
163 :group 'notmuch-faces)
165 ;; Faces for messages that do not match the query.
166 (defface notmuch-tree-no-match-face
167 '((t (:foreground "gray")))
168 "Default face used in tree mode face for non-matching messages"
170 :group 'notmuch-faces)
172 (defface notmuch-tree-no-match-date-face
174 "Face used in tree mode for non-matching dates."
176 :group 'notmuch-faces)
178 (defface notmuch-tree-no-match-subject-face
180 "Face used in tree mode for non-matching subjects."
182 :group 'notmuch-faces)
184 (defface notmuch-tree-no-match-tree-face
186 "Face used in tree mode for the thread tree block graphics in messages matching the query."
188 :group 'notmuch-faces)
190 (defface notmuch-tree-no-match-author-face
192 "Face used in tree mode for the date in messages matching the query."
194 :group 'notmuch-faces)
196 (defface notmuch-tree-no-match-tag-face
198 "Face used in tree mode face for non-matching tags."
200 :group 'notmuch-faces)
202 (defvar notmuch-tree-previous-subject
203 "The subject of the most recent result shown during the async display")
204 (make-variable-buffer-local 'notmuch-tree-previous-subject)
206 (defvar notmuch-tree-basic-query nil
207 "A buffer local copy of argument query to the function notmuch-tree")
208 (make-variable-buffer-local 'notmuch-tree-basic-query)
210 (defvar notmuch-tree-query-context nil
211 "A buffer local copy of argument query-context to the function notmuch-tree")
212 (make-variable-buffer-local 'notmuch-tree-query-context)
214 (defvar notmuch-tree-target-msg nil
215 "A buffer local copy of argument target to the function notmuch-tree")
216 (make-variable-buffer-local 'notmuch-tree-target-msg)
218 (defvar notmuch-tree-open-target nil
219 "A buffer local copy of argument open-target to the function notmuch-tree")
220 (make-variable-buffer-local 'notmuch-tree-open-target)
222 (defvar notmuch-tree-message-window nil
223 "The window of the message pane.
225 It is set in both the tree buffer and the child show buffer. It
226 is used to try and close the message pane when quitting tree view
227 or the child show buffer.")
228 (make-variable-buffer-local 'notmuch-tree-message-window)
229 (put 'notmuch-tree-message-window 'permanent-local t)
231 (defvar notmuch-tree-message-buffer nil
232 "The buffer name of the show buffer in the message pane.
234 This is used to try and make sure we don't close the message pane
235 if the user has loaded a different buffer in that window.")
236 (make-variable-buffer-local 'notmuch-tree-message-buffer)
237 (put 'notmuch-tree-message-buffer 'permanent-local t)
239 (defun notmuch-tree-to-message-pane (func)
240 "Execute FUNC in message pane.
242 This function returns a function (so can be used as a keybinding)
243 which executes function FUNC in the message pane if it is
244 open (if the message pane is closed it does nothing)."
246 ,(concat "(In message pane) " (documentation func t))
248 (when (window-live-p notmuch-tree-message-window)
249 (with-selected-window notmuch-tree-message-window
250 (call-interactively #',func)))))
252 (defun notmuch-tree-inherit-from-message-pane (sym)
253 "Return value of SYM in message-pane if open, or tree-pane if not"
254 (if (window-live-p notmuch-tree-message-window)
255 (with-selected-window notmuch-tree-message-window
259 (defun notmuch-tree-button-activate (&optional button)
260 "Activate BUTTON or button at point
262 This function does not give an error if there is no button."
264 (let ((button (or button (button-at (point)))))
265 (when button (button-activate button))))
267 (defun notmuch-tree-close-message-pane-and (func)
268 "Close message pane and execute FUNC.
270 This function returns a function (so can be used as a keybinding)
271 which closes the message pane if open and then executes function
274 ,(concat "(Close message pane and) " (documentation func t))
276 (let ((notmuch-show-process-crypto
277 (notmuch-tree-inherit-from-message-pane 'notmuch-show-process-crypto)))
278 (notmuch-tree-close-message-window)
279 (call-interactively #',func))))
281 (defvar notmuch-tree-mode-map
282 (let ((map (make-sparse-keymap)))
283 (set-keymap-parent map notmuch-common-keymap)
284 ;; The following override the global keymap.
285 ;; Override because we want to close message pane first.
286 (define-key map [remap notmuch-help] (notmuch-tree-close-message-pane-and #'notmuch-help))
287 ;; Override because we first close message pane and then close tree buffer.
288 (define-key map [remap notmuch-bury-or-kill-this-buffer] 'notmuch-tree-quit)
289 ;; Override because we close message pane after the search query is entered.
290 (define-key map [remap notmuch-search] 'notmuch-tree-to-search)
291 ;; Override because we want to close message pane first.
292 (define-key map [remap notmuch-mua-new-mail] (notmuch-tree-close-message-pane-and #'notmuch-mua-new-mail))
293 ;; Override because we want to close message pane first.
294 (define-key map [remap notmuch-jump-search] (notmuch-tree-close-message-pane-and #'notmuch-jump-search))
296 (define-key map "S" 'notmuch-search-from-tree-current-query)
297 (define-key map "U" 'notmuch-unthreaded-from-tree-current-query)
298 (define-key map "Z" 'notmuch-tree-from-unthreaded-current-query)
300 ;; these use notmuch-show functions directly
301 (define-key map "|" 'notmuch-show-pipe-message)
302 (define-key map "w" 'notmuch-show-save-attachments)
303 (define-key map "v" 'notmuch-show-view-all-mime-parts)
304 (define-key map "c" 'notmuch-show-stash-map)
305 (define-key map "b" 'notmuch-show-resend-message)
307 ;; these apply to the message pane
308 (define-key map (kbd "M-TAB") (notmuch-tree-to-message-pane #'notmuch-show-previous-button))
309 (define-key map (kbd "<backtab>") (notmuch-tree-to-message-pane #'notmuch-show-previous-button))
310 (define-key map (kbd "TAB") (notmuch-tree-to-message-pane #'notmuch-show-next-button))
311 (define-key map "$" (notmuch-tree-to-message-pane #'notmuch-show-toggle-process-crypto))
313 ;; bindings from show (or elsewhere) but we close the message pane first.
314 (define-key map "f" (notmuch-tree-close-message-pane-and #'notmuch-show-forward-message))
315 (define-key map "r" (notmuch-tree-close-message-pane-and #'notmuch-show-reply-sender))
316 (define-key map "R" (notmuch-tree-close-message-pane-and #'notmuch-show-reply))
317 (define-key map "V" (notmuch-tree-close-message-pane-and #'notmuch-show-view-raw-message))
319 ;; The main tree view bindings
320 (define-key map (kbd "RET") 'notmuch-tree-show-message)
321 (define-key map [mouse-1] 'notmuch-tree-show-message)
322 (define-key map "x" 'notmuch-tree-quit)
323 (define-key map "A" 'notmuch-tree-archive-thread)
324 (define-key map "a" 'notmuch-tree-archive-message-then-next)
325 (define-key map "z" 'notmuch-tree-to-tree)
326 (define-key map "n" 'notmuch-tree-next-matching-message)
327 (define-key map "p" 'notmuch-tree-prev-matching-message)
328 (define-key map "N" 'notmuch-tree-next-message)
329 (define-key map "P" 'notmuch-tree-prev-message)
330 (define-key map (kbd "M-p") 'notmuch-tree-prev-thread)
331 (define-key map (kbd "M-n") 'notmuch-tree-next-thread)
332 (define-key map "k" 'notmuch-tag-jump)
333 (define-key map "-" 'notmuch-tree-remove-tag)
334 (define-key map "+" 'notmuch-tree-add-tag)
335 (define-key map "*" 'notmuch-tree-tag-thread)
336 (define-key map " " 'notmuch-tree-scroll-or-next)
337 (define-key map (kbd "DEL") 'notmuch-tree-scroll-message-window-back)
338 (define-key map "e" 'notmuch-tree-resume-message)
340 (fset 'notmuch-tree-mode-map notmuch-tree-mode-map)
342 (defun notmuch-tree-get-message-properties ()
343 "Return the properties of the current message as a plist.
345 Some useful entries are:
346 :headers - Property list containing the headers :Date, :Subject, :From, etc.
347 :tags - Tags for this message"
350 (get-text-property (point) :notmuch-message-properties)))
352 (defun notmuch-tree-set-message-properties (props)
355 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
357 (defun notmuch-tree-set-prop (prop val &optional props)
358 (let ((inhibit-read-only t)
360 (notmuch-tree-get-message-properties))))
361 (plist-put props prop val)
362 (notmuch-tree-set-message-properties props)))
364 (defun notmuch-tree-get-prop (prop &optional props)
365 (let ((props (or props
366 (notmuch-tree-get-message-properties))))
367 (plist-get props prop)))
369 (defun notmuch-tree-set-tags (tags)
370 "Set the tags of the current message."
371 (notmuch-tree-set-prop :tags tags))
373 (defun notmuch-tree-get-tags ()
374 "Return the tags of the current message."
375 (notmuch-tree-get-prop :tags))
377 (defun notmuch-tree-get-message-id (&optional bare)
378 "Return the message id of the current message."
379 (let ((id (notmuch-tree-get-prop :id)))
383 (notmuch-id-to-query id))
386 (defun notmuch-tree-get-match ()
387 "Return whether the current message is a match."
389 (notmuch-tree-get-prop :match))
391 (defun notmuch-tree-refresh-result ()
392 "Redisplay the current message line.
394 This redisplays the current line based on the messages
395 properties (as they are now). This is used when tags are
397 (let ((init-point (point))
398 (end (line-end-position))
399 (msg (notmuch-tree-get-message-properties))
400 (inhibit-read-only t))
402 ;; This is a little tricky: we override
403 ;; notmuch-tree-previous-subject to get the decision between
404 ;; ... and a subject right and it stops notmuch-tree-insert-msg
405 ;; from overwriting the buffer local copy of
406 ;; notmuch-tree-previous-subject if this is called while the
407 ;; buffer is displaying.
408 (let ((notmuch-tree-previous-subject (notmuch-tree-get-prop :previous-subject)))
409 (delete-region (point) (1+ (line-end-position)))
410 (notmuch-tree-insert-msg msg))
411 (let ((new-end (line-end-position)))
412 (goto-char (if (= init-point end)
414 (min init-point (- new-end 1)))))))
416 (defun notmuch-tree-tag-update-display (&optional tag-changes)
417 "Update display for TAG-CHANGES to current message.
419 Updates the message in the message pane if appropriate, but does
420 NOT change the database."
421 (let* ((current-tags (notmuch-tree-get-tags))
422 (new-tags (notmuch-update-tags current-tags tag-changes))
423 (tree-msg-id (notmuch-tree-get-message-id)))
424 (unless (equal current-tags new-tags)
425 (notmuch-tree-set-tags new-tags)
426 (notmuch-tree-refresh-result)
427 (when (window-live-p notmuch-tree-message-window)
428 (with-selected-window notmuch-tree-message-window
429 (when (string= tree-msg-id (notmuch-show-get-message-id))
430 (notmuch-show-update-tags new-tags)))))))
432 (defun notmuch-tree-tag (tag-changes)
433 "Change tags for the current message"
435 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message")))
436 (notmuch-tag (notmuch-tree-get-message-id) tag-changes)
437 (notmuch-tree-tag-update-display tag-changes))
439 (defun notmuch-tree-add-tag (tag-changes)
440 "Same as `notmuch-tree-tag' but sets initial input to '+'."
442 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "+")))
443 (notmuch-tree-tag tag-changes))
445 (defun notmuch-tree-remove-tag (tag-changes)
446 "Same as `notmuch-tree-tag' but sets initial input to '-'."
448 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "-")))
449 (notmuch-tree-tag tag-changes))
451 (defun notmuch-tree-resume-message ()
452 "Resume EDITING the current draft message."
454 (notmuch-tree-close-message-window)
455 (let ((id (notmuch-tree-get-message-id)))
457 (notmuch-draft-resume id)
458 (message "No message to resume!"))))
460 ;; The next two functions close the message window before calling
461 ;; notmuch-search or notmuch-tree but they do so after the user has
462 ;; entered the query (in case the user was basing the query on
463 ;; something in the message window).
465 (defun notmuch-tree-to-search ()
466 "Run \"notmuch search\" with the given `query' and display results."
468 (let ((query (notmuch-read-query "Notmuch search: ")))
469 (notmuch-tree-close-message-window)
470 (notmuch-search query)))
472 (defun notmuch-tree-to-tree ()
473 "Run a query and display results in Tree view"
475 (let ((query (notmuch-read-query "Notmuch tree view search: ")))
476 (notmuch-tree-close-message-window)
477 (notmuch-tree query)))
479 (defun notmuch-unthreaded-from-tree-current-query ()
480 "Switch from tree view to unthreaded view"
482 (unless notmuch-tree-unthreaded
483 (notmuch-tree-refresh-view 'unthreaded)))
485 (defun notmuch-tree-from-unthreaded-current-query ()
486 "Switch from unthreaded view to tree view"
488 (when notmuch-tree-unthreaded
489 (notmuch-tree-refresh-view 'tree)))
491 (defun notmuch-search-from-tree-current-query ()
492 "Call notmuch search with the current query"
494 (notmuch-tree-close-message-window)
495 (notmuch-search (notmuch-tree-get-query)))
497 (defun notmuch-tree-message-window-kill-hook ()
498 "Close the message pane when exiting the show buffer."
499 (let ((buffer (current-buffer)))
500 (when (and (window-live-p notmuch-tree-message-window)
501 (eq (window-buffer notmuch-tree-message-window) buffer))
502 ;; We do not want an error if this is the sole window in the
503 ;; frame and I do not know how to test for that in emacs pre
504 ;; 24. Hence we just ignore-errors.
506 (delete-window notmuch-tree-message-window)))))
508 (defun notmuch-tree-command-hook ()
509 (when (eq major-mode 'notmuch-tree-mode)
510 ;; We just run the notmuch-show-command-hook on the message pane.
511 (when (buffer-live-p notmuch-tree-message-buffer)
512 (with-current-buffer notmuch-tree-message-buffer
513 (notmuch-show-command-hook)))))
515 (defun notmuch-tree-show-message-in ()
516 "Show the current message (in split-pane)."
518 (let ((id (notmuch-tree-get-message-id))
519 (inhibit-read-only t)
522 ;; We close and reopen the window to kill off un-needed buffers
523 ;; this might cause flickering but seems ok.
524 (notmuch-tree-close-message-window)
525 (setq notmuch-tree-message-window
526 (split-window-vertically (/ (window-height) 4)))
527 (with-selected-window notmuch-tree-message-window
528 ;; Since we are only displaying one message do not indent.
529 (let ((notmuch-show-indent-messages-width 0)
530 (notmuch-show-only-matching-messages t))
531 (setq buffer (notmuch-show id))))
532 ;; We need the `let' as notmuch-tree-message-window is buffer local.
533 (let ((window notmuch-tree-message-window))
534 (with-current-buffer buffer
535 (setq notmuch-tree-message-window window)
536 (add-hook 'kill-buffer-hook 'notmuch-tree-message-window-kill-hook)))
537 (when notmuch-show-mark-read-tags
538 (notmuch-tree-tag-update-display notmuch-show-mark-read-tags))
539 (setq notmuch-tree-message-buffer buffer))))
541 (defun notmuch-tree-show-message-out ()
542 "Show the current message (in whole window)."
544 (let ((id (notmuch-tree-get-message-id))
545 (inhibit-read-only t)
548 ;; We close the window to kill off un-needed buffers.
549 (notmuch-tree-close-message-window)
552 (defun notmuch-tree-show-message (arg)
553 "Show the current message.
555 Shows in split pane or whole window according to value of
556 `notmuch-tree-show-out'. A prefix argument reverses the choice."
558 (if (or (and (notmuch-tree-show-out) (not arg))
559 (and (not (notmuch-tree-show-out)) arg))
560 (notmuch-tree-show-message-out)
561 (notmuch-tree-show-message-in)))
563 (defun notmuch-tree-scroll-message-window ()
564 "Scroll the message window (if it exists)"
566 (when (window-live-p notmuch-tree-message-window)
567 (with-selected-window notmuch-tree-message-window
568 (if (pos-visible-in-window-p (point-max))
572 (defun notmuch-tree-scroll-message-window-back ()
573 "Scroll the message window back(if it exists)"
575 (when (window-live-p notmuch-tree-message-window)
576 (with-selected-window notmuch-tree-message-window
577 (if (pos-visible-in-window-p (point-min))
581 (defun notmuch-tree-scroll-or-next ()
582 "Scroll the message window. If it at end go to next message."
584 (when (notmuch-tree-scroll-message-window)
585 (notmuch-tree-next-matching-message)))
587 (defun notmuch-tree-quit ()
588 "Close the split view or exit tree."
590 (unless (notmuch-tree-close-message-window)
591 (kill-buffer (current-buffer))))
593 (defun notmuch-tree-close-message-window ()
594 "Close the message-window. Return t if close succeeds."
596 (when (and (window-live-p notmuch-tree-message-window)
597 (eq (window-buffer notmuch-tree-message-window) notmuch-tree-message-buffer))
598 (delete-window notmuch-tree-message-window)
599 (unless (get-buffer-window-list notmuch-tree-message-buffer)
600 (kill-buffer notmuch-tree-message-buffer))
603 (defun notmuch-tree-archive-message (&optional unarchive)
604 "Archive the current message.
606 Archive the current message by applying the tag changes in
607 `notmuch-archive-tags' to it. If a prefix argument is given, the
608 message will be \"unarchived\", i.e. the tag changes in
609 `notmuch-archive-tags' will be reversed."
611 (when notmuch-archive-tags
612 (notmuch-tree-tag (notmuch-tag-change-list notmuch-archive-tags unarchive))))
614 (defun notmuch-tree-archive-message-then-next (&optional unarchive)
615 "Archive the current message and move to next matching message."
617 (notmuch-tree-archive-message unarchive)
618 (notmuch-tree-next-matching-message))
620 (defun notmuch-tree-next-message ()
621 "Move to next message."
624 (when (window-live-p notmuch-tree-message-window)
625 (notmuch-tree-show-message-in)))
627 (defun notmuch-tree-prev-message ()
628 "Move to previous message."
631 (when (window-live-p notmuch-tree-message-window)
632 (notmuch-tree-show-message-in)))
634 (defun notmuch-tree-goto-matching-message (&optional prev)
635 "Move to the next or previous matching message.
637 Returns t if there was a next matching message in the thread to show,
639 (let ((dir (if prev -1 nil))
640 (eobfn (if prev #'bobp #'eobp)))
641 (while (and (not (funcall eobfn))
642 (not (notmuch-tree-get-match)))
644 (not (funcall eobfn))))
646 (defun notmuch-tree-matching-message (&optional prev)
647 "Move to the next or previous matching message"
649 (forward-line (if prev -1 nil))
650 (notmuch-tree-goto-matching-message prev)
651 (when (window-live-p notmuch-tree-message-window)
652 (notmuch-tree-show-message-in)))
654 (defun notmuch-tree-prev-matching-message ()
655 "Move to previous matching message."
657 (notmuch-tree-matching-message t))
659 (defun notmuch-tree-next-matching-message ()
660 "Move to next matching message."
662 (notmuch-tree-matching-message))
664 (defun notmuch-tree-refresh-view (&optional view)
667 (when (get-buffer-process (current-buffer))
668 (error "notmuch tree process already running for current buffer"))
669 (let ((inhibit-read-only t)
670 (basic-query notmuch-tree-basic-query)
671 (unthreaded (cond ((eq view 'unthreaded) t)
672 ((eq view 'tree) nil)
673 (t notmuch-tree-unthreaded)))
674 (query-context notmuch-tree-query-context)
675 (target (notmuch-tree-get-message-id)))
677 (notmuch-tree-worker basic-query
683 (defun notmuch-tree-thread-top ()
684 (when (notmuch-tree-get-message-properties)
685 (while (not (or (notmuch-tree-get-prop :first) (eobp)))
688 (defun notmuch-tree-prev-thread ()
691 (notmuch-tree-thread-top))
693 (defun notmuch-tree-next-thread ()
694 "Get the next thread in the current tree. Returns t if a thread was
695 found or nil if not."
698 (while (not (or (notmuch-tree-get-prop :first) (eobp)))
702 (defun notmuch-tree-thread-mapcar (function)
703 "Iterate through all messages in the current thread
704 and call FUNCTION for side effects."
706 (notmuch-tree-thread-top)
707 (loop collect (funcall function)
709 while (and (notmuch-tree-get-message-properties)
710 (not (notmuch-tree-get-prop :first))))))
712 (defun notmuch-tree-get-messages-ids-thread-search ()
713 "Return a search string for all message ids of messages in the current thread."
715 (notmuch-tree-thread-mapcar 'notmuch-tree-get-message-id)
718 (defun notmuch-tree-tag-thread (tag-changes)
719 "Tag all messages in the current thread"
721 (let ((tags (apply #'append (notmuch-tree-thread-mapcar
722 (lambda () (notmuch-tree-get-tags))))))
723 (list (notmuch-read-tag-changes tags "Tag thread"))))
724 (when (notmuch-tree-get-message-properties)
725 (notmuch-tag (notmuch-tree-get-messages-ids-thread-search) tag-changes)
726 (notmuch-tree-thread-mapcar
727 (lambda () (notmuch-tree-tag-update-display tag-changes)))))
729 (defun notmuch-tree-archive-thread (&optional unarchive)
730 "Archive each message in thread.
732 Archive each message currently shown by applying the tag changes
733 in `notmuch-archive-tags' to each. If a prefix argument is given,
734 the messages will be \"unarchived\", i.e. the tag changes in
735 `notmuch-archive-tags' will be reversed.
737 Note: This command is safe from any race condition of new messages
738 being delivered to the same thread. It does not archive the
739 entire thread, but only the messages shown in the current
742 (when notmuch-archive-tags
743 (notmuch-tree-tag-thread
744 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
746 ;; Functions below here display the tree buffer itself.
748 (defun notmuch-tree-clean-address (address)
749 "Try to clean a single email ADDRESS for display. Return
750 AUTHOR_NAME if present, otherwise return AUTHOR_EMAIL. Return
751 unchanged ADDRESS if parsing fails."
752 (let* ((clean-address (notmuch-clean-address address))
753 (p-address (car clean-address))
754 (p-name (cdr clean-address)))
756 ;; If we have a name return that otherwise return the address.
757 (or p-name p-address)))
759 (defun notmuch-tree-format-field (field format-string msg)
760 "Format a FIELD of MSG according to FORMAT-STRING and return string"
761 (let* ((headers (plist-get msg :headers))
762 (match (plist-get msg :match)))
765 (format format-string (notmuch-tree-format-field-list field msg)))
767 ((string-equal field "date")
768 (let ((face (if match
769 'notmuch-tree-match-date-face
770 'notmuch-tree-no-match-date-face)))
771 (propertize (format format-string (plist-get msg :date_relative)) 'face face)))
773 ((string-equal field "tree")
774 (let ((tree-status (plist-get msg :tree-status))
776 'notmuch-tree-match-tree-face
777 'notmuch-tree-no-match-tree-face)))
779 (propertize (format format-string
780 (mapconcat #'identity (reverse tree-status) ""))
783 ((string-equal field "subject")
784 (let ((bare-subject (notmuch-show-strip-re (plist-get headers :Subject)))
785 (previous-subject notmuch-tree-previous-subject)
787 'notmuch-tree-match-subject-face
788 'notmuch-tree-no-match-subject-face)))
790 (setq notmuch-tree-previous-subject bare-subject)
791 (propertize (format format-string
792 (if (string= previous-subject bare-subject)
797 ((string-equal field "authors")
798 (let ((author (notmuch-tree-clean-address (plist-get headers :From)))
799 (len (length (format format-string "")))
801 'notmuch-tree-match-author-face
802 'notmuch-tree-no-match-author-face)))
803 (when (> (length author) len)
804 (setq author (substring author 0 len)))
805 (propertize (format format-string author) 'face face)))
807 ((string-equal field "tags")
808 (let ((tags (plist-get msg :tags))
809 (orig-tags (plist-get msg :orig-tags))
811 'notmuch-tree-match-tag-face
812 'notmuch-tree-no-match-tag-face)))
813 (format format-string (notmuch-tag-format-tags tags orig-tags face)))))))
815 (defun notmuch-tree-format-field-list (field-list msg)
816 "Format fields of MSG according to FIELD-LIST and return string"
817 (let ((face (if (plist-get msg :match)
818 'notmuch-tree-match-face
819 'notmuch-tree-no-match-face))
821 (dolist (spec field-list result-string)
822 (let ((field-string (notmuch-tree-format-field (car spec) (cdr spec) msg)))
823 (setq result-string (concat result-string field-string))))
824 (notmuch-apply-face result-string face t)))
826 (defun notmuch-tree-insert-msg (msg)
827 "Insert the message MSG according to notmuch-tree-result-format"
828 ;; We need to save the previous subject as it will get overwritten
829 ;; by the insert-field calls.
830 (let ((previous-subject notmuch-tree-previous-subject))
831 (insert (notmuch-tree-format-field-list (notmuch-tree-result-format) msg))
832 (notmuch-tree-set-message-properties msg)
833 (notmuch-tree-set-prop :previous-subject previous-subject)
836 (defun notmuch-tree-goto-and-insert-msg (msg)
837 "Insert msg at the end of the buffer. Move point to msg if it is the target"
839 (goto-char (point-max))
840 (notmuch-tree-insert-msg msg))
841 (let ((msg-id (notmuch-id-to-query (plist-get msg :id)))
842 (target notmuch-tree-target-msg))
843 (when (or (and (not target) (plist-get msg :match))
844 (string= msg-id target))
845 (setq notmuch-tree-target-msg "found")
846 (goto-char (point-max))
848 (when notmuch-tree-open-target
849 (notmuch-tree-show-message-in)))))
851 (defun notmuch-tree-insert-tree (tree depth tree-status first last)
852 "Insert the message tree TREE at depth DEPTH in the current thread.
854 A message tree is another name for a single sub-thread: i.e., a
855 message together with all its descendents."
856 (let ((msg (car tree))
857 (replies (cadr tree)))
860 ((and (< 0 depth) (not last))
861 (push "├" tree-status))
862 ((and (< 0 depth) last)
863 (push "╰" tree-status))
864 ((and (eq 0 depth) first last)
865 ;; (push "─" tree-status)) choice between this and next line is matter of taste.
866 (push " " tree-status))
867 ((and (eq 0 depth) first (not last))
868 (push "┬" tree-status))
869 ((and (eq 0 depth) (not first) last)
870 (push "╰" tree-status))
871 ((and (eq 0 depth) (not first) (not last))
872 (push "├" tree-status)))
874 (push (concat (if replies "┬" "─") "►") tree-status)
875 (setq msg (plist-put msg :first (and first (eq 0 depth))))
876 (setq msg (plist-put msg :tree-status tree-status))
877 (setq msg (plist-put msg :orig-tags (plist-get msg :tags)))
878 (notmuch-tree-goto-and-insert-msg msg)
883 (push " " tree-status)
884 (push "│" tree-status))
886 (notmuch-tree-insert-thread replies (1+ depth) tree-status)))
888 (defun notmuch-tree-insert-thread (thread depth tree-status)
889 "Insert the collection of sibling sub-threads THREAD at depth DEPTH in the current forest."
890 (let ((n (length thread)))
891 (loop for tree in thread
892 for count from 1 to n
894 do (notmuch-tree-insert-tree tree depth tree-status (eq count 1) (eq count n)))))
896 (defun notmuch-tree-insert-forest-thread (forest-thread)
897 "Insert a single complete thread."
899 ;; Reset at the start of each main thread.
900 (setq notmuch-tree-previous-subject nil)
901 (notmuch-tree-insert-thread forest-thread 0 tree-status)))
903 (defun notmuch-tree-insert-forest (forest)
904 "Insert a forest of threads.
906 This function inserts a collection of several complete threads as
907 passed to it by notmuch-tree-process-filter."
908 (mapc 'notmuch-tree-insert-forest-thread forest))
910 (define-derived-mode notmuch-tree-mode fundamental-mode "notmuch-tree"
911 "Major mode displaying messages (as opposed to threads) of a notmuch search.
913 This buffer contains the results of a \"notmuch tree\" of your
914 email archives. Each line in the buffer represents a single
915 message giving the relative date, the author, subject, and any
918 Pressing \\[notmuch-tree-show-message] on any line displays that message.
920 Complete list of currently available key bindings:
922 \\{notmuch-tree-mode-map}"
924 (setq notmuch-buffer-refresh-function #'notmuch-tree-refresh-view)
926 (setq buffer-read-only t
929 (defun notmuch-tree-process-sentinel (proc msg)
930 "Add a message to let user know when \"notmuch tree\" exits"
931 (let ((buffer (process-buffer proc))
932 (status (process-status proc))
933 (exit-status (process-exit-status proc))
934 (never-found-target-thread nil))
935 (when (memq status '(exit signal))
936 (kill-buffer (process-get proc 'parse-buf))
937 (if (buffer-live-p buffer)
938 (with-current-buffer buffer
940 (let ((inhibit-read-only t)
942 (goto-char (point-max))
943 (if (eq status 'signal)
944 (insert "Incomplete search results (tree view process was killed).\n"))
945 (when (eq status 'exit)
946 (insert "End of search results.")
947 (unless (= exit-status 0)
948 (insert (format " (process returned %d)" exit-status)))
949 (insert "\n")))))))))
951 (defun notmuch-tree-process-filter (proc string)
952 "Process and filter the output of \"notmuch show\" for tree view"
953 (let ((results-buf (process-buffer proc))
954 (parse-buf (process-get proc 'parse-buf))
955 (inhibit-read-only t)
957 (if (not (buffer-live-p results-buf))
958 (delete-process proc)
959 (with-current-buffer parse-buf
962 (goto-char (point-max))
964 (notmuch-sexp-parse-partial-list 'notmuch-tree-insert-forest-thread
967 (defun notmuch-tree-worker (basic-query &optional query-context target open-target unthreaded)
968 "Insert the tree view of the search in the current buffer.
970 This is is a helper function for notmuch-tree. The arguments are
971 the same as for the function notmuch-tree."
974 (add-hook 'post-command-hook #'notmuch-tree-command-hook t t)
975 (setq notmuch-tree-unthreaded unthreaded)
976 (setq notmuch-tree-basic-query basic-query)
977 (setq notmuch-tree-query-context (if (or (string= query-context "")
978 (string= query-context "*"))
980 (setq notmuch-tree-target-msg target)
981 (setq notmuch-tree-open-target open-target)
982 ;; Set the default value for `notmuch-show-process-crypto' in this
983 ;; buffer. Although we don't use this some of the functions we call
984 ;; (such as reply) do. It is a buffer local variable so setting it
985 ;; will not affect genuine show buffers.
986 (setq notmuch-show-process-crypto notmuch-crypto-process-mime)
989 (goto-char (point-min))
990 (let* ((search-args (concat basic-query
991 (if query-context (concat " and (" query-context ")"))
993 (message-arg (if unthreaded "--unthreaded" "--entire-thread")))
994 (if (equal (car (process-lines notmuch-command "count" search-args)) "0")
995 (setq search-args basic-query))
996 (notmuch-tag-clear-cache)
997 (let ((proc (notmuch-start-notmuch
998 "notmuch-tree" (current-buffer) #'notmuch-tree-process-sentinel
999 "show" "--body=false" "--format=sexp" "--format-version=4"
1000 message-arg search-args))
1001 ;; Use a scratch buffer to accumulate partial output.
1002 ;; This buffer will be killed by the sentinel, which
1003 ;; should be called no matter how the process dies.
1004 (parse-buf (generate-new-buffer " *notmuch tree parse*")))
1005 (process-put proc 'parse-buf parse-buf)
1006 (set-process-filter proc 'notmuch-tree-process-filter)
1007 (set-process-query-on-exit-flag proc nil))))
1009 (defun notmuch-tree-get-query ()
1010 "Return the current query in this tree buffer"
1011 (if notmuch-tree-query-context
1012 (concat notmuch-tree-basic-query
1014 notmuch-tree-query-context
1016 notmuch-tree-basic-query))
1018 (defun notmuch-tree (&optional query query-context target buffer-name open-target unthreaded)
1019 "Display threads matching QUERY in Tree View.
1022 QUERY: the main query. This can be any query but in many cases will be
1023 a single thread. If nil this is read interactively from the minibuffer.
1024 QUERY-CONTEXT: is an additional term for the query. The query used
1025 is QUERY and QUERY-CONTEXT unless that does not match any messages
1026 in which case we fall back to just QUERY.
1027 TARGET: A message ID (with the id: prefix) that will be made
1028 current if it appears in the tree view results.
1029 BUFFER-NAME: the name of the buffer to display the tree view. If
1030 it is nil \"*notmuch-tree\" followed by QUERY is used.
1031 OPEN-TARGET: If TRUE open the target message in the message pane.
1032 UNTHREADED: If TRUE only show matching messages in an unthreaded view."
1035 (setq query (notmuch-read-query (concat "Notmuch "
1036 (if unthreaded "unthreaded " "tree ")
1038 (let ((buffer (get-buffer-create (generate-new-buffer-name
1041 (if unthreaded "unthreaded-" "tree-")
1043 (inhibit-read-only t))
1045 (switch-to-buffer buffer))
1046 ;; Don't track undo information for this buffer
1047 (set 'buffer-undo-list t)
1049 (notmuch-tree-worker query query-context target open-target unthreaded)
1051 (setq truncate-lines t))
1053 (defun notmuch-unthreaded (&optional query query-context target buffer-name open-target)
1055 (notmuch-tree query query-context target buffer-name open-target t))
1059 (provide 'notmuch-tree)
1061 ;;; notmuch-tree.el ends here