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.2.
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-modes '(c-mode c++-mode jde-mode java-mode php-mode))
35 (defun company-gtags-fetch-tags (prefix)
38 (when (= 0 (call-process company-gtags-executable nil
39 (list (current-buffer) nil) nil "-c" prefix))
40 (goto-char (point-min))
41 (split-string (buffer-string) "\n" t)))))
43 (defun company-gtags-location (tag)
45 (when (= 0 (call-process company-gtags-executable nil
46 (list (current-buffer) nil) nil "-x" tag))
47 (goto-char (point-min))
48 (when (looking-at (concat (regexp-quote tag)
49 "[ \t]+\\([[:digit:]]+\\)"
50 "[ \t]+\\([^ \t]+\\)"))
51 (cons (expand-file-name (match-string 2))
52 (string-to-number (match-string 1)))))))
55 (defun company-gtags (command &optional arg &rest ignored)
56 "A `company-mode' completion back-end for GNU Global."
57 (interactive (list 'interactive))
59 ('interactive (company-begin-backend 'company-gtags))
60 ('prefix (and company-gtags-executable
61 (memq major-mode company-gtags-modes)
62 (not (company-in-string-or-comment))
63 (or (company-grab-symbol) 'stop)))
64 ('candidates (company-gtags-fetch-tags arg))
66 ('location (company-gtags-location arg))))
68 (provide 'company-gtags)
69 ;;; company-gtags.el ends here