]> code.delx.au - gnu-emacs-elpa/blob - company-gtags.el
62c97070b254a68f167251e2d28f7750c252a159
[gnu-emacs-elpa] / company-gtags.el
1 ;;; company-gtags.el --- A company-mode completion back-end for GNU Global
2
3 ;; Copyright (C) 2009-2011 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
23 ;;; Commentary:
24 ;;
25
26 ;;; Code:
27
28 (require 'company)
29 (eval-when-compile (require 'cl))
30
31 (defcustom company-gtags-executable
32 (executable-find "global")
33 "*Location of GNU global executable."
34 :type 'string
35 :group 'company)
36
37 (define-obsolete-variable-alias
38 'company-gtags-gnu-global-program-name
39 'company-gtags-executable "earlier")
40
41 (defvar company-gtags--tags-available-p 'unknown)
42 (make-variable-buffer-local 'company-gtags--tags-available-p)
43
44 (defvar company-gtags-modes '(c-mode c++-mode jde-mode java-mode php-mode))
45
46 (defun company-gtags--tags-available-p ()
47 (if (eq company-gtags--tags-available-p 'unknown)
48 (setq company-gtags--tags-available-p
49 (company-locate-dominating-file buffer-file-name "GTAGS"))
50 company-gtags--tags-available-p))
51
52 (defun company-gtags-fetch-tags (prefix)
53 (with-temp-buffer
54 (let (tags)
55 (when (= 0 (call-process company-gtags-executable nil
56 (list (current-buffer) nil) nil "-c" prefix))
57 (goto-char (point-min))
58 (split-string (buffer-string) "\n" t)))))
59
60 (defun company-gtags-location (tag)
61 (with-temp-buffer
62 (when (= 0 (call-process company-gtags-executable nil
63 (list (current-buffer) nil) 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 company-gtags-executable
78 (memq major-mode company-gtags-modes)
79 (not (company-in-string-or-comment))
80 (company-gtags--tags-available-p)
81 (or (company-grab-symbol) 'stop)))
82 (candidates (company-gtags-fetch-tags arg))
83 (sorted t)
84 (location (company-gtags-location arg))))
85
86 (provide 'company-gtags)
87 ;;; company-gtags.el ends here