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-documentation-first-line (symbol)
112 "Return the first line of the documentation string for SYMBOL."
113 (let ((doc (documentation symbol)))
116 (insert (documentation symbol t))
117 (goto-char (point-min))
120 (buffer-substring beg (point))))
123 (defun notmuch-prefix-key-description (key)
124 "Given a prefix key code, return a human-readable string representation.
126 This is basically just `format-kbd-macro' but we also convert ESC to M-."
127 (let ((desc (format-kbd-macro (vector key))))
128 (if (string= desc "ESC")
132 ; I would think that emacs would have code handy for walking a keymap
133 ; and generating strings for each key, and I would prefer to just call
134 ; that. But I couldn't find any (could be all implemented in C I
135 ; suppose), so I wrote my own here.
136 (defun notmuch-substitute-one-command-key-with-prefix (prefix binding)
137 "For a key binding, return a string showing a human-readable
138 representation of the prefixed key as well as the first line of
139 documentation from the bound function.
141 For a mouse binding, return nil."
142 (let ((key (car binding))
143 (action (cdr binding)))
144 (if (mouse-event-p key)
147 (let ((substitute (apply-partially 'notmuch-substitute-one-command-key-with-prefix (notmuch-prefix-key-description key)))
149 (map-keymap (lambda (a b)
150 (push (cons a b) as-list))
152 (mapconcat substitute as-list "\n"))
153 (concat prefix (format-kbd-macro (vector key))
155 (notmuch-documentation-first-line action))))))
157 (defalias 'notmuch-substitute-one-command-key
158 (apply-partially 'notmuch-substitute-one-command-key-with-prefix nil))
160 (defun notmuch-substitute-command-keys (doc)
161 "Like `substitute-command-keys' but with documentation, not function names."
163 (while (string-match "\\\\{\\([^}[:space:]]*\\)}" doc beg)
164 (let ((map (substring doc (match-beginning 1) (match-end 1))))
165 (setq doc (replace-match (mapconcat 'notmuch-substitute-one-command-key
166 (cdr (symbol-value (intern map))) "\n") 1 1 doc)))
167 (setq beg (match-end 0)))
170 (defun notmuch-help ()
171 "Display help for the current notmuch mode."
173 (let* ((mode major-mode)
174 (doc (substitute-command-keys (notmuch-substitute-command-keys (documentation mode t)))))
175 (with-current-buffer (generate-new-buffer "*notmuch-help*")
177 (goto-char (point-min))
178 (set-buffer-modified-p nil)
179 (view-buffer (current-buffer) 'kill-buffer-if-not-modified))))
181 (defgroup notmuch nil
182 "Notmuch mail reader for Emacs."
185 (defcustom notmuch-search-hook '(hl-line-mode)
186 "List of functions to call when notmuch displays the search results."
188 :options '(hl-line-mode)
191 (defvar notmuch-search-authors-width 20
192 "Number of columns to use to display authors in a notmuch-search buffer.")
194 (defvar notmuch-search-mode-map
195 (let ((map (make-sparse-keymap)))
196 (define-key map "?" 'notmuch-help)
197 (define-key map "q" 'kill-this-buffer)
198 (define-key map "x" 'kill-this-buffer)
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" 'message-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 "t" 'notmuch-search-filter-by-tag)
212 (define-key map "f" 'notmuch-search-filter)
213 (define-key map [mouse-1] 'notmuch-search-show-thread)
214 (define-key map "*" 'notmuch-search-operate-all)
215 (define-key map "a" 'notmuch-search-archive-thread)
216 (define-key map "-" 'notmuch-search-remove-tag)
217 (define-key map "+" 'notmuch-search-add-tag)
218 (define-key map (kbd "RET") 'notmuch-search-show-thread)
220 "Keymap for \"notmuch search\" buffers.")
221 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
223 (defvar notmuch-search-query-string)
224 (defvar notmuch-search-target-thread)
225 (defvar notmuch-search-target-line)
226 (defvar notmuch-search-oldest-first t
227 "Show the oldest mail first in the search-mode")
229 (defvar notmuch-search-disjunctive-regexp "\\<[oO][rR]\\>")
231 (defun notmuch-search-scroll-up ()
232 "Move forward through search results by one window's worth."
236 ((end-of-buffer) (notmuch-search-last-thread))))
238 (defun notmuch-search-scroll-down ()
239 "Move backward through the search results by one window's worth."
241 ; I don't know why scroll-down doesn't signal beginning-of-buffer
242 ; the way that scroll-up signals end-of-buffer, but c'est la vie.
244 ; So instead of trapping a signal we instead check whether the
245 ; window begins on the first line of the buffer and if so, move
246 ; directly to that position. (We have to count lines since the
247 ; window-start position is not the same as point-min due to the
248 ; invisible thread-ID characters on the first line.
249 (if (equal (count-lines (point-min) (window-start)) 0)
250 (goto-char (point-min))
253 (defun notmuch-search-next-thread ()
254 "Select the next thread in the search results."
258 (defun notmuch-search-previous-thread ()
259 "Select the previous thread in the search results."
263 (defun notmuch-search-last-thread ()
264 "Select the last thread in the search results."
266 (goto-char (point-max))
269 (defun notmuch-search-first-thread ()
270 "Select the first thread in the search results."
272 (goto-char (point-min)))
274 (defface notmuch-message-summary-face
275 '((((class color) (background light)) (:background "#f0f0f0"))
276 (((class color) (background dark)) (:background "#303030")))
277 "Face for the single-line message summary in notmuch-show-mode."
280 (defface notmuch-tag-face
283 (:foreground "OliveDrab1"))
286 (:foreground "navy blue" :bold t))
289 "Notmuch search mode face used to highligh tags."
292 (defvar notmuch-tag-face-alist nil
293 "List containing the tag list that need to be highlighed")
295 (defvar notmuch-search-font-lock-keywords nil)
298 (defun notmuch-search-mode ()
299 "Major mode displaying results of a notmuch search.
301 This buffer contains the results of a \"notmuch search\" of your
302 email archives. Each line in the buffer represents a single
303 thread giving a summary of the thread (a relative date, the
304 number of matched messages and total messages in the thread,
305 participants in the thread, a representative subject line, and
308 Pressing \\[notmuch-search-show-thread] on any line displays that thread. The '\\[notmuch-search-add-tag]' and '\\[notmuch-search-remove-tag]'
309 keys can be used to add or remove tags from a thread. The '\\[notmuch-search-archive-thread]' key
310 is a convenience for archiving a thread (removing the \"inbox\"
311 tag). The '\\[notmuch-search-operate-all]' key can be used to add or remove a tag from all
312 threads in the current buffer.
314 Other useful commands are '\\[notmuch-search-filter]' for filtering the current search
315 based on an additional query string, '\\[notmuch-search-filter-by-tag]' for filtering to include
316 only messages with a given tag, and '\\[notmuch-search]' to execute a new, global
319 Complete list of currently available key bindings:
321 \\{notmuch-search-mode-map}"
323 (kill-all-local-variables)
324 (make-local-variable 'notmuch-search-query-string)
325 (make-local-variable 'notmuch-search-oldest-first)
326 (make-local-variable 'notmuch-search-target-thread)
327 (make-local-variable 'notmuch-search-target-line)
328 (set (make-local-variable 'scroll-preserve-screen-position) t)
329 (add-to-invisibility-spec 'notmuch-search)
330 (use-local-map notmuch-search-mode-map)
331 (setq truncate-lines t)
332 (setq major-mode 'notmuch-search-mode
333 mode-name "notmuch-search")
334 (setq buffer-read-only t)
335 (if (not notmuch-tag-face-alist)
336 (add-to-list 'notmuch-search-font-lock-keywords (list
337 "(\\([^()]*\\))$" '(1 'notmuch-tag-face)))
338 (let ((notmuch-search-tags (mapcar 'car notmuch-tag-face-alist)))
339 (loop for notmuch-search-tag in notmuch-search-tags
340 do (add-to-list 'notmuch-search-font-lock-keywords (list
341 (concat "([^)]*\\(" notmuch-search-tag "\\)[^)]*)$")
342 `(1 ,(cdr (assoc notmuch-search-tag notmuch-tag-face-alist))))))))
343 (set (make-local-variable 'font-lock-defaults)
344 '(notmuch-search-font-lock-keywords t)))
346 (defun notmuch-search-properties-in-region (property beg end)
349 (last-line (line-number-at-pos end))
350 (max-line (- (line-number-at-pos (point-max)) 2)))
353 (while (<= (line-number-at-pos) (min last-line max-line))
354 (setq output (cons (get-text-property (point) property) output))
358 (defun notmuch-search-find-thread-id ()
359 "Return the thread for the current thread"
360 (get-text-property (point) 'notmuch-search-thread-id))
362 (defun notmuch-search-find-thread-id-region (beg end)
363 "Return a list of threads for the current region"
364 (notmuch-search-properties-in-region 'notmuch-search-thread-id beg end))
366 (defun notmuch-search-find-authors ()
367 "Return the authors for the current thread"
368 (get-text-property (point) 'notmuch-search-authors))
370 (defun notmuch-search-find-authors-region (beg end)
371 "Return a list of authors for the current region"
372 (notmuch-search-properties-in-region 'notmuch-search-authors beg end))
374 (defun notmuch-search-find-subject ()
375 "Return the subject for the current thread"
376 (get-text-property (point) 'notmuch-search-subject))
378 (defun notmuch-search-find-subject-region (beg end)
379 "Return a list of authors for the current region"
380 (notmuch-search-properties-in-region 'notmuch-search-subject beg end))
382 (defun notmuch-search-show-thread ()
383 "Display the currently selected thread."
385 (let ((thread-id (notmuch-search-find-thread-id))
386 (subject (notmuch-search-find-subject)))
387 (if (> (length thread-id) 0)
388 (notmuch-show thread-id
390 notmuch-search-query-string
391 ;; name the buffer based on notmuch-search-find-subject
392 (if (string-match "^[ \t]*$" subject)
394 (truncate-string-to-width
396 (truncate-string-to-width subject 32 nil nil t)
399 (error "End of search results"))))
401 (defun notmuch-search-reply-to-thread ()
402 "Begin composing a reply to the entire current thread in a new buffer."
404 (let ((message-id (notmuch-search-find-thread-id)))
405 (notmuch-reply message-id)))
407 (defun notmuch-call-notmuch-process (&rest args)
408 "Synchronously invoke \"notmuch\" with the given list of arguments.
410 Output from the process will be presented to the user as an error
411 and will also appear in a buffer named \"*Notmuch errors*\"."
412 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
413 (with-current-buffer error-buffer
415 (if (eq (apply 'call-process notmuch-command nil error-buffer nil args) 0)
418 (with-current-buffer error-buffer
419 (let ((beg (point-min))
420 (end (- (point-max) 1)))
421 (error (buffer-substring beg end))
424 (defun notmuch-search-set-tags (tags)
427 (re-search-backward "(")
430 (inhibit-read-only t))
431 (re-search-forward ")")
434 (delete-region beg end)
435 (insert (mapconcat 'identity tags " "))))))
437 (defun notmuch-search-get-tags ()
440 (re-search-backward "(")
441 (let ((beg (+ (point) 1)))
442 (re-search-forward ")")
443 (let ((end (- (point) 1)))
444 (split-string (buffer-substring beg end))))))
446 (defun notmuch-search-get-tags-region (beg end)
449 (last-line (line-number-at-pos end))
450 (max-line (- (line-number-at-pos (point-max)) 2)))
452 (while (<= (line-number-at-pos) (min last-line max-line))
453 (setq output (append output (notmuch-search-get-tags)))
457 (defun notmuch-search-add-tag-thread (tag)
458 (notmuch-search-add-tag-region tag (point) (point)))
460 (defun notmuch-search-add-tag-region (tag beg end)
461 (let ((search-id-string (mapconcat 'identity (notmuch-search-find-thread-id-region beg end) " or ")))
462 (notmuch-call-notmuch-process "tag" (concat "+" tag) search-id-string)
464 (let ((last-line (line-number-at-pos end))
465 (max-line (- (line-number-at-pos (point-max)) 2)))
467 (while (<= (line-number-at-pos) (min last-line max-line))
468 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<)))
471 (defun notmuch-search-remove-tag-thread (tag)
472 (notmuch-search-remove-tag-region tag (point) (point)))
474 (defun notmuch-search-remove-tag-region (tag beg end)
475 (let ((search-id-string (mapconcat 'identity (notmuch-search-find-thread-id-region beg end) " or ")))
476 (notmuch-call-notmuch-process "tag" (concat "-" tag) search-id-string)
478 (let ((last-line (line-number-at-pos end))
479 (max-line (- (line-number-at-pos (point-max)) 2)))
481 (while (<= (line-number-at-pos) (min last-line max-line))
482 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags)))
485 (defun notmuch-search-add-tag (tag)
486 "Add a tag to the currently selected thread or region.
488 The tag is added to all messages in the currently selected thread
489 or threads in the current region."
491 (list (notmuch-select-tag-with-completion "Tag to add: ")))
493 (if (region-active-p)
494 (let* ((beg (region-beginning))
496 (notmuch-search-add-tag-region tag beg end))
497 (notmuch-search-add-tag-thread tag))))
499 (defun notmuch-search-remove-tag (tag)
500 "Remove a tag from the currently selected thread or region.
502 The tag is removed from all messages in the currently selected
503 thread or threads in the current region."
505 (list (notmuch-select-tag-with-completion
507 (if (region-active-p)
509 (notmuch-search-find-thread-id-region (region-beginning) (region-end))
511 (notmuch-search-find-thread-id)))))
513 (if (region-active-p)
514 (let* ((beg (region-beginning))
516 (notmuch-search-remove-tag-region tag beg end))
517 (notmuch-search-remove-tag-thread tag))))
519 (defun notmuch-search-archive-thread ()
520 "Archive the currently selected thread (remove its \"inbox\" tag).
522 This function advances the next thread when finished."
524 (notmuch-search-remove-tag-thread "inbox")
527 (defun notmuch-search-process-sentinel (proc msg)
528 "Add a message to let user know when \"notmuch search\" exits"
529 (let ((buffer (process-buffer proc))
530 (status (process-status proc))
531 (exit-status (process-exit-status proc))
532 (never-found-target-thread nil))
533 (if (memq status '(exit signal))
534 (if (buffer-live-p buffer)
535 (with-current-buffer buffer
537 (let ((inhibit-read-only t)
539 (goto-char (point-max))
540 (if (eq status 'signal)
541 (insert "Incomplete search results (search process was killed).\n"))
542 (if (eq status 'exit)
544 (insert "End of search results.")
545 (if (not (= exit-status 0))
546 (insert (format " (process returned %d)" exit-status)))
549 (not (string= notmuch-search-target-thread "found")))
550 (set 'never-found-target-thread t))))))
551 (if (and never-found-target-thread
552 notmuch-search-target-line)
553 (goto-line notmuch-search-target-line)))))))
555 (defcustom notmuch-search-line-faces nil
556 "Tag/face mapping for line highlighting in notmuch-search.
558 Here is an example of how to color search results based on tags.
559 (the following text would be placed in your ~/.emacs file):
561 (setq notmuch-search-line-faces '((\"delete\" . '(:foreground \"red\"))
562 (\"unread\" . '(:foreground \"green\"))))
564 Order matters: for lines with multiple tags, the the first
565 matching will be applied."
566 :type '(alist :key-type (string) :value-type (list))
569 (defun notmuch-search-color-line (start end line-tag-list)
570 "Colorize lines in notmuch-show based on tags"
571 (if notmuch-search-line-faces
572 (let ((overlay (make-overlay start end))
573 (tags-faces (copy-alist notmuch-search-line-faces)))
575 (let* ((tag-face (car tags-faces))
577 (face (cdr tag-face)))
578 (cond ((member tag line-tag-list)
579 (overlay-put overlay 'face face)
580 (setq tags-faces nil))
582 (setq tags-faces (cdr tags-faces)))))))))
584 (defun notmuch-search-process-filter (proc string)
585 "Process and filter the output of \"notmuch search\""
586 (let ((buffer (process-buffer proc))
588 (if (buffer-live-p buffer)
589 (with-current-buffer buffer
593 (inhibit-read-only t))
595 (if (string-match "^\\(thread:[0-9A-Fa-f]*\\) \\(.*\\) \\(\\[[0-9/]*\\]\\) \\([^;]*\\); \\(.*\\) (\\([^()]*\\))$" string line)
596 (let* ((thread-id (match-string 1 string))
597 (date (match-string 2 string))
598 (count (match-string 3 string))
599 (authors (match-string 4 string))
600 (authors-length (length authors))
601 (subject (match-string 5 string))
602 (tags (match-string 6 string))
603 (tag-list (if tags (save-match-data (split-string tags)))))
604 (if (> authors-length notmuch-search-authors-width)
605 (set 'authors (concat (substring authors 0 (- notmuch-search-authors-width 3)) "...")))
606 (goto-char (point-max))
607 (let ((beg (point-marker))
608 (format-string (format "%%s %%-7s %%-%ds %%s (%%s)\n" notmuch-search-authors-width)))
609 (insert (format format-string date count authors subject tags))
610 (notmuch-search-color-line beg (point-marker) tag-list)
611 (put-text-property beg (point-marker) 'notmuch-search-thread-id thread-id)
612 (put-text-property beg (point-marker) 'notmuch-search-authors authors)
613 (put-text-property beg (point-marker) 'notmuch-search-subject subject)
614 (if (string= thread-id notmuch-search-target-thread)
616 (set 'found-target beg)
617 (set 'notmuch-search-target-thread "found"))))
618 (set 'line (match-end 0)))
621 (goto-char found-target)))
622 (delete-process proc))))
624 (defun notmuch-search-operate-all (action)
625 "Add/remove tags from all matching messages.
627 Tis command adds or removes tags from all messages matching the
628 current search terms. When called interactively, this command
629 will prompt for tags to be added or removed. Tags prefixed with
630 '+' will be added and tags prefixed with '-' will be removed.
632 Each character of the tag name may consist of alphanumeric
633 characters as well as `_.+-'.
635 (interactive "sOperation (+add -drop): notmuch tag ")
636 (let ((action-split (split-string action " +")))
637 ;; Perform some validation
638 (let ((words action-split))
639 (when (null words) (error "No operation given"))
641 (unless (string-match-p "^[-+][-+_.[:word:]]+$" (car words))
642 (error "Action must be of the form `+thistag -that_tag'"))
643 (setq words (cdr words))))
644 (apply 'notmuch-call-notmuch-process "tag"
645 (append action-split (list notmuch-search-query-string) nil))))
648 (defun notmuch-search (query &optional oldest-first target-thread target-line)
649 "Run \"notmuch search\" with the given query string and display results.
651 The optional parameters are used as follows:
653 oldest-first: A Boolean controlling the sort order of returned threads
654 target-thread: A thread ID (with the thread: prefix) that will be made
655 current if it appears in the search results.
656 target-line: The line number to move to if the target thread does not
657 appear in the search results."
658 (interactive "sNotmuch search: ")
659 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
660 (switch-to-buffer buffer)
661 (notmuch-search-mode)
662 (set 'notmuch-search-query-string query)
663 (set 'notmuch-search-oldest-first oldest-first)
664 (set 'notmuch-search-target-thread target-thread)
665 (set 'notmuch-search-target-line target-line)
666 (let ((proc (get-buffer-process (current-buffer)))
667 (inhibit-read-only t))
669 (error "notmuch search process already running for query `%s'" query)
672 (goto-char (point-min))
674 (let ((proc (start-process-shell-command
675 "notmuch-search" buffer notmuch-command "search"
676 (if oldest-first "--sort=oldest-first" "--sort=newest-first")
677 (shell-quote-argument query))))
678 (set-process-sentinel proc 'notmuch-search-process-sentinel)
679 (set-process-filter proc 'notmuch-search-process-filter))))
680 (run-hooks 'notmuch-search-hook)))
682 (defun notmuch-search-refresh-view ()
683 "Refresh the current view.
685 Kills the current buffer and runs a new search with the same
686 query string as the current search. If the current thread is in
687 the new search results, then point will be placed on the same
688 thread. Otherwise, point will be moved to attempt to be in the
689 same relative position within the new buffer."
691 (let ((target-line (line-number-at-pos))
692 (oldest-first notmuch-search-oldest-first)
693 (target-thread (notmuch-search-find-thread-id))
694 (query notmuch-search-query-string))
696 (notmuch-search query oldest-first target-thread target-line)
697 (goto-char (point-min))
700 (defun notmuch-search-toggle-order ()
701 "Toggle the current search order.
703 By default, the \"inbox\" view created by `notmuch' is displayed
704 in chronological order (oldest thread at the beginning of the
705 buffer), while any global searches created by `notmuch-search'
706 are displayed in reverse-chronological order (newest thread at
707 the beginning of the buffer).
709 This command toggles the sort order for the current search.
711 Note that any filtered searches created by
712 `notmuch-search-filter' retain the search order of the parent
715 (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))
716 (notmuch-search-refresh-view))
718 (defun notmuch-search-filter (query)
719 "Filter the current search results based on an additional query string.
721 Runs a new search matching only messages that match both the
722 current search results AND the additional query string provided."
723 (interactive "sFilter search: ")
724 (let ((grouped-query (if (string-match-p notmuch-search-disjunctive-regexp query)
725 (concat "( " query " )")
727 (notmuch-search (if (string= notmuch-search-query-string "*")
729 (concat notmuch-search-query-string " and " grouped-query)) notmuch-search-oldest-first)))
731 (defun notmuch-search-filter-by-tag (tag)
732 "Filter the current search results based on a single tag.
734 Runs a new search matching only messages that match both the
735 current search results AND that are tagged with the given tag."
737 (list (notmuch-select-tag-with-completion "Filter by tag: ")))
738 (notmuch-search (concat notmuch-search-query-string " and tag:" tag) notmuch-search-oldest-first))
742 "Run notmuch to display all mail with tag of 'inbox'"
744 (notmuch-search "tag:inbox" notmuch-search-oldest-first))
746 (setq mail-user-agent 'message-user-agent)
748 (defvar notmuch-folder-mode-map
749 (let ((map (make-sparse-keymap)))
750 (define-key map "?" 'notmuch-help)
751 (define-key map "x" 'kill-this-buffer)
752 (define-key map "q" 'kill-this-buffer)
753 (define-key map "m" 'message-mail)
754 (define-key map "e" 'notmuch-folder-show-empty-toggle)
755 (define-key map ">" 'notmuch-folder-last)
756 (define-key map "<" 'notmuch-folder-first)
757 (define-key map "=" 'notmuch-folder)
758 (define-key map "s" 'notmuch-search)
759 (define-key map [mouse-1] 'notmuch-folder-show-search)
760 (define-key map (kbd "RET") 'notmuch-folder-show-search)
761 (define-key map " " 'notmuch-folder-show-search)
762 (define-key map "p" 'notmuch-folder-previous)
763 (define-key map "n" 'notmuch-folder-next)
765 "Keymap for \"notmuch folder\" buffers.")
767 (fset 'notmuch-folder-mode-map notmuch-folder-mode-map)
769 (defcustom notmuch-folders (quote (("inbox" . "tag:inbox") ("unread" . "tag:unread")))
770 "List of searches for the notmuch folder view"
771 :type '(alist :key-type (string) :value-type (string))
774 (defun notmuch-folder-mode ()
775 "Major mode for showing notmuch 'folders'.
777 This buffer contains a list of message counts returned by a
778 customizable set of searches of your email archives. Each line in
779 the buffer shows the name of a saved search and the resulting
782 Pressing RET on any line opens a search window containing the
783 results for the saved search on that line.
785 Here is an example of how the search list could be
786 customized, (the following text would be placed in your ~/.emacs
789 (setq notmuch-folders '((\"inbox\" . \"tag:inbox\")
790 (\"unread\" . \"tag:inbox AND tag:unread\")
791 (\"notmuch\" . \"tag:inbox AND to:notmuchmail.org\")))
793 Of course, you can have any number of folders, each configured
794 with any supported search terms (see \"notmuch help search-terms\").
796 Currently available key bindings:
798 \\{notmuch-folder-mode-map}"
800 (kill-all-local-variables)
801 (use-local-map 'notmuch-folder-mode-map)
802 (setq truncate-lines t)
804 (setq major-mode 'notmuch-folder-mode
805 mode-name "notmuch-folder")
806 (setq buffer-read-only t))
808 (defun notmuch-folder-next ()
809 "Select the next folder in the list."
815 (defun notmuch-folder-previous ()
816 "Select the previous folder in the list."
820 (defun notmuch-folder-first ()
821 "Select the first folder in the list."
823 (goto-char (point-min)))
825 (defun notmuch-folder-last ()
826 "Select the last folder in the list."
828 (goto-char (point-max))
831 (defun notmuch-folder-count (search)
832 (car (process-lines notmuch-command "count" search)))
834 (defvar notmuch-folder-show-empty t
835 "Whether `notmuch-folder-mode' should display empty folders.")
837 (defun notmuch-folder-show-empty-toggle ()
838 "Toggle the listing of empty folders"
840 (setq notmuch-folder-show-empty (not notmuch-folder-show-empty))
843 (defun notmuch-folder-add (folders)
845 (let* ((name (car (car folders)))
846 (inhibit-read-only t)
847 (search (cdr (car folders)))
848 (count (notmuch-folder-count search)))
849 (if (or notmuch-folder-show-empty
850 (not (equal count "0")))
858 (notmuch-folder-add (cdr folders)))))
860 (defun notmuch-folder-find-name ()
864 (re-search-forward "\\([ \t]*[^ \t]+\\)")
865 (filter-buffer-substring (match-beginning 1) (match-end 1)))))
867 (defun notmuch-folder-show-search (&optional folder)
868 "Show a search window for the search related to the specified folder."
871 (setq folder (notmuch-folder-find-name)))
872 (let ((search (assoc folder notmuch-folders)))
874 (notmuch-search (cdr search) notmuch-search-oldest-first))))
877 (defun notmuch-folder ()
878 "Show the notmuch folder view and update the displayed counts."
880 (let ((buffer (get-buffer-create "*notmuch-folders*")))
881 (switch-to-buffer buffer)
882 (let ((inhibit-read-only t)
883 (n (line-number-at-pos)))
885 (notmuch-folder-mode)
886 (notmuch-folder-add notmuch-folders)
887 (goto-char (point-min))