1 ;;; company-gtags.el --- a company-mode completion back-end for GNU Global
3 ;; Copyright (C) 2009 Nikolaj Schumacher
5 ;; This file is part of company 0.4.3.
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.
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.
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/>.
21 (eval-when-compile (require 'cl))
23 (defcustom company-gtags-executable
24 (executable-find "global")
25 "*Location of GNU global executable"
29 (define-obsolete-variable-alias
30 'company-gtags-gnu-global-program-name
31 'company-gtags-executable)
33 (defvar company-gtags--tags-available-p 'unknown)
34 (make-variable-buffer-local 'company-gtags--tags-available-p)
36 (defvar company-gtags-modes '(c-mode c++-mode jde-mode java-mode php-mode))
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))
44 (defun company-gtags-fetch-tags (prefix)
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)))))
52 (defun company-gtags-location (tag)
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)))))))
64 (defun company-gtags (command &optional arg &rest ignored)
65 "A `company-mode' completion back-end for GNU Global."
66 (interactive (list 'interactive))
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))
76 ('location (company-gtags-location arg))))
78 (provide 'company-gtags)
79 ;;; company-gtags.el ends here