]> code.delx.au - gnu-emacs-elpa/blob - company-capf.el
company-capf: get rid of caching last capf-data
[gnu-emacs-elpa] / company-capf.el
1 ;;; company-capf.el --- company-mode completion-at-point-functions back-end -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2013-2014 Free Software Foundation, Inc.
4
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6
7 ;; This file is part of GNU Emacs.
8
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.
13
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.
18
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/>.
21
22
23 ;;; Commentary:
24 ;;
25
26 ;;; Code:
27
28 (require 'company)
29 (require 'cl-lib)
30
31 (defun company--capf-data ()
32 (cl-letf* (((default-value 'completion-at-point-functions)
33 ;; Ignore tags-completion-at-point-function because it subverts
34 ;; company-etags in the default value of company-backends, where
35 ;; the latter comes later.
36 (remove 'tags-completion-at-point-function
37 (default-value 'completion-at-point-functions)))
38 (data (run-hook-wrapped 'completion-at-point-functions
39 ;; Ignore misbehaving functions.
40 #'completion--capf-wrapper 'optimist)))
41 (when (and (consp (cdr data)) (numberp (nth 1 data))) data)))
42
43 (defun company-capf (command &optional arg &rest _args)
44 "`company-mode' back-end using `completion-at-point-functions'."
45 (interactive (list 'interactive))
46 (pcase command
47 (`interactive (company-begin-backend 'company-capf))
48 (`prefix
49 (let ((res (company--capf-data)))
50 (when res
51 (if (> (nth 2 res) (point))
52 'stop
53 (buffer-substring-no-properties (nth 1 res) (point))))))
54 (`candidates
55 (let ((res (company--capf-data)))
56 (when res
57 (let* ((table (nth 3 res))
58 (pred (plist-get (nthcdr 4 res) :predicate))
59 (meta (completion-metadata
60 (buffer-substring (nth 1 res) (nth 2 res))
61 table pred))
62 (sortfun (cdr (assq 'display-sort-function meta)))
63 (candidates (completion-all-completions arg table pred (length arg)))
64 (last (last candidates))
65 (base-size (and (numberp (cdr last)) (cdr last))))
66 (when base-size
67 (setcdr last nil))
68 (when sortfun
69 (setq candidates (funcall sortfun candidates)))
70 (if (not (zerop (or base-size 0)))
71 (let ((before (substring arg 0 base-size)))
72 (mapcar (lambda (candidate)
73 (concat before candidate))
74 candidates))
75 candidates)))))
76 (`sorted
77 (let ((res (company--capf-data)))
78 (when res
79 (let ((meta (completion-metadata
80 (buffer-substring (nth 1 res) (nth 2 res))
81 (nth 3 res) (plist-get (nthcdr 4 res) :predicate))))
82 (cdr (assq 'display-sort-function meta))))))
83 (`match
84 ;; Can't just use 0 when base-size (see above) is non-zero.
85 (let ((start (if (get-text-property 0 'font-lock-face arg)
86 0
87 (next-single-property-change 0 'font-lock-face arg))))
88 (when start
89 ;; completions-common-part comes first, but we can't just look for this
90 ;; value because it can be in a list.
91 (or
92 (let ((value (get-text-property start 'font-lock-face arg)))
93 (text-property-not-all start (length arg)
94 'font-lock-face value arg))
95 (length arg)))))
96 (`duplicates t)
97 (`no-cache t) ;Not much can be done here, as long as we handle
98 ;non-prefix matches.
99 (`meta
100 (let ((f (plist-get (nthcdr 4 (company--capf-data)) :company-docsig)))
101 (when f (funcall f arg))))
102 (`doc-buffer
103 (let ((f (plist-get (nthcdr 4 (company--capf-data)) :company-doc-buffer)))
104 (when f (funcall f arg))))
105 (`location
106 (let ((f (plist-get (nthcdr 4 (company--capf-data)) :company-location)))
107 (when f (funcall f arg))))
108 (`annotation
109 (save-excursion
110 ;; FIXME: `company-begin' sets `company-point' after calling
111 ;; `company--begin-new'. We shouldn't rely on `company-point' here,
112 ;; better to cache the capf-data value instead. However: we can't just
113 ;; save the last capf-data value in `prefix', because that command can
114 ;; get called more often than `candidates', and at any point in the
115 ;; buffer (https://github.com/company-mode/company-mode/issues/153).
116 ;; We could try propertizing the returned prefix string, but it's not
117 ;; passed to `annotation', and `company-prefix' is set only after
118 ;; `company--strip-duplicates' is called.
119 (when company-point
120 (goto-char company-point))
121 (let ((f (plist-get (nthcdr 4 (company--capf-data)) :annotation-function)))
122 (when f (funcall f arg)))))
123 (`require-match
124 (plist-get (nthcdr 4 (company--capf-data)) :company-require-match))
125 (`init nil) ;Don't bother: plenty of other ways to initialize the code.
126 (`post-completion
127 (let* ((res (company--capf-data))
128 (exit-function (plist-get (nthcdr 4 res) :exit-function)))
129 (if exit-function
130 (funcall exit-function arg 'finished))))
131 ))
132
133 (provide 'company-capf)
134
135 ;;; company-capf.el ends here