1 ;; notmuch-company.el --- Mail address completion for notmuch via company-mode -*- no-byte-compile: t; lexical-binding: t -*-
4 ;; Authors: Trevor Jim <tjim@mac.com>
5 ;; Michal Sojka <sojkam1@fel.cvut.cz>
7 ;; Keywords: mail, completion
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
24 ;; To enable this, install company mode (https://company-mode.github.io/)
27 ;; (require 'notmuch-company)
31 ;; NB company-minimum-prefix-length defaults to 3 so you don't get
32 ;; completion unless you type 3 characters
38 (require 'notmuch-address)
41 (defvar-local notmuch-company-last-prefix nil)
44 (defun notmuch-company (command &optional arg &rest ignore)
45 "`company-mode' completion back-end for `notmuch'."
46 (interactive (list 'interactive))
47 (let ((case-fold-search t)
48 (completion-ignore-case t))
50 (interactive (company-begin-backend 'notmuch-company))
51 (prefix (and (eq major-mode 'message-mode)
52 (looking-back "^\\(To\\|Cc\\|Bcc\\):.*"
53 (line-beginning-position))
54 (setq notmuch-company-last-prefix (company-grab "[:,][ \t]*\\(.*\\)" 1 (point-at-bol)))))
56 (notmuch-address-full-harvest-finished
57 ;; Update harvested addressed from time to time
58 (notmuch-address-harvest-trigger)
59 (notmuch-address-matching arg))
63 ;; First run quick asynchronous harvest based on what the user entered so far
64 (notmuch-address-harvest
65 (format "to:%s*" arg) nil
67 (funcall callback (notmuch-address-matching arg))
68 ;; Then (re)start potentially long-running full asynchronous harvesting
69 (notmuch-address-harvest-trigger))))))))
70 (match (if (string-match notmuch-company-last-prefix arg)
76 (add-hook 'message-mode-hook '(lambda ()
78 (make-local-variable 'company-backends)
79 (setq company-backends '(notmuch-company))))
81 (provide 'notmuch-company)