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