]> rtime.felk.cvut.cz Git - sojka/company-mode.git/blob - company-gtags.el
Use cl-lib
[sojka/company-mode.git] / company-gtags.el
1 ;;; company-gtags.el --- 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 (require 'cl-lib)
30
31 (defgroup company-gtags nil
32   "Completion back-end for GNU Global."
33   :group 'company)
34
35 (defcustom company-gtags-executable
36   (executable-find "global")
37   "Location of GNU global executable."
38   :type 'string)
39
40 (define-obsolete-variable-alias
41   'company-gtags-gnu-global-program-name
42   'company-gtags-executable "earlier")
43
44 (defvar company-gtags--tags-available-p 'unknown)
45 (make-variable-buffer-local 'company-gtags--tags-available-p)
46
47 (defvar company-gtags-modes '(c-mode c++-mode jde-mode java-mode php-mode))
48
49 (defun company-gtags--tags-available-p ()
50   (if (eq company-gtags--tags-available-p 'unknown)
51       (setq company-gtags--tags-available-p
52             (locate-dominating-file buffer-file-name "GTAGS"))
53     company-gtags--tags-available-p))
54
55 (defun company-gtags-fetch-tags (prefix)
56   (with-temp-buffer
57     (let (tags)
58       (when (= 0 (call-process company-gtags-executable nil
59                                (list (current-buffer) nil) nil "-c" prefix))
60         (goto-char (point-min))
61         (split-string (buffer-string) "\n" t)))))
62
63 (defun company-gtags-location (tag)
64   (with-temp-buffer
65     (when (= 0 (call-process company-gtags-executable nil
66                              (list (current-buffer) nil) nil "-x" tag))
67         (goto-char (point-min))
68         (when (looking-at (concat (regexp-quote tag)
69                                   "[ \t]+\\([[:digit:]]+\\)"
70                                   "[ \t]+\\([^ \t]+\\)"))
71           (cons (expand-file-name (match-string 2))
72                 (string-to-number (match-string 1)))))))
73
74 ;;;###autoload
75 (defun company-gtags (command &optional arg &rest ignored)
76   "`company-mode' completion back-end for GNU Global."
77   (interactive (list 'interactive))
78   (cl-case command
79     (interactive (company-begin-backend 'company-gtags))
80     (prefix (and company-gtags-executable
81                  (memq major-mode company-gtags-modes)
82                  (not (company-in-string-or-comment))
83                  (company-gtags--tags-available-p)
84                  (or (company-grab-symbol) 'stop)))
85     (candidates (company-gtags-fetch-tags arg))
86     (sorted t)
87     (location (company-gtags-location arg))))
88
89 (provide 'company-gtags)
90 ;;; company-gtags.el ends here