]> git.notmuchmail.org Git - notmuch/blob - notmuch.el
Makefile: Rewrite to use NOTMUCH rather than MY in variable names.
[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-show-thread ()
99   (interactive)
100   (notmuch-show (notmuch-search-find-thread-id)))
101
102 (defun notmuch-search-call-notmuch-process (&rest args)
103   (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
104     (with-current-buffer error-buffer
105         (erase-buffer))
106     (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
107         (point)
108       (progn
109         (with-current-buffer error-buffer
110           (let ((beg (point-min))
111                 (end (- (point-max) 1)))
112             (error (buffer-substring beg end))
113             ))))))
114
115 (defun notmuch-search-add-tag (tag)
116   (interactive "sTag to add: ")
117   (notmuch-search-call-notmuch-process "tag" (concat "+" tag) (concat "thread:" (notmuch-search-find-thread-id))))
118
119 (defun notmuch-search-remove-tag (tag)
120   (interactive "sTag to remove: ")
121   (notmuch-search-call-notmuch-process "tag" (concat "-" tag) (concat "thread:" (notmuch-search-find-thread-id))))
122
123 (defun notmuch-search-archive-thread ()
124   (interactive)
125   (notmuch-search-remove-tag "inbox"))
126
127 (defun notmuch-search (query)
128   "Run \"notmuch search\" with the given query string and display results."
129   (interactive "sNotmuch search: ")
130   (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
131     (switch-to-buffer buffer)
132     (notmuch-search-mode)
133     (let ((proc (get-buffer-process (current-buffer)))
134           (inhibit-read-only t))
135       (if proc
136           (error "notmuch search process already running for query `%s'" query)
137         )
138       (erase-buffer)
139       (beginning-of-buffer)
140       (save-excursion
141         (call-process "notmuch" nil t nil "search" query)
142         )
143       )))
144
145 (defun notmuch ()
146   "Run notmuch to display all mail with tag of 'inbox'"
147   (interactive)
148   (notmuch-search "tag:inbox"))
149
150 (provide 'notmuch)