]> code.delx.au - gnu-emacs-elpa/blob - company-elisp.el
Added company-elisp-functions-only-in-context option.
[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.
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-functions-only-in-context nil
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)" nil)
28 (integer :tag "count" 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
51 (defun company-elisp-parse-local (prefix vars)
52 (let ((regexp (concat "[ \t\n]*\\(" (regexp-quote prefix)
53 "\\(?:\\sw\\|\\s_\\)*\\_>\\)")))
54 (ignore-errors
55 (save-excursion
56 (dotimes (i company-elisp-parse-depth)
57 (up-list -1)
58 (save-excursion
59 (when (looking-at company-elisp-binding-regexp)
60 (down-list 2)
61 (ignore-errors
62 (dotimes (i company-elisp-parse-limit)
63 (save-excursion
64 (when (looking-at "[ \t\n]*(")
65 (down-list 1))
66 (if (looking-at regexp)
67 (add-to-list 'vars (match-string-no-properties 1))
68 (error)))
69 (forward-sexp))))))))
70 vars))
71
72 (defun company-elisp-candidates (prefix)
73 (let* ((completion-ignore-case nil)
74 (before (char-before (- (point) (length prefix))))
75 (predicate (if (and company-elisp-functions-only-in-context
76 (not (eq before ?')))
77 (if (eq before ?\()
78 'fboundp
79 'boundp)
80 'company-elisp-predicate))
81 (candidates (all-completions prefix obarray predicate)))
82 (company-elisp-parse-local prefix candidates)))
83
84 (defun company-elisp-doc (symbol)
85 (let* ((symbol (intern symbol))
86 (doc (if (fboundp symbol)
87 (documentation symbol t)
88 (documentation-property symbol 'variable-documentation t))))
89 (and (stringp doc)
90 (string-match ".*$" doc)
91 (match-string 0 doc))))
92
93 (defun company-elisp (command &optional arg &rest ignored)
94 "A `company-mode' completion back-end for `emacs-lisp-mode'.
95 See `company-elisp-functions-only-in-context'."
96 (case command
97 ('prefix (and (eq (derived-mode-p 'emacs-lisp-mode) 'emacs-lisp-mode)
98 (company-grab-lisp-symbol)))
99 ('candidates (company-elisp-candidates arg))
100 ('meta (company-elisp-doc arg))
101 ('doc-buffer (let ((symbol (intern arg)))
102 (save-window-excursion
103 (when (or (ignore-errors (describe-function symbol))
104 (ignore-errors (describe-variable symbol)))
105 (help-buffer)))))
106 ('location (let ((sym (intern arg)))
107 (or (ignore-errors (find-definition-noselect sym nil))
108 (ignore-errors (find-definition-noselect sym 'defvar))
109 (ignore-errors (find-definition-noselect sym t)))))))
110
111 (provide 'company-elisp)
112 ;;; company-elisp.el ends here