]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-address.el
emacs: replace use of notmuch-address-message-insinuate
[notmuch] / emacs / notmuch-address.el
1 ;; notmuch-address.el --- address completion with notmuch
2 ;;
3 ;; Copyright © David Edmondson
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: David Edmondson <dme@dme.org>
21
22 (require 'message)
23
24 ;;
25
26 (defcustom notmuch-address-command nil
27   "The command which generates possible addresses. It must take a
28 single argument and output a list of possible matches, one per
29 line. The default value of nil disables address completion."
30   :type '(radio
31           (const :tag "Disable address completion" nil)
32           (string :tag "Use external completion command" "notmuch-addresses"))
33   :group 'notmuch-send
34   :group 'notmuch-external)
35
36 (defcustom notmuch-address-selection-function 'notmuch-address-selection-function
37   "The function to select address from given list. The function is
38 called with PROMPT, COLLECTION, and INITIAL-INPUT as arguments
39 (subset of what `completing-read' can be called with).
40 While executed the value of `completion-ignore-case' is t.
41 See documentation of function `notmuch-address-selection-function'
42 to know how address selection is made by default."
43   :type 'function
44   :group 'notmuch-send
45   :group 'notmuch-external)
46
47 (defun notmuch-address-selection-function (prompt collection initial-input)
48   "Call (`completing-read'
49       PROMPT COLLECTION nil nil INITIAL-INPUT 'notmuch-address-history)"
50   (completing-read
51    prompt collection nil nil initial-input 'notmuch-address-history))
52
53 (defvar notmuch-address-message-alist-member
54   '("^\\(Resent-\\)?\\(To\\|B?Cc\\|Reply-To\\|From\\|Mail-Followup-To\\|Mail-Copies-To\\):"
55               . notmuch-address-expand-name))
56
57 (defvar notmuch-address-history nil)
58
59 (defun notmuch-address-message-insinuate ()
60   (message "calling notmuch-address-message-insinuate is no longer needed"))
61
62 (defun notmuch-address-setup ()
63   (unless (memq notmuch-address-message-alist-member message-completion-alist)
64     (setq message-completion-alist
65           (push notmuch-address-message-alist-member message-completion-alist))))
66 (defun notmuch-address-options (original)
67   (process-lines notmuch-address-command original))
68
69 (defun notmuch-address-expand-name ()
70   (let* ((end (point))
71          (beg (save-excursion
72                 (re-search-backward "\\(\\`\\|[\n:,]\\)[ \t]*")
73                 (goto-char (match-end 0))
74                 (point)))
75          (orig (buffer-substring-no-properties beg end))
76          (completion-ignore-case t)
77          (options (with-temp-message "Looking for completion candidates..."
78                     (notmuch-address-options orig)))
79          (num-options (length options))
80          (chosen (cond
81                   ((eq num-options 0)
82                    nil)
83                   ((eq num-options 1)
84                    (car options))
85                   (t
86                    (funcall notmuch-address-selection-function
87                             (format "Address (%s matches): " num-options)
88                             (cdr options) (car options))))))
89     (if chosen
90         (progn
91           (push chosen notmuch-address-history)
92           (delete-region beg end)
93           (insert chosen))
94       (message "No matches.")
95       (ding))))
96
97 ;; Copied from `w3m-which-command'.
98 (defun notmuch-address-locate-command (command)
99   "Return non-nil if `command' is an executable either on
100 `exec-path' or an absolute pathname."
101   (when (stringp command)
102     (if (and (file-name-absolute-p command)
103              (file-executable-p command))
104         command
105       (setq command (file-name-nondirectory command))
106       (catch 'found-command
107         (let (bin)
108           (dolist (dir exec-path)
109             (setq bin (expand-file-name command dir))
110             (when (or (and (file-executable-p bin)
111                            (not (file-directory-p bin)))
112                       (and (file-executable-p (setq bin (concat bin ".exe")))
113                            (not (file-directory-p bin))))
114               (throw 'found-command bin))))))))
115
116 (provide 'notmuch-address)