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