]> git.notmuchmail.org Git - notmuch/blob - notmuch.el
564f47916a5cd77428949816c352d39d6c534490
[notmuch] / notmuch.el
1 ; notmuch.el --- run notmuch within emacs
2 ;
3 ; Copyright © Carl Worth
4 ;
5 ; This file is part of Notmuch.
6 ;
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.
11 ;
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.
16 ;
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/>.
19 ;
20 ; Authors: Carl Worth <cworth@cworth.org>
21 ;
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
25 ; above):
26 ;
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.
30
31 ; Authors: Roland McGrath <roland@gnu.org>,
32 ;           Daniel Pfeiffer <occitan@esperanto.org>
33
34 (defvar notmuch-show-mode-map
35   (let ((map (make-sparse-keymap)))
36     (define-key map "b" 'notmuch-show-toggle-body-read-visible)
37     (define-key map "h" 'notmuch-show-toggle-headers-visible)
38     (define-key map "n" 'notmuch-show-next-message)
39     (define-key map "p" 'notmuch-show-previous-message)
40     (define-key map "q" 'kill-this-buffer)
41     (define-key map "x" 'kill-this-buffer)
42     map)
43   "Keymap for \"notmuch show\" buffers.")
44 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
45
46 (defvar notmuch-show-message-begin-regexp    "\fmessage{")
47 (defvar notmuch-show-message-end-regexp      "\fmessage}")
48 (defvar notmuch-show-header-begin-regexp     "\fheader{")
49 (defvar notmuch-show-header-end-regexp       "\fheader}")
50 (defvar notmuch-show-body-begin-regexp       "\fbody{")
51 (defvar notmuch-show-body-end-regexp         "\fbody}")
52 (defvar notmuch-show-attachment-begin-regexp "\fattachment{")
53 (defvar notmuch-show-attachment-end-regexp   "\fattachment}")
54 (defvar notmuch-show-part-begin-regexp       "\fpart{")
55 (defvar notmuch-show-part-end-regexp         "\fpart}")
56 (defvar notmuch-show-marker-regexp "\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
57 (defvar notmuch-show-headers-visible t)
58
59 (defun notmuch-show-next-message ()
60   "Advance point to the beginning of the next message in the buffer."
61   (interactive)
62   ; First, ensure we get off the current message marker
63   (if (not (eobp))
64       (forward-char))
65   (re-search-forward notmuch-show-message-begin-regexp nil t)
66   (beginning-of-line)
67   (recenter 0))
68
69 (defun notmuch-show-previous-message ()
70   "Advance point to the beginning of the previous message in the buffer."
71   (interactive)
72   ; First, ensure we get off the current message marker
73   (if (not (bobp))
74       (previous-line))
75   (re-search-backward notmuch-show-message-begin-regexp nil t)
76   (beginning-of-line)
77   (recenter 0))
78
79 (defun notmuch-show-markup-body (unread)
80   (re-search-forward notmuch-show-body-begin-regexp)
81   (next-line 1)
82   (beginning-of-line)
83   (let ((beg (point)))
84     (re-search-forward notmuch-show-body-end-regexp)
85     (if (not unread)
86         (overlay-put (make-overlay beg (match-beginning 0))
87                      'invisible 'notmuch-show-body-read))
88 ;      (notmuch-show-markup-citations-region beg point)
89     ))
90
91 (defun notmuch-show-markup-header ()
92   (re-search-forward notmuch-show-header-begin-regexp)
93   (next-line 2)
94   (beginning-of-line)
95   (let ((beg (point)))
96     (re-search-forward notmuch-show-header-end-regexp)
97     (overlay-put (make-overlay beg (match-beginning 0))
98                  'invisible 'notmuch-show-header)))
99
100 (defun notmuch-show-markup-message ()
101   (if (re-search-forward notmuch-show-message-begin-regexp nil t)
102       (progn
103         (let ((unread (looking-at ".*unread$")))
104           (notmuch-show-markup-header)
105           (notmuch-show-markup-body unread)))
106     (goto-char (point-max))))
107
108 (defun notmuch-show-hide-markers ()
109   (save-excursion
110     (goto-char (point-min))
111     (while (not (eobp))
112       (if (re-search-forward notmuch-show-marker-regexp nil t)
113           (progn
114             (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
115                          'invisible 'notmuch-show-marker))
116         (goto-char (point-max))))))
117
118 (defun notmuch-show-markup-messages ()
119   (save-excursion
120     (goto-char (point-min))
121     (while (not (eobp))
122       (notmuch-show-markup-message)))
123   (notmuch-show-hide-markers))
124
125 (defun notmuch-show-toggle-headers-visible ()
126   "Toggle visibility of header fields"
127   (interactive)
128   (if notmuch-show-headers-visible
129       (add-to-invisibility-spec 'notmuch-show-header)
130     (remove-from-invisibility-spec 'notmuch-show-header))
131   (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
132   ; Need to force the redisplay for some reason
133   (force-window-update)
134   (redisplay t))
135
136 (defun notmuch-show-toggle-body-read-visible ()
137   "Toggle visibility of message bodies of read messages"
138   (interactive)
139   (if notmuch-show-body-read-visible
140       (add-to-invisibility-spec 'notmuch-show-body-read)
141     (remove-from-invisibility-spec 'notmuch-show-body-read))
142   (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
143   ; Need to force the redisplay for some reason
144   (force-window-update)
145   (redisplay t))
146
147 ;;;###autoload
148 (defun notmuch-show-mode ()
149   "Major mode for handling the output of \"notmuch show\""
150   (interactive)
151   (kill-all-local-variables)
152   (set (make-local-variable 'notmuch-show-headers-visible) t)
153   (notmuch-show-toggle-headers-visible)
154   (set (make-local-variable 'notmuch-show-body-read-visible) t)
155   (notmuch-show-toggle-body-read-visible)
156   (add-to-invisibility-spec 'notmuch-show-marker)
157   (use-local-map notmuch-show-mode-map)
158   (setq major-mode 'notmuch-show-mode
159         mode-name "notmuch-show")
160   (setq buffer-read-only t))
161
162 (defun notmuch-show (thread-id)
163   "Run \"notmuch show\" with the given thread ID and display results."
164   (interactive "sNotmuch show: ")
165   (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
166     (switch-to-buffer buffer)
167     (notmuch-show-mode)
168     (let ((proc (get-buffer-process (current-buffer)))
169           (inhibit-read-only t))
170       (if proc
171           (error "notmuch search process already running for query `%s'" query)
172         )
173       (erase-buffer)
174       (goto-char (point-min))
175       (save-excursion
176         (call-process "notmuch" nil t nil "show" thread-id)
177         (notmuch-show-markup-messages)
178         )
179       )))
180
181 (defvar notmuch-search-mode-map
182   (let ((map (make-sparse-keymap)))
183     (define-key map "a" 'notmuch-search-archive-thread)
184     (define-key map "f" 'notmuch-search-filter)
185     (define-key map "n" 'next-line)
186     (define-key map "p" 'previous-line)
187     (define-key map "q" 'kill-this-buffer)
188     (define-key map "s" 'notmuch-search)
189     (define-key map "x" 'kill-this-buffer)
190     (define-key map "\r" 'notmuch-search-show-thread)
191     (define-key map "+" 'notmuch-search-add-tag)
192     (define-key map "-" 'notmuch-search-remove-tag)
193     (define-key map "<" 'beginning-of-buffer)
194     (define-key map ">" 'notmuch-search-goto-last-thread)
195     (define-key map "\M->" 'notmuch-search-goto-last-thread)
196     map)
197   "Keymap for \"notmuch search\" buffers.")
198 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
199
200 (defun notmuch-search-goto-last-thread (&optional arg)
201   "Move point to the last thread in the buffer."
202   (interactive "^P")
203   (end-of-buffer arg)
204   (beginning-of-line))
205
206 ;;;###autoload
207 (defun notmuch-search-mode ()
208   "Major mode for handling the output of \"notmuch search\""
209   (interactive)
210   (kill-all-local-variables)
211   (make-local-variable 'notmuch-search-query-string)
212   (use-local-map notmuch-search-mode-map)
213   (setq major-mode 'notmuch-search-mode
214         mode-name "notmuch-search")
215   (setq buffer-read-only t))
216
217 (defun notmuch-search-find-thread-id ()
218   (save-excursion
219     (beginning-of-line)
220     (let ((beg (point)))
221       (re-search-forward "[a-fA-F0-9]*")
222       (filter-buffer-substring beg (point)))))
223
224 (defun notmuch-search-markup-this-thread-id ()
225   (beginning-of-line)
226   (let ((beg (point)))
227     (re-search-forward "[a-fA-F0-9]*")
228     (forward-char)
229     (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)))
230
231 (defun notmuch-search-markup-thread-ids ()
232   (save-excursion
233     (goto-char (point-min))
234     (while (not (eobp))
235       (notmuch-search-markup-this-thread-id)
236       (next-line))))
237
238 (defun notmuch-search-hide-thread-ids ()
239   (interactive)
240   (add-to-invisibility-spec 'notmuch-search))
241
242 (defun notmuch-search-show-thread-ids ()
243   (interactive)
244   (remove-from-invisibility-spec 'notmuch-search))
245
246 (defun notmuch-search-show-thread ()
247   (interactive)
248   (notmuch-show (notmuch-search-find-thread-id)))
249
250 (defun notmuch-search-call-notmuch-process (&rest args)
251   (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
252     (with-current-buffer error-buffer
253         (erase-buffer))
254     (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
255         (point)
256       (progn
257         (with-current-buffer error-buffer
258           (let ((beg (point-min))
259                 (end (- (point-max) 1)))
260             (error (buffer-substring beg end))
261             ))))))
262
263 (defun notmuch-search-set-tags (tags)
264   (save-excursion
265     (end-of-line)
266     (re-search-backward "(")
267     (forward-char)
268     (let ((beg (point))
269           (inhibit-read-only t))
270       (re-search-forward ")")
271       (backward-char)
272       (let ((end (point)))
273         (delete-region beg end)
274         (insert (mapconcat  'identity tags " "))))))
275
276 (defun notmuch-search-get-tags ()
277   (save-excursion
278     (end-of-line)
279     (re-search-backward "(")
280     (let ((beg (+ (point) 1)))
281       (re-search-forward ")")
282       (let ((end (- (point) 1)))
283         (split-string (buffer-substring beg end))))))
284
285 (defun notmuch-search-add-tag (tag)
286   (interactive "sTag to add: ")
287   (notmuch-search-call-notmuch-process "tag" (concat "+" tag) (concat "thread:" (notmuch-search-find-thread-id)))
288   (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
289
290 (defun notmuch-search-remove-tag (tag)
291   (interactive "sTag to remove: ")
292   (notmuch-search-call-notmuch-process "tag" (concat "-" tag) (concat "thread:" (notmuch-search-find-thread-id)))
293   (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
294
295 (defun notmuch-search-archive-thread ()
296   (interactive)
297   (notmuch-search-remove-tag "inbox"))
298
299 (defun notmuch-search (query)
300   "Run \"notmuch search\" with the given query string and display results."
301   (interactive "sNotmuch search: ")
302   (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
303     (switch-to-buffer buffer)
304     (notmuch-search-mode)
305     (set 'notmuch-search-query-string query)
306     (let ((proc (get-buffer-process (current-buffer)))
307           (inhibit-read-only t))
308       (if proc
309           (error "notmuch search process already running for query `%s'" query)
310         )
311       (erase-buffer)
312       (goto-char (point-min))
313       (save-excursion
314         (call-process "notmuch" nil t nil "search" query)
315         (notmuch-search-markup-thread-ids)
316         ; A well-behaved program ends its output with a newline, but we
317         ; don't actually want the blank line at the end of the file.
318         (goto-char (point-max))
319         (if (looking-at "^$")
320             (delete-backward-char 1)
321           )
322         ))))
323
324 (defun notmuch-search-filter (query)
325   "Run \"notmuch search\" to refine the current search results.
326
327 A search string will be constructed by appending QUERY to the
328 current search string, and the results of \"notmuch search\" for
329 the combined query will be displayed."
330   (interactive "sFilter search: ")
331   (notmuch-search (concat notmuch-search-query-string " and " query)))
332
333 (defun notmuch ()
334   "Run notmuch to display all mail with tag of 'inbox'"
335   (interactive)
336   (notmuch-search "tag:inbox"))
337
338 (provide 'notmuch)