]> code.delx.au - gnu-emacs-elpa/blob - company-gtags.el
Support 'stop return value for 'prefix command.
[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-executable
24 (executable-find "global")
25 "*Location of GNU global executable"
26 :type 'string
27 :group 'company)
28
29 (define-obsolete-variable-alias
30 'company-gtags-gnu-global-program-name
31 'company-gtags-executable)
32
33 (defvar company-gtags-modes '(c-mode c++-mode jde-mode java-mode php-mode))
34
35 (defun company-gtags-fetch-tags (prefix)
36 (with-temp-buffer
37 (let (tags)
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)))))
42
43 (defun company-gtags-location (tag)
44 (with-temp-buffer
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)))))))
53
54 ;;;###autoload
55 (defun company-gtags (command &optional arg &rest ignored)
56 "A `company-mode' completion back-end for GNU Global."
57 (interactive (list 'interactive))
58 (case command
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))
65 ('sorted t)
66 ('location (company-gtags-location arg))))
67
68 (provide 'company-gtags)
69 ;;; company-gtags.el ends here
70
71