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 formating. 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 (defun notmuch-select-tag-with-completion (prompt &rest search-terms)
77 (with-output-to-string
78 (with-current-buffer standard-output
79 (apply 'call-process notmuch-command nil t nil "search-tags" search-terms)))))
80 (completing-read prompt (split-string tag-list "\n+" t) nil nil nil)))
82 (defun notmuch-foreach-mime-part (function mm-handle)
83 (cond ((stringp (car mm-handle))
84 (dolist (part (cdr mm-handle))
85 (notmuch-foreach-mime-part function part)))
86 ((bufferp (car mm-handle))
87 (funcall function mm-handle))
88 (t (dolist (part mm-handle)
89 (notmuch-foreach-mime-part function part)))))
91 (defun notmuch-count-attachments (mm-handle)
93 (notmuch-foreach-mime-part
95 (let ((disposition (mm-handle-disposition p)))
96 (and (listp disposition)
97 (or (equal (car disposition) "attachment")
98 (and (equal (car disposition) "inline")
99 (assq 'filename disposition)))
104 (defun notmuch-save-attachments (mm-handle &optional queryp)
105 (notmuch-foreach-mime-part
107 (let ((disposition (mm-handle-disposition p)))
108 (and (listp disposition)
109 (or (equal (car disposition) "attachment")
110 (and (equal (car disposition) "inline")
111 (assq 'filename disposition)))
114 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
118 (defun notmuch-documentation-first-line (symbol)
119 "Return the first line of the documentation string for SYMBOL."
120 (let ((doc (documentation symbol)))
123 (insert (documentation symbol t))
124 (goto-char (point-min))
127 (buffer-substring beg (point))))
130 (defun notmuch-prefix-key-description (key)
131 "Given a prefix key code, return a human-readable string representation.
133 This is basically just `format-kbd-macro' but we also convert ESC to M-."
134 (let ((desc (format-kbd-macro (vector key))))
135 (if (string= desc "ESC")
139 ; I would think that emacs would have code handy for walking a keymap
140 ; and generating strings for each key, and I would prefer to just call
141 ; that. But I couldn't find any (could be all implemented in C I
142 ; suppose), so I wrote my own here.
143 (defun notmuch-substitute-one-command-key-with-prefix (prefix binding)
144 "For a key binding, return a string showing a human-readable
145 representation of the prefixed key as well as the first line of
146 documentation from the bound function.
148 For a mouse binding, return nil."
149 (let ((key (car binding))
150 (action (cdr binding)))
151 (if (mouse-event-p key)
154 (let ((substitute (apply-partially 'notmuch-substitute-one-command-key-with-prefix (notmuch-prefix-key-description key)))
156 (map-keymap (lambda (a b)
157 (push (cons a b) as-list))
159 (mapconcat substitute as-list "\n"))
160 (concat prefix (format-kbd-macro (vector key))
162 (notmuch-documentation-first-line action))))))
164 (defalias 'notmuch-substitute-one-command-key
165 (apply-partially 'notmuch-substitute-one-command-key-with-prefix nil))
167 (defun notmuch-substitute-command-keys (doc)
168 "Like `substitute-command-keys' but with documentation, not function names."
170 (while (string-match "\\\\{\\([^}[:space:]]*\\)}" doc beg)
171 (let ((map (substring doc (match-beginning 1) (match-end 1))))
172 (setq doc (replace-match (mapconcat 'notmuch-substitute-one-command-key
173 (cdr (symbol-value (intern map))) "\n") 1 1 doc)))
174 (setq beg (match-end 0)))
177 (defun notmuch-help ()
178 "Display help for the current notmuch mode."
180 (let* ((mode major-mode)
181 (doc (substitute-command-keys (notmuch-substitute-command-keys (documentation mode t)))))
182 (with-current-buffer (generate-new-buffer "*notmuch-help*")
184 (goto-char (point-min))
185 (set-buffer-modified-p nil)
186 (view-buffer (current-buffer) 'kill-buffer-if-not-modified))))
188 (defcustom notmuch-search-hook '(hl-line-mode)
189 "List of functions to call when notmuch displays the search results."
191 :options '(hl-line-mode)
194 (defvar notmuch-search-mode-map
195 (let ((map (make-sparse-keymap)))
196 (define-key map "?" 'notmuch-help)
197 (define-key map "q" 'notmuch-search-quit)
198 (define-key map "x" 'notmuch-search-quit)
199 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
200 (define-key map "b" 'notmuch-search-scroll-down)
201 (define-key map " " 'notmuch-search-scroll-up)
202 (define-key map "<" 'notmuch-search-first-thread)
203 (define-key map ">" 'notmuch-search-last-thread)
204 (define-key map "p" 'notmuch-search-previous-thread)
205 (define-key map "n" 'notmuch-search-next-thread)
206 (define-key map "r" 'notmuch-search-reply-to-thread)
207 (define-key map "m" 'notmuch-mua-mail)
208 (define-key map "s" 'notmuch-search)
209 (define-key map "o" 'notmuch-search-toggle-order)
210 (define-key map "=" 'notmuch-search-refresh-view)
211 (define-key map "G" 'notmuch-search-poll-and-refresh-view)
212 (define-key map "t" 'notmuch-search-filter-by-tag)
213 (define-key map "f" 'notmuch-search-filter)
214 (define-key map [mouse-1] 'notmuch-search-show-thread)
215 (define-key map "*" 'notmuch-search-operate-all)
216 (define-key map "a" 'notmuch-search-archive-thread)
217 (define-key map "-" 'notmuch-search-remove-tag)
218 (define-key map "+" 'notmuch-search-add-tag)
219 (define-key map (kbd "RET") 'notmuch-search-show-thread)
221 "Keymap for \"notmuch search\" buffers.")
222 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
224 (defvar notmuch-search-query-string)
225 (defvar notmuch-search-target-thread)
226 (defvar notmuch-search-target-line)
227 (defvar notmuch-search-continuation)
229 (defvar notmuch-search-disjunctive-regexp "\\<[oO][rR]\\>")
231 (defun notmuch-search-quit ()
232 "Exit the search buffer, calling any defined continuation function."
234 (let ((continuation notmuch-search-continuation))
237 (funcall continuation))))
239 (defun notmuch-search-scroll-up ()
240 "Move forward through search results by one window's worth."
244 ((end-of-buffer) (notmuch-search-last-thread))))
246 (defun notmuch-search-scroll-down ()
247 "Move backward through the search results by one window's worth."
249 ; I don't know why scroll-down doesn't signal beginning-of-buffer
250 ; the way that scroll-up signals end-of-buffer, but c'est la vie.
252 ; So instead of trapping a signal we instead check whether the
253 ; window begins on the first line of the buffer and if so, move
254 ; directly to that position. (We have to count lines since the
255 ; window-start position is not the same as point-min due to the
256 ; invisible thread-ID characters on the first line.
257 (if (equal (count-lines (point-min) (window-start)) 0)
258 (goto-char (point-min))
261 (defun notmuch-search-next-thread ()
262 "Select the next thread in the search results."
266 (defun notmuch-search-previous-thread ()
267 "Select the previous thread in the search results."
271 (defun notmuch-search-last-thread ()
272 "Select the last thread in the search results."
274 (goto-char (point-max))
277 (defun notmuch-search-first-thread ()
278 "Select the first thread in the search results."
280 (goto-char (point-min)))
282 (defface notmuch-message-summary-face
283 '((((class color) (background light)) (:background "#f0f0f0"))
284 (((class color) (background dark)) (:background "#303030")))
285 "Face for the single-line message summary in notmuch-show-mode."
288 (defface notmuch-search-date
289 '((t :inherit default))
290 "Face used in search mode for dates."
293 (defface notmuch-search-count
294 '((t :inherit default))
295 "Face used in search mode for the count matching the query."
298 (defface notmuch-search-subject
299 '((t :inherit default))
300 "Face used in search mode for subjects."
303 (defface notmuch-search-matching-authors
304 '((t :inherit default))
305 "Face used in search mode for authors matching the query."
308 (defface notmuch-search-non-matching-authors
311 (:foreground "grey30"))
314 (:foreground "grey60"))
317 "Face used in search mode for authors not matching the query."
320 (defface notmuch-tag-face
323 (:foreground "OliveDrab1"))
326 (:foreground "navy blue" :bold t))
329 "Face used in search mode face for tags."
333 (defun notmuch-search-mode ()
334 "Major mode displaying results of a notmuch search.
336 This buffer contains the results of a \"notmuch search\" of your
337 email archives. Each line in the buffer represents a single
338 thread giving a summary of the thread (a relative date, the
339 number of matched messages and total messages in the thread,
340 participants in the thread, a representative subject line, and
343 Pressing \\[notmuch-search-show-thread] on any line displays that thread. The '\\[notmuch-search-add-tag]' and '\\[notmuch-search-remove-tag]'
344 keys can be used to add or remove tags from a thread. The '\\[notmuch-search-archive-thread]' key
345 is a convenience for archiving a thread (removing the \"inbox\"
346 tag). The '\\[notmuch-search-operate-all]' key can be used to add or remove a tag from all
347 threads in the current buffer.
349 Other useful commands are '\\[notmuch-search-filter]' for filtering the current search
350 based on an additional query string, '\\[notmuch-search-filter-by-tag]' for filtering to include
351 only messages with a given tag, and '\\[notmuch-search]' to execute a new, global
354 Complete list of currently available key bindings:
356 \\{notmuch-search-mode-map}"
358 (kill-all-local-variables)
359 (make-local-variable 'notmuch-search-query-string)
360 (make-local-variable 'notmuch-search-oldest-first)
361 (make-local-variable 'notmuch-search-target-thread)
362 (make-local-variable 'notmuch-search-target-line)
363 (set (make-local-variable 'notmuch-search-continuation) nil)
364 (set (make-local-variable 'scroll-preserve-screen-position) t)
365 (add-to-invisibility-spec 'notmuch-search)
366 (use-local-map notmuch-search-mode-map)
367 (setq truncate-lines t)
368 (setq major-mode 'notmuch-search-mode
369 mode-name "notmuch-search")
370 (setq buffer-read-only t))
372 (defun notmuch-search-properties-in-region (property beg end)
375 (last-line (line-number-at-pos end))
376 (max-line (- (line-number-at-pos (point-max)) 2)))
379 (while (<= (line-number-at-pos) (min last-line max-line))
380 (setq output (cons (get-text-property (point) property) output))
384 (defun notmuch-search-find-thread-id ()
385 "Return the thread for the current thread"
386 (get-text-property (point) 'notmuch-search-thread-id))
388 (defun notmuch-search-find-thread-id-region (beg end)
389 "Return a list of threads for the current region"
390 (notmuch-search-properties-in-region 'notmuch-search-thread-id beg end))
392 (defun notmuch-search-find-authors ()
393 "Return the authors for the current thread"
394 (get-text-property (point) 'notmuch-search-authors))
396 (defun notmuch-search-find-authors-region (beg end)
397 "Return a list of authors for the current region"
398 (notmuch-search-properties-in-region 'notmuch-search-authors beg end))
400 (defun notmuch-search-find-subject ()
401 "Return the subject for the current thread"
402 (get-text-property (point) 'notmuch-search-subject))
404 (defun notmuch-search-find-subject-region (beg end)
405 "Return a list of authors for the current region"
406 (notmuch-search-properties-in-region 'notmuch-search-subject beg end))
408 (defun notmuch-search-show-thread ()
409 "Display the currently selected thread."
411 (let ((thread-id (notmuch-search-find-thread-id))
412 (subject (notmuch-search-find-subject)))
413 (if (> (length thread-id) 0)
414 (notmuch-show thread-id
416 notmuch-search-query-string
417 ;; name the buffer based on notmuch-search-find-subject
418 (if (string-match "^[ \t]*$" subject)
420 (truncate-string-to-width
422 (truncate-string-to-width subject 32 nil nil t)
425 (error "End of search results"))))
427 (defun notmuch-search-reply-to-thread ()
428 "Begin composing a reply to the entire current thread in a new buffer."
430 (let ((message-id (notmuch-search-find-thread-id)))
431 (notmuch-mua-reply message-id)))
433 (defun notmuch-call-notmuch-process (&rest args)
434 "Synchronously invoke \"notmuch\" with the given list of arguments.
436 Output from the process will be presented to the user as an error
437 and will also appear in a buffer named \"*Notmuch errors*\"."
438 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
439 (with-current-buffer error-buffer
441 (if (eq (apply 'call-process notmuch-command nil error-buffer nil args) 0)
444 (with-current-buffer error-buffer
445 (let ((beg (point-min))
446 (end (- (point-max) 1)))
447 (error (buffer-substring beg end))
450 (defun notmuch-search-set-tags (tags)
453 (re-search-backward "(")
456 (inhibit-read-only t))
457 (re-search-forward ")")
460 (delete-region beg end)
461 (insert (propertize (mapconcat 'identity tags " ")
462 'face 'notmuch-tag-face))))))
464 (defun notmuch-search-get-tags ()
467 (re-search-backward "(")
468 (let ((beg (+ (point) 1)))
469 (re-search-forward ")")
470 (let ((end (- (point) 1)))
471 (split-string (buffer-substring beg end))))))
473 (defun notmuch-search-get-tags-region (beg end)
476 (last-line (line-number-at-pos end))
477 (max-line (- (line-number-at-pos (point-max)) 2)))
479 (while (<= (line-number-at-pos) (min last-line max-line))
480 (setq output (append output (notmuch-search-get-tags)))
484 (defun notmuch-search-add-tag-thread (tag)
485 (notmuch-search-add-tag-region tag (point) (point)))
487 (defun notmuch-search-add-tag-region (tag beg end)
488 (let ((search-id-string (mapconcat 'identity (notmuch-search-find-thread-id-region beg end) " or ")))
489 (notmuch-call-notmuch-process "tag" (concat "+" tag) search-id-string)
491 (let ((last-line (line-number-at-pos end))
492 (max-line (- (line-number-at-pos (point-max)) 2)))
494 (while (<= (line-number-at-pos) (min last-line max-line))
495 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<)))
498 (defun notmuch-search-remove-tag-thread (tag)
499 (notmuch-search-remove-tag-region tag (point) (point)))
501 (defun notmuch-search-remove-tag-region (tag beg end)
502 (let ((search-id-string (mapconcat 'identity (notmuch-search-find-thread-id-region beg end) " or ")))
503 (notmuch-call-notmuch-process "tag" (concat "-" tag) search-id-string)
505 (let ((last-line (line-number-at-pos end))
506 (max-line (- (line-number-at-pos (point-max)) 2)))
508 (while (<= (line-number-at-pos) (min last-line max-line))
509 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags)))
512 (defun notmuch-search-add-tag (tag)
513 "Add a tag to the currently selected thread or region.
515 The tag is added to all messages in the currently selected thread
516 or threads in the current region."
518 (list (notmuch-select-tag-with-completion "Tag to add: ")))
520 (if (region-active-p)
521 (let* ((beg (region-beginning))
523 (notmuch-search-add-tag-region tag beg end))
524 (notmuch-search-add-tag-thread tag))))
526 (defun notmuch-search-remove-tag (tag)
527 "Remove a tag from the currently selected thread or region.
529 The tag is removed from all messages in the currently selected
530 thread or threads in the current region."
532 (list (notmuch-select-tag-with-completion
534 (if (region-active-p)
536 (notmuch-search-find-thread-id-region (region-beginning) (region-end))
538 (notmuch-search-find-thread-id)))))
540 (if (region-active-p)
541 (let* ((beg (region-beginning))
543 (notmuch-search-remove-tag-region tag beg end))
544 (notmuch-search-remove-tag-thread tag))))
546 (defun notmuch-search-archive-thread ()
547 "Archive the currently selected thread (remove its \"inbox\" tag).
549 This function advances the next thread when finished."
551 (notmuch-search-remove-tag-thread "inbox")
554 (defun notmuch-search-process-sentinel (proc msg)
555 "Add a message to let user know when \"notmuch search\" exits"
556 (let ((buffer (process-buffer proc))
557 (status (process-status proc))
558 (exit-status (process-exit-status proc))
559 (never-found-target-thread nil))
560 (if (memq status '(exit signal))
561 (if (buffer-live-p buffer)
562 (with-current-buffer buffer
564 (let ((inhibit-read-only t)
566 (goto-char (point-max))
567 (if (eq status 'signal)
568 (insert "Incomplete search results (search process was killed).\n"))
569 (if (eq status 'exit)
571 (insert "End of search results.")
572 (if (not (= exit-status 0))
573 (insert (format " (process returned %d)" exit-status)))
576 (not (string= notmuch-search-target-thread "found")))
577 (set 'never-found-target-thread t))))))
578 (when (and never-found-target-thread
579 notmuch-search-target-line)
580 (goto-char (point-min))
581 (forward-line (1- notmuch-search-target-line))))))))
583 (defcustom notmuch-search-line-faces nil
584 "Tag/face mapping for line highlighting in notmuch-search.
586 Here is an example of how to color search results based on tags.
587 (the following text would be placed in your ~/.emacs file):
589 (setq notmuch-search-line-faces '((\"delete\" . '(:foreground \"red\"))
590 (\"unread\" . '(:foreground \"green\"))))
592 Order matters: for lines with multiple tags, the the first
593 matching will be applied."
594 :type '(alist :key-type (string) :value-type (list))
597 (defun notmuch-search-color-line (start end line-tag-list)
598 "Colorize lines in notmuch-show based on tags"
599 (if notmuch-search-line-faces
600 (let ((overlay (make-overlay start end))
601 (tags-faces (copy-alist notmuch-search-line-faces)))
603 (let* ((tag-face (car tags-faces))
605 (face (cdr tag-face)))
606 (cond ((member tag line-tag-list)
607 (overlay-put overlay 'face face)
608 (setq tags-faces nil))
610 (setq tags-faces (cdr tags-faces)))))))))
612 (defun notmuch-search-isearch-authors-show (overlay)
613 (remove-from-invisibility-spec (cons (overlay-get overlay 'invisible) t)))
615 (defun notmuch-search-insert-authors (format-string authors)
616 (let* ((propertized-authors
617 ;; Need to save the match data to avoid interfering with
618 ;; `notmuch-search-process-filter'.
620 ;; Authors that don't match the search query are shown in a
622 (if (string-match "\\(.*\\)|\\(..*\\)" authors)
623 (concat (propertize (concat (match-string 1 authors) ",")
624 'face 'notmuch-search-matching-authors)
625 (propertize (match-string 2 authors)
626 'face 'notmuch-search-non-matching-authors))
627 (propertize authors 'face 'notmuch-search-matching-authors))))
629 (formatted-sample (format format-string ""))
630 (formatted-authors (format format-string propertized-authors))
631 visible-string invisible-string)
633 ;; Determine the part of the authors that will be visible by
635 (if (> (length formatted-authors)
636 (length formatted-sample))
637 ;; 4 is `(length "... ")'.
638 (let ((visible-length (- (length formatted-sample) 4)))
639 (setq visible-string (substring propertized-authors 0 visible-length)
640 invisible-string (substring propertized-authors visible-length)))
641 (setq visible-string formatted-authors
642 invisible-string nil))
644 ;; Insert both the visible and invisible author strings.
645 (insert visible-string)
646 (when invisible-string
647 (let ((start (point))
648 (invis-spec (make-symbol "notmuch-search-authors"))
650 (insert invisible-string)
651 ;; Using a cons-cell here causes an ellipsis to be inserted
652 ;; instead of the invisible text.
653 (add-to-invisibility-spec (cons invis-spec t))
654 (setq overlay (make-overlay start (point)))
655 (overlay-put overlay 'invisible invis-spec)
656 (overlay-put overlay 'isearch-open-invisible #'notmuch-search-isearch-authors-show)
659 (defun notmuch-search-insert-field (field date count authors subject tags)
661 ((string-equal field "date")
662 (insert (propertize (format (cdr (assoc field notmuch-search-result-format)) date)
663 'face 'notmuch-search-date)))
664 ((string-equal field "count")
665 (insert (propertize (format (cdr (assoc field notmuch-search-result-format)) count)
666 'face 'notmuch-search-count)))
667 ((string-equal field "subject")
668 (insert (propertize (format (cdr (assoc field notmuch-search-result-format)) subject)
669 'face 'notmuch-search-subject)))
671 ((string-equal field "authors")
672 (notmuch-search-insert-authors (cdr (assoc field notmuch-search-result-format)) authors))
674 ((string-equal field "tags")
675 (insert (concat "(" (propertize tags 'font-lock-face 'notmuch-tag-face) ")")))))
677 (defun notmuch-search-show-result (date count authors subject tags)
678 (let ((fields) (field))
679 (setq fields (mapcar 'car notmuch-search-result-format))
680 (loop for field in fields
681 do (notmuch-search-insert-field field date count authors subject tags)))
684 (defun notmuch-search-process-filter (proc string)
685 "Process and filter the output of \"notmuch search\""
686 (let ((buffer (process-buffer proc))
688 (if (buffer-live-p buffer)
689 (with-current-buffer buffer
693 (inhibit-read-only t))
695 (if (string-match "^\\(thread:[0-9A-Fa-f]*\\) \\([^][]*\\) \\(\\[[0-9/]*\\]\\) \\([^;]*\\); \\(.*\\) (\\([^()]*\\))$" string line)
696 (let* ((thread-id (match-string 1 string))
697 (date (match-string 2 string))
698 (count (match-string 3 string))
699 (authors (match-string 4 string))
700 (subject (match-string 5 string))
701 (tags (match-string 6 string))
702 (tag-list (if tags (save-match-data (split-string tags)))))
703 (goto-char (point-max))
704 (let ((beg (point-marker)))
705 (notmuch-search-show-result date count authors subject tags)
706 (notmuch-search-color-line beg (point-marker) tag-list)
707 (put-text-property beg (point-marker) 'notmuch-search-thread-id thread-id)
708 (put-text-property beg (point-marker) 'notmuch-search-authors authors)
709 (put-text-property beg (point-marker) 'notmuch-search-subject subject)
710 (if (string= thread-id notmuch-search-target-thread)
712 (set 'found-target beg)
713 (set 'notmuch-search-target-thread "found"))))
714 (set 'line (match-end 0)))
717 (goto-char found-target)))
718 (delete-process proc))))
720 (defun notmuch-search-operate-all (action)
721 "Add/remove tags from all matching messages.
723 Tis command adds or removes tags from all messages matching the
724 current search terms. When called interactively, this command
725 will prompt for tags to be added or removed. Tags prefixed with
726 '+' will be added and tags prefixed with '-' will be removed.
728 Each character of the tag name may consist of alphanumeric
729 characters as well as `_.+-'.
731 (interactive "sOperation (+add -drop): notmuch tag ")
732 (let ((action-split (split-string action " +")))
733 ;; Perform some validation
734 (let ((words action-split))
735 (when (null words) (error "No operation given"))
737 (unless (string-match-p "^[-+][-+_.[:word:]]+$" (car words))
738 (error "Action must be of the form `+thistag -that_tag'"))
739 (setq words (cdr words))))
740 (apply 'notmuch-call-notmuch-process "tag"
741 (append action-split (list notmuch-search-query-string) nil))))
743 (defun notmuch-search-buffer-title (query)
744 "Returns the title for a buffer with notmuch search results."
748 (loop for tuple in notmuch-saved-searches
749 if (let ((quoted-query (regexp-quote (cdr tuple))))
750 (and (string-match (concat "^" quoted-query) query)
751 (> (length (match-string 0 query))
753 do (setq longest tuple))
755 (saved-search-name (car saved-search))
756 (saved-search-query (cdr saved-search)))
757 (cond ((and saved-search (equal saved-search-query query))
758 ;; Query is the same as saved search (ignoring case)
759 (concat "*notmuch-saved-search-" saved-search-name "*"))
761 (concat "*notmuch-search-"
762 (replace-regexp-in-string (concat "^" (regexp-quote saved-search-query))
763 (concat "[ " saved-search-name " ]")
767 (concat "*notmuch-search-" query "*"))
771 (defun notmuch-search (query &optional oldest-first target-thread target-line continuation)
772 "Run \"notmuch search\" with the given query string and display results.
774 The optional parameters are used as follows:
776 oldest-first: A Boolean controlling the sort order of returned threads
777 target-thread: A thread ID (with the thread: prefix) that will be made
778 current if it appears in the search results.
779 target-line: The line number to move to if the target thread does not
780 appear in the search results."
781 (interactive "sNotmuch search: ")
782 (let ((buffer (get-buffer-create (notmuch-search-buffer-title query))))
783 (switch-to-buffer buffer)
784 (notmuch-search-mode)
785 (set 'notmuch-search-query-string query)
786 (set 'notmuch-search-oldest-first oldest-first)
787 (set 'notmuch-search-target-thread target-thread)
788 (set 'notmuch-search-target-line target-line)
789 (set 'notmuch-search-continuation continuation)
790 (let ((proc (get-buffer-process (current-buffer)))
791 (inhibit-read-only t))
793 (error "notmuch search process already running for query `%s'" query)
796 (goto-char (point-min))
798 (let ((proc (start-process-shell-command
799 "notmuch-search" buffer notmuch-command "search"
800 (if oldest-first "--sort=oldest-first" "--sort=newest-first")
801 (shell-quote-argument query))))
802 (set-process-sentinel proc 'notmuch-search-process-sentinel)
803 (set-process-filter proc 'notmuch-search-process-filter))))
804 (run-hooks 'notmuch-search-hook)))
806 (defun notmuch-search-refresh-view ()
807 "Refresh the current view.
809 Kills the current buffer and runs a new search with the same
810 query string as the current search. If the current thread is in
811 the new search results, then point will be placed on the same
812 thread. Otherwise, point will be moved to attempt to be in the
813 same relative position within the new buffer."
815 (let ((target-line (line-number-at-pos))
816 (oldest-first notmuch-search-oldest-first)
817 (target-thread (notmuch-search-find-thread-id))
818 (query notmuch-search-query-string)
819 (continuation notmuch-search-continuation))
821 (notmuch-search query oldest-first target-thread target-line continuation)
822 (goto-char (point-min))))
824 (defcustom notmuch-poll-script ""
825 "An external script to incorporate new mail into the notmuch database.
827 If this variable is non empty, then it should name a script to be
828 invoked by `notmuch-search-poll-and-refresh-view' and
829 `notmuch-hello-poll-and-update' (each have a default keybinding
830 of 'G'). The script could do any of the following depending on
833 1. Invoke a program to transfer mail to the local mail store
834 2. Invoke \"notmuch new\" to incorporate the new mail
835 3. Invoke one or more \"notmuch tag\" commands to classify the mail"
839 (defun notmuch-poll ()
840 "Run external script to import mail.
842 Invokes `notmuch-poll-script' if it is not set to an empty string."
844 (if (not (string= notmuch-poll-script ""))
845 (call-process notmuch-poll-script nil nil)))
847 (defun notmuch-search-poll-and-refresh-view ()
848 "Invoke `notmuch-poll' to import mail, then refresh the current view."
851 (notmuch-search-refresh-view))
853 (defun notmuch-search-toggle-order ()
854 "Toggle the current search order.
856 By default, the \"inbox\" view created by `notmuch' is displayed
857 in chronological order (oldest thread at the beginning of the
858 buffer), while any global searches created by `notmuch-search'
859 are displayed in reverse-chronological order (newest thread at
860 the beginning of the buffer).
862 This command toggles the sort order for the current search.
864 Note that any filtered searches created by
865 `notmuch-search-filter' retain the search order of the parent
868 (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
869 (notmuch-search-refresh-view))
871 (defun notmuch-search-filter (query)
872 "Filter the current search results based on an additional query string.
874 Runs a new search matching only messages that match both the
875 current search results AND the additional query string provided."
876 (interactive "sFilter search: ")
877 (let ((grouped-query (if (string-match-p notmuch-search-disjunctive-regexp query)
878 (concat "( " query " )")
880 (notmuch-search (if (string= notmuch-search-query-string "*")
882 (concat notmuch-search-query-string " and " grouped-query)) notmuch-search-oldest-first)))
884 (defun notmuch-search-filter-by-tag (tag)
885 "Filter the current search results based on a single tag.
887 Runs a new search matching only messages that match both the
888 current search results AND that are tagged with the given tag."
890 (list (notmuch-select-tag-with-completion "Filter by tag: ")))
891 (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
895 "Run notmuch and display saved searches, known tags, etc."
899 (setq mail-user-agent 'notmuch-user-agent)