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-mua)
58 (require 'notmuch-hello)
59 (require 'notmuch-maildir-fcc)
60 (require 'notmuch-message)
61 (require 'notmuch-parser)
63 (defcustom notmuch-search-result-format
66 ("authors" . "%-20s ")
69 "Search result formatting. Supported fields are:
70 date, count, authors, subject, tags
72 (setq notmuch-search-result-format \(\(\"authors\" . \"%-40s\"\)
73 \(\"subject\" . \"%s\"\)\)\)
74 Line breaks are permitted in format strings (though this is
75 currently experimental). Note that a line break at the end of an
76 \"authors\" field will get elided if the authors list is long;
77 place it instead at the beginning of the following field. To
78 enter a line break when setting this variable with setq, use \\n.
79 To enter a line break in customize, press \\[quoted-insert] C-j."
80 :type '(alist :key-type (string) :value-type (string))
81 :group 'notmuch-search)
83 (defvar notmuch-query-history nil
84 "Variable to store minibuffer history for notmuch queries")
86 (defun notmuch-foreach-mime-part (function mm-handle)
87 (cond ((stringp (car mm-handle))
88 (dolist (part (cdr mm-handle))
89 (notmuch-foreach-mime-part function part)))
90 ((bufferp (car mm-handle))
91 (funcall function mm-handle))
92 (t (dolist (part mm-handle)
93 (notmuch-foreach-mime-part function part)))))
95 (defun notmuch-count-attachments (mm-handle)
97 (notmuch-foreach-mime-part
99 (let ((disposition (mm-handle-disposition p)))
100 (and (listp disposition)
101 (or (equal (car disposition) "attachment")
102 (and (equal (car disposition) "inline")
103 (assq 'filename disposition)))
108 (defun notmuch-save-attachments (mm-handle &optional queryp)
109 (notmuch-foreach-mime-part
111 (let ((disposition (mm-handle-disposition p)))
112 (and (listp disposition)
113 (or (equal (car disposition) "attachment")
114 (and (equal (car disposition) "inline")
115 (assq 'filename disposition)))
118 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
122 (defun notmuch-documentation-first-line (symbol)
123 "Return the first line of the documentation string for SYMBOL."
124 (let ((doc (documentation symbol)))
127 (insert (documentation symbol t))
128 (goto-char (point-min))
131 (buffer-substring beg (point))))
134 (defun notmuch-prefix-key-description (key)
135 "Given a prefix key code, return a human-readable string representation.
137 This is basically just `format-kbd-macro' but we also convert ESC to M-."
138 (let ((desc (format-kbd-macro (vector key))))
139 (if (string= desc "ESC")
143 (defun notmuch-describe-keymap (keymap &optional prefix tail)
144 "Return a list of strings, each describing one key in KEYMAP.
146 Each string gives a human-readable description of the key and the
147 first line of documentation for the bound function."
149 (lambda (key binding)
150 (cond ((mouse-event-p key) nil)
153 (notmuch-describe-keymap
154 binding (notmuch-prefix-key-description key) tail)))
156 (push (concat prefix (format-kbd-macro (vector key)) "\t"
157 (notmuch-documentation-first-line binding))
162 (defun notmuch-substitute-command-keys (doc)
163 "Like `substitute-command-keys' but with documentation, not function names."
165 (while (string-match "\\\\{\\([^}[:space:]]*\\)}" doc beg)
166 (let* ((keymap-name (substring doc (match-beginning 1) (match-end 1)))
167 (keymap (symbol-value (intern keymap-name)))
168 (desc-list (notmuch-describe-keymap keymap))
169 (desc (mapconcat #'identity desc-list "\n")))
170 (setq doc (replace-match desc 1 1 doc)))
171 (setq beg (match-end 0)))
174 (defun notmuch-help ()
175 "Display help for the current notmuch mode."
177 (let* ((mode major-mode)
178 (doc (substitute-command-keys (notmuch-substitute-command-keys (documentation mode t)))))
179 (with-current-buffer (generate-new-buffer "*notmuch-help*")
181 (goto-char (point-min))
182 (set-buffer-modified-p nil)
183 (view-buffer (current-buffer) 'kill-buffer-if-not-modified))))
187 (defun notmuch-hl-line-mode ()
188 (prog1 (hl-line-mode)
189 (when hl-line-overlay
190 (overlay-put hl-line-overlay 'priority 1))))
192 (defcustom notmuch-search-hook '(notmuch-hl-line-mode)
193 "List of functions to call when notmuch displays the search results."
195 :options '(notmuch-hl-line-mode)
196 :group 'notmuch-search
197 :group 'notmuch-hooks)
199 (defvar notmuch-search-mode-map
200 (let ((map (make-sparse-keymap)))
201 (set-keymap-parent map notmuch-common-keymap)
202 (define-key map "x" 'notmuch-kill-this-buffer)
203 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
204 (define-key map "b" 'notmuch-search-scroll-down)
205 (define-key map " " 'notmuch-search-scroll-up)
206 (define-key map "<" 'notmuch-search-first-thread)
207 (define-key map ">" 'notmuch-search-last-thread)
208 (define-key map "p" 'notmuch-search-previous-thread)
209 (define-key map "n" 'notmuch-search-next-thread)
210 (define-key map "r" 'notmuch-search-reply-to-thread-sender)
211 (define-key map "R" 'notmuch-search-reply-to-thread)
212 (define-key map "o" 'notmuch-search-toggle-order)
213 (define-key map "c" 'notmuch-search-stash-map)
214 (define-key map "t" 'notmuch-search-filter-by-tag)
215 (define-key map "f" 'notmuch-search-filter)
216 (define-key map [mouse-1] 'notmuch-search-show-thread)
217 (define-key map "*" 'notmuch-search-tag-all)
218 (define-key map "a" 'notmuch-search-archive-thread)
219 (define-key map "-" 'notmuch-search-remove-tag)
220 (define-key map "+" 'notmuch-search-add-tag)
221 (define-key map (kbd "RET") 'notmuch-search-show-thread)
223 "Keymap for \"notmuch search\" buffers.")
224 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
226 (defvar notmuch-search-stash-map
227 (let ((map (make-sparse-keymap)))
228 (define-key map "i" 'notmuch-search-stash-thread-id)
230 "Submap for stash commands")
231 (fset 'notmuch-search-stash-map notmuch-search-stash-map)
233 (defun notmuch-search-stash-thread-id ()
234 "Copy thread ID of current thread to kill-ring."
236 (notmuch-common-do-stash (notmuch-search-find-thread-id)))
238 (defvar notmuch-search-query-string)
239 (defvar notmuch-search-target-thread)
240 (defvar notmuch-search-target-line)
242 (defvar notmuch-search-disjunctive-regexp "\\<[oO][rR]\\>")
244 (defun notmuch-search-scroll-up ()
245 "Move forward through search results by one window's worth."
249 ((end-of-buffer) (notmuch-search-last-thread))))
251 (defun notmuch-search-scroll-down ()
252 "Move backward through the search results by one window's worth."
254 ;; I don't know why scroll-down doesn't signal beginning-of-buffer
255 ;; the way that scroll-up signals end-of-buffer, but c'est la vie.
257 ;; So instead of trapping a signal we instead check whether the
258 ;; window begins on the first line of the buffer and if so, move
259 ;; directly to that position. (We have to count lines since the
260 ;; window-start position is not the same as point-min due to the
261 ;; invisible thread-ID characters on the first line.
262 (if (equal (count-lines (point-min) (window-start)) 0)
263 (goto-char (point-min))
266 (defun notmuch-search-next-thread ()
267 "Select the next thread in the search results."
269 (when (notmuch-search-get-result)
270 (goto-char (notmuch-search-result-end))))
272 (defun notmuch-search-previous-thread ()
273 "Select the previous thread in the search results."
275 (if (notmuch-search-get-result)
277 (goto-char (notmuch-search-result-beginning (- (point) 1))))
278 ;; We must be past the end; jump to the last result
279 (notmuch-search-last-thread)))
281 (defun notmuch-search-last-thread ()
282 "Select the last thread in the search results."
284 (goto-char (point-max))
286 (let ((beg (notmuch-search-result-beginning)))
287 (when beg (goto-char beg))))
289 (defun notmuch-search-first-thread ()
290 "Select the first thread in the search results."
292 (goto-char (point-min)))
294 (defface notmuch-message-summary-face
295 '((((class color) (background light)) (:background "#f0f0f0"))
296 (((class color) (background dark)) (:background "#303030")))
297 "Face for the single-line message summary in notmuch-show-mode."
299 :group 'notmuch-faces)
301 (defface notmuch-search-date
302 '((t :inherit default))
303 "Face used in search mode for dates."
304 :group 'notmuch-search
305 :group 'notmuch-faces)
307 (defface notmuch-search-count
308 '((t :inherit default))
309 "Face used in search mode for the count matching the query."
310 :group 'notmuch-search
311 :group 'notmuch-faces)
313 (defface notmuch-search-subject
314 '((t :inherit default))
315 "Face used in search mode for subjects."
316 :group 'notmuch-search
317 :group 'notmuch-faces)
319 (defface notmuch-search-matching-authors
320 '((t :inherit default))
321 "Face used in search mode for authors matching the query."
322 :group 'notmuch-search
323 :group 'notmuch-faces)
325 (defface notmuch-search-non-matching-authors
328 (:foreground "grey30"))
331 (:foreground "grey60"))
334 "Face used in search mode for authors not matching the query."
335 :group 'notmuch-search
336 :group 'notmuch-faces)
338 (defface notmuch-tag-face
341 (:foreground "OliveDrab1"))
344 (:foreground "navy blue" :bold t))
347 "Face used in search mode face for tags."
348 :group 'notmuch-search
349 :group 'notmuch-faces)
351 (defun notmuch-search-mode ()
352 "Major mode displaying results of a notmuch search.
354 This buffer contains the results of a \"notmuch search\" of your
355 email archives. Each line in the buffer represents a single
356 thread giving a summary of the thread (a relative date, the
357 number of matched messages and total messages in the thread,
358 participants in the thread, a representative subject line, and
361 Pressing \\[notmuch-search-show-thread] on any line displays that
362 thread. The '\\[notmuch-search-add-tag]' and
363 '\\[notmuch-search-remove-tag]' keys can be used to add or remove
364 tags from a thread. The '\\[notmuch-search-archive-thread]' key
365 is a convenience for archiving a thread (applying changes in
366 `notmuch-archive-tags'). The '\\[notmuch-search-tag-all]' key can
367 be used to add and/or remove tags from all messages (as opposed
368 to threads) that match the current query. Use with caution, as
369 this will also tag matching messages that arrived *after*
370 constructing the buffer.
372 Other useful commands are '\\[notmuch-search-filter]' for
373 filtering the current search based on an additional query string,
374 '\\[notmuch-search-filter-by-tag]' for filtering to include only
375 messages with a given tag, and '\\[notmuch-search]' to execute a
378 Complete list of currently available key bindings:
380 \\{notmuch-search-mode-map}"
382 (kill-all-local-variables)
383 (make-local-variable 'notmuch-search-query-string)
384 (make-local-variable 'notmuch-search-oldest-first)
385 (make-local-variable 'notmuch-search-target-thread)
386 (make-local-variable 'notmuch-search-target-line)
387 (setq notmuch-buffer-refresh-function #'notmuch-search-refresh-view)
388 (set (make-local-variable 'scroll-preserve-screen-position) t)
389 (add-to-invisibility-spec (cons 'ellipsis t))
390 (use-local-map notmuch-search-mode-map)
391 (setq truncate-lines t)
392 (setq major-mode 'notmuch-search-mode
393 mode-name "notmuch-search")
394 (setq buffer-read-only t))
396 (defun notmuch-search-get-result (&optional pos)
397 "Return the result object for the thread at POS (or point).
399 If there is no thread at POS (or point), returns nil."
400 (get-text-property (or pos (point)) 'notmuch-search-result))
402 (defun notmuch-search-result-beginning (&optional pos)
403 "Return the point at the beginning of the thread at POS (or point).
405 If there is no thread at POS (or point), returns nil."
406 (when (notmuch-search-get-result pos)
407 ;; We pass 1+point because previous-single-property-change starts
408 ;; searching one before the position we give it.
409 (previous-single-property-change (1+ (or pos (point)))
410 'notmuch-search-result nil (point-min))))
412 (defun notmuch-search-result-end (&optional pos)
413 "Return the point at the end of the thread at POS (or point).
415 The returned point will be just after the newline character that
416 ends the result line. If there is no thread at POS (or point),
418 (when (notmuch-search-get-result pos)
419 (next-single-property-change (or pos (point)) 'notmuch-search-result
422 (defun notmuch-search-foreach-result (beg end function)
423 "Invoke FUNCTION for each result between BEG and END.
425 FUNCTION should take one argument. It will be applied to the
426 character position of the beginning of each result that overlaps
427 the region between points BEG and END. As a special case, if (=
428 BEG END), FUNCTION will be applied to the result containing point
431 (lexical-let ((pos (notmuch-search-result-beginning beg))
432 ;; End must be a marker in case function changes the
434 (end (copy-marker end))
435 ;; Make sure we examine at least one result, even if
438 ;; We have to be careful if the region extends beyond the results.
439 ;; In this case, pos could be null or there could be no result at
441 (while (and pos (or (< pos end) first))
442 (when (notmuch-search-get-result pos)
443 (funcall function pos))
444 (setq pos (notmuch-search-result-end pos)
446 ;; Unindent the function argument of notmuch-search-foreach-result so
447 ;; the indentation of callers doesn't get out of hand.
448 (put 'notmuch-search-foreach-result 'lisp-indent-function 2)
450 (defun notmuch-search-properties-in-region (property beg end)
452 (notmuch-search-foreach-result beg end
454 (push (plist-get (notmuch-search-get-result pos) property) output)))
457 (defun notmuch-search-find-thread-id (&optional bare)
458 "Return the thread for the current thread
460 If BARE is set then do not prefix with \"thread:\""
461 (let ((thread (plist-get (notmuch-search-get-result) :thread)))
462 (when thread (concat (unless bare "thread:") thread))))
464 (defun notmuch-search-find-thread-id-region (beg end)
465 "Return a list of threads for the current region"
466 (mapcar (lambda (thread) (concat "thread:" thread))
467 (notmuch-search-properties-in-region :thread beg end)))
469 (defun notmuch-search-find-thread-id-region-search (beg end)
470 "Return a search string for threads for the current region"
471 (mapconcat 'identity (notmuch-search-find-thread-id-region beg end) " or "))
473 (defun notmuch-search-find-authors ()
474 "Return the authors for the current thread"
475 (plist-get (notmuch-search-get-result) :authors))
477 (defun notmuch-search-find-authors-region (beg end)
478 "Return a list of authors for the current region"
479 (notmuch-search-properties-in-region :authors beg end))
481 (defun notmuch-search-find-subject ()
482 "Return the subject for the current thread"
483 (plist-get (notmuch-search-get-result) :subject))
485 (defun notmuch-search-find-subject-region (beg end)
486 "Return a list of authors for the current region"
487 (notmuch-search-properties-in-region :subject beg end))
489 (defun notmuch-search-show-thread ()
490 "Display the currently selected thread."
492 (let ((thread-id (notmuch-search-find-thread-id))
493 (subject (notmuch-search-find-subject)))
494 (if (> (length thread-id) 0)
495 (notmuch-show thread-id
497 notmuch-search-query-string
498 ;; Name the buffer based on the subject.
499 (concat "*" (truncate-string-to-width subject 30 nil nil t) "*"))
500 (message "End of search results."))))
502 (defun notmuch-search-reply-to-thread (&optional prompt-for-sender)
503 "Begin composing a reply-all to the entire current thread in a new buffer."
505 (let ((message-id (notmuch-search-find-thread-id)))
506 (notmuch-mua-new-reply message-id prompt-for-sender t)))
508 (defun notmuch-search-reply-to-thread-sender (&optional prompt-for-sender)
509 "Begin composing a reply to the entire current thread in a new buffer."
511 (let ((message-id (notmuch-search-find-thread-id)))
512 (notmuch-mua-new-reply message-id prompt-for-sender nil)))
514 (defun notmuch-call-notmuch-process (&rest args)
515 "Synchronously invoke \"notmuch\" with the given list of arguments.
517 If notmuch exits with a non-zero status, output from the process
518 will appear in a buffer named \"*Notmuch errors*\" and an error
521 (let ((status (apply #'call-process notmuch-command nil t nil args)))
522 (notmuch-check-exit-status status (cons notmuch-command args)
525 (defun notmuch-search-set-tags (tags &optional pos)
526 (let ((new-result (plist-put (notmuch-search-get-result pos) :tags tags)))
527 (notmuch-search-update-result new-result pos)))
529 (defun notmuch-search-get-tags (&optional pos)
530 (plist-get (notmuch-search-get-result pos) :tags))
532 (defun notmuch-search-get-tags-region (beg end)
534 (notmuch-search-foreach-result beg end
536 (setq output (append output (notmuch-search-get-tags pos)))))
539 (defun notmuch-search-tag-region (beg end &optional tag-changes)
540 "Change tags for threads in the given region."
541 (let ((search-string (notmuch-search-find-thread-id-region-search beg end)))
542 (setq tag-changes (notmuch-tag search-string tag-changes))
543 (notmuch-search-foreach-result beg end
545 (notmuch-search-set-tags
546 (notmuch-update-tags (notmuch-search-get-tags pos) tag-changes)
549 (defun notmuch-search-tag (&optional tag-changes)
550 "Change tags for the currently selected thread or region.
552 See `notmuch-tag' for information on the format of TAG-CHANGES."
554 (let* ((beg (if (region-active-p) (region-beginning) (point)))
555 (end (if (region-active-p) (region-end) (point))))
556 (notmuch-search-tag-region beg end tag-changes)))
558 (defun notmuch-search-add-tag ()
559 "Same as `notmuch-search-tag' but sets initial input to '+'."
561 (notmuch-search-tag "+"))
563 (defun notmuch-search-remove-tag ()
564 "Same as `notmuch-search-tag' but sets initial input to '-'."
566 (notmuch-search-tag "-"))
568 (defun notmuch-search-archive-thread (&optional unarchive)
569 "Archive the currently selected thread.
571 Archive each message in the currently selected thread by applying
572 the tag changes in `notmuch-archive-tags' to each (remove the
573 \"inbox\" tag by default). If a prefix argument is given, the
574 messages will be \"unarchived\" (i.e. the tag changes in
575 `notmuch-archive-tags' will be reversed).
577 This function advances the next thread when finished."
579 (when notmuch-archive-tags
581 (notmuch-tag-change-list notmuch-archive-tags unarchive)))
582 (notmuch-search-next-thread))
584 (defun notmuch-search-update-result (result &optional pos)
585 "Replace the result object of the thread at POS (or point) by
586 RESULT and redraw it.
588 This will keep point in a reasonable location. However, if there
589 are enclosing save-excursions and the saved point is in the
590 result being updated, the point will be restored to the beginning
592 (let ((start (notmuch-search-result-beginning pos))
593 (end (notmuch-search-result-end pos))
595 (inhibit-read-only t))
596 ;; Delete the current thread
597 (delete-region start end)
598 ;; Insert the updated thread
599 (notmuch-search-show-result result start)
600 ;; If point was inside the old result, make an educated guess
601 ;; about where to place it now. Unfortunately, this won't work
602 ;; with save-excursion (or any other markers that would be nice to
603 ;; preserve, such as the window start), but there's nothing we can
604 ;; do about that without a way to retrieve markers in a region.
605 (when (and (>= init-point start) (<= init-point end))
606 (let* ((new-end (notmuch-search-result-end start))
607 (new-point (if (= init-point end)
609 (min init-point (- new-end 1)))))
610 (goto-char new-point)))))
612 (defun notmuch-search-process-sentinel (proc msg)
613 "Add a message to let user know when \"notmuch search\" exits"
614 (let ((buffer (process-buffer proc))
615 (status (process-status proc))
616 (exit-status (process-exit-status proc))
617 (never-found-target-thread nil))
618 (when (memq status '(exit signal))
620 (kill-buffer (process-get proc 'parse-buf))
621 (if (buffer-live-p buffer)
622 (with-current-buffer buffer
624 (let ((inhibit-read-only t)
626 (goto-char (point-max))
627 (if (eq status 'signal)
628 (insert "Incomplete search results (search process was killed).\n"))
629 (when (eq status 'exit)
630 (insert "End of search results.\n")
631 ;; For version mismatch, there's no point in
632 ;; showing the search buffer
633 (when (or (= exit-status 20) (= exit-status 21))
637 (not (string= notmuch-search-target-thread "found")))
638 (set 'never-found-target-thread t)))))
639 (when (and never-found-target-thread
640 notmuch-search-target-line)
641 (goto-char (point-min))
642 (forward-line (1- notmuch-search-target-line)))))))))
644 (defcustom notmuch-search-line-faces '(("unread" :weight bold)
645 ("flagged" :foreground "blue"))
646 "Tag/face mapping for line highlighting in notmuch-search.
648 Here is an example of how to color search results based on tags.
649 (the following text would be placed in your ~/.emacs file):
651 (setq notmuch-search-line-faces '((\"deleted\" . (:foreground \"red\"
652 :background \"blue\"))
653 (\"unread\" . (:foreground \"green\"))))
655 The attributes defined for matching tags are merged, with later
656 attributes overriding earlier. A message having both \"deleted\"
657 and \"unread\" tags with the above settings would have a green
658 foreground and blue background."
659 :type '(alist :key-type (string) :value-type (custom-face-edit))
660 :group 'notmuch-search
661 :group 'notmuch-faces)
663 (defun notmuch-search-color-line (start end line-tag-list)
664 "Colorize lines in `notmuch-show' based on tags."
666 (let ((tag (car elem))
667 (attributes (cdr elem)))
668 (when (member tag line-tag-list)
669 (notmuch-combine-face-text-property start end attributes))))
670 ;; Reverse the list so earlier entries take precedence
671 (reverse notmuch-search-line-faces)))
673 (defun notmuch-search-author-propertize (authors)
674 "Split `authors' into matching and non-matching authors and
675 propertize appropriately. If no boundary between authors and
676 non-authors is found, assume that all of the authors match."
677 (if (string-match "\\(.*\\)|\\(.*\\)" authors)
678 (concat (propertize (concat (match-string 1 authors) ",")
679 'face 'notmuch-search-matching-authors)
680 (propertize (match-string 2 authors)
681 'face 'notmuch-search-non-matching-authors))
682 (propertize authors 'face 'notmuch-search-matching-authors)))
684 (defun notmuch-search-insert-authors (format-string authors)
685 ;; Save the match data to avoid interfering with
686 ;; `notmuch-search-process-filter'.
688 (let* ((formatted-authors (format format-string authors))
689 (formatted-sample (format format-string ""))
690 (visible-string formatted-authors)
691 (invisible-string "")
694 ;; Truncate the author string to fit the specification.
695 (if (> (length formatted-authors)
696 (length formatted-sample))
697 (let ((visible-length (- (length formatted-sample)
699 ;; Truncate the visible string according to the width of
700 ;; the display string.
701 (setq visible-string (substring formatted-authors 0 visible-length)
702 invisible-string (substring formatted-authors visible-length))
703 ;; If possible, truncate the visible string at a natural
704 ;; break (comma or pipe), as incremental search doesn't
705 ;; match across the visible/invisible border.
706 (when (string-match "\\(.*\\)\\([,|] \\)\\([^,|]*\\)" visible-string)
707 ;; Second clause is destructive on `visible-string', so
708 ;; order is important.
709 (setq invisible-string (concat (match-string 3 visible-string)
711 visible-string (concat (match-string 1 visible-string)
712 (match-string 2 visible-string))))
713 ;; `visible-string' may be shorter than the space allowed
714 ;; by `format-string'. If so we must insert some padding
715 ;; after `invisible-string'.
716 (setq padding (make-string (- (length formatted-sample)
717 (length visible-string)
721 ;; Use different faces to show matching and non-matching authors.
722 (if (string-match "\\(.*\\)|\\(.*\\)" visible-string)
723 ;; The visible string contains both matching and
724 ;; non-matching authors.
725 (setq visible-string (notmuch-search-author-propertize visible-string)
726 ;; The invisible string must contain only non-matching
727 ;; authors, as the visible-string contains both.
728 invisible-string (propertize invisible-string
729 'face 'notmuch-search-non-matching-authors))
730 ;; The visible string contains only matching authors.
731 (setq visible-string (propertize visible-string
732 'face 'notmuch-search-matching-authors)
733 ;; The invisible string may contain both matching and
734 ;; non-matching authors.
735 invisible-string (notmuch-search-author-propertize invisible-string)))
737 ;; If there is any invisible text, add it as a tooltip to the
739 (when (not (string= invisible-string ""))
740 (setq visible-string (propertize visible-string 'help-echo (concat "..." invisible-string))))
742 ;; Insert the visible and, if present, invisible author strings.
743 (insert visible-string)
744 (when (not (string= invisible-string ""))
745 (let ((start (point))
747 (insert invisible-string)
748 (setq overlay (make-overlay start (point)))
749 (overlay-put overlay 'invisible 'ellipsis)
750 (overlay-put overlay 'isearch-open-invisible #'delete-overlay)))
753 (defun notmuch-search-insert-field (field format-string result)
755 ((string-equal field "date")
756 (insert (propertize (format format-string (plist-get result :date_relative))
757 'face 'notmuch-search-date)))
758 ((string-equal field "count")
759 (insert (propertize (format format-string
760 (format "[%s/%s]" (plist-get result :matched)
761 (plist-get result :total)))
762 'face 'notmuch-search-count)))
763 ((string-equal field "subject")
764 (insert (propertize (format format-string (plist-get result :subject))
765 'face 'notmuch-search-subject)))
767 ((string-equal field "authors")
768 (notmuch-search-insert-authors format-string (plist-get result :authors)))
770 ((string-equal field "tags")
771 (let ((tags (plist-get result :tags)))
772 (insert (format format-string (notmuch-tag-format-tags tags)))))))
774 (defun notmuch-search-show-result (result &optional pos)
775 "Insert RESULT at POS or the end of the buffer if POS is null."
776 ;; Ignore excluded matches
777 (unless (= (plist-get result :matched) 0)
778 (let ((beg (or pos (point-max))))
781 (dolist (spec notmuch-search-result-format)
782 (notmuch-search-insert-field (car spec) (cdr spec) result))
784 (notmuch-search-color-line beg (point) (plist-get result :tags))
785 (put-text-property beg (point) 'notmuch-search-result result))
786 (when (string= (plist-get result :thread) notmuch-search-target-thread)
787 (setq notmuch-search-target-thread "found")
790 (defun notmuch-search-process-filter (proc string)
791 "Process and filter the output of \"notmuch search\""
792 (let ((results-buf (process-buffer proc))
793 (parse-buf (process-get proc 'parse-buf))
794 (inhibit-read-only t)
796 (when (buffer-live-p results-buf)
797 (with-current-buffer parse-buf
800 (goto-char (point-max))
802 (notmuch-sexp-parse-partial-list 'notmuch-search-show-result
805 (defun notmuch-search-tag-all (&optional tag-changes)
806 "Add/remove tags from all messages in current search buffer.
808 See `notmuch-tag' for information on the format of TAG-CHANGES."
810 (apply 'notmuch-tag notmuch-search-query-string tag-changes))
812 (defun notmuch-search-buffer-title (query)
813 "Returns the title for a buffer with notmuch search results."
817 (loop for tuple in notmuch-saved-searches
818 if (let ((quoted-query (regexp-quote (cdr tuple))))
819 (and (string-match (concat "^" quoted-query) query)
820 (> (length (match-string 0 query))
822 do (setq longest tuple))
824 (saved-search-name (car saved-search))
825 (saved-search-query (cdr saved-search)))
826 (cond ((and saved-search (equal saved-search-query query))
827 ;; Query is the same as saved search (ignoring case)
828 (concat "*notmuch-saved-search-" saved-search-name "*"))
830 (concat "*notmuch-search-"
831 (replace-regexp-in-string (concat "^" (regexp-quote saved-search-query))
832 (concat "[ " saved-search-name " ]")
836 (concat "*notmuch-search-" query "*"))
839 (defun notmuch-read-query (prompt)
840 "Read a notmuch-query from the minibuffer with completion.
842 PROMPT is the string to prompt with."
845 (append (list "folder:" "thread:" "id:" "date:" "from:" "to:"
846 "subject:" "attachment:")
847 (mapcar (lambda (tag)
848 (concat "tag:" (notmuch-escape-boolean-term tag)))
849 (process-lines notmuch-command "search" "--output=tags" "*")))))
850 (let ((keymap (copy-keymap minibuffer-local-map))
851 (minibuffer-completion-table
852 (completion-table-dynamic
854 ;; generate a list of possible completions for the current input
856 ;; this ugly regexp is used to get the last word of the input
857 ;; possibly preceded by a '('
858 ((string-match "\\(^\\|.* (?\\)\\([^ ]*\\)$" string)
859 (mapcar (lambda (compl)
860 (concat (match-string-no-properties 1 string) compl))
861 (all-completions (match-string-no-properties 2 string)
863 (t (list string)))))))
864 ;; this was simpler than convincing completing-read to accept spaces:
865 (define-key keymap (kbd "TAB") 'minibuffer-complete)
866 (let ((history-delete-duplicates t))
867 (read-from-minibuffer prompt nil keymap nil
868 'notmuch-search-history nil nil)))))
871 (defun notmuch-search (&optional query oldest-first target-thread target-line)
872 "Run \"notmuch search\" with the given `query' and display results.
874 If `query' is nil, it is read interactively from the minibuffer.
875 Other optional parameters are used as follows:
877 oldest-first: A Boolean controlling the sort order of returned threads
878 target-thread: A thread ID (without the thread: prefix) that will be made
879 current if it appears in the search results.
880 target-line: The line number to move to if the target thread does not
881 appear in the search results.
883 When called interactively, this will prompt for a query and use
884 the configured default sort order."
887 ;; Prompt for a query
889 ;; Use the default search order (if we're doing a search from a
890 ;; search buffer, ignore any buffer-local overrides)
891 (default-value 'notmuch-search-oldest-first)))
893 (let* ((query (or query (notmuch-read-query "Notmuch search: ")))
894 (buffer (get-buffer-create (notmuch-search-buffer-title query))))
895 (switch-to-buffer buffer)
896 (notmuch-search-mode)
897 ;; Don't track undo information for this buffer
898 (set 'buffer-undo-list t)
899 (set 'notmuch-search-query-string query)
900 (set 'notmuch-search-oldest-first oldest-first)
901 (set 'notmuch-search-target-thread target-thread)
902 (set 'notmuch-search-target-line target-line)
903 (let ((proc (get-buffer-process (current-buffer)))
904 (inhibit-read-only t))
906 (error "notmuch search process already running for query `%s'" query)
909 (goto-char (point-min))
911 (let ((proc (notmuch-start-notmuch
912 "notmuch-search" buffer #'notmuch-search-process-sentinel
913 "search" "--format=sexp" "--format-version=1"
915 "--sort=oldest-first"
916 "--sort=newest-first")
918 ;; Use a scratch buffer to accumulate partial output.
919 ;; This buffer will be killed by the sentinel, which
920 ;; should be called no matter how the process dies.
921 (parse-buf (generate-new-buffer " *notmuch search parse*")))
922 (process-put proc 'parse-buf parse-buf)
923 (set-process-filter proc 'notmuch-search-process-filter)
924 (set-process-query-on-exit-flag proc nil))))
925 (run-hooks 'notmuch-search-hook)))
927 (defun notmuch-search-refresh-view ()
928 "Refresh the current view.
930 Kills the current buffer and runs a new search with the same
931 query string as the current search. If the current thread is in
932 the new search results, then point will be placed on the same
933 thread. Otherwise, point will be moved to attempt to be in the
934 same relative position within the new buffer."
935 (let ((target-line (line-number-at-pos))
936 (oldest-first notmuch-search-oldest-first)
937 (target-thread (notmuch-search-find-thread-id 'bare))
938 (query notmuch-search-query-string))
939 (notmuch-kill-this-buffer)
940 (notmuch-search query oldest-first target-thread target-line)
941 (goto-char (point-min))))
943 (defun notmuch-search-toggle-order ()
944 "Toggle the current search order.
946 This command toggles the sort order for the current search. The
947 default sort order is defined by `notmuch-search-oldest-first'."
949 (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
950 (notmuch-search-refresh-view))
952 (defun notmuch-search-filter (query)
953 "Filter the current search results based on an additional query string.
955 Runs a new search matching only messages that match both the
956 current search results AND the additional query string provided."
957 (interactive (list (notmuch-read-query "Filter search: ")))
958 (let ((grouped-query (if (string-match-p notmuch-search-disjunctive-regexp query)
959 (concat "( " query " )")
961 (notmuch-search (if (string= notmuch-search-query-string "*")
963 (concat notmuch-search-query-string " and " grouped-query)) notmuch-search-oldest-first)))
965 (defun notmuch-search-filter-by-tag (tag)
966 "Filter the current search results based on a single tag.
968 Runs a new search matching only messages that match both the
969 current search results AND that are tagged with the given tag."
971 (list (notmuch-select-tag-with-completion "Filter by tag: ")))
972 (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
976 "Run notmuch and display saved searches, known tags, etc."
980 (defun notmuch-interesting-buffer (b)
981 "Is the current buffer of interest to a notmuch user?"
982 (with-current-buffer b
983 (memq major-mode '(notmuch-show-mode
989 (defun notmuch-cycle-notmuch-buffers ()
990 "Cycle through any existing notmuch buffers (search, show or hello).
992 If the current buffer is the only notmuch buffer, bury it. If no
993 notmuch buffers exist, run `notmuch'."
997 ;; If the current buffer is a notmuch buffer, remember it and then
999 (when (notmuch-interesting-buffer (current-buffer))
1000 (setq start (current-buffer))
1003 ;; Find the first notmuch buffer.
1004 (setq first (loop for buffer in (buffer-list)
1005 if (notmuch-interesting-buffer buffer)
1009 ;; If the first one we found is any other than the starting
1010 ;; buffer, switch to it.
1011 (unless (eq first start)
1012 (switch-to-buffer first))
1015 (setq mail-user-agent 'notmuch-user-agent)