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