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