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 (make-variable-buffer-local 'notmuch-pick-previous-subject)
152 ;; The basic query i.e. the key part of the search request.
153 (defvar notmuch-pick-basic-query nil)
154 (make-variable-buffer-local 'notmuch-pick-basic-query)
155 ;; The context of the search: i.e., useful but can be dropped.
156 (defvar notmuch-pick-query-context nil)
157 (make-variable-buffer-local 'notmuch-pick-query-context)
158 (defvar notmuch-pick-buffer-name nil)
159 (make-variable-buffer-local 'notmuch-pick-buffer-name)
160 (defvar notmuch-pick-message-window nil)
161 (make-variable-buffer-local 'notmuch-pick-message-window)
162 (put 'notmuch-pick-message-window 'permanent-local t)
163 (defvar notmuch-pick-message-buffer nil)
164 (make-variable-buffer-local 'notmuch-pick-message-buffer-name)
165 (put 'notmuch-pick-message-buffer-name 'permanent-local t)
166 (defvar notmuch-pick-process-state nil
167 "Parsing state of the search process filter.")
170 (defvar notmuch-pick-mode-map
171 (let ((map (make-sparse-keymap)))
172 (define-key map [mouse-1] 'notmuch-pick-show-message)
173 (define-key map "q" 'notmuch-pick-quit)
174 (define-key map "x" 'notmuch-pick-quit)
175 (define-key map "?" 'notmuch-help)
176 (define-key map "a" 'notmuch-pick-archive-message-then-next)
177 (define-key map "=" 'notmuch-pick-refresh-view)
178 (define-key map "s" 'notmuch-search)
179 (define-key map "z" 'notmuch-pick)
180 (define-key map "m" 'notmuch-pick-new-mail)
181 (define-key map "f" 'notmuch-pick-forward-message)
182 (define-key map "r" 'notmuch-pick-reply-sender)
183 (define-key map "R" 'notmuch-pick-reply)
184 (define-key map "n" 'notmuch-pick-next-matching-message)
185 (define-key map "p" 'notmuch-pick-prev-matching-message)
186 (define-key map "N" 'notmuch-pick-next-message)
187 (define-key map "P" 'notmuch-pick-prev-message)
188 (define-key map "|" 'notmuch-pick-pipe-message)
189 (define-key map "-" 'notmuch-pick-remove-tag)
190 (define-key map "+" 'notmuch-pick-add-tag)
191 (define-key map " " 'notmuch-pick-scroll-or-next)
192 (define-key map "b" 'notmuch-pick-scroll-message-window-back)
194 (fset 'notmuch-pick-mode-map notmuch-pick-mode-map)
196 (defun notmuch-pick-setup-show-out ()
197 (let ((map notmuch-pick-mode-map))
198 (if notmuch-pick-show-out
200 (define-key map (kbd "M-RET") 'notmuch-pick-show-message)
201 (define-key map (kbd "RET") 'notmuch-pick-show-message-out))
203 (define-key map (kbd "RET") 'notmuch-pick-show-message)
204 (define-key map (kbd "M-RET") 'notmuch-pick-show-message-out)))))
206 (defun notmuch-pick-get-message-properties ()
207 "Return the properties of the current message as a plist.
209 Some useful entries are:
210 :headers - Property list containing the headers :Date, :Subject, :From, etc.
211 :tags - Tags for this message"
214 (get-text-property (point) :notmuch-message-properties)))
216 (defun notmuch-pick-set-message-properties (props)
219 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
221 (defun notmuch-pick-set-prop (prop val &optional props)
222 (let ((inhibit-read-only t)
224 (notmuch-pick-get-message-properties))))
225 (plist-put props prop val)
226 (notmuch-pick-set-message-properties props)))
228 (defun notmuch-pick-get-prop (prop &optional props)
229 (let ((props (or props
230 (notmuch-pick-get-message-properties))))
231 (plist-get props prop)))
233 (defun notmuch-pick-set-tags (tags)
234 "Set the tags of the current message."
235 (notmuch-pick-set-prop :tags tags))
237 (defun notmuch-pick-get-tags ()
238 "Return the tags of the current message."
239 (notmuch-pick-get-prop :tags))
241 (defun notmuch-pick-get-message-id ()
242 "Return the message id of the current message."
243 (let ((id (notmuch-pick-get-prop :id)))
245 (notmuch-id-to-query id)
248 (defun notmuch-pick-get-match ()
249 "Return whether the current message is a match."
251 (notmuch-pick-get-prop :match))
253 (defun notmuch-pick-refresh-result ()
254 (let ((init-point (point))
255 (end (line-end-position))
256 (msg (notmuch-pick-get-message-properties))
257 (inhibit-read-only t))
259 (delete-region (point) (1+ (line-end-position)))
260 (notmuch-pick-insert-msg msg)
261 (let ((new-end (line-end-position)))
262 (goto-char (if (= init-point end)
264 (min init-point (- new-end 1)))))))
266 (defun notmuch-pick-tag-update-display (&optional tag-changes)
267 "Update display for TAG-CHANGES to current message.
269 Does NOT change the database."
270 (let* ((current-tags (notmuch-pick-get-tags))
271 (new-tags (notmuch-update-tags current-tags tag-changes)))
272 (unless (equal current-tags new-tags)
273 (notmuch-pick-set-tags new-tags)
274 (notmuch-pick-refresh-result))))
276 (defun notmuch-pick-tag (&optional tag-changes)
277 "Change tags for the current message"
279 (setq tag-changes (funcall 'notmuch-tag (notmuch-pick-get-message-id) tag-changes))
280 (notmuch-pick-tag-update-display tag-changes))
282 (defun notmuch-pick-add-tag ()
283 "Same as `notmuch-pick-tag' but sets initial input to '+'."
285 (notmuch-pick-tag "+"))
287 (defun notmuch-pick-remove-tag ()
288 "Same as `notmuch-pick-tag' but sets initial input to '-'."
290 (notmuch-pick-tag "-"))
292 ;; This function should be in notmuch-hello.el but we are trying to
293 ;; minimise impact on the rest of the codebase.
294 (defun notmuch-pick-from-hello (&optional search)
295 "Run a query and display results in experimental notmuch-pick mode"
297 (unless (null search)
298 (setq search (notmuch-hello-trim search))
299 (let ((history-delete-duplicates t))
300 (add-to-history 'notmuch-search-history search)))
301 (notmuch-pick search))
303 ;; This function should be in notmuch-show.el but be we trying to
304 ;; minimise impact on the rest of the codebase.
305 (defun notmuch-pick-from-show-current-query ()
306 "Call notmuch pick with the current query"
308 (notmuch-pick notmuch-show-thread-id notmuch-show-query-context))
310 ;; This function should be in notmuch.el but be we trying to minimise
311 ;; impact on the rest of the codebase.
312 (defun notmuch-pick-from-search-current-query ()
313 "Call notmuch pick with the current query"
315 (notmuch-pick notmuch-search-query-string))
317 ;; This function should be in notmuch.el but be we trying to minimise
318 ;; impact on the rest of the codebase.
319 (defun notmuch-pick-from-search-thread ()
320 "Show the selected thread with notmuch-pick"
322 (notmuch-pick (notmuch-search-find-thread-id)
323 notmuch-search-query-string
324 (notmuch-prettify-subject (notmuch-search-find-subject)))
325 (notmuch-pick-show-match-message-with-wait))
327 (defun notmuch-pick-show-message ()
328 "Show the current message (in split-pane)."
330 (let ((id (notmuch-pick-get-message-id))
331 (inhibit-read-only t)
334 ;; We close and reopen the window to kill off un-needed buffers
335 ;; this might cause flickering but seems ok.
336 (notmuch-pick-close-message-window)
337 (setq notmuch-pick-message-window
338 (split-window-vertically (/ (window-height) 4)))
339 (with-selected-window notmuch-pick-message-window
340 (setq current-prefix-arg '(4))
341 (setq buffer (notmuch-show id nil nil nil)))
342 (notmuch-pick-tag-update-display (list "-unread"))
343 (setq notmuch-pick-message-buffer buffer))))
345 (defun notmuch-pick-show-message-out ()
346 "Show the current message (in whole window)."
348 (let ((id (notmuch-pick-get-message-id))
349 (inhibit-read-only t)
352 ;; We close the window to kill off un-needed buffers.
353 (notmuch-pick-close-message-window)
354 (notmuch-show id nil nil nil))))
356 (defun notmuch-pick-scroll-message-window ()
357 "Scroll the message window (if it exists)"
359 (when (window-live-p notmuch-pick-message-window)
360 (with-selected-window notmuch-pick-message-window
361 (if (pos-visible-in-window-p (point-max))
365 (defun notmuch-pick-scroll-message-window-back ()
366 "Scroll the message window back(if it exists)"
368 (when (window-live-p notmuch-pick-message-window)
369 (with-selected-window notmuch-pick-message-window
370 (if (pos-visible-in-window-p (point-min))
374 (defun notmuch-pick-scroll-or-next ()
375 "Scroll the message window. If it at end go to next message."
377 (when (notmuch-pick-scroll-message-window)
378 (notmuch-pick-next-matching-message)))
380 (defun notmuch-pick-quit ()
381 "Close the split view or exit pick."
383 (unless (notmuch-pick-close-message-window)
384 (kill-buffer (current-buffer))))
386 (defun notmuch-pick-close-message-window ()
387 "Close the message-window. Return t if close succeeds."
389 (when (and (window-live-p notmuch-pick-message-window)
390 (eq (window-buffer notmuch-pick-message-window) notmuch-pick-message-buffer))
391 (delete-window notmuch-pick-message-window)
392 (unless (get-buffer-window-list notmuch-pick-message-buffer)
393 (kill-buffer notmuch-pick-message-buffer))
396 (defun notmuch-pick-archive-message (&optional unarchive)
397 "Archive the current message.
399 Archive the current message by applying the tag changes in
400 `notmuch-archive-tags' to it (remove the \"inbox\" tag by
401 default). If a prefix argument is given, the message will be
402 \"unarchived\", i.e. the tag changes in `notmuch-archive-tags'
405 (when notmuch-archive-tags
406 (apply 'notmuch-pick-tag
407 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
409 (defun notmuch-pick-archive-message-then-next (&optional unarchive)
410 "Archive the current message and move to next matching message."
412 (notmuch-pick-archive-message unarchive)
413 (notmuch-pick-next-matching-message))
415 (defun notmuch-pick-next-message ()
416 "Move to next message."
419 (when (window-live-p notmuch-pick-message-window)
420 (notmuch-pick-show-message)))
422 (defun notmuch-pick-prev-message ()
423 "Move to previous message."
426 (when (window-live-p notmuch-pick-message-window)
427 (notmuch-pick-show-message)))
429 (defun notmuch-pick-prev-matching-message ()
430 "Move to previous matching message."
433 (while (and (not (bobp)) (not (notmuch-pick-get-match)))
435 (when (window-live-p notmuch-pick-message-window)
436 (notmuch-pick-show-message)))
438 (defun notmuch-pick-next-matching-message ()
439 "Move to next matching message."
442 (while (and (not (eobp)) (not (notmuch-pick-get-match)))
444 (when (window-live-p notmuch-pick-message-window)
445 (notmuch-pick-show-message)))
447 (defun notmuch-pick-show-match-message-with-wait ()
448 "Show the first matching message but wait for it to appear or search to finish."
450 (unless (notmuch-pick-get-match)
451 (notmuch-pick-next-matching-message))
452 (while (and (not (notmuch-pick-get-match))
453 (get-buffer-process (current-buffer)))
454 (message "waiting for message")
456 (goto-char (point-min))
457 (unless (notmuch-pick-get-match)
458 (notmuch-pick-next-matching-message)))
460 (when (notmuch-pick-get-match)
461 (notmuch-pick-show-message)))
463 (defun notmuch-pick-refresh-view ()
466 (let ((inhibit-read-only t)
467 (basic-query notmuch-pick-basic-query)
468 (query-context notmuch-pick-query-context)
469 (buffer-name notmuch-pick-buffer-name))
471 (notmuch-pick-worker basic-query query-context (get-buffer buffer-name))))
473 (defmacro with-current-notmuch-pick-message (&rest body)
474 "Evaluate body with current buffer set to the text of current message"
476 (let ((id (notmuch-pick-get-message-id)))
477 (let ((buf (generate-new-buffer (concat "*notmuch-msg-" id "*"))))
478 (with-current-buffer buf
479 (call-process notmuch-command nil t nil "show" "--format=raw" id)
481 (kill-buffer buf)))))
483 (defun notmuch-pick-new-mail (&optional prompt-for-sender)
486 (notmuch-pick-close-message-window)
487 (notmuch-mua-new-mail prompt-for-sender ))
489 (defun notmuch-pick-forward-message (&optional prompt-for-sender)
490 "Forward the current message."
492 (notmuch-pick-close-message-window)
493 (with-current-notmuch-pick-message
494 (notmuch-mua-new-forward-message prompt-for-sender)))
496 (defun notmuch-pick-reply (&optional prompt-for-sender)
497 "Reply to the sender and all recipients of the current message."
499 (notmuch-pick-close-message-window)
500 (notmuch-mua-new-reply (notmuch-pick-get-message-id) prompt-for-sender t))
502 (defun notmuch-pick-reply-sender (&optional prompt-for-sender)
503 "Reply to the sender of the current message."
505 (notmuch-pick-close-message-window)
506 (notmuch-mua-new-reply (notmuch-pick-get-message-id) prompt-for-sender nil))
508 ;; Shamelessly stolen from notmuch-show.el: maybe should be unified.
509 (defun notmuch-pick-pipe-message (command)
510 "Pipe the contents of the current message to the given command.
512 The given command will be executed with the raw contents of the
513 current email message as stdin. Anything printed by the command
514 to stdout or stderr will appear in the *notmuch-pipe* buffer.
516 When invoked with a prefix argument, the command will receive all
517 open messages in the current thread (formatted as an mbox) rather
518 than only the current message."
519 (interactive "sPipe message to command: ")
521 (concat notmuch-command " show --format=raw "
522 (shell-quote-argument (notmuch-pick-get-message-id)) " | " command))
523 (buf (get-buffer-create (concat "*notmuch-pipe*"))))
524 (with-current-buffer buf
525 (setq buffer-read-only nil)
527 (let ((exit-code (call-process-shell-command shell-command nil buf)))
528 (goto-char (point-max))
529 (set-buffer-modified-p nil)
530 (setq buffer-read-only t)
531 (unless (zerop exit-code)
532 (switch-to-buffer-other-window buf)
533 (message (format "Command '%s' exited abnormally with code %d"
534 shell-command exit-code)))))))
536 (defun notmuch-pick-clean-address (address)
537 "Try to clean a single email ADDRESS for display. Return
538 AUTHOR_NAME if present, otherwise return AUTHOR_EMAIL. Return
539 unchanged ADDRESS if parsing fails."
540 (let* ((clean-address (notmuch-clean-address address))
541 (p-address (car clean-address))
542 (p-name (cdr clean-address)))
544 ;; If we have a name return that otherwise return the address.
545 (or p-name p-address)))
547 (defun notmuch-pick-insert-field (field format-string msg)
548 (let* ((headers (plist-get msg :headers))
549 (match (plist-get msg :match)))
551 ((string-equal field "date")
552 (let ((face (if match
553 'notmuch-pick-match-date-face
554 'notmuch-pick-no-match-date-face)))
555 (insert (propertize (format format-string (plist-get msg :date_relative))
558 ((string-equal field "subject")
559 (let ((tree-status (plist-get msg :tree-status))
560 (bare-subject (notmuch-show-strip-re (plist-get headers :Subject)))
562 'notmuch-pick-match-subject-face
563 'notmuch-pick-no-match-subject-face)))
564 (insert (propertize (format format-string
566 (mapconcat #'identity (reverse tree-status) "")
567 (if (string= notmuch-pick-previous-subject bare-subject)
571 (setq notmuch-pick-previous-subject bare-subject)))
573 ((string-equal field "authors")
574 (let ((author (notmuch-pick-clean-address (plist-get headers :From)))
575 (len (length (format format-string "")))
577 'notmuch-pick-match-author-face
578 'notmuch-pick-no-match-author-face)))
579 (when (> (length author) len)
580 (setq author (substring author 0 len)))
581 (insert (propertize (format format-string author)
584 ((string-equal field "tags")
585 (let ((tags (plist-get msg :tags))
587 'notmuch-pick-match-tag-face
588 'notmuch-pick-no-match-tag-face)))
590 (insert (propertize (format format-string
591 (mapconcat #'identity tags ", "))
594 (defun notmuch-pick-insert-msg (msg)
595 "Insert the message MSG according to notmuch-pick-result-format"
596 (dolist (spec notmuch-pick-result-format)
597 (notmuch-pick-insert-field (car spec) (cdr spec) msg))
598 (notmuch-pick-set-message-properties msg)
601 (defun notmuch-pick-insert-tree (tree depth tree-status first last)
602 "Insert the message tree TREE at depth DEPTH in the current thread."
603 (let ((msg (car tree))
604 (replies (cadr tree)))
607 ((and (< 0 depth) (not last))
608 (push "├" tree-status))
609 ((and (< 0 depth) last)
610 (push "╰" tree-status))
611 ((and (eq 0 depth) first last)
612 ;; (push "─" tree-status)) choice between this and next line is matter of taste.
613 (push " " tree-status))
614 ((and (eq 0 depth) first (not last))
615 (push "┬" tree-status))
616 ((and (eq 0 depth) (not first) last)
617 (push "╰" tree-status))
618 ((and (eq 0 depth) (not first) (not last))
619 (push "├" tree-status)))
621 (push (concat (if replies "┬" "─") "►") tree-status)
622 (notmuch-pick-insert-msg (plist-put msg :tree-status tree-status))
627 (push " " tree-status)
628 (push "│" tree-status))
630 (notmuch-pick-insert-thread replies (1+ depth) tree-status)))
632 (defun notmuch-pick-insert-thread (thread depth tree-status)
633 "Insert the thread THREAD at depth DEPTH >= 1 in the current forest."
634 (let ((n (length thread)))
635 (loop for tree in thread
636 for count from 1 to n
638 do (notmuch-pick-insert-tree tree depth tree-status (eq count 1) (eq count n)))))
640 (defun notmuch-pick-insert-forest-thread (forest-thread)
642 (goto-char (point-max))
644 ;; Reset at the start of each main thread.
645 (setq notmuch-pick-previous-subject nil)
646 (notmuch-pick-insert-thread forest-thread 0 tree-status))))
648 (defun notmuch-pick-insert-forest (forest)
649 (mapc 'notmuch-pick-insert-forest-thread forest))
651 (defun notmuch-pick-mode ()
652 "Major mode displaying messages (as opposed to threads) of of a notmuch search.
654 This buffer contains the results of a \"notmuch pick\" of your
655 email archives. Each line in the buffer represents a single
656 message giving the relative date, the author, subject, and any
659 Pressing \\[notmuch-pick-show-message] on any line displays that message.
661 Complete list of currently available key bindings:
663 \\{notmuch-pick-mode-map}"
666 (kill-all-local-variables)
667 (use-local-map notmuch-pick-mode-map)
668 (setq major-mode 'notmuch-pick-mode
669 mode-name "notmuch-pick")
671 (setq buffer-read-only t
674 (defun notmuch-pick-process-sentinel (proc msg)
675 "Add a message to let user know when \"notmuch pick\" exits"
676 (let ((buffer (process-buffer proc))
677 (status (process-status proc))
678 (exit-status (process-exit-status proc))
679 (never-found-target-thread nil))
680 (when (memq status '(exit signal))
681 (kill-buffer (process-get proc 'parse-buf))
682 (if (buffer-live-p buffer)
683 (with-current-buffer buffer
685 (let ((inhibit-read-only t)
687 (goto-char (point-max))
688 (if (eq status 'signal)
689 (insert "Incomplete search results (pick process was killed).\n"))
690 (when (eq status 'exit)
691 (insert "End of search results.")
692 (unless (= exit-status 0)
693 (insert (format " (process returned %d)" exit-status)))
694 (insert "\n")))))))))
697 (defun notmuch-pick-show-error (string &rest objects)
699 (goto-char (point-max))
700 (insert "Error: Unexpected output from notmuch search:\n")
701 (insert (apply #'format string objects))
705 (defun notmuch-pick-process-filter (proc string)
706 "Process and filter the output of \"notmuch show\" (for pick)"
707 (let ((results-buf (process-buffer proc))
708 (parse-buf (process-get proc 'parse-buf))
709 (inhibit-read-only t)
711 (if (not (buffer-live-p results-buf))
712 (delete-process proc)
713 (with-current-buffer parse-buf
716 (goto-char (point-max))
718 (notmuch-json-parse-partial-list 'notmuch-pick-insert-forest-thread
719 'notmuch-pick-show-error
722 (defun notmuch-pick-worker (basic-query &optional query-context buffer)
725 (setq notmuch-pick-basic-query basic-query)
726 (setq notmuch-pick-query-context query-context)
727 (setq notmuch-pick-buffer-name (buffer-name buffer))
730 (goto-char (point-min))
731 (let* ((search-args (concat basic-query
732 (if query-context (concat " and (" query-context ")"))
734 (message-arg "--entire-thread"))
735 (if (equal (car (process-lines notmuch-command "count" search-args)) "0")
736 (setq search-args basic-query))
737 (if notmuch-pick-asynchronous-parser
738 (let ((proc (start-process
739 "notmuch-pick" buffer
740 notmuch-command "show" "--body=false" "--format=json"
741 message-arg search-args))
742 ;; Use a scratch buffer to accumulate partial output.
743 ;; This buffer will be killed by the sentinel, which
744 ;; should be called no matter how the process dies.
745 (parse-buf (generate-new-buffer " *notmuch pick parse*")))
746 (process-put proc 'parse-buf parse-buf)
747 (set-process-sentinel proc 'notmuch-pick-process-sentinel)
748 (set-process-filter proc 'notmuch-pick-process-filter)
749 (set-process-query-on-exit-flag proc nil))
751 (notmuch-pick-insert-forest
752 (notmuch-query-get-threads
753 (list "--body=false" message-arg search-args)))
755 (goto-char (point-max))
756 (insert "End of search results.\n"))))))
759 (defun notmuch-pick (&optional query query-context buffer-name show-first-match)
760 "Run notmuch pick with the given `query' and display the results"
761 (interactive "sNotmuch pick: ")
763 (setq query (notmuch-read-query "Notmuch pick: ")))
764 (let ((buffer (get-buffer-create (generate-new-buffer-name
766 (concat "*notmuch-pick-" query "*")))))
767 (inhibit-read-only t))
769 (switch-to-buffer buffer)
770 ;; Don't track undo information for this buffer
771 (set 'buffer-undo-list t)
773 (notmuch-pick-worker query query-context buffer)
775 (setq truncate-lines t)
776 (when show-first-match
777 (notmuch-pick-show-match-message-with-wait))))
780 ;; Set up key bindings from the rest of notmuch.
781 (define-key 'notmuch-search-mode-map "z" 'notmuch-pick)
782 (define-key 'notmuch-search-mode-map "Z" 'notmuch-pick-from-search-current-query)
783 (define-key 'notmuch-search-mode-map (kbd "M-RET") 'notmuch-pick-from-search-thread)
784 (define-key 'notmuch-hello-mode-map "z" 'notmuch-pick-from-hello)
785 (define-key 'notmuch-show-mode-map "z" 'notmuch-pick)
786 (define-key 'notmuch-show-mode-map "Z" 'notmuch-pick-from-show-current-query)
787 (notmuch-pick-setup-show-out)
788 (message "Initialised notmuch-pick")
790 (provide 'notmuch-pick)