]> code.delx.au - gnu-emacs-elpa/blob - packages/company-0.5/company-gtags.el
* admin/archive-contents.el: New file.
[gnu-emacs-elpa] / packages / company-0.5 / company-gtags.el
1 ;;; company-gtags.el --- a company-mode completion back-end for GNU Global
2
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4
5 ;; Author: Nikolaj Schumacher
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Code:
23
24 (require 'company)
25 (eval-when-compile (require 'cl))
26
27 (defcustom company-gtags-executable
28 (executable-find "global")
29 "*Location of GNU global executable"
30 :type 'string
31 :group 'company)
32
33 (define-obsolete-variable-alias
34 'company-gtags-gnu-global-program-name
35 'company-gtags-executable "earlier")
36
37 (defvar company-gtags--tags-available-p 'unknown)
38 (make-variable-buffer-local 'company-gtags--tags-available-p)
39
40 (defvar company-gtags-modes '(c-mode c++-mode jde-mode java-mode php-mode))
41
42 (defun company-gtags--tags-available-p ()
43 (if (eq company-gtags--tags-available-p 'unknown)
44 (setq company-gtags--tags-available-p
45 (company-locate-dominating-file buffer-file-name "GTAGS"))
46 company-gtags--tags-available-p))
47
48 (defun company-gtags-fetch-tags (prefix)
49 (with-temp-buffer
50 (let (tags)
51 (when (= 0 (call-process company-gtags-executable nil
52 (list (current-buffer) nil) nil "-c" prefix))
53 (goto-char (point-min))
54 (split-string (buffer-string) "\n" t)))))
55
56 (defun company-gtags-location (tag)
57 (with-temp-buffer
58 (when (= 0 (call-process company-gtags-executable nil
59 (list (current-buffer) nil) nil "-x" tag))
60 (goto-char (point-min))
61 (when (looking-at (concat (regexp-quote tag)
62 "[ \t]+\\([[:digit:]]+\\)"
63 "[ \t]+\\([^ \t]+\\)"))
64 (cons (expand-file-name (match-string 2))
65 (string-to-number (match-string 1)))))))
66
67 ;;;###autoload
68 (defun company-gtags (command &optional arg &rest ignored)
69 "A `company-mode' completion back-end for GNU Global."
70 (interactive (list 'interactive))
71 (case command
72 ('interactive (company-begin-backend 'company-gtags))
73 ('prefix (and company-gtags-executable
74 (memq major-mode company-gtags-modes)
75 (not (company-in-string-or-comment))
76 (company-gtags--tags-available-p)
77 (or (company-grab-symbol) 'stop)))
78 ('candidates (company-gtags-fetch-tags arg))
79 ('sorted t)
80 ('location (company-gtags-location arg))))
81
82 (provide 'company-gtags)
83 ;;; company-gtags.el ends here