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