]> code.delx.au - gnu-emacs-elpa/blob - company-elisp.el
company-elisp-locals: Differentiate local function and variable bindings
[gnu-emacs-elpa] / company-elisp.el
1 ;;; company-elisp.el --- A company-mode completion back-end for emacs-lisp-mode
2
3 ;; Copyright (C) 2009, 2011-2012 Free Software Foundation, Inc.
4
5 ;; Author: Nikolaj Schumacher
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 (eval-when-compile (require 'cl))
30 (require 'help-mode)
31 (require 'find-func)
32
33 (defcustom company-elisp-detect-function-context t
34 "If enabled, offer Lisp functions only in appropriate contexts.
35 Functions are offered for completion only after ' and \(."
36 :group 'company
37 :type '(choice (const :tag "Off" nil)
38 (const :tag "On" t)))
39
40 (defun company-grab-lisp-symbol ()
41 (let ((prefix (company-grab-symbol)))
42 (if prefix
43 (unless (and (company-in-string-or-comment)
44 (/= (char-before (- (point) (length prefix))) ?`))
45 prefix)
46 'stop)))
47
48 (defun company-elisp-predicate (symbol)
49 (or (boundp symbol)
50 (fboundp symbol)
51 (facep symbol)
52 (featurep symbol)))
53
54 (defvar company-elisp-parse-limit 30)
55 (defvar company-elisp-parse-depth 100)
56
57 (defvar company-elisp-var-binding-regexp
58 (concat "\\_<" (regexp-opt '("let" "defun" "defmacro" "defsubst"
59 "lambda" "lexical-let"))
60 "\\*?\\_>")
61 "Regular expression matching head of a multiple variable bindings form.")
62
63 (defvar company-elisp-var-binding-regexp-1
64 (concat "\\_<\\(?:cl-\\)?" (regexp-opt '("dolist" "dotimes")) "\\_>")
65 "Regular expression matching head of a form with one variable binding.")
66
67 (defvar company-elisp-fun-binding-regexp
68 (concat "\\_<\\(?:cl-\\)?" (regexp-opt '("flet" "labels")) "\\_>")
69 "Regular expression matching head of a function bindings form.")
70
71 (defun company-elisp-locals (prefix functions-p)
72 (let ((regexp (concat "[ \t\n]*\\(\\_<" (regexp-quote prefix)
73 "\\(?:\\sw\\|\\s_\\)*\\_>\\)"))
74 (pos (point))
75 res)
76 (condition-case nil
77 (save-excursion
78 (dotimes (i company-elisp-parse-depth)
79 (up-list -1)
80 (save-excursion
81 (when (eq (char-after) ?\()
82 (forward-char 1)
83 (skip-chars-forward " \t\n")
84 (cond
85 ((looking-at (if functions-p
86 company-elisp-fun-binding-regexp
87 company-elisp-var-binding-regexp))
88 (down-list 1)
89 (condition-case nil
90 (dotimes (i company-elisp-parse-limit)
91 (save-excursion
92 (when (looking-at "[ \t\n]*(")
93 (down-list 1))
94 (and (looking-at regexp)
95 ;; Don't add incomplete text as candidate.
96 (not (eq (match-end 0) pos))
97 (pushnew (match-string-no-properties 1) res)))
98 (forward-sexp))
99 (scan-error nil)))
100 ((unless functions-p
101 (looking-at company-elisp-var-binding-regexp-1))
102 (down-list 1)
103 (and (looking-at regexp)
104 ;; Don't add incomplete text as candidate.
105 (not (eq (match-end 0) pos))
106 (pushnew (match-string-no-properties 1) res))))))))
107 (scan-error nil))
108 res))
109
110 (defun company-elisp-candidates (prefix)
111 (let ((predicate (company-elisp-candidates-predicate prefix)))
112 (append (company-elisp-locals prefix (eq predicate 'fboundp))
113 (company-elisp-globals prefix predicate))))
114
115 (defun company-elisp-globals (prefix predicate)
116 (all-completions prefix obarray predicate))
117
118 (defun company-elisp-candidates-predicate (prefix)
119 (let* ((completion-ignore-case nil)
120 (before (char-before (- (point) (length prefix)))))
121 (if (and company-elisp-detect-function-context
122 (not (eq before ?')))
123 (if (and (eq before ?\()
124 (not
125 (save-excursion
126 (ignore-errors
127 (up-list -2)
128 (forward-char 1)
129 (looking-at " *(")))))
130 'fboundp
131 'boundp)
132 'company-elisp-predicate)))
133
134 (defun company-elisp-doc (symbol)
135 (let* ((symbol (intern symbol))
136 (doc (if (fboundp symbol)
137 (documentation symbol t)
138 (documentation-property symbol 'variable-documentation t))))
139 (and (stringp doc)
140 (string-match ".*$" doc)
141 (match-string 0 doc))))
142
143 ;;;###autoload
144 (defun company-elisp (command &optional arg &rest ignored)
145 "A `company-mode' completion back-end for `emacs-lisp-mode'."
146 (interactive (list 'interactive))
147 (case command
148 (interactive (company-begin-backend 'company-elisp))
149 (prefix (and (eq (derived-mode-p 'emacs-lisp-mode) 'emacs-lisp-mode)
150 (company-grab-lisp-symbol)))
151 (candidates (company-elisp-candidates arg))
152 (meta (company-elisp-doc arg))
153 (doc-buffer (let ((symbol (intern arg)))
154 (save-window-excursion
155 (ignore-errors
156 (cond
157 ((fboundp symbol) (describe-function symbol))
158 ((boundp symbol) (describe-variable symbol))
159 ((featurep symbol) (describe-package symbol))
160 ((facep symbol) (describe-face symbol))
161 (t (signal 'user-error nil)))
162 (help-buffer)))))
163 (location (let ((sym (intern arg)))
164 (cond
165 ((fboundp sym) (find-definition-noselect sym nil))
166 ((boundp sym) (find-definition-noselect sym 'defvar))
167 ((featurep sym) (cons (find-file-noselect (find-library-name
168 (symbol-name sym)))
169 0))
170 ((facep sym) (find-definition-noselect sym 'defface)))))))
171
172 (provide 'company-elisp)
173 ;;; company-elisp.el ends here