1 ;;; notmuch-company.el --- Mail address completion for notmuch via company-mode -*- lexical-binding: t -*-
3 ;; Copyright © Trevor Jim
4 ;; Copyright © Michal Sojka
6 ;; This file is part of Notmuch.
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.
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.
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/>.
21 ;; Authors: Trevor Jim <tjim@mac.com>
22 ;; Michal Sojka <sojkam1@fel.cvut.cz>
23 ;; Keywords: mail, completion
27 ;; Mail address completion for notmuch via company-mode. To enable
28 ;; this, install company mode from <https://company-mode.github.io/>.
30 ;; NB company-minimum-prefix-length defaults to 3 so you don't get
31 ;; completion unless you type 3 characters.
35 (eval-when-compile (require 'cl-lib))
37 (require 'notmuch-lib)
39 (defvar notmuch-company-last-prefix nil)
40 (make-variable-buffer-local 'notmuch-company-last-prefix)
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)
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)
56 (defun notmuch-company-setup ()
58 (make-local-variable 'company-backends)
59 (setq company-backends '(notmuch-company))
60 ;; Disable automatic company completion unless an internal
61 ;; completion method is configured. Company completion (using
62 ;; internal completion) can still be accessed via standard company
63 ;; functions, e.g., company-complete.
64 (unless (eq notmuch-address-command 'internal)
65 (setq-local company-idle-delay nil)))
68 (defun notmuch-company (command &optional arg &rest _ignore)
69 "`company-mode' completion back-end for `notmuch'."
70 (interactive (list 'interactive))
72 (let ((case-fold-search t)
73 (completion-ignore-case t))
75 (interactive (company-begin-backend 'notmuch-company))
76 (prefix (and (derived-mode-p 'message-mode)
78 (concat notmuch-address-completion-headers-regexp ".*")
79 (line-beginning-position))
80 (setq notmuch-company-last-prefix
81 (company-grab "[:,][ \t]*\\(.*\\)" 1 (point-at-bol)))))
83 ((notmuch-address--harvest-ready)
84 ;; Update harvested addressed from time to time
85 (notmuch-address-harvest-trigger)
86 (notmuch-address-matching arg))
90 ;; First run quick asynchronous harvest
91 ;; based on what the user entered so far
92 (notmuch-address-harvest
94 (lambda (_proc _event)
95 (funcall callback (notmuch-address-matching arg))
96 ;; Then start the (potentially long-running)
97 ;; full asynchronous harvest if necessary
98 (notmuch-address-harvest-trigger))))))))
99 (match (if (string-match notmuch-company-last-prefix arg)
103 (run-hook-with-args 'notmuch-address-post-completion-functions arg))
107 (provide 'notmuch-company)
109 ;;; notmuch-company.el ends here