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.5.
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 (defun company-grab-lisp-symbol ()
32 (let ((prefix (company-grab-symbol)))
34 (unless (and (company-in-string-or-comment)
35 (/= (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" "flet" "labels"))
50 "Regular expression matching sexps containing variable bindings.")
52 (defvar company-elisp-binding-regexp-1
53 (concat "([ \t\n]*\\_<" (regexp-opt '("dolist" "dotimes")))
54 "Regular expression matching sexps containing one variable binding.")
56 (defun company-elisp-parse-local (prefix vars)
57 (let ((regexp (concat "[ \t\n]*\\(\\_<" (regexp-quote prefix)
58 "\\(?:\\sw\\|\\s_\\)*\\_>\\)"))
62 (dotimes (i company-elisp-parse-depth)
66 ((looking-at company-elisp-binding-regexp)
69 (dotimes (i company-elisp-parse-limit)
71 (when (looking-at "[ \t\n]*(")
73 (and (looking-at regexp)
74 ;; Don't add incomplete text as candidate.
75 (not (eq (match-end 0) pos))
76 (add-to-list 'vars (match-string-no-properties 1))))
78 ((looking-at company-elisp-binding-regexp-1)
80 (and (looking-at regexp)
81 ;; Don't add incomplete text as candidate.
82 (not (eq (match-end 0) pos))
83 (add-to-list 'vars (match-string-no-properties 1)))))))))
86 (defun company-elisp-candidates (prefix)
87 (let* ((completion-ignore-case nil)
88 (before (char-before (- (point) (length prefix))))
89 (predicate (if (and company-elisp-detect-function-context
94 'company-elisp-predicate))
95 (candidates (all-completions prefix obarray predicate)))
96 (company-elisp-parse-local prefix candidates)))
98 (defun company-elisp-doc (symbol)
99 (let* ((symbol (intern symbol))
100 (doc (if (fboundp symbol)
101 (documentation symbol t)
102 (documentation-property symbol 'variable-documentation t))))
104 (string-match ".*$" doc)
105 (match-string 0 doc))))
108 (defun company-elisp (command &optional arg &rest ignored)
109 "A `company-mode' completion back-end for `emacs-lisp-mode'."
110 (interactive (list 'interactive))
112 ('interactive (company-begin-backend 'company-elisp))
113 ('prefix (and (eq (derived-mode-p 'emacs-lisp-mode) 'emacs-lisp-mode)
114 (company-grab-lisp-symbol)))
115 ('candidates (company-elisp-candidates arg))
116 ('meta (company-elisp-doc arg))
117 ('doc-buffer (let ((symbol (intern arg)))
118 (save-window-excursion
119 (when (or (ignore-errors (describe-function symbol))
120 (ignore-errors (describe-variable symbol)))
122 ('location (let ((sym (intern arg)))
123 (or (ignore-errors (find-definition-noselect sym nil))
124 (ignore-errors (find-definition-noselect sym 'defvar))
125 (ignore-errors (find-definition-noselect sym t)))))))
127 (provide 'company-elisp)
128 ;;; company-elisp.el ends here