]> code.delx.au - gnu-emacs-elpa/blob - company-elisp.el
Fixed duplicate completions for let-bound variables.
[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 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 (prefix vars)
40 (let ((regexp (concat "[ \t\n]*\\(" (regexp-quote prefix)
41 "\\(?:\\sw\\|\\s_\\)*\\_>\\)")))
42 (ignore-errors
43 (save-excursion
44 (dotimes (i company-elisp-parse-depth)
45 (up-list -1)
46 (save-excursion
47 (when (looking-at "([ \t\n]*let")
48 (down-list 2)
49 (ignore-errors
50 (dotimes (i company-elisp-parse-limit)
51 (save-excursion
52 (when (looking-at "[ \t\n]*(")
53 (down-list 1))
54 (if (looking-at regexp)
55 (add-to-list 'vars (match-string-no-properties 1))
56 (error)))
57 (forward-sexp))))))))
58 vars))
59
60 (defun company-elisp-candidates (prefix)
61 (let* ((completion-ignore-case nil)
62 (candidates (all-completions prefix obarray 'company-elisp-predicate)))
63 (company-elisp-parse-let prefix candidates)))
64
65 (defun company-elisp-doc (symbol)
66 (let* ((symbol (intern symbol))
67 (doc (if (fboundp symbol)
68 (documentation symbol t)
69 (documentation-property symbol 'variable-documentation t))))
70 (and (stringp doc)
71 (string-match ".*$" doc)
72 (match-string 0 doc))))
73
74 (defun company-elisp (command &optional arg &rest ignored)
75 "A `company-mode' completion back-end for `emacs-lisp-mode'."
76 (case command
77 ('prefix (and (eq (derived-mode-p 'emacs-lisp-mode) 'emacs-lisp-mode)
78 (company-grab-lisp-symbol)))
79 ('candidates (company-elisp-candidates arg))
80 ('meta (company-elisp-doc arg))
81 ('doc-buffer (let ((symbol (intern arg)))
82 (when (or (ignore-errors (describe-function symbol))
83 (ignore-errors (describe-variable symbol)))
84 (help-buffer))))))
85
86 (provide 'company-elisp)
87 ;;; company-elisp.el ends here