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).
54 (require 'notmuch-lib)
55 (require 'notmuch-show)
57 (defun notmuch-select-tag-with-completion (prompt &rest search-terms)
59 (with-output-to-string
60 (with-current-buffer standard-output
61 (apply 'call-process notmuch-command nil t nil "search-tags" search-terms)))))
62 (completing-read prompt (split-string tag-list "\n+" t) nil nil nil)))
64 (defun notmuch-foreach-mime-part (function mm-handle)
65 (cond ((stringp (car mm-handle))
66 (dolist (part (cdr mm-handle))
67 (notmuch-foreach-mime-part function part)))
68 ((bufferp (car mm-handle))
69 (funcall function mm-handle))
70 (t (dolist (part mm-handle)
71 (notmuch-foreach-mime-part function part)))))
73 (defun notmuch-count-attachments (mm-handle)
75 (notmuch-foreach-mime-part
77 (let ((disposition (mm-handle-disposition p)))
78 (and (listp disposition)
79 (or (equal (car disposition) "attachment")
80 (and (equal (car disposition) "inline")
81 (assq 'filename disposition)))
86 (defun notmuch-save-attachments (mm-handle &optional queryp)
87 (notmuch-foreach-mime-part
89 (let ((disposition (mm-handle-disposition p)))
90 (and (listp disposition)
91 (or (equal (car disposition) "attachment")
92 (and (equal (car disposition) "inline")
93 (assq 'filename disposition)))
96 (concat "Save '" (cdr (assq 'filename disposition)) "' ")))
100 (defun notmuch-reply (query-string)
101 (switch-to-buffer (generate-new-buffer "notmuch-draft"))
102 (call-process notmuch-command nil t nil "reply" query-string)
103 (message-insert-signature)
104 (goto-char (point-min))
105 (if (re-search-forward "^$" nil t)
107 (insert "--text follows this line--")
111 (defun notmuch-toggle-invisible-action (cite-button)
112 (let ((invis-spec (button-get cite-button 'invisibility-spec)))
113 (if (invisible-p invis-spec)
114 (remove-from-invisibility-spec invis-spec)
115 (add-to-invisibility-spec invis-spec)
117 (force-window-update)
120 (define-button-type 'notmuch-button-citation-toggle-type 'help-echo "mouse-1, RET: Show citation"
121 :supertype 'notmuch-button-invisibility-toggle-type)
122 (define-button-type 'notmuch-button-signature-toggle-type 'help-echo "mouse-1, RET: Show signature"
123 :supertype 'notmuch-button-invisibility-toggle-type)
124 (define-button-type 'notmuch-button-body-toggle-type
125 'help-echo "mouse-1, RET: Show message"
126 'face 'notmuch-message-summary-face
127 :supertype 'notmuch-button-invisibility-toggle-type)
129 (defun notmuch-fontify-headers ()
130 (while (looking-at "[[:space:]]")
132 (if (looking-at "[Tt]o:")
134 (overlay-put (make-overlay (point) (re-search-forward ":"))
135 'face 'message-header-name)
136 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
137 'face 'message-header-to))
138 (if (looking-at "[B]?[Cc][Cc]:")
140 (overlay-put (make-overlay (point) (re-search-forward ":"))
141 'face 'message-header-name)
142 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
143 'face 'message-header-cc))
144 (if (looking-at "[Ss]ubject:")
146 (overlay-put (make-overlay (point) (re-search-forward ":"))
147 'face 'message-header-name)
148 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
149 'face 'message-header-subject))
150 (if (looking-at "[Ff]rom:")
152 (overlay-put (make-overlay (point) (re-search-forward ":"))
153 'face 'message-header-name)
154 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
155 'face 'message-header-other))
156 (if (looking-at "[Dd]ate:")
158 (overlay-put (make-overlay (point) (re-search-forward ":"))
159 'face 'message-header-name)
160 (overlay-put (make-overlay (point) (re-search-forward ".*$"))
161 'face 'message-header-other))))))))
163 (defun notmuch-documentation-first-line (symbol)
164 "Return the first line of the documentation string for SYMBOL."
165 (let ((doc (documentation symbol)))
168 (insert (documentation symbol t))
169 (goto-char (point-min))
172 (buffer-substring beg (point))))
175 (defun notmuch-prefix-key-description (key)
176 "Given a prefix key code, return a human-readable string representation.
178 This is basically just `format-kbd-macro' but we also convert ESC to M-."
179 (let ((desc (format-kbd-macro (vector key))))
180 (if (string= desc "ESC")
184 ; I would think that emacs would have code handy for walking a keymap
185 ; and generating strings for each key, and I would prefer to just call
186 ; that. But I couldn't find any (could be all implemented in C I
187 ; suppose), so I wrote my own here.
188 (defun notmuch-substitute-one-command-key-with-prefix (prefix binding)
189 "For a key binding, return a string showing a human-readable
190 representation of the prefixed key as well as the first line of
191 documentation from the bound function.
193 For a mouse binding, return nil."
194 (let ((key (car binding))
195 (action (cdr binding)))
196 (if (mouse-event-p key)
199 (let ((substitute (apply-partially 'notmuch-substitute-one-command-key-with-prefix (notmuch-prefix-key-description key)))
201 (map-keymap (lambda (a b)
202 (push (cons a b) as-list))
204 (mapconcat substitute as-list "\n"))
205 (concat prefix (format-kbd-macro (vector key))
207 (notmuch-documentation-first-line action))))))
209 (defalias 'notmuch-substitute-one-command-key
210 (apply-partially 'notmuch-substitute-one-command-key-with-prefix nil))
212 (defun notmuch-substitute-command-keys (doc)
213 "Like `substitute-command-keys' but with documentation, not function names."
215 (while (string-match "\\\\{\\([^}[:space:]]*\\)}" doc beg)
216 (let ((map (substring doc (match-beginning 1) (match-end 1))))
217 (setq doc (replace-match (mapconcat 'notmuch-substitute-one-command-key
218 (cdr (symbol-value (intern map))) "\n") 1 1 doc)))
219 (setq beg (match-end 0)))
222 (defun notmuch-help ()
223 "Display help for the current notmuch mode."
225 (let* ((mode major-mode)
226 (doc (substitute-command-keys (notmuch-substitute-command-keys (documentation mode t)))))
227 (with-current-buffer (generate-new-buffer "*notmuch-help*")
229 (goto-char (point-min))
230 (set-buffer-modified-p nil)
231 (view-buffer (current-buffer) 'kill-buffer-if-not-modified))))
233 (defgroup notmuch nil
234 "Notmuch mail reader for Emacs."
237 (defcustom notmuch-search-hook nil
238 "List of functions to call when notmuch displays the search results."
240 :options '(hl-line-mode)
243 (defvar notmuch-search-authors-width 20
244 "Number of columns to use to display authors in a notmuch-search buffer.")
246 (defvar notmuch-search-mode-map
247 (let ((map (make-sparse-keymap)))
248 (define-key map "?" 'notmuch-help)
249 (define-key map "q" 'kill-this-buffer)
250 (define-key map "x" 'kill-this-buffer)
251 (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
252 (define-key map "b" 'notmuch-search-scroll-down)
253 (define-key map " " 'notmuch-search-scroll-up)
254 (define-key map "<" 'notmuch-search-first-thread)
255 (define-key map ">" 'notmuch-search-last-thread)
256 (define-key map "p" 'notmuch-search-previous-thread)
257 (define-key map "n" 'notmuch-search-next-thread)
258 (define-key map "r" 'notmuch-search-reply-to-thread)
259 (define-key map "m" 'message-mail)
260 (define-key map "s" 'notmuch-search)
261 (define-key map "o" 'notmuch-search-toggle-order)
262 (define-key map "=" 'notmuch-search-refresh-view)
263 (define-key map "t" 'notmuch-search-filter-by-tag)
264 (define-key map "f" 'notmuch-search-filter)
265 (define-key map [mouse-1] 'notmuch-search-show-thread)
266 (define-key map "*" 'notmuch-search-operate-all)
267 (define-key map "a" 'notmuch-search-archive-thread)
268 (define-key map "-" 'notmuch-search-remove-tag)
269 (define-key map "+" 'notmuch-search-add-tag)
270 (define-key map (kbd "RET") 'notmuch-search-show-thread)
271 (define-key map "F" 'notmuch-folder)
273 "Keymap for \"notmuch search\" buffers.")
274 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
276 (defvar notmuch-search-query-string)
277 (defvar notmuch-search-target-thread)
278 (defvar notmuch-search-target-line)
279 (defvar notmuch-search-oldest-first t
280 "Show the oldest mail first in the search-mode")
282 (defvar notmuch-search-disjunctive-regexp "\\<[oO][rR]\\>")
284 (defun notmuch-search-scroll-up ()
285 "Move forward through search results by one window's worth."
289 ((end-of-buffer) (notmuch-search-last-thread))))
291 (defun notmuch-search-scroll-down ()
292 "Move backward through the search results by one window's worth."
294 ; I don't know why scroll-down doesn't signal beginning-of-buffer
295 ; the way that scroll-up signals end-of-buffer, but c'est la vie.
297 ; So instead of trapping a signal we instead check whether the
298 ; window begins on the first line of the buffer and if so, move
299 ; directly to that position. (We have to count lines since the
300 ; window-start position is not the same as point-min due to the
301 ; invisible thread-ID characters on the first line.
302 (if (equal (count-lines (point-min) (window-start)) 0)
303 (goto-char (point-min))
306 (defun notmuch-search-next-thread ()
307 "Select the next thread in the search results."
311 (defun notmuch-search-previous-thread ()
312 "Select the previous thread in the search results."
316 (defun notmuch-search-last-thread ()
317 "Select the last thread in the search results."
319 (goto-char (point-max))
322 (defun notmuch-search-first-thread ()
323 "Select the first thread in the search results."
325 (goto-char (point-min)))
327 (defface notmuch-message-summary-face
328 '((((class color) (background light)) (:background "#f0f0f0"))
329 (((class color) (background dark)) (:background "#303030")))
330 "Face for the single-line message summary in notmuch-show-mode."
333 (defface notmuch-tag-face
336 (:foreground "OliveDrab1"))
339 (:foreground "navy blue" :bold t))
342 "Notmuch search mode face used to highligh tags."
345 (defvar notmuch-tag-face-alist nil
346 "List containing the tag list that need to be highlighed")
348 (defvar notmuch-search-font-lock-keywords nil)
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 thread. The '\\[notmuch-search-add-tag]' and '\\[notmuch-search-remove-tag]'
362 keys can be used to add or remove tags from a thread. The '\\[notmuch-search-archive-thread]' key
363 is a convenience for archiving a thread (removing the \"inbox\"
364 tag). The '\\[notmuch-search-operate-all]' key can be used to add or remove a tag from all
365 threads in the current buffer.
367 Other useful commands are '\\[notmuch-search-filter]' for filtering the current search
368 based on an additional query string, '\\[notmuch-search-filter-by-tag]' for filtering to include
369 only messages with a given tag, and '\\[notmuch-search]' to execute a new, global
372 Complete list of currently available key bindings:
374 \\{notmuch-search-mode-map}"
376 (kill-all-local-variables)
377 (make-local-variable 'notmuch-search-query-string)
378 (make-local-variable 'notmuch-search-oldest-first)
379 (make-local-variable 'notmuch-search-target-thread)
380 (make-local-variable 'notmuch-search-target-line)
381 (set (make-local-variable 'scroll-preserve-screen-position) t)
382 (add-to-invisibility-spec 'notmuch-search)
383 (use-local-map notmuch-search-mode-map)
384 (setq truncate-lines t)
385 (setq major-mode 'notmuch-search-mode
386 mode-name "notmuch-search")
387 (setq buffer-read-only t)
388 (if (not notmuch-tag-face-alist)
389 (add-to-list 'notmuch-search-font-lock-keywords (list
390 "(\\([^()]*\\))$" '(1 'notmuch-tag-face)))
391 (let ((notmuch-search-tags (mapcar 'car notmuch-tag-face-alist)))
392 (loop for notmuch-search-tag in notmuch-search-tags
393 do (add-to-list 'notmuch-search-font-lock-keywords (list
394 (concat "([^)]*\\(" notmuch-search-tag "\\)[^)]*)$")
395 `(1 ,(cdr (assoc notmuch-search-tag notmuch-tag-face-alist))))))))
396 (set (make-local-variable 'font-lock-defaults)
397 '(notmuch-search-font-lock-keywords t)))
399 (defun notmuch-search-properties-in-region (property beg end)
402 (last-line (line-number-at-pos end))
403 (max-line (- (line-number-at-pos (point-max)) 2)))
406 (while (<= (line-number-at-pos) (min last-line max-line))
407 (setq output (cons (get-text-property (point) property) output))
411 (defun notmuch-search-find-thread-id ()
412 "Return the thread for the current thread"
413 (get-text-property (point) 'notmuch-search-thread-id))
415 (defun notmuch-search-find-thread-id-region (beg end)
416 "Return a list of threads for the current region"
417 (notmuch-search-properties-in-region 'notmuch-search-thread-id beg end))
419 (defun notmuch-search-find-authors ()
420 "Return the authors for the current thread"
421 (get-text-property (point) 'notmuch-search-authors))
423 (defun notmuch-search-find-authors-region (beg end)
424 "Return a list of authors for the current region"
425 (notmuch-search-properties-in-region 'notmuch-search-authors beg end))
427 (defun notmuch-search-find-subject ()
428 "Return the subject for the current thread"
429 (get-text-property (point) 'notmuch-search-subject))
431 (defun notmuch-search-find-subject-region (beg end)
432 "Return a list of authors for the current region"
433 (notmuch-search-properties-in-region 'notmuch-search-subject beg end))
435 (defun notmuch-search-show-thread ()
436 "Display the currently selected thread."
438 (let ((thread-id (notmuch-search-find-thread-id))
439 (subject (notmuch-search-find-subject))
441 (when (string-match "^[ \t]*$" subject)
442 (setq subject "[No Subject]"))
443 (setq buffer-name (concat "*"
444 (truncate-string-to-width subject 32 nil nil t)
446 (if (> (length thread-id) 0)
447 (notmuch-show thread-id
449 notmuch-search-query-string
451 (error "End of search results"))))
453 (defun notmuch-search-reply-to-thread ()
454 "Begin composing a reply to the entire current thread in a new buffer."
456 (let ((message-id (notmuch-search-find-thread-id)))
457 (notmuch-reply message-id)))
459 (defun notmuch-call-notmuch-process (&rest args)
460 "Synchronously invoke \"notmuch\" with the given list of arguments.
462 Output from the process will be presented to the user as an error
463 and will also appear in a buffer named \"*Notmuch errors*\"."
464 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
465 (with-current-buffer error-buffer
467 (if (eq (apply 'call-process notmuch-command nil error-buffer nil args) 0)
470 (with-current-buffer error-buffer
471 (let ((beg (point-min))
472 (end (- (point-max) 1)))
473 (error (buffer-substring beg end))
476 (defun notmuch-search-set-tags (tags)
479 (re-search-backward "(")
482 (inhibit-read-only t))
483 (re-search-forward ")")
486 (delete-region beg end)
487 (insert (mapconcat 'identity tags " "))))))
489 (defun notmuch-search-get-tags ()
492 (re-search-backward "(")
493 (let ((beg (+ (point) 1)))
494 (re-search-forward ")")
495 (let ((end (- (point) 1)))
496 (split-string (buffer-substring beg end))))))
498 (defun notmuch-search-get-tags-region (beg end)
501 (last-line (line-number-at-pos end))
502 (max-line (- (line-number-at-pos (point-max)) 2)))
504 (while (<= (line-number-at-pos) (min last-line max-line))
505 (setq output (append output (notmuch-search-get-tags)))
509 (defun notmuch-search-add-tag-thread (tag)
510 (notmuch-search-add-tag-region tag (point) (point)))
512 (defun notmuch-search-add-tag-region (tag beg end)
513 (let ((search-id-string (mapconcat 'identity (notmuch-search-find-thread-id-region beg end) " or ")))
514 (notmuch-call-notmuch-process "tag" (concat "+" tag) search-id-string)
516 (let ((last-line (line-number-at-pos end))
517 (max-line (- (line-number-at-pos (point-max)) 2)))
519 (while (<= (line-number-at-pos) (min last-line max-line))
520 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<)))
523 (defun notmuch-search-remove-tag-thread (tag)
524 (notmuch-search-remove-tag-region tag (point) (point)))
526 (defun notmuch-search-remove-tag-region (tag beg end)
527 (let ((search-id-string (mapconcat 'identity (notmuch-search-find-thread-id-region beg end) " or ")))
528 (notmuch-call-notmuch-process "tag" (concat "-" tag) search-id-string)
530 (let ((last-line (line-number-at-pos end))
531 (max-line (- (line-number-at-pos (point-max)) 2)))
533 (while (<= (line-number-at-pos) (min last-line max-line))
534 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags)))
537 (defun notmuch-search-add-tag (tag)
538 "Add a tag to the currently selected thread or region.
540 The tag is added to all messages in the currently selected thread
541 or threads in the current region."
543 (list (notmuch-select-tag-with-completion "Tag to add: ")))
545 (if (region-active-p)
546 (let* ((beg (region-beginning))
548 (notmuch-search-add-tag-region tag beg end))
549 (notmuch-search-add-tag-thread tag))))
551 (defun notmuch-search-remove-tag (tag)
552 "Remove a tag from the currently selected thread or region.
554 The tag is removed from all messages in the currently selected
555 thread or threads in the current region."
557 (list (notmuch-select-tag-with-completion
559 (if (region-active-p)
561 (notmuch-search-find-thread-id-region (region-beginning) (region-end))
563 (notmuch-search-find-thread-id)))))
565 (if (region-active-p)
566 (let* ((beg (region-beginning))
568 (notmuch-search-remove-tag-region tag beg end))
569 (notmuch-search-remove-tag-thread tag))))
571 (defun notmuch-search-archive-thread ()
572 "Archive the currently selected thread (remove its \"inbox\" tag).
574 This function advances the next thread when finished."
576 (notmuch-search-remove-tag-thread "inbox")
579 (defun notmuch-search-process-sentinel (proc msg)
580 "Add a message to let user know when \"notmuch search\" exits"
581 (let ((buffer (process-buffer proc))
582 (status (process-status proc))
583 (exit-status (process-exit-status proc))
584 (never-found-target-thread nil))
585 (if (memq status '(exit signal))
586 (if (buffer-live-p buffer)
587 (with-current-buffer buffer
589 (let ((inhibit-read-only t)
591 (goto-char (point-max))
592 (if (eq status 'signal)
593 (insert "Incomplete search results (search process was killed).\n"))
594 (if (eq status 'exit)
596 (insert "End of search results.")
597 (if (not (= exit-status 0))
598 (insert (format " (process returned %d)" exit-status)))
601 (not (string= notmuch-search-target-thread "found")))
602 (set 'never-found-target-thread t))))))
603 (if (and never-found-target-thread
604 notmuch-search-target-line)
605 (goto-line notmuch-search-target-line)))))))
607 (defcustom notmuch-search-line-faces nil
608 "Tag/face mapping for line highlighting in notmuch-search.
610 Here is an example of how to color search results based on tags.
611 (the following text would be placed in your ~/.emacs file):
613 (setq notmuch-search-line-faces '((\"delete\" . '(:foreground \"red\"))
614 (\"unread\" . '(:foreground \"green\"))))
616 Order matters: for lines with multiple tags, the the first
617 matching will be applied."
618 :type '(alist :key-type (string) :value-type (list))
621 (defun notmuch-search-color-line (start end line-tag-list)
622 "Colorize lines in notmuch-show based on tags"
623 (if notmuch-search-line-faces
624 (let ((overlay (make-overlay start end))
625 (tags-faces (copy-alist notmuch-search-line-faces)))
627 (let* ((tag-face (car tags-faces))
629 (face (cdr tag-face)))
630 (cond ((member tag line-tag-list)
631 (overlay-put overlay 'face face)
632 (setq tags-faces nil))
634 (setq tags-faces (cdr tags-faces)))))))))
636 (defun notmuch-search-process-filter (proc string)
637 "Process and filter the output of \"notmuch search\""
638 (let ((buffer (process-buffer proc))
640 (if (buffer-live-p buffer)
641 (with-current-buffer buffer
645 (inhibit-read-only t))
647 (if (string-match "^\\(thread:[0-9A-Fa-f]*\\) \\(.*\\) \\(\\[[0-9/]*\\]\\) \\([^;]*\\); \\(.*\\) (\\([^()]*\\))$" string line)
648 (let* ((thread-id (match-string 1 string))
649 (date (match-string 2 string))
650 (count (match-string 3 string))
651 (authors (match-string 4 string))
652 (authors-length (length authors))
653 (subject (match-string 5 string))
654 (tags (match-string 6 string))
655 (tag-list (if tags (save-match-data (split-string tags)))))
656 (if (> authors-length notmuch-search-authors-width)
657 (set 'authors (concat (substring authors 0 (- notmuch-search-authors-width 3)) "...")))
658 (goto-char (point-max))
659 (let ((beg (point-marker))
660 (format-string (format "%%s %%-7s %%-%ds %%s (%%s)\n" notmuch-search-authors-width)))
661 (insert (format format-string date count authors subject tags))
662 (notmuch-search-color-line beg (point-marker) tag-list)
663 (put-text-property beg (point-marker) 'notmuch-search-thread-id thread-id)
664 (put-text-property beg (point-marker) 'notmuch-search-authors authors)
665 (put-text-property beg (point-marker) 'notmuch-search-subject subject)
666 (if (string= thread-id notmuch-search-target-thread)
668 (set 'found-target beg)
669 (set 'notmuch-search-target-thread "found"))))
670 (set 'line (match-end 0)))
673 (goto-char found-target)))
674 (delete-process proc))))
676 (defun notmuch-search-operate-all (action)
677 "Add/remove tags from all matching messages.
679 Tis command adds or removes tags from all messages matching the
680 current search terms. When called interactively, this command
681 will prompt for tags to be added or removed. Tags prefixed with
682 '+' will be added and tags prefixed with '-' will be removed.
684 Each character of the tag name may consist of alphanumeric
685 characters as well as `_.+-'.
687 (interactive "sOperation (+add -drop): notmuch tag ")
688 (let ((action-split (split-string action " +")))
689 ;; Perform some validation
690 (let ((words action-split))
691 (when (null words) (error "No operation given"))
693 (unless (string-match-p "^[-+][-+_.[:word:]]+$" (car words))
694 (error "Action must be of the form `+thistag -that_tag'"))
695 (setq words (cdr words))))
696 (apply 'notmuch-call-notmuch-process "tag"
697 (append action-split (list notmuch-search-query-string) nil))))
700 (defun notmuch-search (query &optional oldest-first target-thread target-line)
701 "Run \"notmuch search\" with the given query string and display results.
703 The optional parameters are used as follows:
705 oldest-first: A Boolean controlling the sort order of returned threads
706 target-thread: A thread ID (with the thread: prefix) that will be made
707 current if it appears in the search results.
708 target-line: The line number to move to if the target thread does not
709 appear in the search results."
710 (interactive "sNotmuch search: ")
711 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
712 (switch-to-buffer buffer)
713 (notmuch-search-mode)
714 (set 'notmuch-search-query-string query)
715 (set 'notmuch-search-oldest-first oldest-first)
716 (set 'notmuch-search-target-thread target-thread)
717 (set 'notmuch-search-target-line target-line)
718 (let ((proc (get-buffer-process (current-buffer)))
719 (inhibit-read-only t))
721 (error "notmuch search process already running for query `%s'" query)
724 (goto-char (point-min))
726 (let ((proc (start-process-shell-command
727 "notmuch-search" buffer notmuch-command "search"
728 (if oldest-first "--sort=oldest-first" "--sort=newest-first")
729 (shell-quote-argument query))))
730 (set-process-sentinel proc 'notmuch-search-process-sentinel)
731 (set-process-filter proc 'notmuch-search-process-filter))))
732 (run-hooks 'notmuch-search-hook)))
734 (defun notmuch-search-refresh-view ()
735 "Refresh the current view.
737 Kills the current buffer and runs a new search with the same
738 query string as the current search. If the current thread is in
739 the new search results, then point will be placed on the same
740 thread. Otherwise, point will be moved to attempt to be in the
741 same relative position within the new buffer."
743 (let ((target-line (line-number-at-pos))
744 (oldest-first notmuch-search-oldest-first)
745 (target-thread (notmuch-search-find-thread-id))
746 (query notmuch-search-query-string))
748 (notmuch-search query oldest-first target-thread target-line)
749 (goto-char (point-min))
752 (defun notmuch-search-toggle-order ()
753 "Toggle the current search order.
755 By default, the \"inbox\" view created by `notmuch' is displayed
756 in chronological order (oldest thread at the beginning of the
757 buffer), while any global searches created by `notmuch-search'
758 are displayed in reverse-chronological order (newest thread at
759 the beginning of the buffer).
761 This command toggles the sort order for the current search.
763 Note that any filtered searches created by
764 `notmuch-search-filter' retain the search order of the parent
767 (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
768 (notmuch-search-refresh-view))
770 (defun notmuch-search-filter (query)
771 "Filter the current search results based on an additional query string.
773 Runs a new search matching only messages that match both the
774 current search results AND the additional query string provided."
775 (interactive "sFilter search: ")
776 (let ((grouped-query (if (string-match-p notmuch-search-disjunctive-regexp query)
777 (concat "( " query " )")
779 (notmuch-search (if (string= notmuch-search-query-string "*")
781 (concat notmuch-search-query-string " and " grouped-query)) notmuch-search-oldest-first)))
783 (defun notmuch-search-filter-by-tag (tag)
784 "Filter the current search results based on a single tag.
786 Runs a new search matching only messages that match both the
787 current search results AND that are tagged with the given tag."
789 (list (notmuch-select-tag-with-completion "Filter by tag: ")))
790 (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
794 "Run notmuch to display all mail with tag of 'inbox'"
796 (notmuch-search "tag:inbox" notmuch-search-oldest-first))
798 (setq mail-user-agent 'message-user-agent)
800 (defvar notmuch-folder-mode-map
801 (let ((map (make-sparse-keymap)))
802 (define-key map "?" 'notmuch-help)
803 (define-key map "x" 'kill-this-buffer)
804 (define-key map "q" 'kill-this-buffer)
805 (define-key map "m" 'message-mail)
806 (define-key map "e" 'notmuch-folder-show-empty-toggle)
807 (define-key map ">" 'notmuch-folder-last)
808 (define-key map "<" 'notmuch-folder-first)
809 (define-key map "=" 'notmuch-folder)
810 (define-key map "s" 'notmuch-search)
811 (define-key map [mouse-1] 'notmuch-folder-show-search)
812 (define-key map (kbd "RET") 'notmuch-folder-show-search)
813 (define-key map " " 'notmuch-folder-show-search)
814 (define-key map "p" 'notmuch-folder-previous)
815 (define-key map "n" 'notmuch-folder-next)
817 "Keymap for \"notmuch folder\" buffers.")
819 (fset 'notmuch-folder-mode-map notmuch-folder-mode-map)
821 (defcustom notmuch-folders (quote (("inbox" . "tag:inbox") ("unread" . "tag:unread")))
822 "List of searches for the notmuch folder view"
823 :type '(alist :key-type (string) :value-type (string))
826 (defun notmuch-folder-mode ()
827 "Major mode for showing notmuch 'folders'.
829 This buffer contains a list of message counts returned by a
830 customizable set of searches of your email archives. Each line in
831 the buffer shows the name of a saved search and the resulting
834 Pressing RET on any line opens a search window containing the
835 results for the saved search on that line.
837 Here is an example of how the search list could be
838 customized, (the following text would be placed in your ~/.emacs
841 (setq notmuch-folders '((\"inbox\" . \"tag:inbox\")
842 (\"unread\" . \"tag:inbox AND tag:unread\")
843 (\"notmuch\" . \"tag:inbox AND to:notmuchmail.org\")))
845 Of course, you can have any number of folders, each configured
846 with any supported search terms (see \"notmuch help search-terms\").
848 Currently available key bindings:
850 \\{notmuch-folder-mode-map}"
852 (kill-all-local-variables)
853 (use-local-map 'notmuch-folder-mode-map)
854 (setq truncate-lines t)
856 (setq major-mode 'notmuch-folder-mode
857 mode-name "notmuch-folder")
858 (setq buffer-read-only t))
860 (defun notmuch-folder-next ()
861 "Select the next folder in the list."
867 (defun notmuch-folder-previous ()
868 "Select the previous folder in the list."
872 (defun notmuch-folder-first ()
873 "Select the first folder in the list."
875 (goto-char (point-min)))
877 (defun notmuch-folder-last ()
878 "Select the last folder in the list."
880 (goto-char (point-max))
883 (defun notmuch-folder-count (search)
884 (car (process-lines notmuch-command "count" search)))
886 (defvar notmuch-folder-show-empty t
887 "Whether `notmuch-folder-mode' should display empty folders.")
889 (defun notmuch-folder-show-empty-toggle ()
890 "Toggle the listing of empty folders"
892 (setq notmuch-folder-show-empty (not notmuch-folder-show-empty))
895 (defun notmuch-folder-add (folders)
897 (let* ((name (car (car folders)))
898 (inhibit-read-only t)
899 (search (cdr (car folders)))
900 (count (notmuch-folder-count search)))
901 (if (or notmuch-folder-show-empty
902 (not (equal count "0")))
910 (notmuch-folder-add (cdr folders)))))
912 (defun notmuch-folder-find-name ()
916 (re-search-forward "\\([ \t]*[^ \t]+\\)")
917 (filter-buffer-substring (match-beginning 1) (match-end 1)))))
919 (defun notmuch-folder-show-search (&optional folder)
920 "Show a search window for the search related to the specified folder."
923 (setq folder (notmuch-folder-find-name)))
924 (let ((search (assoc folder notmuch-folders)))
926 (notmuch-search (cdr search) notmuch-search-oldest-first))))
929 (defun notmuch-folder ()
930 "Show the notmuch folder view and update the displayed counts."
932 (let ((buffer (get-buffer-create "*notmuch-folders*")))
933 (switch-to-buffer buffer)
934 (let ((inhibit-read-only t)
935 (n (line-number-at-pos)))
937 (notmuch-folder-mode)
938 (notmuch-folder-add notmuch-folders)
939 (goto-char (point-min))