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-show)
56 (require 'notmuch-mua)
57 (require 'notmuch-hello)
58 (require 'notmuch-maildir-fcc)
59 (require 'notmuch-message)
61 (defcustom notmuch-search-result-format
64 ("authors" . "%-20s ")
67 "Search result formatting. Supported fields are:
68 date, count, authors, subject, tags
70 (setq notmuch-search-result-format \(\(\"authors\" . \"%-40s\"\)
71 \(\"subject\" . \"%s\"\)\)\)"
72 :type '(alist :key-type (string) :value-type (string))
75 (defvar notmuch-query-history nil
76 "Variable to store minibuffer history for notmuch queries")
78 (defun notmuch-select-tag-with-completion (prompt &rest search-terms)
80 (with-output-to-string
81 (with-current-buffer standard-output
82 (apply 'call-process notmuch-command nil t nil "search-tags" search-terms)))))
83 (completing-read prompt (split-string tag-list "\n+" t) nil nil nil)))
85 (defun notmuch-foreach-mime-part (function mm-handle)
86 (cond ((stringp (car mm-handle))
87 (dolist (part (cdr mm-handle))
88 (notmuch-foreach-mime-part function part)))
89 ((bufferp (car mm-handle))
90 (funcall function mm-handle))
91 (t (dolist (part mm-handle)
92 (notmuch-foreach-mime-part function part)))))
94 (defun notmuch-count-attachments (mm-handle)
96 (notmuch-foreach-mime-part
98 (let ((disposition (mm-handle-disposition p)))
99 (and (listp disposition)
100 (or (equal (car disposition) "attachment")
101 (and (equal (car disposition) "inline")
102 (assq 'filename disposition)))
107 (defun notmuch-save-attachments (mm-handle &optional queryp)
108 (notmuch-foreach-mime-part
110 (let ((disposition (mm-handle-disposition p)))
111 (and (listp disposition)
112 (or (equal (car disposition) "attachment")
113 (and (equal (car disposition) "inline")
114 (assq 'filename disposition)))
117 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
121 (defun notmuch-documentation-first-line (symbol)
122 "Return the first line of the documentation string for SYMBOL."
123 (let ((doc (documentation symbol)))
126 (insert (documentation symbol t))
127 (goto-char (point-min))
130 (buffer-substring beg (point))))
133 (defun notmuch-prefix-key-description (key)
134 "Given a prefix key code, return a human-readable string representation.
136 This is basically just `format-kbd-macro' but we also convert ESC to M-."
137 (let ((desc (format-kbd-macro (vector key))))
138 (if (string= desc "ESC")
142 ; I would think that emacs would have code handy for walking a keymap
143 ; and generating strings for each key, and I would prefer to just call
144 ; that. But I couldn't find any (could be all implemented in C I
145 ; suppose), so I wrote my own here.
146 (defun notmuch-substitute-one-command-key-with-prefix (prefix binding)
147 "For a key binding, return a string showing a human-readable
148 representation of the prefixed key as well as the first line of
149 documentation from the bound function.
151 For a mouse binding, return nil."
152 (let ((key (car binding))
153 (action (cdr binding)))
154 (if (mouse-event-p key)
157 (let ((substitute (apply-partially 'notmuch-substitute-one-command-key-with-prefix (notmuch-prefix-key-description key)))
159 (map-keymap (lambda (a b)
160 (push (cons a b) as-list))
162 (mapconcat substitute as-list "\n"))
163 (concat prefix (format-kbd-macro (vector key))
165 (notmuch-documentation-first-line action))))))
167 (defun notmuch-substitute-command-keys-one (key)
168 ;; A `keymap' key indicates inheritance from a parent keymap - the
169 ;; inherited mappings follow, so there is nothing to print for
171 (when (not (eq key 'keymap))
172 (notmuch-substitute-one-command-key-with-prefix nil key)))
174 (defun notmuch-substitute-command-keys (doc)
175 "Like `substitute-command-keys' but with documentation, not function names."
177 (while (string-match "\\\\{\\([^}[:space:]]*\\)}" doc beg)
178 (let* ((keymap-name (substring doc (match-beginning 1) (match-end 1)))
179 (keymap (symbol-value (intern keymap-name))))
180 (setq doc (replace-match
181 (mapconcat #'notmuch-substitute-command-keys-one
184 (setq beg (match-end 0)))
187 (defun notmuch-help ()
188 "Display help for the current notmuch mode."
190 (let* ((mode major-mode)
191 (doc (substitute-command-keys (notmuch-substitute-command-keys (documentation mode t)))))
192 (with-current-buffer (generate-new-buffer "*notmuch-help*")
194 (goto-char (point-min))
195 (set-buffer-modified-p nil)
196 (view-buffer (current-buffer) 'kill-buffer-if-not-modified))))
198 (defcustom notmuch-search-hook '(hl-line-mode)
199 "List of functions to call when notmuch displays the search results."
201 :options '(hl-line-mode)
204 (defvar notmuch-search-mode-map
205 (let ((map (make-sparse-keymap)))
206 (define-key map "?" 'notmuch-help)
207 (define-key map "q" 'notmuch-search-quit)
208 (define-key map "x" 'notmuch-search-quit)
209 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
210 (define-key map "b" 'notmuch-search-scroll-down)
211 (define-key map " " 'notmuch-search-scroll-up)
212 (define-key map "<" 'notmuch-search-first-thread)
213 (define-key map ">" 'notmuch-search-last-thread)
214 (define-key map "p" 'notmuch-search-previous-thread)
215 (define-key map "n" 'notmuch-search-next-thread)
216 (define-key map "r" 'notmuch-search-reply-to-thread)
217 (define-key map "m" 'notmuch-mua-new-mail)
218 (define-key map "s" 'notmuch-search)
219 (define-key map "o" 'notmuch-search-toggle-order)
220 (define-key map "c" 'notmuch-search-stash-map)
221 (define-key map "=" 'notmuch-search-refresh-view)
222 (define-key map "G" 'notmuch-search-poll-and-refresh-view)
223 (define-key map "t" 'notmuch-search-filter-by-tag)
224 (define-key map "f" 'notmuch-search-filter)
225 (define-key map [mouse-1] 'notmuch-search-show-thread)
226 (define-key map "*" 'notmuch-search-operate-all)
227 (define-key map "a" 'notmuch-search-archive-thread)
228 (define-key map "-" 'notmuch-search-remove-tag)
229 (define-key map "+" 'notmuch-search-add-tag)
230 (define-key map (kbd "RET") 'notmuch-search-show-thread)
232 "Keymap for \"notmuch search\" buffers.")
233 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
235 (defvar notmuch-search-stash-map
236 (let ((map (make-sparse-keymap)))
237 (define-key map "i" 'notmuch-search-stash-thread-id)
239 "Submap for stash commands")
240 (fset 'notmuch-search-stash-map notmuch-search-stash-map)
242 (defun notmuch-search-stash-thread-id ()
243 "Copy thread ID of current thread to kill-ring."
245 (notmuch-common-do-stash (notmuch-search-find-thread-id)))
247 (defvar notmuch-search-query-string)
248 (defvar notmuch-search-target-thread)
249 (defvar notmuch-search-target-line)
250 (defvar notmuch-search-continuation)
252 (defvar notmuch-search-disjunctive-regexp "\\<[oO][rR]\\>")
254 (defun notmuch-search-quit ()
255 "Exit the search buffer, calling any defined continuation function."
257 (let ((continuation notmuch-search-continuation))
258 (notmuch-kill-this-buffer)
260 (funcall continuation))))
262 (defun notmuch-search-scroll-up ()
263 "Move forward through search results by one window's worth."
267 ((end-of-buffer) (notmuch-search-last-thread))))
269 (defun notmuch-search-scroll-down ()
270 "Move backward through the search results by one window's worth."
272 ; I don't know why scroll-down doesn't signal beginning-of-buffer
273 ; the way that scroll-up signals end-of-buffer, but c'est la vie.
275 ; So instead of trapping a signal we instead check whether the
276 ; window begins on the first line of the buffer and if so, move
277 ; directly to that position. (We have to count lines since the
278 ; window-start position is not the same as point-min due to the
279 ; invisible thread-ID characters on the first line.
280 (if (equal (count-lines (point-min) (window-start)) 0)
281 (goto-char (point-min))
284 (defun notmuch-search-next-thread ()
285 "Select the next thread in the search results."
289 (defun notmuch-search-previous-thread ()
290 "Select the previous thread in the search results."
294 (defun notmuch-search-last-thread ()
295 "Select the last thread in the search results."
297 (goto-char (point-max))
300 (defun notmuch-search-first-thread ()
301 "Select the first thread in the search results."
303 (goto-char (point-min)))
305 (defface notmuch-message-summary-face
306 '((((class color) (background light)) (:background "#f0f0f0"))
307 (((class color) (background dark)) (:background "#303030")))
308 "Face for the single-line message summary in notmuch-show-mode."
311 (defface notmuch-search-date
312 '((t :inherit default))
313 "Face used in search mode for dates."
316 (defface notmuch-search-count
317 '((t :inherit default))
318 "Face used in search mode for the count matching the query."
321 (defface notmuch-search-subject
322 '((t :inherit default))
323 "Face used in search mode for subjects."
326 (defface notmuch-search-matching-authors
327 '((t :inherit default))
328 "Face used in search mode for authors matching the query."
331 (defface notmuch-search-non-matching-authors
334 (:foreground "grey30"))
337 (:foreground "grey60"))
340 "Face used in search mode for authors not matching the query."
343 (defface notmuch-tag-face
346 (:foreground "OliveDrab1"))
349 (:foreground "navy blue" :bold t))
352 "Face used in search mode face for tags."
355 (defun notmuch-search-mode ()
356 "Major mode displaying results of a notmuch search.
358 This buffer contains the results of a \"notmuch search\" of your
359 email archives. Each line in the buffer represents a single
360 thread giving a summary of the thread (a relative date, the
361 number of matched messages and total messages in the thread,
362 participants in the thread, a representative subject line, and
365 Pressing \\[notmuch-search-show-thread] on any line displays that thread. The '\\[notmuch-search-add-tag]' and '\\[notmuch-search-remove-tag]'
366 keys can be used to add or remove tags from a thread. The '\\[notmuch-search-archive-thread]' key
367 is a convenience for archiving a thread (removing the \"inbox\"
368 tag). The '\\[notmuch-search-operate-all]' key can be used to add or remove a tag from all
369 threads in the current buffer.
371 Other useful commands are '\\[notmuch-search-filter]' for filtering the current search
372 based on an additional query string, '\\[notmuch-search-filter-by-tag]' for filtering to include
373 only messages with a given tag, and '\\[notmuch-search]' to execute a new, global
376 Complete list of currently available key bindings:
378 \\{notmuch-search-mode-map}"
380 (kill-all-local-variables)
381 (make-local-variable 'notmuch-search-query-string)
382 (make-local-variable 'notmuch-search-oldest-first)
383 (make-local-variable 'notmuch-search-target-thread)
384 (make-local-variable 'notmuch-search-target-line)
385 (set (make-local-variable 'notmuch-search-continuation) nil)
386 (set (make-local-variable 'scroll-preserve-screen-position) t)
387 (add-to-invisibility-spec (cons 'ellipsis t))
388 (use-local-map notmuch-search-mode-map)
389 (setq truncate-lines t)
390 (setq major-mode 'notmuch-search-mode
391 mode-name "notmuch-search")
392 (setq buffer-read-only t))
394 (defun notmuch-search-properties-in-region (property beg end)
397 (last-line (line-number-at-pos end))
398 (max-line (- (line-number-at-pos (point-max)) 2)))
401 (while (<= (line-number-at-pos) (min last-line max-line))
402 (setq output (cons (get-text-property (point) property) output))
406 (defun notmuch-search-find-thread-id ()
407 "Return the thread for the current thread"
408 (get-text-property (point) 'notmuch-search-thread-id))
410 (defun notmuch-search-find-thread-id-region (beg end)
411 "Return a list of threads for the current region"
412 (notmuch-search-properties-in-region 'notmuch-search-thread-id beg end))
414 (defun notmuch-search-find-authors ()
415 "Return the authors for the current thread"
416 (get-text-property (point) 'notmuch-search-authors))
418 (defun notmuch-search-find-authors-region (beg end)
419 "Return a list of authors for the current region"
420 (notmuch-search-properties-in-region 'notmuch-search-authors beg end))
422 (defun notmuch-search-find-subject ()
423 "Return the subject for the current thread"
424 (get-text-property (point) 'notmuch-search-subject))
426 (defun notmuch-search-find-subject-region (beg end)
427 "Return a list of authors for the current region"
428 (notmuch-search-properties-in-region 'notmuch-search-subject beg end))
430 (defun notmuch-search-show-thread (&optional crypto-switch)
431 "Display the currently selected thread."
433 (let ((thread-id (notmuch-search-find-thread-id))
434 (subject (notmuch-search-find-subject)))
435 (if (> (length thread-id) 0)
436 (notmuch-show thread-id
438 notmuch-search-query-string
439 ;; name the buffer based on notmuch-search-find-subject
440 (if (string-match "^[ \t]*$" subject)
442 (truncate-string-to-width
444 (truncate-string-to-width subject 32 nil nil t)
448 (message "End of search results."))))
450 (defun notmuch-search-reply-to-thread (&optional prompt-for-sender)
451 "Begin composing a reply to the entire current thread in a new buffer."
453 (let ((message-id (notmuch-search-find-thread-id)))
454 (notmuch-mua-new-reply message-id prompt-for-sender)))
456 (defun notmuch-call-notmuch-process (&rest args)
457 "Synchronously invoke \"notmuch\" with the given list of arguments.
459 Output from the process will be presented to the user as an error
460 and will also appear in a buffer named \"*Notmuch errors*\"."
461 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
462 (with-current-buffer error-buffer
464 (if (eq (apply 'call-process notmuch-command nil error-buffer nil args) 0)
467 (with-current-buffer error-buffer
468 (let ((beg (point-min))
469 (end (- (point-max) 1)))
470 (error (buffer-substring beg end))
473 (defun notmuch-tag (query &rest tags)
474 "Add/remove tags in TAGS to messages matching QUERY.
476 TAGS should be a list of strings of the form \"+TAG\" or \"-TAG\" and
477 QUERY should be a string containing the search-query.
479 Note: Other code should always use this function alter tags of
480 messages instead of running (notmuch-call-notmuch-process \"tag\" ..)
481 directly, so that hooks specified in notmuch-before-tag-hook and
482 notmuch-after-tag-hook will be run."
483 (run-hooks 'notmuch-before-tag-hook)
484 (apply 'notmuch-call-notmuch-process
485 (append (list "tag") tags (list "--" query)))
486 (run-hooks 'notmuch-after-tag-hook))
488 (defcustom notmuch-before-tag-hook nil
489 "Hooks that are run before tags of a message are modified.
491 'tags' will contain the tags that are about to be added or removed as
492 a list of strings of the form \"+TAG\" or \"-TAG\".
493 'query' will be a string containing the search query that determines
494 the messages that are about to be tagged"
497 :options '(hl-line-mode)
500 (defcustom notmuch-after-tag-hook nil
501 "Hooks that are run after tags of a message are modified.
503 'tags' will contain the tags that were added or removed as
504 a list of strings of the form \"+TAG\" or \"-TAG\".
505 'query' will be a string containing the search query that determines
506 the messages that were tagged"
508 :options '(hl-line-mode)
511 (defun notmuch-search-set-tags (tags)
514 (re-search-backward "(")
517 (inhibit-read-only t))
518 (re-search-forward ")")
521 (delete-region beg end)
522 (insert (propertize (mapconcat 'identity tags " ")
523 'face 'notmuch-tag-face))))))
525 (defun notmuch-search-get-tags ()
528 (re-search-backward "(")
529 (let ((beg (+ (point) 1)))
530 (re-search-forward ")")
531 (let ((end (- (point) 1)))
532 (split-string (buffer-substring beg end))))))
534 (defun notmuch-search-get-tags-region (beg end)
537 (last-line (line-number-at-pos end))
538 (max-line (- (line-number-at-pos (point-max)) 2)))
540 (while (<= (line-number-at-pos) (min last-line max-line))
541 (setq output (append output (notmuch-search-get-tags)))
545 (defun notmuch-search-add-tag-thread (tag)
546 (notmuch-search-add-tag-region tag (point) (point)))
548 (defun notmuch-search-add-tag-region (tag beg end)
549 (let ((search-id-string (mapconcat 'identity (notmuch-search-find-thread-id-region beg end) " or ")))
550 (notmuch-tag search-id-string (concat "+" tag))
552 (let ((last-line (line-number-at-pos end))
553 (max-line (- (line-number-at-pos (point-max)) 2)))
555 (while (<= (line-number-at-pos) (min last-line max-line))
556 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<)))
559 (defun notmuch-search-remove-tag-thread (tag)
560 (notmuch-search-remove-tag-region tag (point) (point)))
562 (defun notmuch-search-remove-tag-region (tag beg end)
563 (let ((search-id-string (mapconcat 'identity (notmuch-search-find-thread-id-region beg end) " or ")))
564 (notmuch-tag search-id-string (concat "-" tag))
566 (let ((last-line (line-number-at-pos end))
567 (max-line (- (line-number-at-pos (point-max)) 2)))
569 (while (<= (line-number-at-pos) (min last-line max-line))
570 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags)))
573 (defun notmuch-search-add-tag (tag)
574 "Add a tag to the currently selected thread or region.
576 The tag is added to all messages in the currently selected thread
577 or threads in the current region."
579 (list (notmuch-select-tag-with-completion "Tag to add: ")))
581 (if (region-active-p)
582 (let* ((beg (region-beginning))
584 (notmuch-search-add-tag-region tag beg end))
585 (notmuch-search-add-tag-thread tag))))
587 (defun notmuch-search-remove-tag (tag)
588 "Remove a tag from the currently selected thread or region.
590 The tag is removed from all messages in the currently selected
591 thread or threads in the current region."
593 (list (notmuch-select-tag-with-completion
595 (if (region-active-p)
597 (notmuch-search-find-thread-id-region (region-beginning) (region-end))
599 (notmuch-search-find-thread-id)))))
601 (if (region-active-p)
602 (let* ((beg (region-beginning))
604 (notmuch-search-remove-tag-region tag beg end))
605 (notmuch-search-remove-tag-thread tag))))
607 (defun notmuch-search-archive-thread ()
608 "Archive the currently selected thread (remove its \"inbox\" tag).
610 This function advances the next thread when finished."
612 (notmuch-search-remove-tag-thread "inbox")
615 (defvar notmuch-search-process-filter-data nil
616 "Data that has not yet been processed.")
617 (make-variable-buffer-local 'notmuch-search-process-filter-data)
619 (defun notmuch-search-process-sentinel (proc msg)
620 "Add a message to let user know when \"notmuch search\" exits"
621 (let ((buffer (process-buffer proc))
622 (status (process-status proc))
623 (exit-status (process-exit-status proc))
624 (never-found-target-thread nil))
625 (if (memq status '(exit signal))
626 (if (buffer-live-p buffer)
627 (with-current-buffer buffer
629 (let ((inhibit-read-only t)
631 (goto-char (point-max))
632 (if (eq status 'signal)
633 (insert "Incomplete search results (search process was killed).\n"))
634 (if (eq status 'exit)
636 (if notmuch-search-process-filter-data
637 (insert (concat "Error: Unexpected output from notmuch search:\n" notmuch-search-process-filter-data)))
638 (insert "End of search results.")
639 (if (not (= exit-status 0))
640 (insert (format " (process returned %d)" exit-status)))
643 (not (string= notmuch-search-target-thread "found")))
644 (set 'never-found-target-thread t))))))
645 (when (and never-found-target-thread
646 notmuch-search-target-line)
647 (goto-char (point-min))
648 (forward-line (1- notmuch-search-target-line))))))))
650 (defcustom notmuch-search-line-faces nil
651 "Tag/face mapping for line highlighting in notmuch-search.
653 Here is an example of how to color search results based on tags.
654 (the following text would be placed in your ~/.emacs file):
656 (setq notmuch-search-line-faces '((\"delete\" . (:foreground \"red\"
657 :background \"blue\"))
658 (\"unread\" . (:foreground \"green\"))))
660 The attributes defined for matching tags are merged, with later
661 attributes overriding earlier. A message having both \"delete\"
662 and \"unread\" tags with the above settings would have a green
663 foreground and blue background."
664 :type '(alist :key-type (string) :value-type (custom-face-edit))
667 (defun notmuch-search-color-line (start end line-tag-list)
668 "Colorize lines in `notmuch-show' based on tags."
669 ;; Create the overlay only if the message has tags which match one
670 ;; of those specified in `notmuch-search-line-faces'.
673 (let ((tag (car elem))
674 (attributes (cdr elem)))
675 (when (member tag line-tag-list)
677 (setq overlay (make-overlay start end)))
678 ;; Merge the specified properties with any already
679 ;; applied from an earlier match.
680 (overlay-put overlay 'face
681 (append (overlay-get overlay 'face) attributes)))))
682 notmuch-search-line-faces)))
684 (defun notmuch-search-author-propertize (authors)
685 "Split `authors' into matching and non-matching authors and
686 propertize appropriately. If no boundary between authors and
687 non-authors is found, assume that all of the authors match."
688 (if (string-match "\\(.*\\)|\\(.*\\)" authors)
689 (concat (propertize (concat (match-string 1 authors) ",")
690 'face 'notmuch-search-matching-authors)
691 (propertize (match-string 2 authors)
692 'face 'notmuch-search-non-matching-authors))
693 (propertize authors 'face 'notmuch-search-matching-authors)))
695 (defun notmuch-search-insert-authors (format-string authors)
696 ;; Save the match data to avoid interfering with
697 ;; `notmuch-search-process-filter'.
699 (let* ((formatted-authors (format format-string authors))
700 (formatted-sample (format format-string ""))
701 (visible-string formatted-authors)
702 (invisible-string "")
705 ;; Truncate the author string to fit the specification.
706 (if (> (length formatted-authors)
707 (length formatted-sample))
708 (let ((visible-length (- (length formatted-sample)
710 ;; Truncate the visible string according to the width of
711 ;; the display string.
712 (setq visible-string (substring formatted-authors 0 visible-length)
713 invisible-string (substring formatted-authors visible-length))
714 ;; If possible, truncate the visible string at a natural
715 ;; break (comma or pipe), as incremental search doesn't
716 ;; match across the visible/invisible border.
717 (when (string-match "\\(.*\\)\\([,|] \\)\\([^,|]*\\)" visible-string)
718 ;; Second clause is destructive on `visible-string', so
719 ;; order is important.
720 (setq invisible-string (concat (match-string 3 visible-string)
722 visible-string (concat (match-string 1 visible-string)
723 (match-string 2 visible-string))))
724 ;; `visible-string' may be shorter than the space allowed
725 ;; by `format-string'. If so we must insert some padding
726 ;; after `invisible-string'.
727 (setq padding (make-string (- (length formatted-sample)
728 (length visible-string)
732 ;; Use different faces to show matching and non-matching authors.
733 (if (string-match "\\(.*\\)|\\(.*\\)" visible-string)
734 ;; The visible string contains both matching and
735 ;; non-matching authors.
736 (setq visible-string (notmuch-search-author-propertize visible-string)
737 ;; The invisible string must contain only non-matching
738 ;; authors, as the visible-string contains both.
739 invisible-string (propertize invisible-string
740 'face 'notmuch-search-non-matching-authors))
741 ;; The visible string contains only matching authors.
742 (setq visible-string (propertize visible-string
743 'face 'notmuch-search-matching-authors)
744 ;; The invisible string may contain both matching and
745 ;; non-matching authors.
746 invisible-string (notmuch-search-author-propertize invisible-string)))
748 ;; If there is any invisible text, add it as a tooltip to the
750 (when (not (string= invisible-string ""))
751 (setq visible-string (propertize visible-string 'help-echo (concat "..." invisible-string))))
753 ;; Insert the visible and, if present, invisible author strings.
754 (insert visible-string)
755 (when (not (string= invisible-string ""))
756 (let ((start (point))
758 (insert invisible-string)
759 (setq overlay (make-overlay start (point)))
760 (overlay-put overlay 'invisible 'ellipsis)
761 (overlay-put overlay 'isearch-open-invisible #'delete-overlay)))
764 (defun notmuch-search-insert-field (field date count authors subject tags)
766 ((string-equal field "date")
767 (insert (propertize (format (cdr (assoc field notmuch-search-result-format)) date)
768 'face 'notmuch-search-date)))
769 ((string-equal field "count")
770 (insert (propertize (format (cdr (assoc field notmuch-search-result-format)) count)
771 'face 'notmuch-search-count)))
772 ((string-equal field "subject")
773 (insert (propertize (format (cdr (assoc field notmuch-search-result-format)) subject)
774 'face 'notmuch-search-subject)))
776 ((string-equal field "authors")
777 (notmuch-search-insert-authors (cdr (assoc field notmuch-search-result-format)) authors))
779 ((string-equal field "tags")
780 (insert (concat "(" (propertize tags 'font-lock-face 'notmuch-tag-face) ")")))))
782 (defun notmuch-search-show-result (date count authors subject tags)
783 (let ((fields) (field))
784 (setq fields (mapcar 'car notmuch-search-result-format))
785 (loop for field in fields
786 do (notmuch-search-insert-field field date count authors subject tags)))
789 (defun notmuch-search-process-filter (proc string)
790 "Process and filter the output of \"notmuch search\""
791 (let ((buffer (process-buffer proc))
793 (if (buffer-live-p buffer)
794 (with-current-buffer buffer
798 (inhibit-read-only t)
799 (string (concat notmuch-search-process-filter-data string)))
800 (setq notmuch-search-process-filter-data nil)
802 (while (and (< line (length string)) (= (elt string line) ?\n))
803 (setq line (1+ line)))
804 (if (string-match "^\\(thread:[0-9A-Fa-f]*\\) \\([^][]*\\) \\(\\[[0-9/]*\\]\\) \\([^;]*\\); \\(.*\\) (\\([^()]*\\))$" string line)
805 (let* ((thread-id (match-string 1 string))
806 (date (match-string 2 string))
807 (count (match-string 3 string))
808 (authors (match-string 4 string))
809 (subject (match-string 5 string))
810 (tags (match-string 6 string))
811 (tag-list (if tags (save-match-data (split-string tags)))))
812 (goto-char (point-max))
813 (if (/= (match-beginning 1) line)
814 (insert (concat "Error: Unexpected output from notmuch search:\n" (substring string line (match-beginning 1)) "\n")))
816 (notmuch-search-show-result date count authors subject tags)
817 (notmuch-search-color-line beg (point) tag-list)
818 (put-text-property beg (point) 'notmuch-search-thread-id thread-id)
819 (put-text-property beg (point) 'notmuch-search-authors authors)
820 (put-text-property beg (point) 'notmuch-search-subject subject)
821 (if (string= thread-id notmuch-search-target-thread)
823 (set 'found-target beg)
824 (set 'notmuch-search-target-thread "found"))))
825 (set 'line (match-end 0)))
827 (while (and (< line (length string)) (= (elt string line) ?\n))
828 (setq line (1+ line)))
829 (if (< line (length string))
830 (setq notmuch-search-process-filter-data (substring string line)))
833 (goto-char found-target)))
834 (delete-process proc))))
836 (defun notmuch-search-operate-all (action)
837 "Add/remove tags from all matching messages.
839 This command adds or removes tags from all messages matching the
840 current search terms. When called interactively, this command
841 will prompt for tags to be added or removed. Tags prefixed with
842 '+' will be added and tags prefixed with '-' will be removed.
844 Each character of the tag name may consist of alphanumeric
845 characters as well as `_.+-'.
847 (interactive "sOperation (+add -drop): notmuch tag ")
848 (let ((action-split (split-string action " +")))
849 ;; Perform some validation
850 (let ((words action-split))
851 (when (null words) (error "No operation given"))
853 (unless (string-match-p "^[-+][-+_.[:word:]]+$" (car words))
854 (error "Action must be of the form `+thistag -that_tag'"))
855 (setq words (cdr words))))
856 (apply 'notmuch-tag notmuch-search-query-string action-split)))
858 (defun notmuch-search-buffer-title (query)
859 "Returns the title for a buffer with notmuch search results."
863 (loop for tuple in notmuch-saved-searches
864 if (let ((quoted-query (regexp-quote (cdr tuple))))
865 (and (string-match (concat "^" quoted-query) query)
866 (> (length (match-string 0 query))
868 do (setq longest tuple))
870 (saved-search-name (car saved-search))
871 (saved-search-query (cdr saved-search)))
872 (cond ((and saved-search (equal saved-search-query query))
873 ;; Query is the same as saved search (ignoring case)
874 (concat "*notmuch-saved-search-" saved-search-name "*"))
876 (concat "*notmuch-search-"
877 (replace-regexp-in-string (concat "^" (regexp-quote saved-search-query))
878 (concat "[ " saved-search-name " ]")
882 (concat "*notmuch-search-" query "*"))
885 (defun notmuch-read-query (prompt)
886 "Read a notmuch-query from the minibuffer with completion.
888 PROMPT is the string to prompt with."
891 (append (list "folder:" "thread:" "id:" "date:" "from:" "to:"
892 "subject:" "attachment:")
893 (mapcar (lambda (tag)
895 (process-lines notmuch-command "search" "--output=tags" "*")))))
896 (let ((keymap (copy-keymap minibuffer-local-map))
897 (minibuffer-completion-table
898 (completion-table-dynamic
900 ;; generate a list of possible completions for the current input
902 ;; this ugly regexp is used to get the last word of the input
903 ;; possibly preceded by a '('
904 ((string-match "\\(^\\|.* (?\\)\\([^ ]*\\)$" string)
905 (mapcar (lambda (compl)
906 (concat (match-string-no-properties 1 string) compl))
907 (all-completions (match-string-no-properties 2 string)
909 (t (list string)))))))
910 ;; this was simpler than convincing completing-read to accept spaces:
911 (define-key keymap (kbd "<tab>") 'minibuffer-complete)
912 (read-from-minibuffer prompt nil keymap nil
913 'notmuch-query-history nil nil))))
916 (defun notmuch-search (query &optional oldest-first target-thread target-line continuation)
917 "Run \"notmuch search\" with the given query string and display results.
919 The optional parameters are used as follows:
921 oldest-first: A Boolean controlling the sort order of returned threads
922 target-thread: A thread ID (with the thread: prefix) that will be made
923 current if it appears in the search results.
924 target-line: The line number to move to if the target thread does not
925 appear in the search results."
926 (interactive (list (notmuch-read-query "Notmuch search: ")))
927 (let ((buffer (get-buffer-create (notmuch-search-buffer-title query))))
928 (switch-to-buffer buffer)
929 (notmuch-search-mode)
930 ;; Don't track undo information for this buffer
931 (set 'buffer-undo-list t)
932 (set 'notmuch-search-query-string query)
933 (set 'notmuch-search-oldest-first oldest-first)
934 (set 'notmuch-search-target-thread target-thread)
935 (set 'notmuch-search-target-line target-line)
936 (set 'notmuch-search-continuation continuation)
937 (let ((proc (get-buffer-process (current-buffer)))
938 (inhibit-read-only t))
940 (error "notmuch search process already running for query `%s'" query)
943 (goto-char (point-min))
945 (let ((proc (start-process
946 "notmuch-search" buffer
947 notmuch-command "search"
949 "--sort=oldest-first"
950 "--sort=newest-first")
952 (set-process-sentinel proc 'notmuch-search-process-sentinel)
953 (set-process-filter proc 'notmuch-search-process-filter)
954 (set-process-query-on-exit-flag proc nil))))
955 (run-hooks 'notmuch-search-hook)))
957 (defun notmuch-search-refresh-view ()
958 "Refresh the current view.
960 Kills the current buffer and runs a new search with the same
961 query string as the current search. If the current thread is in
962 the new search results, then point will be placed on the same
963 thread. Otherwise, point will be moved to attempt to be in the
964 same relative position within the new buffer."
966 (let ((target-line (line-number-at-pos))
967 (oldest-first notmuch-search-oldest-first)
968 (target-thread (notmuch-search-find-thread-id))
969 (query notmuch-search-query-string)
970 (continuation notmuch-search-continuation))
971 (notmuch-kill-this-buffer)
972 (notmuch-search query oldest-first target-thread target-line continuation)
973 (goto-char (point-min))))
975 (defcustom notmuch-poll-script nil
976 "An external script to incorporate new mail into the notmuch database.
978 This variable controls the action invoked by
979 `notmuch-search-poll-and-refresh-view' and
980 `notmuch-hello-poll-and-update' (each have a default keybinding
981 of 'G') to incorporate new mail into the notmuch database.
983 If set to nil (the default), new mail is processed by invoking
984 \"notmuch new\". Otherwise, this should be set to a string that
985 gives the name of an external script that processes new mail. If
986 set to the empty string, no command will be run.
988 The external script could do any of the following depending on
991 1. Invoke a program to transfer mail to the local mail store
992 2. Invoke \"notmuch new\" to incorporate the new mail
993 3. Invoke one or more \"notmuch tag\" commands to classify the mail
995 Note that the recommended way of achieving the same is using
996 \"notmuch new\" hooks."
997 :type '(choice (const :tag "notmuch new" nil)
998 (const :tag "Disabled" "")
999 (string :tag "Custom script"))
1002 (defun notmuch-poll ()
1003 "Run \"notmuch new\" or an external script to import mail.
1005 Invokes `notmuch-poll-script', \"notmuch new\", or does nothing
1006 depending on the value of `notmuch-poll-script'."
1008 (if (stringp notmuch-poll-script)
1009 (if (not (string= notmuch-poll-script ""))
1010 (call-process notmuch-poll-script nil nil))
1011 (call-process notmuch-command nil nil nil "new")))
1013 (defun notmuch-search-poll-and-refresh-view ()
1014 "Invoke `notmuch-poll' to import mail, then refresh the current view."
1017 (notmuch-search-refresh-view))
1019 (defun notmuch-search-toggle-order ()
1020 "Toggle the current search order.
1022 By default, the \"inbox\" view created by `notmuch' is displayed
1023 in chronological order (oldest thread at the beginning of the
1024 buffer), while any global searches created by `notmuch-search'
1025 are displayed in reverse-chronological order (newest thread at
1026 the beginning of the buffer).
1028 This command toggles the sort order for the current search.
1030 Note that any filtered searches created by
1031 `notmuch-search-filter' retain the search order of the parent
1034 (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
1035 (notmuch-search-refresh-view))
1037 (defun notmuch-search-filter (query)
1038 "Filter the current search results based on an additional query string.
1040 Runs a new search matching only messages that match both the
1041 current search results AND the additional query string provided."
1042 (interactive (list (notmuch-read-query "Filter search: ")))
1043 (let ((grouped-query (if (string-match-p notmuch-search-disjunctive-regexp query)
1044 (concat "( " query " )")
1046 (notmuch-search (if (string= notmuch-search-query-string "*")
1048 (concat notmuch-search-query-string " and " grouped-query)) notmuch-search-oldest-first)))
1050 (defun notmuch-search-filter-by-tag (tag)
1051 "Filter the current search results based on a single tag.
1053 Runs a new search matching only messages that match both the
1054 current search results AND that are tagged with the given tag."
1056 (list (notmuch-select-tag-with-completion "Filter by tag: ")))
1057 (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
1061 "Run notmuch and display saved searches, known tags, etc."
1066 (defun notmuch-jump-to-recent-buffer ()
1067 "Jump to the most recent notmuch buffer (search, show or hello).
1069 If no recent buffer is found, run `notmuch'."
1072 (loop for buffer in (buffer-list)
1073 if (with-current-buffer buffer
1074 (memq major-mode '(notmuch-show-mode
1076 notmuch-hello-mode)))
1079 (switch-to-buffer last)
1082 (setq mail-user-agent 'notmuch-user-agent)