1 ;; notmuch-pick.el --- displaying notmuch forests.
3 ;; Copyright © Carl Worth
4 ;; Copyright © David Edmondson
5 ;; Copyright © Mark Walters
7 ;; This file is part of Notmuch.
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.
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.
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/>.
22 ;; Authors: David Edmondson <dme@dme.org>
23 ;; Mark Walters <markwalters1009@gmail.com>
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
32 (eval-when-compile (require 'cl))
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" ())
46 ;; the following variable is defined in notmuch.el
47 (defvar notmuch-search-query-string)
49 (defgroup notmuch-pick nil
50 "Showing message and thread structure."
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
56 (defcustom notmuch-pick-show-out nil
57 "View selected messages in new window rather than split-pane."
60 :set (lambda (symbol value)
61 (set-default symbol value)
62 (when (fboundp 'notmuch-pick-setup-show-out)
63 (notmuch-pick-setup-show-out))))
65 (defcustom notmuch-pick-result-format
68 ("subject" . " %-54s ")
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))
80 (defcustom notmuch-pick-asynchronous-parser t
81 "Use the asynchronous parser."
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."
90 :group 'notmuch-faces)
92 (defface notmuch-pick-match-author-face
95 (:foreground "OliveDrab1"))
98 (:foreground "dark blue"))
101 "Face used in pick mode for the date in messages matching the query."
103 :group 'notmuch-faces)
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."
109 :group 'notmuch-faces)
111 (defface notmuch-pick-match-tag-face
114 (:foreground "OliveDrab1"))
117 (:foreground "navy blue" :bold t))
120 "Face used in pick mode for tags in messages matching the query."
122 :group 'notmuch-faces)
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."
129 :group 'notmuch-faces)
131 (defface notmuch-pick-no-match-subject-face
132 '((t (:foreground "gray")))
133 "Face used in pick mode for non-matching subjects."
135 :group 'notmuch-faces)
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."
141 :group 'notmuch-faces)
143 (defface notmuch-pick-no-match-tag-face
144 '((t (:foreground "gray")))
145 "Face used in pick mode face for non-matching tags."
147 :group 'notmuch-faces)
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)
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)
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)
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)
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)
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)
173 (defvar notmuch-pick-message-window nil
174 "The window of the message pane.
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)
182 (defvar notmuch-pick-message-buffer nil
183 "The buffer name of the show buffer in the message pane.
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)
190 (defvar notmuch-pick-mode-map
191 (let ((map (make-sparse-keymap)))
192 (define-key map [mouse-1] 'notmuch-pick-show-message)
193 (define-key map "q" 'notmuch-pick-quit)
194 (define-key map "x" 'notmuch-pick-quit)
195 (define-key map "?" 'notmuch-help)
196 (define-key map "a" 'notmuch-pick-archive-message-then-next)
197 (define-key map "=" 'notmuch-pick-refresh-view)
198 (define-key map "s" 'notmuch-pick-to-search)
199 (define-key map "z" 'notmuch-pick-to-pick)
200 (define-key map "m" 'notmuch-pick-new-mail)
201 (define-key map "f" 'notmuch-pick-forward-message)
202 (define-key map "r" 'notmuch-pick-reply-sender)
203 (define-key map "R" 'notmuch-pick-reply)
204 (define-key map "n" 'notmuch-pick-next-matching-message)
205 (define-key map "p" 'notmuch-pick-prev-matching-message)
206 (define-key map "N" 'notmuch-pick-next-message)
207 (define-key map "P" 'notmuch-pick-prev-message)
208 (define-key map "|" 'notmuch-pick-pipe-message)
209 (define-key map "-" 'notmuch-pick-remove-tag)
210 (define-key map "+" 'notmuch-pick-add-tag)
211 (define-key map " " 'notmuch-pick-scroll-or-next)
212 (define-key map "b" 'notmuch-pick-scroll-message-window-back)
214 (fset 'notmuch-pick-mode-map notmuch-pick-mode-map)
216 (defun notmuch-pick-setup-show-out ()
217 "Set up the keymap for showing a thread
219 This uses the value of the defcustom notmuch-pick-show-out to
220 decide whether to show a message in the message pane or in the
222 (let ((map notmuch-pick-mode-map))
223 (if notmuch-pick-show-out
225 (define-key map (kbd "M-RET") 'notmuch-pick-show-message)
226 (define-key map (kbd "RET") 'notmuch-pick-show-message-out))
228 (define-key map (kbd "RET") 'notmuch-pick-show-message)
229 (define-key map (kbd "M-RET") 'notmuch-pick-show-message-out)))))
231 (defun notmuch-pick-get-message-properties ()
232 "Return the properties of the current message as a plist.
234 Some useful entries are:
235 :headers - Property list containing the headers :Date, :Subject, :From, etc.
236 :tags - Tags for this message"
239 (get-text-property (point) :notmuch-message-properties)))
241 (defun notmuch-pick-set-message-properties (props)
244 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
246 (defun notmuch-pick-set-prop (prop val &optional props)
247 (let ((inhibit-read-only t)
249 (notmuch-pick-get-message-properties))))
250 (plist-put props prop val)
251 (notmuch-pick-set-message-properties props)))
253 (defun notmuch-pick-get-prop (prop &optional props)
254 (let ((props (or props
255 (notmuch-pick-get-message-properties))))
256 (plist-get props prop)))
258 (defun notmuch-pick-set-tags (tags)
259 "Set the tags of the current message."
260 (notmuch-pick-set-prop :tags tags))
262 (defun notmuch-pick-get-tags ()
263 "Return the tags of the current message."
264 (notmuch-pick-get-prop :tags))
266 (defun notmuch-pick-get-message-id ()
267 "Return the message id of the current message."
268 (let ((id (notmuch-pick-get-prop :id)))
270 (notmuch-id-to-query id)
273 (defun notmuch-pick-get-match ()
274 "Return whether the current message is a match."
276 (notmuch-pick-get-prop :match))
278 (defun notmuch-pick-refresh-result ()
279 "Redisplay the current message line.
281 This redisplays the current line based on the messages
282 properties (as they are now). This is used when tags are
284 (let ((init-point (point))
285 (end (line-end-position))
286 (msg (notmuch-pick-get-message-properties))
287 (inhibit-read-only t))
289 ;; This is a little tricky: we override
290 ;; notmuch-pick-previous-subject to get the decision between
291 ;; ... and a subject right and it stops notmuch-pick-insert-msg
292 ;; from overwriting the buffer local copy of
293 ;; notmuch-pick-previous-subject if this is called while the
294 ;; buffer is displaying.
295 (let ((notmuch-pick-previous-subject (notmuch-pick-get-prop :previous-subject)))
296 (delete-region (point) (1+ (line-end-position)))
297 (notmuch-pick-insert-msg msg))
298 (let ((new-end (line-end-position)))
299 (goto-char (if (= init-point end)
301 (min init-point (- new-end 1)))))))
303 (defun notmuch-pick-tag-update-display (&optional tag-changes)
304 "Update display for TAG-CHANGES to current message.
306 Does NOT change the database."
307 (let* ((current-tags (notmuch-pick-get-tags))
308 (new-tags (notmuch-update-tags current-tags tag-changes)))
309 (unless (equal current-tags new-tags)
310 (notmuch-pick-set-tags new-tags)
311 (notmuch-pick-refresh-result))))
313 (defun notmuch-pick-tag (&optional tag-changes)
314 "Change tags for the current message"
316 (setq tag-changes (notmuch-tag (notmuch-pick-get-message-id) tag-changes))
317 (notmuch-pick-tag-update-display tag-changes))
319 (defun notmuch-pick-add-tag ()
320 "Same as `notmuch-pick-tag' but sets initial input to '+'."
322 (notmuch-pick-tag "+"))
324 (defun notmuch-pick-remove-tag ()
325 "Same as `notmuch-pick-tag' but sets initial input to '-'."
327 (notmuch-pick-tag "-"))
329 ;; The next two functions close the message window before searching or
330 ;; picking but they do so after the user has entered the query (in
331 ;; case the user was basing the query on something in the message
334 (defun notmuch-pick-to-search ()
335 "Run \"notmuch search\" with the given `query' and display results."
337 (let ((query (notmuch-read-query "Notmuch search: ")))
338 (notmuch-pick-close-message-window)
339 (notmuch-search query)))
341 (defun notmuch-pick-to-pick ()
342 "Run a query and display results in experimental notmuch-pick mode"
344 (let ((query (notmuch-read-query "Notmuch pick: ")))
345 (notmuch-pick-close-message-window)
346 (notmuch-pick query)))
348 ;; This function should be in notmuch-hello.el but we are trying to
349 ;; minimise impact on the rest of the codebase.
350 (defun notmuch-pick-from-hello (&optional search)
351 "Run a query and display results in experimental notmuch-pick mode"
353 (unless (null search)
354 (setq search (notmuch-hello-trim search))
355 (let ((history-delete-duplicates t))
356 (add-to-history 'notmuch-search-history search)))
357 (notmuch-pick search))
359 ;; This function should be in notmuch-show.el but be we trying to
360 ;; minimise impact on the rest of the codebase.
361 (defun notmuch-pick-from-show-current-query ()
362 "Call notmuch pick with the current query"
364 (notmuch-pick notmuch-show-thread-id
365 notmuch-show-query-context
366 (notmuch-show-get-message-id)))
368 ;; This function should be in notmuch.el but be we trying to minimise
369 ;; impact on the rest of the codebase.
370 (defun notmuch-pick-from-search-current-query ()
371 "Call notmuch pick with the current query"
373 (notmuch-pick notmuch-search-query-string))
375 ;; This function should be in notmuch.el but be we trying to minimise
376 ;; impact on the rest of the codebase.
377 (defun notmuch-pick-from-search-thread ()
378 "Show the selected thread with notmuch-pick"
380 (notmuch-pick (notmuch-search-find-thread-id)
381 notmuch-search-query-string
383 (notmuch-prettify-subject (notmuch-search-find-subject))
386 (defun notmuch-pick-message-window-kill-hook ()
387 "Close the message pane when exiting the show buffer."
388 (let ((buffer (current-buffer)))
389 (when (and (window-live-p notmuch-pick-message-window)
390 (eq (window-buffer notmuch-pick-message-window) buffer))
391 ;; We do not want an error if this is the sole window in the
392 ;; frame and I do not know how to test for that in emacs pre
393 ;; 24. Hence we just ignore-errors.
395 (delete-window notmuch-pick-message-window)))))
397 (defun notmuch-pick-show-message ()
398 "Show the current message (in split-pane)."
400 (let ((id (notmuch-pick-get-message-id))
401 (inhibit-read-only t)
404 ;; We close and reopen the window to kill off un-needed buffers
405 ;; this might cause flickering but seems ok.
406 (notmuch-pick-close-message-window)
407 (setq notmuch-pick-message-window
408 (split-window-vertically (/ (window-height) 4)))
409 (with-selected-window notmuch-pick-message-window
410 ;; Since we are only displaying one message do not indent.
411 (let ((notmuch-show-indent-messages-width 0)
412 (notmuch-show-only-matching-messages t))
413 (setq buffer (notmuch-show id nil nil nil))))
414 ;; We need the `let' as notmuch-pick-message-window is buffer local.
415 (let ((window notmuch-pick-message-window))
416 (with-current-buffer buffer
417 (setq notmuch-pick-message-window window)
418 (add-hook 'kill-buffer-hook 'notmuch-pick-message-window-kill-hook)))
419 (when notmuch-show-mark-read-tags
420 (notmuch-pick-tag-update-display notmuch-show-mark-read-tags))
421 (setq notmuch-pick-message-buffer buffer))))
423 (defun notmuch-pick-show-message-out ()
424 "Show the current message (in whole window)."
426 (let ((id (notmuch-pick-get-message-id))
427 (inhibit-read-only t)
430 ;; We close the window to kill off un-needed buffers.
431 (notmuch-pick-close-message-window)
432 (notmuch-show id nil nil nil))))
434 (defun notmuch-pick-scroll-message-window ()
435 "Scroll the message window (if it exists)"
437 (when (window-live-p notmuch-pick-message-window)
438 (with-selected-window notmuch-pick-message-window
439 (if (pos-visible-in-window-p (point-max))
443 (defun notmuch-pick-scroll-message-window-back ()
444 "Scroll the message window back(if it exists)"
446 (when (window-live-p notmuch-pick-message-window)
447 (with-selected-window notmuch-pick-message-window
448 (if (pos-visible-in-window-p (point-min))
452 (defun notmuch-pick-scroll-or-next ()
453 "Scroll the message window. If it at end go to next message."
455 (when (notmuch-pick-scroll-message-window)
456 (notmuch-pick-next-matching-message)))
458 (defun notmuch-pick-quit ()
459 "Close the split view or exit pick."
461 (unless (notmuch-pick-close-message-window)
462 (kill-buffer (current-buffer))))
464 (defun notmuch-pick-close-message-window ()
465 "Close the message-window. Return t if close succeeds."
467 (when (and (window-live-p notmuch-pick-message-window)
468 (eq (window-buffer notmuch-pick-message-window) notmuch-pick-message-buffer))
469 (delete-window notmuch-pick-message-window)
470 (unless (get-buffer-window-list notmuch-pick-message-buffer)
471 (kill-buffer notmuch-pick-message-buffer))
474 (defun notmuch-pick-archive-message (&optional unarchive)
475 "Archive the current message.
477 Archive the current message by applying the tag changes in
478 `notmuch-archive-tags' to it. If a prefix argument is given, the
479 message will be \"unarchived\", i.e. the tag changes in
480 `notmuch-archive-tags' will be reversed."
482 (when notmuch-archive-tags
483 (apply 'notmuch-pick-tag
484 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
486 (defun notmuch-pick-archive-message-then-next (&optional unarchive)
487 "Archive the current message and move to next matching message."
489 (notmuch-pick-archive-message unarchive)
490 (notmuch-pick-next-matching-message))
492 (defun notmuch-pick-next-message ()
493 "Move to next message."
496 (when (window-live-p notmuch-pick-message-window)
497 (notmuch-pick-show-message)))
499 (defun notmuch-pick-prev-message ()
500 "Move to previous message."
503 (when (window-live-p notmuch-pick-message-window)
504 (notmuch-pick-show-message)))
506 (defun notmuch-pick-prev-matching-message ()
507 "Move to previous matching message."
510 (while (and (not (bobp)) (not (notmuch-pick-get-match)))
512 (when (window-live-p notmuch-pick-message-window)
513 (notmuch-pick-show-message)))
515 (defun notmuch-pick-next-matching-message ()
516 "Move to next matching message."
519 (while (and (not (eobp)) (not (notmuch-pick-get-match)))
521 (when (window-live-p notmuch-pick-message-window)
522 (notmuch-pick-show-message)))
524 (defun notmuch-pick-refresh-view ()
527 (let ((inhibit-read-only t)
528 (basic-query notmuch-pick-basic-query)
529 (query-context notmuch-pick-query-context)
530 (target (notmuch-pick-get-message-id))
531 (buffer-name notmuch-pick-buffer-name))
533 (notmuch-pick-worker basic-query
536 (get-buffer buffer-name))))
538 (defmacro with-current-notmuch-pick-message (&rest body)
539 "Evaluate body with current buffer set to the text of current message"
541 (let ((id (notmuch-pick-get-message-id)))
542 (let ((buf (generate-new-buffer (concat "*notmuch-msg-" id "*"))))
543 (with-current-buffer buf
544 (call-process notmuch-command nil t nil "show" "--format=raw" id)
546 (kill-buffer buf)))))
548 (defun notmuch-pick-new-mail (&optional prompt-for-sender)
551 (notmuch-pick-close-message-window)
552 (notmuch-mua-new-mail prompt-for-sender ))
554 (defun notmuch-pick-forward-message (&optional prompt-for-sender)
555 "Forward the current message."
557 (notmuch-pick-close-message-window)
558 (with-current-notmuch-pick-message
559 (notmuch-mua-new-forward-message prompt-for-sender)))
561 (defun notmuch-pick-reply (&optional prompt-for-sender)
562 "Reply to the sender and all recipients of the current message."
564 (notmuch-pick-close-message-window)
565 (notmuch-mua-new-reply (notmuch-pick-get-message-id) prompt-for-sender t))
567 (defun notmuch-pick-reply-sender (&optional prompt-for-sender)
568 "Reply to the sender of the current message."
570 (notmuch-pick-close-message-window)
571 (notmuch-mua-new-reply (notmuch-pick-get-message-id) prompt-for-sender nil))
573 ;; Shamelessly stolen from notmuch-show.el: maybe should be unified.
574 (defun notmuch-pick-pipe-message (command)
575 "Pipe the contents of the current message to the given command.
577 The given command will be executed with the raw contents of the
578 current email message as stdin. Anything printed by the command
579 to stdout or stderr will appear in the *notmuch-pipe* buffer.
581 When invoked with a prefix argument, the command will receive all
582 open messages in the current thread (formatted as an mbox) rather
583 than only the current message."
584 (interactive "sPipe message to command: ")
586 (concat notmuch-command " show --format=raw "
587 (shell-quote-argument (notmuch-pick-get-message-id)) " | " command))
588 (buf (get-buffer-create (concat "*notmuch-pipe*"))))
589 (with-current-buffer buf
590 (setq buffer-read-only nil)
592 (let ((exit-code (call-process-shell-command shell-command nil buf)))
593 (goto-char (point-max))
594 (set-buffer-modified-p nil)
595 (setq buffer-read-only t)
596 (unless (zerop exit-code)
597 (switch-to-buffer-other-window buf)
598 (message (format "Command '%s' exited abnormally with code %d"
599 shell-command exit-code)))))))
601 (defun notmuch-pick-clean-address (address)
602 "Try to clean a single email ADDRESS for display. Return
603 AUTHOR_NAME if present, otherwise return AUTHOR_EMAIL. Return
604 unchanged ADDRESS if parsing fails."
605 (let* ((clean-address (notmuch-clean-address address))
606 (p-address (car clean-address))
607 (p-name (cdr clean-address)))
609 ;; If we have a name return that otherwise return the address.
610 (or p-name p-address)))
612 (defun notmuch-pick-insert-field (field format-string msg)
613 (let* ((headers (plist-get msg :headers))
614 (match (plist-get msg :match)))
616 ((string-equal field "date")
617 (let ((face (if match
618 'notmuch-pick-match-date-face
619 'notmuch-pick-no-match-date-face)))
620 (insert (propertize (format format-string (plist-get msg :date_relative))
623 ((string-equal field "subject")
624 (let ((tree-status (plist-get msg :tree-status))
625 (bare-subject (notmuch-show-strip-re (plist-get headers :Subject)))
627 'notmuch-pick-match-subject-face
628 'notmuch-pick-no-match-subject-face)))
629 (insert (propertize (format format-string
631 (mapconcat #'identity (reverse tree-status) "")
632 (if (string= notmuch-pick-previous-subject bare-subject)
636 (setq notmuch-pick-previous-subject bare-subject)))
638 ((string-equal field "authors")
639 (let ((author (notmuch-pick-clean-address (plist-get headers :From)))
640 (len (length (format format-string "")))
642 'notmuch-pick-match-author-face
643 'notmuch-pick-no-match-author-face)))
644 (when (> (length author) len)
645 (setq author (substring author 0 len)))
646 (insert (propertize (format format-string author)
649 ((string-equal field "tags")
650 (let ((tags (plist-get msg :tags))
652 'notmuch-pick-match-tag-face
653 'notmuch-pick-no-match-tag-face)))
655 (insert (propertize (format format-string
656 (mapconcat #'identity tags ", "))
659 (defun notmuch-pick-insert-msg (msg)
660 "Insert the message MSG according to notmuch-pick-result-format"
661 ;; We need to save the previous subject as it will get overwritten
662 ;; by the insert-field calls.
663 (let ((previous-subject notmuch-pick-previous-subject))
664 (dolist (spec notmuch-pick-result-format)
665 (notmuch-pick-insert-field (car spec) (cdr spec) msg))
666 (notmuch-pick-set-message-properties msg)
667 (notmuch-pick-set-prop :previous-subject previous-subject)
670 (defun notmuch-pick-goto-and-insert-msg (msg)
671 "Insert msg at the end of the buffer. Move point to msg if it is the target"
673 (goto-char (point-max))
674 (notmuch-pick-insert-msg msg))
675 (let ((msg-id (notmuch-id-to-query (plist-get msg :id)))
676 (target notmuch-pick-target-msg))
677 (when (or (and (not target) (plist-get msg :match))
678 (string= msg-id target))
679 (setq notmuch-pick-target-msg "found")
680 (goto-char (point-max))
682 (when notmuch-pick-open-target
683 (notmuch-pick-show-message)))))
685 (defun notmuch-pick-insert-tree (tree depth tree-status first last)
686 "Insert the message tree TREE at depth DEPTH in the current thread.
688 A message tree is another name for a single sub-thread: i.e., a
689 message together with all its descendents."
690 (let ((msg (car tree))
691 (replies (cadr tree)))
694 ((and (< 0 depth) (not last))
695 (push "├" tree-status))
696 ((and (< 0 depth) last)
697 (push "╰" tree-status))
698 ((and (eq 0 depth) first last)
699 ;; (push "─" tree-status)) choice between this and next line is matter of taste.
700 (push " " tree-status))
701 ((and (eq 0 depth) first (not last))
702 (push "┬" tree-status))
703 ((and (eq 0 depth) (not first) last)
704 (push "╰" tree-status))
705 ((and (eq 0 depth) (not first) (not last))
706 (push "├" tree-status)))
708 (push (concat (if replies "┬" "─") "►") tree-status)
709 (notmuch-pick-goto-and-insert-msg (plist-put msg :tree-status tree-status))
714 (push " " tree-status)
715 (push "│" tree-status))
717 (notmuch-pick-insert-thread replies (1+ depth) tree-status)))
719 (defun notmuch-pick-insert-thread (thread depth tree-status)
720 "Insert the collection of sibling sub-threads THREAD at depth DEPTH in the current forest."
721 (let ((n (length thread)))
722 (loop for tree in thread
723 for count from 1 to n
725 do (notmuch-pick-insert-tree tree depth tree-status (eq count 1) (eq count n)))))
727 (defun notmuch-pick-insert-forest-thread (forest-thread)
728 "Insert a single complete thread."
730 ;; Reset at the start of each main thread.
731 (setq notmuch-pick-previous-subject nil)
732 (notmuch-pick-insert-thread forest-thread 0 tree-status)))
734 (defun notmuch-pick-insert-forest (forest)
735 "Insert a forest of threads.
737 This function inserts a collection of several complete threads as
738 passed to it by notmuch-pick-process-filter."
739 (mapc 'notmuch-pick-insert-forest-thread forest))
741 (defun notmuch-pick-mode ()
742 "Major mode displaying messages (as opposed to threads) of of a notmuch search.
744 This buffer contains the results of a \"notmuch pick\" of your
745 email archives. Each line in the buffer represents a single
746 message giving the relative date, the author, subject, and any
749 Pressing \\[notmuch-pick-show-message] on any line displays that message.
751 Complete list of currently available key bindings:
753 \\{notmuch-pick-mode-map}"
756 (kill-all-local-variables)
757 (use-local-map notmuch-pick-mode-map)
758 (setq major-mode 'notmuch-pick-mode
759 mode-name "notmuch-pick")
761 (setq buffer-read-only t
764 (defun notmuch-pick-process-sentinel (proc msg)
765 "Add a message to let user know when \"notmuch pick\" exits"
766 (let ((buffer (process-buffer proc))
767 (status (process-status proc))
768 (exit-status (process-exit-status proc))
769 (never-found-target-thread nil))
770 (when (memq status '(exit signal))
771 (kill-buffer (process-get proc 'parse-buf))
772 (if (buffer-live-p buffer)
773 (with-current-buffer buffer
775 (let ((inhibit-read-only t)
777 (goto-char (point-max))
778 (if (eq status 'signal)
779 (insert "Incomplete search results (pick process was killed).\n"))
780 (when (eq status 'exit)
781 (insert "End of search results.")
782 (unless (= exit-status 0)
783 (insert (format " (process returned %d)" exit-status)))
784 (insert "\n")))))))))
786 (defun notmuch-pick-process-filter (proc string)
787 "Process and filter the output of \"notmuch show\" (for pick)"
788 (let ((results-buf (process-buffer proc))
789 (parse-buf (process-get proc 'parse-buf))
790 (inhibit-read-only t)
792 (if (not (buffer-live-p results-buf))
793 (delete-process proc)
794 (with-current-buffer parse-buf
797 (goto-char (point-max))
799 (notmuch-sexp-parse-partial-list 'notmuch-pick-insert-forest-thread
802 (defun notmuch-pick-worker (basic-query &optional query-context target buffer open-target)
805 (setq notmuch-pick-basic-query basic-query)
806 (setq notmuch-pick-query-context query-context)
807 (setq notmuch-pick-buffer-name (buffer-name buffer))
808 (setq notmuch-pick-target-msg target)
809 (setq notmuch-pick-open-target open-target)
812 (goto-char (point-min))
813 (let* ((search-args (concat basic-query
814 (if query-context (concat " and (" query-context ")"))
816 (message-arg "--entire-thread"))
817 (if (equal (car (process-lines notmuch-command "count" search-args)) "0")
818 (setq search-args basic-query))
819 (if notmuch-pick-asynchronous-parser
820 (let ((proc (notmuch-start-notmuch
821 "notmuch-pick" buffer #'notmuch-pick-process-sentinel
822 "show" "--body=false" "--format=sexp"
823 message-arg search-args))
824 ;; Use a scratch buffer to accumulate partial output.
825 ;; This buffer will be killed by the sentinel, which
826 ;; should be called no matter how the process dies.
827 (parse-buf (generate-new-buffer " *notmuch pick parse*")))
828 (process-put proc 'parse-buf parse-buf)
829 (set-process-filter proc 'notmuch-pick-process-filter)
830 (set-process-query-on-exit-flag proc nil))
832 (notmuch-pick-insert-forest
833 (notmuch-query-get-threads
834 (list "--body=false" message-arg search-args)))
836 (goto-char (point-max))
837 (insert "End of search results.\n"))))))
840 (defun notmuch-pick (&optional query query-context target buffer-name open-target)
841 "Run notmuch pick with the given `query' and display the results.
844 QUERY: the main query. This can be any query but in many cases will be
845 a single thread. If nil this is read interactively from the minibuffer.
846 QUERY-CONTEXT: is an additional term for the query. The query used
847 is QUERY and QUERY-CONTEXT unless that does not match any messages
848 in which case we fall back to just QUERY.
849 TARGET: A message ID (with the id: prefix) that will be made
850 current if it appears in the pick results.
851 BUFFER-NAME: the name of the buffer to show the pick tree. If
852 it is nil \"*notmuch-pick\" followed by QUERY is used.
853 OPEN-TARGET: If TRUE open the target message in the message pane."
854 (interactive "sNotmuch pick: ")
856 (setq query (notmuch-read-query "Notmuch pick: ")))
857 (let ((buffer (get-buffer-create (generate-new-buffer-name
859 (concat "*notmuch-pick-" query "*")))))
860 (inhibit-read-only t))
862 (switch-to-buffer buffer)
863 ;; Don't track undo information for this buffer
864 (set 'buffer-undo-list t)
866 (notmuch-pick-worker query query-context target buffer open-target)
868 (setq truncate-lines t)))
871 ;; Set up key bindings from the rest of notmuch.
872 (define-key 'notmuch-search-mode-map "z" 'notmuch-pick)
873 (define-key 'notmuch-search-mode-map "Z" 'notmuch-pick-from-search-current-query)
874 (define-key 'notmuch-search-mode-map (kbd "M-RET") 'notmuch-pick-from-search-thread)
875 (define-key 'notmuch-hello-mode-map "z" 'notmuch-pick-from-hello)
876 (define-key 'notmuch-show-mode-map "z" 'notmuch-pick)
877 (define-key 'notmuch-show-mode-map "Z" 'notmuch-pick-from-show-current-query)
878 (notmuch-pick-setup-show-out)
879 (message "Initialised notmuch-pick")
881 (provide 'notmuch-pick)