c1f3594e513ad55ff2ace05d90ae9eaf6a6956fe
[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-lib))
31
32 (require 'notmuch-lib)
33
34 (defvar notmuch-company-last-prefix nil)
35 (make-variable-buffer-local 'notmuch-company-last-prefix)
36 (declare-function company-begin-backend "company")
37 (declare-function company-grab "company")
38 (declare-function company-mode "company")
39 (declare-function company-manual-begin "company")
40 (defvar company-backends)
41 (defvar company-idle-delay)
42
43 (declare-function notmuch-address-harvest "notmuch-address")
44 (declare-function notmuch-address-harvest-trigger "notmuch-address")
45 (declare-function notmuch-address-matching "notmuch-address")
46 (declare-function notmuch-address--harvest-ready "notmuch-address")
47 (defvar notmuch-address-completion-headers-regexp)
48 (defvar notmuch-address-command)
49
50 ;;;###autoload
51 (defun notmuch-company-setup ()
52   (company-mode)
53   (make-local-variable 'company-backends)
54   (setq company-backends '(notmuch-company))
55   ;; Disable automatic company completion unless an internal
56   ;; completion method is configured. Company completion (using
57   ;; internal completion) can still be accessed via standard company
58   ;; functions, e.g., company-complete.
59   (unless (eq notmuch-address-command 'internal)
60     (notmuch-setq-local company-idle-delay nil)))
61
62 ;;;###autoload
63 (defun notmuch-company (command &optional arg &rest _ignore)
64   "`company-mode' completion back-end for `notmuch'."
65   (interactive (list 'interactive))
66   (require 'company)
67   (let ((case-fold-search t)
68         (completion-ignore-case t))
69     (cl-case command
70       (interactive (company-begin-backend 'notmuch-company))
71       (prefix (and (derived-mode-p 'message-mode)
72                    (looking-back
73                     (concat notmuch-address-completion-headers-regexp ".*")
74                     (line-beginning-position))
75                    (setq notmuch-company-last-prefix
76                          (company-grab "[:,][ \t]*\\(.*\\)" 1 (point-at-bol)))))
77       (candidates (cond
78                    ((notmuch-address--harvest-ready)
79                     ;; Update harvested addressed from time to time
80                     (notmuch-address-harvest-trigger)
81                     (notmuch-address-matching arg))
82                    (t
83                     (cons :async
84                           (lambda (callback)
85                             ;; First run quick asynchronous harvest
86                             ;; based on what the user entered so far
87                             (notmuch-address-harvest
88                              arg nil
89                              (lambda (_proc _event)
90                                (funcall callback (notmuch-address-matching arg))
91                                ;; Then start the (potentially long-running)
92                                ;; full asynchronous harvest if necessary
93                                (notmuch-address-harvest-trigger))))))))
94       (match (if (string-match notmuch-company-last-prefix arg)
95                  (match-end 0)
96                0))
97       (post-completion
98        (run-hook-with-args 'notmuch-address-post-completion-functions arg))
99       (no-cache t))))
100
101
102 (provide 'notmuch-company)
103
104 ;;; notmuch-company.el ends here