]> code.delx.au - gnu-emacs-elpa/blob - company-elisp.el
company-elisp-candidates: Recognize when inside 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-binding-regexp
58 (concat "([ \t\n]*\\_<" (regexp-opt '("let" "defun" "defmacro" "defsubst"
59 "lambda" "lexical-let" "flet" "labels"))
60 "\\*?")
61 "Regular expression matching sexps containing variable bindings.")
62
63 (defvar company-elisp-binding-regexp-1
64 (concat "([ \t\n]*\\_<" (regexp-opt '("dolist" "dotimes")))
65 "Regular expression matching sexps containing one variable binding.")
66
67 (defun company-elisp-locals (prefix)
68 (let ((regexp (concat "[ \t\n]*\\(\\_<" (regexp-quote prefix)
69 "\\(?:\\sw\\|\\s_\\)*\\_>\\)"))
70 (pos (point))
71 res)
72 (ignore-errors
73 (save-excursion
74 (dotimes (i company-elisp-parse-depth)
75 (up-list -1)
76 (save-excursion
77 (cond
78 ((looking-at company-elisp-binding-regexp)
79 (down-list 2)
80 (ignore-errors
81 (dotimes (i company-elisp-parse-limit)
82 (save-excursion
83 (when (looking-at "[ \t\n]*(")
84 (down-list 1))
85 (and (looking-at regexp)
86 ;; Don't add incomplete text as candidate.
87 (not (eq (match-end 0) pos))
88 (push (match-string-no-properties 1) res)))
89 (forward-sexp))))
90 ((looking-at company-elisp-binding-regexp-1)
91 (down-list 2)
92 (and (looking-at regexp)
93 ;; Don't add incomplete text as candidate.
94 (not (eq (match-end 0) pos))
95 (pushnew (match-string-no-properties 1) res))))))))
96 res))
97
98 (defun company-elisp-candidates (prefix)
99 (append (company-elisp-locals prefix)
100 (company-elisp-globals prefix
101 (company-elisp-candidates-predicate prefix))))
102
103 (defun company-elisp-globals (prefix predicate)
104 (all-completions prefix obarray predicate))
105
106 (defun company-elisp-candidates-predicate (prefix)
107 (let* ((completion-ignore-case nil)
108 (before (char-before (- (point) (length prefix)))))
109 (if (and company-elisp-detect-function-context
110 (not (eq before ?')))
111 (if (and (eq before ?\()
112 (not
113 (save-excursion
114 (ignore-errors
115 (up-list -2)
116 (forward-char 1)
117 (looking-at " *(")))))
118 'fboundp
119 'boundp)
120 'company-elisp-predicate)))
121
122 (defun company-elisp-doc (symbol)
123 (let* ((symbol (intern symbol))
124 (doc (if (fboundp symbol)
125 (documentation symbol t)
126 (documentation-property symbol 'variable-documentation t))))
127 (and (stringp doc)
128 (string-match ".*$" doc)
129 (match-string 0 doc))))
130
131 ;;;###autoload
132 (defun company-elisp (command &optional arg &rest ignored)
133 "A `company-mode' completion back-end for `emacs-lisp-mode'."
134 (interactive (list 'interactive))
135 (case command
136 (interactive (company-begin-backend 'company-elisp))
137 (prefix (and (eq (derived-mode-p 'emacs-lisp-mode) 'emacs-lisp-mode)
138 (company-grab-lisp-symbol)))
139 (candidates (company-elisp-candidates arg))
140 (meta (company-elisp-doc arg))
141 (doc-buffer (let ((symbol (intern arg)))
142 (save-window-excursion
143 (ignore-errors
144 (cond
145 ((fboundp symbol) (describe-function symbol))
146 ((boundp symbol) (describe-variable symbol))
147 ((featurep symbol) (describe-package symbol))
148 ((facep symbol) (describe-face symbol))
149 (t (signal 'user-error nil)))
150 (help-buffer)))))
151 (location (let ((sym (intern arg)))
152 (cond
153 ((fboundp sym) (find-definition-noselect sym nil))
154 ((boundp sym) (find-definition-noselect sym 'defvar))
155 ((featurep sym) (cons (find-file-noselect (find-library-name
156 (symbol-name sym)))
157 0))
158 ((facep sym) (find-definition-noselect sym 'defface)))))))
159
160 (provide 'company-elisp)
161 ;;; company-elisp.el ends here