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