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 (defvar notmuch-show-id-regexp "ID: \\([^ ]*\\)")
69 (defun notmuch-show-get-message-id ()
72 (if (not (looking-at notmuch-show-message-begin-regexp))
73 (re-search-backward notmuch-show-message-begin-regexp))
74 (re-search-forward notmuch-show-id-regexp)
75 (buffer-substring (match-beginning 1) (match-end 1))))
77 (defun notmuch-show-next-message ()
78 "Advance point to the beginning of the next message in the buffer.
80 Before moving, also remove the \"unread\" tag from the current message."
82 (notmuch-call-notmuch-process "tag" "-unread" (concat "id:" (notmuch-show-get-message-id)))
83 ; First, ensure we get off the current message marker
86 (re-search-forward notmuch-show-message-begin-regexp nil t)
87 ; This dance might look pointless, but it's important. I originally
88 ; just had (beginning-of-line) here which looked right on the
89 ; display but actually put point all the way back to the first
90 ; character of the first invisible line. That is, it put point into
91 ; the closing markers of the previous message rather than at the
92 ; beginning of the current message. And that in turn meant that
93 ; looking up the current message-ID would actually return the
94 ; previous message ID.
96 ; So this dance ensures that we're actually on the current message
97 ; when it looks like we are.
102 (defun notmuch-show-previous-message ()
103 "Advance point to the beginning of the previous message in the buffer."
105 ; First, ensure we get off the current message marker
108 (re-search-backward notmuch-show-message-begin-regexp nil t)
109 ; This dance might look pointless, but it's important. I originally
110 ; just had (beginning-of-line) here which looked right on the
111 ; display but actually put point all the way back to the first
112 ; character of the first invisible line. That is, it put point into
113 ; the closing markers of the previous message rather than at the
114 ; beginning of the current message. And that in turn meant that
115 ; looking up the current message-ID would actually return the
116 ; previous message ID.
118 ; So this dance ensures that we're actually on the current message
119 ; when it looks like we are.
120 (end-of-visible-line)
124 (defun notmuch-show-markup-citations-region (beg end)
127 (while (< (point) end)
128 (let ((beg-sub (point)))
131 (while (looking-at ">")
133 (let ((overlay (make-overlay beg-sub (point))))
134 (overlay-put overlay 'invisible 'notmuch-show-citation)
135 (overlay-put overlay 'before-string
136 (concat "[" (number-to-string (count-lines beg-sub (point)))
137 " quoted lines.]")))))
138 (if (looking-at "--[ ]?$")
139 (let ((overlay (make-overlay beg-sub end)))
140 (overlay-put overlay 'invisible 'notmuch-show-signature)
141 (overlay-put overlay 'before-string
142 (concat "[" (number-to-string (count-lines beg-sub (point)))
143 "-line signature.]"))
147 (defun notmuch-show-markup-body (unread)
148 (re-search-forward notmuch-show-body-begin-regexp)
152 (re-search-forward notmuch-show-body-end-regexp)
154 (overlay-put (make-overlay beg (match-beginning 0))
155 'invisible 'notmuch-show-body-read))
156 (notmuch-show-markup-citations-region beg (point))
159 (defun notmuch-show-markup-header ()
160 (re-search-forward notmuch-show-header-begin-regexp)
164 (re-search-forward notmuch-show-header-end-regexp)
165 (overlay-put (make-overlay beg (match-beginning 0))
166 'invisible 'notmuch-show-header)))
168 (defun notmuch-show-markup-message ()
169 (if (re-search-forward notmuch-show-message-begin-regexp nil t)
171 (let ((unread (looking-at ".*unread$")))
172 (notmuch-show-markup-header)
173 (notmuch-show-markup-body unread)))
174 (goto-char (point-max))))
176 (defun notmuch-show-hide-markers ()
178 (goto-char (point-min))
180 (if (re-search-forward notmuch-show-marker-regexp nil t)
182 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
183 'invisible 'notmuch-show-marker))
184 (goto-char (point-max))))))
186 (defun notmuch-show-markup-messages ()
188 (goto-char (point-min))
190 (notmuch-show-markup-message)))
191 (notmuch-show-hide-markers))
193 (defun notmuch-show-toggle-citations-visible ()
194 "Toggle visibility of citations"
196 (if notmuch-show-citations-visible
197 (add-to-invisibility-spec 'notmuch-show-citation)
198 (remove-from-invisibility-spec 'notmuch-show-citation))
199 (set 'notmuch-show-citations-visible (not notmuch-show-citations-visible))
200 ; Need to force the redisplay for some reason
201 (force-window-update)
204 (defun notmuch-show-toggle-signatures-visible ()
205 "Toggle visibility of signatures"
207 (if notmuch-show-signatures-visible
208 (add-to-invisibility-spec 'notmuch-show-signature)
209 (remove-from-invisibility-spec 'notmuch-show-signature))
210 (set 'notmuch-show-signatures-visible (not notmuch-show-signatures-visible))
211 ; Need to force the redisplay for some reason
212 (force-window-update)
215 (defun notmuch-show-toggle-headers-visible ()
216 "Toggle visibility of header fields"
218 (if notmuch-show-headers-visible
219 (add-to-invisibility-spec 'notmuch-show-header)
220 (remove-from-invisibility-spec 'notmuch-show-header))
221 (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
222 ; Need to force the redisplay for some reason
223 (force-window-update)
226 (defun notmuch-show-toggle-body-read-visible ()
227 "Toggle visibility of message bodies of read messages"
229 (if notmuch-show-body-read-visible
230 (add-to-invisibility-spec 'notmuch-show-body-read)
231 (remove-from-invisibility-spec 'notmuch-show-body-read))
232 (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
233 ; Need to force the redisplay for some reason
234 (force-window-update)
238 (defun notmuch-show-mode ()
239 "Major mode for handling the output of \"notmuch show\""
241 (kill-all-local-variables)
242 (set (make-local-variable 'notmuch-show-headers-visible) t)
243 (notmuch-show-toggle-headers-visible)
244 (set (make-local-variable 'notmuch-show-body-read-visible) t)
245 (notmuch-show-toggle-body-read-visible)
246 (set (make-local-variable 'notmuch-show-citations-visible) t)
247 (notmuch-show-toggle-citations-visible)
248 (set (make-local-variable 'notmuch-show-signatures-visible) t)
249 (notmuch-show-toggle-signatures-visible)
250 (add-to-invisibility-spec 'notmuch-show-marker)
251 (use-local-map notmuch-show-mode-map)
252 (setq major-mode 'notmuch-show-mode
253 mode-name "notmuch-show")
254 (setq buffer-read-only t))
256 (defun notmuch-show (thread-id)
257 "Run \"notmuch show\" with the given thread ID and display results."
258 (interactive "sNotmuch show: ")
259 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
260 (switch-to-buffer buffer)
262 (let ((proc (get-buffer-process (current-buffer)))
263 (inhibit-read-only t))
265 (error "notmuch search process already running for query `%s'" query)
268 (goto-char (point-min))
270 (call-process "notmuch" nil t nil "show" thread-id)
271 (notmuch-show-markup-messages)
275 (defvar notmuch-search-mode-map
276 (let ((map (make-sparse-keymap)))
277 (define-key map "a" 'notmuch-search-archive-thread)
278 (define-key map "f" 'notmuch-search-filter)
279 (define-key map "n" 'next-line)
280 (define-key map "p" 'previous-line)
281 (define-key map "q" 'kill-this-buffer)
282 (define-key map "s" 'notmuch-search)
283 (define-key map "x" 'kill-this-buffer)
284 (define-key map "\r" 'notmuch-search-show-thread)
285 (define-key map "+" 'notmuch-search-add-tag)
286 (define-key map "-" 'notmuch-search-remove-tag)
287 (define-key map "<" 'beginning-of-buffer)
288 (define-key map ">" 'notmuch-search-goto-last-thread)
289 (define-key map "=" 'notmuch-search-refresh-view)
290 (define-key map "\M->" 'notmuch-search-goto-last-thread)
292 "Keymap for \"notmuch search\" buffers.")
293 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
295 (defun notmuch-search-goto-last-thread (&optional arg)
296 "Move point to the last thread in the buffer."
302 (defun notmuch-search-mode ()
303 "Major mode for handling the output of \"notmuch search\""
305 (kill-all-local-variables)
306 (make-local-variable 'notmuch-search-query-string)
307 (use-local-map notmuch-search-mode-map)
308 (setq major-mode 'notmuch-search-mode
309 mode-name "notmuch-search")
310 (setq buffer-read-only t))
312 (defun notmuch-search-find-thread-id ()
316 (re-search-forward "[a-fA-F0-9]*")
317 (filter-buffer-substring beg (point)))))
319 (defun notmuch-search-markup-this-thread-id ()
322 (re-search-forward "[a-fA-F0-9]*")
324 (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)))
326 (defun notmuch-search-markup-thread-ids ()
328 (goto-char (point-min))
330 (notmuch-search-markup-this-thread-id)
333 (defun notmuch-search-hide-thread-ids ()
335 (add-to-invisibility-spec 'notmuch-search))
337 (defun notmuch-search-show-thread-ids ()
339 (remove-from-invisibility-spec 'notmuch-search))
341 (defun notmuch-search-show-thread ()
343 (notmuch-show (notmuch-search-find-thread-id)))
345 (defun notmuch-call-notmuch-process (&rest args)
346 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
347 (with-current-buffer error-buffer
349 (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
352 (with-current-buffer error-buffer
353 (let ((beg (point-min))
354 (end (- (point-max) 1)))
355 (error (buffer-substring beg end))
358 (defun notmuch-search-set-tags (tags)
361 (re-search-backward "(")
364 (inhibit-read-only t))
365 (re-search-forward ")")
368 (delete-region beg end)
369 (insert (mapconcat 'identity tags " "))))))
371 (defun notmuch-search-get-tags ()
374 (re-search-backward "(")
375 (let ((beg (+ (point) 1)))
376 (re-search-forward ")")
377 (let ((end (- (point) 1)))
378 (split-string (buffer-substring beg end))))))
380 (defun notmuch-search-add-tag (tag)
381 (interactive "sTag to add: ")
382 (notmuch-call-notmuch-process "tag" (concat "+" tag) (concat "thread:" (notmuch-search-find-thread-id)))
383 (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
385 (defun notmuch-search-remove-tag (tag)
386 (interactive "sTag to remove: ")
387 (notmuch-call-notmuch-process "tag" (concat "-" tag) (concat "thread:" (notmuch-search-find-thread-id)))
388 (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
390 (defun notmuch-search-archive-thread ()
392 (notmuch-search-remove-tag "inbox"))
394 (defun notmuch-search (query)
395 "Run \"notmuch search\" with the given query string and display results."
396 (interactive "sNotmuch search: ")
397 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
398 (switch-to-buffer buffer)
399 (notmuch-search-mode)
400 (set 'notmuch-search-query-string query)
401 (let ((proc (get-buffer-process (current-buffer)))
402 (inhibit-read-only t))
404 (error "notmuch search process already running for query `%s'" query)
407 (goto-char (point-min))
409 (call-process "notmuch" nil t nil "search" query)
410 (notmuch-search-markup-thread-ids)
411 ; A well-behaved program ends its output with a newline, but we
412 ; don't actually want the blank line at the end of the file.
413 (goto-char (point-max))
414 (if (looking-at "^$")
415 (delete-backward-char 1)
419 (defun notmuch-search-refresh-view ()
420 "Refresh the current view.
422 Kills the current buffer and runs a new search with the same
423 query string as the current search. If the current thread is in
424 the new search results, then point will be placed on the same
425 thread. Otherwise, point will be moved to attempt to be in the
426 same relative position within the new buffer."
429 (thread (notmuch-search-find-thread-id))
430 (query notmuch-search-query-string))
432 (notmuch-search query)
433 (goto-char (point-min))
434 (if (re-search-forward (concat "^" thread) nil t)
438 (defun notmuch-search-filter (query)
439 "Run \"notmuch search\" to refine the current search results.
441 A search string will be constructed by appending QUERY to the
442 current search string, and the results of \"notmuch search\" for
443 the combined query will be displayed."
444 (interactive "sFilter search: ")
445 (notmuch-search (concat notmuch-search-query-string " and " query)))
448 "Run notmuch to display all mail with tag of 'inbox'"
450 (notmuch-search "tag:inbox"))