]> code.delx.au - gnu-emacs-elpa/blob - company-etags.el
Merge changes from the GNU ELPA repository
[gnu-emacs-elpa] / company-etags.el
1 ;;; company-etags.el --- A company-mode completion back-end for etags
2
3 ;; Copyright (C) 2009-2011 Free Software Foundation, Inc.
4
5 ;; Author: Nikolaj Schumacher
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21
22
23 ;;; Commentary:
24 ;;
25
26 ;;; Code:
27
28 (require 'company)
29 (require 'etags)
30
31 (defcustom company-etags-use-main-table-list t
32 "*Always search `tags-table-list' if set.
33 If this is disabled, `company-etags' will try to find the one table for each
34 buffer automatically."
35 :group 'company-mode
36 :type '(choice (const :tag "off" nil)
37 (const :tag "on" t)))
38
39 (defvar company-etags-modes '(c-mode objc-mode c++-mode java-mode jde-mode
40 pascal-mode perl-mode python-mode))
41
42 (defvar company-etags-buffer-table 'unknown)
43 (make-variable-buffer-local 'company-etags-buffer-table)
44
45 (defun company-etags-find-table ()
46 (let ((file (company-locate-dominating-file (or buffer-file-name
47 default-directory)
48 "TAGS")))
49 (when file
50 (list (expand-file-name file)))))
51
52 (defun company-etags-buffer-table ()
53 (or (and company-etags-use-main-table-list tags-table-list)
54 (if (eq company-etags-buffer-table 'unknown)
55 (setq company-etags-buffer-table (company-etags-find-table))
56 company-etags-buffer-table)))
57
58 (defun company-etags--candidates (prefix)
59 (let ((tags-table-list (company-etags-buffer-table))
60 (completion-ignore-case nil))
61 (and (or tags-file-name tags-table-list)
62 (fboundp 'tags-completion-table)
63 (save-excursion
64 (visit-tags-table-buffer)
65 (all-completions prefix (tags-completion-table))))))
66
67 ;;;###autoload
68 (defun company-etags (command &optional arg &rest ignored)
69 "A `company-mode' completion back-end for etags."
70 (interactive (list 'interactive))
71 (case command
72 (interactive (company-begin-backend 'company-etags))
73 (prefix (and (memq major-mode company-etags-modes)
74 (not (company-in-string-or-comment))
75 (company-etags-buffer-table)
76 (or (company-grab-symbol) 'stop)))
77 (candidates (company-etags--candidates arg))
78 (location (let ((tags-table-list (company-etags-buffer-table)))
79 (when (fboundp 'find-tag-noselect)
80 (save-excursion
81 (let ((buffer (find-tag-noselect arg)))
82 (cons buffer (with-current-buffer buffer (point))))))))
83 (sorted t)))
84
85 (provide 'company-etags)
86 ;;; company-etags.el ends here