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