1 ;;; company-gtags.el --- company-mode completion back-end for GNU Global
3 ;; Copyright (C) 2009-2011, 2014 Free Software Foundation, Inc.
5 ;; Author: Nikolaj Schumacher
7 ;; This file is part of GNU Emacs.
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.
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.
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/>.
31 (defgroup company-gtags nil
32 "Completion back-end for GNU Global."
35 (defcustom company-gtags-executable
36 (executable-find "global")
37 "Location of GNU global executable."
40 (define-obsolete-variable-alias
41 'company-gtags-gnu-global-program-name
42 'company-gtags-executable "earlier")
44 (defcustom company-gtags-insert-arguments t
45 "When non-nil, insert function arguments as a template after completion."
47 :package-version '(company . "0.8.1"))
49 (defvar-local company-gtags--tags-available-p 'unknown)
51 (defvar company-gtags-modes '(c-mode c++-mode jde-mode java-mode php-mode))
53 (defun company-gtags--tags-available-p ()
54 (if (eq company-gtags--tags-available-p 'unknown)
55 (setq company-gtags--tags-available-p
56 (locate-dominating-file buffer-file-name "GTAGS"))
57 company-gtags--tags-available-p))
59 (defun company-gtags--fetch-tags (prefix)
62 (when (= 0 (call-process company-gtags-executable nil
63 (list (current-buffer) nil) nil "-xGq" (concat "^" prefix)))
64 (goto-char (point-min))
66 (re-search-forward (concat
68 "\\([^ ]*\\)" ;; completion
69 "[ \t]+\\([[:digit:]]+\\)" ;; linum
70 "[ \t]+\\([^ \t]+\\)" ;; file
71 "[ \t]+\\(.*\\)" ;; definition
75 (propertize (match-string 1)
76 'meta (match-string 4)
77 'location (cons (expand-file-name (match-string 3))
78 (string-to-number (match-string 2)))
81 (defun company-gtags--annotation (arg)
82 (let ((meta (get-text-property 0 'meta arg)))
83 (when (string-match (concat arg "\\((.*)\\).*") meta)
84 (match-string 1 meta))))
87 (defun company-gtags (command &optional arg &rest ignored)
88 "`company-mode' completion back-end for GNU Global."
89 (interactive (list 'interactive))
91 (interactive (company-begin-backend 'company-gtags))
92 (prefix (and company-gtags-executable
93 (memq major-mode company-gtags-modes)
94 (not (company-in-string-or-comment))
95 (company-gtags--tags-available-p)
96 (or (company-grab-symbol) 'stop)))
97 (candidates (company-gtags--fetch-tags arg))
100 (annotation (company-gtags--annotation arg))
101 (meta (get-text-property 0 'meta arg))
102 (location (get-text-property 0 'location arg))
103 (post-completion (let ((anno (company-gtags--annotation arg)))
104 (when (and company-gtags-insert-arguments anno)
106 (company-template-c-like-templatify anno))))))
108 (provide 'company-gtags)
109 ;;; company-gtags.el ends here