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