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