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