]> code.delx.au - gnu-emacs-elpa/blob - company-gtags.el
Merge pull request #531 from juergenhoetzel/master
[gnu-emacs-elpa] / company-gtags.el
1 ;;; company-gtags.el --- company-mode completion backend for GNU Global
2
3 ;; Copyright (C) 2009-2011, 2014 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 'company-template)
30 (require 'cl-lib)
31
32 (defgroup company-gtags nil
33 "Completion backend for GNU Global."
34 :group 'company)
35
36 (defcustom company-gtags-executable
37 (executable-find "global")
38 "Location of GNU global executable."
39 :type 'string)
40
41 (define-obsolete-variable-alias
42 'company-gtags-gnu-global-program-name
43 'company-gtags-executable "earlier")
44
45 (defcustom company-gtags-insert-arguments t
46 "When non-nil, insert function arguments as a template after completion."
47 :type 'boolean
48 :package-version '(company . "0.8.1"))
49
50 (defvar-local company-gtags--tags-available-p 'unknown)
51
52 (defcustom company-gtags-modes '(prog-mode jde-mode)
53 "Modes that use `company-gtags'.
54 In all these modes (and their derivatives) `company-gtags' will perform
55 completion."
56 :type '(repeat (symbol :tag "Major mode"))
57 :package-version '(company . "0.8.4"))
58
59 (defun company-gtags--tags-available-p ()
60 (if (eq company-gtags--tags-available-p 'unknown)
61 (setq company-gtags--tags-available-p
62 (locate-dominating-file buffer-file-name "GTAGS"))
63 company-gtags--tags-available-p))
64
65 (defun company-gtags--fetch-tags (prefix)
66 (with-temp-buffer
67 (let (tags)
68 (when (= 0 (call-process company-gtags-executable nil
69 (list (current-buffer) nil) nil "-xGq" (concat "^" prefix)))
70 (goto-char (point-min))
71 (cl-loop while
72 (re-search-forward (concat
73 "^"
74 "\\([^ ]*\\)" ;; completion
75 "[ \t]+\\([[:digit:]]+\\)" ;; linum
76 "[ \t]+\\([^ \t]+\\)" ;; file
77 "[ \t]+\\(.*\\)" ;; definition
78 "$"
79 ) nil t)
80 collect
81 (propertize (match-string 1)
82 'meta (match-string 4)
83 'location (cons (expand-file-name (match-string 3))
84 (string-to-number (match-string 2)))
85 ))))))
86
87 (defun company-gtags--annotation (arg)
88 (let ((meta (get-text-property 0 'meta arg)))
89 (when (string-match (concat arg "\\((.*)\\).*") meta)
90 (match-string 1 meta))))
91
92 ;;;###autoload
93 (defun company-gtags (command &optional arg &rest ignored)
94 "`company-mode' completion backend for GNU Global."
95 (interactive (list 'interactive))
96 (cl-case command
97 (interactive (company-begin-backend 'company-gtags))
98 (prefix (and company-gtags-executable
99 buffer-file-name
100 (apply #'derived-mode-p company-gtags-modes)
101 (not (company-in-string-or-comment))
102 (company-gtags--tags-available-p)
103 (or (company-grab-symbol) 'stop)))
104 (candidates (company-gtags--fetch-tags arg))
105 (sorted t)
106 (duplicates t)
107 (annotation (company-gtags--annotation arg))
108 (meta (get-text-property 0 'meta arg))
109 (location (get-text-property 0 'location arg))
110 (post-completion (let ((anno (company-gtags--annotation arg)))
111 (when (and company-gtags-insert-arguments anno)
112 (insert anno)
113 (company-template-c-like-templatify anno))))))
114
115 (provide 'company-gtags)
116 ;;; company-gtags.el ends here