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