]> code.delx.au - gnu-emacs-elpa/blob - company-elisp.el
Added interactive form for back-ends.
[gnu-emacs-elpa] / company-elisp.el
1 ;;; company-elisp.el --- a company-mode completion back-end for emacs-lisp-mode
2 ;;
3 ;; Copyright (C) 2009 Nikolaj Schumacher
4 ;;
5 ;; This file is part of company 0.2.1.
6 ;;
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.
11 ;;
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.
16 ;;
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/>.
19
20 (require 'company)
21 (eval-when-compile (require 'cl))
22
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 \(."
26 :group 'company
27 :type '(choice (const :tag "Off" nil)
28 (const :tag "On" t)))
29
30 (defvar company-lisp-symbol-regexp
31 "\\_<\\(\\sw\\|\\s_\\)+\\_>\\=")
32
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))) ?`))
37 prefix)))
38
39 (defun company-elisp-predicate (symbol)
40 (or (boundp symbol)
41 (fboundp symbol)))
42
43 (defvar company-elisp-parse-limit 30)
44 (defvar company-elisp-parse-depth 100)
45
46 (defvar company-elisp-binding-regexp
47 (concat "([ \t\n]*\\_<" (regexp-opt '("let" "defun" "defmacro" "defsubst"
48 "lambda" "lexical-let"))
49 "\\*?")
50 "Regular expression matching sexps containing variable bindings.")
51
52 (defvar company-elisp-binding-regexp-1
53 (concat "([ \t\n]*\\_<" (regexp-opt '("dolist" "dotimes")))
54 "Regular expression matching sexps containing one variable binding.")
55
56 (defun company-elisp-parse-local (prefix vars)
57 (let ((regexp (concat "[ \t\n]*\\(\\_<" (regexp-quote prefix)
58 "\\(?:\\sw\\|\\s_\\)*\\_>\\)"))
59 (pos (point)))
60 (ignore-errors
61 (save-excursion
62 (dotimes (i company-elisp-parse-depth)
63 (up-list -1)
64 (save-excursion
65 (cond
66 ((looking-at company-elisp-binding-regexp)
67 (down-list 2)
68 (ignore-errors
69 (dotimes (i company-elisp-parse-limit)
70 (save-excursion
71 (when (looking-at "[ \t\n]*(")
72 (down-list 1))
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))))
77 (forward-sexp))))
78 ((looking-at company-elisp-binding-regexp-1)
79 (down-list 2)
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)))))))))
84 vars))
85
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
90 (not (eq before ?')))
91 (if (eq before ?\()
92 'fboundp
93 'boundp)
94 'company-elisp-predicate))
95 (candidates (all-completions prefix obarray predicate)))
96 (company-elisp-parse-local prefix candidates)))
97
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))))
103 (and (stringp doc)
104 (string-match ".*$" doc)
105 (match-string 0 doc))))
106
107 ;;;###autoload
108 (defun company-elisp (command &optional arg &rest ignored)
109 "A `company-mode' completion back-end for `emacs-lisp-mode'."
110 (interactive (list 'interactive))
111 (case command
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)))
121 (help-buffer)))))
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)))))))
126
127 (provide 'company-elisp)
128 ;;; company-elisp.el ends here