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 ; Much of notmuch.el was written by looking at the implementation of
23 ; compile.el from the emacs distribution source which has the
24 ; following copyright and authorsip (and the identical license as
27 ; Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
28 ; 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
29 ; Free Software Foundation, Inc.
31 ; Authors: Roland McGrath <roland@gnu.org>,
32 ; Daniel Pfeiffer <occitan@esperanto.org>
34 (defvar notmuch-show-mode-map
35 (let ((map (make-sparse-keymap)))
36 (define-key map "q" 'kill-this-buffer)
37 (define-key map "x" 'kill-this-buffer)
39 "Keymap for \"notmuch show\" buffers.")
40 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
43 (defun notmuch-show-mode ()
44 "Major mode for handling the output of \"notmuch show\""
46 (kill-all-local-variables)
47 (use-local-map notmuch-show-mode-map)
48 (setq major-mode 'notmuch-show-mode
49 mode-name "notmuch-show")
50 (setq buffer-read-only t))
52 (defun notmuch-show (thread-id)
53 "Run \"notmuch show\" with the given thread ID and display results."
54 (interactive "sNotmuch show: ")
55 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
56 (switch-to-buffer buffer)
58 (let ((proc (get-buffer-process (current-buffer)))
59 (inhibit-read-only t))
61 (error "notmuch search process already running for query `%s'" query)
64 (goto-char (point-min))
66 (call-process "notmuch" nil t nil "show" thread-id)
70 (defvar notmuch-search-mode-map
71 (let ((map (make-sparse-keymap)))
72 (define-key map "a" 'notmuch-search-archive-thread)
73 (define-key map "f" 'notmuch-search-filter)
74 (define-key map "n" 'next-line)
75 (define-key map "p" 'previous-line)
76 (define-key map "q" 'kill-this-buffer)
77 (define-key map "s" 'notmuch-search)
78 (define-key map "x" 'kill-this-buffer)
79 (define-key map "\r" 'notmuch-search-show-thread)
80 (define-key map "+" 'notmuch-search-add-tag)
81 (define-key map "-" 'notmuch-search-remove-tag)
82 (define-key map "<" 'beginning-of-buffer)
83 (define-key map ">" 'notmuch-search-goto-last-thread)
84 (define-key map "\M->" 'notmuch-search-goto-last-thread)
86 "Keymap for \"notmuch search\" buffers.")
87 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
89 (defun notmuch-search-goto-last-thread (&optional arg)
90 "Move point to the last thread in the buffer."
96 (defun notmuch-search-mode ()
97 "Major mode for handling the output of \"notmuch search\""
99 (kill-all-local-variables)
100 (make-local-variable 'notmuch-search-query-string)
101 (use-local-map notmuch-search-mode-map)
102 (setq major-mode 'notmuch-search-mode
103 mode-name "notmuch-search")
104 (setq buffer-read-only t))
106 (defun notmuch-search-find-thread-id ()
110 (re-search-forward "[a-fA-F0-9]*")
111 (filter-buffer-substring beg (point)))))
113 (defun notmuch-search-markup-this-thread-id ()
116 (re-search-forward "[a-fA-F0-9]*")
118 (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)))
120 (defun notmuch-search-markup-thread-ids ()
122 (goto-char (point-min))
124 (notmuch-search-markup-this-thread-id)
127 (defun notmuch-search-hide-thread-ids ()
129 (add-to-invisibility-spec 'notmuch-search))
131 (defun notmuch-search-show-thread-ids ()
133 (remove-from-invisibility-spec 'notmuch-search))
135 (defun notmuch-search-show-thread ()
137 (notmuch-show (notmuch-search-find-thread-id)))
139 (defun notmuch-search-call-notmuch-process (&rest args)
140 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
141 (with-current-buffer error-buffer
143 (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
146 (with-current-buffer error-buffer
147 (let ((beg (point-min))
148 (end (- (point-max) 1)))
149 (error (buffer-substring beg end))
152 (defun notmuch-search-set-tags (tags)
155 (re-search-backward "(")
158 (inhibit-read-only t))
159 (re-search-forward ")")
162 (delete-region beg end)
163 (insert (mapconcat 'identity tags " "))))))
165 (defun notmuch-search-get-tags ()
168 (re-search-backward "(")
169 (let ((beg (+ (point) 1)))
170 (re-search-forward ")")
171 (let ((end (- (point) 1)))
172 (split-string (buffer-substring beg end))))))
174 (defun notmuch-search-add-tag (tag)
175 (interactive "sTag to add: ")
176 (notmuch-search-call-notmuch-process "tag" (concat "+" tag) (concat "thread:" (notmuch-search-find-thread-id)))
177 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
179 (defun notmuch-search-remove-tag (tag)
180 (interactive "sTag to remove: ")
181 (notmuch-search-call-notmuch-process "tag" (concat "-" tag) (concat "thread:" (notmuch-search-find-thread-id)))
182 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
184 (defun notmuch-search-archive-thread ()
186 (notmuch-search-remove-tag "inbox"))
188 (defun notmuch-search (query)
189 "Run \"notmuch search\" with the given query string and display results."
190 (interactive "sNotmuch search: ")
191 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
192 (switch-to-buffer buffer)
193 (notmuch-search-mode)
194 (set 'notmuch-search-query-string query)
195 (let ((proc (get-buffer-process (current-buffer)))
196 (inhibit-read-only t))
198 (error "notmuch search process already running for query `%s'" query)
201 (goto-char (point-min))
203 (call-process "notmuch" nil t nil "search" query)
204 (notmuch-search-markup-thread-ids)
205 ; A well-behaved program ends its output with a newline, but we
206 ; don't actually want the blank line at the end of the file.
207 (goto-char (point-max))
208 (if (looking-at "^$")
209 (delete-backward-char 1)
213 (defun notmuch-search-filter (query)
214 "Run \"notmuch search\" to refine the current search results.
216 A search string will be constructed by appending QUERY to the
217 current search string, and the results of \"notmuch search\" for
218 the combined query will be displayed."
219 (interactive "sFilter search: ")
220 (notmuch-search (concat notmuch-search-query-string " and " query)))
223 "Run notmuch to display all mail with tag of 'inbox'"
225 (notmuch-search "tag:inbox"))