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))
32 (defcustom company-elisp-detect-function-context t
33 "If enabled, offer Lisp functions only in appropriate contexts.
34 Functions are offered for completion only after ' and \(."
36 :type '(choice (const :tag "Off" nil)
39 (defun company-grab-lisp-symbol ()
40 (let ((prefix (company-grab-symbol)))
42 (unless (and (company-in-string-or-comment)
43 (/= (char-before (- (point) (length prefix))) ?`))
47 (defun company-elisp-predicate (symbol)
53 (defvar company-elisp-parse-limit 30)
54 (defvar company-elisp-parse-depth 100)
56 (defvar company-elisp-binding-regexp
57 (concat "([ \t\n]*\\_<" (regexp-opt '("let" "defun" "defmacro" "defsubst"
58 "lambda" "lexical-let" "flet" "labels"))
60 "Regular expression matching sexps containing variable bindings.")
62 (defvar company-elisp-binding-regexp-1
63 (concat "([ \t\n]*\\_<" (regexp-opt '("dolist" "dotimes")))
64 "Regular expression matching sexps containing one variable binding.")
66 (defun company-elisp-parse-local (prefix vars)
67 (let ((regexp (concat "[ \t\n]*\\(\\_<" (regexp-quote prefix)
68 "\\(?:\\sw\\|\\s_\\)*\\_>\\)"))
72 (dotimes (i company-elisp-parse-depth)
76 ((looking-at company-elisp-binding-regexp)
79 (dotimes (i company-elisp-parse-limit)
81 (when (looking-at "[ \t\n]*(")
83 (and (looking-at regexp)
84 ;; Don't add incomplete text as candidate.
85 (not (eq (match-end 0) pos))
86 (add-to-list 'vars (match-string-no-properties 1))))
88 ((looking-at company-elisp-binding-regexp-1)
90 (and (looking-at regexp)
91 ;; Don't add incomplete text as candidate.
92 (not (eq (match-end 0) pos))
93 (add-to-list 'vars (match-string-no-properties 1)))))))))
96 (defun company-elisp-candidates (prefix)
97 (let* ((completion-ignore-case nil)
98 (before (char-before (- (point) (length prefix))))
99 (predicate (if (and company-elisp-detect-function-context
100 (not (eq before ?')))
104 'company-elisp-predicate))
105 (candidates (all-completions prefix obarray predicate)))
106 (company-elisp-parse-local prefix candidates)))
108 (defun company-elisp-doc (symbol)
109 (let* ((symbol (intern symbol))
110 (doc (if (fboundp symbol)
111 (documentation symbol t)
112 (documentation-property symbol 'variable-documentation t))))
114 (string-match ".*$" doc)
115 (match-string 0 doc))))
118 (defun company-elisp (command &optional arg &rest ignored)
119 "A `company-mode' completion back-end for `emacs-lisp-mode'."
120 (interactive (list 'interactive))
122 (interactive (company-begin-backend 'company-elisp))
123 (prefix (and (eq (derived-mode-p 'emacs-lisp-mode) 'emacs-lisp-mode)
124 (company-grab-lisp-symbol)))
125 (candidates (company-elisp-candidates arg))
126 (meta (company-elisp-doc arg))
127 (doc-buffer (let ((symbol (intern arg)))
128 (save-window-excursion
131 ((fboundp symbol) (describe-function symbol))
132 ((boundp symbol) (describe-variable symbol))
133 ((featurep symbol) (describe-package symbol))
134 ((facep symbol) (describe-face symbol))
135 (t (signal 'user-error nil)))
137 (location (let ((sym (intern arg)))
139 ((fboundp sym) (find-definition-noselect sym nil))
140 ((boundp sym) (find-definition-noselect sym 'defvar))
141 ((featurep sym) (cons (find-file-noselect (find-library-name
144 ((facep sym) (find-definition-noselect sym 'defface)))))))
146 (provide 'company-elisp)
147 ;;; company-elisp.el ends here