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