]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-company.el
emacs: address completion, allow sender/recipient and filters
[notmuch] / emacs / notmuch-company.el
1 ;;; notmuch-company.el --- Mail address completion for notmuch via company-mode  -*- lexical-binding: t -*-
2
3 ;; Authors: Trevor Jim <tjim@mac.com>
4 ;;          Michal Sojka <sojkam1@fel.cvut.cz>
5 ;;
6 ;; Keywords: mail, completion
7
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
21 ;;; Commentary:
22
23 ;; To enable this, install company mode (https://company-mode.github.io/)
24 ;;
25 ;; NB company-minimum-prefix-length defaults to 3 so you don't get
26 ;; completion unless you type 3 characters
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (defvar notmuch-company-last-prefix nil)
33 (make-variable-buffer-local 'notmuch-company-last-prefix)
34 (declare-function company-begin-backend "company")
35 (declare-function company-grab "company")
36 (declare-function company-mode "company")
37 (declare-function company-manual-begin "company")
38 (defvar company-backends)
39
40 (declare-function notmuch-address-harvest "notmuch-address")
41 (declare-function notmuch-address-harvest-trigger "notmuch-address")
42 (declare-function notmuch-address-matching "notmuch-address")
43 (defvar notmuch-address-full-harvest-finished)
44 (defvar notmuch-address-completion-headers-regexp)
45
46 ;;;###autoload
47 (defun notmuch-company-setup ()
48   (company-mode)
49   (make-local-variable 'company-backends)
50   (setq company-backends '(notmuch-company)))
51
52 ;;;###autoload
53 (defun notmuch-company (command &optional arg &rest _ignore)
54   "`company-mode' completion back-end for `notmuch'."
55   (interactive (list 'interactive))
56   (require 'company)
57   (let ((case-fold-search t)
58         (completion-ignore-case t))
59     (case command
60       (interactive (company-begin-backend 'notmuch-company))
61       (prefix (and (derived-mode-p 'message-mode)
62                    (looking-back (concat notmuch-address-completion-headers-regexp ".*")
63                                  (line-beginning-position))
64                    (setq notmuch-company-last-prefix (company-grab "[:,][ \t]*\\(.*\\)" 1 (point-at-bol)))))
65       (candidates (cond
66                    (notmuch-address-full-harvest-finished
67                     ;; Update harvested addressed from time to time
68                     (notmuch-address-harvest-trigger)
69                     (notmuch-address-matching arg))
70                    (t
71                     (cons :async
72                           (lambda (callback)
73                             ;; First run quick asynchronous harvest based on what the user entered so far
74                             (notmuch-address-harvest
75                              arg nil
76                              (lambda (_proc _event)
77                                (funcall callback (notmuch-address-matching arg))
78                                ;; Then start the (potentially long-running) full asynchronous harvest if necessary
79                                (notmuch-address-harvest-trigger))))))))
80       (match (if (string-match notmuch-company-last-prefix arg)
81                  (match-end 0)
82                0))
83       (no-cache t))))
84
85
86 (provide 'notmuch-company)
87
88 ;;; notmuch-company.el ends here