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