]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-tree.el
220c010dfbfa61e4f01d37961055176f69c1fa37
[notmuch] / emacs / notmuch-tree.el
1 ;;; notmuch-tree.el --- displaying notmuch forests.
2 ;;
3 ;; Copyright © Carl Worth
4 ;; Copyright © David Edmondson
5 ;; Copyright © Mark Walters
6 ;;
7 ;; This file is part of Notmuch.
8 ;;
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.
13 ;;
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.
18 ;;
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/>.
21 ;;
22 ;; Authors: David Edmondson <dme@dme.org>
23 ;;          Mark Walters <markwalters1009@gmail.com>
24
25 ;;; Code:
26
27 (require 'mail-parse)
28
29 (require 'notmuch-lib)
30 (require 'notmuch-query)
31 (require 'notmuch-show)
32 (require 'notmuch-tag)
33 (require 'notmuch-parser)
34
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" ())
41
42 ;; the following variable is defined in notmuch.el
43 (defvar notmuch-search-query-string)
44
45 (defgroup notmuch-tree nil
46   "Showing message and thread structure."
47   :group 'notmuch)
48
49 (defcustom notmuch-tree-show-out nil
50   "View selected messages in new window rather than split-pane."
51   :type 'boolean
52   :group 'notmuch-tree)
53
54 (defcustom notmuch-tree-result-format
55   `(("date" . "%12s  ")
56     ("authors" . "%-20s")
57     ((("tree" . "%s")("subject" . "%s")) ." %-54s ")
58     ("tags" . "(%s)"))
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.
65
66 Note the author string should not contain
67         whitespace (put it in the neighbouring fields instead).
68         For example:
69         (setq notmuch-tree-result-format \(\(\"authors\" . \"%-40s\"\)
70                                              \(\"subject\" . \"%s\"\)\)\)"
71   :type '(alist :key-type (string) :value-type (string))
72   :group 'notmuch-tree)
73
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"
78   :group 'notmuch-tree
79   :group 'notmuch-faces)
80
81 (defface notmuch-tree-match-date-face
82   nil
83   "Face used in tree mode for the date in messages matching the query."
84   :group 'notmuch-tree
85   :group 'notmuch-faces)
86
87 (defface notmuch-tree-match-author-face
88   '((((class color)
89       (background dark))
90      (:foreground "OliveDrab1"))
91     (((class color)
92       (background light))
93      (:foreground "dark blue"))
94     (t
95      (:bold t)))
96   "Face used in tree mode for the date in messages matching the query."
97   :group 'notmuch-tree
98   :group 'notmuch-faces)
99
100 (defface notmuch-tree-match-subject-face
101   nil
102   "Face used in tree mode for the subject in messages matching the query."
103   :group 'notmuch-tree
104   :group 'notmuch-faces)
105
106 (defface notmuch-tree-match-tree-face
107   nil
108   "Face used in tree mode for the thread tree block graphics in messages matching the query."
109   :group 'notmuch-tree
110   :group 'notmuch-faces)
111
112 (defface notmuch-tree-match-tag-face
113   '((((class color)
114       (background dark))
115      (:foreground "OliveDrab1"))
116     (((class color)
117       (background light))
118      (:foreground "navy blue" :bold t))
119     (t
120      (:bold t)))
121   "Face used in tree mode for tags in messages matching the query."
122   :group 'notmuch-tree
123   :group 'notmuch-faces)
124
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"
129   :group 'notmuch-tree
130   :group 'notmuch-faces)
131
132 (defface notmuch-tree-no-match-date-face
133   nil
134   "Face used in tree mode for non-matching dates."
135   :group 'notmuch-tree
136   :group 'notmuch-faces)
137
138 (defface notmuch-tree-no-match-subject-face
139   nil
140   "Face used in tree mode for non-matching subjects."
141   :group 'notmuch-tree
142   :group 'notmuch-faces)
143
144 (defface notmuch-tree-no-match-tree-face
145   nil
146   "Face used in tree mode for the thread tree block graphics in messages matching the query."
147   :group 'notmuch-tree
148   :group 'notmuch-faces)
149
150 (defface notmuch-tree-no-match-author-face
151   nil
152   "Face used in tree mode for the date in messages matching the query."
153   :group 'notmuch-tree
154   :group 'notmuch-faces)
155
156 (defface notmuch-tree-no-match-tag-face
157   nil
158   "Face used in tree mode face for non-matching tags."
159   :group 'notmuch-tree
160   :group 'notmuch-faces)
161
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)
165
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)
169
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)
173
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)
177
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)
181
182 (defvar notmuch-tree-message-window nil
183   "The window of the message pane.
184
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)
190
191 (defvar notmuch-tree-message-buffer nil
192   "The buffer name of the show buffer in the message pane.
193
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)
198
199 (defun notmuch-tree-to-message-pane (func)
200   "Execute FUNC in message pane.
201
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)."
205   `(lambda ()
206       ,(concat "(In message pane) " (documentation func t))
207      (interactive)
208      (when (window-live-p notmuch-tree-message-window)
209        (with-selected-window notmuch-tree-message-window
210          (call-interactively #',func)))))
211
212 (defun notmuch-tree-button-activate (&optional button)
213   "Activate BUTTON or button at point
214
215 This function does not give an error if there is no button."
216   (interactive)
217   (let ((button (or button (button-at (point)))))
218     (when button (button-activate button))))
219
220 (defun notmuch-tree-close-message-pane-and (func)
221   "Close message pane and execute FUNC.
222
223 This function returns a function (so can be used as a keybinding)
224 which closes the message pane if open and then executes function
225 FUNC."
226   `(lambda ()
227       ,(concat "(Close message pane and) " (documentation func t))
228      (interactive)
229      (notmuch-tree-close-message-window)
230      (call-interactively #',func)))
231
232 (defvar notmuch-tree-mode-map
233   (let ((map (make-sparse-keymap)))
234     (set-keymap-parent map notmuch-common-keymap)
235     ;; The following override the global keymap.
236     ;; Override because we want to close message pane first.
237     (define-key map [remap notmuch-help] (notmuch-tree-close-message-pane-and #'notmuch-help))
238     ;; Override because we first close message pane and then close tree buffer.
239     (define-key map [remap notmuch-bury-or-kill-this-buffer] 'notmuch-tree-quit)
240     ;; Override because we close message pane after the search query is entered.
241     (define-key map [remap notmuch-search] 'notmuch-tree-to-search)
242     ;; Override because we want to close message pane first.
243     (define-key map [remap notmuch-mua-new-mail] (notmuch-tree-close-message-pane-and #'notmuch-mua-new-mail))
244     ;; Override because we want to close message pane first.
245     (define-key map [remap notmuch-jump-search] (notmuch-tree-close-message-pane-and #'notmuch-jump-search))
246
247     (define-key map "S" 'notmuch-search-from-tree-current-query)
248
249     ;; these use notmuch-show functions directly
250     (define-key map "|" 'notmuch-show-pipe-message)
251     (define-key map "w" 'notmuch-show-save-attachments)
252     (define-key map "v" 'notmuch-show-view-all-mime-parts)
253     (define-key map "c" 'notmuch-show-stash-map)
254     (define-key map "b" 'notmuch-show-resend-message)
255
256     ;; these apply to the message pane
257     (define-key map (kbd "M-TAB") (notmuch-tree-to-message-pane #'notmuch-show-previous-button))
258     (define-key map (kbd "<backtab>")  (notmuch-tree-to-message-pane #'notmuch-show-previous-button))
259     (define-key map (kbd "TAB") (notmuch-tree-to-message-pane #'notmuch-show-next-button))
260     (define-key map "e" (notmuch-tree-to-message-pane #'notmuch-tree-button-activate))
261     (define-key map "$" (notmuch-tree-to-message-pane #'notmuch-show-toggle-process-crypto))
262
263     ;; bindings from show (or elsewhere) but we close the message pane first.
264     (define-key map "f" (notmuch-tree-close-message-pane-and #'notmuch-show-forward-message))
265     (define-key map "r" (notmuch-tree-close-message-pane-and #'notmuch-show-reply-sender))
266     (define-key map "R" (notmuch-tree-close-message-pane-and #'notmuch-show-reply))
267     (define-key map "V" (notmuch-tree-close-message-pane-and #'notmuch-show-view-raw-message))
268
269     ;; The main tree view bindings
270     (define-key map (kbd "RET") 'notmuch-tree-show-message)
271     (define-key map [mouse-1] 'notmuch-tree-show-message)
272     (define-key map "x" 'notmuch-tree-quit)
273     (define-key map "A" 'notmuch-tree-archive-thread)
274     (define-key map "a" 'notmuch-tree-archive-message-then-next)
275     (define-key map "z" 'notmuch-tree-to-tree)
276     (define-key map "n" 'notmuch-tree-next-matching-message)
277     (define-key map "p" 'notmuch-tree-prev-matching-message)
278     (define-key map "N" 'notmuch-tree-next-message)
279     (define-key map "P" 'notmuch-tree-prev-message)
280     (define-key map (kbd "M-p") 'notmuch-tree-prev-thread)
281     (define-key map (kbd "M-n") 'notmuch-tree-next-thread)
282     (define-key map "k" 'notmuch-tag-jump)
283     (define-key map "-" 'notmuch-tree-remove-tag)
284     (define-key map "+" 'notmuch-tree-add-tag)
285     (define-key map "*" 'notmuch-tree-tag-thread)
286     (define-key map " " 'notmuch-tree-scroll-or-next)
287     (define-key map (kbd "DEL") 'notmuch-tree-scroll-message-window-back)
288     map))
289 (fset 'notmuch-tree-mode-map notmuch-tree-mode-map)
290
291 (defun notmuch-tree-get-message-properties ()
292   "Return the properties of the current message as a plist.
293
294 Some useful entries are:
295 :headers - Property list containing the headers :Date, :Subject, :From, etc.
296 :tags - Tags for this message"
297   (save-excursion
298     (beginning-of-line)
299     (get-text-property (point) :notmuch-message-properties)))
300
301 (defun notmuch-tree-set-message-properties (props)
302   (save-excursion
303     (beginning-of-line)
304     (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
305
306 (defun notmuch-tree-set-prop (prop val &optional props)
307   (let ((inhibit-read-only t)
308         (props (or props
309                    (notmuch-tree-get-message-properties))))
310     (plist-put props prop val)
311     (notmuch-tree-set-message-properties props)))
312
313 (defun notmuch-tree-get-prop (prop &optional props)
314   (let ((props (or props
315                    (notmuch-tree-get-message-properties))))
316     (plist-get props prop)))
317
318 (defun notmuch-tree-set-tags (tags)
319   "Set the tags of the current message."
320   (notmuch-tree-set-prop :tags tags))
321
322 (defun notmuch-tree-get-tags ()
323   "Return the tags of the current message."
324   (notmuch-tree-get-prop :tags))
325
326 (defun notmuch-tree-get-message-id (&optional bare)
327   "Return the message id of the current message."
328   (let ((id (notmuch-tree-get-prop :id)))
329     (if id
330         (if bare
331             id
332           (notmuch-id-to-query id))
333       nil)))
334
335 (defun notmuch-tree-get-match ()
336   "Return whether the current message is a match."
337   (interactive)
338   (notmuch-tree-get-prop :match))
339
340 (defun notmuch-tree-refresh-result ()
341   "Redisplay the current message line.
342
343 This redisplays the current line based on the messages
344 properties (as they are now). This is used when tags are
345 updated."
346   (let ((init-point (point))
347         (end (line-end-position))
348         (msg (notmuch-tree-get-message-properties))
349         (inhibit-read-only t))
350     (beginning-of-line)
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)
362                      new-end
363                    (min init-point (- new-end 1)))))))
364
365 (defun notmuch-tree-tag-update-display (&optional tag-changes)
366   "Update display for TAG-CHANGES to current message.
367
368 Updates the message in the message pane if appropriate, but does
369 NOT change the database."
370   (let* ((current-tags (notmuch-tree-get-tags))
371          (new-tags (notmuch-update-tags current-tags tag-changes))
372          (tree-msg-id (notmuch-tree-get-message-id)))
373     (unless (equal current-tags new-tags)
374       (notmuch-tree-set-tags new-tags)
375       (notmuch-tree-refresh-result)
376       (when (window-live-p notmuch-tree-message-window)
377         (with-selected-window notmuch-tree-message-window
378           (when (string= tree-msg-id (notmuch-show-get-message-id))
379             (notmuch-show-update-tags new-tags)))))))
380
381 (defun notmuch-tree-tag (tag-changes)
382   "Change tags for the current message"
383   (interactive
384    (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message")))
385   (notmuch-tag (notmuch-tree-get-message-id) tag-changes)
386   (notmuch-tree-tag-update-display tag-changes))
387
388 (defun notmuch-tree-add-tag (tag-changes)
389   "Same as `notmuch-tree-tag' but sets initial input to '+'."
390   (interactive
391    (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "+")))
392   (notmuch-tree-tag tag-changes))
393
394 (defun notmuch-tree-remove-tag (tag-changes)
395   "Same as `notmuch-tree-tag' but sets initial input to '-'."
396   (interactive
397    (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "-")))
398   (notmuch-tree-tag tag-changes))
399
400 ;; The next two functions close the message window before calling
401 ;; notmuch-search or notmuch-tree but they do so after the user has
402 ;; entered the query (in case the user was basing the query on
403 ;; something in the message window).
404
405 (defun notmuch-tree-to-search ()
406   "Run \"notmuch search\" with the given `query' and display results."
407   (interactive)
408   (let ((query (notmuch-read-query "Notmuch search: ")))
409     (notmuch-tree-close-message-window)
410     (notmuch-search query)))
411
412 (defun notmuch-tree-to-tree ()
413   "Run a query and display results in Tree view"
414   (interactive)
415   (let ((query (notmuch-read-query "Notmuch tree view search: ")))
416     (notmuch-tree-close-message-window)
417     (notmuch-tree query)))
418
419 (defun notmuch-search-from-tree-current-query ()
420   "Call notmuch search with the current query"
421   (interactive)
422   (notmuch-tree-close-message-window)
423   (notmuch-search (notmuch-tree-get-query)))
424
425 (defun notmuch-tree-message-window-kill-hook ()
426   "Close the message pane when exiting the show buffer."
427   (let ((buffer (current-buffer)))
428     (when (and (window-live-p notmuch-tree-message-window)
429                (eq (window-buffer notmuch-tree-message-window) buffer))
430       ;; We do not want an error if this is the sole window in the
431       ;; frame and I do not know how to test for that in emacs pre
432       ;; 24. Hence we just ignore-errors.
433       (ignore-errors
434         (delete-window notmuch-tree-message-window)))))
435
436 (defun notmuch-tree-command-hook ()
437   (when (eq major-mode 'notmuch-tree-mode)
438     ;; We just run the notmuch-show-command-hook on the message pane.
439     (when (buffer-live-p notmuch-tree-message-buffer)
440       (with-current-buffer notmuch-tree-message-buffer
441         (notmuch-show-command-hook)))))
442
443 (defun notmuch-tree-show-message-in ()
444   "Show the current message (in split-pane)."
445   (interactive)
446   (let ((id (notmuch-tree-get-message-id))
447         (inhibit-read-only t)
448         buffer)
449     (when id
450       ;; We close and reopen the window to kill off un-needed buffers
451       ;; this might cause flickering but seems ok.
452       (notmuch-tree-close-message-window)
453       (setq notmuch-tree-message-window
454             (split-window-vertically (/ (window-height) 4)))
455       (with-selected-window notmuch-tree-message-window
456         ;; Since we are only displaying one message do not indent.
457         (let ((notmuch-show-indent-messages-width 0)
458               (notmuch-show-only-matching-messages t))
459           (setq buffer (notmuch-show id))))
460       ;; We need the `let' as notmuch-tree-message-window is buffer local.
461       (let ((window notmuch-tree-message-window))
462         (with-current-buffer buffer
463           (setq notmuch-tree-message-window window)
464           (add-hook 'kill-buffer-hook 'notmuch-tree-message-window-kill-hook)))
465       (when notmuch-show-mark-read-tags
466         (notmuch-tree-tag-update-display notmuch-show-mark-read-tags))
467       (setq notmuch-tree-message-buffer buffer))))
468
469 (defun notmuch-tree-show-message-out ()
470   "Show the current message (in whole window)."
471   (interactive)
472   (let ((id (notmuch-tree-get-message-id))
473         (inhibit-read-only t)
474         buffer)
475     (when id
476       ;; We close the window to kill off un-needed buffers.
477       (notmuch-tree-close-message-window)
478       (notmuch-show id))))
479
480 (defun notmuch-tree-show-message (arg)
481   "Show the current message.
482
483 Shows in split pane or whole window according to value of
484 `notmuch-tree-show-out'. A prefix argument reverses the choice."
485   (interactive "P")
486   (if (or (and notmuch-tree-show-out  (not arg))
487           (and (not notmuch-tree-show-out) arg))
488       (notmuch-tree-show-message-out)
489     (notmuch-tree-show-message-in)))
490
491 (defun notmuch-tree-scroll-message-window ()
492   "Scroll the message window (if it exists)"
493   (interactive)
494   (when (window-live-p notmuch-tree-message-window)
495     (with-selected-window notmuch-tree-message-window
496       (if (pos-visible-in-window-p (point-max))
497           t
498         (scroll-up)))))
499
500 (defun notmuch-tree-scroll-message-window-back ()
501   "Scroll the message window back(if it exists)"
502   (interactive)
503   (when (window-live-p notmuch-tree-message-window)
504     (with-selected-window notmuch-tree-message-window
505       (if (pos-visible-in-window-p (point-min))
506           t
507         (scroll-down)))))
508
509 (defun notmuch-tree-scroll-or-next ()
510   "Scroll the message window. If it at end go to next message."
511   (interactive)
512   (when (notmuch-tree-scroll-message-window)
513     (notmuch-tree-next-matching-message)))
514
515 (defun notmuch-tree-quit ()
516   "Close the split view or exit tree."
517   (interactive)
518   (unless (notmuch-tree-close-message-window)
519     (kill-buffer (current-buffer))))
520
521 (defun notmuch-tree-close-message-window ()
522   "Close the message-window. Return t if close succeeds."
523   (interactive)
524   (when (and (window-live-p notmuch-tree-message-window)
525              (eq (window-buffer notmuch-tree-message-window) notmuch-tree-message-buffer))
526     (delete-window notmuch-tree-message-window)
527     (unless (get-buffer-window-list notmuch-tree-message-buffer)
528       (kill-buffer notmuch-tree-message-buffer))
529     t))
530
531 (defun notmuch-tree-archive-message (&optional unarchive)
532   "Archive the current message.
533
534 Archive the current message by applying the tag changes in
535 `notmuch-archive-tags' to it. If a prefix argument is given, the
536 message will be \"unarchived\", i.e. the tag changes in
537 `notmuch-archive-tags' will be reversed."
538   (interactive "P")
539   (when notmuch-archive-tags
540     (notmuch-tree-tag (notmuch-tag-change-list notmuch-archive-tags unarchive))))
541
542 (defun notmuch-tree-archive-message-then-next (&optional unarchive)
543   "Archive the current message and move to next matching message."
544   (interactive "P")
545   (notmuch-tree-archive-message unarchive)
546   (notmuch-tree-next-matching-message))
547
548 (defun notmuch-tree-next-message ()
549   "Move to next message."
550   (interactive)
551   (forward-line)
552   (when (window-live-p notmuch-tree-message-window)
553     (notmuch-tree-show-message-in)))
554
555 (defun notmuch-tree-prev-message ()
556   "Move to previous message."
557   (interactive)
558   (forward-line -1)
559   (when (window-live-p notmuch-tree-message-window)
560     (notmuch-tree-show-message-in)))
561
562 (defun notmuch-tree-prev-matching-message ()
563   "Move to previous matching message."
564   (interactive)
565   (forward-line -1)
566   (while (and (not (bobp)) (not (notmuch-tree-get-match)))
567     (forward-line -1))
568   (when (window-live-p notmuch-tree-message-window)
569     (notmuch-tree-show-message-in)))
570
571 (defun notmuch-tree-next-matching-message ()
572   "Move to next matching message."
573   (interactive)
574   (forward-line)
575   (while (and (not (eobp)) (not (notmuch-tree-get-match)))
576     (forward-line))
577   (when (window-live-p notmuch-tree-message-window)
578     (notmuch-tree-show-message-in)))
579
580 (defun notmuch-tree-refresh-view ()
581   "Refresh view."
582   (interactive)
583   (let ((inhibit-read-only t)
584         (basic-query notmuch-tree-basic-query)
585         (query-context notmuch-tree-query-context)
586         (target (notmuch-tree-get-message-id)))
587     (erase-buffer)
588     (notmuch-tree-worker basic-query
589                          query-context
590                          target)))
591
592 (defun notmuch-tree-thread-top ()
593   (when (notmuch-tree-get-message-properties)
594     (while (not (or (notmuch-tree-get-prop :first) (eobp)))
595       (forward-line -1))))
596
597 (defun notmuch-tree-prev-thread ()
598   (interactive)
599   (forward-line -1)
600   (notmuch-tree-thread-top))
601
602 (defun notmuch-tree-next-thread ()
603   (interactive)
604   (forward-line 1)
605   (while (not (or (notmuch-tree-get-prop :first) (eobp)))
606     (forward-line 1)))
607
608 (defun notmuch-tree-thread-mapcar (function)
609   "Iterate through all messages in the current thread
610  and call FUNCTION for side effects."
611   (save-excursion
612     (notmuch-tree-thread-top)
613     (loop collect (funcall function)
614           do (forward-line)
615           while (and (notmuch-tree-get-message-properties)
616                      (not (notmuch-tree-get-prop :first))))))
617
618 (defun notmuch-tree-get-messages-ids-thread-search ()
619   "Return a search string for all message ids of messages in the current thread."
620   (mapconcat 'identity
621              (notmuch-tree-thread-mapcar 'notmuch-tree-get-message-id)
622              " or "))
623
624 (defun notmuch-tree-tag-thread (tag-changes)
625   "Tag all messages in the current thread"
626   (interactive
627    (let ((tags (apply #'append (notmuch-tree-thread-mapcar
628                                 (lambda () (notmuch-tree-get-tags))))))
629      (list (notmuch-read-tag-changes tags "Tag thread"))))
630   (when (notmuch-tree-get-message-properties)
631     (notmuch-tag (notmuch-tree-get-messages-ids-thread-search) tag-changes)
632     (notmuch-tree-thread-mapcar
633      (lambda () (notmuch-tree-tag-update-display tag-changes)))))
634
635 (defun notmuch-tree-archive-thread (&optional unarchive)
636   "Archive each message in thread.
637
638 Archive each message currently shown by applying the tag changes
639 in `notmuch-archive-tags' to each. If a prefix argument is given,
640 the messages will be \"unarchived\", i.e. the tag changes in
641 `notmuch-archive-tags' will be reversed.
642
643 Note: This command is safe from any race condition of new messages
644 being delivered to the same thread. It does not archive the
645 entire thread, but only the messages shown in the current
646 buffer."
647   (interactive "P")
648   (when notmuch-archive-tags
649     (notmuch-tree-tag-thread
650      (notmuch-tag-change-list notmuch-archive-tags unarchive))))
651
652 ;; Functions below here display the tree buffer itself.
653
654 (defun notmuch-tree-clean-address (address)
655   "Try to clean a single email ADDRESS for display. Return
656 AUTHOR_NAME if present, otherwise return AUTHOR_EMAIL. Return
657 unchanged ADDRESS if parsing fails."
658   (let* ((clean-address (notmuch-clean-address address))
659          (p-address (car clean-address))
660          (p-name (cdr clean-address)))
661
662     ;; If we have a name return that otherwise return the address.
663     (or p-name p-address)))
664
665 (defun notmuch-tree-format-field (field format-string msg)
666   "Format a FIELD of MSG according to FORMAT-STRING and return string"
667   (let* ((headers (plist-get msg :headers))
668          (match (plist-get msg :match)))
669     (cond
670      ((listp field)
671       (format format-string (notmuch-tree-format-field-list field msg)))
672
673      ((string-equal field "date")
674       (let ((face (if match
675                       'notmuch-tree-match-date-face
676                     'notmuch-tree-no-match-date-face)))
677         (propertize (format format-string (plist-get msg :date_relative)) 'face face)))
678
679      ((string-equal field "tree")
680       (let ((tree-status (plist-get msg :tree-status))
681             (face (if match
682                       'notmuch-tree-match-tree-face
683                     'notmuch-tree-no-match-tree-face)))
684
685         (propertize (format format-string
686                             (mapconcat #'identity (reverse tree-status) ""))
687                     'face face)))
688
689      ((string-equal field "subject")
690       (let ((bare-subject (notmuch-show-strip-re (plist-get headers :Subject)))
691             (previous-subject notmuch-tree-previous-subject)
692             (face (if match
693                       'notmuch-tree-match-subject-face
694                     'notmuch-tree-no-match-subject-face)))
695
696         (setq notmuch-tree-previous-subject bare-subject)
697         (propertize (format format-string
698                             (if (string= previous-subject bare-subject)
699                                 " ..."
700                               bare-subject))
701                     'face face)))
702
703      ((string-equal field "authors")
704       (let ((author (notmuch-tree-clean-address (plist-get headers :From)))
705             (len (length (format format-string "")))
706             (face (if match
707                       'notmuch-tree-match-author-face
708                     'notmuch-tree-no-match-author-face)))
709         (when (> (length author) len)
710           (setq author (substring author 0 len)))
711         (propertize (format format-string author) 'face face)))
712
713      ((string-equal field "tags")
714       (let ((tags (plist-get msg :tags))
715             (orig-tags (plist-get msg :orig-tags))
716             (face (if match
717                       'notmuch-tree-match-tag-face
718                     'notmuch-tree-no-match-tag-face)))
719         (format format-string (notmuch-tag-format-tags tags orig-tags face)))))))
720
721 (defun notmuch-tree-format-field-list (field-list msg)
722   "Format fields of MSG according to FIELD-LIST and return string"
723   (let ((face (if (plist-get msg :match)
724                   'notmuch-tree-match-face
725                 'notmuch-tree-no-match-face))
726         (result-string))
727     (dolist (spec field-list result-string)
728       (let ((field-string (notmuch-tree-format-field (car spec) (cdr spec) msg)))
729         (setq result-string (concat result-string field-string))))
730     (notmuch-apply-face result-string face t)))
731
732 (defun notmuch-tree-insert-msg (msg)
733   "Insert the message MSG according to notmuch-tree-result-format"
734   ;; We need to save the previous subject as it will get overwritten
735   ;; by the insert-field calls.
736   (let ((previous-subject notmuch-tree-previous-subject))
737     (insert (notmuch-tree-format-field-list notmuch-tree-result-format msg))
738     (notmuch-tree-set-message-properties msg)
739     (notmuch-tree-set-prop :previous-subject previous-subject)
740     (insert "\n")))
741
742 (defun notmuch-tree-goto-and-insert-msg (msg)
743   "Insert msg at the end of the buffer. Move point to msg if it is the target"
744   (save-excursion
745     (goto-char (point-max))
746     (notmuch-tree-insert-msg msg))
747   (let ((msg-id (notmuch-id-to-query (plist-get msg :id)))
748         (target notmuch-tree-target-msg))
749     (when (or (and (not target) (plist-get msg :match))
750               (string= msg-id target))
751       (setq notmuch-tree-target-msg "found")
752       (goto-char (point-max))
753       (forward-line -1)
754       (when notmuch-tree-open-target
755         (notmuch-tree-show-message-in)))))
756
757 (defun notmuch-tree-insert-tree (tree depth tree-status first last)
758   "Insert the message tree TREE at depth DEPTH in the current thread.
759
760 A message tree is another name for a single sub-thread: i.e., a
761 message together with all its descendents."
762   (let ((msg (car tree))
763         (replies (cadr tree)))
764
765       (cond
766        ((and (< 0 depth) (not last))
767         (push "├" tree-status))
768        ((and (< 0 depth) last)
769         (push "╰" tree-status))
770        ((and (eq 0 depth) first last)
771 ;;        (push "─" tree-status)) choice between this and next line is matter of taste.
772         (push " " tree-status))
773        ((and (eq 0 depth) first (not last))
774           (push "┬" tree-status))
775        ((and (eq 0 depth) (not first) last)
776         (push "╰" tree-status))
777        ((and (eq 0 depth) (not first) (not last))
778         (push "├" tree-status)))
779
780       (push (concat (if replies "┬" "─") "►") tree-status)
781       (setq msg (plist-put msg :first (and first (eq 0 depth))))
782       (setq msg (plist-put msg :tree-status tree-status))
783       (setq msg (plist-put msg :orig-tags (plist-get msg :tags)))
784       (notmuch-tree-goto-and-insert-msg msg)
785       (pop tree-status)
786       (pop tree-status)
787
788       (if last
789           (push " " tree-status)
790         (push "│" tree-status))
791
792     (notmuch-tree-insert-thread replies (1+ depth) tree-status)))
793
794 (defun notmuch-tree-insert-thread (thread depth tree-status)
795   "Insert the collection of sibling sub-threads THREAD at depth DEPTH in the current forest."
796   (let ((n (length thread)))
797     (loop for tree in thread
798           for count from 1 to n
799
800           do (notmuch-tree-insert-tree tree depth tree-status (eq count 1) (eq count n)))))
801
802 (defun notmuch-tree-insert-forest-thread (forest-thread)
803   "Insert a single complete thread."
804   (let (tree-status)
805     ;; Reset at the start of each main thread.
806     (setq notmuch-tree-previous-subject nil)
807     (notmuch-tree-insert-thread forest-thread 0 tree-status)))
808
809 (defun notmuch-tree-insert-forest (forest)
810   "Insert a forest of threads.
811
812 This function inserts a collection of several complete threads as
813 passed to it by notmuch-tree-process-filter."
814   (mapc 'notmuch-tree-insert-forest-thread forest))
815
816 (define-derived-mode notmuch-tree-mode fundamental-mode "notmuch-tree"
817   "Major mode displaying messages (as opposed to threads) of of a notmuch search.
818
819 This buffer contains the results of a \"notmuch tree\" of your
820 email archives. Each line in the buffer represents a single
821 message giving the relative date, the author, subject, and any
822 tags.
823
824 Pressing \\[notmuch-tree-show-message] on any line displays that message.
825
826 Complete list of currently available key bindings:
827
828 \\{notmuch-tree-mode-map}"
829
830   (setq notmuch-buffer-refresh-function #'notmuch-tree-refresh-view)
831   (hl-line-mode 1)
832   (setq buffer-read-only t
833         truncate-lines t))
834
835 (defun notmuch-tree-process-sentinel (proc msg)
836   "Add a message to let user know when \"notmuch tree\" exits"
837   (let ((buffer (process-buffer proc))
838         (status (process-status proc))
839         (exit-status (process-exit-status proc))
840         (never-found-target-thread nil))
841     (when (memq status '(exit signal))
842         (kill-buffer (process-get proc 'parse-buf))
843         (if (buffer-live-p buffer)
844             (with-current-buffer buffer
845               (save-excursion
846                 (let ((inhibit-read-only t)
847                       (atbob (bobp)))
848                   (goto-char (point-max))
849                   (if (eq status 'signal)
850                       (insert "Incomplete search results (tree view process was killed).\n"))
851                   (when (eq status 'exit)
852                     (insert "End of search results.")
853                     (unless (= exit-status 0)
854                       (insert (format " (process returned %d)" exit-status)))
855                     (insert "\n")))))))))
856
857 (defun notmuch-tree-process-filter (proc string)
858   "Process and filter the output of \"notmuch show\" for tree view"
859   (let ((results-buf (process-buffer proc))
860         (parse-buf (process-get proc 'parse-buf))
861         (inhibit-read-only t)
862         done)
863     (if (not (buffer-live-p results-buf))
864         (delete-process proc)
865       (with-current-buffer parse-buf
866         ;; Insert new data
867         (save-excursion
868           (goto-char (point-max))
869           (insert string))
870         (notmuch-sexp-parse-partial-list 'notmuch-tree-insert-forest-thread
871                                          results-buf)))))
872
873 (defun notmuch-tree-worker (basic-query &optional query-context target open-target)
874   "Insert the tree view of the search in the current buffer.
875
876 This is is a helper function for notmuch-tree. The arguments are
877 the same as for the function notmuch-tree."
878   (interactive)
879   (notmuch-tree-mode)
880   (add-hook 'post-command-hook #'notmuch-tree-command-hook t t)
881   (setq notmuch-tree-basic-query basic-query)
882   (setq notmuch-tree-query-context query-context)
883   (setq notmuch-tree-target-msg target)
884   (setq notmuch-tree-open-target open-target)
885   ;; Set the default value for `notmuch-show-process-crypto' in this
886   ;; buffer. Although we don't use this some of the functions we call
887   ;; (such as reply) do. It is a buffer local variable so setting it
888   ;; will not affect genuine show buffers.
889   (setq notmuch-show-process-crypto notmuch-crypto-process-mime)
890
891   (erase-buffer)
892   (goto-char (point-min))
893   (let* ((search-args (concat basic-query
894                        (if query-context (concat " and (" query-context ")"))
895                        ))
896          (message-arg "--entire-thread"))
897     (if (equal (car (process-lines notmuch-command "count" search-args)) "0")
898         (setq search-args basic-query))
899     (notmuch-tag-clear-cache)
900     (let ((proc (notmuch-start-notmuch
901                  "notmuch-tree" (current-buffer) #'notmuch-tree-process-sentinel
902                  "show" "--body=false" "--format=sexp"
903                  message-arg search-args))
904           ;; Use a scratch buffer to accumulate partial output.
905           ;; This buffer will be killed by the sentinel, which
906           ;; should be called no matter how the process dies.
907           (parse-buf (generate-new-buffer " *notmuch tree parse*")))
908       (process-put proc 'parse-buf parse-buf)
909       (set-process-filter proc 'notmuch-tree-process-filter)
910       (set-process-query-on-exit-flag proc nil))))
911
912 (defun notmuch-tree-get-query ()
913   "Return the current query in this tree buffer"
914   (if notmuch-tree-query-context
915       (concat notmuch-tree-basic-query
916               " and ("
917               notmuch-tree-query-context
918               ")")
919     notmuch-tree-basic-query))
920
921 (defun notmuch-tree (&optional query query-context target buffer-name open-target)
922   "Display threads matching QUERY in Tree View.
923
924 The arguments are:
925   QUERY: the main query. This can be any query but in many cases will be
926       a single thread. If nil this is read interactively from the minibuffer.
927   QUERY-CONTEXT: is an additional term for the query. The query used
928       is QUERY and QUERY-CONTEXT unless that does not match any messages
929       in which case we fall back to just QUERY.
930   TARGET: A message ID (with the id: prefix) that will be made
931       current if it appears in the tree view results.
932   BUFFER-NAME: the name of the buffer to display the tree view. If
933       it is nil \"*notmuch-tree\" followed by QUERY is used.
934   OPEN-TARGET: If TRUE open the target message in the message pane."
935   (interactive)
936   (if (null query)
937       (setq query (notmuch-read-query "Notmuch tree view search: ")))
938   (let ((buffer (get-buffer-create (generate-new-buffer-name
939                                     (or buffer-name
940                                         (concat "*notmuch-tree-" query "*")))))
941         (inhibit-read-only t))
942
943     (switch-to-buffer buffer))
944   ;; Don't track undo information for this buffer
945   (set 'buffer-undo-list t)
946
947   (notmuch-tree-worker query query-context target open-target)
948
949   (setq truncate-lines t))
950
951
952 ;;
953
954 (provide 'notmuch-tree)
955
956 ;;; notmuch-tree.el ends here