]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-company.el
b50e73c8899bda57525bff9528920f17c479bc93
[notmuch] / emacs / notmuch-company.el
1 ;;; notmuch-company.el --- Mail address completion for notmuch via company-mode  -*- lexical-binding: t -*-
2 ;;
3 ;; Copyright © Trevor Jim
4 ;; Copyright © Michal Sojka
5 ;;
6 ;; This file is part of Notmuch.
7 ;;
8 ;; Notmuch is free software: you can redistribute it and/or modify it
9 ;; 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 ;; Notmuch is distributed in the hope that it will be useful, but
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 ;; General Public License for more details.
17 ;;
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with Notmuch.  If not, see <https://www.gnu.org/licenses/>.
20 ;;
21 ;; Authors: Trevor Jim <tjim@mac.com>
22 ;;          Michal Sojka <sojkam1@fel.cvut.cz>
23 ;; Keywords: mail, completion
24
25 ;;; Commentary:
26
27 ;; Mail address completion for notmuch via company-mode.  To enable
28 ;; this, install company mode from <https://company-mode.github.io/>.
29 ;;
30 ;; NB company-minimum-prefix-length defaults to 3 so you don't get
31 ;; completion unless you type 3 characters.
32
33 ;;; Code:
34
35 (eval-when-compile (require 'cl-lib))
36
37 (require 'notmuch-lib)
38
39 (defvar-local notmuch-company-last-prefix nil)
40
41 (declare-function company-begin-backend "company")
42 (declare-function company-grab "company")
43 (declare-function company-mode "company")
44 (declare-function company-manual-begin "company")
45 (defvar company-backends)
46 (defvar company-idle-delay)
47
48 (declare-function notmuch-address-harvest "notmuch-address")
49 (declare-function notmuch-address-harvest-trigger "notmuch-address")
50 (declare-function notmuch-address-matching "notmuch-address")
51 (declare-function notmuch-address--harvest-ready "notmuch-address")
52 (defvar notmuch-address-completion-headers-regexp)
53 (defvar notmuch-address-command)
54
55 ;;;###autoload
56 (defun notmuch-company-setup ()
57   (company-mode)
58   (setq-local company-backends '(notmuch-company))
59   ;; Disable automatic company completion unless an internal
60   ;; completion method is configured. Company completion (using
61   ;; internal completion) can still be accessed via standard company
62   ;; functions, e.g., company-complete.
63   (unless (eq notmuch-address-command 'internal)
64     (setq-local company-idle-delay nil)))
65
66 ;;;###autoload
67 (defun notmuch-company (command &optional arg &rest _ignore)
68   "`company-mode' completion back-end for `notmuch'."
69   (interactive (list 'interactive))
70   (require 'company)
71   (let ((case-fold-search t)
72         (completion-ignore-case t))
73     (cl-case command
74       (interactive (company-begin-backend 'notmuch-company))
75       (prefix (and (derived-mode-p 'message-mode)
76                    (looking-back
77                     (concat notmuch-address-completion-headers-regexp ".*")
78                     (line-beginning-position))
79                    (setq notmuch-company-last-prefix
80                          (company-grab "[:,][ \t]*\\(.*\\)" 1 (point-at-bol)))))
81       (candidates (cond
82                    ((notmuch-address--harvest-ready)
83                     ;; Update harvested addressed from time to time
84                     (notmuch-address-harvest-trigger)
85                     (notmuch-address-matching arg))
86                    (t
87                     (cons :async
88                           (lambda (callback)
89                             ;; First run quick asynchronous harvest
90                             ;; based on what the user entered so far
91                             (notmuch-address-harvest
92                              arg nil
93                              (lambda (_proc _event)
94                                (funcall callback (notmuch-address-matching arg))
95                                ;; Then start the (potentially long-running)
96                                ;; full asynchronous harvest if necessary
97                                (notmuch-address-harvest-trigger))))))))
98       (match (if (string-match notmuch-company-last-prefix arg)
99                  (match-end 0)
100                0))
101       (post-completion
102        (run-hook-with-args 'notmuch-address-post-completion-functions arg))
103       (no-cache t))))
104
105
106 (provide 'notmuch-company)
107
108 ;;; notmuch-company.el ends here