]> git.notmuchmail.org Git - notmuch/blob - notmuch.el
notmuch.el: Add (n)ext and (p)revious bindings to notmuch-show mode.
[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 "n" 'notmuch-show-next-message)
37     (define-key map "p" 'notmuch-show-previous-message)
38     (define-key map "q" 'kill-this-buffer)
39     (define-key map "x" 'kill-this-buffer)
40     map)
41   "Keymap for \"notmuch show\" buffers.")
42 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
43
44 (defvar notmuch-show-message-begin-regexp "%message{")
45
46 (defun notmuch-show-next-message ()
47   "Advance point to the beginning of the next message in the buffer."
48   (interactive)
49   ; First, ensure we get off the current message marker
50   (if (not (eobp))
51       (forward-char))
52   (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
53       (goto-char (point-max)))
54   (beginning-of-line))
55
56 (defun notmuch-show-previous-message ()
57   "Advance point to the beginning of the previous message in the buffer."
58   (interactive)
59   ; First, ensure we get off the current message marker
60   (if (not (eobp))
61       (forward-char))
62   (if (not (re-search-backward notmuch-show-message-begin-regexp nil t))
63       (progn
64         (goto-char (point-min))
65         (beginning-of-line))))
66
67 ;;;###autoload
68 (defun notmuch-show-mode ()
69   "Major mode for handling the output of \"notmuch show\""
70   (interactive)
71   (kill-all-local-variables)
72   (use-local-map notmuch-show-mode-map)
73   (setq major-mode 'notmuch-show-mode
74         mode-name "notmuch-show")
75   (setq buffer-read-only t))
76
77 (defun notmuch-show (thread-id)
78   "Run \"notmuch show\" with the given thread ID and display results."
79   (interactive "sNotmuch show: ")
80   (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
81     (switch-to-buffer buffer)
82     (notmuch-show-mode)
83     (let ((proc (get-buffer-process (current-buffer)))
84           (inhibit-read-only t))
85       (if proc
86           (error "notmuch search process already running for query `%s'" query)
87         )
88       (erase-buffer)
89       (goto-char (point-min))
90       (save-excursion
91         (call-process "notmuch" nil t nil "show" thread-id)
92         )
93       )))
94
95 (defvar notmuch-search-mode-map
96   (let ((map (make-sparse-keymap)))
97     (define-key map "a" 'notmuch-search-archive-thread)
98     (define-key map "f" 'notmuch-search-filter)
99     (define-key map "n" 'next-line)
100     (define-key map "p" 'previous-line)
101     (define-key map "q" 'kill-this-buffer)
102     (define-key map "s" 'notmuch-search)
103     (define-key map "x" 'kill-this-buffer)
104     (define-key map "\r" 'notmuch-search-show-thread)
105     (define-key map "+" 'notmuch-search-add-tag)
106     (define-key map "-" 'notmuch-search-remove-tag)
107     (define-key map "<" 'beginning-of-buffer)
108     (define-key map ">" 'notmuch-search-goto-last-thread)
109     (define-key map "\M->" 'notmuch-search-goto-last-thread)
110     map)
111   "Keymap for \"notmuch search\" buffers.")
112 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
113
114 (defun notmuch-search-goto-last-thread (&optional arg)
115   "Move point to the last thread in the buffer."
116   (interactive "^P")
117   (end-of-buffer arg)
118   (beginning-of-line))
119
120 ;;;###autoload
121 (defun notmuch-search-mode ()
122   "Major mode for handling the output of \"notmuch search\""
123   (interactive)
124   (kill-all-local-variables)
125   (make-local-variable 'notmuch-search-query-string)
126   (use-local-map notmuch-search-mode-map)
127   (setq major-mode 'notmuch-search-mode
128         mode-name "notmuch-search")
129   (setq buffer-read-only t))
130
131 (defun notmuch-search-find-thread-id ()
132   (save-excursion
133     (beginning-of-line)
134     (let ((beg (point)))
135       (re-search-forward "[a-fA-F0-9]*")
136       (filter-buffer-substring beg (point)))))
137
138 (defun notmuch-search-markup-this-thread-id ()
139   (beginning-of-line)
140   (let ((beg (point)))
141     (re-search-forward "[a-fA-F0-9]*")
142     (forward-char)
143     (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)))
144
145 (defun notmuch-search-markup-thread-ids ()
146   (save-excursion
147     (goto-char (point-min))
148     (while (not (eobp))
149       (notmuch-search-markup-this-thread-id)
150       (next-line))))
151
152 (defun notmuch-search-hide-thread-ids ()
153   (interactive)
154   (add-to-invisibility-spec 'notmuch-search))
155
156 (defun notmuch-search-show-thread-ids ()
157   (interactive)
158   (remove-from-invisibility-spec 'notmuch-search))
159
160 (defun notmuch-search-show-thread ()
161   (interactive)
162   (notmuch-show (notmuch-search-find-thread-id)))
163
164 (defun notmuch-search-call-notmuch-process (&rest args)
165   (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
166     (with-current-buffer error-buffer
167         (erase-buffer))
168     (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
169         (point)
170       (progn
171         (with-current-buffer error-buffer
172           (let ((beg (point-min))
173                 (end (- (point-max) 1)))
174             (error (buffer-substring beg end))
175             ))))))
176
177 (defun notmuch-search-set-tags (tags)
178   (save-excursion
179     (end-of-line)
180     (re-search-backward "(")
181     (forward-char)
182     (let ((beg (point))
183           (inhibit-read-only t))
184       (re-search-forward ")")
185       (backward-char)
186       (let ((end (point)))
187         (delete-region beg end)
188         (insert (mapconcat  'identity tags " "))))))
189
190 (defun notmuch-search-get-tags ()
191   (save-excursion
192     (end-of-line)
193     (re-search-backward "(")
194     (let ((beg (+ (point) 1)))
195       (re-search-forward ")")
196       (let ((end (- (point) 1)))
197         (split-string (buffer-substring beg end))))))
198
199 (defun notmuch-search-add-tag (tag)
200   (interactive "sTag to add: ")
201   (notmuch-search-call-notmuch-process "tag" (concat "+" tag) (concat "thread:" (notmuch-search-find-thread-id)))
202   (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
203
204 (defun notmuch-search-remove-tag (tag)
205   (interactive "sTag to remove: ")
206   (notmuch-search-call-notmuch-process "tag" (concat "-" tag) (concat "thread:" (notmuch-search-find-thread-id)))
207   (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
208
209 (defun notmuch-search-archive-thread ()
210   (interactive)
211   (notmuch-search-remove-tag "inbox"))
212
213 (defun notmuch-search (query)
214   "Run \"notmuch search\" with the given query string and display results."
215   (interactive "sNotmuch search: ")
216   (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
217     (switch-to-buffer buffer)
218     (notmuch-search-mode)
219     (set 'notmuch-search-query-string query)
220     (let ((proc (get-buffer-process (current-buffer)))
221           (inhibit-read-only t))
222       (if proc
223           (error "notmuch search process already running for query `%s'" query)
224         )
225       (erase-buffer)
226       (goto-char (point-min))
227       (save-excursion
228         (call-process "notmuch" nil t nil "search" query)
229         (notmuch-search-markup-thread-ids)
230         ; A well-behaved program ends its output with a newline, but we
231         ; don't actually want the blank line at the end of the file.
232         (goto-char (point-max))
233         (if (looking-at "^$")
234             (delete-backward-char 1)
235           )
236         ))))
237
238 (defun notmuch-search-filter (query)
239   "Run \"notmuch search\" to refine the current search results.
240
241 A search string will be constructed by appending QUERY to the
242 current search string, and the results of \"notmuch search\" for
243 the combined query will be displayed."
244   (interactive "sFilter search: ")
245   (notmuch-search (concat notmuch-search-query-string " and " query)))
246
247 (defun notmuch ()
248   "Run notmuch to display all mail with tag of 'inbox'"
249   (interactive)
250   (notmuch-search "tag:inbox"))
251
252 (provide 'notmuch)