1 ;;; company-elisp.el --- A company-mode completion back-end for emacs-lisp-mode
3 ;; Copyright (C) 2009, 2011-2012 Free Software Foundation, Inc.
5 ;; Author: Nikolaj Schumacher
7 ;; This file is part of GNU Emacs.
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.
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.
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/>.
29 (eval-when-compile (require 'cl))
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 \(."
37 :type '(choice (const :tag "Off" nil)
40 (defun company-grab-lisp-symbol ()
41 (let ((prefix (company-grab-symbol)))
43 (unless (and (company-in-string-or-comment)
44 (/= (char-before (- (point) (length prefix))) ?`))
48 (defun company-elisp-predicate (symbol)
54 (defvar company-elisp-parse-limit 30)
55 (defvar company-elisp-parse-depth 100)
57 (defvar company-elisp-var-binding-regexp
58 (concat "\\_<" (regexp-opt '("let" "defun" "defmacro" "defsubst"
59 "lambda" "lexical-let"))
61 "Regular expression matching head of a multiple variable bindings form.")
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.")
67 (defvar company-elisp-fun-binding-regexp
68 (concat "\\_<\\(?:cl-\\)?" (regexp-opt '("flet" "labels")) "\\_>")
69 "Regular expression matching head of a function bindings form.")
71 (defun company-elisp-locals (prefix functions-p)
72 (let ((regexp (concat "[ \t\n]*\\(\\_<" (regexp-quote prefix)
73 "\\(?:\\sw\\|\\s_\\)*\\_>\\)"))
78 (dotimes (i company-elisp-parse-depth)
81 (when (eq (char-after) ?\()
83 (skip-chars-forward " \t\n")
85 ((looking-at (if functions-p
86 company-elisp-fun-binding-regexp
87 company-elisp-var-binding-regexp))
90 (dotimes (i company-elisp-parse-limit)
92 (when (looking-at "[ \t\n]*(")
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)))
101 (looking-at company-elisp-var-binding-regexp-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))))))))
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))))
115 (defun company-elisp-globals (prefix predicate)
116 (all-completions prefix obarray predicate))
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 ?\()
129 (looking-at " *(")))))
132 'company-elisp-predicate)))
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))))
140 (string-match ".*$" doc)
141 (match-string 0 doc))))
144 (defun company-elisp (command &optional arg &rest ignored)
145 "A `company-mode' completion back-end for `emacs-lisp-mode'."
146 (interactive (list 'interactive))
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
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)))
163 (location (let ((sym (intern arg)))
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
170 ((facep sym) (find-definition-noselect sym 'defface)))))))
172 (provide 'company-elisp)
173 ;;; company-elisp.el ends here