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