]> rtime.felk.cvut.cz Git - sojka/company-mode.git/blob - company-elisp.el
Added completion for local lisp variables.
[sojka/company-mode.git] / company-elisp.el
1 (require 'company)
2 (eval-when-compile (require 'cl))
3
4 (defvar company-lisp-symbol-regexp
5   "\\_<\\(\\sw\\|\\s_\\)+\\_>\\=")
6
7 (defun company-grab-lisp-symbol ()
8   (let ((prefix (or (company-grab company-lisp-symbol-regexp) "")))
9     (unless (and (company-in-string-or-comment (- (point) (length prefix)))
10                  (/= (char-before (- (point) (length prefix))) ?`))
11       prefix)))
12
13 (defun company-elisp-predicate (symbol)
14   (or (boundp symbol)
15       (fboundp symbol)))
16
17 (defvar company-elisp-parse-limit 30)
18 (defvar company-elisp-parse-depth 100)
19
20 (defun company-elisp-parse-let ()
21   (let (vars)
22     (ignore-errors
23       (save-excursion
24         (dotimes (i company-elisp-parse-depth)
25           (up-list -1)
26           (save-excursion
27             (when (looking-at "([ \t\n]*let")
28               (down-list 2)
29               (ignore-errors
30                 (dotimes (i company-elisp-parse-limit)
31                   (save-excursion
32                     (down-list 1)
33                     (if (looking-at "[ \t\n]*\\(\\(?:\\sw\\|\\s_\\)+\\)")
34                         (add-to-list 'vars (match-string-no-properties 1))
35                       (error)))
36                   (forward-sexp))))))))
37     vars))
38
39 (defun company-elisp (command &optional arg &rest ignored)
40   (case command
41     ('prefix (and (eq major-mode 'emacs-lisp-mode)
42                   (company-grab-lisp-symbol)))
43     ('candidates (let ((completion-ignore-case nil))
44                    (append (all-completions arg (company-elisp-parse-let))
45                            (all-completions arg obarray
46                                             'company-elisp-predicate))))))
47
48 (provide 'company-elisp)
49 ;;; company-elisp.el ends here