]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-lib.el
emacs: use a single history for all searches
[notmuch] / emacs / notmuch-lib.el
1 ;; notmuch-lib.el --- common variables, functions and function declarations
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 ;; This is an part of an emacs-based interface to the notmuch mail system.
23
24 (defvar notmuch-command "notmuch"
25   "Command to run the notmuch binary.")
26
27 (defgroup notmuch nil
28   "Notmuch mail reader for Emacs."
29   :group 'mail)
30
31 (defgroup notmuch-hello nil
32   "Overview of saved searches, tags, etc."
33   :group 'notmuch)
34
35 (defgroup notmuch-search nil
36   "Searching and sorting mail."
37   :group 'notmuch)
38
39 (defgroup notmuch-show nil
40   "Showing messages and threads."
41   :group 'notmuch)
42
43 (defgroup notmuch-send nil
44   "Sending messages from Notmuch."
45   :group 'notmuch)
46
47 (custom-add-to-group 'notmuch-send 'message 'custom-group)
48
49 (defgroup notmuch-crypto nil
50   "Processing and display of cryptographic MIME parts."
51   :group 'notmuch)
52
53 (defgroup notmuch-hooks nil
54   "Running custom code on well-defined occasions."
55   :group 'notmuch)
56
57 (defgroup notmuch-external nil
58   "Running external commands from within Notmuch."
59   :group 'notmuch)
60
61 (defgroup notmuch-faces nil
62   "Graphical attributes for displaying text"
63   :group 'notmuch)
64
65 (defcustom notmuch-search-oldest-first t
66   "Show the oldest mail first when searching."
67   :type 'boolean
68   :group 'notmuch-search)
69
70 ;;
71
72 (defvar notmuch-search-history nil
73   "Variable to store notmuch searches history.")
74
75 (defcustom notmuch-saved-searches nil
76   "A list of saved searches to display."
77   :type '(alist :key-type string :value-type string)
78   :group 'notmuch-hello)
79
80 (defvar notmuch-folders nil
81   "Deprecated name for what is now known as `notmuch-saved-searches'.")
82
83 (defun notmuch-saved-searches ()
84   "Common function for querying the notmuch-saved-searches variable.
85
86 We do this as a function to support the old name of the
87 variable (`notmuch-folders') as well as for the default value if
88 the user hasn't set this variable with the old or new value."
89   (if notmuch-saved-searches
90       notmuch-saved-searches
91     (if notmuch-folders
92         notmuch-folders
93       '(("inbox" . "tag:inbox")
94         ("unread" . "tag:unread")))))
95
96 (defun notmuch-version ()
97   "Return a string with the notmuch version number."
98   (let ((long-string
99          ;; Trim off the trailing newline.
100          (substring (shell-command-to-string
101                      (concat notmuch-command " --version"))
102                     0 -1)))
103     (if (string-match "^notmuch\\( version\\)? \\(.*\\)$"
104                       long-string)
105         (match-string 2 long-string)
106       "unknown")))
107
108 (defun notmuch-config-get (item)
109   "Return a value from the notmuch configuration."
110   ;; Trim off the trailing newline
111   (substring (shell-command-to-string
112               (concat notmuch-command " config get " item))
113               0 -1))
114
115 (defun notmuch-database-path ()
116   "Return the database.path value from the notmuch configuration."
117   (notmuch-config-get "database.path"))
118
119 (defun notmuch-user-name ()
120   "Return the user.name value from the notmuch configuration."
121   (notmuch-config-get "user.name"))
122
123 (defun notmuch-user-primary-email ()
124   "Return the user.primary_email value from the notmuch configuration."
125   (notmuch-config-get "user.primary_email"))
126
127 (defun notmuch-user-other-email ()
128   "Return the user.other_email value (as a list) from the notmuch configuration."
129   (split-string (notmuch-config-get "user.other_email") "\n"))
130
131 (defun notmuch-kill-this-buffer ()
132   "Kill the current buffer."
133   (interactive)
134   (kill-buffer (current-buffer)))
135
136 ;;
137
138 (defun notmuch-common-do-stash (text)
139   "Common function to stash text in kill ring, and display in minibuffer."
140   (kill-new text)
141   (message "Stashed: %s" text))
142
143 ;;
144
145 (defun notmuch-remove-if-not (predicate list)
146   "Return a copy of LIST with all items not satisfying PREDICATE removed."
147   (let (out)
148     (while list
149       (when (funcall predicate (car list))
150         (push (car list) out))
151       (setq list (cdr list)))
152     (nreverse out)))
153
154 ;; This lets us avoid compiling these replacement functions when emacs
155 ;; is sufficiently new enough to supply them alone. We do the macro
156 ;; treatment rather than just wrapping our defun calls in a when form
157 ;; specifically so that the compiler never sees the code on new emacs,
158 ;; (since the code is triggering warnings that we don't know how to get
159 ;; rid of.
160 ;;
161 ;; A more clever macro here would accept a condition and a list of forms.
162 (defmacro compile-on-emacs-prior-to-23 (form)
163   "Conditionally evaluate form only on emacs < emacs-23."
164   (list 'when (< emacs-major-version 23)
165         form))
166
167 ;; Compatibility functions for versions of emacs before emacs 23.
168 ;;
169 ;; Both functions here were copied from emacs 23 with the following copyright:
170 ;;
171 ;; Copyright (C) 1985, 1986, 1992, 1994, 1995, 1999, 2000, 2001, 2002, 2003,
172 ;;   2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
173 ;;
174 ;; and under the GPL version 3 (or later) exactly as notmuch itself.
175 (compile-on-emacs-prior-to-23
176  (defun apply-partially (fun &rest args)
177    "Return a function that is a partial application of FUN to ARGS.
178 ARGS is a list of the first N arguments to pass to FUN.
179 The result is a new function which does the same as FUN, except that
180 the first N arguments are fixed at the values with which this function
181 was called."
182    (lexical-let ((fun fun) (args1 args))
183      (lambda (&rest args2) (apply fun (append args1 args2))))))
184
185 (compile-on-emacs-prior-to-23
186  (defun mouse-event-p (object)
187    "Return non-nil if OBJECT is a mouse click event."
188    (memq (event-basic-type object) '(mouse-1 mouse-2 mouse-3 mouse-movement))))
189
190 ;; This variable is used only buffer local, but it needs to be
191 ;; declared globally first to avoid compiler warnings.
192 (defvar notmuch-show-process-crypto nil)
193 (make-variable-buffer-local 'notmuch-show-process-crypto)
194
195 (provide 'notmuch-lib)
196