1 ;;; company-elisp.el --- A company-mode completion back-end for emacs-lisp-mode
3 ;; Copyright (C) 2009, 2011 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)
51 (defvar company-elisp-parse-limit 30)
52 (defvar company-elisp-parse-depth 100)
54 (defvar company-elisp-binding-regexp
55 (concat "([ \t\n]*\\_<" (regexp-opt '("let" "defun" "defmacro" "defsubst"
56 "lambda" "lexical-let" "flet" "labels"))
58 "Regular expression matching sexps containing variable bindings.")
60 (defvar company-elisp-binding-regexp-1
61 (concat "([ \t\n]*\\_<" (regexp-opt '("dolist" "dotimes")))
62 "Regular expression matching sexps containing one variable binding.")
64 (defun company-elisp-parse-local (prefix vars)
65 (let ((regexp (concat "[ \t\n]*\\(\\_<" (regexp-quote prefix)
66 "\\(?:\\sw\\|\\s_\\)*\\_>\\)"))
70 (dotimes (i company-elisp-parse-depth)
74 ((looking-at company-elisp-binding-regexp)
77 (dotimes (i company-elisp-parse-limit)
79 (when (looking-at "[ \t\n]*(")
81 (and (looking-at regexp)
82 ;; Don't add incomplete text as candidate.
83 (not (eq (match-end 0) pos))
84 (add-to-list 'vars (match-string-no-properties 1))))
86 ((looking-at company-elisp-binding-regexp-1)
88 (and (looking-at regexp)
89 ;; Don't add incomplete text as candidate.
90 (not (eq (match-end 0) pos))
91 (add-to-list 'vars (match-string-no-properties 1)))))))))
94 (defun company-elisp-candidates (prefix)
95 (let* ((completion-ignore-case nil)
96 (before (char-before (- (point) (length prefix))))
97 (predicate (if (and company-elisp-detect-function-context
102 'company-elisp-predicate))
103 (candidates (all-completions prefix obarray predicate)))
104 (company-elisp-parse-local prefix candidates)))
106 (defun company-elisp-doc (symbol)
107 (let* ((symbol (intern symbol))
108 (doc (if (fboundp symbol)
109 (documentation symbol t)
110 (documentation-property symbol 'variable-documentation t))))
112 (string-match ".*$" doc)
113 (match-string 0 doc))))
116 (defun company-elisp (command &optional arg &rest ignored)
117 "A `company-mode' completion back-end for `emacs-lisp-mode'."
118 (interactive (list 'interactive))
120 (interactive (company-begin-backend 'company-elisp))
121 (prefix (and (eq (derived-mode-p 'emacs-lisp-mode) 'emacs-lisp-mode)
122 (company-grab-lisp-symbol)))
123 (candidates (company-elisp-candidates arg))
124 (meta (company-elisp-doc arg))
125 (doc-buffer (let ((symbol (intern arg)))
126 (save-window-excursion
127 (when (or (ignore-errors (describe-function symbol))
128 (ignore-errors (describe-variable symbol)))
130 (location (let ((sym (intern arg)))
131 (or (ignore-errors (find-definition-noselect sym nil))
132 (ignore-errors (find-definition-noselect sym 'defvar))
133 (ignore-errors (find-definition-noselect sym t)))))))
135 (provide 'company-elisp)
136 ;;; company-elisp.el ends here