]> code.delx.au - gnu-emacs-elpa/blob - company-capf.el
Merge pull request #101 from bbatsov/master
[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 'cl-lib)
29
30 (defvar company--capf-data nil)
31 (make-variable-buffer-local 'company--capf-data)
32
33 (defun company--capf-clear-data (&optional _ignore)
34 (setq company--capf-data nil)
35 (remove-hook 'company-completion-cancelled-hook 'company--capf-clear-data t)
36 (remove-hook 'company-completion-finished-hook 'company--capf-clear-data t))
37
38 (defun company--capf-data ()
39 ;; Ignore tags-completion-at-point-function because it subverts company-etags
40 ;; in the default value of company-backends, where the latter comes later.
41 (cl-letf* (((default-value 'completion-at-point-functions) nil)
42 (data (run-hook-wrapped 'completion-at-point-functions
43 ;; Ignore misbehaving functions.
44 #'completion--capf-wrapper 'optimist)))
45 (when (and (consp (cdr data)) (numberp (nth 1 data))) data)))
46
47 (defun company-capf (command &optional arg &rest _args)
48 "`company-mode' back-end using `completion-at-point-functions'.
49 Requires Emacs 24.1 or newer."
50 (interactive (list 'interactive))
51 (pcase command
52 (`interactive (company-begin-backend 'company-capf))
53 (`prefix
54 (let ((res (company--capf-data)))
55 (when res
56 (if (> (nth 2 res) (point))
57 'stop
58 (setq company--capf-data res)
59 (add-hook 'company-completion-cancelled-hook 'company--capf-clear-data nil t)
60 (add-hook 'company-completion-finished-hook 'company--capf-clear-data nil t)
61 (buffer-substring-no-properties (nth 1 res) (point))))))
62 (`candidates
63 (let ((res company--capf-data))
64 (when res
65 (let* ((table (nth 3 res))
66 (pred (plist-get (nthcdr 4 res) :predicate))
67 (meta (completion-metadata
68 (buffer-substring (nth 1 res) (nth 2 res))
69 table pred))
70 (sortfun (cdr (assq 'display-sort-function meta)))
71 (candidates (completion-all-completions arg table pred (length arg)))
72 (last (last candidates))
73 (base-size (and (numberp (cdr last)) (cdr last))))
74 (when base-size
75 (setcdr last nil))
76 (when sortfun
77 (setq candidates (funcall sortfun candidates)))
78 (if (not (zerop (or base-size 0)))
79 (let ((before (substring arg 0 base-size)))
80 (mapcar (lambda (candidate)
81 (concat before candidate))
82 candidates))
83 candidates)))))
84 (`sorted
85 (let ((res company--capf-data))
86 (when res
87 (let ((meta (completion-metadata
88 (buffer-substring (nth 1 res) (nth 2 res))
89 (nth 3 res) (plist-get (nthcdr 4 res) :predicate))))
90 (cdr (assq 'display-sort-function meta))))))
91 (`match
92 ;; Can't just use 0 when base-size (see above) is non-zero.
93 (let ((start (if (get-text-property 0 'font-lock-face arg)
94 0
95 (next-single-property-change 0 'font-lock-face arg))))
96 (when start
97 ;; completions-common-part comes first, but we can't just look for this
98 ;; value because it can be in a list.
99 (or
100 (let ((value (get-text-property start 'font-lock-face arg)))
101 (text-property-not-all start (length arg)
102 'font-lock-face value arg))
103 (length arg)))))
104 (`duplicates t)
105 (`no-cache t) ;Not much can be done here, as long as we handle
106 ;non-prefix matches.
107 (`meta
108 (let ((f (plist-get (nthcdr 4 company--capf-data) :company-docsig)))
109 (when f (funcall f arg))))
110 (`doc-buffer
111 (let ((f (plist-get (nthcdr 4 company--capf-data) :company-doc-buffer)))
112 (when f (funcall f arg))))
113 (`location
114 (let ((f (plist-get (nthcdr 4 company--capf-data) :company-location)))
115 (when f (funcall f arg))))
116 (`annotation
117 (save-excursion
118 ;; FIXME: `company-begin' sets `company-point' after calling
119 ;; `company--begin-new'. We shouldn't rely on `company-point' here,
120 ;; better to cache the capf-data value instead.
121 (when company-point
122 (goto-char company-point))
123 (let ((f (plist-get (nthcdr 4 company--capf-data) :annotation-function)))
124 (when f (funcall f arg)))))
125 (`require-match
126 (plist-get (nthcdr 4 company--capf-data) :company-require-match))
127 (`init nil) ;Don't bother: plenty of other ways to initialize the code.
128 (`post-completion
129 (let* ((res company--capf-data)
130 (exit-function (plist-get (nthcdr 4 res) :exit-function)))
131 (if exit-function
132 (funcall exit-function arg 'finished))))
133 ))
134
135 (provide 'company-capf)
136
137 ;;; company-capf.el ends here