]> code.delx.au - gnu-emacs-elpa/blob - company-gtags.el
Added faster and more convenient grab functions.
[gnu-emacs-elpa] / 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-modes '(c-mode c++-mode jde-mode java-mode php-mode))
31
32 (defvar company-gtags-available 'unknown)
33 (make-variable-buffer-local 'company-gtags-available)
34
35 (defun company-gtags-available ()
36 (when (eq company-gtags-available 'unknown)
37 (condition-case err
38 (setq company-gtags-available
39 (= 0 (call-process company-gtags-gnu-global-program-name
40 nil nil nil "-c" "WHATEVER")))
41 (error
42 (message "Company: GNU Global not found")
43 (setq-default company-gtags-available nil))))
44 company-gtags-available)
45
46 (defun company-gtags-fetch-tags (prefix)
47 (with-temp-buffer
48 (let (tags)
49 (when (= 0 (call-process "global" nil (list (current-buffer) nil)
50 nil "-c" prefix))
51 (goto-char (point-min))
52 (split-string (buffer-string) "\n" t)))))
53
54 (defun company-gtags-location (tag)
55 (with-temp-buffer
56 (when (= 0 (call-process "global" nil (list (current-buffer) nil)
57 nil "-x" tag))
58 (goto-char (point-min))
59 (when (looking-at (concat (regexp-quote tag)
60 "[ \t]+\\([[:digit:]]+\\)"
61 "[ \t]+\\([^ \t]+\\)"))
62 (cons (expand-file-name (match-string 2))
63 (string-to-number (match-string 1)))))))
64
65 ;;;###autoload
66 (defun company-gtags (command &optional arg &rest ignored)
67 "A `company-mode' completion back-end for GNU Global."
68 (interactive (list 'interactive))
69 (case command
70 ('interactive (company-begin-backend 'company-gtags))
71 ('prefix (and (memq major-mode company-gtags-modes)
72 (not (company-in-string-or-comment))
73 (company-gtags-available)
74 (company-grab-symbol)))
75 ('candidates (company-gtags-fetch-tags arg))
76 ('sorted t)
77 ('location (company-gtags-location arg))))
78
79 (provide 'company-gtags)
80 ;;; company-gtags.el ends here
81
82