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