]> code.delx.au - gnu-emacs-elpa/blob - company-capf.el
Move company-capf to a separate file
[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 Free Software Foundation, Inc.
4
5 ;; Author: Stefan Monnier
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 (defun company--capf-data ()
29 (let ((data (run-hook-wrapped 'completion-at-point-functions
30 ;; Ignore misbehaving functions.
31 #'completion--capf-wrapper 'optimist)))
32 (when (consp data) data)))
33
34 (defun company-capf (command &optional arg &rest _args)
35 "`company-mode' back-end using `completion-at-point-functions'.
36 Requires Emacs 24.1 or newer."
37 (interactive (list 'interactive))
38 (case command
39 (interactive (company-begin-backend 'company-capf))
40 (prefix
41 (let ((res (company--capf-data)))
42 (when res
43 (if (> (nth 2 res) (point))
44 'stop
45 (buffer-substring-no-properties (nth 1 res) (point))))))
46 (candidates
47 (let ((res (company--capf-data)))
48 (when res
49 (let* ((table (nth 3 res))
50 (pred (plist-get (nthcdr 4 res) :predicate))
51 (meta (completion-metadata
52 (buffer-substring (nth 1 res) (nth 2 res))
53 table pred))
54 (sortfun (cdr (assq 'display-sort-function meta)))
55 (candidates (all-completions arg table pred)))
56 (if sortfun (funcall sortfun candidates) candidates)))))
57 (sorted
58 (let ((res (company--capf-data)))
59 (when res
60 (let ((meta (completion-metadata
61 (buffer-substring (nth 1 res) (nth 2 res))
62 (nth 3 res) (plist-get (nthcdr 4 res) :predicate))))
63 (cdr (assq 'display-sort-function meta))))))
64 (duplicates nil) ;Don't bother.
65 (no-cache t) ;FIXME: Improve!
66 (meta nil) ;FIXME: Return one-line docstring for `arg'.
67 (doc-buffer nil) ;FIXME: Return help buffer for `arg'.
68 (location nil) ;FIXME: Return (BUF . POS) or (FILE . LINENB) of `arg'.
69 (require-match nil) ;This should be a property of the front-end!
70 (init nil) ;Don't bother: plenty of other ways to initialize the code.
71 (post-completion
72 (let* ((res (company--capf-data))
73 (exit-function (plist-get (nthcdr 4 res) :exit-function)))
74 (if exit-function
75 (funcall exit-function arg 'finished))))
76 ))
77
78 (provide 'company-capf)
79
80 ;;; company-capf.el ends here