]> code.delx.au - gnu-emacs-elpa/blob - company-etags.el
Cleaned up company-etags.
[gnu-emacs-elpa] / company-etags.el
1 ;;; company-etags.el --- a company-mode completion back-end for etags
2 ;;
3 ;; Copyright (C) 2009-2010 Nikolaj Schumacher
4 ;;
5 ;; This file is part of company 0.5.
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 (require 'etags)
22
23 (defcustom company-etags-use-main-table-list t
24 "*Always search `tags-table-list' if set.
25 If this is disabled, `company-etags' will try to find the one table for each
26 buffer automatically."
27 :group 'company-mode
28 :type '(choice (const :tag "off" nil)
29 (const :tag "on" t)))
30
31 (defvar company-etags-modes '(c-mode objc-mode c++-mode java-mode jde-mode
32 pascal-mode perl-mode python-mode))
33
34 (defvar company-etags-buffer-table 'unknown)
35 (make-variable-buffer-local 'company-etags-buffer-table)
36
37 (defun company-etags-find-table ()
38 (let ((file (company-locate-dominating-file (or buffer-file-name
39 default-directory)
40 "TAGS")))
41 (when file
42 (list (expand-file-name file)))))
43
44 (defun company-etags-buffer-table ()
45 (or (and company-etags-use-main-table-list tags-table-list)
46 (if (eq company-etags-buffer-table 'unknown)
47 (setq company-etags-buffer-table (company-etags-find-table))
48 company-etags-buffer-table)))
49
50 (defun company-etags--candidates (prefix)
51 (let ((tags-table-list (company-etags-buffer-table))
52 (completion-ignore-case nil))
53 (and (or tags-file-name tags-table-list)
54 (fboundp 'tags-completion-table)
55 (save-excursion
56 (visit-tags-table-buffer)
57 (all-completions prefix (tags-completion-table))))))
58
59 ;;;###autoload
60 (defun company-etags (command &optional arg &rest ignored)
61 "A `company-mode' completion back-end for etags."
62 (interactive (list 'interactive))
63 (case command
64 ('interactive (company-begin-backend 'company-etags))
65 ('prefix (and (memq major-mode company-etags-modes)
66 (not (company-in-string-or-comment))
67 (company-etags-buffer-table)
68 (or (company-grab-symbol) 'stop)))
69 ('candidates (company-etags--candidates arg))
70 ('location (let ((tags-table-list (company-etags-buffer-table)))
71 (when (fboundp 'find-tag-noselect)
72 (save-excursion
73 (let ((buffer (find-tag-noselect arg)))
74 (cons buffer (with-current-buffer buffer (point))))))))
75 ('sorted t)))
76
77 (provide 'company-etags)
78 ;;; company-etags.el ends here