]> code.delx.au - gnu-emacs-elpa/blob - company-etags.el
Use cl-lib
[gnu-emacs-elpa] / company-etags.el
1 ;;; company-etags.el --- company-mode completion back-end for etags
2
3 ;; Copyright (C) 2009-2011, 2014 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 'cl-lib)
30 (require 'etags)
31
32 (defgroup company-etags nil
33 "Completion back-end for etags."
34 :group 'company)
35
36 (defcustom company-etags-use-main-table-list t
37 "Always search `tags-table-list' if set.
38 If this is disabled, `company-etags' will try to find the one table for each
39 buffer automatically."
40 :type '(choice (const :tag "off" nil)
41 (const :tag "on" t)))
42
43 (defcustom company-etags-ignore-case nil
44 "Non-nil to ignore case in completion candidates."
45 :type 'boolean)
46
47 (defvar company-etags-modes '(prog-mode c-mode objc-mode c++-mode java-mode
48 jde-mode pascal-mode perl-mode python-mode))
49
50 (defvar company-etags-buffer-table 'unknown)
51 (make-variable-buffer-local 'company-etags-buffer-table)
52
53 (defun company-etags-find-table ()
54 (let ((file (locate-dominating-file (or buffer-file-name
55 default-directory)
56 "TAGS")))
57 (when file
58 (list (expand-file-name file)))))
59
60 (defun company-etags-buffer-table ()
61 (or (and company-etags-use-main-table-list tags-table-list)
62 (if (eq company-etags-buffer-table 'unknown)
63 (setq company-etags-buffer-table (company-etags-find-table))
64 company-etags-buffer-table)))
65
66 (defun company-etags--candidates (prefix)
67 (let ((tags-table-list (company-etags-buffer-table))
68 (completion-ignore-case company-etags-ignore-case))
69 (and (or tags-file-name tags-table-list)
70 (fboundp 'tags-completion-table)
71 (save-excursion
72 (visit-tags-table-buffer)
73 (all-completions prefix (tags-completion-table))))))
74
75 ;;;###autoload
76 (defun company-etags (command &optional arg &rest ignored)
77 "`company-mode' completion back-end for etags."
78 (interactive (list 'interactive))
79 (cl-case command
80 (interactive (company-begin-backend 'company-etags))
81 (prefix (and (apply 'derived-mode-p company-etags-modes)
82 (not (company-in-string-or-comment))
83 (company-etags-buffer-table)
84 (or (company-grab-symbol) 'stop)))
85 (candidates (company-etags--candidates arg))
86 (location (let ((tags-table-list (company-etags-buffer-table)))
87 (when (fboundp 'find-tag-noselect)
88 (save-excursion
89 (let ((buffer (find-tag-noselect arg)))
90 (cons buffer (with-current-buffer buffer (point))))))))
91 (ignore-case company-etags-ignore-case)))
92
93 (provide 'company-etags)
94 ;;; company-etags.el ends here