1 ;; notmuch.el --- run notmuch within emacs
3 ;; Copyright © Carl Worth
5 ;; This file is part of Notmuch.
7 ;; Notmuch is free software: you can redistribute it and/or modify it
8 ;; under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; Notmuch is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;; General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with Notmuch. If not, see <http://www.gnu.org/licenses/>.
20 ;; Authors: Carl Worth <cworth@cworth.org>
22 ;; This is an emacs-based interface to the notmuch mail system.
24 ;; You will first need to have the notmuch program installed and have a
25 ;; notmuch database built in order to use this. See
26 ;; http://notmuchmail.org for details.
28 ;; To install this software, copy it to a directory that is on the
29 ;; `load-path' variable within emacs (a good candidate is
30 ;; /usr/local/share/emacs/site-lisp). If you are viewing this from the
31 ;; notmuch source distribution then you can simply run:
33 ;; sudo make install-emacs
37 ;; Then, to actually run it, add:
41 ;; to your ~/.emacs file, and then run "M-x notmuch" from within emacs,
46 ;; Have fun, and let us know if you have any comment, questions, or
47 ;; kudos: Notmuch list <notmuch@notmuchmail.org> (subscription is not
48 ;; required, but is available from http://notmuchmail.org).
50 (eval-when-compile (require 'cl))
54 (require 'notmuch-lib)
55 (require 'notmuch-tag)
56 (require 'notmuch-show)
57 (require 'notmuch-tree)
58 (require 'notmuch-mua)
59 (require 'notmuch-hello)
60 (require 'notmuch-maildir-fcc)
61 (require 'notmuch-message)
62 (require 'notmuch-parser)
64 (defcustom notmuch-search-result-format
67 ("authors" . "%-20s ")
70 "Search result formatting. Supported fields are:
71 date, count, authors, subject, tags
73 (setq notmuch-search-result-format \(\(\"authors\" . \"%-40s\"\)
74 \(\"subject\" . \"%s\"\)\)\)
75 Line breaks are permitted in format strings (though this is
76 currently experimental). Note that a line break at the end of an
77 \"authors\" field will get elided if the authors list is long;
78 place it instead at the beginning of the following field. To
79 enter a line break when setting this variable with setq, use \\n.
80 To enter a line break in customize, press \\[quoted-insert] C-j."
81 :type '(alist :key-type (string) :value-type (string))
82 :group 'notmuch-search)
84 (defvar notmuch-query-history nil
85 "Variable to store minibuffer history for notmuch queries")
87 (defun notmuch-foreach-mime-part (function mm-handle)
88 (cond ((stringp (car mm-handle))
89 (dolist (part (cdr mm-handle))
90 (notmuch-foreach-mime-part function part)))
91 ((bufferp (car mm-handle))
92 (funcall function mm-handle))
93 (t (dolist (part mm-handle)
94 (notmuch-foreach-mime-part function part)))))
96 (defun notmuch-count-attachments (mm-handle)
98 (notmuch-foreach-mime-part
100 (let ((disposition (mm-handle-disposition p)))
101 (and (listp disposition)
102 (or (equal (car disposition) "attachment")
103 (and (equal (car disposition) "inline")
104 (assq 'filename disposition)))
109 (defun notmuch-save-attachments (mm-handle &optional queryp)
110 (notmuch-foreach-mime-part
112 (let ((disposition (mm-handle-disposition p)))
113 (and (listp disposition)
114 (or (equal (car disposition) "attachment")
115 (and (equal (car disposition) "inline")
116 (assq 'filename disposition)))
119 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
125 (defun notmuch-hl-line-mode ()
126 (prog1 (hl-line-mode)
127 (when hl-line-overlay
128 (overlay-put hl-line-overlay 'priority 1))))
130 (defcustom notmuch-search-hook '(notmuch-hl-line-mode)
131 "List of functions to call when notmuch displays the search results."
133 :options '(notmuch-hl-line-mode)
134 :group 'notmuch-search
135 :group 'notmuch-hooks)
137 (defvar notmuch-search-mode-map
138 (let ((map (make-sparse-keymap)))
139 (set-keymap-parent map notmuch-common-keymap)
140 (define-key map "x" 'notmuch-kill-this-buffer)
141 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
142 (define-key map "b" 'notmuch-search-scroll-down)
143 (define-key map " " 'notmuch-search-scroll-up)
144 (define-key map "<" 'notmuch-search-first-thread)
145 (define-key map ">" 'notmuch-search-last-thread)
146 (define-key map "p" 'notmuch-search-previous-thread)
147 (define-key map "n" 'notmuch-search-next-thread)
148 (define-key map "r" 'notmuch-search-reply-to-thread-sender)
149 (define-key map "R" 'notmuch-search-reply-to-thread)
150 (define-key map "o" 'notmuch-search-toggle-order)
151 (define-key map "c" 'notmuch-search-stash-map)
152 (define-key map "t" 'notmuch-search-filter-by-tag)
153 (define-key map "f" 'notmuch-search-filter)
154 (define-key map [mouse-1] 'notmuch-search-show-thread)
155 (define-key map "*" 'notmuch-search-tag-all)
156 (define-key map "a" 'notmuch-search-archive-thread)
157 (define-key map "-" 'notmuch-search-remove-tag)
158 (define-key map "+" 'notmuch-search-add-tag)
159 (define-key map (kbd "RET") 'notmuch-search-show-thread)
160 (define-key map "Z" 'notmuch-tree-from-search-current-query)
162 "Keymap for \"notmuch search\" buffers.")
163 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
165 (defvar notmuch-search-stash-map
166 (let ((map (make-sparse-keymap)))
167 (define-key map "i" 'notmuch-search-stash-thread-id)
169 "Submap for stash commands")
170 (fset 'notmuch-search-stash-map notmuch-search-stash-map)
172 (defun notmuch-search-stash-thread-id ()
173 "Copy thread ID of current thread to kill-ring."
175 (notmuch-common-do-stash (notmuch-search-find-thread-id)))
177 (defvar notmuch-search-query-string)
178 (defvar notmuch-search-target-thread)
179 (defvar notmuch-search-target-line)
181 (defvar notmuch-search-disjunctive-regexp "\\<[oO][rR]\\>")
183 (defun notmuch-search-scroll-up ()
184 "Move forward through search results by one window's worth."
188 ((end-of-buffer) (notmuch-search-last-thread))))
190 (defun notmuch-search-scroll-down ()
191 "Move backward through the search results by one window's worth."
193 ;; I don't know why scroll-down doesn't signal beginning-of-buffer
194 ;; the way that scroll-up signals end-of-buffer, but c'est la vie.
196 ;; So instead of trapping a signal we instead check whether the
197 ;; window begins on the first line of the buffer and if so, move
198 ;; directly to that position. (We have to count lines since the
199 ;; window-start position is not the same as point-min due to the
200 ;; invisible thread-ID characters on the first line.
201 (if (equal (count-lines (point-min) (window-start)) 0)
202 (goto-char (point-min))
205 (defun notmuch-search-next-thread ()
206 "Select the next thread in the search results."
208 (when (notmuch-search-get-result)
209 (goto-char (notmuch-search-result-end))))
211 (defun notmuch-search-previous-thread ()
212 "Select the previous thread in the search results."
214 (if (notmuch-search-get-result)
216 (goto-char (notmuch-search-result-beginning (- (point) 1))))
217 ;; We must be past the end; jump to the last result
218 (notmuch-search-last-thread)))
220 (defun notmuch-search-last-thread ()
221 "Select the last thread in the search results."
223 (goto-char (point-max))
225 (let ((beg (notmuch-search-result-beginning)))
226 (when beg (goto-char beg))))
228 (defun notmuch-search-first-thread ()
229 "Select the first thread in the search results."
231 (goto-char (point-min)))
233 (defface notmuch-message-summary-face
234 '((((class color) (background light)) (:background "#f0f0f0"))
235 (((class color) (background dark)) (:background "#303030")))
236 "Face for the single-line message summary in notmuch-show-mode."
238 :group 'notmuch-faces)
240 (defface notmuch-search-date
241 '((t :inherit default))
242 "Face used in search mode for dates."
243 :group 'notmuch-search
244 :group 'notmuch-faces)
246 (defface notmuch-search-count
247 '((t :inherit default))
248 "Face used in search mode for the count matching the query."
249 :group 'notmuch-search
250 :group 'notmuch-faces)
252 (defface notmuch-search-subject
253 '((t :inherit default))
254 "Face used in search mode for subjects."
255 :group 'notmuch-search
256 :group 'notmuch-faces)
258 (defface notmuch-search-matching-authors
259 '((t :inherit default))
260 "Face used in search mode for authors matching the query."
261 :group 'notmuch-search
262 :group 'notmuch-faces)
264 (defface notmuch-search-non-matching-authors
267 (:foreground "grey30"))
270 (:foreground "grey60"))
273 "Face used in search mode for authors not matching the query."
274 :group 'notmuch-search
275 :group 'notmuch-faces)
277 (defface notmuch-tag-face
280 (:foreground "OliveDrab1"))
283 (:foreground "navy blue" :bold t))
286 "Face used in search mode face for tags."
287 :group 'notmuch-search
288 :group 'notmuch-faces)
290 (defun notmuch-search-mode ()
291 "Major mode displaying results of a notmuch search.
293 This buffer contains the results of a \"notmuch search\" of your
294 email archives. Each line in the buffer represents a single
295 thread giving a summary of the thread (a relative date, the
296 number of matched messages and total messages in the thread,
297 participants in the thread, a representative subject line, and
300 Pressing \\[notmuch-search-show-thread] on any line displays that
301 thread. The '\\[notmuch-search-add-tag]' and
302 '\\[notmuch-search-remove-tag]' keys can be used to add or remove
303 tags from a thread. The '\\[notmuch-search-archive-thread]' key
304 is a convenience for archiving a thread (applying changes in
305 `notmuch-archive-tags'). The '\\[notmuch-search-tag-all]' key can
306 be used to add and/or remove tags from all messages (as opposed
307 to threads) that match the current query. Use with caution, as
308 this will also tag matching messages that arrived *after*
309 constructing the buffer.
311 Other useful commands are '\\[notmuch-search-filter]' for
312 filtering the current search based on an additional query string,
313 '\\[notmuch-search-filter-by-tag]' for filtering to include only
314 messages with a given tag, and '\\[notmuch-search]' to execute a
317 Complete list of currently available key bindings:
319 \\{notmuch-search-mode-map}"
321 (kill-all-local-variables)
322 (make-local-variable 'notmuch-search-query-string)
323 (make-local-variable 'notmuch-search-oldest-first)
324 (make-local-variable 'notmuch-search-target-thread)
325 (make-local-variable 'notmuch-search-target-line)
326 (setq notmuch-buffer-refresh-function #'notmuch-search-refresh-view)
327 (set (make-local-variable 'scroll-preserve-screen-position) t)
328 (add-to-invisibility-spec (cons 'ellipsis t))
329 (use-local-map notmuch-search-mode-map)
330 (setq truncate-lines t)
331 (setq major-mode 'notmuch-search-mode
332 mode-name "notmuch-search")
333 (setq buffer-read-only t))
335 (defun notmuch-search-get-result (&optional pos)
336 "Return the result object for the thread at POS (or point).
338 If there is no thread at POS (or point), returns nil."
339 (get-text-property (or pos (point)) 'notmuch-search-result))
341 (defun notmuch-search-result-beginning (&optional pos)
342 "Return the point at the beginning of the thread at POS (or point).
344 If there is no thread at POS (or point), returns nil."
345 (when (notmuch-search-get-result pos)
346 ;; We pass 1+point because previous-single-property-change starts
347 ;; searching one before the position we give it.
348 (previous-single-property-change (1+ (or pos (point)))
349 'notmuch-search-result nil (point-min))))
351 (defun notmuch-search-result-end (&optional pos)
352 "Return the point at the end of the thread at POS (or point).
354 The returned point will be just after the newline character that
355 ends the result line. If there is no thread at POS (or point),
357 (when (notmuch-search-get-result pos)
358 (next-single-property-change (or pos (point)) 'notmuch-search-result
361 (defun notmuch-search-foreach-result (beg end function)
362 "Invoke FUNCTION for each result between BEG and END.
364 FUNCTION should take one argument. It will be applied to the
365 character position of the beginning of each result that overlaps
366 the region between points BEG and END. As a special case, if (=
367 BEG END), FUNCTION will be applied to the result containing point
370 (lexical-let ((pos (notmuch-search-result-beginning beg))
371 ;; End must be a marker in case function changes the
373 (end (copy-marker end))
374 ;; Make sure we examine at least one result, even if
377 ;; We have to be careful if the region extends beyond the results.
378 ;; In this case, pos could be null or there could be no result at
380 (while (and pos (or (< pos end) first))
381 (when (notmuch-search-get-result pos)
382 (funcall function pos))
383 (setq pos (notmuch-search-result-end pos)
385 ;; Unindent the function argument of notmuch-search-foreach-result so
386 ;; the indentation of callers doesn't get out of hand.
387 (put 'notmuch-search-foreach-result 'lisp-indent-function 2)
389 (defun notmuch-search-properties-in-region (property beg end)
391 (notmuch-search-foreach-result beg end
393 (push (plist-get (notmuch-search-get-result pos) property) output)))
396 (defun notmuch-search-find-thread-id (&optional bare)
397 "Return the thread for the current thread
399 If BARE is set then do not prefix with \"thread:\""
400 (let ((thread (plist-get (notmuch-search-get-result) :thread)))
401 (when thread (concat (unless bare "thread:") thread))))
403 (defun notmuch-search-find-stable-query ()
404 "Return the stable queries for the current thread.
406 This returns a list (MATCHED-QUERY UNMATCHED-QUERY) for the
407 matched and unmatched messages in the current thread."
408 (plist-get (notmuch-search-get-result) :query))
410 (defun notmuch-search-find-stable-query-region (beg end &optional only-matched)
411 "Return the stable query for the current region.
413 If ONLY-MATCHED is non-nil, include only matched messages. If it
414 is nil, include both matched and unmatched messages."
415 (let ((query-list nil) (all (not only-matched)))
416 (dolist (queries (notmuch-search-properties-in-region :query beg end))
417 (when (first queries)
418 (push (first queries) query-list))
419 (when (and all (second queries))
420 (push (second queries) query-list)))
421 (concat "(" (mapconcat 'identity query-list ") or (") ")")))
423 (defun notmuch-search-find-authors ()
424 "Return the authors for the current thread"
425 (plist-get (notmuch-search-get-result) :authors))
427 (defun notmuch-search-find-authors-region (beg end)
428 "Return a list of authors for the current region"
429 (notmuch-search-properties-in-region :authors beg end))
431 (defun notmuch-search-find-subject ()
432 "Return the subject for the current thread"
433 (plist-get (notmuch-search-get-result) :subject))
435 (defun notmuch-search-find-subject-region (beg end)
436 "Return a list of authors for the current region"
437 (notmuch-search-properties-in-region :subject beg end))
439 (defun notmuch-search-show-thread (&optional elide-toggle)
440 "Display the currently selected thread."
442 (let ((thread-id (notmuch-search-find-thread-id))
443 (subject (notmuch-search-find-subject)))
444 (if (> (length thread-id) 0)
445 (notmuch-show thread-id
448 notmuch-search-query-string
449 ;; Name the buffer based on the subject.
450 (concat "*" (truncate-string-to-width subject 30 nil nil t) "*"))
451 (message "End of search results."))))
453 (defun notmuch-tree-from-search-current-query ()
454 "Call notmuch tree with the current query"
456 (notmuch-tree notmuch-search-query-string))
458 (defun notmuch-tree-from-search-thread ()
459 "Show the selected thread with notmuch-tree"
461 (notmuch-tree (notmuch-search-find-thread-id)
462 notmuch-search-query-string
464 (notmuch-prettify-subject (notmuch-search-find-subject))
467 (defun notmuch-search-reply-to-thread (&optional prompt-for-sender)
468 "Begin composing a reply-all to the entire current thread in a new buffer."
470 (let ((message-id (notmuch-search-find-thread-id)))
471 (notmuch-mua-new-reply message-id prompt-for-sender t)))
473 (defun notmuch-search-reply-to-thread-sender (&optional prompt-for-sender)
474 "Begin composing a reply to the entire current thread in a new buffer."
476 (let ((message-id (notmuch-search-find-thread-id)))
477 (notmuch-mua-new-reply message-id prompt-for-sender nil)))
479 (defun notmuch-search-set-tags (tags &optional pos)
480 (let ((new-result (plist-put (notmuch-search-get-result pos) :tags tags)))
481 (notmuch-search-update-result new-result pos)))
483 (defun notmuch-search-get-tags (&optional pos)
484 (plist-get (notmuch-search-get-result pos) :tags))
486 (defun notmuch-search-get-tags-region (beg end)
488 (notmuch-search-foreach-result beg end
490 (setq output (append output (notmuch-search-get-tags pos)))))
493 (defun notmuch-search-interactive-region ()
494 "Return the bounds of the current interactive region.
496 This returns (BEG END), where BEG and END are the bounds of the
497 region if the region is active, or both `point' otherwise."
498 (if (region-active-p)
499 (list (region-beginning) (region-end))
500 (list (point) (point))))
502 (defun notmuch-search-interactive-tag-changes (&optional initial-input)
503 "Prompt for tag changes for the current thread or region.
505 Returns (TAG-CHANGES REGION-BEGIN REGION-END)."
506 (let* ((region (notmuch-search-interactive-region))
507 (beg (first region)) (end (second region))
508 (prompt (if (= beg end) "Tag thread" "Tag region")))
509 (cons (notmuch-read-tag-changes
510 (notmuch-search-get-tags-region beg end) prompt initial-input)
513 (defun notmuch-search-tag (tag-changes &optional beg end only-matched)
514 "Change tags for the currently selected thread or region.
516 See `notmuch-tag' for information on the format of TAG-CHANGES.
517 When called interactively, this uses the region if the region is
518 active. When called directly, BEG and END provide the region.
519 If these are nil or not provided, this applies to the thread at
522 If ONLY-MATCHED is non-nil, only tag matched messages."
523 (interactive (notmuch-search-interactive-tag-changes))
524 (unless (and beg end) (setq beg (point) end (point)))
525 (let ((search-string (notmuch-search-find-stable-query-region
526 beg end only-matched)))
527 (notmuch-tag search-string tag-changes)
528 (notmuch-search-foreach-result beg end
530 (notmuch-search-set-tags
531 (notmuch-update-tags (notmuch-search-get-tags pos) tag-changes)
534 (defun notmuch-search-add-tag (tag-changes &optional beg end)
535 "Change tags for the current thread or region (defaulting to add).
537 Same as `notmuch-search-tag' but sets initial input to '+'."
538 (interactive (notmuch-search-interactive-tag-changes "+"))
539 (notmuch-search-tag tag-changes beg end))
541 (defun notmuch-search-remove-tag (tag-changes &optional beg end)
542 "Change tags for the current thread or region (defaulting to remove).
544 Same as `notmuch-search-tag' but sets initial input to '-'."
545 (interactive (notmuch-search-interactive-tag-changes "-"))
546 (notmuch-search-tag tag-changes beg end))
548 (put 'notmuch-search-archive-thread 'notmuch-prefix-doc
549 "Un-archive the currently selected thread.")
550 (defun notmuch-search-archive-thread (&optional unarchive beg end)
551 "Archive the currently selected thread or region.
553 Archive each message in the currently selected thread by applying
554 the tag changes in `notmuch-archive-tags' to each (remove the
555 \"inbox\" tag by default). If a prefix argument is given, the
556 messages will be \"unarchived\" (i.e. the tag changes in
557 `notmuch-archive-tags' will be reversed).
559 This function advances the next thread when finished."
560 (interactive (cons current-prefix-arg (notmuch-search-interactive-region)))
561 (when notmuch-archive-tags
563 (notmuch-tag-change-list notmuch-archive-tags unarchive) beg end))
564 (notmuch-search-next-thread))
566 (defun notmuch-search-update-result (result &optional pos)
567 "Replace the result object of the thread at POS (or point) by
568 RESULT and redraw it.
570 This will keep point in a reasonable location. However, if there
571 are enclosing save-excursions and the saved point is in the
572 result being updated, the point will be restored to the beginning
574 (let ((start (notmuch-search-result-beginning pos))
575 (end (notmuch-search-result-end pos))
577 (inhibit-read-only t))
578 ;; Delete the current thread
579 (delete-region start end)
580 ;; Insert the updated thread
581 (notmuch-search-show-result result start)
582 ;; If point was inside the old result, make an educated guess
583 ;; about where to place it now. Unfortunately, this won't work
584 ;; with save-excursion (or any other markers that would be nice to
585 ;; preserve, such as the window start), but there's nothing we can
586 ;; do about that without a way to retrieve markers in a region.
587 (when (and (>= init-point start) (<= init-point end))
588 (let* ((new-end (notmuch-search-result-end start))
589 (new-point (if (= init-point end)
591 (min init-point (- new-end 1)))))
592 (goto-char new-point)))))
594 (defun notmuch-search-process-sentinel (proc msg)
595 "Add a message to let user know when \"notmuch search\" exits"
596 (let ((buffer (process-buffer proc))
597 (status (process-status proc))
598 (exit-status (process-exit-status proc))
599 (never-found-target-thread nil))
600 (when (memq status '(exit signal))
602 (kill-buffer (process-get proc 'parse-buf))
603 (if (buffer-live-p buffer)
604 (with-current-buffer buffer
606 (let ((inhibit-read-only t)
608 (goto-char (point-max))
609 (if (eq status 'signal)
610 (insert "Incomplete search results (search process was killed).\n"))
611 (when (eq status 'exit)
612 (insert "End of search results.\n")
613 ;; For version mismatch, there's no point in
614 ;; showing the search buffer
615 (when (or (= exit-status 20) (= exit-status 21))
619 (not (string= notmuch-search-target-thread "found")))
620 (set 'never-found-target-thread t)))))
621 (when (and never-found-target-thread
622 notmuch-search-target-line)
623 (goto-char (point-min))
624 (forward-line (1- notmuch-search-target-line)))))))))
626 (defcustom notmuch-search-line-faces '(("unread" :weight bold)
627 ("flagged" :foreground "blue"))
628 "Tag/face mapping for line highlighting in notmuch-search.
630 Here is an example of how to color search results based on tags.
631 (the following text would be placed in your ~/.emacs file):
633 (setq notmuch-search-line-faces '((\"deleted\" . (:foreground \"red\"
634 :background \"blue\"))
635 (\"unread\" . (:foreground \"green\"))))
637 The attributes defined for matching tags are merged, with later
638 attributes overriding earlier. A message having both \"deleted\"
639 and \"unread\" tags with the above settings would have a green
640 foreground and blue background."
641 :type '(alist :key-type (string) :value-type (custom-face-edit))
642 :group 'notmuch-search
643 :group 'notmuch-faces)
645 (defun notmuch-search-color-line (start end line-tag-list)
646 "Colorize lines in `notmuch-show' based on tags."
648 (let ((tag (car elem))
649 (attributes (cdr elem)))
650 (when (member tag line-tag-list)
651 (notmuch-combine-face-text-property start end attributes))))
652 ;; Reverse the list so earlier entries take precedence
653 (reverse notmuch-search-line-faces)))
655 (defun notmuch-search-author-propertize (authors)
656 "Split `authors' into matching and non-matching authors and
657 propertize appropriately. If no boundary between authors and
658 non-authors is found, assume that all of the authors match."
659 (if (string-match "\\(.*\\)|\\(.*\\)" authors)
660 (concat (propertize (concat (match-string 1 authors) ",")
661 'face 'notmuch-search-matching-authors)
662 (propertize (match-string 2 authors)
663 'face 'notmuch-search-non-matching-authors))
664 (propertize authors 'face 'notmuch-search-matching-authors)))
666 (defun notmuch-search-insert-authors (format-string authors)
667 ;; Save the match data to avoid interfering with
668 ;; `notmuch-search-process-filter'.
670 (let* ((formatted-authors (format format-string authors))
671 (formatted-sample (format format-string ""))
672 (visible-string formatted-authors)
673 (invisible-string "")
676 ;; Truncate the author string to fit the specification.
677 (if (> (length formatted-authors)
678 (length formatted-sample))
679 (let ((visible-length (- (length formatted-sample)
681 ;; Truncate the visible string according to the width of
682 ;; the display string.
683 (setq visible-string (substring formatted-authors 0 visible-length)
684 invisible-string (substring formatted-authors visible-length))
685 ;; If possible, truncate the visible string at a natural
686 ;; break (comma or pipe), as incremental search doesn't
687 ;; match across the visible/invisible border.
688 (when (string-match "\\(.*\\)\\([,|] \\)\\([^,|]*\\)" visible-string)
689 ;; Second clause is destructive on `visible-string', so
690 ;; order is important.
691 (setq invisible-string (concat (match-string 3 visible-string)
693 visible-string (concat (match-string 1 visible-string)
694 (match-string 2 visible-string))))
695 ;; `visible-string' may be shorter than the space allowed
696 ;; by `format-string'. If so we must insert some padding
697 ;; after `invisible-string'.
698 (setq padding (make-string (- (length formatted-sample)
699 (length visible-string)
703 ;; Use different faces to show matching and non-matching authors.
704 (if (string-match "\\(.*\\)|\\(.*\\)" visible-string)
705 ;; The visible string contains both matching and
706 ;; non-matching authors.
707 (setq visible-string (notmuch-search-author-propertize visible-string)
708 ;; The invisible string must contain only non-matching
709 ;; authors, as the visible-string contains both.
710 invisible-string (propertize invisible-string
711 'face 'notmuch-search-non-matching-authors))
712 ;; The visible string contains only matching authors.
713 (setq visible-string (propertize visible-string
714 'face 'notmuch-search-matching-authors)
715 ;; The invisible string may contain both matching and
716 ;; non-matching authors.
717 invisible-string (notmuch-search-author-propertize invisible-string)))
719 ;; If there is any invisible text, add it as a tooltip to the
721 (when (not (string= invisible-string ""))
722 (setq visible-string (propertize visible-string 'help-echo (concat "..." invisible-string))))
724 ;; Insert the visible and, if present, invisible author strings.
725 (insert visible-string)
726 (when (not (string= invisible-string ""))
727 (let ((start (point))
729 (insert invisible-string)
730 (setq overlay (make-overlay start (point)))
731 (overlay-put overlay 'invisible 'ellipsis)
732 (overlay-put overlay 'isearch-open-invisible #'delete-overlay)))
735 (defun notmuch-search-insert-field (field format-string result)
737 ((string-equal field "date")
738 (insert (propertize (format format-string (plist-get result :date_relative))
739 'face 'notmuch-search-date)))
740 ((string-equal field "count")
741 (insert (propertize (format format-string
742 (format "[%s/%s]" (plist-get result :matched)
743 (plist-get result :total)))
744 'face 'notmuch-search-count)))
745 ((string-equal field "subject")
746 (insert (propertize (format format-string
747 (notmuch-sanitize (plist-get result :subject)))
748 'face 'notmuch-search-subject)))
750 ((string-equal field "authors")
751 (notmuch-search-insert-authors
752 format-string (notmuch-sanitize (plist-get result :authors))))
754 ((string-equal field "tags")
755 (let ((tags (plist-get result :tags)))
756 (insert (format format-string (notmuch-tag-format-tags tags)))))))
758 (defun notmuch-search-show-result (result &optional pos)
759 "Insert RESULT at POS or the end of the buffer if POS is null."
760 ;; Ignore excluded matches
761 (unless (= (plist-get result :matched) 0)
762 (let ((beg (or pos (point-max))))
765 (dolist (spec notmuch-search-result-format)
766 (notmuch-search-insert-field (car spec) (cdr spec) result))
768 (notmuch-search-color-line beg (point) (plist-get result :tags))
769 (put-text-property beg (point) 'notmuch-search-result result))
770 (when (string= (plist-get result :thread) notmuch-search-target-thread)
771 (setq notmuch-search-target-thread "found")
774 (defun notmuch-search-process-filter (proc string)
775 "Process and filter the output of \"notmuch search\""
776 (let ((results-buf (process-buffer proc))
777 (parse-buf (process-get proc 'parse-buf))
778 (inhibit-read-only t)
780 (when (buffer-live-p results-buf)
781 (with-current-buffer parse-buf
784 (goto-char (point-max))
786 (notmuch-sexp-parse-partial-list 'notmuch-search-show-result
789 (defun notmuch-search-tag-all (tag-changes)
790 "Add/remove tags from all messages in current search buffer.
792 See `notmuch-tag' for information on the format of TAG-CHANGES."
794 (list (notmuch-read-tag-changes
795 (notmuch-search-get-tags-region (point-min) (point-max)) "Tag all")))
796 (notmuch-search-tag tag-changes (point-min) (point-max) t))
798 (defun notmuch-search-buffer-title (query)
799 "Returns the title for a buffer with notmuch search results."
803 (loop for tuple in notmuch-saved-searches
804 if (let ((quoted-query (regexp-quote (cdr tuple))))
805 (and (string-match (concat "^" quoted-query) query)
806 (> (length (match-string 0 query))
808 do (setq longest tuple))
810 (saved-search-name (car saved-search))
811 (saved-search-query (cdr saved-search)))
812 (cond ((and saved-search (equal saved-search-query query))
813 ;; Query is the same as saved search (ignoring case)
814 (concat "*notmuch-saved-search-" saved-search-name "*"))
816 (concat "*notmuch-search-"
817 (replace-regexp-in-string (concat "^" (regexp-quote saved-search-query))
818 (concat "[ " saved-search-name " ]")
822 (concat "*notmuch-search-" query "*"))
825 (defun notmuch-read-query (prompt)
826 "Read a notmuch-query from the minibuffer with completion.
828 PROMPT is the string to prompt with."
831 (append (list "folder:" "thread:" "id:" "date:" "from:" "to:"
832 "subject:" "attachment:")
833 (mapcar (lambda (tag)
834 (concat "tag:" (notmuch-escape-boolean-term tag)))
835 (process-lines notmuch-command "search" "--output=tags" "*")))))
836 (let ((keymap (copy-keymap minibuffer-local-map))
837 (minibuffer-completion-table
838 (completion-table-dynamic
840 ;; generate a list of possible completions for the current input
842 ;; this ugly regexp is used to get the last word of the input
843 ;; possibly preceded by a '('
844 ((string-match "\\(^\\|.* (?\\)\\([^ ]*\\)$" string)
845 (mapcar (lambda (compl)
846 (concat (match-string-no-properties 1 string) compl))
847 (all-completions (match-string-no-properties 2 string)
849 (t (list string)))))))
850 ;; this was simpler than convincing completing-read to accept spaces:
851 (define-key keymap (kbd "TAB") 'minibuffer-complete)
852 (let ((history-delete-duplicates t))
853 (read-from-minibuffer prompt nil keymap nil
854 'notmuch-search-history nil nil)))))
857 (put 'notmuch-search 'notmuch-doc "Search for messages.")
858 (defun notmuch-search (&optional query oldest-first target-thread target-line)
859 "Display threads matching QUERY in a notmuch-search buffer.
861 If QUERY is nil, it is read interactively from the minibuffer.
862 Other optional parameters are used as follows:
864 OLDEST-FIRST: A Boolean controlling the sort order of returned threads
865 TARGET-THREAD: A thread ID (without the thread: prefix) that will be made
866 current if it appears in the search results.
867 TARGET-LINE: The line number to move to if the target thread does not
868 appear in the search results.
870 When called interactively, this will prompt for a query and use
871 the configured default sort order."
874 ;; Prompt for a query
876 ;; Use the default search order (if we're doing a search from a
877 ;; search buffer, ignore any buffer-local overrides)
878 (default-value 'notmuch-search-oldest-first)))
880 (let* ((query (or query (notmuch-read-query "Notmuch search: ")))
881 (buffer (get-buffer-create (notmuch-search-buffer-title query))))
882 (switch-to-buffer buffer)
883 (notmuch-search-mode)
884 ;; Don't track undo information for this buffer
885 (set 'buffer-undo-list t)
886 (set 'notmuch-search-query-string query)
887 (set 'notmuch-search-oldest-first oldest-first)
888 (set 'notmuch-search-target-thread target-thread)
889 (set 'notmuch-search-target-line target-line)
890 (let ((proc (get-buffer-process (current-buffer)))
891 (inhibit-read-only t))
893 (error "notmuch search process already running for query `%s'" query)
896 (goto-char (point-min))
898 (let ((proc (notmuch-start-notmuch
899 "notmuch-search" buffer #'notmuch-search-process-sentinel
900 "search" "--format=sexp" "--format-version=2"
902 "--sort=oldest-first"
903 "--sort=newest-first")
905 ;; Use a scratch buffer to accumulate partial output.
906 ;; This buffer will be killed by the sentinel, which
907 ;; should be called no matter how the process dies.
908 (parse-buf (generate-new-buffer " *notmuch search parse*")))
909 (process-put proc 'parse-buf parse-buf)
910 (set-process-filter proc 'notmuch-search-process-filter)
911 (set-process-query-on-exit-flag proc nil))))
912 (run-hooks 'notmuch-search-hook)))
914 (defun notmuch-search-refresh-view ()
915 "Refresh the current view.
917 Kills the current buffer and runs a new search with the same
918 query string as the current search. If the current thread is in
919 the new search results, then point will be placed on the same
920 thread. Otherwise, point will be moved to attempt to be in the
921 same relative position within the new buffer."
922 (let ((target-line (line-number-at-pos))
923 (oldest-first notmuch-search-oldest-first)
924 (target-thread (notmuch-search-find-thread-id 'bare))
925 (query notmuch-search-query-string))
926 (notmuch-kill-this-buffer)
927 (notmuch-search query oldest-first target-thread target-line)
928 (goto-char (point-min))))
930 (defun notmuch-search-toggle-order ()
931 "Toggle the current search order.
933 This command toggles the sort order for the current search. The
934 default sort order is defined by `notmuch-search-oldest-first'."
936 (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
937 (notmuch-search-refresh-view))
939 (defun notmuch-search-filter (query)
940 "Filter the current search results based on an additional query string.
942 Runs a new search matching only messages that match both the
943 current search results AND the additional query string provided."
944 (interactive (list (notmuch-read-query "Filter search: ")))
945 (let ((grouped-query (if (string-match-p notmuch-search-disjunctive-regexp query)
946 (concat "( " query " )")
948 (notmuch-search (if (string= notmuch-search-query-string "*")
950 (concat notmuch-search-query-string " and " grouped-query)) notmuch-search-oldest-first)))
952 (defun notmuch-search-filter-by-tag (tag)
953 "Filter the current search results based on a single tag.
955 Runs a new search matching only messages that match both the
956 current search results AND that are tagged with the given tag."
958 (list (notmuch-select-tag-with-completion "Filter by tag: ")))
959 (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
963 "Run notmuch and display saved searches, known tags, etc."
967 (defun notmuch-interesting-buffer (b)
968 "Is the current buffer of interest to a notmuch user?"
969 (with-current-buffer b
970 (memq major-mode '(notmuch-show-mode
976 (defun notmuch-cycle-notmuch-buffers ()
977 "Cycle through any existing notmuch buffers (search, show or hello).
979 If the current buffer is the only notmuch buffer, bury it. If no
980 notmuch buffers exist, run `notmuch'."
984 ;; If the current buffer is a notmuch buffer, remember it and then
986 (when (notmuch-interesting-buffer (current-buffer))
987 (setq start (current-buffer))
990 ;; Find the first notmuch buffer.
991 (setq first (loop for buffer in (buffer-list)
992 if (notmuch-interesting-buffer buffer)
996 ;; If the first one we found is any other than the starting
997 ;; buffer, switch to it.
998 (unless (eq first start)
999 (switch-to-buffer first))
1002 (setq mail-user-agent 'notmuch-user-agent)