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