]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-address.el
69a52a295a59404b70bc8a9d8d75e809a083931b
[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 "notmuch-addresses"
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."
30   :type 'string
31   :group 'notmuch)
32
33 (defvar notmuch-address-message-alist-member
34   '("^\\(Resent-\\)?\\(To\\|B?Cc\\|Reply-To\\|From\\|Mail-Followup-To\\|Mail-Copies-To\\):"
35               . notmuch-address-expand-name))
36
37 (defvar notmuch-address-history nil)
38
39 (defun notmuch-address-message-insinuate ()
40   (if (not (memq notmuch-address-message-alist-member message-completion-alist))
41       (setq message-completion-alist
42             (push notmuch-address-message-alist-member message-completion-alist))))
43
44 (defun notmuch-address-options (original)
45   (process-lines notmuch-address-command original))
46
47 (defun notmuch-address-expand-name ()
48   (let* ((end (point))
49          (beg (save-excursion
50                 (re-search-backward "\\(\\`\\|[\n:,]\\)[ \t]*")
51                 (goto-char (match-end 0))
52                 (point)))
53          (orig (buffer-substring-no-properties beg end))
54          (completion-ignore-case t)
55          (options (notmuch-address-options orig))
56          (chosen (if (eq (length options) 1)
57                      (car options)
58                    (completing-read "Address: " (cdr options) nil nil (car options)
59                                     'notmuch-address-history))))
60     (when chosen
61       (push chosen notmuch-address-history)
62       (delete-region beg end)
63       (insert chosen))))
64
65 ;; Copied from `w3m-which-command'.
66 (defun notmuch-address-locate-command (command)
67   "Return non-nil if `command' is an executable either on
68 `exec-path' or an absolute pathname."
69   (when (stringp command)
70     (if (and (file-name-absolute-p command)
71              (file-executable-p command))
72         command
73       (setq command (file-name-nondirectory command))
74       (catch 'found-command
75         (let (bin)
76           (dolist (dir exec-path)
77             (setq bin (expand-file-name command dir))
78             (when (or (and (file-executable-p bin)
79                            (not (file-directory-p bin)))
80                       (and (file-executable-p (setq bin (concat bin ".exe")))
81                            (not (file-directory-p bin))))
82               (throw 'found-command bin))))))))
83
84 ;; If we can find the program specified by `notmuch-address-command',
85 ;; insinuate ourselves into `message-mode'.
86 (when (notmuch-address-locate-command notmuch-address-command)
87   (notmuch-address-message-insinuate))
88
89 ;;
90
91 (provide 'notmuch-address)