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 (defun notmuch-pick-to-message-pane (func)
191 "Execute FUNC in message pane.
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)."
197 ,(concat "(In message pane) " (documentation func t))
199 (when (window-live-p notmuch-pick-message-window)
200 (with-selected-window notmuch-pick-message-window
201 (call-interactively #',func)))))
203 (defun notmuch-pick-button-activate (&optional button)
204 "Activate BUTTON or button at point
206 This function does not give an error if there is no button."
208 (let ((button (or button (button-at (point)))))
209 (when button (button-activate button))))
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)
220 ;; these apply to the message pane
221 (define-key map (kbd "M-TAB") (notmuch-pick-to-message-pane #'notmuch-show-previous-button))
222 (define-key map (kbd "<backtab>") (notmuch-pick-to-message-pane #'notmuch-show-previous-button))
223 (define-key map (kbd "TAB") (notmuch-pick-to-message-pane #'notmuch-show-next-button))
224 (define-key map "e" (notmuch-pick-to-message-pane #'notmuch-pick-button-activate))
226 ;; The main pick bindings
227 (define-key map "q" 'notmuch-pick-quit)
228 (define-key map "x" 'notmuch-pick-quit)
229 (define-key map "?" 'notmuch-help)
230 (define-key map "a" 'notmuch-pick-archive-message-then-next)
231 (define-key map "=" 'notmuch-pick-refresh-view)
232 (define-key map "s" 'notmuch-pick-to-search)
233 (define-key map "z" 'notmuch-pick-to-pick)
234 (define-key map "m" 'notmuch-pick-new-mail)
235 (define-key map "f" 'notmuch-pick-forward-message)
236 (define-key map "r" 'notmuch-pick-reply-sender)
237 (define-key map "R" 'notmuch-pick-reply)
238 (define-key map "n" 'notmuch-pick-next-matching-message)
239 (define-key map "p" 'notmuch-pick-prev-matching-message)
240 (define-key map "N" 'notmuch-pick-next-message)
241 (define-key map "P" 'notmuch-pick-prev-message)
242 (define-key map "-" 'notmuch-pick-remove-tag)
243 (define-key map "+" 'notmuch-pick-add-tag)
244 (define-key map " " 'notmuch-pick-scroll-or-next)
245 (define-key map "b" 'notmuch-pick-scroll-message-window-back)
247 (fset 'notmuch-pick-mode-map notmuch-pick-mode-map)
249 (defun notmuch-pick-setup-show-out ()
250 "Set up the keymap for showing a thread
252 This uses the value of the defcustom notmuch-pick-show-out to
253 decide whether to show a message in the message pane or in the
255 (let ((map notmuch-pick-mode-map))
256 (if notmuch-pick-show-out
258 (define-key map (kbd "M-RET") 'notmuch-pick-show-message)
259 (define-key map (kbd "RET") 'notmuch-pick-show-message-out))
261 (define-key map (kbd "RET") 'notmuch-pick-show-message)
262 (define-key map (kbd "M-RET") 'notmuch-pick-show-message-out)))))
264 (defun notmuch-pick-get-message-properties ()
265 "Return the properties of the current message as a plist.
267 Some useful entries are:
268 :headers - Property list containing the headers :Date, :Subject, :From, etc.
269 :tags - Tags for this message"
272 (get-text-property (point) :notmuch-message-properties)))
274 ;; XXX This should really be a lib function but we are trying to
275 ;; reduce impact on the code base.
276 (defun notmuch-show-get-prop (prop &optional props)
277 "This is a pick overridden version of notmuch-show-get-prop
279 It gets property PROP from PROPS or, if PROPS is nil, the current
280 message in either pick or show. This means that several functions
281 in notmuch-show now work unchanged in pick as they just need the
282 correct message properties."
283 (let ((props (or props
284 (cond ((eq major-mode 'notmuch-show-mode)
285 (notmuch-show-get-message-properties))
286 ((eq major-mode 'notmuch-pick-mode)
287 (notmuch-pick-get-message-properties))))))
288 (plist-get props prop)))
290 (defun notmuch-pick-set-message-properties (props)
293 (put-text-property (point) (+ (point) 1) :notmuch-message-properties props)))
295 (defun notmuch-pick-set-prop (prop val &optional props)
296 (let ((inhibit-read-only t)
298 (notmuch-pick-get-message-properties))))
299 (plist-put props prop val)
300 (notmuch-pick-set-message-properties props)))
302 (defun notmuch-pick-get-prop (prop &optional props)
303 (let ((props (or props
304 (notmuch-pick-get-message-properties))))
305 (plist-get props prop)))
307 (defun notmuch-pick-set-tags (tags)
308 "Set the tags of the current message."
309 (notmuch-pick-set-prop :tags tags))
311 (defun notmuch-pick-get-tags ()
312 "Return the tags of the current message."
313 (notmuch-pick-get-prop :tags))
315 (defun notmuch-pick-get-message-id ()
316 "Return the message id of the current message."
317 (let ((id (notmuch-pick-get-prop :id)))
319 (notmuch-id-to-query id)
322 (defun notmuch-pick-get-match ()
323 "Return whether the current message is a match."
325 (notmuch-pick-get-prop :match))
327 (defun notmuch-pick-refresh-result ()
328 "Redisplay the current message line.
330 This redisplays the current line based on the messages
331 properties (as they are now). This is used when tags are
333 (let ((init-point (point))
334 (end (line-end-position))
335 (msg (notmuch-pick-get-message-properties))
336 (inhibit-read-only t))
338 ;; This is a little tricky: we override
339 ;; notmuch-pick-previous-subject to get the decision between
340 ;; ... and a subject right and it stops notmuch-pick-insert-msg
341 ;; from overwriting the buffer local copy of
342 ;; notmuch-pick-previous-subject if this is called while the
343 ;; buffer is displaying.
344 (let ((notmuch-pick-previous-subject (notmuch-pick-get-prop :previous-subject)))
345 (delete-region (point) (1+ (line-end-position)))
346 (notmuch-pick-insert-msg msg))
347 (let ((new-end (line-end-position)))
348 (goto-char (if (= init-point end)
350 (min init-point (- new-end 1)))))))
352 (defun notmuch-pick-tag-update-display (&optional tag-changes)
353 "Update display for TAG-CHANGES to current message.
355 Does NOT change the database."
356 (let* ((current-tags (notmuch-pick-get-tags))
357 (new-tags (notmuch-update-tags current-tags tag-changes)))
358 (unless (equal current-tags new-tags)
359 (notmuch-pick-set-tags new-tags)
360 (notmuch-pick-refresh-result))))
362 (defun notmuch-pick-tag (&optional tag-changes)
363 "Change tags for the current message"
365 (setq tag-changes (notmuch-tag (notmuch-pick-get-message-id) tag-changes))
366 (notmuch-pick-tag-update-display tag-changes))
368 (defun notmuch-pick-add-tag ()
369 "Same as `notmuch-pick-tag' but sets initial input to '+'."
371 (notmuch-pick-tag "+"))
373 (defun notmuch-pick-remove-tag ()
374 "Same as `notmuch-pick-tag' but sets initial input to '-'."
376 (notmuch-pick-tag "-"))
378 ;; The next two functions close the message window before searching or
379 ;; picking but they do so after the user has entered the query (in
380 ;; case the user was basing the query on something in the message
383 (defun notmuch-pick-to-search ()
384 "Run \"notmuch search\" with the given `query' and display results."
386 (let ((query (notmuch-read-query "Notmuch search: ")))
387 (notmuch-pick-close-message-window)
388 (notmuch-search query)))
390 (defun notmuch-pick-to-pick ()
391 "Run a query and display results in experimental notmuch-pick mode"
393 (let ((query (notmuch-read-query "Notmuch pick: ")))
394 (notmuch-pick-close-message-window)
395 (notmuch-pick query)))
397 ;; This function should be in notmuch-hello.el but we are trying to
398 ;; minimise impact on the rest of the codebase.
399 (defun notmuch-pick-from-hello (&optional search)
400 "Run a query and display results in experimental notmuch-pick mode"
402 (unless (null search)
403 (setq search (notmuch-hello-trim search))
404 (let ((history-delete-duplicates t))
405 (add-to-history 'notmuch-search-history search)))
406 (notmuch-pick search))
408 ;; This function should be in notmuch-show.el but be we trying to
409 ;; minimise impact on the rest of the codebase.
410 (defun notmuch-pick-from-show-current-query ()
411 "Call notmuch pick with the current query"
413 (notmuch-pick notmuch-show-thread-id
414 notmuch-show-query-context
415 (notmuch-show-get-message-id)))
417 ;; This function should be in notmuch.el but be we trying to minimise
418 ;; impact on the rest of the codebase.
419 (defun notmuch-pick-from-search-current-query ()
420 "Call notmuch pick with the current query"
422 (notmuch-pick notmuch-search-query-string))
424 ;; This function should be in notmuch.el but be we trying to minimise
425 ;; impact on the rest of the codebase.
426 (defun notmuch-pick-from-search-thread ()
427 "Show the selected thread with notmuch-pick"
429 (notmuch-pick (notmuch-search-find-thread-id)
430 notmuch-search-query-string
432 (notmuch-prettify-subject (notmuch-search-find-subject))
435 (defun notmuch-pick-message-window-kill-hook ()
436 "Close the message pane when exiting the show buffer."
437 (let ((buffer (current-buffer)))
438 (when (and (window-live-p notmuch-pick-message-window)
439 (eq (window-buffer notmuch-pick-message-window) buffer))
440 ;; We do not want an error if this is the sole window in the
441 ;; frame and I do not know how to test for that in emacs pre
442 ;; 24. Hence we just ignore-errors.
444 (delete-window notmuch-pick-message-window)))))
446 (defun notmuch-pick-show-message ()
447 "Show the current message (in split-pane)."
449 (let ((id (notmuch-pick-get-message-id))
450 (inhibit-read-only t)
453 ;; We close and reopen the window to kill off un-needed buffers
454 ;; this might cause flickering but seems ok.
455 (notmuch-pick-close-message-window)
456 (setq notmuch-pick-message-window
457 (split-window-vertically (/ (window-height) 4)))
458 (with-selected-window notmuch-pick-message-window
459 ;; Since we are only displaying one message do not indent.
460 (let ((notmuch-show-indent-messages-width 0)
461 (notmuch-show-only-matching-messages t))
462 (setq buffer (notmuch-show id nil nil nil))))
463 ;; We need the `let' as notmuch-pick-message-window is buffer local.
464 (let ((window notmuch-pick-message-window))
465 (with-current-buffer buffer
466 (setq notmuch-pick-message-window window)
467 (add-hook 'kill-buffer-hook 'notmuch-pick-message-window-kill-hook)))
468 (when notmuch-show-mark-read-tags
469 (notmuch-pick-tag-update-display notmuch-show-mark-read-tags))
470 (setq notmuch-pick-message-buffer buffer))))
472 (defun notmuch-pick-show-message-out ()
473 "Show the current message (in whole window)."
475 (let ((id (notmuch-pick-get-message-id))
476 (inhibit-read-only t)
479 ;; We close the window to kill off un-needed buffers.
480 (notmuch-pick-close-message-window)
481 (notmuch-show id nil nil nil))))
483 (defun notmuch-pick-scroll-message-window ()
484 "Scroll the message window (if it exists)"
486 (when (window-live-p notmuch-pick-message-window)
487 (with-selected-window notmuch-pick-message-window
488 (if (pos-visible-in-window-p (point-max))
492 (defun notmuch-pick-scroll-message-window-back ()
493 "Scroll the message window back(if it exists)"
495 (when (window-live-p notmuch-pick-message-window)
496 (with-selected-window notmuch-pick-message-window
497 (if (pos-visible-in-window-p (point-min))
501 (defun notmuch-pick-scroll-or-next ()
502 "Scroll the message window. If it at end go to next message."
504 (when (notmuch-pick-scroll-message-window)
505 (notmuch-pick-next-matching-message)))
507 (defun notmuch-pick-quit ()
508 "Close the split view or exit pick."
510 (unless (notmuch-pick-close-message-window)
511 (kill-buffer (current-buffer))))
513 (defun notmuch-pick-close-message-window ()
514 "Close the message-window. Return t if close succeeds."
516 (when (and (window-live-p notmuch-pick-message-window)
517 (eq (window-buffer notmuch-pick-message-window) notmuch-pick-message-buffer))
518 (delete-window notmuch-pick-message-window)
519 (unless (get-buffer-window-list notmuch-pick-message-buffer)
520 (kill-buffer notmuch-pick-message-buffer))
523 (defun notmuch-pick-archive-message (&optional unarchive)
524 "Archive the current message.
526 Archive the current message by applying the tag changes in
527 `notmuch-archive-tags' to it. If a prefix argument is given, the
528 message will be \"unarchived\", i.e. the tag changes in
529 `notmuch-archive-tags' will be reversed."
531 (when notmuch-archive-tags
532 (apply 'notmuch-pick-tag
533 (notmuch-tag-change-list notmuch-archive-tags unarchive))))
535 (defun notmuch-pick-archive-message-then-next (&optional unarchive)
536 "Archive the current message and move to next matching message."
538 (notmuch-pick-archive-message unarchive)
539 (notmuch-pick-next-matching-message))
541 (defun notmuch-pick-next-message ()
542 "Move to next message."
545 (when (window-live-p notmuch-pick-message-window)
546 (notmuch-pick-show-message)))
548 (defun notmuch-pick-prev-message ()
549 "Move to previous message."
552 (when (window-live-p notmuch-pick-message-window)
553 (notmuch-pick-show-message)))
555 (defun notmuch-pick-prev-matching-message ()
556 "Move to previous matching message."
559 (while (and (not (bobp)) (not (notmuch-pick-get-match)))
561 (when (window-live-p notmuch-pick-message-window)
562 (notmuch-pick-show-message)))
564 (defun notmuch-pick-next-matching-message ()
565 "Move to next matching message."
568 (while (and (not (eobp)) (not (notmuch-pick-get-match)))
570 (when (window-live-p notmuch-pick-message-window)
571 (notmuch-pick-show-message)))
573 (defun notmuch-pick-refresh-view ()
576 (let ((inhibit-read-only t)
577 (basic-query notmuch-pick-basic-query)
578 (query-context notmuch-pick-query-context)
579 (target (notmuch-pick-get-message-id))
580 (buffer-name notmuch-pick-buffer-name))
582 (notmuch-pick-worker basic-query
585 (get-buffer buffer-name))))
587 (defmacro with-current-notmuch-pick-message (&rest body)
588 "Evaluate body with current buffer set to the text of current message"
590 (let ((id (notmuch-pick-get-message-id)))
591 (let ((buf (generate-new-buffer (concat "*notmuch-msg-" id "*"))))
592 (with-current-buffer buf
593 (call-process notmuch-command nil t nil "show" "--format=raw" id)
595 (kill-buffer buf)))))
597 (defun notmuch-pick-new-mail (&optional prompt-for-sender)
600 (notmuch-pick-close-message-window)
601 (notmuch-mua-new-mail prompt-for-sender ))
603 (defun notmuch-pick-forward-message (&optional prompt-for-sender)
604 "Forward the current message."
606 (notmuch-pick-close-message-window)
607 (with-current-notmuch-pick-message
608 (notmuch-mua-new-forward-message prompt-for-sender)))
610 (defun notmuch-pick-reply (&optional prompt-for-sender)
611 "Reply to the sender and all recipients of the current message."
613 (notmuch-pick-close-message-window)
614 (notmuch-mua-new-reply (notmuch-pick-get-message-id) prompt-for-sender t))
616 (defun notmuch-pick-reply-sender (&optional prompt-for-sender)
617 "Reply to the sender of the current message."
619 (notmuch-pick-close-message-window)
620 (notmuch-mua-new-reply (notmuch-pick-get-message-id) prompt-for-sender nil))
622 (defun notmuch-pick-clean-address (address)
623 "Try to clean a single email ADDRESS for display. Return
624 AUTHOR_NAME if present, otherwise return AUTHOR_EMAIL. Return
625 unchanged ADDRESS if parsing fails."
626 (let* ((clean-address (notmuch-clean-address address))
627 (p-address (car clean-address))
628 (p-name (cdr clean-address)))
630 ;; If we have a name return that otherwise return the address.
631 (or p-name p-address)))
633 (defun notmuch-pick-insert-field (field format-string msg)
634 (let* ((headers (plist-get msg :headers))
635 (match (plist-get msg :match)))
637 ((string-equal field "date")
638 (let ((face (if match
639 'notmuch-pick-match-date-face
640 'notmuch-pick-no-match-date-face)))
641 (insert (propertize (format format-string (plist-get msg :date_relative))
644 ((string-equal field "subject")
645 (let ((tree-status (plist-get msg :tree-status))
646 (bare-subject (notmuch-show-strip-re (plist-get headers :Subject)))
648 'notmuch-pick-match-subject-face
649 'notmuch-pick-no-match-subject-face)))
650 (insert (propertize (format format-string
652 (mapconcat #'identity (reverse tree-status) "")
653 (if (string= notmuch-pick-previous-subject bare-subject)
657 (setq notmuch-pick-previous-subject bare-subject)))
659 ((string-equal field "authors")
660 (let ((author (notmuch-pick-clean-address (plist-get headers :From)))
661 (len (length (format format-string "")))
663 'notmuch-pick-match-author-face
664 'notmuch-pick-no-match-author-face)))
665 (when (> (length author) len)
666 (setq author (substring author 0 len)))
667 (insert (propertize (format format-string author)
670 ((string-equal field "tags")
671 (let ((tags (plist-get msg :tags))
673 'notmuch-pick-match-tag-face
674 'notmuch-pick-no-match-tag-face)))
676 (insert (propertize (format format-string
677 (mapconcat #'identity tags ", "))
680 (defun notmuch-pick-insert-msg (msg)
681 "Insert the message MSG according to notmuch-pick-result-format"
682 ;; We need to save the previous subject as it will get overwritten
683 ;; by the insert-field calls.
684 (let ((previous-subject notmuch-pick-previous-subject))
685 (dolist (spec notmuch-pick-result-format)
686 (notmuch-pick-insert-field (car spec) (cdr spec) msg))
687 (notmuch-pick-set-message-properties msg)
688 (notmuch-pick-set-prop :previous-subject previous-subject)
691 (defun notmuch-pick-goto-and-insert-msg (msg)
692 "Insert msg at the end of the buffer. Move point to msg if it is the target"
694 (goto-char (point-max))
695 (notmuch-pick-insert-msg msg))
696 (let ((msg-id (notmuch-id-to-query (plist-get msg :id)))
697 (target notmuch-pick-target-msg))
698 (when (or (and (not target) (plist-get msg :match))
699 (string= msg-id target))
700 (setq notmuch-pick-target-msg "found")
701 (goto-char (point-max))
703 (when notmuch-pick-open-target
704 (notmuch-pick-show-message)))))
706 (defun notmuch-pick-insert-tree (tree depth tree-status first last)
707 "Insert the message tree TREE at depth DEPTH in the current thread.
709 A message tree is another name for a single sub-thread: i.e., a
710 message together with all its descendents."
711 (let ((msg (car tree))
712 (replies (cadr tree)))
715 ((and (< 0 depth) (not last))
716 (push "├" tree-status))
717 ((and (< 0 depth) last)
718 (push "╰" tree-status))
719 ((and (eq 0 depth) first last)
720 ;; (push "─" tree-status)) choice between this and next line is matter of taste.
721 (push " " tree-status))
722 ((and (eq 0 depth) first (not last))
723 (push "┬" tree-status))
724 ((and (eq 0 depth) (not first) last)
725 (push "╰" tree-status))
726 ((and (eq 0 depth) (not first) (not last))
727 (push "├" tree-status)))
729 (push (concat (if replies "┬" "─") "►") tree-status)
730 (notmuch-pick-goto-and-insert-msg (plist-put msg :tree-status tree-status))
735 (push " " tree-status)
736 (push "│" tree-status))
738 (notmuch-pick-insert-thread replies (1+ depth) tree-status)))
740 (defun notmuch-pick-insert-thread (thread depth tree-status)
741 "Insert the collection of sibling sub-threads THREAD at depth DEPTH in the current forest."
742 (let ((n (length thread)))
743 (loop for tree in thread
744 for count from 1 to n
746 do (notmuch-pick-insert-tree tree depth tree-status (eq count 1) (eq count n)))))
748 (defun notmuch-pick-insert-forest-thread (forest-thread)
749 "Insert a single complete thread."
751 ;; Reset at the start of each main thread.
752 (setq notmuch-pick-previous-subject nil)
753 (notmuch-pick-insert-thread forest-thread 0 tree-status)))
755 (defun notmuch-pick-insert-forest (forest)
756 "Insert a forest of threads.
758 This function inserts a collection of several complete threads as
759 passed to it by notmuch-pick-process-filter."
760 (mapc 'notmuch-pick-insert-forest-thread forest))
762 (defun notmuch-pick-mode ()
763 "Major mode displaying messages (as opposed to threads) of of a notmuch search.
765 This buffer contains the results of a \"notmuch pick\" of your
766 email archives. Each line in the buffer represents a single
767 message giving the relative date, the author, subject, and any
770 Pressing \\[notmuch-pick-show-message] on any line displays that message.
772 Complete list of currently available key bindings:
774 \\{notmuch-pick-mode-map}"
777 (kill-all-local-variables)
778 (use-local-map notmuch-pick-mode-map)
779 (setq major-mode 'notmuch-pick-mode
780 mode-name "notmuch-pick")
782 (setq buffer-read-only t
785 (defun notmuch-pick-process-sentinel (proc msg)
786 "Add a message to let user know when \"notmuch pick\" exits"
787 (let ((buffer (process-buffer proc))
788 (status (process-status proc))
789 (exit-status (process-exit-status proc))
790 (never-found-target-thread nil))
791 (when (memq status '(exit signal))
792 (kill-buffer (process-get proc 'parse-buf))
793 (if (buffer-live-p buffer)
794 (with-current-buffer buffer
796 (let ((inhibit-read-only t)
798 (goto-char (point-max))
799 (if (eq status 'signal)
800 (insert "Incomplete search results (pick process was killed).\n"))
801 (when (eq status 'exit)
802 (insert "End of search results.")
803 (unless (= exit-status 0)
804 (insert (format " (process returned %d)" exit-status)))
805 (insert "\n")))))))))
807 (defun notmuch-pick-process-filter (proc string)
808 "Process and filter the output of \"notmuch show\" (for pick)"
809 (let ((results-buf (process-buffer proc))
810 (parse-buf (process-get proc 'parse-buf))
811 (inhibit-read-only t)
813 (if (not (buffer-live-p results-buf))
814 (delete-process proc)
815 (with-current-buffer parse-buf
818 (goto-char (point-max))
820 (notmuch-sexp-parse-partial-list 'notmuch-pick-insert-forest-thread
823 (defun notmuch-pick-worker (basic-query &optional query-context target buffer open-target)
826 (setq notmuch-pick-basic-query basic-query)
827 (setq notmuch-pick-query-context query-context)
828 (setq notmuch-pick-buffer-name (buffer-name buffer))
829 (setq notmuch-pick-target-msg target)
830 (setq notmuch-pick-open-target open-target)
833 (goto-char (point-min))
834 (let* ((search-args (concat basic-query
835 (if query-context (concat " and (" query-context ")"))
837 (message-arg "--entire-thread"))
838 (if (equal (car (process-lines notmuch-command "count" search-args)) "0")
839 (setq search-args basic-query))
840 (if notmuch-pick-asynchronous-parser
841 (let ((proc (notmuch-start-notmuch
842 "notmuch-pick" buffer #'notmuch-pick-process-sentinel
843 "show" "--body=false" "--format=sexp"
844 message-arg search-args))
845 ;; Use a scratch buffer to accumulate partial output.
846 ;; This buffer will be killed by the sentinel, which
847 ;; should be called no matter how the process dies.
848 (parse-buf (generate-new-buffer " *notmuch pick parse*")))
849 (process-put proc 'parse-buf parse-buf)
850 (set-process-filter proc 'notmuch-pick-process-filter)
851 (set-process-query-on-exit-flag proc nil))
853 (notmuch-pick-insert-forest
854 (notmuch-query-get-threads
855 (list "--body=false" message-arg search-args)))
857 (goto-char (point-max))
858 (insert "End of search results.\n"))))))
861 (defun notmuch-pick (&optional query query-context target buffer-name open-target)
862 "Run notmuch pick with the given `query' and display the results.
865 QUERY: the main query. This can be any query but in many cases will be
866 a single thread. If nil this is read interactively from the minibuffer.
867 QUERY-CONTEXT: is an additional term for the query. The query used
868 is QUERY and QUERY-CONTEXT unless that does not match any messages
869 in which case we fall back to just QUERY.
870 TARGET: A message ID (with the id: prefix) that will be made
871 current if it appears in the pick results.
872 BUFFER-NAME: the name of the buffer to show the pick tree. If
873 it is nil \"*notmuch-pick\" followed by QUERY is used.
874 OPEN-TARGET: If TRUE open the target message in the message pane."
875 (interactive "sNotmuch pick: ")
877 (setq query (notmuch-read-query "Notmuch pick: ")))
878 (let ((buffer (get-buffer-create (generate-new-buffer-name
880 (concat "*notmuch-pick-" query "*")))))
881 (inhibit-read-only t))
883 (switch-to-buffer buffer)
884 ;; Don't track undo information for this buffer
885 (set 'buffer-undo-list t)
887 (notmuch-pick-worker query query-context target buffer open-target)
889 (setq truncate-lines t)))
892 ;; Set up key bindings from the rest of notmuch.
893 (define-key 'notmuch-search-mode-map "z" 'notmuch-pick)
894 (define-key 'notmuch-search-mode-map "Z" 'notmuch-pick-from-search-current-query)
895 (define-key 'notmuch-search-mode-map (kbd "M-RET") 'notmuch-pick-from-search-thread)
896 (define-key 'notmuch-hello-mode-map "z" 'notmuch-pick-from-hello)
897 (define-key 'notmuch-show-mode-map "z" 'notmuch-pick)
898 (define-key 'notmuch-show-mode-map "Z" 'notmuch-pick-from-show-current-query)
899 (notmuch-pick-setup-show-out)
900 (message "Initialised notmuch-pick")
902 (provide 'notmuch-pick)