]> code.delx.au - gnu-emacs-elpa/blob - company-elisp.el
Support 'stop return value for 'prefix command.
[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.3.
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 (require 'help-mode)
23
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 \(."
27 :group 'company
28 :type '(choice (const :tag "Off" nil)
29 (const :tag "On" t)))
30
31 (defun company-grab-lisp-symbol ()
32 (let ((prefix (company-grab-symbol)))
33 (unless (and (company-in-string-or-comment)
34 (/= (char-before (- (point) (length prefix))) ?`))
35 prefix)))
36
37 (defun company-elisp-predicate (symbol)
38 (or (boundp symbol)
39 (fboundp symbol)))
40
41 (defvar company-elisp-parse-limit 30)
42 (defvar company-elisp-parse-depth 100)
43
44 (defvar company-elisp-binding-regexp
45 (concat "([ \t\n]*\\_<" (regexp-opt '("let" "defun" "defmacro" "defsubst"
46 "lambda" "lexical-let"))
47 "\\*?")
48 "Regular expression matching sexps containing variable bindings.")
49
50 (defvar company-elisp-binding-regexp-1
51 (concat "([ \t\n]*\\_<" (regexp-opt '("dolist" "dotimes")))
52 "Regular expression matching sexps containing one variable binding.")
53
54 (defun company-elisp-parse-local (prefix vars)
55 (let ((regexp (concat "[ \t\n]*\\(\\_<" (regexp-quote prefix)
56 "\\(?:\\sw\\|\\s_\\)*\\_>\\)"))
57 (pos (point)))
58 (ignore-errors
59 (save-excursion
60 (dotimes (i company-elisp-parse-depth)
61 (up-list -1)
62 (save-excursion
63 (cond
64 ((looking-at company-elisp-binding-regexp)
65 (down-list 2)
66 (ignore-errors
67 (dotimes (i company-elisp-parse-limit)
68 (save-excursion
69 (when (looking-at "[ \t\n]*(")
70 (down-list 1))
71 (and (looking-at regexp)
72 ;; Don't add incomplete text as candidate.
73 (not (eq (match-end 0) pos))
74 (add-to-list 'vars (match-string-no-properties 1))))
75 (forward-sexp))))
76 ((looking-at company-elisp-binding-regexp-1)
77 (down-list 2)
78 (and (looking-at regexp)
79 ;; Don't add incomplete text as candidate.
80 (not (eq (match-end 0) pos))
81 (add-to-list 'vars (match-string-no-properties 1)))))))))
82 vars))
83
84 (defun company-elisp-candidates (prefix)
85 (let* ((completion-ignore-case nil)
86 (before (char-before (- (point) (length prefix))))
87 (predicate (if (and company-elisp-detect-function-context
88 (not (eq before ?')))
89 (if (eq before ?\()
90 'fboundp
91 'boundp)
92 'company-elisp-predicate))
93 (candidates (all-completions prefix obarray predicate)))
94 (company-elisp-parse-local prefix candidates)))
95
96 (defun company-elisp-doc (symbol)
97 (let* ((symbol (intern symbol))
98 (doc (if (fboundp symbol)
99 (documentation symbol t)
100 (documentation-property symbol 'variable-documentation t))))
101 (and (stringp doc)
102 (string-match ".*$" doc)
103 (match-string 0 doc))))
104
105 ;;;###autoload
106 (defun company-elisp (command &optional arg &rest ignored)
107 "A `company-mode' completion back-end for `emacs-lisp-mode'."
108 (interactive (list 'interactive))
109 (case command
110 ('interactive (company-begin-backend 'company-elisp))
111 ('prefix (and (eq (derived-mode-p 'emacs-lisp-mode) 'emacs-lisp-mode)
112 (or (company-grab-lisp-symbol) 'stop)))
113 ('candidates (company-elisp-candidates arg))
114 ('meta (company-elisp-doc arg))
115 ('doc-buffer (let ((symbol (intern arg)))
116 (save-window-excursion
117 (when (or (ignore-errors (describe-function symbol))
118 (ignore-errors (describe-variable symbol)))
119 (help-buffer)))))
120 ('location (let ((sym (intern arg)))
121 (or (ignore-errors (find-definition-noselect sym nil))
122 (ignore-errors (find-definition-noselect sym 'defvar))
123 (ignore-errors (find-definition-noselect sym t)))))))
124
125 (provide 'company-elisp)
126 ;;; company-elisp.el ends here