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 (defgroup notmuch-tree nil
46 "Showing message and thread structure."
49 (defcustom notmuch-tree-show-out nil
50 "View selected messages in new window rather than split-pane."
54 (defcustom notmuch-tree-result-format
57 ((("tree" . "%s")("subject" . "%s")) ." %-54s ")
59 "Result formatting for Tree view. Supported fields are: date,
60 authors, subject, tree, tags. Tree means the thread tree
61 box graphics. The field may also be a list in which case
62 the formatting rules are applied recursively and then the
63 output of all the fields in the list is inserted
64 according to format-string.
66 Note the author string should not contain
67 whitespace (put it in the neighbouring fields instead).
69 (setq notmuch-tree-result-format \(\(\"authors\" . \"%-40s\"\)
70 \(\"subject\" . \"%s\"\)\)\)"
71 :type '(alist :key-type (string) :value-type (string))
74 ;; Faces for messages that match the query.
75 (defface notmuch-tree-match-face
76 '((t :inherit default))
77 "Default face used in tree mode face for matching messages"
79 :group 'notmuch-faces)
81 (defface notmuch-tree-match-date-face
83 "Face used in tree mode for the date in messages matching the query."
85 :group 'notmuch-faces)
87 (defface notmuch-tree-match-author-face
90 (:foreground "OliveDrab1"))
93 (:foreground "dark blue"))
96 "Face used in tree mode for the date in messages matching the query."
98 :group 'notmuch-faces)
100 (defface notmuch-tree-match-subject-face
102 "Face used in tree mode for the subject in messages matching the query."
104 :group 'notmuch-faces)
106 (defface notmuch-tree-match-tree-face
108 "Face used in tree mode for the thread tree block graphics in messages matching the query."
110 :group 'notmuch-faces)
112 (defface notmuch-tree-match-tag-face
115 (:foreground "OliveDrab1"))
118 (:foreground "navy blue" :bold t))
121 "Face used in tree mode for tags in messages matching the query."
123 :group 'notmuch-faces)
125 ;; Faces for messages that do not match the query.
126 (defface notmuch-tree-no-match-face
127 '((t (:foreground "gray")))
128 "Default face used in tree mode face for non-matching messages"
130 :group 'notmuch-faces)
132 (defface notmuch-tree-no-match-date-face
134 "Face used in tree mode for non-matching dates."
136 :group 'notmuch-faces)
138 (defface notmuch-tree-no-match-subject-face
140 "Face used in tree mode for non-matching subjects."
142 :group 'notmuch-faces)
144 (defface notmuch-tree-no-match-tree-face
146 "Face used in tree mode for the thread tree block graphics in messages matching the query."
148 :group 'notmuch-faces)
150 (defface notmuch-tree-no-match-author-face
152 "Face used in tree mode for the date in messages matching the query."
154 :group 'notmuch-faces)
156 (defface notmuch-tree-no-match-tag-face
158 "Face used in tree mode face for non-matching tags."
160 :group 'notmuch-faces)
162 (defvar notmuch-tree-previous-subject
163 "The subject of the most recent result shown during the async display")
164 (make-variable-buffer-local 'notmuch-tree-previous-subject)
166 (defvar notmuch-tree-basic-query nil
167 "A buffer local copy of argument query to the function notmuch-tree")
168 (make-variable-buffer-local 'notmuch-tree-basic-query)
170 (defvar notmuch-tree-query-context nil
171 "A buffer local copy of argument query-context to the function notmuch-tree")
172 (make-variable-buffer-local 'notmuch-tree-query-context)
174 (defvar notmuch-tree-target-msg nil
175 "A buffer local copy of argument target to the function notmuch-tree")
176 (make-variable-buffer-local 'notmuch-tree-target-msg)
178 (defvar notmuch-tree-open-target nil
179 "A buffer local copy of argument open-target to the function notmuch-tree")
180 (make-variable-buffer-local 'notmuch-tree-open-target)
182 (defvar notmuch-tree-message-window nil
183 "The window of the message pane.
185 It is set in both the tree buffer and the child show buffer. It
186 is used to try and close the message pane when quitting tree view
187 or the child show buffer.")
188 (make-variable-buffer-local 'notmuch-tree-message-window)
189 (put 'notmuch-tree-message-window 'permanent-local t)
191 (defvar notmuch-tree-message-buffer nil
192 "The buffer name of the show buffer in the message pane.
194 This is used to try and make sure we don't close the message pane
195 if the user has loaded a different buffer in that window.")
196 (make-variable-buffer-local 'notmuch-tree-message-buffer)
197 (put 'notmuch-tree-message-buffer 'permanent-local t)
199 (defun notmuch-tree-to-message-pane (func)
200 "Execute FUNC in message pane.
202 This function returns a function (so can be used as a keybinding)
203 which executes function FUNC in the message pane if it is
204 open (if the message pane is closed it does nothing)."
206 ,(concat "(In message pane) " (documentation func t))
208 (when (window-live-p notmuch-tree-message-window)
209 (with-selected-window notmuch-tree-message-window
210 (call-interactively #',func)))))
212 (defun notmuch-tree-inherit-from-message-pane (sym)
213 "Return value of SYM in message-pane if open, or tree-pane if not"
214 (if (window-live-p notmuch-tree-message-window)
215 (with-selected-window notmuch-tree-message-window
219 (defun notmuch-tree-button-activate (&optional button)
220 "Activate BUTTON or button at point
222 This function does not give an error if there is no button."
224 (let ((button (or button (button-at (point)))))
225 (when button (button-activate button))))
227 (defun notmuch-tree-close-message-pane-and (func)
228 "Close message pane and execute FUNC.
230 This function returns a function (so can be used as a keybinding)
231 which closes the message pane if open and then executes function
234 ,(concat "(Close message pane and) " (documentation func t))
236 (let ((notmuch-show-process-crypto
237 (notmuch-tree-inherit-from-message-pane 'notmuch-show-process-crypto)))
238 (notmuch-tree-close-message-window)
239 (call-interactively #',func))))
241 (defvar notmuch-tree-mode-map
242 (let ((map (make-sparse-keymap)))
243 (set-keymap-parent map notmuch-common-keymap)
244 ;; The following override the global keymap.
245 ;; Override because we want to close message pane first.
246 (define-key map [remap notmuch-help] (notmuch-tree-close-message-pane-and #'notmuch-help))
247 ;; Override because we first close message pane and then close tree buffer.
248 (define-key map [remap notmuch-bury-or-kill-this-buffer] 'notmuch-tree-quit)
249 ;; Override because we close message pane after the search query is entered.
250 (define-key map [remap notmuch-search] 'notmuch-tree-to-search)
251 ;; Override because we want to close message pane first.
252 (define-key map [remap notmuch-mua-new-mail] (notmuch-tree-close-message-pane-and #'notmuch-mua-new-mail))
253 ;; Override because we want to close message pane first.
254 (define-key map [remap notmuch-jump-search] (notmuch-tree-close-message-pane-and #'notmuch-jump-search))
256 (define-key map "S" 'notmuch-search-from-tree-current-query)
258 ;; these use notmuch-show functions directly
259 (define-key map "|" 'notmuch-show-pipe-message)
260 (define-key map "w" 'notmuch-show-save-attachments)
261 (define-key map "v" 'notmuch-show-view-all-mime-parts)
262 (define-key map "c" 'notmuch-show-stash-map)
263 (define-key map "b" 'notmuch-show-resend-message)
265 ;; these apply to the message pane
266 (define-key map (kbd "M-TAB") (notmuch-tree-to-message-pane #'notmuch-show-previous-button))
267 (define-key map (kbd "<backtab>") (notmuch-tree-to-message-pane #'notmuch-show-previous-button))
268 (define-key map (kbd "TAB") (notmuch-tree-to-message-pane #'notmuch-show-next-button))
269 (define-key map "$" (notmuch-tree-to-message-pane #'notmuch-show-toggle-process-crypto))
271 ;; bindings from show (or elsewhere) but we close the message pane first.
272 (define-key map "f" (notmuch-tree-close-message-pane-and #'notmuch-show-forward-message))
273 (define-key map "r" (notmuch-tree-close-message-pane-and #'notmuch-show-reply-sender))
274 (define-key map "R" (notmuch-tree-close-message-pane-and #'notmuch-show-reply))
275 (define-key map "V" (notmuch-tree-close-message-pane-and #'notmuch-show-view-raw-message))
277 ;; The main tree view bindings
278 (define-key map (kbd "RET") 'notmuch-tree-show-message)
279 (define-key map [mouse-1] 'notmuch-tree-show-message)
280 (define-key map "x" 'notmuch-tree-quit)
281 (define-key map "A" 'notmuch-tree-archive-thread)
282 (define-key map "a" 'notmuch-tree-archive-message-then-next)
283 (define-key map "z" 'notmuch-tree-to-tree)
284 (define-key map "n" 'notmuch-tree-next-matching-message)
285 (define-key map "p" 'notmuch-tree-prev-matching-message)
286 (define-key map "N" 'notmuch-tree-next-message)
287 (define-key map "P" 'notmuch-tree-prev-message)
288 (define-key map (kbd "M-p") 'notmuch-tree-prev-thread)
289 (define-key map (kbd "M-n") 'notmuch-tree-next-thread)
290 (define-key map "k" 'notmuch-tag-jump)
291 (define-key map "-" 'notmuch-tree-remove-tag)
292 (define-key map "+" 'notmuch-tree-add-tag)
293 (define-key map "*" 'notmuch-tree-tag-thread)
294 (define-key map " " 'notmuch-tree-scroll-or-next)
295 (define-key map (kbd "DEL") 'notmuch-tree-scroll-message-window-back)
296 (define-key map "e" 'notmuch-tree-resume-message)
298 (fset 'notmuch-tree-mode-map notmuch-tree-mode-map)
300 (defun notmuch-tree-get-message-properties ()
301 "Return the properties of the current message as a plist.
303 Some useful entries are:
304 :headers - Property list containing the headers :Date, :Subject, :From, etc.
305 :tags - Tags for this message"
308 (get-text-property (point) :notmuch-message-properties)))
310 (defun notmuch-tree-set-message-properties (props)
313 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
315 (defun notmuch-tree-set-prop (prop val &optional props)
316 (let ((inhibit-read-only t)
318 (notmuch-tree-get-message-properties))))
319 (plist-put props prop val)
320 (notmuch-tree-set-message-properties props)))
322 (defun notmuch-tree-get-prop (prop &optional props)
323 (let ((props (or props
324 (notmuch-tree-get-message-properties))))
325 (plist-get props prop)))
327 (defun notmuch-tree-set-tags (tags)
328 "Set the tags of the current message."
329 (notmuch-tree-set-prop :tags tags))
331 (defun notmuch-tree-get-tags ()
332 "Return the tags of the current message."
333 (notmuch-tree-get-prop :tags))
335 (defun notmuch-tree-get-message-id (&optional bare)
336 "Return the message id of the current message."
337 (let ((id (notmuch-tree-get-prop :id)))
341 (notmuch-id-to-query id))
344 (defun notmuch-tree-get-match ()
345 "Return whether the current message is a match."
347 (notmuch-tree-get-prop :match))
349 (defun notmuch-tree-refresh-result ()
350 "Redisplay the current message line.
352 This redisplays the current line based on the messages
353 properties (as they are now). This is used when tags are
355 (let ((init-point (point))
356 (end (line-end-position))
357 (msg (notmuch-tree-get-message-properties))
358 (inhibit-read-only t))
360 ;; This is a little tricky: we override
361 ;; notmuch-tree-previous-subject to get the decision between
362 ;; ... and a subject right and it stops notmuch-tree-insert-msg
363 ;; from overwriting the buffer local copy of
364 ;; notmuch-tree-previous-subject if this is called while the
365 ;; buffer is displaying.
366 (let ((notmuch-tree-previous-subject (notmuch-tree-get-prop :previous-subject)))
367 (delete-region (point) (1+ (line-end-position)))
368 (notmuch-tree-insert-msg msg))
369 (let ((new-end (line-end-position)))
370 (goto-char (if (= init-point end)
372 (min init-point (- new-end 1)))))))
374 (defun notmuch-tree-tag-update-display (&optional tag-changes)
375 "Update display for TAG-CHANGES to current message.
377 Updates the message in the message pane if appropriate, but does
378 NOT change the database."
379 (let* ((current-tags (notmuch-tree-get-tags))
380 (new-tags (notmuch-update-tags current-tags tag-changes))
381 (tree-msg-id (notmuch-tree-get-message-id)))
382 (unless (equal current-tags new-tags)
383 (notmuch-tree-set-tags new-tags)
384 (notmuch-tree-refresh-result)
385 (when (window-live-p notmuch-tree-message-window)
386 (with-selected-window notmuch-tree-message-window
387 (when (string= tree-msg-id (notmuch-show-get-message-id))
388 (notmuch-show-update-tags new-tags)))))))
390 (defun notmuch-tree-tag (tag-changes)
391 "Change tags for the current message"
393 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message")))
394 (notmuch-tag (notmuch-tree-get-message-id) tag-changes)
395 (notmuch-tree-tag-update-display tag-changes))
397 (defun notmuch-tree-add-tag (tag-changes)
398 "Same as `notmuch-tree-tag' but sets initial input to '+'."
400 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "+")))
401 (notmuch-tree-tag tag-changes))
403 (defun notmuch-tree-remove-tag (tag-changes)
404 "Same as `notmuch-tree-tag' but sets initial input to '-'."
406 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "-")))
407 (notmuch-tree-tag tag-changes))
409 (defun notmuch-tree-resume-message ()
410 "Resume EDITING the current draft message."
412 (notmuch-tree-close-message-window)
413 (let ((id (notmuch-tree-get-message-id)))
415 (notmuch-draft-resume id)
416 (message "No message to resume!"))))
418 ;; The next two functions close the message window before calling
419 ;; notmuch-search or notmuch-tree but they do so after the user has
420 ;; entered the query (in case the user was basing the query on
421 ;; something in the message window).
423 (defun notmuch-tree-to-search ()
424 "Run \"notmuch search\" with the given `query' and display results."
426 (let ((query (notmuch-read-query "Notmuch search: ")))
427 (notmuch-tree-close-message-window)
428 (notmuch-search query)))
430 (defun notmuch-tree-to-tree ()
431 "Run a query and display results in Tree view"
433 (let ((query (notmuch-read-query "Notmuch tree view search: ")))
434 (notmuch-tree-close-message-window)
435 (notmuch-tree query)))
437 (defun notmuch-search-from-tree-current-query ()
438 "Call notmuch search with the current query"
440 (notmuch-tree-close-message-window)
441 (notmuch-search (notmuch-tree-get-query)))
443 (defun notmuch-tree-message-window-kill-hook ()
444 "Close the message pane when exiting the show buffer."
445 (let ((buffer (current-buffer)))
446 (when (and (window-live-p notmuch-tree-message-window)
447 (eq (window-buffer notmuch-tree-message-window) buffer))
448 ;; We do not want an error if this is the sole window in the
449 ;; frame and I do not know how to test for that in emacs pre
450 ;; 24. Hence we just ignore-errors.
452 (delete-window notmuch-tree-message-window)))))
454 (defun notmuch-tree-command-hook ()
455 (when (eq major-mode 'notmuch-tree-mode)
456 ;; We just run the notmuch-show-command-hook on the message pane.
457 (when (buffer-live-p notmuch-tree-message-buffer)
458 (with-current-buffer notmuch-tree-message-buffer
459 (notmuch-show-command-hook)))))
461 (defun notmuch-tree-show-message-in ()
462 "Show the current message (in split-pane)."
464 (let ((id (notmuch-tree-get-message-id))
465 (inhibit-read-only t)
468 ;; We close and reopen the window to kill off un-needed buffers
469 ;; this might cause flickering but seems ok.
470 (notmuch-tree-close-message-window)
471 (setq notmuch-tree-message-window
472 (split-window-vertically (/ (window-height) 4)))
473 (with-selected-window notmuch-tree-message-window
474 ;; Since we are only displaying one message do not indent.
475 (let ((notmuch-show-indent-messages-width 0)
476 (notmuch-show-only-matching-messages t))
477 (setq buffer (notmuch-show id))))
478 ;; We need the `let' as notmuch-tree-message-window is buffer local.
479 (let ((window notmuch-tree-message-window))
480 (with-current-buffer buffer
481 (setq notmuch-tree-message-window window)
482 (add-hook 'kill-buffer-hook 'notmuch-tree-message-window-kill-hook)))
483 (when notmuch-show-mark-read-tags
484 (notmuch-tree-tag-update-display notmuch-show-mark-read-tags))
485 (setq notmuch-tree-message-buffer buffer))))
487 (defun notmuch-tree-show-message-out ()
488 "Show the current message (in whole window)."
490 (let ((id (notmuch-tree-get-message-id))
491 (inhibit-read-only t)
494 ;; We close the window to kill off un-needed buffers.
495 (notmuch-tree-close-message-window)
498 (defun notmuch-tree-show-message (arg)
499 "Show the current message.
501 Shows in split pane or whole window according to value of
502 `notmuch-tree-show-out'. A prefix argument reverses the choice."
504 (if (or (and notmuch-tree-show-out (not arg))
505 (and (not notmuch-tree-show-out) arg))
506 (notmuch-tree-show-message-out)
507 (notmuch-tree-show-message-in)))
509 (defun notmuch-tree-scroll-message-window ()
510 "Scroll the message window (if it exists)"
512 (when (window-live-p notmuch-tree-message-window)
513 (with-selected-window notmuch-tree-message-window
514 (if (pos-visible-in-window-p (point-max))
518 (defun notmuch-tree-scroll-message-window-back ()
519 "Scroll the message window back(if it exists)"
521 (when (window-live-p notmuch-tree-message-window)
522 (with-selected-window notmuch-tree-message-window
523 (if (pos-visible-in-window-p (point-min))
527 (defun notmuch-tree-scroll-or-next ()
528 "Scroll the message window. If it at end go to next message."
530 (when (notmuch-tree-scroll-message-window)
531 (notmuch-tree-next-matching-message)))
533 (defun notmuch-tree-quit ()
534 "Close the split view or exit tree."
536 (unless (notmuch-tree-close-message-window)
537 (kill-buffer (current-buffer))))
539 (defun notmuch-tree-close-message-window ()
540 "Close the message-window. Return t if close succeeds."
542 (when (and (window-live-p notmuch-tree-message-window)
543 (eq (window-buffer notmuch-tree-message-window) notmuch-tree-message-buffer))
544 (delete-window notmuch-tree-message-window)
545 (unless (get-buffer-window-list notmuch-tree-message-buffer)
546 (kill-buffer notmuch-tree-message-buffer))
549 (defun notmuch-tree-archive-message (&optional unarchive)
550 "Archive the current message.
552 Archive the current message by applying the tag changes in
553 `notmuch-archive-tags' to it. If a prefix argument is given, the
554 message will be \"unarchived\", i.e. the tag changes in
555 `notmuch-archive-tags' will be reversed."
557 (when notmuch-archive-tags
558 (notmuch-tree-tag (notmuch-tag-change-list notmuch-archive-tags unarchive))))
560 (defun notmuch-tree-archive-message-then-next (&optional unarchive)
561 "Archive the current message and move to next matching message."
563 (notmuch-tree-archive-message unarchive)
564 (notmuch-tree-next-matching-message))
566 (defun notmuch-tree-next-message ()
567 "Move to next message."
570 (when (window-live-p notmuch-tree-message-window)
571 (notmuch-tree-show-message-in)))
573 (defun notmuch-tree-prev-message ()
574 "Move to previous message."
577 (when (window-live-p notmuch-tree-message-window)
578 (notmuch-tree-show-message-in)))
580 (defun notmuch-tree-prev-matching-message ()
581 "Move to previous matching message."
584 (while (and (not (bobp)) (not (notmuch-tree-get-match)))
586 (when (window-live-p notmuch-tree-message-window)
587 (notmuch-tree-show-message-in)))
589 (defun notmuch-tree-next-matching-message ()
590 "Move to next matching message."
593 (while (and (not (eobp)) (not (notmuch-tree-get-match)))
595 (when (window-live-p notmuch-tree-message-window)
596 (notmuch-tree-show-message-in)))
598 (defun notmuch-tree-refresh-view ()
601 (when (get-buffer-process (current-buffer))
602 (error "notmuch tree process already running for current buffer"))
603 (let ((inhibit-read-only t)
604 (basic-query notmuch-tree-basic-query)
605 (query-context notmuch-tree-query-context)
606 (target (notmuch-tree-get-message-id)))
608 (notmuch-tree-worker basic-query
612 (defun notmuch-tree-thread-top ()
613 (when (notmuch-tree-get-message-properties)
614 (while (not (or (notmuch-tree-get-prop :first) (eobp)))
617 (defun notmuch-tree-prev-thread ()
620 (notmuch-tree-thread-top))
622 (defun notmuch-tree-next-thread ()
625 (while (not (or (notmuch-tree-get-prop :first) (eobp)))
628 (defun notmuch-tree-thread-mapcar (function)
629 "Iterate through all messages in the current thread
630 and call FUNCTION for side effects."
632 (notmuch-tree-thread-top)
633 (loop collect (funcall function)
635 while (and (notmuch-tree-get-message-properties)
636 (not (notmuch-tree-get-prop :first))))))
638 (defun notmuch-tree-get-messages-ids-thread-search ()
639 "Return a search string for all message ids of messages in the current thread."
641 (notmuch-tree-thread-mapcar 'notmuch-tree-get-message-id)
644 (defun notmuch-tree-tag-thread (tag-changes)
645 "Tag all messages in the current thread"
647 (let ((tags (apply #'append (notmuch-tree-thread-mapcar
648 (lambda () (notmuch-tree-get-tags))))))
649 (list (notmuch-read-tag-changes tags "Tag thread"))))
650 (when (notmuch-tree-get-message-properties)
651 (notmuch-tag (notmuch-tree-get-messages-ids-thread-search) tag-changes)
652 (notmuch-tree-thread-mapcar
653 (lambda () (notmuch-tree-tag-update-display tag-changes)))))
655 (defun notmuch-tree-archive-thread (&optional unarchive)
656 "Archive each message in thread.
658 Archive each message currently shown by applying the tag changes
659 in `notmuch-archive-tags' to each. If a prefix argument is given,
660 the messages will be \"unarchived\", i.e. the tag changes in
661 `notmuch-archive-tags' will be reversed.
663 Note: This command is safe from any race condition of new messages
664 being delivered to the same thread. It does not archive the
665 entire thread, but only the messages shown in the current
668 (when notmuch-archive-tags
669 (notmuch-tree-tag-thread
670 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
672 ;; Functions below here display the tree buffer itself.
674 (defun notmuch-tree-clean-address (address)
675 "Try to clean a single email ADDRESS for display. Return
676 AUTHOR_NAME if present, otherwise return AUTHOR_EMAIL. Return
677 unchanged ADDRESS if parsing fails."
678 (let* ((clean-address (notmuch-clean-address address))
679 (p-address (car clean-address))
680 (p-name (cdr clean-address)))
682 ;; If we have a name return that otherwise return the address.
683 (or p-name p-address)))
685 (defun notmuch-tree-format-field (field format-string msg)
686 "Format a FIELD of MSG according to FORMAT-STRING and return string"
687 (let* ((headers (plist-get msg :headers))
688 (match (plist-get msg :match)))
691 (format format-string (notmuch-tree-format-field-list field msg)))
693 ((string-equal field "date")
694 (let ((face (if match
695 'notmuch-tree-match-date-face
696 'notmuch-tree-no-match-date-face)))
697 (propertize (format format-string (plist-get msg :date_relative)) 'face face)))
699 ((string-equal field "tree")
700 (let ((tree-status (plist-get msg :tree-status))
702 'notmuch-tree-match-tree-face
703 'notmuch-tree-no-match-tree-face)))
705 (propertize (format format-string
706 (mapconcat #'identity (reverse tree-status) ""))
709 ((string-equal field "subject")
710 (let ((bare-subject (notmuch-show-strip-re (plist-get headers :Subject)))
711 (previous-subject notmuch-tree-previous-subject)
713 'notmuch-tree-match-subject-face
714 'notmuch-tree-no-match-subject-face)))
716 (setq notmuch-tree-previous-subject bare-subject)
717 (propertize (format format-string
718 (if (string= previous-subject bare-subject)
723 ((string-equal field "authors")
724 (let ((author (notmuch-tree-clean-address (plist-get headers :From)))
725 (len (length (format format-string "")))
727 'notmuch-tree-match-author-face
728 'notmuch-tree-no-match-author-face)))
729 (when (> (length author) len)
730 (setq author (substring author 0 len)))
731 (propertize (format format-string author) 'face face)))
733 ((string-equal field "tags")
734 (let ((tags (plist-get msg :tags))
735 (orig-tags (plist-get msg :orig-tags))
737 'notmuch-tree-match-tag-face
738 'notmuch-tree-no-match-tag-face)))
739 (format format-string (notmuch-tag-format-tags tags orig-tags face)))))))
741 (defun notmuch-tree-format-field-list (field-list msg)
742 "Format fields of MSG according to FIELD-LIST and return string"
743 (let ((face (if (plist-get msg :match)
744 'notmuch-tree-match-face
745 'notmuch-tree-no-match-face))
747 (dolist (spec field-list result-string)
748 (let ((field-string (notmuch-tree-format-field (car spec) (cdr spec) msg)))
749 (setq result-string (concat result-string field-string))))
750 (notmuch-apply-face result-string face t)))
752 (defun notmuch-tree-insert-msg (msg)
753 "Insert the message MSG according to notmuch-tree-result-format"
754 ;; We need to save the previous subject as it will get overwritten
755 ;; by the insert-field calls.
756 (let ((previous-subject notmuch-tree-previous-subject))
757 (insert (notmuch-tree-format-field-list notmuch-tree-result-format msg))
758 (notmuch-tree-set-message-properties msg)
759 (notmuch-tree-set-prop :previous-subject previous-subject)
762 (defun notmuch-tree-goto-and-insert-msg (msg)
763 "Insert msg at the end of the buffer. Move point to msg if it is the target"
765 (goto-char (point-max))
766 (notmuch-tree-insert-msg msg))
767 (let ((msg-id (notmuch-id-to-query (plist-get msg :id)))
768 (target notmuch-tree-target-msg))
769 (when (or (and (not target) (plist-get msg :match))
770 (string= msg-id target))
771 (setq notmuch-tree-target-msg "found")
772 (goto-char (point-max))
774 (when notmuch-tree-open-target
775 (notmuch-tree-show-message-in)))))
777 (defun notmuch-tree-insert-tree (tree depth tree-status first last)
778 "Insert the message tree TREE at depth DEPTH in the current thread.
780 A message tree is another name for a single sub-thread: i.e., a
781 message together with all its descendents."
782 (let ((msg (car tree))
783 (replies (cadr tree)))
786 ((and (< 0 depth) (not last))
787 (push "├" tree-status))
788 ((and (< 0 depth) last)
789 (push "╰" tree-status))
790 ((and (eq 0 depth) first last)
791 ;; (push "─" tree-status)) choice between this and next line is matter of taste.
792 (push " " tree-status))
793 ((and (eq 0 depth) first (not last))
794 (push "┬" tree-status))
795 ((and (eq 0 depth) (not first) last)
796 (push "╰" tree-status))
797 ((and (eq 0 depth) (not first) (not last))
798 (push "├" tree-status)))
800 (push (concat (if replies "┬" "─") "►") tree-status)
801 (setq msg (plist-put msg :first (and first (eq 0 depth))))
802 (setq msg (plist-put msg :tree-status tree-status))
803 (setq msg (plist-put msg :orig-tags (plist-get msg :tags)))
804 (notmuch-tree-goto-and-insert-msg msg)
809 (push " " tree-status)
810 (push "│" tree-status))
812 (notmuch-tree-insert-thread replies (1+ depth) tree-status)))
814 (defun notmuch-tree-insert-thread (thread depth tree-status)
815 "Insert the collection of sibling sub-threads THREAD at depth DEPTH in the current forest."
816 (let ((n (length thread)))
817 (loop for tree in thread
818 for count from 1 to n
820 do (notmuch-tree-insert-tree tree depth tree-status (eq count 1) (eq count n)))))
822 (defun notmuch-tree-insert-forest-thread (forest-thread)
823 "Insert a single complete thread."
825 ;; Reset at the start of each main thread.
826 (setq notmuch-tree-previous-subject nil)
827 (notmuch-tree-insert-thread forest-thread 0 tree-status)))
829 (defun notmuch-tree-insert-forest (forest)
830 "Insert a forest of threads.
832 This function inserts a collection of several complete threads as
833 passed to it by notmuch-tree-process-filter."
834 (mapc 'notmuch-tree-insert-forest-thread forest))
836 (define-derived-mode notmuch-tree-mode fundamental-mode "notmuch-tree"
837 "Major mode displaying messages (as opposed to threads) of a notmuch search.
839 This buffer contains the results of a \"notmuch tree\" of your
840 email archives. Each line in the buffer represents a single
841 message giving the relative date, the author, subject, and any
844 Pressing \\[notmuch-tree-show-message] on any line displays that message.
846 Complete list of currently available key bindings:
848 \\{notmuch-tree-mode-map}"
850 (setq notmuch-buffer-refresh-function #'notmuch-tree-refresh-view)
852 (setq buffer-read-only t
855 (defun notmuch-tree-process-sentinel (proc msg)
856 "Add a message to let user know when \"notmuch tree\" exits"
857 (let ((buffer (process-buffer proc))
858 (status (process-status proc))
859 (exit-status (process-exit-status proc))
860 (never-found-target-thread nil))
861 (when (memq status '(exit signal))
862 (kill-buffer (process-get proc 'parse-buf))
863 (if (buffer-live-p buffer)
864 (with-current-buffer buffer
866 (let ((inhibit-read-only t)
868 (goto-char (point-max))
869 (if (eq status 'signal)
870 (insert "Incomplete search results (tree view process was killed).\n"))
871 (when (eq status 'exit)
872 (insert "End of search results.")
873 (unless (= exit-status 0)
874 (insert (format " (process returned %d)" exit-status)))
875 (insert "\n")))))))))
877 (defun notmuch-tree-process-filter (proc string)
878 "Process and filter the output of \"notmuch show\" for tree view"
879 (let ((results-buf (process-buffer proc))
880 (parse-buf (process-get proc 'parse-buf))
881 (inhibit-read-only t)
883 (if (not (buffer-live-p results-buf))
884 (delete-process proc)
885 (with-current-buffer parse-buf
888 (goto-char (point-max))
890 (notmuch-sexp-parse-partial-list 'notmuch-tree-insert-forest-thread
893 (defun notmuch-tree-worker (basic-query &optional query-context target open-target)
894 "Insert the tree view of the search in the current buffer.
896 This is is a helper function for notmuch-tree. The arguments are
897 the same as for the function notmuch-tree."
900 (add-hook 'post-command-hook #'notmuch-tree-command-hook t t)
901 (setq notmuch-tree-basic-query basic-query)
902 (setq notmuch-tree-query-context (if (or (string= query-context "")
903 (string= query-context "*"))
905 (setq notmuch-tree-target-msg target)
906 (setq notmuch-tree-open-target open-target)
907 ;; Set the default value for `notmuch-show-process-crypto' in this
908 ;; buffer. Although we don't use this some of the functions we call
909 ;; (such as reply) do. It is a buffer local variable so setting it
910 ;; will not affect genuine show buffers.
911 (setq notmuch-show-process-crypto notmuch-crypto-process-mime)
914 (goto-char (point-min))
915 (let* ((search-args (concat basic-query
916 (if query-context (concat " and (" query-context ")"))
918 (message-arg "--entire-thread"))
919 (if (equal (car (process-lines notmuch-command "count" search-args)) "0")
920 (setq search-args basic-query))
921 (notmuch-tag-clear-cache)
922 (let ((proc (notmuch-start-notmuch
923 "notmuch-tree" (current-buffer) #'notmuch-tree-process-sentinel
924 "show" "--body=false" "--format=sexp" "--format-version=4"
925 message-arg search-args))
926 ;; Use a scratch buffer to accumulate partial output.
927 ;; This buffer will be killed by the sentinel, which
928 ;; should be called no matter how the process dies.
929 (parse-buf (generate-new-buffer " *notmuch tree parse*")))
930 (process-put proc 'parse-buf parse-buf)
931 (set-process-filter proc 'notmuch-tree-process-filter)
932 (set-process-query-on-exit-flag proc nil))))
934 (defun notmuch-tree-get-query ()
935 "Return the current query in this tree buffer"
936 (if notmuch-tree-query-context
937 (concat notmuch-tree-basic-query
939 notmuch-tree-query-context
941 notmuch-tree-basic-query))
943 (defun notmuch-tree (&optional query query-context target buffer-name open-target)
944 "Display threads matching QUERY in Tree View.
947 QUERY: the main query. This can be any query but in many cases will be
948 a single thread. If nil this is read interactively from the minibuffer.
949 QUERY-CONTEXT: is an additional term for the query. The query used
950 is QUERY and QUERY-CONTEXT unless that does not match any messages
951 in which case we fall back to just QUERY.
952 TARGET: A message ID (with the id: prefix) that will be made
953 current if it appears in the tree view results.
954 BUFFER-NAME: the name of the buffer to display the tree view. If
955 it is nil \"*notmuch-tree\" followed by QUERY is used.
956 OPEN-TARGET: If TRUE open the target message in the message pane."
959 (setq query (notmuch-read-query "Notmuch tree view search: ")))
960 (let ((buffer (get-buffer-create (generate-new-buffer-name
962 (concat "*notmuch-tree-" query "*")))))
963 (inhibit-read-only t))
965 (switch-to-buffer buffer))
966 ;; Don't track undo information for this buffer
967 (set 'buffer-undo-list t)
969 (notmuch-tree-worker query query-context target open-target)
971 (setq truncate-lines t))
976 (provide 'notmuch-tree)
978 ;;; notmuch-tree.el ends here