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