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 ; I don't actually want all of these toggle commands occupying
37 ; keybindings. They steal valuable key-binding space, are hard
38 ; to remember, and act globally rather than locally.
40 ; Will be much preferable to switch to direct manipulation for
41 ; toggling visibility of these components. Probably using
42 ; overlays-at to query and manipulate the current overlay.
43 (define-key map "b" 'notmuch-show-toggle-body-read-visible)
44 (define-key map "c" 'notmuch-show-toggle-citations-visible)
45 (define-key map "h" 'notmuch-show-toggle-headers-visible)
46 (define-key map "n" 'notmuch-show-next-message)
47 (define-key map "p" 'notmuch-show-previous-message)
48 (define-key map "q" 'kill-this-buffer)
49 (define-key map "s" 'notmuch-show-toggle-signatures-visible)
50 (define-key map "x" 'kill-this-buffer)
52 "Keymap for \"notmuch show\" buffers.")
53 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
55 (defvar notmuch-show-message-begin-regexp "
\fmessage{")
56 (defvar notmuch-show-message-end-regexp "
\fmessage}")
57 (defvar notmuch-show-header-begin-regexp "
\fheader{")
58 (defvar notmuch-show-header-end-regexp "
\fheader}")
59 (defvar notmuch-show-body-begin-regexp "
\fbody{")
60 (defvar notmuch-show-body-end-regexp "
\fbody}")
61 (defvar notmuch-show-attachment-begin-regexp "
\fattachment{")
62 (defvar notmuch-show-attachment-end-regexp "
\fattachment}")
63 (defvar notmuch-show-part-begin-regexp "
\fpart{")
64 (defvar notmuch-show-part-end-regexp "
\fpart}")
65 (defvar notmuch-show-marker-regexp "
\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
67 (defun notmuch-show-next-message ()
68 "Advance point to the beginning of the next message in the buffer."
70 ; First, ensure we get off the current message marker
73 (re-search-forward notmuch-show-message-begin-regexp nil t)
77 (defun notmuch-show-previous-message ()
78 "Advance point to the beginning of the previous message in the buffer."
80 ; First, ensure we get off the current message marker
83 (re-search-backward notmuch-show-message-begin-regexp nil t)
87 (defun notmuch-show-markup-citations-region (beg end)
90 (while (< (point) end)
91 (let ((beg-sub (point)))
94 (while (looking-at ">")
96 (let ((overlay (make-overlay beg-sub (point))))
97 (overlay-put overlay 'invisible 'notmuch-show-citation)
98 (overlay-put overlay 'before-string
99 (concat "[" (number-to-string (count-lines beg-sub (point)))
100 " quoted lines.]")))))
101 (if (looking-at "--[ ]?$")
102 (let ((overlay (make-overlay beg-sub end)))
103 (overlay-put overlay 'invisible 'notmuch-show-signature)
104 (overlay-put overlay 'before-string
105 (concat "[" (number-to-string (count-lines beg-sub (point)))
106 "-line signature.]"))
110 (defun notmuch-show-markup-body (unread)
111 (re-search-forward notmuch-show-body-begin-regexp)
115 (re-search-forward notmuch-show-body-end-regexp)
117 (overlay-put (make-overlay beg (match-beginning 0))
118 'invisible 'notmuch-show-body-read))
119 (notmuch-show-markup-citations-region beg (point))
122 (defun notmuch-show-markup-header ()
123 (re-search-forward notmuch-show-header-begin-regexp)
127 (re-search-forward notmuch-show-header-end-regexp)
128 (overlay-put (make-overlay beg (match-beginning 0))
129 'invisible 'notmuch-show-header)))
131 (defun notmuch-show-markup-message ()
132 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
134 (let ((unread (looking-at ".*unread$")))
135 (notmuch-show-markup-header)
136 (notmuch-show-markup-body unread)))
137 (goto-char (point-max))))
139 (defun notmuch-show-hide-markers ()
141 (goto-char (point-min))
143 (if (re-search-forward notmuch-show-marker-regexp nil t)
145 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
146 'invisible 'notmuch-show-marker))
147 (goto-char (point-max))))))
149 (defun notmuch-show-markup-messages ()
151 (goto-char (point-min))
153 (notmuch-show-markup-message)))
154 (notmuch-show-hide-markers))
156 (defun notmuch-show-toggle-citations-visible ()
157 "Toggle visibility of citations"
159 (if notmuch-show-citations-visible
160 (add-to-invisibility-spec 'notmuch-show-citation)
161 (remove-from-invisibility-spec 'notmuch-show-citation))
162 (set 'notmuch-show-citations-visible (not notmuch-show-citations-visible))
163 ; Need to force the redisplay for some reason
164 (force-window-update)
167 (defun notmuch-show-toggle-signatures-visible ()
168 "Toggle visibility of signatures"
170 (if notmuch-show-signatures-visible
171 (add-to-invisibility-spec 'notmuch-show-signature)
172 (remove-from-invisibility-spec 'notmuch-show-signature))
173 (set 'notmuch-show-signatures-visible (not notmuch-show-signatures-visible))
174 ; Need to force the redisplay for some reason
175 (force-window-update)
178 (defun notmuch-show-toggle-headers-visible ()
179 "Toggle visibility of header fields"
181 (if notmuch-show-headers-visible
182 (add-to-invisibility-spec 'notmuch-show-header)
183 (remove-from-invisibility-spec 'notmuch-show-header))
184 (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
185 ; Need to force the redisplay for some reason
186 (force-window-update)
189 (defun notmuch-show-toggle-body-read-visible ()
190 "Toggle visibility of message bodies of read messages"
192 (if notmuch-show-body-read-visible
193 (add-to-invisibility-spec 'notmuch-show-body-read)
194 (remove-from-invisibility-spec 'notmuch-show-body-read))
195 (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
196 ; Need to force the redisplay for some reason
197 (force-window-update)
201 (defun notmuch-show-mode ()
202 "Major mode for handling the output of \"notmuch show\""
204 (kill-all-local-variables)
205 (set (make-local-variable 'notmuch-show-headers-visible) t)
206 (notmuch-show-toggle-headers-visible)
207 (set (make-local-variable 'notmuch-show-body-read-visible) t)
208 (notmuch-show-toggle-body-read-visible)
209 (set (make-local-variable 'notmuch-show-citations-visible) t)
210 (notmuch-show-toggle-citations-visible)
211 (set (make-local-variable 'notmuch-show-signatures-visible) t)
212 (notmuch-show-toggle-signatures-visible)
213 (add-to-invisibility-spec 'notmuch-show-marker)
214 (use-local-map notmuch-show-mode-map)
215 (setq major-mode 'notmuch-show-mode
216 mode-name "notmuch-show")
217 (setq buffer-read-only t))
219 (defun notmuch-show (thread-id)
220 "Run \"notmuch show\" with the given thread ID and display results."
221 (interactive "sNotmuch show: ")
222 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
223 (switch-to-buffer buffer)
225 (let ((proc (get-buffer-process (current-buffer)))
226 (inhibit-read-only t))
228 (error "notmuch search process already running for query `%s'" query)
231 (goto-char (point-min))
233 (call-process "notmuch" nil t nil "show" thread-id)
234 (notmuch-show-markup-messages)
238 (defvar notmuch-search-mode-map
239 (let ((map (make-sparse-keymap)))
240 (define-key map "a" 'notmuch-search-archive-thread)
241 (define-key map "f" 'notmuch-search-filter)
242 (define-key map "n" 'next-line)
243 (define-key map "p" 'previous-line)
244 (define-key map "q" 'kill-this-buffer)
245 (define-key map "s" 'notmuch-search)
246 (define-key map "x" 'kill-this-buffer)
247 (define-key map "\r" 'notmuch-search-show-thread)
248 (define-key map "+" 'notmuch-search-add-tag)
249 (define-key map "-" 'notmuch-search-remove-tag)
250 (define-key map "<" 'beginning-of-buffer)
251 (define-key map ">" 'notmuch-search-goto-last-thread)
252 (define-key map "\M->" 'notmuch-search-goto-last-thread)
254 "Keymap for \"notmuch search\" buffers.")
255 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
257 (defun notmuch-search-goto-last-thread (&optional arg)
258 "Move point to the last thread in the buffer."
264 (defun notmuch-search-mode ()
265 "Major mode for handling the output of \"notmuch search\""
267 (kill-all-local-variables)
268 (make-local-variable 'notmuch-search-query-string)
269 (use-local-map notmuch-search-mode-map)
270 (setq major-mode 'notmuch-search-mode
271 mode-name "notmuch-search")
272 (setq buffer-read-only t))
274 (defun notmuch-search-find-thread-id ()
278 (re-search-forward "[a-fA-F0-9]*")
279 (filter-buffer-substring beg (point)))))
281 (defun notmuch-search-markup-this-thread-id ()
284 (re-search-forward "[a-fA-F0-9]*")
286 (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)))
288 (defun notmuch-search-markup-thread-ids ()
290 (goto-char (point-min))
292 (notmuch-search-markup-this-thread-id)
295 (defun notmuch-search-hide-thread-ids ()
297 (add-to-invisibility-spec 'notmuch-search))
299 (defun notmuch-search-show-thread-ids ()
301 (remove-from-invisibility-spec 'notmuch-search))
303 (defun notmuch-search-show-thread ()
305 (notmuch-show (notmuch-search-find-thread-id)))
307 (defun notmuch-search-call-notmuch-process (&rest args)
308 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
309 (with-current-buffer error-buffer
311 (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
314 (with-current-buffer error-buffer
315 (let ((beg (point-min))
316 (end (- (point-max) 1)))
317 (error (buffer-substring beg end))
320 (defun notmuch-search-set-tags (tags)
323 (re-search-backward "(")
326 (inhibit-read-only t))
327 (re-search-forward ")")
330 (delete-region beg end)
331 (insert (mapconcat 'identity tags " "))))))
333 (defun notmuch-search-get-tags ()
336 (re-search-backward "(")
337 (let ((beg (+ (point) 1)))
338 (re-search-forward ")")
339 (let ((end (- (point) 1)))
340 (split-string (buffer-substring beg end))))))
342 (defun notmuch-search-add-tag (tag)
343 (interactive "sTag to add: ")
344 (notmuch-search-call-notmuch-process "tag" (concat "+" tag) (concat "thread:" (notmuch-search-find-thread-id)))
345 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
347 (defun notmuch-search-remove-tag (tag)
348 (interactive "sTag to remove: ")
349 (notmuch-search-call-notmuch-process "tag" (concat "-" tag) (concat "thread:" (notmuch-search-find-thread-id)))
350 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
352 (defun notmuch-search-archive-thread ()
354 (notmuch-search-remove-tag "inbox"))
356 (defun notmuch-search (query)
357 "Run \"notmuch search\" with the given query string and display results."
358 (interactive "sNotmuch search: ")
359 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
360 (switch-to-buffer buffer)
361 (notmuch-search-mode)
362 (set 'notmuch-search-query-string query)
363 (let ((proc (get-buffer-process (current-buffer)))
364 (inhibit-read-only t))
366 (error "notmuch search process already running for query `%s'" query)
369 (goto-char (point-min))
371 (call-process "notmuch" nil t nil "search" query)
372 (notmuch-search-markup-thread-ids)
373 ; A well-behaved program ends its output with a newline, but we
374 ; don't actually want the blank line at the end of the file.
375 (goto-char (point-max))
376 (if (looking-at "^$")
377 (delete-backward-char 1)
381 (defun notmuch-search-filter (query)
382 "Run \"notmuch search\" to refine the current search results.
384 A search string will be constructed by appending QUERY to the
385 current search string, and the results of \"notmuch search\" for
386 the combined query will be displayed."
387 (interactive "sFilter search: ")
388 (notmuch-search (concat notmuch-search-query-string " and " query)))
391 "Run notmuch to display all mail with tag of 'inbox'"
393 (notmuch-search "tag:inbox"))