Update copyright year to 2015
[emacs.git] / lisp / cedet / cedet-global.el
blob99bffcb16b5f208f42ab4c05d8b48b4f1bb23c30
1 ;;; cedet-global.el --- GNU Global support for CEDET.
3 ;; Copyright (C) 2008-2015 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <eric@siege-engine.com>
6 ;; Package: cedet
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; Basic support for calling GNU Global, and testing version numbers.
27 (declare-function inversion-check-version "inversion")
29 (defvar cedet-global-min-version "5.0"
30 "Minimum version of GNU Global required.")
32 (defcustom cedet-global-command "global"
33 "Command name for the GNU Global executable."
34 :type 'string
35 :group 'cedet)
37 (defcustom cedet-global-gtags-command "gtags"
38 "Command name for the GNU Global gtags executable.
39 GTAGS is used to create the tags table queried by the 'global' command."
40 :type 'string
41 :group 'cedet)
43 ;;; Code:
44 (defun cedet-gnu-global-search (searchtext texttype type scope)
45 "Perform a search with GNU Global, return the created buffer.
46 SEARCHTEXT is text to find.
47 TEXTTYPE is the type of text, such as 'regexp, 'string, 'tagname,
48 'tagregexp, or 'tagcompletions.
49 TYPE is the type of search, meaning that SEARCHTEXT is compared to
50 filename, tagname (tags table), references (uses of a tag) , or
51 symbol (uses of something not in the tag table.)
52 SCOPE is the scope of the search, such as 'project or 'subdirs."
53 (let ((flgs (cond ((eq type 'file)
54 "-a")
55 (t "-xa")))
56 (scopeflgs (cond
57 ((eq scope 'project)
60 ((eq scope 'target)
61 "l")))
62 (stflag (cond ((or (eq texttype 'tagname)
63 (eq texttype 'tagregexp))
64 "")
65 ((eq texttype 'tagcompletions)
66 "c")
67 ((eq texttype 'regexp)
68 "g")
69 (t "r"))))
70 (cedet-gnu-global-call (list (concat flgs scopeflgs stflag)
71 searchtext))))
73 (defun cedet-gnu-global-call (flags)
74 "Call GNU Global with the list of FLAGS."
75 (let ((b (get-buffer-create "*CEDET Global*"))
76 (cd default-directory))
77 (with-current-buffer b
78 (setq default-directory cd)
79 (erase-buffer))
80 (apply 'call-process cedet-global-command
81 nil b nil
82 flags)
83 b))
85 (defun cedet-gnu-global-gtags-call (flags)
86 "Create GNU Global TAGS using gtags with FLAGS."
87 (let ((b (get-buffer-create "*CEDET Global gtags*"))
88 (cd default-directory)
90 (with-current-buffer b
91 (setq default-directory cd)
92 (erase-buffer))
93 (apply 'call-process cedet-global-gtags-command
94 nil b nil
95 flags)
97 ;; Check for warnings.
98 (with-current-buffer b
99 (goto-char (point-min))
100 (when (re-search-forward "Error\\|Warning" nil t)
101 (error "Output:\n%S" (buffer-string))))
105 (defun cedet-gnu-global-expand-filename (filename)
106 "Expand the FILENAME with GNU Global.
107 Return a fully qualified filename."
108 (interactive "sFile: ")
109 (let ((ans (with-current-buffer (cedet-gnu-global-call (list "-Pa" filename))
110 (goto-char (point-min))
111 (if (looking-at "global: ")
112 (error "GNU Global not available")
113 (split-string (buffer-string) "\n" t)))))
114 (when (called-interactively-p 'interactive)
115 (if ans
116 (if (= (length ans) 1)
117 (message "%s" (car ans))
118 (message "%s + %d others" (car ans)
119 (length (cdr ans))))
120 (error "No file found")))
121 ans))
123 (defun cedet-gnu-global-show-root ()
124 "Show the root of a GNU Global area under the current buffer."
125 (interactive)
126 (message "%s" (cedet-gnu-global-root)))
128 (defun cedet-gnu-global-root (&optional dir)
129 "Return the root of any GNU Global scanned project.
130 If a default starting DIR is not specified, the current buffer's
131 `default-directory' is used."
132 (let ((default-directory (or dir default-directory)))
133 (with-current-buffer (cedet-gnu-global-call (list "-pq"))
134 (goto-char (point-min))
135 (when (not (eobp))
136 (file-name-as-directory
137 (buffer-substring (point) (point-at-eol)))))))
139 (defun cedet-gnu-global-version-check (&optional noerror)
140 "Check the version of the installed GNU Global command.
141 If optional programmatic argument NOERROR is non-nil,
142 then instead of throwing an error if Global isn't available,
143 return nil."
144 (interactive)
145 (require 'inversion)
146 (let ((b (condition-case nil
147 (cedet-gnu-global-call (list "--version"))
148 (error nil)))
149 (rev nil))
150 (if (not b)
151 (progn
152 (when (called-interactively-p 'interactive)
153 (message "GNU Global not found."))
154 nil)
155 (with-current-buffer b
156 (goto-char (point-min))
157 (re-search-forward "(?GNU GLOBAL)? \\([0-9.]+\\)" nil t)
158 (setq rev (match-string 1))
159 (if (inversion-check-version rev nil cedet-global-min-version)
160 (if noerror
162 (error "Version of GNU Global is %s. Need at least %s"
163 rev cedet-global-min-version))
164 ;; Else, return TRUE, as in good enough.
165 (when (called-interactively-p 'interactive)
166 (message "GNU Global %s - Good enough for CEDET." rev))
167 t)))))
169 (defun cedet-gnu-global-scan-hits (buffer)
170 "Scan all the hits from the GNU Global output BUFFER."
171 (let ((hits nil)
172 (r1 "^\\([^ ]+\\) +\\([0-9]+\\) \\([^ ]+\\) "))
173 (with-current-buffer buffer
174 (goto-char (point-min))
175 (while (re-search-forward r1 nil t)
176 (setq hits (cons (cons (string-to-number (match-string 2))
177 (match-string 3))
178 hits)))
179 ;; Return the results
180 (nreverse hits))))
182 (defun cedet-gnu-global-create/update-database (&optional dir)
183 "Create a GNU Global database in DIR.
184 If a database already exists, then just update it."
185 (interactive "DDirectory: ")
186 (let ((root (cedet-gnu-global-root dir)))
187 (if root (setq dir root))
188 (let ((default-directory dir))
189 (cedet-gnu-global-gtags-call
190 (when root
191 '("-u");; Incremental update flag.
196 (provide 'cedet-global)
198 ;;; cedet-global.el ends here