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