mirror of https://github.com/Chizi123/.emacs.d.git

Chizi123
2018-11-21 e75a20334813452c6912c090d70a0de2c805f94d
commit | author | age
5cb5f7 1 ;;; company-gtags.el --- company-mode completion backend for GNU Global
C 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 (define-obsolete-variable-alias
37   'company-gtags-gnu-global-program-name
38   'company-gtags-executable "earlier")
39
40 (defcustom company-gtags-executable
41   (executable-find "global")
42   "Location of GNU global executable."
43   :type 'string)
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 (process-file company-gtags-executable nil
69                                ;; "-T" goes through all the tag files listed in GTAGSLIBPATH
70                                (list (current-buffer) nil) nil "-xGqT" (concat "^" prefix)))
71         (goto-char (point-min))
72         (cl-loop while
73                  (re-search-forward (concat
74                                      "^"
75                                      "\\([^ ]*\\)" ;; completion
76                                      "[ \t]+\\([[:digit:]]+\\)" ;; linum
77                                      "[ \t]+\\([^ \t]+\\)" ;; file
78                                      "[ \t]+\\(.*\\)" ;; definition
79                                      "$"
80                                      ) nil t)
81                  collect
82                  (propertize (match-string 1)
83                              'meta (match-string 4)
84                              'location (cons (expand-file-name (match-string 3))
85                                              (string-to-number (match-string 2)))
86                              ))))))
87
88 (defun company-gtags--annotation (arg)
89   (let ((meta (get-text-property 0 'meta arg)))
90     (when (string-match (concat arg "\\((.*)\\).*") meta)
91       (match-string 1 meta))))
92
93 ;;;###autoload
94 (defun company-gtags (command &optional arg &rest ignored)
95   "`company-mode' completion backend for GNU Global."
96   (interactive (list 'interactive))
97   (cl-case command
98     (interactive (company-begin-backend 'company-gtags))
99     (prefix (and company-gtags-executable
100                  buffer-file-name
101                  (apply #'derived-mode-p company-gtags-modes)
102                  (not (company-in-string-or-comment))
103                  (company-gtags--tags-available-p)
104                  (or (company-grab-symbol) 'stop)))
105     (candidates (company-gtags--fetch-tags arg))
106     (sorted t)
107     (duplicates t)
108     (annotation (company-gtags--annotation arg))
109     (meta (get-text-property 0 'meta arg))
110     (location (get-text-property 0 'location arg))
111     (post-completion (let ((anno (company-gtags--annotation arg)))
112                        (when (and company-gtags-insert-arguments anno)
113                          (insert anno)
114                          (company-template-c-like-templatify anno))))))
115
116 (provide 'company-gtags)
117 ;;; company-gtags.el ends here