]> git.notmuchmail.org Git - notmuch/blob - contrib/notmuch-pick/notmuch-pick.el
contrib: pick: remove hack notmuch-pick-show-match-message-with-wait
[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-target-msg nil)
159 (make-variable-buffer-local 'notmuch-pick-target-msg)
160 (defvar notmuch-pick-open-target nil)
161 (make-variable-buffer-local 'notmuch-pick-open-target)
162 (defvar notmuch-pick-buffer-name nil)
163 (make-variable-buffer-local 'notmuch-pick-buffer-name)
164 ;; This variable is the window used for the message pane. It is set
165 ;; in both the parent pick buffer and the child show buffer. It is
166 ;; used to try and close the message pane when quitting pick or the
167 ;; child show buffer.
168 (defvar notmuch-pick-message-window nil)
169 (make-variable-buffer-local 'notmuch-pick-message-window)
170 (put 'notmuch-pick-message-window 'permanent-local t)
171 (defvar notmuch-pick-message-buffer nil)
172 (make-variable-buffer-local 'notmuch-pick-message-buffer-name)
173 (put 'notmuch-pick-message-buffer-name 'permanent-local t)
174 (defvar notmuch-pick-process-state nil
175   "Parsing state of the search process filter.")
176
177
178 (defvar notmuch-pick-mode-map
179   (let ((map (make-sparse-keymap)))
180     (define-key map [mouse-1] 'notmuch-pick-show-message)
181     (define-key map "q" 'notmuch-pick-quit)
182     (define-key map "x" 'notmuch-pick-quit)
183     (define-key map "?" 'notmuch-help)
184     (define-key map "a" 'notmuch-pick-archive-message-then-next)
185     (define-key map "=" 'notmuch-pick-refresh-view)
186     (define-key map "s" 'notmuch-pick-to-search)
187     (define-key map "z" 'notmuch-pick-to-pick)
188     (define-key map "m" 'notmuch-pick-new-mail)
189     (define-key map "f" 'notmuch-pick-forward-message)
190     (define-key map "r" 'notmuch-pick-reply-sender)
191     (define-key map "R" 'notmuch-pick-reply)
192     (define-key map "n" 'notmuch-pick-next-matching-message)
193     (define-key map "p" 'notmuch-pick-prev-matching-message)
194     (define-key map "N" 'notmuch-pick-next-message)
195     (define-key map "P" 'notmuch-pick-prev-message)
196     (define-key map "|" 'notmuch-pick-pipe-message)
197     (define-key map "-" 'notmuch-pick-remove-tag)
198     (define-key map "+" 'notmuch-pick-add-tag)
199     (define-key map " " 'notmuch-pick-scroll-or-next)
200     (define-key map "b" 'notmuch-pick-scroll-message-window-back)
201     map))
202 (fset 'notmuch-pick-mode-map notmuch-pick-mode-map)
203
204 (defun notmuch-pick-setup-show-out ()
205   (let ((map notmuch-pick-mode-map))
206     (if notmuch-pick-show-out
207         (progn
208           (define-key map (kbd "M-RET") 'notmuch-pick-show-message)
209           (define-key map (kbd "RET") 'notmuch-pick-show-message-out))
210       (progn
211         (define-key map (kbd "RET") 'notmuch-pick-show-message)
212         (define-key map (kbd "M-RET") 'notmuch-pick-show-message-out)))))
213
214 (defun notmuch-pick-get-message-properties ()
215   "Return the properties of the current message as a plist.
216
217 Some useful entries are:
218 :headers - Property list containing the headers :Date, :Subject, :From, etc.
219 :tags - Tags for this message"
220   (save-excursion
221     (beginning-of-line)
222     (get-text-property (point) :notmuch-message-properties)))
223
224 (defun notmuch-pick-set-message-properties (props)
225   (save-excursion
226     (beginning-of-line)
227     (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
228
229 (defun notmuch-pick-set-prop (prop val &optional props)
230   (let ((inhibit-read-only t)
231         (props (or props
232                    (notmuch-pick-get-message-properties))))
233     (plist-put props prop val)
234     (notmuch-pick-set-message-properties props)))
235
236 (defun notmuch-pick-get-prop (prop &optional props)
237   (let ((props (or props
238                    (notmuch-pick-get-message-properties))))
239     (plist-get props prop)))
240
241 (defun notmuch-pick-set-tags (tags)
242   "Set the tags of the current message."
243   (notmuch-pick-set-prop :tags tags))
244
245 (defun notmuch-pick-get-tags ()
246   "Return the tags of the current message."
247   (notmuch-pick-get-prop :tags))
248
249 (defun notmuch-pick-get-message-id ()
250   "Return the message id of the current message."
251   (let ((id (notmuch-pick-get-prop :id)))
252     (if id
253         (notmuch-id-to-query id)
254       nil)))
255
256 (defun notmuch-pick-get-match ()
257   "Return whether the current message is a match."
258   (interactive)
259   (notmuch-pick-get-prop :match))
260
261 (defun notmuch-pick-refresh-result ()
262   (let ((init-point (point))
263         (end (line-end-position))
264         (msg (notmuch-pick-get-message-properties))
265         (inhibit-read-only t))
266     (beginning-of-line)
267     (delete-region (point) (1+ (line-end-position)))
268     (notmuch-pick-insert-msg msg)
269     (let ((new-end (line-end-position)))
270       (goto-char (if (= init-point end)
271                      new-end
272                    (min init-point (- new-end 1)))))))
273
274 (defun notmuch-pick-tag-update-display (&optional tag-changes)
275   "Update display for TAG-CHANGES to current message.
276
277 Does NOT change the database."
278   (let* ((current-tags (notmuch-pick-get-tags))
279          (new-tags (notmuch-update-tags current-tags tag-changes)))
280     (unless (equal current-tags new-tags)
281       (notmuch-pick-set-tags new-tags)
282       (notmuch-pick-refresh-result))))
283
284 (defun notmuch-pick-tag (&optional tag-changes)
285   "Change tags for the current message"
286   (interactive)
287   (setq tag-changes (notmuch-tag (notmuch-pick-get-message-id) tag-changes))
288   (notmuch-pick-tag-update-display tag-changes))
289
290 (defun notmuch-pick-add-tag ()
291   "Same as `notmuch-pick-tag' but sets initial input to '+'."
292   (interactive)
293   (notmuch-pick-tag "+"))
294
295 (defun notmuch-pick-remove-tag ()
296   "Same as `notmuch-pick-tag' but sets initial input to '-'."
297   (interactive)
298   (notmuch-pick-tag "-"))
299
300 ;; The next two functions close the message window before searching or
301 ;; picking but they do so after the user has entered the query (in
302 ;; case the user was basing the query on something in the message
303 ;; window).
304
305 (defun notmuch-pick-to-search ()
306   "Run \"notmuch search\" with the given `query' and display results."
307   (interactive)
308   (let ((query (notmuch-read-query "Notmuch search: ")))
309     (notmuch-pick-close-message-window)
310     (notmuch-search query)))
311
312 (defun notmuch-pick-to-pick ()
313   "Run a query and display results in experimental notmuch-pick mode"
314   (interactive)
315   (let ((query (notmuch-read-query "Notmuch pick: ")))
316     (notmuch-pick-close-message-window)
317     (notmuch-pick query)))
318
319 ;; This function should be in notmuch-hello.el but we are trying to
320 ;; minimise impact on the rest of the codebase.
321 (defun notmuch-pick-from-hello (&optional search)
322   "Run a query and display results in experimental notmuch-pick mode"
323   (interactive)
324   (unless (null search)
325     (setq search (notmuch-hello-trim search))
326     (let ((history-delete-duplicates t))
327       (add-to-history 'notmuch-search-history search)))
328   (notmuch-pick search))
329
330 ;; This function should be in notmuch-show.el but be we trying to
331 ;; minimise impact on the rest of the codebase.
332 (defun notmuch-pick-from-show-current-query ()
333   "Call notmuch pick with the current query"
334   (interactive)
335   (notmuch-pick notmuch-show-thread-id
336                 notmuch-show-query-context
337                 (notmuch-show-get-message-id)))
338
339 ;; This function should be in notmuch.el but be we trying to minimise
340 ;; impact on the rest of the codebase.
341 (defun notmuch-pick-from-search-current-query ()
342   "Call notmuch pick with the current query"
343   (interactive)
344   (notmuch-pick notmuch-search-query-string))
345
346 ;; This function should be in notmuch.el but be we trying to minimise
347 ;; impact on the rest of the codebase.
348 (defun notmuch-pick-from-search-thread ()
349   "Show the selected thread with notmuch-pick"
350   (interactive)
351   (notmuch-pick (notmuch-search-find-thread-id)
352                 notmuch-search-query-string
353                 nil
354                 (notmuch-prettify-subject (notmuch-search-find-subject))
355                 t))
356
357 (defun notmuch-pick-message-window-kill-hook ()
358   (let ((buffer (current-buffer)))
359     (when (and (window-live-p notmuch-pick-message-window)
360                (eq (window-buffer notmuch-pick-message-window) buffer))
361       ;; We do not want an error if this is the sole window in the
362       ;; frame and I do not know how to test for that in emacs pre
363       ;; 24. Hence we just ignore-errors.
364       (ignore-errors
365         (delete-window notmuch-pick-message-window)))))
366
367 (defun notmuch-pick-show-message ()
368   "Show the current message (in split-pane)."
369   (interactive)
370   (let ((id (notmuch-pick-get-message-id))
371         (inhibit-read-only t)
372         buffer)
373     (when id
374       ;; We close and reopen the window to kill off un-needed buffers
375       ;; this might cause flickering but seems ok.
376       (notmuch-pick-close-message-window)
377       (setq notmuch-pick-message-window
378             (split-window-vertically (/ (window-height) 4)))
379       (with-selected-window notmuch-pick-message-window
380         ;; Since we are only displaying one message do not indent.
381         (let ((notmuch-show-indent-messages-width 0)
382               (notmuch-show-only-matching-messages t))
383           (setq buffer (notmuch-show id nil nil nil))))
384       ;; We need the `let' as notmuch-pick-message-window is buffer local.
385       (let ((window notmuch-pick-message-window))
386         (with-current-buffer buffer
387           (setq notmuch-pick-message-window window)
388           (add-hook 'kill-buffer-hook 'notmuch-pick-message-window-kill-hook)))
389       (when notmuch-show-mark-read-tags
390         (notmuch-pick-tag-update-display notmuch-show-mark-read-tags))
391       (setq notmuch-pick-message-buffer buffer))))
392
393 (defun notmuch-pick-show-message-out ()
394   "Show the current message (in whole window)."
395   (interactive)
396   (let ((id (notmuch-pick-get-message-id))
397         (inhibit-read-only t)
398         buffer)
399     (when id
400       ;; We close the window to kill off un-needed buffers.
401       (notmuch-pick-close-message-window)
402       (notmuch-show id nil nil nil))))
403
404 (defun notmuch-pick-scroll-message-window ()
405   "Scroll the message window (if it exists)"
406   (interactive)
407   (when (window-live-p notmuch-pick-message-window)
408     (with-selected-window notmuch-pick-message-window
409       (if (pos-visible-in-window-p (point-max))
410           t
411         (scroll-up)))))
412
413 (defun notmuch-pick-scroll-message-window-back ()
414   "Scroll the message window back(if it exists)"
415   (interactive)
416   (when (window-live-p notmuch-pick-message-window)
417     (with-selected-window notmuch-pick-message-window
418       (if (pos-visible-in-window-p (point-min))
419           t
420         (scroll-down)))))
421
422 (defun notmuch-pick-scroll-or-next ()
423   "Scroll the message window. If it at end go to next message."
424   (interactive)
425   (when (notmuch-pick-scroll-message-window)
426     (notmuch-pick-next-matching-message)))
427
428 (defun notmuch-pick-quit ()
429   "Close the split view or exit pick."
430   (interactive)
431   (unless (notmuch-pick-close-message-window)
432     (kill-buffer (current-buffer))))
433
434 (defun notmuch-pick-close-message-window ()
435   "Close the message-window. Return t if close succeeds."
436   (interactive)
437   (when (and (window-live-p notmuch-pick-message-window)
438              (eq (window-buffer notmuch-pick-message-window) notmuch-pick-message-buffer))
439     (delete-window notmuch-pick-message-window)
440     (unless (get-buffer-window-list notmuch-pick-message-buffer)
441       (kill-buffer notmuch-pick-message-buffer))
442     t))
443
444 (defun notmuch-pick-archive-message (&optional unarchive)
445   "Archive the current message.
446
447 Archive the current message by applying the tag changes in
448 `notmuch-archive-tags' to it. If a prefix argument is given, the
449 message will be \"unarchived\", i.e. the tag changes in
450 `notmuch-archive-tags' will be reversed."
451   (interactive "P")
452   (when notmuch-archive-tags
453     (apply 'notmuch-pick-tag
454            (notmuch-tag-change-list notmuch-archive-tags unarchive))))
455
456 (defun notmuch-pick-archive-message-then-next (&optional unarchive)
457   "Archive the current message and move to next matching message."
458   (interactive "P")
459   (notmuch-pick-archive-message unarchive)
460   (notmuch-pick-next-matching-message))
461
462 (defun notmuch-pick-next-message ()
463   "Move to next message."
464   (interactive)
465   (forward-line)
466   (when (window-live-p notmuch-pick-message-window)
467     (notmuch-pick-show-message)))
468
469 (defun notmuch-pick-prev-message ()
470   "Move to previous message."
471   (interactive)
472   (forward-line -1)
473   (when (window-live-p notmuch-pick-message-window)
474     (notmuch-pick-show-message)))
475
476 (defun notmuch-pick-prev-matching-message ()
477   "Move to previous matching message."
478   (interactive)
479   (forward-line -1)
480   (while (and (not (bobp)) (not (notmuch-pick-get-match)))
481     (forward-line -1))
482   (when (window-live-p notmuch-pick-message-window)
483     (notmuch-pick-show-message)))
484
485 (defun notmuch-pick-next-matching-message ()
486   "Move to next matching message."
487   (interactive)
488   (forward-line)
489   (while (and (not (eobp)) (not (notmuch-pick-get-match)))
490     (forward-line))
491   (when (window-live-p notmuch-pick-message-window)
492     (notmuch-pick-show-message)))
493
494 (defun notmuch-pick-refresh-view ()
495   "Refresh view."
496   (interactive)
497   (let ((inhibit-read-only t)
498         (basic-query notmuch-pick-basic-query)
499         (query-context notmuch-pick-query-context)
500         (target (notmuch-pick-get-message-id))
501         (buffer-name notmuch-pick-buffer-name))
502     (erase-buffer)
503     (notmuch-pick-worker basic-query
504                          query-context
505                          target
506                          (get-buffer buffer-name))))
507
508 (defmacro with-current-notmuch-pick-message (&rest body)
509   "Evaluate body with current buffer set to the text of current message"
510   `(save-excursion
511      (let ((id (notmuch-pick-get-message-id)))
512        (let ((buf (generate-new-buffer (concat "*notmuch-msg-" id "*"))))
513          (with-current-buffer buf
514             (call-process notmuch-command nil t nil "show" "--format=raw" id)
515            ,@body)
516          (kill-buffer buf)))))
517
518 (defun notmuch-pick-new-mail (&optional prompt-for-sender)
519   "Compose new mail."
520   (interactive "P")
521   (notmuch-pick-close-message-window)
522   (notmuch-mua-new-mail prompt-for-sender ))
523
524 (defun notmuch-pick-forward-message (&optional prompt-for-sender)
525   "Forward the current message."
526   (interactive "P")
527   (notmuch-pick-close-message-window)
528   (with-current-notmuch-pick-message
529    (notmuch-mua-new-forward-message prompt-for-sender)))
530
531 (defun notmuch-pick-reply (&optional prompt-for-sender)
532   "Reply to the sender and all recipients of the current message."
533   (interactive "P")
534   (notmuch-pick-close-message-window)
535   (notmuch-mua-new-reply (notmuch-pick-get-message-id) prompt-for-sender t))
536
537 (defun notmuch-pick-reply-sender (&optional prompt-for-sender)
538   "Reply to the sender of the current message."
539   (interactive "P")
540   (notmuch-pick-close-message-window)
541   (notmuch-mua-new-reply (notmuch-pick-get-message-id) prompt-for-sender nil))
542
543 ;; Shamelessly stolen from notmuch-show.el: maybe should be unified.
544 (defun notmuch-pick-pipe-message (command)
545   "Pipe the contents of the current message to the given command.
546
547 The given command will be executed with the raw contents of the
548 current email message as stdin. Anything printed by the command
549 to stdout or stderr will appear in the *notmuch-pipe* buffer.
550
551 When invoked with a prefix argument, the command will receive all
552 open messages in the current thread (formatted as an mbox) rather
553 than only the current message."
554   (interactive "sPipe message to command: ")
555   (let ((shell-command
556          (concat notmuch-command " show --format=raw "
557                  (shell-quote-argument (notmuch-pick-get-message-id)) " | " command))
558          (buf (get-buffer-create (concat "*notmuch-pipe*"))))
559     (with-current-buffer buf
560       (setq buffer-read-only nil)
561       (erase-buffer)
562       (let ((exit-code (call-process-shell-command shell-command nil buf)))
563         (goto-char (point-max))
564         (set-buffer-modified-p nil)
565         (setq buffer-read-only t)
566         (unless (zerop exit-code)
567           (switch-to-buffer-other-window buf)
568           (message (format "Command '%s' exited abnormally with code %d"
569                            shell-command exit-code)))))))
570
571 (defun notmuch-pick-clean-address (address)
572   "Try to clean a single email ADDRESS for display. Return
573 AUTHOR_NAME if present, otherwise return AUTHOR_EMAIL. Return
574 unchanged ADDRESS if parsing fails."
575   (let* ((clean-address (notmuch-clean-address address))
576          (p-address (car clean-address))
577          (p-name (cdr clean-address)))
578
579     ;; If we have a name return that otherwise return the address.
580     (or p-name p-address)))
581
582 (defun notmuch-pick-insert-field (field format-string msg)
583   (let* ((headers (plist-get msg :headers))
584         (match (plist-get msg :match)))
585     (cond
586      ((string-equal field "date")
587       (let ((face (if match
588                       'notmuch-pick-match-date-face
589                     'notmuch-pick-no-match-date-face)))
590         (insert (propertize (format format-string (plist-get msg :date_relative))
591                             'face face))))
592
593      ((string-equal field "subject")
594       (let ((tree-status (plist-get msg :tree-status))
595             (bare-subject (notmuch-show-strip-re (plist-get headers :Subject)))
596             (face (if match
597                       'notmuch-pick-match-subject-face
598                     'notmuch-pick-no-match-subject-face)))
599         (insert (propertize (format format-string
600                                     (concat
601                                      (mapconcat #'identity (reverse tree-status) "")
602                                      (if (string= notmuch-pick-previous-subject bare-subject)
603                                          " ..."
604                                        bare-subject)))
605                             'face face))
606         (setq notmuch-pick-previous-subject bare-subject)))
607
608      ((string-equal field "authors")
609       (let ((author (notmuch-pick-clean-address (plist-get headers :From)))
610             (len (length (format format-string "")))
611             (face (if match
612                       'notmuch-pick-match-author-face
613                     'notmuch-pick-no-match-author-face)))
614         (when (> (length author) len)
615           (setq author (substring author 0 len)))
616         (insert (propertize (format format-string author)
617                             'face face))))
618
619      ((string-equal field "tags")
620       (let ((tags (plist-get msg :tags))
621             (face (if match
622                           'notmuch-pick-match-tag-face
623                         'notmuch-pick-no-match-tag-face)))
624         (when tags
625           (insert (propertize (format format-string
626                                       (mapconcat #'identity tags ", "))
627                               'face face))))))))
628
629 (defun notmuch-pick-insert-msg (msg)
630   "Insert the message MSG according to notmuch-pick-result-format"
631   (dolist (spec notmuch-pick-result-format)
632     (notmuch-pick-insert-field (car spec) (cdr spec) msg))
633   (notmuch-pick-set-message-properties msg)
634   (insert "\n"))
635
636 (defun notmuch-pick-goto-and-insert-msg (msg)
637   "Insert msg at the end of the buffer. Move point to msg if it is the target"
638   (save-excursion
639     (goto-char (point-max))
640     (notmuch-pick-insert-msg msg))
641   (let ((msg-id (notmuch-id-to-query (plist-get msg :id)))
642         (target notmuch-pick-target-msg))
643     (when (or (and (not target) (plist-get msg :match))
644               (string= msg-id target))
645       (setq notmuch-pick-target-msg "found")
646       (goto-char (point-max))
647       (forward-line -1)
648       (when notmuch-pick-open-target
649         (notmuch-pick-show-message)))))
650
651 (defun notmuch-pick-insert-tree (tree depth tree-status first last)
652   "Insert the message tree TREE at depth DEPTH in the current thread."
653   (let ((msg (car tree))
654         (replies (cadr tree)))
655
656       (cond
657        ((and (< 0 depth) (not last))
658         (push "├" tree-status))
659        ((and (< 0 depth) last)
660         (push "╰" tree-status))
661        ((and (eq 0 depth) first last)
662 ;;        (push "─" tree-status)) choice between this and next line is matter of taste.
663         (push " " tree-status))
664        ((and (eq 0 depth) first (not last))
665           (push "┬" tree-status))
666        ((and (eq 0 depth) (not first) last)
667         (push "╰" tree-status))
668        ((and (eq 0 depth) (not first) (not last))
669         (push "├" tree-status)))
670
671       (push (concat (if replies "┬" "─") "►") tree-status)
672       (notmuch-pick-goto-and-insert-msg (plist-put msg :tree-status tree-status))
673       (pop tree-status)
674       (pop tree-status)
675
676       (if last
677           (push " " tree-status)
678         (push "│" tree-status))
679
680     (notmuch-pick-insert-thread replies (1+ depth) tree-status)))
681
682 (defun notmuch-pick-insert-thread (thread depth tree-status)
683   "Insert the thread THREAD at depth DEPTH >= 1 in the current forest."
684   (let ((n (length thread)))
685     (loop for tree in thread
686           for count from 1 to n
687
688           do (notmuch-pick-insert-tree tree depth tree-status (eq count 1) (eq count n)))))
689
690 (defun notmuch-pick-insert-forest-thread (forest-thread)
691   (let (tree-status)
692     ;; Reset at the start of each main thread.
693     (setq notmuch-pick-previous-subject nil)
694     (notmuch-pick-insert-thread forest-thread 0 tree-status)))
695
696 (defun notmuch-pick-insert-forest (forest)
697   (mapc 'notmuch-pick-insert-forest-thread forest))
698
699 (defun notmuch-pick-mode ()
700   "Major mode displaying messages (as opposed to threads) of of a notmuch search.
701
702 This buffer contains the results of a \"notmuch pick\" of your
703 email archives. Each line in the buffer represents a single
704 message giving the relative date, the author, subject, and any
705 tags.
706
707 Pressing \\[notmuch-pick-show-message] on any line displays that message.
708
709 Complete list of currently available key bindings:
710
711 \\{notmuch-pick-mode-map}"
712
713   (interactive)
714   (kill-all-local-variables)
715   (use-local-map notmuch-pick-mode-map)
716   (setq major-mode 'notmuch-pick-mode
717         mode-name "notmuch-pick")
718   (hl-line-mode 1)
719   (setq buffer-read-only t
720         truncate-lines t))
721
722 (defun notmuch-pick-process-sentinel (proc msg)
723   "Add a message to let user know when \"notmuch pick\" exits"
724   (let ((buffer (process-buffer proc))
725         (status (process-status proc))
726         (exit-status (process-exit-status proc))
727         (never-found-target-thread nil))
728     (when (memq status '(exit signal))
729         (kill-buffer (process-get proc 'parse-buf))
730         (if (buffer-live-p buffer)
731             (with-current-buffer buffer
732               (save-excursion
733                 (let ((inhibit-read-only t)
734                       (atbob (bobp)))
735                   (goto-char (point-max))
736                   (if (eq status 'signal)
737                       (insert "Incomplete search results (pick process was killed).\n"))
738                   (when (eq status 'exit)
739                     (insert "End of search results.")
740                     (unless (= exit-status 0)
741                       (insert (format " (process returned %d)" exit-status)))
742                     (insert "\n")))))))))
743
744
745 (defun notmuch-pick-show-error (string &rest objects)
746   (save-excursion
747     (goto-char (point-max))
748     (insert "Error: Unexpected output from notmuch search:\n")
749     (insert (apply #'format string objects))
750     (insert "\n")))
751
752
753 (defun notmuch-pick-process-filter (proc string)
754   "Process and filter the output of \"notmuch show\" (for pick)"
755   (let ((results-buf (process-buffer proc))
756         (parse-buf (process-get proc 'parse-buf))
757         (inhibit-read-only t)
758         done)
759     (if (not (buffer-live-p results-buf))
760         (delete-process proc)
761       (with-current-buffer parse-buf
762         ;; Insert new data
763         (save-excursion
764           (goto-char (point-max))
765           (insert string))
766         (notmuch-sexp-parse-partial-list 'notmuch-pick-insert-forest-thread
767                                          results-buf)))))
768
769 (defun notmuch-pick-worker (basic-query &optional query-context target buffer open-target)
770   (interactive)
771   (notmuch-pick-mode)
772   (setq notmuch-pick-basic-query basic-query)
773   (setq notmuch-pick-query-context query-context)
774   (setq notmuch-pick-buffer-name (buffer-name buffer))
775   (setq notmuch-pick-target-msg target)
776   (setq notmuch-pick-open-target open-target)
777
778   (erase-buffer)
779   (goto-char (point-min))
780   (let* ((search-args (concat basic-query
781                        (if query-context (concat " and (" query-context ")"))
782                        ))
783          (message-arg "--entire-thread"))
784     (if (equal (car (process-lines notmuch-command "count" search-args)) "0")
785         (setq search-args basic-query))
786     (if notmuch-pick-asynchronous-parser
787         (let ((proc (start-process
788                      "notmuch-pick" buffer
789                      notmuch-command "show" "--body=false" "--format=sexp"
790                      message-arg search-args))
791               ;; Use a scratch buffer to accumulate partial output.
792               ;; This buffer will be killed by the sentinel, which
793               ;; should be called no matter how the process dies.
794               (parse-buf (generate-new-buffer " *notmuch pick parse*")))
795           (process-put proc 'parse-buf parse-buf)
796           (set-process-sentinel proc 'notmuch-pick-process-sentinel)
797           (set-process-filter proc 'notmuch-pick-process-filter)
798           (set-process-query-on-exit-flag proc nil))
799       (progn
800         (notmuch-pick-insert-forest
801          (notmuch-query-get-threads
802           (list "--body=false" message-arg search-args)))
803         (save-excursion
804           (goto-char (point-max))
805           (insert "End of search results.\n"))))))
806
807
808 (defun notmuch-pick (&optional query query-context target buffer-name open-target)
809   "Run notmuch pick with the given `query' and display the results"
810   (interactive "sNotmuch pick: ")
811   (if (null query)
812       (setq query (notmuch-read-query "Notmuch pick: ")))
813   (let ((buffer (get-buffer-create (generate-new-buffer-name
814                                     (or buffer-name
815                                         (concat "*notmuch-pick-" query "*")))))
816         (inhibit-read-only t))
817
818     (switch-to-buffer buffer)
819     ;; Don't track undo information for this buffer
820     (set 'buffer-undo-list t)
821
822     (notmuch-pick-worker query query-context target buffer open-target)
823
824     (setq truncate-lines t)))
825
826
827 ;; Set up key bindings from the rest of notmuch.
828 (define-key 'notmuch-search-mode-map "z" 'notmuch-pick)
829 (define-key 'notmuch-search-mode-map "Z" 'notmuch-pick-from-search-current-query)
830 (define-key 'notmuch-search-mode-map (kbd "M-RET") 'notmuch-pick-from-search-thread)
831 (define-key 'notmuch-hello-mode-map "z" 'notmuch-pick-from-hello)
832 (define-key 'notmuch-show-mode-map "z" 'notmuch-pick)
833 (define-key 'notmuch-show-mode-map "Z" 'notmuch-pick-from-show-current-query)
834 (notmuch-pick-setup-show-out)
835 (message "Initialised notmuch-pick")
836
837 (provide 'notmuch-pick)