]> rtime.felk.cvut.cz Git - sojka/company-mode.git/blob - company-gtags.el
Bumped version to 0.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.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-gnu-global-program-name
24   (or (locate-file "global" exec-path exec-suffixes 'file-executable-p)
25       "global")
26   "*"
27   :type 'string
28   :group 'company)
29
30 (defvar company-gtags-symbol-regexp
31   "\\_<[A-Za-z_][A-Za-z_0-9]*\\_>")
32
33 (defvar company-gtags-modes '(c-mode c++-mode jde-mode java-mode php-mode))
34
35 (defvar company-gtags-available 'unknown)
36 (make-variable-buffer-local 'company-gtags-available)
37
38 (defun company-gtags-available ()
39   (when (eq company-gtags-available 'unknown)
40     (condition-case err
41         (setq company-gtags-available
42               (= 0 (call-process company-gtags-gnu-global-program-name
43                                  nil nil nil "-c" "WHATEVER")))
44       (error
45        (message "Company: GNU Global not found")
46        (setq-default company-gtags-available nil))))
47   company-gtags-available)
48
49 (defun company-gtags-fetch-tags (prefix)
50   (with-temp-buffer
51     (let (tags)
52       (when (= 0 (call-process "global" nil (list (current-buffer) nil)
53                                nil "-c" prefix))
54         (goto-char (point-min))
55         (while (looking-at company-gtags-symbol-regexp)
56           (push (match-string-no-properties 0) tags)
57           (forward-line)))
58       (nreverse tags))))
59
60 (defun company-gtags-location (tag)
61   (with-temp-buffer
62     (when (= 0 (call-process "global" nil (list (current-buffer) nil)
63                                nil "-x" tag))
64         (goto-char (point-min))
65         (when (looking-at (concat (regexp-quote tag)
66                                   "[ \t]+\\([[:digit:]]+\\)"
67                                   "[ \t]+\\([^ \t]+\\)"))
68           (cons (expand-file-name (match-string 2))
69                 (string-to-number (match-string 1)))))))
70
71 ;;;###autoload
72 (defun company-gtags (command &optional arg &rest ignored)
73   "A `company-mode' completion back-end for GNU Global."
74   (interactive (list 'interactive))
75   (case command
76     ('interactive (company-begin-backend 'company-gtags))
77     ('prefix (and (memq major-mode company-gtags-modes)
78                   (not (company-in-string-or-comment))
79                   (company-gtags-available)
80                (or (company-grab company-gtags-symbol-regexp) "")))
81     ('candidates (company-gtags-fetch-tags arg))
82     ('sorted t)
83     ('location (company-gtags-location arg))))
84
85 (provide 'company-gtags)
86 ;;; company-gtags.el ends here
87
88