]> rtime.felk.cvut.cz Git - sojka/company-mode.git/blob - company-gtags.el
Bumped version to 0.4.3.
[sojka/company-mode.git] / company-gtags.el
1 ;;; company-gtags.el --- a company-mode completion back-end for GNU Global
2 ;;
3 ;; Copyright (C) 2009 Nikolaj Schumacher
4 ;;
5 ;; This file is part of company 0.4.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 'cl))
22
23 (defcustom company-gtags-executable
24   (executable-find "global")
25   "*Location of GNU global executable"
26   :type 'string
27   :group 'company)
28
29 (define-obsolete-variable-alias
30   'company-gtags-gnu-global-program-name
31   'company-gtags-executable)
32
33 (defvar company-gtags--tags-available-p 'unknown)
34 (make-variable-buffer-local 'company-gtags--tags-available-p)
35
36 (defvar company-gtags-modes '(c-mode c++-mode jde-mode java-mode php-mode))
37
38 (defun company-gtags--tags-available-p ()
39   (if (eq company-gtags--tags-available-p 'unknown)
40       (setq company-gtags--tags-available-p
41             (company-locate-dominating-file buffer-file-name "GTAGS"))
42     company-gtags--tags-available-p))
43
44 (defun company-gtags-fetch-tags (prefix)
45   (with-temp-buffer
46     (let (tags)
47       (when (= 0 (call-process company-gtags-executable nil
48                                (list (current-buffer) nil) nil "-c" prefix))
49         (goto-char (point-min))
50         (split-string (buffer-string) "\n" t)))))
51
52 (defun company-gtags-location (tag)
53   (with-temp-buffer
54     (when (= 0 (call-process company-gtags-executable nil
55                              (list (current-buffer) nil) nil "-x" tag))
56         (goto-char (point-min))
57         (when (looking-at (concat (regexp-quote tag)
58                                   "[ \t]+\\([[:digit:]]+\\)"
59                                   "[ \t]+\\([^ \t]+\\)"))
60           (cons (expand-file-name (match-string 2))
61                 (string-to-number (match-string 1)))))))
62
63 ;;;###autoload
64 (defun company-gtags (command &optional arg &rest ignored)
65   "A `company-mode' completion back-end for GNU Global."
66   (interactive (list 'interactive))
67   (case command
68     ('interactive (company-begin-backend 'company-gtags))
69     ('prefix (and company-gtags-executable
70                   (memq major-mode company-gtags-modes)
71                   (not (company-in-string-or-comment))
72                   (company-gtags--tags-available-p)
73                   (or (company-grab-symbol) 'stop)))
74     ('candidates (company-gtags-fetch-tags arg))
75     ('sorted t)
76     ('location (company-gtags-location arg))))
77
78 (provide 'company-gtags)
79 ;;; company-gtags.el ends here