]> rtime.felk.cvut.cz Git - sojka/company-mode.git/blob - company-elisp.el
Use overriding active map.
[sojka/company-mode.git] / company-elisp.el
1 ;;; company-elisp.el --- a company-mode completion back-end for emacs-lisp-mode
2 ;;
3 ;; Copyright (C) 2009 Nikolaj Schumacher
4 ;;
5 ;; This file is part of company.
6 ;;
7 ;; This program is free software; you can redistribute it and/or
8 ;; modify it under the terms of the GNU General Public License
9 ;; as published by the Free Software Foundation; either version 2
10 ;; of the License, or (at your option) any later version.
11 ;;
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ;; GNU General Public License for more details.
16 ;;
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 (require 'company)
21 (eval-when-compile (require 'cl))
22
23 (defvar company-lisp-symbol-regexp
24   "\\_<\\(\\sw\\|\\s_\\)+\\_>\\=")
25
26 (defun company-grab-lisp-symbol ()
27   (let ((prefix (or (company-grab company-lisp-symbol-regexp) "")))
28     (unless (and (company-in-string-or-comment (- (point) (length prefix)))
29                  (/= (char-before (- (point) (length prefix))) ?`))
30       prefix)))
31
32 (defun company-elisp-predicate (symbol)
33   (or (boundp symbol)
34       (fboundp symbol)))
35
36 (defvar company-elisp-parse-limit 30)
37 (defvar company-elisp-parse-depth 100)
38
39 (defun company-elisp-parse-let ()
40   (let (vars)
41     (ignore-errors
42       (save-excursion
43         (dotimes (i company-elisp-parse-depth)
44           (up-list -1)
45           (save-excursion
46             (when (looking-at "([ \t\n]*let")
47               (down-list 2)
48               (ignore-errors
49                 (dotimes (i company-elisp-parse-limit)
50                   (save-excursion
51                     (down-list 1)
52                     (if (looking-at "[ \t\n]*\\(\\(?:\\sw\\|\\s_\\)+\\)")
53                         (add-to-list 'vars (match-string-no-properties 1))
54                       (error)))
55                   (forward-sexp))))))))
56     vars))
57
58 (defun company-elisp-doc (symbol)
59   (let* ((symbol (intern symbol))
60          (doc (if (fboundp symbol)
61                   (documentation symbol t)
62                 (documentation-property symbol 'variable-documentation t))))
63     (and (stringp doc)
64          (string-match ".*$" doc)
65          (match-string 0 doc))))
66
67 (defun company-elisp (command &optional arg &rest ignored)
68   (case command
69     ('prefix (and (eq major-mode 'emacs-lisp-mode)
70                   (company-grab-lisp-symbol)))
71     ('candidates (let ((completion-ignore-case nil))
72                    (append (all-completions arg (company-elisp-parse-let))
73                            (all-completions arg obarray
74                                             'company-elisp-predicate))))
75     ('meta (company-elisp-doc arg))
76     ('doc-buffer (describe-function 'describe-function)
77                  (help-buffer))))
78
79 (provide 'company-elisp)
80 ;;; company-elisp.el ends here