]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-tree.el
emacs: tree: remove binding for pressing button in message pane
[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-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
216         (symbol-value sym))
217     (symbol-value sym)))
218
219 (defun notmuch-tree-button-activate (&optional button)
220   "Activate BUTTON or button at point
221
222 This function does not give an error if there is no button."
223   (interactive)
224   (let ((button (or button (button-at (point)))))
225     (when button (button-activate button))))
226
227 (defun notmuch-tree-close-message-pane-and (func)
228   "Close message pane and execute FUNC.
229
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
232 FUNC."
233   `(lambda ()
234       ,(concat "(Close message pane and) " (documentation func t))
235      (interactive)
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))))
240
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))
255
256     (define-key map "S" 'notmuch-search-from-tree-current-query)
257
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)
264
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))
270
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))
276
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     map))
297 (fset 'notmuch-tree-mode-map notmuch-tree-mode-map)
298
299 (defun notmuch-tree-get-message-properties ()
300   "Return the properties of the current message as a plist.
301
302 Some useful entries are:
303 :headers - Property list containing the headers :Date, :Subject, :From, etc.
304 :tags - Tags for this message"
305   (save-excursion
306     (beginning-of-line)
307     (get-text-property (point) :notmuch-message-properties)))
308
309 (defun notmuch-tree-set-message-properties (props)
310   (save-excursion
311     (beginning-of-line)
312     (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
313
314 (defun notmuch-tree-set-prop (prop val &optional props)
315   (let ((inhibit-read-only t)
316         (props (or props
317                    (notmuch-tree-get-message-properties))))
318     (plist-put props prop val)
319     (notmuch-tree-set-message-properties props)))
320
321 (defun notmuch-tree-get-prop (prop &optional props)
322   (let ((props (or props
323                    (notmuch-tree-get-message-properties))))
324     (plist-get props prop)))
325
326 (defun notmuch-tree-set-tags (tags)
327   "Set the tags of the current message."
328   (notmuch-tree-set-prop :tags tags))
329
330 (defun notmuch-tree-get-tags ()
331   "Return the tags of the current message."
332   (notmuch-tree-get-prop :tags))
333
334 (defun notmuch-tree-get-message-id (&optional bare)
335   "Return the message id of the current message."
336   (let ((id (notmuch-tree-get-prop :id)))
337     (if id
338         (if bare
339             id
340           (notmuch-id-to-query id))
341       nil)))
342
343 (defun notmuch-tree-get-match ()
344   "Return whether the current message is a match."
345   (interactive)
346   (notmuch-tree-get-prop :match))
347
348 (defun notmuch-tree-refresh-result ()
349   "Redisplay the current message line.
350
351 This redisplays the current line based on the messages
352 properties (as they are now). This is used when tags are
353 updated."
354   (let ((init-point (point))
355         (end (line-end-position))
356         (msg (notmuch-tree-get-message-properties))
357         (inhibit-read-only t))
358     (beginning-of-line)
359     ;; This is a little tricky: we override
360     ;; notmuch-tree-previous-subject to get the decision between
361     ;; ... and a subject right and it stops notmuch-tree-insert-msg
362     ;; from overwriting the buffer local copy of
363     ;; notmuch-tree-previous-subject if this is called while the
364     ;; buffer is displaying.
365     (let ((notmuch-tree-previous-subject (notmuch-tree-get-prop :previous-subject)))
366       (delete-region (point) (1+ (line-end-position)))
367       (notmuch-tree-insert-msg msg))
368     (let ((new-end (line-end-position)))
369       (goto-char (if (= init-point end)
370                      new-end
371                    (min init-point (- new-end 1)))))))
372
373 (defun notmuch-tree-tag-update-display (&optional tag-changes)
374   "Update display for TAG-CHANGES to current message.
375
376 Updates the message in the message pane if appropriate, but does
377 NOT change the database."
378   (let* ((current-tags (notmuch-tree-get-tags))
379          (new-tags (notmuch-update-tags current-tags tag-changes))
380          (tree-msg-id (notmuch-tree-get-message-id)))
381     (unless (equal current-tags new-tags)
382       (notmuch-tree-set-tags new-tags)
383       (notmuch-tree-refresh-result)
384       (when (window-live-p notmuch-tree-message-window)
385         (with-selected-window notmuch-tree-message-window
386           (when (string= tree-msg-id (notmuch-show-get-message-id))
387             (notmuch-show-update-tags new-tags)))))))
388
389 (defun notmuch-tree-tag (tag-changes)
390   "Change tags for the current message"
391   (interactive
392    (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message")))
393   (notmuch-tag (notmuch-tree-get-message-id) tag-changes)
394   (notmuch-tree-tag-update-display tag-changes))
395
396 (defun notmuch-tree-add-tag (tag-changes)
397   "Same as `notmuch-tree-tag' but sets initial input to '+'."
398   (interactive
399    (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "+")))
400   (notmuch-tree-tag tag-changes))
401
402 (defun notmuch-tree-remove-tag (tag-changes)
403   "Same as `notmuch-tree-tag' but sets initial input to '-'."
404   (interactive
405    (list (notmuch-read-tag-changes (notmuch-tree-get-tags) "Tag message" "-")))
406   (notmuch-tree-tag tag-changes))
407
408 ;; The next two functions close the message window before calling
409 ;; notmuch-search or notmuch-tree but they do so after the user has
410 ;; entered the query (in case the user was basing the query on
411 ;; something in the message window).
412
413 (defun notmuch-tree-to-search ()
414   "Run \"notmuch search\" with the given `query' and display results."
415   (interactive)
416   (let ((query (notmuch-read-query "Notmuch search: ")))
417     (notmuch-tree-close-message-window)
418     (notmuch-search query)))
419
420 (defun notmuch-tree-to-tree ()
421   "Run a query and display results in Tree view"
422   (interactive)
423   (let ((query (notmuch-read-query "Notmuch tree view search: ")))
424     (notmuch-tree-close-message-window)
425     (notmuch-tree query)))
426
427 (defun notmuch-search-from-tree-current-query ()
428   "Call notmuch search with the current query"
429   (interactive)
430   (notmuch-tree-close-message-window)
431   (notmuch-search (notmuch-tree-get-query)))
432
433 (defun notmuch-tree-message-window-kill-hook ()
434   "Close the message pane when exiting the show buffer."
435   (let ((buffer (current-buffer)))
436     (when (and (window-live-p notmuch-tree-message-window)
437                (eq (window-buffer notmuch-tree-message-window) buffer))
438       ;; We do not want an error if this is the sole window in the
439       ;; frame and I do not know how to test for that in emacs pre
440       ;; 24. Hence we just ignore-errors.
441       (ignore-errors
442         (delete-window notmuch-tree-message-window)))))
443
444 (defun notmuch-tree-command-hook ()
445   (when (eq major-mode 'notmuch-tree-mode)
446     ;; We just run the notmuch-show-command-hook on the message pane.
447     (when (buffer-live-p notmuch-tree-message-buffer)
448       (with-current-buffer notmuch-tree-message-buffer
449         (notmuch-show-command-hook)))))
450
451 (defun notmuch-tree-show-message-in ()
452   "Show the current message (in split-pane)."
453   (interactive)
454   (let ((id (notmuch-tree-get-message-id))
455         (inhibit-read-only t)
456         buffer)
457     (when id
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))))
476
477 (defun notmuch-tree-show-message-out ()
478   "Show the current message (in whole window)."
479   (interactive)
480   (let ((id (notmuch-tree-get-message-id))
481         (inhibit-read-only t)
482         buffer)
483     (when id
484       ;; We close the window to kill off un-needed buffers.
485       (notmuch-tree-close-message-window)
486       (notmuch-show id))))
487
488 (defun notmuch-tree-show-message (arg)
489   "Show the current message.
490
491 Shows in split pane or whole window according to value of
492 `notmuch-tree-show-out'. A prefix argument reverses the choice."
493   (interactive "P")
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)))
498
499 (defun notmuch-tree-scroll-message-window ()
500   "Scroll the message window (if it exists)"
501   (interactive)
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))
505           t
506         (scroll-up)))))
507
508 (defun notmuch-tree-scroll-message-window-back ()
509   "Scroll the message window back(if it exists)"
510   (interactive)
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))
514           t
515         (scroll-down)))))
516
517 (defun notmuch-tree-scroll-or-next ()
518   "Scroll the message window. If it at end go to next message."
519   (interactive)
520   (when (notmuch-tree-scroll-message-window)
521     (notmuch-tree-next-matching-message)))
522
523 (defun notmuch-tree-quit ()
524   "Close the split view or exit tree."
525   (interactive)
526   (unless (notmuch-tree-close-message-window)
527     (kill-buffer (current-buffer))))
528
529 (defun notmuch-tree-close-message-window ()
530   "Close the message-window. Return t if close succeeds."
531   (interactive)
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))
537     t))
538
539 (defun notmuch-tree-archive-message (&optional unarchive)
540   "Archive the current message.
541
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."
546   (interactive "P")
547   (when notmuch-archive-tags
548     (notmuch-tree-tag (notmuch-tag-change-list notmuch-archive-tags unarchive))))
549
550 (defun notmuch-tree-archive-message-then-next (&optional unarchive)
551   "Archive the current message and move to next matching message."
552   (interactive "P")
553   (notmuch-tree-archive-message unarchive)
554   (notmuch-tree-next-matching-message))
555
556 (defun notmuch-tree-next-message ()
557   "Move to next message."
558   (interactive)
559   (forward-line)
560   (when (window-live-p notmuch-tree-message-window)
561     (notmuch-tree-show-message-in)))
562
563 (defun notmuch-tree-prev-message ()
564   "Move to previous message."
565   (interactive)
566   (forward-line -1)
567   (when (window-live-p notmuch-tree-message-window)
568     (notmuch-tree-show-message-in)))
569
570 (defun notmuch-tree-prev-matching-message ()
571   "Move to previous matching message."
572   (interactive)
573   (forward-line -1)
574   (while (and (not (bobp)) (not (notmuch-tree-get-match)))
575     (forward-line -1))
576   (when (window-live-p notmuch-tree-message-window)
577     (notmuch-tree-show-message-in)))
578
579 (defun notmuch-tree-next-matching-message ()
580   "Move to next matching message."
581   (interactive)
582   (forward-line)
583   (while (and (not (eobp)) (not (notmuch-tree-get-match)))
584     (forward-line))
585   (when (window-live-p notmuch-tree-message-window)
586     (notmuch-tree-show-message-in)))
587
588 (defun notmuch-tree-refresh-view ()
589   "Refresh view."
590   (interactive)
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)))
595     (erase-buffer)
596     (notmuch-tree-worker basic-query
597                          query-context
598                          target)))
599
600 (defun notmuch-tree-thread-top ()
601   (when (notmuch-tree-get-message-properties)
602     (while (not (or (notmuch-tree-get-prop :first) (eobp)))
603       (forward-line -1))))
604
605 (defun notmuch-tree-prev-thread ()
606   (interactive)
607   (forward-line -1)
608   (notmuch-tree-thread-top))
609
610 (defun notmuch-tree-next-thread ()
611   (interactive)
612   (forward-line 1)
613   (while (not (or (notmuch-tree-get-prop :first) (eobp)))
614     (forward-line 1)))
615
616 (defun notmuch-tree-thread-mapcar (function)
617   "Iterate through all messages in the current thread
618  and call FUNCTION for side effects."
619   (save-excursion
620     (notmuch-tree-thread-top)
621     (loop collect (funcall function)
622           do (forward-line)
623           while (and (notmuch-tree-get-message-properties)
624                      (not (notmuch-tree-get-prop :first))))))
625
626 (defun notmuch-tree-get-messages-ids-thread-search ()
627   "Return a search string for all message ids of messages in the current thread."
628   (mapconcat 'identity
629              (notmuch-tree-thread-mapcar 'notmuch-tree-get-message-id)
630              " or "))
631
632 (defun notmuch-tree-tag-thread (tag-changes)
633   "Tag all messages in the current thread"
634   (interactive
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)))))
642
643 (defun notmuch-tree-archive-thread (&optional unarchive)
644   "Archive each message in thread.
645
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.
650
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
654 buffer."
655   (interactive "P")
656   (when notmuch-archive-tags
657     (notmuch-tree-tag-thread
658      (notmuch-tag-change-list notmuch-archive-tags unarchive))))
659
660 ;; Functions below here display the tree buffer itself.
661
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)))
669
670     ;; If we have a name return that otherwise return the address.
671     (or p-name p-address)))
672
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)))
677     (cond
678      ((listp field)
679       (format format-string (notmuch-tree-format-field-list field msg)))
680
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)))
686
687      ((string-equal field "tree")
688       (let ((tree-status (plist-get msg :tree-status))
689             (face (if match
690                       'notmuch-tree-match-tree-face
691                     'notmuch-tree-no-match-tree-face)))
692
693         (propertize (format format-string
694                             (mapconcat #'identity (reverse tree-status) ""))
695                     'face face)))
696
697      ((string-equal field "subject")
698       (let ((bare-subject (notmuch-show-strip-re (plist-get headers :Subject)))
699             (previous-subject notmuch-tree-previous-subject)
700             (face (if match
701                       'notmuch-tree-match-subject-face
702                     'notmuch-tree-no-match-subject-face)))
703
704         (setq notmuch-tree-previous-subject bare-subject)
705         (propertize (format format-string
706                             (if (string= previous-subject bare-subject)
707                                 " ..."
708                               bare-subject))
709                     'face face)))
710
711      ((string-equal field "authors")
712       (let ((author (notmuch-tree-clean-address (plist-get headers :From)))
713             (len (length (format format-string "")))
714             (face (if match
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)))
720
721      ((string-equal field "tags")
722       (let ((tags (plist-get msg :tags))
723             (orig-tags (plist-get msg :orig-tags))
724             (face (if match
725                       'notmuch-tree-match-tag-face
726                     'notmuch-tree-no-match-tag-face)))
727         (format format-string (notmuch-tag-format-tags tags orig-tags face)))))))
728
729 (defun notmuch-tree-format-field-list (field-list msg)
730   "Format fields of MSG according to FIELD-LIST and return string"
731   (let ((face (if (plist-get msg :match)
732                   'notmuch-tree-match-face
733                 'notmuch-tree-no-match-face))
734         (result-string))
735     (dolist (spec field-list result-string)
736       (let ((field-string (notmuch-tree-format-field (car spec) (cdr spec) msg)))
737         (setq result-string (concat result-string field-string))))
738     (notmuch-apply-face result-string face t)))
739
740 (defun notmuch-tree-insert-msg (msg)
741   "Insert the message MSG according to notmuch-tree-result-format"
742   ;; We need to save the previous subject as it will get overwritten
743   ;; by the insert-field calls.
744   (let ((previous-subject notmuch-tree-previous-subject))
745     (insert (notmuch-tree-format-field-list notmuch-tree-result-format msg))
746     (notmuch-tree-set-message-properties msg)
747     (notmuch-tree-set-prop :previous-subject previous-subject)
748     (insert "\n")))
749
750 (defun notmuch-tree-goto-and-insert-msg (msg)
751   "Insert msg at the end of the buffer. Move point to msg if it is the target"
752   (save-excursion
753     (goto-char (point-max))
754     (notmuch-tree-insert-msg msg))
755   (let ((msg-id (notmuch-id-to-query (plist-get msg :id)))
756         (target notmuch-tree-target-msg))
757     (when (or (and (not target) (plist-get msg :match))
758               (string= msg-id target))
759       (setq notmuch-tree-target-msg "found")
760       (goto-char (point-max))
761       (forward-line -1)
762       (when notmuch-tree-open-target
763         (notmuch-tree-show-message-in)))))
764
765 (defun notmuch-tree-insert-tree (tree depth tree-status first last)
766   "Insert the message tree TREE at depth DEPTH in the current thread.
767
768 A message tree is another name for a single sub-thread: i.e., a
769 message together with all its descendents."
770   (let ((msg (car tree))
771         (replies (cadr tree)))
772
773       (cond
774        ((and (< 0 depth) (not last))
775         (push "├" tree-status))
776        ((and (< 0 depth) last)
777         (push "╰" tree-status))
778        ((and (eq 0 depth) first last)
779 ;;        (push "─" tree-status)) choice between this and next line is matter of taste.
780         (push " " tree-status))
781        ((and (eq 0 depth) first (not last))
782           (push "┬" tree-status))
783        ((and (eq 0 depth) (not first) last)
784         (push "╰" tree-status))
785        ((and (eq 0 depth) (not first) (not last))
786         (push "├" tree-status)))
787
788       (push (concat (if replies "┬" "─") "►") tree-status)
789       (setq msg (plist-put msg :first (and first (eq 0 depth))))
790       (setq msg (plist-put msg :tree-status tree-status))
791       (setq msg (plist-put msg :orig-tags (plist-get msg :tags)))
792       (notmuch-tree-goto-and-insert-msg msg)
793       (pop tree-status)
794       (pop tree-status)
795
796       (if last
797           (push " " tree-status)
798         (push "│" tree-status))
799
800     (notmuch-tree-insert-thread replies (1+ depth) tree-status)))
801
802 (defun notmuch-tree-insert-thread (thread depth tree-status)
803   "Insert the collection of sibling sub-threads THREAD at depth DEPTH in the current forest."
804   (let ((n (length thread)))
805     (loop for tree in thread
806           for count from 1 to n
807
808           do (notmuch-tree-insert-tree tree depth tree-status (eq count 1) (eq count n)))))
809
810 (defun notmuch-tree-insert-forest-thread (forest-thread)
811   "Insert a single complete thread."
812   (let (tree-status)
813     ;; Reset at the start of each main thread.
814     (setq notmuch-tree-previous-subject nil)
815     (notmuch-tree-insert-thread forest-thread 0 tree-status)))
816
817 (defun notmuch-tree-insert-forest (forest)
818   "Insert a forest of threads.
819
820 This function inserts a collection of several complete threads as
821 passed to it by notmuch-tree-process-filter."
822   (mapc 'notmuch-tree-insert-forest-thread forest))
823
824 (define-derived-mode notmuch-tree-mode fundamental-mode "notmuch-tree"
825   "Major mode displaying messages (as opposed to threads) of of a notmuch search.
826
827 This buffer contains the results of a \"notmuch tree\" of your
828 email archives. Each line in the buffer represents a single
829 message giving the relative date, the author, subject, and any
830 tags.
831
832 Pressing \\[notmuch-tree-show-message] on any line displays that message.
833
834 Complete list of currently available key bindings:
835
836 \\{notmuch-tree-mode-map}"
837
838   (setq notmuch-buffer-refresh-function #'notmuch-tree-refresh-view)
839   (hl-line-mode 1)
840   (setq buffer-read-only t
841         truncate-lines t))
842
843 (defun notmuch-tree-process-sentinel (proc msg)
844   "Add a message to let user know when \"notmuch tree\" exits"
845   (let ((buffer (process-buffer proc))
846         (status (process-status proc))
847         (exit-status (process-exit-status proc))
848         (never-found-target-thread nil))
849     (when (memq status '(exit signal))
850         (kill-buffer (process-get proc 'parse-buf))
851         (if (buffer-live-p buffer)
852             (with-current-buffer buffer
853               (save-excursion
854                 (let ((inhibit-read-only t)
855                       (atbob (bobp)))
856                   (goto-char (point-max))
857                   (if (eq status 'signal)
858                       (insert "Incomplete search results (tree view process was killed).\n"))
859                   (when (eq status 'exit)
860                     (insert "End of search results.")
861                     (unless (= exit-status 0)
862                       (insert (format " (process returned %d)" exit-status)))
863                     (insert "\n")))))))))
864
865 (defun notmuch-tree-process-filter (proc string)
866   "Process and filter the output of \"notmuch show\" for tree view"
867   (let ((results-buf (process-buffer proc))
868         (parse-buf (process-get proc 'parse-buf))
869         (inhibit-read-only t)
870         done)
871     (if (not (buffer-live-p results-buf))
872         (delete-process proc)
873       (with-current-buffer parse-buf
874         ;; Insert new data
875         (save-excursion
876           (goto-char (point-max))
877           (insert string))
878         (notmuch-sexp-parse-partial-list 'notmuch-tree-insert-forest-thread
879                                          results-buf)))))
880
881 (defun notmuch-tree-worker (basic-query &optional query-context target open-target)
882   "Insert the tree view of the search in the current buffer.
883
884 This is is a helper function for notmuch-tree. The arguments are
885 the same as for the function notmuch-tree."
886   (interactive)
887   (notmuch-tree-mode)
888   (add-hook 'post-command-hook #'notmuch-tree-command-hook t t)
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)
893   ;; Set the default value for `notmuch-show-process-crypto' in this
894   ;; buffer. Although we don't use this some of the functions we call
895   ;; (such as reply) do. It is a buffer local variable so setting it
896   ;; will not affect genuine show buffers.
897   (setq notmuch-show-process-crypto notmuch-crypto-process-mime)
898
899   (erase-buffer)
900   (goto-char (point-min))
901   (let* ((search-args (concat basic-query
902                        (if query-context (concat " and (" query-context ")"))
903                        ))
904          (message-arg "--entire-thread"))
905     (if (equal (car (process-lines notmuch-command "count" search-args)) "0")
906         (setq search-args basic-query))
907     (notmuch-tag-clear-cache)
908     (let ((proc (notmuch-start-notmuch
909                  "notmuch-tree" (current-buffer) #'notmuch-tree-process-sentinel
910                  "show" "--body=false" "--format=sexp"
911                  message-arg search-args))
912           ;; Use a scratch buffer to accumulate partial output.
913           ;; This buffer will be killed by the sentinel, which
914           ;; should be called no matter how the process dies.
915           (parse-buf (generate-new-buffer " *notmuch tree parse*")))
916       (process-put proc 'parse-buf parse-buf)
917       (set-process-filter proc 'notmuch-tree-process-filter)
918       (set-process-query-on-exit-flag proc nil))))
919
920 (defun notmuch-tree-get-query ()
921   "Return the current query in this tree buffer"
922   (if notmuch-tree-query-context
923       (concat notmuch-tree-basic-query
924               " and ("
925               notmuch-tree-query-context
926               ")")
927     notmuch-tree-basic-query))
928
929 (defun notmuch-tree (&optional query query-context target buffer-name open-target)
930   "Display threads matching QUERY in Tree View.
931
932 The arguments are:
933   QUERY: the main query. This can be any query but in many cases will be
934       a single thread. If nil this is read interactively from the minibuffer.
935   QUERY-CONTEXT: is an additional term for the query. The query used
936       is QUERY and QUERY-CONTEXT unless that does not match any messages
937       in which case we fall back to just QUERY.
938   TARGET: A message ID (with the id: prefix) that will be made
939       current if it appears in the tree view results.
940   BUFFER-NAME: the name of the buffer to display the tree view. If
941       it is nil \"*notmuch-tree\" followed by QUERY is used.
942   OPEN-TARGET: If TRUE open the target message in the message pane."
943   (interactive)
944   (if (null query)
945       (setq query (notmuch-read-query "Notmuch tree view search: ")))
946   (let ((buffer (get-buffer-create (generate-new-buffer-name
947                                     (or buffer-name
948                                         (concat "*notmuch-tree-" query "*")))))
949         (inhibit-read-only t))
950
951     (switch-to-buffer buffer))
952   ;; Don't track undo information for this buffer
953   (set 'buffer-undo-list t)
954
955   (notmuch-tree-worker query query-context target open-target)
956
957   (setq truncate-lines t))
958
959
960 ;;
961
962 (provide 'notmuch-tree)
963
964 ;;; notmuch-tree.el ends here