1 ;;; company-elisp.el --- a company-mode completion back-end for emacs-lisp-mode
3 ;; Copyright (C) 2009 Nikolaj Schumacher
5 ;; This file is part of company.
7 ;; This program is free software; you can redistribute it and/or
8 ;; modify it under the terms of the GNU General Public License
9 ;; as published by the Free Software Foundation; either version 2
10 ;; of the License, or (at your option) any later version.
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21 (eval-when-compile (require 'cl))
23 (defvar company-lisp-symbol-regexp
24 "\\_<\\(\\sw\\|\\s_\\)+\\_>\\=")
26 (defun company-grab-lisp-symbol ()
27 (let ((prefix (or (company-grab company-lisp-symbol-regexp) "")))
28 (unless (and (company-in-string-or-comment (- (point) (length prefix)))
29 (/= (char-before (- (point) (length prefix))) ?`))
32 (defun company-elisp-predicate (symbol)
36 (defvar company-elisp-parse-limit 30)
37 (defvar company-elisp-parse-depth 100)
39 (defun company-elisp-parse-let ()
43 (dotimes (i company-elisp-parse-depth)
46 (when (looking-at "([ \t\n]*let")
49 (dotimes (i company-elisp-parse-limit)
51 (when (looking-at "[ \t\n]*(")
53 (if (looking-at "[ \t\n]*\\(\\(?:\\sw\\|\\s_\\)+\\)")
54 (add-to-list 'vars (match-string-no-properties 1))
59 (defun company-elisp-doc (symbol)
60 (let* ((symbol (intern symbol))
61 (doc (if (fboundp symbol)
62 (documentation symbol t)
63 (documentation-property symbol 'variable-documentation t))))
65 (string-match ".*$" doc)
66 (match-string 0 doc))))
68 (defun company-elisp (command &optional arg &rest ignored)
69 "A `company-mode' completion back-end for `emacs-lisp-mode'."
71 ('prefix (and (eq (derived-mode-p 'emacs-lisp-mode) 'emacs-lisp-mode)
72 (company-grab-lisp-symbol)))
73 ('candidates (let ((completion-ignore-case nil))
74 (append (all-completions arg (company-elisp-parse-let))
75 (all-completions arg obarray
76 'company-elisp-predicate))))
77 ('meta (company-elisp-doc arg))
78 ('doc-buffer (let ((symbol (intern arg)))
79 (when (or (ignore-errors (describe-function symbol))
80 (ignore-errors (describe-variable symbol)))
83 (provide 'company-elisp)
84 ;;; company-elisp.el ends here