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 0.2.
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 (defcustom company-elisp-detect-function-context t
24 "*If enabled, offer lisp functions only in appropriate contexts.
25 Functions are offered for completion only after ' and \(."
27 :type '(choice (const :tag "Off" nil)
30 (defvar company-lisp-symbol-regexp
31 "\\_<\\(\\sw\\|\\s_\\)+\\_>\\=")
33 (defun company-grab-lisp-symbol ()
34 (let ((prefix (or (company-grab company-lisp-symbol-regexp) "")))
35 (unless (and (company-in-string-or-comment (- (point) (length prefix)))
36 (/= (char-before (- (point) (length prefix))) ?`))
39 (defun company-elisp-predicate (symbol)
43 (defvar company-elisp-parse-limit 30)
44 (defvar company-elisp-parse-depth 100)
46 (defvar company-elisp-binding-regexp
47 (concat "([ \t\n]*\\_<" (regexp-opt '("let" "defun" "defmacro" "defsubst"
48 "lambda" "lexical-let"))
51 (defun company-elisp-parse-local (prefix vars)
52 (let ((regexp (concat "[ \t\n]*\\(\\_<" (regexp-quote prefix)
53 "\\(?:\\sw\\|\\s_\\)*\\_>\\)")))
56 (dotimes (i company-elisp-parse-depth)
59 (when (looking-at company-elisp-binding-regexp)
62 (dotimes (i company-elisp-parse-limit)
64 (when (looking-at "[ \t\n]*(")
66 (when (looking-at regexp)
67 (add-to-list 'vars (match-string-no-properties 1))))
71 (defun company-elisp-candidates (prefix)
72 (let* ((completion-ignore-case nil)
73 (before (char-before (- (point) (length prefix))))
74 (predicate (if (and company-elisp-detect-function-context
79 'company-elisp-predicate))
80 (candidates (all-completions prefix obarray predicate)))
81 (company-elisp-parse-local prefix candidates)))
83 (defun company-elisp-doc (symbol)
84 (let* ((symbol (intern symbol))
85 (doc (if (fboundp symbol)
86 (documentation symbol t)
87 (documentation-property symbol 'variable-documentation t))))
89 (string-match ".*$" doc)
90 (match-string 0 doc))))
92 (defun company-elisp (command &optional arg &rest ignored)
93 "A `company-mode' completion back-end for `emacs-lisp-mode'."
95 ('prefix (and (eq (derived-mode-p 'emacs-lisp-mode) 'emacs-lisp-mode)
96 (company-grab-lisp-symbol)))
97 ('candidates (company-elisp-candidates arg))
98 ('meta (company-elisp-doc arg))
99 ('doc-buffer (let ((symbol (intern arg)))
100 (save-window-excursion
101 (when (or (ignore-errors (describe-function symbol))
102 (ignore-errors (describe-variable symbol)))
104 ('location (let ((sym (intern arg)))
105 (or (ignore-errors (find-definition-noselect sym nil))
106 (ignore-errors (find-definition-noselect sym 'defvar))
107 (ignore-errors (find-definition-noselect sym t)))))))
109 (provide 'company-elisp)
110 ;;; company-elisp.el ends here