]> code.delx.au - gnu-emacs-elpa/blob - company-0.5/company-elisp.el
Initial repository contents
[gnu-emacs-elpa] / company-0.5 / company-elisp.el
1 ;;; company-elisp.el --- a company-mode completion back-end for emacs-lisp-mode
2
3 ;; Copyright (C) 2009 Free Software Foundation, Inc.
4
5 ;; Author: Nikolaj Schumacher
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Code:
23
24 (require 'company)
25 (eval-when-compile (require 'cl))
26 (require 'help-mode)
27
28 (defcustom company-elisp-detect-function-context t
29 "*If enabled, offer lisp functions only in appropriate contexts.
30 Functions are offered for completion only after ' and \(."
31 :group 'company
32 :type '(choice (const :tag "Off" nil)
33 (const :tag "On" t)))
34
35 (defun company-grab-lisp-symbol ()
36 (let ((prefix (company-grab-symbol)))
37 (if prefix
38 (unless (and (company-in-string-or-comment)
39 (/= (char-before (- (point) (length prefix))) ?`))
40 prefix)
41 'stop)))
42
43 (defun company-elisp-predicate (symbol)
44 (or (boundp symbol)
45 (fboundp symbol)))
46
47 (defvar company-elisp-parse-limit 30)
48 (defvar company-elisp-parse-depth 100)
49
50 (defvar company-elisp-binding-regexp
51 (concat "([ \t\n]*\\_<" (regexp-opt '("let" "defun" "defmacro" "defsubst"
52 "lambda" "lexical-let" "flet" "labels"))
53 "\\*?")
54 "Regular expression matching sexps containing variable bindings.")
55
56 (defvar company-elisp-binding-regexp-1
57 (concat "([ \t\n]*\\_<" (regexp-opt '("dolist" "dotimes")))
58 "Regular expression matching sexps containing one variable binding.")
59
60 (defun company-elisp-parse-local (prefix vars)
61 (let ((regexp (concat "[ \t\n]*\\(\\_<" (regexp-quote prefix)
62 "\\(?:\\sw\\|\\s_\\)*\\_>\\)"))
63 (pos (point)))
64 (ignore-errors
65 (save-excursion
66 (dotimes (i company-elisp-parse-depth)
67 (up-list -1)
68 (save-excursion
69 (cond
70 ((looking-at company-elisp-binding-regexp)
71 (down-list 2)
72 (ignore-errors
73 (dotimes (i company-elisp-parse-limit)
74 (save-excursion
75 (when (looking-at "[ \t\n]*(")
76 (down-list 1))
77 (and (looking-at regexp)
78 ;; Don't add incomplete text as candidate.
79 (not (eq (match-end 0) pos))
80 (add-to-list 'vars (match-string-no-properties 1))))
81 (forward-sexp))))
82 ((looking-at company-elisp-binding-regexp-1)
83 (down-list 2)
84 (and (looking-at regexp)
85 ;; Don't add incomplete text as candidate.
86 (not (eq (match-end 0) pos))
87 (add-to-list 'vars (match-string-no-properties 1)))))))))
88 vars))
89
90 (defun company-elisp-candidates (prefix)
91 (let* ((completion-ignore-case nil)
92 (before (char-before (- (point) (length prefix))))
93 (predicate (if (and company-elisp-detect-function-context
94 (not (eq before ?')))
95 (if (eq before ?\()
96 'fboundp
97 'boundp)
98 'company-elisp-predicate))
99 (candidates (all-completions prefix obarray predicate)))
100 (company-elisp-parse-local prefix candidates)))
101
102 (defun company-elisp-doc (symbol)
103 (let* ((symbol (intern symbol))
104 (doc (if (fboundp symbol)
105 (documentation symbol t)
106 (documentation-property symbol 'variable-documentation t))))
107 (and (stringp doc)
108 (string-match ".*$" doc)
109 (match-string 0 doc))))
110
111 ;;;###autoload
112 (defun company-elisp (command &optional arg &rest ignored)
113 "A `company-mode' completion back-end for `emacs-lisp-mode'."
114 (interactive (list 'interactive))
115 (case command
116 ('interactive (company-begin-backend 'company-elisp))
117 ('prefix (and (eq (derived-mode-p 'emacs-lisp-mode) 'emacs-lisp-mode)
118 (company-grab-lisp-symbol)))
119 ('candidates (company-elisp-candidates arg))
120 ('meta (company-elisp-doc arg))
121 ('doc-buffer (let ((symbol (intern arg)))
122 (save-window-excursion
123 (when (or (ignore-errors (describe-function symbol))
124 (ignore-errors (describe-variable symbol)))
125 (help-buffer)))))
126 ('location (let ((sym (intern arg)))
127 (or (ignore-errors (find-definition-noselect sym nil))
128 (ignore-errors (find-definition-noselect sym 'defvar))
129 (ignore-errors (find-definition-noselect sym t)))))))
130
131 (provide 'company-elisp)
132 ;;; company-elisp.el ends here