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