]> code.delx.au - gnu-emacs-elpa/blob - company-elisp.el
company-elisp-locals: Skip current varlist
[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 (when (ignore-errors
84 (save-excursion (forward-list)
85 (<= (point) pos)))
86 (skip-chars-forward " \t\n")
87 (cond
88 ((looking-at (if functions-p
89 company-elisp-fun-binding-regexp
90 company-elisp-var-binding-regexp))
91 (down-list 1)
92 (condition-case nil
93 (dotimes (i company-elisp-parse-limit)
94 (save-excursion
95 (when (looking-at "[ \t\n]*(")
96 (down-list 1))
97 (when (looking-at regexp)
98 (pushnew (match-string-no-properties 1) res)))
99 (forward-sexp))
100 (scan-error nil)))
101 ((unless functions-p
102 (looking-at company-elisp-var-binding-regexp-1))
103 (down-list 1)
104 (when (looking-at regexp)
105 (pushnew (match-string-no-properties 1) res)))))))))
106 (scan-error nil))
107 res))
108
109 (defun company-elisp-candidates (prefix)
110 (let ((predicate (company-elisp-candidates-predicate prefix)))
111 (append (company-elisp-locals prefix (eq predicate 'fboundp))
112 (company-elisp-globals prefix predicate))))
113
114 (defun company-elisp-globals (prefix predicate)
115 (all-completions prefix obarray predicate))
116
117 (defun company-elisp-candidates-predicate (prefix)
118 (let* ((completion-ignore-case nil)
119 (before (char-before (- (point) (length prefix)))))
120 (if (and company-elisp-detect-function-context
121 (not (eq before ?')))
122 (if (and (eq before ?\()
123 (not
124 (save-excursion
125 (ignore-errors
126 (up-list -2)
127 (forward-char 1)
128 (looking-at " *(")))))
129 'fboundp
130 'boundp)
131 'company-elisp-predicate)))
132
133 (defun company-elisp-doc (symbol)
134 (let* ((symbol (intern symbol))
135 (doc (if (fboundp symbol)
136 (documentation symbol t)
137 (documentation-property symbol 'variable-documentation t))))
138 (and (stringp doc)
139 (string-match ".*$" doc)
140 (match-string 0 doc))))
141
142 ;;;###autoload
143 (defun company-elisp (command &optional arg &rest ignored)
144 "A `company-mode' completion back-end for `emacs-lisp-mode'."
145 (interactive (list 'interactive))
146 (case command
147 (interactive (company-begin-backend 'company-elisp))
148 (prefix (and (eq (derived-mode-p 'emacs-lisp-mode) 'emacs-lisp-mode)
149 (company-grab-lisp-symbol)))
150 (candidates (company-elisp-candidates arg))
151 (meta (company-elisp-doc arg))
152 (doc-buffer (let ((symbol (intern arg)))
153 (save-window-excursion
154 (ignore-errors
155 (cond
156 ((fboundp symbol) (describe-function symbol))
157 ((boundp symbol) (describe-variable symbol))
158 ((featurep symbol) (describe-package symbol))
159 ((facep symbol) (describe-face symbol))
160 (t (signal 'user-error nil)))
161 (help-buffer)))))
162 (location (let ((sym (intern arg)))
163 (cond
164 ((fboundp sym) (find-definition-noselect sym nil))
165 ((boundp sym) (find-definition-noselect sym 'defvar))
166 ((featurep sym) (cons (find-file-noselect (find-library-name
167 (symbol-name sym)))
168 0))
169 ((facep sym) (find-definition-noselect sym 'defface)))))))
170
171 (provide 'company-elisp)
172 ;;; company-elisp.el ends here