1 ;;; company-capf.el --- company-mode completion-at-point-functions back-end -*- lexical-binding: t -*-
3 ;; Copyright (C) 2013-2014 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs 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 ;; GNU Emacs 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31 (defvar company--capf-cache nil)
33 (defun company--capf-data ()
34 (let ((cache company--capf-cache))
35 (if (and (equal (current-buffer) (car cache))
36 (equal (point) (car (setq cache (cdr cache))))
37 (equal (buffer-chars-modified-tick) (car (setq cache (cdr cache)))))
39 (let ((data (company--capf-data-real)))
40 (setq company--capf-cache
41 (list (current-buffer) (point) (buffer-chars-modified-tick) data))
44 (defun company--capf-data-real ()
45 (cl-letf* (((default-value 'completion-at-point-functions)
46 ;; Ignore tags-completion-at-point-function because it subverts
47 ;; company-etags in the default value of company-backends, where
48 ;; the latter comes later.
49 (remove 'tags-completion-at-point-function
50 (default-value 'completion-at-point-functions)))
51 (data (run-hook-wrapped 'completion-at-point-functions
52 ;; Ignore misbehaving functions.
53 #'completion--capf-wrapper 'optimist)))
54 (when (and (consp (cdr data)) (numberp (nth 1 data))) data)))
56 (defun company-capf (command &optional arg &rest _args)
57 "`company-mode' back-end using `completion-at-point-functions'."
58 (interactive (list 'interactive))
60 (`interactive (company-begin-backend 'company-capf))
62 (let ((res (company--capf-data)))
64 (if (> (nth 2 res) (point))
66 (buffer-substring-no-properties (nth 1 res) (point))))))
68 (let ((res (company--capf-data)))
70 (let* ((table (nth 3 res))
71 (pred (plist-get (nthcdr 4 res) :predicate))
72 (meta (completion-metadata
73 (buffer-substring (nth 1 res) (nth 2 res))
75 (sortfun (cdr (assq 'display-sort-function meta)))
76 (candidates (completion-all-completions arg table pred (length arg)))
77 (last (last candidates))
78 (base-size (and (numberp (cdr last)) (cdr last))))
82 (setq candidates (funcall sortfun candidates)))
83 (if (not (zerop (or base-size 0)))
84 (let ((before (substring arg 0 base-size)))
85 (mapcar (lambda (candidate)
86 (concat before candidate))
90 (let ((res (company--capf-data)))
92 (let ((meta (completion-metadata
93 (buffer-substring (nth 1 res) (nth 2 res))
94 (nth 3 res) (plist-get (nthcdr 4 res) :predicate))))
95 (cdr (assq 'display-sort-function meta))))))
97 ;; Can't just use 0 when base-size (see above) is non-zero.
98 (let ((start (if (get-text-property 0 'font-lock-face arg)
100 (next-single-property-change 0 'font-lock-face arg))))
102 ;; completions-common-part comes first, but we can't just look for this
103 ;; value because it can be in a list.
105 (let ((value (get-text-property start 'font-lock-face arg)))
106 (text-property-not-all start (length arg)
107 'font-lock-face value arg))
110 (`no-cache t) ;Not much can be done here, as long as we handle
113 (let ((f (plist-get (nthcdr 4 (company--capf-data)) :company-docsig)))
114 (when f (funcall f arg))))
116 (let ((f (plist-get (nthcdr 4 (company--capf-data)) :company-doc-buffer)))
117 (when f (funcall f arg))))
119 (let ((f (plist-get (nthcdr 4 (company--capf-data)) :company-location)))
120 (when f (funcall f arg))))
123 ;; FIXME: `company-begin' sets `company-point' after calling
124 ;; `company--begin-new'. We shouldn't rely on `company-point' here,
125 ;; better to cache the capf-data value instead. However: we can't just
126 ;; save the last capf-data value in `prefix', because that command can
127 ;; get called more often than `candidates', and at any point in the
128 ;; buffer (https://github.com/company-mode/company-mode/issues/153).
129 ;; We could try propertizing the returned prefix string, but it's not
130 ;; passed to `annotation', and `company-prefix' is set only after
131 ;; `company--strip-duplicates' is called.
133 (goto-char company-point))
134 (let ((f (plist-get (nthcdr 4 (company--capf-data)) :annotation-function)))
135 (when f (funcall f arg)))))
137 (plist-get (nthcdr 4 (company--capf-data)) :company-require-match))
138 (`init nil) ;Don't bother: plenty of other ways to initialize the code.
140 (let* ((res (company--capf-data))
141 (exit-function (plist-get (nthcdr 4 res) :exit-function))
143 (pred (plist-get (nthcdr 4 res) :predicate)))
145 ;; Follow the example of `completion--done'.
146 (funcall exit-function arg
147 (if (eq (try-completion arg table pred) t)
151 (provide 'company-capf)
153 ;;; company-capf.el ends here