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