]> rtime.felk.cvut.cz Git - sojka/company-mode.git/blob - company-elisp.el
Added version number to back-ends.
[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 0.2.
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 (defvar company-elisp-binding-regexp
40   (concat "([ \t\n]*\\_<" (regexp-opt '("let" "defun" "defmacro" "defsubst"
41                                         "lambda" "lexical-let"))
42           "\\*?"))
43
44 (defun company-elisp-parse-local (prefix vars)
45   (let ((regexp (concat "[ \t\n]*\\(" (regexp-quote prefix)
46                         "\\(?:\\sw\\|\\s_\\)*\\_>\\)")))
47     (ignore-errors
48       (save-excursion
49         (dotimes (i company-elisp-parse-depth)
50           (up-list -1)
51           (save-excursion
52             (when (looking-at company-elisp-binding-regexp)
53               (down-list 2)
54               (ignore-errors
55                 (dotimes (i company-elisp-parse-limit)
56                   (save-excursion
57                     (when (looking-at "[ \t\n]*(")
58                       (down-list 1))
59                     (if (looking-at regexp)
60                         (add-to-list 'vars (match-string-no-properties 1))
61                       (error)))
62                   (forward-sexp))))))))
63     vars))
64
65 (defun company-elisp-candidates (prefix)
66   (let* ((completion-ignore-case nil)
67          (candidates (all-completions prefix obarray 'company-elisp-predicate)))
68     (company-elisp-parse-local prefix candidates)))
69
70 (defun company-elisp-doc (symbol)
71   (let* ((symbol (intern symbol))
72          (doc (if (fboundp symbol)
73                   (documentation symbol t)
74                 (documentation-property symbol 'variable-documentation t))))
75     (and (stringp doc)
76          (string-match ".*$" doc)
77          (match-string 0 doc))))
78
79 (defun company-elisp (command &optional arg &rest ignored)
80   "A `company-mode' completion back-end for `emacs-lisp-mode'."
81   (case command
82     ('prefix (and (eq (derived-mode-p 'emacs-lisp-mode) 'emacs-lisp-mode)
83                   (company-grab-lisp-symbol)))
84     ('candidates (company-elisp-candidates arg))
85     ('meta (company-elisp-doc arg))
86     ('doc-buffer (let ((symbol (intern arg)))
87                    (save-window-excursion
88                      (when (or (ignore-errors (describe-function symbol))
89                                (ignore-errors (describe-variable symbol)))
90                        (help-buffer)))))
91     ('location (let ((sym (intern arg)))
92                  (or (ignore-errors (find-definition-noselect sym nil))
93                      (ignore-errors (find-definition-noselect sym 'defvar))
94                      (ignore-errors (find-definition-noselect sym t)))))))
95
96 (provide 'company-elisp)
97 ;;; company-elisp.el ends here