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.1.
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))
24 (defcustom company-elisp-detect-function-context t
25 "*If enabled, offer lisp functions only in appropriate contexts.
26 Functions are offered for completion only after ' and \(."
28 :type '(choice (const :tag "Off" nil)
31 (defvar company-lisp-symbol-regexp
32 "\\_<\\(\\sw\\|\\s_\\)+\\_>\\=")
34 (defun company-grab-lisp-symbol ()
35 (let ((prefix (or (company-grab company-lisp-symbol-regexp) "")))
36 (unless (and (company-in-string-or-comment (- (point) (length prefix)))
37 (/= (char-before (- (point) (length prefix))) ?`))
40 (defun company-elisp-predicate (symbol)
44 (defvar company-elisp-parse-limit 30)
45 (defvar company-elisp-parse-depth 100)
47 (defvar company-elisp-binding-regexp
48 (concat "([ \t\n]*\\_<" (regexp-opt '("let" "defun" "defmacro" "defsubst"
49 "lambda" "lexical-let"))
51 "Regular expression matching sexps containing variable bindings.")
53 (defvar company-elisp-binding-regexp-1
54 (concat "([ \t\n]*\\_<" (regexp-opt '("dolist" "dotimes")))
55 "Regular expression matching sexps containing one variable binding.")
57 (defun company-elisp-parse-local (prefix vars)
58 (let ((regexp (concat "[ \t\n]*\\(\\_<" (regexp-quote prefix)
59 "\\(?:\\sw\\|\\s_\\)*\\_>\\)"))
63 (dotimes (i company-elisp-parse-depth)
67 ((looking-at company-elisp-binding-regexp)
70 (dotimes (i company-elisp-parse-limit)
72 (when (looking-at "[ \t\n]*(")
74 (and (looking-at regexp)
75 ;; Don't add incomplete text as candidate.
76 (not (eq (match-end 0) pos))
77 (add-to-list 'vars (match-string-no-properties 1))))
79 ((looking-at company-elisp-binding-regexp-1)
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)))))))))
87 (defun company-elisp-candidates (prefix)
88 (let* ((completion-ignore-case nil)
89 (before (char-before (- (point) (length prefix))))
90 (predicate (if (and company-elisp-detect-function-context
95 'company-elisp-predicate))
96 (candidates (all-completions prefix obarray predicate)))
97 (company-elisp-parse-local prefix candidates)))
99 (defun company-elisp-doc (symbol)
100 (let* ((symbol (intern symbol))
101 (doc (if (fboundp symbol)
102 (documentation symbol t)
103 (documentation-property symbol 'variable-documentation t))))
105 (string-match ".*$" doc)
106 (match-string 0 doc))))
109 (defun company-elisp (command &optional arg &rest ignored)
110 "A `company-mode' completion back-end for `emacs-lisp-mode'."
111 (interactive (list 'interactive))
113 ('interactive (company-begin-backend 'company-elisp))
114 ('prefix (and (eq (derived-mode-p 'emacs-lisp-mode) 'emacs-lisp-mode)
115 (company-grab-lisp-symbol)))
116 ('candidates (company-elisp-candidates arg))
117 ('meta (company-elisp-doc arg))
118 ('doc-buffer (let ((symbol (intern arg)))
119 (save-window-excursion
120 (when (or (ignore-errors (describe-function symbol))
121 (ignore-errors (describe-variable symbol)))
123 ('location (let ((sym (intern arg)))
124 (or (ignore-errors (find-definition-noselect sym nil))
125 (ignore-errors (find-definition-noselect sym 'defvar))
126 (ignore-errors (find-definition-noselect sym t)))))))
128 (provide 'company-elisp)
129 ;;; company-elisp.el ends here