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