]> code.delx.au - gnu-emacs-elpa/blob - company-elisp.el
Merge changes from the GNU ELPA repository
[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, 2011 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
23 ;;; Commentary:
24 ;;
25
26 ;;; Code:
27
28 (require 'company)
29 (eval-when-compile (require 'cl))
30 (require 'help-mode)
31
32 (defcustom company-elisp-detect-function-context t
33 "*If enabled, offer Lisp functions only in appropriate contexts.
34 Functions are offered for completion only after ' and \(."
35 :group 'company
36 :type '(choice (const :tag "Off" nil)
37 (const :tag "On" t)))
38
39 (defun company-grab-lisp-symbol ()
40 (let ((prefix (company-grab-symbol)))
41 (if prefix
42 (unless (and (company-in-string-or-comment)
43 (/= (char-before (- (point) (length prefix))) ?`))
44 prefix)
45 'stop)))
46
47 (defun company-elisp-predicate (symbol)
48 (or (boundp symbol)
49 (fboundp symbol)
50 (facep symbol)
51 (featurep symbol)))
52
53 (defvar company-elisp-parse-limit 30)
54 (defvar company-elisp-parse-depth 100)
55
56 (defvar company-elisp-binding-regexp
57 (concat "([ \t\n]*\\_<" (regexp-opt '("let" "defun" "defmacro" "defsubst"
58 "lambda" "lexical-let" "flet" "labels"))
59 "\\*?")
60 "Regular expression matching sexps containing variable bindings.")
61
62 (defvar company-elisp-binding-regexp-1
63 (concat "([ \t\n]*\\_<" (regexp-opt '("dolist" "dotimes")))
64 "Regular expression matching sexps containing one variable binding.")
65
66 (defun company-elisp-parse-local (prefix vars)
67 (let ((regexp (concat "[ \t\n]*\\(\\_<" (regexp-quote prefix)
68 "\\(?:\\sw\\|\\s_\\)*\\_>\\)"))
69 (pos (point)))
70 (ignore-errors
71 (save-excursion
72 (dotimes (i company-elisp-parse-depth)
73 (up-list -1)
74 (save-excursion
75 (cond
76 ((looking-at company-elisp-binding-regexp)
77 (down-list 2)
78 (ignore-errors
79 (dotimes (i company-elisp-parse-limit)
80 (save-excursion
81 (when (looking-at "[ \t\n]*(")
82 (down-list 1))
83 (and (looking-at regexp)
84 ;; Don't add incomplete text as candidate.
85 (not (eq (match-end 0) pos))
86 (add-to-list 'vars (match-string-no-properties 1))))
87 (forward-sexp))))
88 ((looking-at company-elisp-binding-regexp-1)
89 (down-list 2)
90 (and (looking-at regexp)
91 ;; Don't add incomplete text as candidate.
92 (not (eq (match-end 0) pos))
93 (add-to-list 'vars (match-string-no-properties 1)))))))))
94 vars))
95
96 (defun company-elisp-candidates (prefix)
97 (let* ((completion-ignore-case nil)
98 (before (char-before (- (point) (length prefix))))
99 (predicate (if (and company-elisp-detect-function-context
100 (not (eq before ?')))
101 (if (eq before ?\()
102 'fboundp
103 'boundp)
104 'company-elisp-predicate))
105 (candidates (all-completions prefix obarray predicate)))
106 (company-elisp-parse-local prefix candidates)))
107
108 (defun company-elisp-doc (symbol)
109 (let* ((symbol (intern symbol))
110 (doc (if (fboundp symbol)
111 (documentation symbol t)
112 (documentation-property symbol 'variable-documentation t))))
113 (and (stringp doc)
114 (string-match ".*$" doc)
115 (match-string 0 doc))))
116
117 ;;;###autoload
118 (defun company-elisp (command &optional arg &rest ignored)
119 "A `company-mode' completion back-end for `emacs-lisp-mode'."
120 (interactive (list 'interactive))
121 (case command
122 (interactive (company-begin-backend 'company-elisp))
123 (prefix (and (eq (derived-mode-p 'emacs-lisp-mode) 'emacs-lisp-mode)
124 (company-grab-lisp-symbol)))
125 (candidates (company-elisp-candidates arg))
126 (meta (company-elisp-doc arg))
127 (doc-buffer (let ((symbol (intern arg)))
128 (save-window-excursion
129 (ignore-errors
130 (cond
131 ((fboundp symbol) (describe-function symbol))
132 ((boundp symbol) (describe-variable symbol))
133 ((featurep symbol) (describe-package symbol))
134 ((facep symbol) (describe-face symbol))
135 (t (signal 'user-error nil)))
136 (help-buffer)))))
137 (location (let ((sym (intern arg)))
138 (cond
139 ((fboundp sym) (find-definition-noselect sym nil))
140 ((boundp sym) (find-definition-noselect sym 'defvar))
141 ((featurep sym) (cons (find-file-noselect (find-library-name
142 (symbol-name sym)))
143 0))
144 ((facep sym) (find-definition-noselect sym 'defface)))))))
145
146 (provide 'company-elisp)
147 ;;; company-elisp.el ends here