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