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 <http://www.gnu.org/licenses/>.
22 ;; Authors: David Edmondson <dme@dme.org>
23 ;; Mark Walters <markwalters1009@gmail.com>
27 (require 'notmuch-lib)
28 (require 'notmuch-query)
29 (require 'notmuch-show)
30 (require 'notmuch) ;; XXX ATM, as notmuch-search-mode-map is defined here
32 (eval-when-compile (require 'cl))
34 (declare-function notmuch-call-notmuch-process "notmuch" (&rest args))
35 (declare-function notmuch-show "notmuch-show" (&rest args))
36 (declare-function notmuch-tag "notmuch" (query &rest tags))
37 (declare-function notmuch-show-strip-re "notmuch-show" (subject))
38 (declare-function notmuch-show-spaces-n "notmuch-show" (n))
39 (declare-function notmuch-read-query "notmuch" (prompt))
40 (declare-function notmuch-read-tag-changes "notmuch" (current-tags &optional prompt initial-input))
41 (declare-function notmuch-update-tags "notmuch" (current-tags tag-changes))
42 (declare-function notmuch-hello-trim "notmuch-hello" (search))
43 (declare-function notmuch-search-find-thread-id "notmuch" ())
44 (declare-function notmuch-search-find-subject "notmuch" ())
46 ;; the following variable is defined in notmuch.el
47 (defvar notmuch-search-query-string)
49 (defgroup notmuch-tree nil
50 "Showing message and thread structure."
53 (defcustom notmuch-tree-show-out nil
54 "View selected messages in new window rather than split-pane."
58 (defcustom notmuch-tree-result-format
61 ((("tree" . "%s")("subject" . "%s")) ." %-54s ")
63 "Result formatting for Tree view. Supported fields are: date,
64 authors, subject, tree, tags. Tree means the thread tree
65 box graphics. The field may also be a list in which case
66 the formatting rules are applied recursively and then the
67 output of all the fields in the list is inserted
68 according to format-string.
70 Note the author string should not contain
71 whitespace (put it in the neighbouring fields instead).
73 (setq notmuch-tree-result-format \(\(\"authors\" . \"%-40s\"\)
74 \(\"subject\" . \"%s\"\)\)\)"
75 :type '(alist :key-type (string) :value-type (string))
78 ;; Faces for messages that match the query.
79 (defface notmuch-tree-match-date-face
80 '((t :inherit default))
81 "Face used in tree mode for the date in messages matching the query."
83 :group 'notmuch-faces)
85 (defface notmuch-tree-match-author-face
88 (:foreground "OliveDrab1"))
91 (:foreground "dark blue"))
94 "Face used in tree mode for the date in messages matching the query."
96 :group 'notmuch-faces)
98 (defface notmuch-tree-match-subject-face
99 '((t :inherit default))
100 "Face used in tree mode for the subject in messages matching the query."
102 :group 'notmuch-faces)
104 (defface notmuch-tree-match-tree-face
105 '((t :inherit default))
106 "Face used in tree mode for the thread tree block graphics in messages matching the query."
108 :group 'notmuch-faces)
110 (defface notmuch-tree-match-tag-face
113 (:foreground "OliveDrab1"))
116 (:foreground "navy blue" :bold t))
119 "Face used in tree mode for tags in messages matching the query."
121 :group 'notmuch-faces)
123 ;; Faces for messages that do not match the query.
124 (defface notmuch-tree-no-match-date-face
125 '((t (:foreground "gray")))
126 "Face used in tree mode for non-matching dates."
128 :group 'notmuch-faces)
130 (defface notmuch-tree-no-match-subject-face
131 '((t (:foreground "gray")))
132 "Face used in tree mode for non-matching subjects."
134 :group 'notmuch-faces)
136 (defface notmuch-tree-no-match-tree-face
137 '((t (:foreground "gray")))
138 "Face used in tree mode for the thread tree block graphics in messages matching the query."
140 :group 'notmuch-faces)
142 (defface notmuch-tree-no-match-author-face
143 '((t (:foreground "gray")))
144 "Face used in tree mode for the date in messages matching the query."
146 :group 'notmuch-faces)
148 (defface notmuch-tree-no-match-tag-face
149 '((t (:foreground "gray")))
150 "Face used in tree mode face for non-matching tags."
152 :group 'notmuch-faces)
154 (defvar notmuch-tree-previous-subject
155 "The subject of the most recent result shown during the async display")
156 (make-variable-buffer-local 'notmuch-tree-previous-subject)
158 (defvar notmuch-tree-basic-query nil
159 "A buffer local copy of argument query to the function notmuch-tree")
160 (make-variable-buffer-local 'notmuch-tree-basic-query)
162 (defvar notmuch-tree-query-context nil
163 "A buffer local copy of argument query-context to the function notmuch-tree")
164 (make-variable-buffer-local 'notmuch-tree-query-context)
166 (defvar notmuch-tree-target-msg nil
167 "A buffer local copy of argument target to the function notmuch-tree")
168 (make-variable-buffer-local 'notmuch-tree-target-msg)
170 (defvar notmuch-tree-open-target nil
171 "A buffer local copy of argument open-target to the function notmuch-tree")
172 (make-variable-buffer-local 'notmuch-tree-open-target)
174 (defvar notmuch-tree-message-window nil
175 "The window of the message pane.
177 It is set in both the tree buffer and the child show buffer. It
178 is used to try and close the message pane when quitting tree view
179 or the child show buffer.")
180 (make-variable-buffer-local 'notmuch-tree-message-window)
181 (put 'notmuch-tree-message-window 'permanent-local t)
183 (defvar notmuch-tree-message-buffer nil
184 "The buffer name of the show buffer in the message pane.
186 This is used to try and make sure we don't close the message pane
187 if the user has loaded a different buffer in that window.")
188 (make-variable-buffer-local 'notmuch-tree-message-buffer)
189 (put 'notmuch-tree-message-buffer 'permanent-local t)
191 (defun notmuch-tree-to-message-pane (func)
192 "Execute FUNC in message pane.
194 This function returns a function (so can be used as a keybinding)
195 which executes function FUNC in the message pane if it is
196 open (if the message pane is closed it does nothing)."
198 ,(concat "(In message pane) " (documentation func t))
200 (when (window-live-p notmuch-tree-message-window)
201 (with-selected-window notmuch-tree-message-window
202 (call-interactively #',func)))))
204 (defun notmuch-tree-button-activate (&optional button)
205 "Activate BUTTON or button at point
207 This function does not give an error if there is no button."
209 (let ((button (or button (button-at (point)))))
210 (when button (button-activate button))))
212 (defun notmuch-tree-close-message-pane-and (func)
213 "Close message pane and execute FUNC.
215 This function returns a function (so can be used as a keybinding)
216 which closes the message pane if open and then executes function
219 ,(concat "(Close message pane and) " (documentation func t))
221 (notmuch-tree-close-message-window)
222 (call-interactively #',func)))
224 (defvar notmuch-tree-mode-map
225 (let ((map (make-sparse-keymap)))
226 (set-keymap-parent map notmuch-common-keymap)
227 ;; The following override the global keymap.
228 ;; Override because we want to close message pane first.
229 (define-key map "?" (notmuch-tree-close-message-pane-and #'notmuch-help))
230 ;; Override because we first close message pane and then close tree buffer.
231 (define-key map "q" 'notmuch-tree-quit)
232 ;; Override because we close message pane after the search query is entered.
233 (define-key map "s" 'notmuch-tree-to-search)
234 ;; Override because we want to close message pane first.
235 (define-key map "m" (notmuch-tree-close-message-pane-and #'notmuch-mua-new-mail))
237 ;; these use notmuch-show functions directly
238 (define-key map "|" 'notmuch-show-pipe-message)
239 (define-key map "w" 'notmuch-show-save-attachments)
240 (define-key map "v" 'notmuch-show-view-all-mime-parts)
241 (define-key map "c" 'notmuch-show-stash-map)
243 ;; these apply to the message pane
244 (define-key map (kbd "M-TAB") (notmuch-tree-to-message-pane #'notmuch-show-previous-button))
245 (define-key map (kbd "<backtab>") (notmuch-tree-to-message-pane #'notmuch-show-previous-button))
246 (define-key map (kbd "TAB") (notmuch-tree-to-message-pane #'notmuch-show-next-button))
247 (define-key map "e" (notmuch-tree-to-message-pane #'notmuch-tree-button-activate))
249 ;; bindings from show (or elsewhere) but we close the message pane first.
250 (define-key map "f" (notmuch-tree-close-message-pane-and #'notmuch-show-forward-message))
251 (define-key map "r" (notmuch-tree-close-message-pane-and #'notmuch-show-reply-sender))
252 (define-key map "R" (notmuch-tree-close-message-pane-and #'notmuch-show-reply))
253 (define-key map "V" (notmuch-tree-close-message-pane-and #'notmuch-show-view-raw-message))
255 ;; The main tree view bindings
256 (define-key map (kbd "RET") 'notmuch-tree-show-message)
257 (define-key map [mouse-1] 'notmuch-tree-show-message)
258 (define-key map "x" 'notmuch-tree-quit)
259 (define-key map "A" 'notmuch-tree-archive-thread)
260 (define-key map "a" 'notmuch-tree-archive-message-then-next)
261 (define-key map "=" 'notmuch-tree-refresh-view)
262 (define-key map "z" 'notmuch-tree-to-tree)
263 (define-key map "n" 'notmuch-tree-next-matching-message)
264 (define-key map "p" 'notmuch-tree-prev-matching-message)
265 (define-key map "N" 'notmuch-tree-next-message)
266 (define-key map "P" 'notmuch-tree-prev-message)
267 (define-key map (kbd "M-p") 'notmuch-tree-prev-thread)
268 (define-key map (kbd "M-n") 'notmuch-tree-next-thread)
269 (define-key map "-" 'notmuch-tree-remove-tag)
270 (define-key map "+" 'notmuch-tree-add-tag)
271 (define-key map "*" 'notmuch-tree-tag-thread)
272 (define-key map " " 'notmuch-tree-scroll-or-next)
273 (define-key map "b" 'notmuch-tree-scroll-message-window-back)
275 (fset 'notmuch-tree-mode-map notmuch-tree-mode-map)
277 (defun notmuch-tree-get-message-properties ()
278 "Return the properties of the current message as a plist.
280 Some useful entries are:
281 :headers - Property list containing the headers :Date, :Subject, :From, etc.
282 :tags - Tags for this message"
285 (get-text-property (point) :notmuch-message-properties)))
287 ;; XXX This should really be a lib function but we are trying to
288 ;; reduce impact on the code base.
289 (defun notmuch-show-get-prop (prop &optional props)
290 "This is a tree view overridden version of notmuch-show-get-prop
292 It gets property PROP from PROPS or, if PROPS is nil, the current
293 message in either tree or show. This means that several functions
294 in notmuch-show now work unchanged in tree as they just need the
295 correct message properties."
296 (let ((props (or props
297 (cond ((eq major-mode 'notmuch-show-mode)
298 (notmuch-show-get-message-properties))
299 ((eq major-mode 'notmuch-tree-mode)
300 (notmuch-tree-get-message-properties))))))
301 (plist-get props prop)))
303 (defun notmuch-tree-set-message-properties (props)
306 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
308 (defun notmuch-tree-set-prop (prop val &optional props)
309 (let ((inhibit-read-only t)
311 (notmuch-tree-get-message-properties))))
312 (plist-put props prop val)
313 (notmuch-tree-set-message-properties props)))
315 (defun notmuch-tree-get-prop (prop &optional props)
316 (let ((props (or props
317 (notmuch-tree-get-message-properties))))
318 (plist-get props prop)))
320 (defun notmuch-tree-set-tags (tags)
321 "Set the tags of the current message."
322 (notmuch-tree-set-prop :tags tags))
324 (defun notmuch-tree-get-tags ()
325 "Return the tags of the current message."
326 (notmuch-tree-get-prop :tags))
328 (defun notmuch-tree-get-message-id ()
329 "Return the message id of the current message."
330 (let ((id (notmuch-tree-get-prop :id)))
332 (notmuch-id-to-query id)
335 (defun notmuch-tree-get-match ()
336 "Return whether the current message is a match."
338 (notmuch-tree-get-prop :match))
340 (defun notmuch-tree-refresh-result ()
341 "Redisplay the current message line.
343 This redisplays the current line based on the messages
344 properties (as they are now). This is used when tags are
346 (let ((init-point (point))
347 (end (line-end-position))
348 (msg (notmuch-tree-get-message-properties))
349 (inhibit-read-only t))
351 ;; This is a little tricky: we override
352 ;; notmuch-tree-previous-subject to get the decision between
353 ;; ... and a subject right and it stops notmuch-tree-insert-msg
354 ;; from overwriting the buffer local copy of
355 ;; notmuch-tree-previous-subject if this is called while the
356 ;; buffer is displaying.
357 (let ((notmuch-tree-previous-subject (notmuch-tree-get-prop :previous-subject)))
358 (delete-region (point) (1+ (line-end-position)))
359 (notmuch-tree-insert-msg msg))
360 (let ((new-end (line-end-position)))
361 (goto-char (if (= init-point end)
363 (min init-point (- new-end 1)))))))
365 (defun notmuch-tree-tag-update-display (&optional tag-changes)
366 "Update display for TAG-CHANGES to current message.
368 Does NOT change the database."
369 (let* ((current-tags (notmuch-tree-get-tags))
370 (new-tags (notmuch-update-tags current-tags tag-changes)))
371 (unless (equal current-tags new-tags)
372 (notmuch-tree-set-tags new-tags)
373 (notmuch-tree-refresh-result))))
375 (defun notmuch-tree-tag (tag-changes)
376 "Change tags for the current message"
378 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message")))
379 (notmuch-tag (notmuch-tree-get-message-id) tag-changes)
380 (notmuch-tree-tag-update-display tag-changes))
382 (defun notmuch-tree-add-tag (tag-changes)
383 "Same as `notmuch-tree-tag' but sets initial input to '+'."
385 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "+")))
386 (notmuch-tree-tag tag-changes))
388 (defun notmuch-tree-remove-tag (tag-changes)
389 "Same as `notmuch-tree-tag' but sets initial input to '-'."
391 (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "-")))
392 (notmuch-tree-tag tag-changes))
394 ;; The next two functions close the message window before calling
395 ;; notmuch-search or notmuch-tree but they do so after the user has
396 ;; entered the query (in case the user was basing the query on
397 ;; something in the message window).
399 (defun notmuch-tree-to-search ()
400 "Run \"notmuch search\" with the given `query' and display results."
402 (let ((query (notmuch-read-query "Notmuch search: ")))
403 (notmuch-tree-close-message-window)
404 (notmuch-search query)))
406 (defun notmuch-tree-to-tree ()
407 "Run a query and display results in Tree view"
409 (let ((query (notmuch-read-query "Notmuch tree view search: ")))
410 (notmuch-tree-close-message-window)
411 (notmuch-tree query)))
413 ;; This function should be in notmuch-show.el but be we trying to
414 ;; minimise impact on the rest of the codebase.
415 (defun notmuch-tree-from-show-current-query ()
416 "Call notmuch tree with the current query"
418 (notmuch-tree notmuch-show-thread-id
419 notmuch-show-query-context
420 (notmuch-show-get-message-id)))
422 ;; This function should be in notmuch.el but be we trying to minimise
423 ;; impact on the rest of the codebase.
424 (defun notmuch-tree-from-search-current-query ()
425 "Call notmuch tree with the current query"
427 (notmuch-tree notmuch-search-query-string))
429 ;; This function should be in notmuch.el but be we trying to minimise
430 ;; impact on the rest of the codebase.
431 (defun notmuch-tree-from-search-thread ()
432 "Show the selected thread with notmuch-tree"
434 (notmuch-tree (notmuch-search-find-thread-id)
435 notmuch-search-query-string
437 (notmuch-prettify-subject (notmuch-search-find-subject))
440 (defun notmuch-tree-message-window-kill-hook ()
441 "Close the message pane when exiting the show buffer."
442 (let ((buffer (current-buffer)))
443 (when (and (window-live-p notmuch-tree-message-window)
444 (eq (window-buffer notmuch-tree-message-window) buffer))
445 ;; We do not want an error if this is the sole window in the
446 ;; frame and I do not know how to test for that in emacs pre
447 ;; 24. Hence we just ignore-errors.
449 (delete-window notmuch-tree-message-window)))))
451 (defun notmuch-tree-show-message-in ()
452 "Show the current message (in split-pane)."
454 (let ((id (notmuch-tree-get-message-id))
455 (inhibit-read-only t)
458 ;; We close and reopen the window to kill off un-needed buffers
459 ;; this might cause flickering but seems ok.
460 (notmuch-tree-close-message-window)
461 (setq notmuch-tree-message-window
462 (split-window-vertically (/ (window-height) 4)))
463 (with-selected-window notmuch-tree-message-window
464 ;; Since we are only displaying one message do not indent.
465 (let ((notmuch-show-indent-messages-width 0)
466 (notmuch-show-only-matching-messages t))
467 (setq buffer (notmuch-show id))))
468 ;; We need the `let' as notmuch-tree-message-window is buffer local.
469 (let ((window notmuch-tree-message-window))
470 (with-current-buffer buffer
471 (setq notmuch-tree-message-window window)
472 (add-hook 'kill-buffer-hook 'notmuch-tree-message-window-kill-hook)))
473 (when notmuch-show-mark-read-tags
474 (notmuch-tree-tag-update-display notmuch-show-mark-read-tags))
475 (setq notmuch-tree-message-buffer buffer))))
477 (defun notmuch-tree-show-message-out ()
478 "Show the current message (in whole window)."
480 (let ((id (notmuch-tree-get-message-id))
481 (inhibit-read-only t)
484 ;; We close the window to kill off un-needed buffers.
485 (notmuch-tree-close-message-window)
488 (defun notmuch-tree-show-message (arg)
489 "Show the current message.
491 Shows in split pane or whole window according to value of
492 `notmuch-tree-show-out'. A prefix argument reverses the choice."
494 (if (or (and notmuch-tree-show-out (not arg))
495 (and (not notmuch-tree-show-out) arg))
496 (notmuch-tree-show-message-out)
497 (notmuch-tree-show-message-in)))
499 (defun notmuch-tree-scroll-message-window ()
500 "Scroll the message window (if it exists)"
502 (when (window-live-p notmuch-tree-message-window)
503 (with-selected-window notmuch-tree-message-window
504 (if (pos-visible-in-window-p (point-max))
508 (defun notmuch-tree-scroll-message-window-back ()
509 "Scroll the message window back(if it exists)"
511 (when (window-live-p notmuch-tree-message-window)
512 (with-selected-window notmuch-tree-message-window
513 (if (pos-visible-in-window-p (point-min))
517 (defun notmuch-tree-scroll-or-next ()
518 "Scroll the message window. If it at end go to next message."
520 (when (notmuch-tree-scroll-message-window)
521 (notmuch-tree-next-matching-message)))
523 (defun notmuch-tree-quit ()
524 "Close the split view or exit tree."
526 (unless (notmuch-tree-close-message-window)
527 (kill-buffer (current-buffer))))
529 (defun notmuch-tree-close-message-window ()
530 "Close the message-window. Return t if close succeeds."
532 (when (and (window-live-p notmuch-tree-message-window)
533 (eq (window-buffer notmuch-tree-message-window) notmuch-tree-message-buffer))
534 (delete-window notmuch-tree-message-window)
535 (unless (get-buffer-window-list notmuch-tree-message-buffer)
536 (kill-buffer notmuch-tree-message-buffer))
539 (defun notmuch-tree-archive-message (&optional unarchive)
540 "Archive the current message.
542 Archive the current message by applying the tag changes in
543 `notmuch-archive-tags' to it. If a prefix argument is given, the
544 message will be \"unarchived\", i.e. the tag changes in
545 `notmuch-archive-tags' will be reversed."
547 (when notmuch-archive-tags
548 (notmuch-tree-tag (notmuch-tag-change-list notmuch-archive-tags unarchive))))
550 (defun notmuch-tree-archive-message-then-next (&optional unarchive)
551 "Archive the current message and move to next matching message."
553 (notmuch-tree-archive-message unarchive)
554 (notmuch-tree-next-matching-message))
556 (defun notmuch-tree-next-message ()
557 "Move to next message."
560 (when (window-live-p notmuch-tree-message-window)
561 (notmuch-tree-show-message-in)))
563 (defun notmuch-tree-prev-message ()
564 "Move to previous message."
567 (when (window-live-p notmuch-tree-message-window)
568 (notmuch-tree-show-message-in)))
570 (defun notmuch-tree-prev-matching-message ()
571 "Move to previous matching message."
574 (while (and (not (bobp)) (not (notmuch-tree-get-match)))
576 (when (window-live-p notmuch-tree-message-window)
577 (notmuch-tree-show-message-in)))
579 (defun notmuch-tree-next-matching-message ()
580 "Move to next matching message."
583 (while (and (not (eobp)) (not (notmuch-tree-get-match)))
585 (when (window-live-p notmuch-tree-message-window)
586 (notmuch-tree-show-message-in)))
588 (defun notmuch-tree-refresh-view ()
591 (let ((inhibit-read-only t)
592 (basic-query notmuch-tree-basic-query)
593 (query-context notmuch-tree-query-context)
594 (target (notmuch-tree-get-message-id)))
596 (notmuch-tree-worker basic-query
600 (defun notmuch-tree-thread-top ()
601 (when (notmuch-tree-get-message-properties)
602 (while (not (or (notmuch-tree-get-prop :first) (eobp)))
605 (defun notmuch-tree-prev-thread ()
608 (notmuch-tree-thread-top))
610 (defun notmuch-tree-next-thread ()
613 (while (not (or (notmuch-tree-get-prop :first) (eobp)))
616 (defun notmuch-tree-thread-mapcar (function)
617 "Iterate through all messages in the current thread
618 and call FUNCTION for side effects."
620 (notmuch-tree-thread-top)
621 (loop collect (funcall function)
623 while (and (notmuch-tree-get-message-properties)
624 (not (notmuch-tree-get-prop :first))))))
626 (defun notmuch-tree-get-messages-ids-thread-search ()
627 "Return a search string for all message ids of messages in the current thread."
629 (notmuch-tree-thread-mapcar 'notmuch-tree-get-message-id)
632 (defun notmuch-tree-tag-thread (tag-changes)
633 "Tag all messages in the current thread"
635 (let ((tags (apply #'append (notmuch-tree-thread-mapcar
636 (lambda () (notmuch-tree-get-tags))))))
637 (list (notmuch-read-tag-changes tags "Tag thread"))))
638 (when (notmuch-tree-get-message-properties)
639 (notmuch-tag (notmuch-tree-get-messages-ids-thread-search) tag-changes)
640 (notmuch-tree-thread-mapcar
641 (lambda () (notmuch-tree-tag-update-display tag-changes)))))
643 (defun notmuch-tree-archive-thread (&optional unarchive)
644 "Archive each message in thread.
646 Archive each message currently shown by applying the tag changes
647 in `notmuch-archive-tags' to each. If a prefix argument is given,
648 the messages will be \"unarchived\", i.e. the tag changes in
649 `notmuch-archive-tags' will be reversed.
651 Note: This command is safe from any race condition of new messages
652 being delivered to the same thread. It does not archive the
653 entire thread, but only the messages shown in the current
656 (when notmuch-archive-tags
657 (notmuch-tree-tag-thread
658 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
660 ;; Functions below here display the tree buffer itself.
662 (defun notmuch-tree-clean-address (address)
663 "Try to clean a single email ADDRESS for display. Return
664 AUTHOR_NAME if present, otherwise return AUTHOR_EMAIL. Return
665 unchanged ADDRESS if parsing fails."
666 (let* ((clean-address (notmuch-clean-address address))
667 (p-address (car clean-address))
668 (p-name (cdr clean-address)))
670 ;; If we have a name return that otherwise return the address.
671 (or p-name p-address)))
673 (defun notmuch-tree-format-field (field format-string msg)
674 "Format a FIELD of MSG according to FORMAT-STRING and return string"
675 (let* ((headers (plist-get msg :headers))
676 (match (plist-get msg :match)))
679 (format format-string (notmuch-tree-format-field-list field msg)))
681 ((string-equal field "date")
682 (let ((face (if match
683 'notmuch-tree-match-date-face
684 'notmuch-tree-no-match-date-face)))
685 (propertize (format format-string (plist-get msg :date_relative)) 'face face)))
687 ((string-equal field "tree")
688 (let ((tree-status (plist-get msg :tree-status))
690 'notmuch-tree-match-tree-face
691 'notmuch-tree-no-match-tree-face)))
693 (propertize (format format-string
694 (mapconcat #'identity (reverse tree-status) ""))
697 ((string-equal field "subject")
698 (let ((bare-subject (notmuch-show-strip-re (plist-get headers :Subject)))
699 (previous-subject notmuch-tree-previous-subject)
701 'notmuch-tree-match-subject-face
702 'notmuch-tree-no-match-subject-face)))
704 (setq notmuch-tree-previous-subject bare-subject)
705 (propertize (format format-string
706 (if (string= previous-subject bare-subject)
711 ((string-equal field "authors")
712 (let ((author (notmuch-tree-clean-address (plist-get headers :From)))
713 (len (length (format format-string "")))
715 'notmuch-tree-match-author-face
716 'notmuch-tree-no-match-author-face)))
717 (when (> (length author) len)
718 (setq author (substring author 0 len)))
719 (propertize (format format-string author) 'face face)))
721 ((string-equal field "tags")
722 (let ((tags (plist-get msg :tags))
724 'notmuch-tree-match-tag-face
725 'notmuch-tree-no-match-tag-face)))
726 (propertize (format format-string
727 (mapconcat #'identity tags ", "))
731 (defun notmuch-tree-format-field-list (field-list msg)
732 "Format fields of MSG according to FIELD-LIST and return string"
734 (dolist (spec field-list result-string)
735 (let ((field-string (notmuch-tree-format-field (car spec) (cdr spec) msg)))
736 (setq result-string (concat result-string field-string))))))
738 (defun notmuch-tree-insert-msg (msg)
739 "Insert the message MSG according to notmuch-tree-result-format"
740 ;; We need to save the previous subject as it will get overwritten
741 ;; by the insert-field calls.
742 (let ((previous-subject notmuch-tree-previous-subject))
743 (insert (notmuch-tree-format-field-list notmuch-tree-result-format msg))
744 (notmuch-tree-set-message-properties msg)
745 (notmuch-tree-set-prop :previous-subject previous-subject)
748 (defun notmuch-tree-goto-and-insert-msg (msg)
749 "Insert msg at the end of the buffer. Move point to msg if it is the target"
751 (goto-char (point-max))
752 (notmuch-tree-insert-msg msg))
753 (let ((msg-id (notmuch-id-to-query (plist-get msg :id)))
754 (target notmuch-tree-target-msg))
755 (when (or (and (not target) (plist-get msg :match))
756 (string= msg-id target))
757 (setq notmuch-tree-target-msg "found")
758 (goto-char (point-max))
760 (when notmuch-tree-open-target
761 (notmuch-tree-show-message-in)))))
763 (defun notmuch-tree-insert-tree (tree depth tree-status first last)
764 "Insert the message tree TREE at depth DEPTH in the current thread.
766 A message tree is another name for a single sub-thread: i.e., a
767 message together with all its descendents."
768 (let ((msg (car tree))
769 (replies (cadr tree)))
772 ((and (< 0 depth) (not last))
773 (push "├" tree-status))
774 ((and (< 0 depth) last)
775 (push "╰" tree-status))
776 ((and (eq 0 depth) first last)
777 ;; (push "─" tree-status)) choice between this and next line is matter of taste.
778 (push " " tree-status))
779 ((and (eq 0 depth) first (not last))
780 (push "┬" tree-status))
781 ((and (eq 0 depth) (not first) last)
782 (push "╰" tree-status))
783 ((and (eq 0 depth) (not first) (not last))
784 (push "├" tree-status)))
786 (push (concat (if replies "┬" "─") "►") tree-status)
787 (plist-put msg :first (and first (eq 0 depth)))
788 (notmuch-tree-goto-and-insert-msg (plist-put msg :tree-status tree-status))
793 (push " " tree-status)
794 (push "│" tree-status))
796 (notmuch-tree-insert-thread replies (1+ depth) tree-status)))
798 (defun notmuch-tree-insert-thread (thread depth tree-status)
799 "Insert the collection of sibling sub-threads THREAD at depth DEPTH in the current forest."
800 (let ((n (length thread)))
801 (loop for tree in thread
802 for count from 1 to n
804 do (notmuch-tree-insert-tree tree depth tree-status (eq count 1) (eq count n)))))
806 (defun notmuch-tree-insert-forest-thread (forest-thread)
807 "Insert a single complete thread."
809 ;; Reset at the start of each main thread.
810 (setq notmuch-tree-previous-subject nil)
811 (notmuch-tree-insert-thread forest-thread 0 tree-status)))
813 (defun notmuch-tree-insert-forest (forest)
814 "Insert a forest of threads.
816 This function inserts a collection of several complete threads as
817 passed to it by notmuch-tree-process-filter."
818 (mapc 'notmuch-tree-insert-forest-thread forest))
820 (defun notmuch-tree-mode ()
821 "Major mode displaying messages (as opposed to threads) of of a notmuch search.
823 This buffer contains the results of a \"notmuch tree\" of your
824 email archives. Each line in the buffer represents a single
825 message giving the relative date, the author, subject, and any
828 Pressing \\[notmuch-tree-show-message] on any line displays that message.
830 Complete list of currently available key bindings:
832 \\{notmuch-tree-mode-map}"
835 (kill-all-local-variables)
836 (setq notmuch-buffer-refresh-function #'notmuch-tree-refresh-view)
837 (use-local-map notmuch-tree-mode-map)
838 (setq major-mode 'notmuch-tree-mode
839 mode-name "notmuch-tree")
841 (setq buffer-read-only t
844 (defun notmuch-tree-process-sentinel (proc msg)
845 "Add a message to let user know when \"notmuch tree\" exits"
846 (let ((buffer (process-buffer proc))
847 (status (process-status proc))
848 (exit-status (process-exit-status proc))
849 (never-found-target-thread nil))
850 (when (memq status '(exit signal))
851 (kill-buffer (process-get proc 'parse-buf))
852 (if (buffer-live-p buffer)
853 (with-current-buffer buffer
855 (let ((inhibit-read-only t)
857 (goto-char (point-max))
858 (if (eq status 'signal)
859 (insert "Incomplete search results (tree view process was killed).\n"))
860 (when (eq status 'exit)
861 (insert "End of search results.")
862 (unless (= exit-status 0)
863 (insert (format " (process returned %d)" exit-status)))
864 (insert "\n")))))))))
866 (defun notmuch-tree-process-filter (proc string)
867 "Process and filter the output of \"notmuch show\" for tree view"
868 (let ((results-buf (process-buffer proc))
869 (parse-buf (process-get proc 'parse-buf))
870 (inhibit-read-only t)
872 (if (not (buffer-live-p results-buf))
873 (delete-process proc)
874 (with-current-buffer parse-buf
877 (goto-char (point-max))
879 (notmuch-sexp-parse-partial-list 'notmuch-tree-insert-forest-thread
882 (defun notmuch-tree-worker (basic-query &optional query-context target open-target)
883 "Insert the tree view of the search in the current buffer.
885 This is is a helper function for notmuch-tree. The arguments are
886 the same as for the function notmuch-tree."
889 (setq notmuch-tree-basic-query basic-query)
890 (setq notmuch-tree-query-context query-context)
891 (setq notmuch-tree-target-msg target)
892 (setq notmuch-tree-open-target open-target)
895 (goto-char (point-min))
896 (let* ((search-args (concat basic-query
897 (if query-context (concat " and (" query-context ")"))
899 (message-arg "--entire-thread"))
900 (if (equal (car (process-lines notmuch-command "count" search-args)) "0")
901 (setq search-args basic-query))
902 (let ((proc (notmuch-start-notmuch
903 "notmuch-tree" (current-buffer) #'notmuch-tree-process-sentinel
904 "show" "--body=false" "--format=sexp"
905 message-arg search-args))
906 ;; Use a scratch buffer to accumulate partial output.
907 ;; This buffer will be killed by the sentinel, which
908 ;; should be called no matter how the process dies.
909 (parse-buf (generate-new-buffer " *notmuch tree parse*")))
910 (process-put proc 'parse-buf parse-buf)
911 (set-process-filter proc 'notmuch-tree-process-filter)
912 (set-process-query-on-exit-flag proc nil))))
914 (defun notmuch-tree (&optional query query-context target buffer-name open-target)
915 "Display threads matching QUERY in Tree View.
918 QUERY: the main query. This can be any query but in many cases will be
919 a single thread. If nil this is read interactively from the minibuffer.
920 QUERY-CONTEXT: is an additional term for the query. The query used
921 is QUERY and QUERY-CONTEXT unless that does not match any messages
922 in which case we fall back to just QUERY.
923 TARGET: A message ID (with the id: prefix) that will be made
924 current if it appears in the tree view results.
925 BUFFER-NAME: the name of the buffer to display the tree view. If
926 it is nil \"*notmuch-tree\" followed by QUERY is used.
927 OPEN-TARGET: If TRUE open the target message in the message pane."
930 (setq query (notmuch-read-query "Notmuch tree view search: ")))
931 (let ((buffer (get-buffer-create (generate-new-buffer-name
933 (concat "*notmuch-tree-" query "*")))))
934 (inhibit-read-only t))
936 (switch-to-buffer buffer))
937 ;; Don't track undo information for this buffer
938 (set 'buffer-undo-list t)
940 (notmuch-tree-worker query query-context target open-target)
942 (setq truncate-lines t))
945 ;; Set up key bindings from the rest of notmuch.
946 (define-key notmuch-common-keymap "z" 'notmuch-tree)
947 (define-key notmuch-search-mode-map "Z" 'notmuch-tree-from-search-current-query)
948 (define-key notmuch-show-mode-map "Z" 'notmuch-tree-from-show-current-query)
949 (message "Initialised notmuch-tree")
951 (provide 'notmuch-tree)