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