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