1 ;;; company-elisp.el --- a company-mode completion back-end for emacs-lisp-mode
3 ;; Copyright (C) 2009 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/>.
25 (eval-when-compile (require 'cl))
28 (defcustom company-elisp-detect-function-context t
29 "*If enabled, offer lisp functions only in appropriate contexts.
30 Functions are offered for completion only after ' and \(."
32 :type '(choice (const :tag "Off" nil)
35 (defun company-grab-lisp-symbol ()
36 (let ((prefix (company-grab-symbol)))
38 (unless (and (company-in-string-or-comment)
39 (/= (char-before (- (point) (length prefix))) ?`))
43 (defun company-elisp-predicate (symbol)
47 (defvar company-elisp-parse-limit 30)
48 (defvar company-elisp-parse-depth 100)
50 (defvar company-elisp-binding-regexp
51 (concat "([ \t\n]*\\_<" (regexp-opt '("let" "defun" "defmacro" "defsubst"
52 "lambda" "lexical-let" "flet" "labels"))
54 "Regular expression matching sexps containing variable bindings.")
56 (defvar company-elisp-binding-regexp-1
57 (concat "([ \t\n]*\\_<" (regexp-opt '("dolist" "dotimes")))
58 "Regular expression matching sexps containing one variable binding.")
60 (defun company-elisp-parse-local (prefix vars)
61 (let ((regexp (concat "[ \t\n]*\\(\\_<" (regexp-quote prefix)
62 "\\(?:\\sw\\|\\s_\\)*\\_>\\)"))
66 (dotimes (i company-elisp-parse-depth)
70 ((looking-at company-elisp-binding-regexp)
73 (dotimes (i company-elisp-parse-limit)
75 (when (looking-at "[ \t\n]*(")
77 (and (looking-at regexp)
78 ;; Don't add incomplete text as candidate.
79 (not (eq (match-end 0) pos))
80 (add-to-list 'vars (match-string-no-properties 1))))
82 ((looking-at company-elisp-binding-regexp-1)
84 (and (looking-at regexp)
85 ;; Don't add incomplete text as candidate.
86 (not (eq (match-end 0) pos))
87 (add-to-list 'vars (match-string-no-properties 1)))))))))
90 (defun company-elisp-candidates (prefix)
91 (let* ((completion-ignore-case nil)
92 (before (char-before (- (point) (length prefix))))
93 (predicate (if (and company-elisp-detect-function-context
98 'company-elisp-predicate))
99 (candidates (all-completions prefix obarray predicate)))
100 (company-elisp-parse-local prefix candidates)))
102 (defun company-elisp-doc (symbol)
103 (let* ((symbol (intern symbol))
104 (doc (if (fboundp symbol)
105 (documentation symbol t)
106 (documentation-property symbol 'variable-documentation t))))
108 (string-match ".*$" doc)
109 (match-string 0 doc))))
112 (defun company-elisp (command &optional arg &rest ignored)
113 "A `company-mode' completion back-end for `emacs-lisp-mode'."
114 (interactive (list 'interactive))
116 ('interactive (company-begin-backend 'company-elisp))
117 ('prefix (and (eq (derived-mode-p 'emacs-lisp-mode) 'emacs-lisp-mode)
118 (company-grab-lisp-symbol)))
119 ('candidates (company-elisp-candidates arg))
120 ('meta (company-elisp-doc arg))
121 ('doc-buffer (let ((symbol (intern arg)))
122 (save-window-excursion
123 (when (or (ignore-errors (describe-function symbol))
124 (ignore-errors (describe-variable symbol)))
126 ('location (let ((sym (intern arg)))
127 (or (ignore-errors (find-definition-noselect sym nil))
128 (ignore-errors (find-definition-noselect sym 'defvar))
129 (ignore-errors (find-definition-noselect sym t)))))))
131 (provide 'company-elisp)
132 ;;; company-elisp.el ends here