]> git.notmuchmail.org Git - notmuch/blob - contrib/notmuch-pick/notmuch-pick.el
contrib: pick: use notmuch-clean-address
[notmuch] / contrib / notmuch-pick / notmuch-pick.el
1 ;; notmuch-pick.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) ;; XXX ATM, as notmuch-search-mode-map is defined here
31
32 (eval-when-compile (require 'cl))
33
34 (declare-function notmuch-call-notmuch-process "notmuch" (&rest args))
35 (declare-function notmuch-show "notmuch-show" (&rest args))
36 (declare-function notmuch-tag "notmuch" (query &rest tags))
37 (declare-function notmuch-show-strip-re "notmuch-show" (subject))
38 (declare-function notmuch-show-spaces-n "notmuch-show" (n))
39 (declare-function notmuch-read-query "notmuch" (prompt))
40 (declare-function notmuch-read-tag-changes "notmuch" (&optional initial-input &rest search-terms))
41 (declare-function notmuch-update-tags "notmuch" (current-tags tag-changes))
42 (declare-function notmuch-hello-trim "notmuch-hello" (search))
43 (declare-function notmuch-search-find-thread-id "notmuch" ())
44 (declare-function notmuch-search-find-subject "notmuch" ())
45
46 ;; the following variable is defined in notmuch.el
47 (defvar notmuch-search-query-string)
48
49 (defgroup notmuch-pick nil
50   "Showing message and thread structure."
51   :group 'notmuch)
52
53 ;; This is ugly. We can't run setup-show-out until it has been defined
54 ;; which needs the keymap to be defined. So we defer setting up to
55 ;; notmuch-pick-init.
56 (defcustom notmuch-pick-show-out nil
57   "View selected messages in new window rather than split-pane."
58   :type 'boolean
59   :group 'notmuch-pick
60   :set (lambda (symbol value)
61          (set-default symbol value)
62          (when (fboundp 'notmuch-pick-setup-show-out)
63            (notmuch-pick-setup-show-out))))
64
65 (defcustom notmuch-pick-result-format
66   `(("date" . "%12s  ")
67     ("authors" . "%-20s")
68     ("subject" . " %-54s ")
69     ("tags" . "(%s)"))
70   "Result formatting for Pick. Supported fields are: date,
71         authors, subject, tags Note: subject includes the tree
72         structure graphics, and the author string should not
73         contain whitespace (put it in the neighbouring fields
74         instead).  For example:
75         (setq notmuch-pick-result-format \(\(\"authors\" . \"%-40s\"\)
76                                              \(\"subject\" . \"%s\"\)\)\)"
77   :type '(alist :key-type (string) :value-type (string))
78   :group 'notmuch-pick)
79
80 (defcustom notmuch-pick-asynchronous-parser t
81   "Use the asynchronous parser."
82   :type 'boolean
83   :group 'notmuch-pick)
84
85 ;; Faces for messages that match the query.
86 (defface notmuch-pick-match-date-face
87   '((t :inherit default))
88   "Face used in pick mode for the date in messages matching the query."
89   :group 'notmuch-pick
90   :group 'notmuch-faces)
91
92 (defface notmuch-pick-match-author-face
93   '((((class color)
94       (background dark))
95      (:foreground "OliveDrab1"))
96     (((class color)
97       (background light))
98      (:foreground "dark blue"))
99     (t
100      (:bold t)))
101   "Face used in pick mode for the date in messages matching the query."
102   :group 'notmuch-pick
103   :group 'notmuch-faces)
104
105 (defface notmuch-pick-match-subject-face
106   '((t :inherit default))
107   "Face used in pick mode for the subject in messages matching the query."
108   :group 'notmuch-pick
109   :group 'notmuch-faces)
110
111 (defface notmuch-pick-match-tag-face
112   '((((class color)
113       (background dark))
114      (:foreground "OliveDrab1"))
115     (((class color)
116       (background light))
117      (:foreground "navy blue" :bold t))
118     (t
119      (:bold t)))
120   "Face used in pick mode for tags in messages matching the query."
121   :group 'notmuch-pick
122   :group 'notmuch-faces)
123
124 ;; Faces for messages that do not match the query.
125 (defface notmuch-pick-no-match-date-face
126   '((t (:foreground "gray")))
127   "Face used in pick mode for non-matching dates."
128   :group 'notmuch-pick
129   :group 'notmuch-faces)
130
131 (defface notmuch-pick-no-match-subject-face
132   '((t (:foreground "gray")))
133   "Face used in pick mode for non-matching subjects."
134   :group 'notmuch-pick
135   :group 'notmuch-faces)
136
137 (defface notmuch-pick-no-match-author-face
138   '((t (:foreground "gray")))
139   "Face used in pick mode for the date in messages matching the query."
140   :group 'notmuch-pick
141   :group 'notmuch-faces)
142
143 (defface notmuch-pick-no-match-tag-face
144   '((t (:foreground "gray")))
145   "Face used in pick mode face for non-matching tags."
146   :group 'notmuch-pick
147   :group 'notmuch-faces)
148
149 (defvar notmuch-pick-previous-subject "")
150 (make-variable-buffer-local 'notmuch-pick-previous-subject)
151
152 ;; The basic query i.e. the key part of the search request.
153 (defvar notmuch-pick-basic-query nil)
154 (make-variable-buffer-local 'notmuch-pick-basic-query)
155 ;; The context of the search: i.e., useful but can be dropped.
156 (defvar notmuch-pick-query-context nil)
157 (make-variable-buffer-local 'notmuch-pick-query-context)
158 (defvar notmuch-pick-buffer-name nil)
159 (make-variable-buffer-local 'notmuch-pick-buffer-name)
160 (defvar notmuch-pick-message-window nil)
161 (make-variable-buffer-local 'notmuch-pick-message-window)
162 (put 'notmuch-pick-message-window 'permanent-local t)
163 (defvar notmuch-pick-message-buffer nil)
164 (make-variable-buffer-local 'notmuch-pick-message-buffer-name)
165 (put 'notmuch-pick-message-buffer-name 'permanent-local t)
166 (defvar notmuch-pick-process-state nil
167   "Parsing state of the search process filter.")
168
169
170 (defvar notmuch-pick-mode-map
171   (let ((map (make-sparse-keymap)))
172     (define-key map [mouse-1] 'notmuch-pick-show-message)
173     (define-key map "q" 'notmuch-pick-quit)
174     (define-key map "x" 'notmuch-pick-quit)
175     (define-key map "?" 'notmuch-help)
176     (define-key map "a" 'notmuch-pick-archive-message)
177     (define-key map "=" 'notmuch-pick-refresh-view)
178     (define-key map "s" 'notmuch-search)
179     (define-key map "z" 'notmuch-pick)
180     (define-key map "m" 'notmuch-pick-new-mail)
181     (define-key map "f" 'notmuch-pick-forward-message)
182     (define-key map "r" 'notmuch-pick-reply-sender)
183     (define-key map "R" 'notmuch-pick-reply)
184     (define-key map "n" 'notmuch-pick-next-matching-message)
185     (define-key map "p" 'notmuch-pick-prev-matching-message)
186     (define-key map "N" 'notmuch-pick-next-message)
187     (define-key map "P" 'notmuch-pick-prev-message)
188     (define-key map "|" 'notmuch-pick-pipe-message)
189     (define-key map "-" 'notmuch-pick-remove-tag)
190     (define-key map "+" 'notmuch-pick-add-tag)
191     (define-key map " " 'notmuch-pick-scroll-or-next)
192     (define-key map "b" 'notmuch-pick-scroll-message-window-back)
193     map))
194 (fset 'notmuch-pick-mode-map notmuch-pick-mode-map)
195
196 (defun notmuch-pick-setup-show-out ()
197   (let ((map notmuch-pick-mode-map))
198     (if notmuch-pick-show-out
199         (progn
200           (define-key map (kbd "M-RET") 'notmuch-pick-show-message)
201           (define-key map (kbd "RET") 'notmuch-pick-show-message-out))
202       (progn
203         (define-key map (kbd "RET") 'notmuch-pick-show-message)
204         (define-key map (kbd "M-RET") 'notmuch-pick-show-message-out)))))
205
206 (defun notmuch-pick-get-message-properties ()
207   "Return the properties of the current message as a plist.
208
209 Some useful entries are:
210 :headers - Property list containing the headers :Date, :Subject, :From, etc.
211 :tags - Tags for this message"
212   (save-excursion
213     (beginning-of-line)
214     (get-text-property (point) :notmuch-message-properties)))
215
216 (defun notmuch-pick-set-message-properties (props)
217   (save-excursion
218     (beginning-of-line)
219     (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
220
221 (defun notmuch-pick-set-prop (prop val &optional props)
222   (let ((inhibit-read-only t)
223         (props (or props
224                    (notmuch-pick-get-message-properties))))
225     (plist-put props prop val)
226     (notmuch-pick-set-message-properties props)))
227
228 (defun notmuch-pick-get-prop (prop &optional props)
229   (let ((props (or props
230                    (notmuch-pick-get-message-properties))))
231     (plist-get props prop)))
232
233 (defun notmuch-pick-set-tags (tags)
234   "Set the tags of the current message."
235   (notmuch-pick-set-prop :tags tags))
236
237 (defun notmuch-pick-get-tags ()
238   "Return the tags of the current message."
239   (notmuch-pick-get-prop :tags))
240
241 (defun notmuch-pick-get-message-id ()
242   "Return the message id of the current message."
243   (let ((id (notmuch-pick-get-prop :id)))
244     (if id
245         (notmuch-id-to-query id)
246       nil)))
247
248 (defun notmuch-pick-get-match ()
249   "Return whether the current message is a match."
250   (interactive)
251   (notmuch-pick-get-prop :match))
252
253 (defun notmuch-pick-refresh-result ()
254   (let ((init-point (point))
255         (end (line-end-position))
256         (msg (notmuch-pick-get-message-properties))
257         (inhibit-read-only t))
258     (beginning-of-line)
259     (delete-region (point) (1+ (line-end-position)))
260     (notmuch-pick-insert-msg msg)
261     (let ((new-end (line-end-position)))
262       (goto-char (if (= init-point end)
263                      new-end
264                    (min init-point (- new-end 1)))))))
265
266 (defun notmuch-pick-tag-update-display (&optional tag-changes)
267   "Update display for TAG-CHANGES to current message.
268
269 Does NOT change the database."
270   (let* ((current-tags (notmuch-pick-get-tags))
271          (new-tags (notmuch-update-tags current-tags tag-changes)))
272     (unless (equal current-tags new-tags)
273       (notmuch-pick-set-tags new-tags)
274       (notmuch-pick-refresh-result))))
275
276 (defun notmuch-pick-tag (&optional tag-changes)
277   "Change tags for the current message"
278   (interactive)
279   (setq tag-changes (funcall 'notmuch-tag (notmuch-pick-get-message-id) tag-changes))
280   (notmuch-pick-tag-update-display tag-changes))
281
282 (defun notmuch-pick-add-tag ()
283   "Same as `notmuch-pick-tag' but sets initial input to '+'."
284   (interactive)
285   (notmuch-pick-tag "+"))
286
287 (defun notmuch-pick-remove-tag ()
288   "Same as `notmuch-pick-tag' but sets initial input to '-'."
289   (interactive)
290   (notmuch-pick-tag "-"))
291
292 ;; This function should be in notmuch-hello.el but we are trying to
293 ;; minimise impact on the rest of the codebase.
294 (defun notmuch-pick-from-hello (&optional search)
295   "Run a query and display results in experimental notmuch-pick mode"
296   (interactive)
297   (unless (null search)
298     (setq search (notmuch-hello-trim search))
299     (let ((history-delete-duplicates t))
300       (add-to-history 'notmuch-search-history search)))
301   (notmuch-pick search))
302
303 ;; This function should be in notmuch-show.el but be we trying to
304 ;; minimise impact on the rest of the codebase.
305 (defun notmuch-pick-from-show-current-query ()
306   "Call notmuch pick with the current query"
307   (interactive)
308   (notmuch-pick notmuch-show-thread-id notmuch-show-query-context))
309
310 ;; This function should be in notmuch.el but be we trying to minimise
311 ;; impact on the rest of the codebase.
312 (defun notmuch-pick-from-search-current-query ()
313   "Call notmuch pick with the current query"
314   (interactive)
315   (notmuch-pick notmuch-search-query-string))
316
317 ;; This function should be in notmuch.el but be we trying to minimise
318 ;; impact on the rest of the codebase.
319 (defun notmuch-pick-from-search-thread ()
320   "Show the selected thread with notmuch-pick"
321   (interactive)
322   (notmuch-pick (notmuch-search-find-thread-id)
323                 notmuch-search-query-string
324                 (notmuch-prettify-subject (notmuch-search-find-subject)))
325   (notmuch-pick-show-match-message-with-wait))
326
327 (defun notmuch-pick-show-message ()
328   "Show the current message (in split-pane)."
329   (interactive)
330   (let ((id (notmuch-pick-get-message-id))
331         (inhibit-read-only t)
332         buffer)
333     (when id
334       ;; We close and reopen the window to kill off un-needed buffers
335       ;; this might cause flickering but seems ok.
336       (notmuch-pick-close-message-window)
337       (setq notmuch-pick-message-window
338             (split-window-vertically (/ (window-height) 4)))
339       (with-selected-window notmuch-pick-message-window
340         (setq current-prefix-arg '(4))
341         (setq buffer (notmuch-show id nil nil nil)))
342       (notmuch-pick-tag-update-display (list "-unread")))
343     (setq notmuch-pick-message-buffer buffer)))
344
345 (defun notmuch-pick-show-message-out ()
346   "Show the current message (in whole window)."
347   (interactive)
348   (let ((id (notmuch-pick-get-message-id))
349         (inhibit-read-only t)
350         buffer)
351     (when id
352       ;; We close the window to kill off un-needed buffers.
353       (notmuch-pick-close-message-window)
354       (notmuch-show id nil nil nil))))
355
356 (defun notmuch-pick-scroll-message-window ()
357   "Scroll the message window (if it exists)"
358   (interactive)
359   (when (window-live-p notmuch-pick-message-window)
360     (with-selected-window notmuch-pick-message-window
361       (if (pos-visible-in-window-p (point-max))
362           t
363         (scroll-up)))))
364
365 (defun notmuch-pick-scroll-message-window-back ()
366   "Scroll the message window back(if it exists)"
367   (interactive)
368   (when (window-live-p notmuch-pick-message-window)
369     (with-selected-window notmuch-pick-message-window
370       (if (pos-visible-in-window-p (point-min))
371           t
372         (scroll-down)))))
373
374 (defun notmuch-pick-scroll-or-next ()
375   "Scroll the message window. If it at end go to next message."
376   (interactive)
377   (when (notmuch-pick-scroll-message-window)
378     (notmuch-pick-next-matching-message)))
379
380 (defun notmuch-pick-quit ()
381   "Close the split view or exit pick."
382   (interactive)
383   (unless (notmuch-pick-close-message-window)
384     (kill-buffer (current-buffer))))
385
386 (defun notmuch-pick-close-message-window ()
387   "Close the message-window. Return t if close succeeds."
388   (interactive)
389   (when (and (window-live-p notmuch-pick-message-window)
390              (eq (window-buffer notmuch-pick-message-window) notmuch-pick-message-buffer))
391     (delete-window notmuch-pick-message-window)
392     (unless (get-buffer-window-list notmuch-pick-message-buffer)
393       (kill-buffer notmuch-pick-message-buffer))
394     t))
395
396 (defun notmuch-pick-archive-message ()
397   "Archive the current message and move to next matching message."
398   (interactive)
399   (notmuch-pick-tag "-inbox")
400   (notmuch-pick-next-matching-message))
401
402 (defun notmuch-pick-next-message ()
403   "Move to next message."
404   (interactive)
405   (forward-line)
406   (when (window-live-p notmuch-pick-message-window)
407     (notmuch-pick-show-message)))
408
409 (defun notmuch-pick-prev-message ()
410   "Move to previous message."
411   (interactive)
412   (forward-line -1)
413   (when (window-live-p notmuch-pick-message-window)
414     (notmuch-pick-show-message)))
415
416 (defun notmuch-pick-prev-matching-message ()
417   "Move to previous matching message."
418   (interactive)
419   (forward-line -1)
420   (while (and (not (bobp)) (not (notmuch-pick-get-match)))
421     (forward-line -1))
422   (when (window-live-p notmuch-pick-message-window)
423     (notmuch-pick-show-message)))
424
425 (defun notmuch-pick-next-matching-message ()
426   "Move to next matching message."
427   (interactive)
428   (forward-line)
429   (while (and (not (eobp)) (not (notmuch-pick-get-match)))
430     (forward-line))
431   (when (window-live-p notmuch-pick-message-window)
432     (notmuch-pick-show-message)))
433
434 (defun notmuch-pick-show-match-message-with-wait ()
435   "Show the first matching message but wait for it to appear or search to finish."
436   (interactive)
437   (unless (notmuch-pick-get-match)
438     (notmuch-pick-next-matching-message))
439   (while (and (not (notmuch-pick-get-match))
440               (get-buffer-process (current-buffer)))
441     (message "waiting for message")
442     (sit-for 0.1)
443     (goto-char (point-min))
444     (unless (notmuch-pick-get-match)
445       (notmuch-pick-next-matching-message)))
446   (message nil)
447   (when (notmuch-pick-get-match)
448     (notmuch-pick-show-message)))
449
450 (defun notmuch-pick-refresh-view ()
451   "Refresh view."
452   (interactive)
453   (let ((inhibit-read-only t)
454         (basic-query notmuch-pick-basic-query)
455         (query-context notmuch-pick-query-context)
456         (buffer-name notmuch-pick-buffer-name))
457     (erase-buffer)
458     (notmuch-pick-worker basic-query query-context (get-buffer buffer-name))))
459
460 (defmacro with-current-notmuch-pick-message (&rest body)
461   "Evaluate body with current buffer set to the text of current message"
462   `(save-excursion
463      (let ((id (notmuch-pick-get-message-id)))
464        (let ((buf (generate-new-buffer (concat "*notmuch-msg-" id "*"))))
465          (with-current-buffer buf
466             (call-process notmuch-command nil t nil "show" "--format=raw" id)
467            ,@body)
468          (kill-buffer buf)))))
469
470 (defun notmuch-pick-new-mail (&optional prompt-for-sender)
471   "Compose new mail."
472   (interactive "P")
473   (notmuch-pick-close-message-window)
474   (notmuch-mua-new-mail prompt-for-sender ))
475
476 (defun notmuch-pick-forward-message (&optional prompt-for-sender)
477   "Forward the current message."
478   (interactive "P")
479   (notmuch-pick-close-message-window)
480   (with-current-notmuch-pick-message
481    (notmuch-mua-new-forward-message prompt-for-sender)))
482
483 (defun notmuch-pick-reply (&optional prompt-for-sender)
484   "Reply to the sender and all recipients of the current message."
485   (interactive "P")
486   (notmuch-pick-close-message-window)
487   (notmuch-mua-new-reply (notmuch-pick-get-message-id) prompt-for-sender t))
488
489 (defun notmuch-pick-reply-sender (&optional prompt-for-sender)
490   "Reply to the sender of the current message."
491   (interactive "P")
492   (notmuch-pick-close-message-window)
493   (notmuch-mua-new-reply (notmuch-pick-get-message-id) prompt-for-sender nil))
494
495 ;; Shamelessly stolen from notmuch-show.el: maybe should be unified.
496 (defun notmuch-pick-pipe-message (command)
497   "Pipe the contents of the current message to the given command.
498
499 The given command will be executed with the raw contents of the
500 current email message as stdin. Anything printed by the command
501 to stdout or stderr will appear in the *notmuch-pipe* buffer.
502
503 When invoked with a prefix argument, the command will receive all
504 open messages in the current thread (formatted as an mbox) rather
505 than only the current message."
506   (interactive "sPipe message to command: ")
507   (let ((shell-command
508          (concat notmuch-command " show --format=raw "
509                  (shell-quote-argument (notmuch-pick-get-message-id)) " | " command))
510          (buf (get-buffer-create (concat "*notmuch-pipe*"))))
511     (with-current-buffer buf
512       (setq buffer-read-only nil)
513       (erase-buffer)
514       (let ((exit-code (call-process-shell-command shell-command nil buf)))
515         (goto-char (point-max))
516         (set-buffer-modified-p nil)
517         (setq buffer-read-only t)
518         (unless (zerop exit-code)
519           (switch-to-buffer-other-window buf)
520           (message (format "Command '%s' exited abnormally with code %d"
521                            shell-command exit-code)))))))
522
523 (defun notmuch-pick-clean-address (address)
524   "Try to clean a single email ADDRESS for display. Return
525 AUTHOR_NAME if present, otherwise return AUTHOR_EMAIL. Return
526 unchanged ADDRESS if parsing fails."
527   (let* ((clean-address (notmuch-clean-address address))
528          (p-address (car clean-address))
529          (p-name (cdr clean-address)))
530
531     ;; If we have a name return that otherwise return the address.
532     (or p-name p-address)))
533
534 (defun notmuch-pick-insert-field (field format-string msg)
535   (let* ((headers (plist-get msg :headers))
536         (match (plist-get msg :match)))
537     (cond
538      ((string-equal field "date")
539       (let ((face (if match
540                       'notmuch-pick-match-date-face
541                     'notmuch-pick-no-match-date-face)))
542         (insert (propertize (format format-string (plist-get msg :date_relative))
543                             'face face))))
544
545      ((string-equal field "subject")
546       (let ((tree-status (plist-get msg :tree-status))
547             (bare-subject (notmuch-show-strip-re (plist-get headers :Subject)))
548             (face (if match
549                       'notmuch-pick-match-subject-face
550                     'notmuch-pick-no-match-subject-face)))
551         (insert (propertize (format format-string
552                                     (concat
553                                      (mapconcat #'identity (reverse tree-status) "")
554                                      (if (string= notmuch-pick-previous-subject bare-subject)
555                                          " ..."
556                                        bare-subject)))
557                             'face face))
558         (setq notmuch-pick-previous-subject bare-subject)))
559
560      ((string-equal field "authors")
561       (let ((author (notmuch-pick-clean-address (plist-get headers :From)))
562             (len (length (format format-string "")))
563             (face (if match
564                       'notmuch-pick-match-author-face
565                     'notmuch-pick-no-match-author-face)))
566         (when (> (length author) len)
567           (setq author (substring author 0 len)))
568         (insert (propertize (format format-string author)
569                             'face face))))
570
571      ((string-equal field "tags")
572       (let ((tags (plist-get msg :tags))
573             (face (if match
574                           'notmuch-pick-match-tag-face
575                         'notmuch-pick-no-match-tag-face)))
576         (when tags
577           (insert (propertize (format format-string
578                                       (mapconcat #'identity tags ", "))
579                               'face face))))))))
580
581 (defun notmuch-pick-insert-msg (msg)
582   "Insert the message MSG according to notmuch-pick-result-format"
583   (dolist (spec notmuch-pick-result-format)
584     (notmuch-pick-insert-field (car spec) (cdr spec) msg))
585   (notmuch-pick-set-message-properties msg)
586   (insert "\n"))
587
588 (defun notmuch-pick-insert-tree (tree depth tree-status first last)
589   "Insert the message tree TREE at depth DEPTH in the current thread."
590   (let ((msg (car tree))
591         (replies (cadr tree)))
592
593       (cond
594        ((and (< 0 depth) (not last))
595         (push "├" tree-status))
596        ((and (< 0 depth) last)
597         (push "╰" tree-status))
598        ((and (eq 0 depth) first last)
599 ;;        (push "─" tree-status)) choice between this and next line is matter of taste.
600         (push " " tree-status))
601        ((and (eq 0 depth) first (not last))
602           (push "┬" tree-status))
603        ((and (eq 0 depth) (not first) last)
604         (push "╰" tree-status))
605        ((and (eq 0 depth) (not first) (not last))
606         (push "├" tree-status)))
607
608       (push (concat (if replies "┬" "─") "►") tree-status)
609       (notmuch-pick-insert-msg (plist-put msg :tree-status tree-status))
610       (pop tree-status)
611       (pop tree-status)
612
613       (if last
614           (push " " tree-status)
615         (push "│" tree-status))
616
617     (notmuch-pick-insert-thread replies (1+ depth) tree-status)))
618
619 (defun notmuch-pick-insert-thread (thread depth tree-status)
620   "Insert the thread THREAD at depth DEPTH >= 1 in the current forest."
621   (let ((n (length thread)))
622     (loop for tree in thread
623           for count from 1 to n
624
625           do (notmuch-pick-insert-tree tree depth tree-status (eq count 1) (eq count n)))))
626
627 (defun notmuch-pick-insert-forest-thread (forest-thread)
628   (save-excursion
629     (goto-char (point-max))
630     (let (tree-status)
631       ;; Reset at the start of each main thread.
632       (setq notmuch-pick-previous-subject nil)
633       (notmuch-pick-insert-thread forest-thread 0 tree-status))))
634
635 (defun notmuch-pick-insert-forest (forest)
636   (mapc 'notmuch-pick-insert-forest-thread forest))
637
638 (defun notmuch-pick-mode ()
639   "Major mode displaying messages (as opposed to threads) of of a notmuch search.
640
641 This buffer contains the results of a \"notmuch pick\" of your
642 email archives. Each line in the buffer represents a single
643 message giving the relative date, the author, subject, and any
644 tags.
645
646 Pressing \\[notmuch-pick-show-message] on any line displays that message.
647
648 Complete list of currently available key bindings:
649
650 \\{notmuch-pick-mode-map}"
651
652   (interactive)
653   (kill-all-local-variables)
654   (use-local-map notmuch-pick-mode-map)
655   (setq major-mode 'notmuch-pick-mode
656         mode-name "notmuch-pick")
657   (hl-line-mode 1)
658   (setq buffer-read-only t
659         truncate-lines t))
660
661 (defun notmuch-pick-process-sentinel (proc msg)
662   "Add a message to let user know when \"notmuch pick\" exits"
663   (let ((buffer (process-buffer proc))
664         (status (process-status proc))
665         (exit-status (process-exit-status proc))
666         (never-found-target-thread nil))
667     (when (memq status '(exit signal))
668         (kill-buffer (process-get proc 'parse-buf))
669         (if (buffer-live-p buffer)
670             (with-current-buffer buffer
671               (save-excursion
672                 (let ((inhibit-read-only t)
673                       (atbob (bobp)))
674                   (goto-char (point-max))
675                   (if (eq status 'signal)
676                       (insert "Incomplete search results (pick process was killed).\n"))
677                   (when (eq status 'exit)
678                     (insert "End of search results.")
679                     (unless (= exit-status 0)
680                       (insert (format " (process returned %d)" exit-status)))
681                     (insert "\n")))))))))
682
683
684 (defun notmuch-pick-show-error (string &rest objects)
685   (save-excursion
686     (goto-char (point-max))
687     (insert "Error: Unexpected output from notmuch search:\n")
688     (insert (apply #'format string objects))
689     (insert "\n")))
690
691
692 (defun notmuch-pick-process-filter (proc string)
693   "Process and filter the output of \"notmuch show\" (for pick)"
694   (let ((results-buf (process-buffer proc))
695         (parse-buf (process-get proc 'parse-buf))
696         (inhibit-read-only t)
697         done)
698     (if (not (buffer-live-p results-buf))
699         (delete-process proc)
700       (with-current-buffer parse-buf
701         ;; Insert new data
702         (save-excursion
703           (goto-char (point-max))
704           (insert string))
705         (notmuch-json-parse-partial-list 'notmuch-pick-insert-forest-thread
706                                          'notmuch-pick-show-error
707                                          results-buf)))))
708
709 (defun notmuch-pick-worker (basic-query &optional query-context buffer)
710   (interactive)
711   (notmuch-pick-mode)
712   (setq notmuch-pick-basic-query basic-query)
713   (setq notmuch-pick-query-context query-context)
714   (setq notmuch-pick-buffer-name (buffer-name buffer))
715
716   (erase-buffer)
717   (goto-char (point-min))
718   (let* ((search-args (concat basic-query
719                        (if query-context (concat " and (" query-context ")"))
720                        ))
721          (message-arg "--entire-thread"))
722     (if (equal (car (process-lines notmuch-command "count" search-args)) "0")
723         (setq search-args basic-query))
724     (if notmuch-pick-asynchronous-parser
725         (let ((proc (start-process
726                      "notmuch-pick" buffer
727                      notmuch-command "show" "--body=false" "--format=json"
728                      message-arg search-args))
729               ;; Use a scratch buffer to accumulate partial output.
730               ;; This buffer will be killed by the sentinel, which
731               ;; should be called no matter how the process dies.
732               (parse-buf (generate-new-buffer " *notmuch pick parse*")))
733           (process-put proc 'parse-buf parse-buf)
734           (set-process-sentinel proc 'notmuch-pick-process-sentinel)
735           (set-process-filter proc 'notmuch-pick-process-filter)
736           (set-process-query-on-exit-flag proc nil))
737       (progn
738         (notmuch-pick-insert-forest
739          (notmuch-query-get-threads
740           (list "--body=false" message-arg search-args)))
741         (save-excursion
742           (goto-char (point-max))
743           (insert "End of search results.\n"))))))
744
745
746 (defun notmuch-pick (&optional query query-context buffer-name show-first-match)
747   "Run notmuch pick with the given `query' and display the results"
748   (interactive "sNotmuch pick: ")
749   (if (null query)
750       (setq query (notmuch-read-query "Notmuch pick: ")))
751   (let ((buffer (get-buffer-create (generate-new-buffer-name
752                                     (or buffer-name
753                                         (concat "*notmuch-pick-" query "*")))))
754         (inhibit-read-only t))
755
756     (switch-to-buffer buffer)
757     ;; Don't track undo information for this buffer
758     (set 'buffer-undo-list t)
759
760     (notmuch-pick-worker query query-context buffer)
761
762     (setq truncate-lines t)
763     (when show-first-match
764       (notmuch-pick-show-match-message-with-wait))))
765
766
767 ;; Set up key bindings from the rest of notmuch.
768 (define-key 'notmuch-search-mode-map "z" 'notmuch-pick)
769 (define-key 'notmuch-search-mode-map "Z" 'notmuch-pick-from-search-current-query)
770 (define-key 'notmuch-search-mode-map (kbd "M-RET") 'notmuch-pick-from-search-thread)
771 (define-key 'notmuch-hello-mode-map "z" 'notmuch-pick-from-hello)
772 (define-key 'notmuch-show-mode-map "z" 'notmuch-pick)
773 (define-key 'notmuch-show-mode-map "Z" 'notmuch-pick-from-show-current-query)
774 (notmuch-pick-setup-show-out)
775 (message "Initialised notmuch-pick")
776
777 (provide 'notmuch-pick)