Add "Package:" file headers to denote built-in packages.
[bpt/emacs.git] / lisp / cedet / cedet-global.el
1 ;;; cedet-global.el --- GNU Global support for CEDET.
2
3 ;; Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
4
5 ;; Author: Eric M. Ludlam <eric@siege-engine.com>
6 ;; Package: cedet
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24 ;;
25 ;; Basic support for calling GNU Global, and testing version numbers.
26
27 (declare-function inversion-check-version "inversion")
28
29 (defvar cedet-global-min-version "5.0"
30 "Minimum version of GNU global required.")
31
32 (defcustom cedet-global-command "global"
33 "Command name for the GNU Global executable."
34 :type 'string
35 :group 'cedet)
36
37 ;;; Code:
38 (defun cedet-gnu-global-search (searchtext texttype type scope)
39 "Perform a search with GNU Global, return the created buffer.
40 SEARCHTEXT is text to find.
41 TEXTTYPE is the type of text, such as 'regexp, 'string, 'tagname,
42 'tagregexp, or 'tagcompletions.
43 TYPE is the type of search, meaning that SEARCHTEXT is compared to
44 filename, tagname (tags table), references (uses of a tag) , or
45 symbol (uses of something not in the tag table.)
46 SCOPE is the scope of the search, such as 'project or 'subdirs."
47 (let ((flgs (cond ((eq type 'file)
48 "-a")
49 (t "-xa")))
50 (scopeflgs (cond
51 ((eq scope 'project)
52 ""
53 )
54 ((eq scope 'target)
55 "l")))
56 (stflag (cond ((or (eq texttype 'tagname)
57 (eq texttype 'tagregexp))
58 "")
59 ((eq texttype 'tagcompletions)
60 "c")
61 ((eq texttype 'regexp)
62 "g")
63 (t "r"))))
64 (cedet-gnu-global-call (list (concat flgs scopeflgs stflag)
65 searchtext))))
66
67 (defun cedet-gnu-global-call (flags)
68 "Call GNU Global with the list of FLAGS."
69 (let ((b (get-buffer-create "*CEDET Global*"))
70 (cd default-directory))
71 (with-current-buffer b
72 (setq default-directory cd)
73 (erase-buffer))
74 (apply 'call-process cedet-global-command
75 nil b nil
76 flags)
77 b))
78
79 (defun cedet-gnu-global-expand-filename (filename)
80 "Expand the FILENAME with GNU Global.
81 Return a fully qualified filename."
82 (interactive "sFile: ")
83 (let ((ans (with-current-buffer (cedet-gnu-global-call (list "-Pa" filename))
84 (goto-char (point-min))
85 (if (looking-at "global: ")
86 (error "GNU Global not available")
87 (split-string (buffer-string) "\n" t)))))
88 (when (called-interactively-p 'interactive)
89 (if ans
90 (if (= (length ans) 1)
91 (message "%s" (car ans))
92 (message "%s + %d others" (car ans)
93 (length (cdr ans))))
94 (error "No file found")))
95 ans))
96
97 (defun cedet-gnu-global-show-root ()
98 "Show the root of a GNU Global area under the current buffer."
99 (interactive)
100 (message "%s" (cedet-gnu-global-root)))
101
102 (defun cedet-gnu-global-root (&optional dir)
103 "Return the root of any GNU Global scanned project.
104 If a default starting DIR is not specified, the current buffer's
105 `default-directory' is used."
106 (let ((default-directory (or dir default-directory)))
107 (with-current-buffer (cedet-gnu-global-call (list "-pq"))
108 (goto-char (point-min))
109 (when (not (eobp))
110 (file-name-as-directory
111 (buffer-substring (point) (point-at-eol)))))))
112
113 (defun cedet-gnu-global-version-check (&optional noerror)
114 "Check the version of the installed GNU Global command.
115 If optional programatic argument NOERROR is non-nil, then
116 instead of throwing an error if Global isn't available, then
117 return nil."
118 (interactive)
119 (require 'inversion)
120 (let ((b (condition-case nil
121 (cedet-gnu-global-call (list "--version"))
122 (error nil)))
123 (rev nil))
124 (if (not b)
125 (progn
126 (when (called-interactively-p 'interactive)
127 (message "GNU Global not found."))
128 nil)
129 (with-current-buffer b
130 (goto-char (point-min))
131 (re-search-forward "GNU GLOBAL \\([0-9.]+\\)" nil t)
132 (setq rev (match-string 1))
133 (if (inversion-check-version rev nil cedet-global-min-version)
134 (if noerror
135 nil
136 (error "Version of GNU Global is %s. Need at least %s"
137 rev cedet-global-min-version))
138 ;; Else, return TRUE, as in good enough.
139 (when (called-interactively-p 'interactive)
140 (message "GNU Global %s - Good enough for CEDET." rev))
141 t)))))
142
143 (defun cedet-gnu-global-scan-hits (buffer)
144 "Scan all the hits from the GNU Global output BUFFER."
145 (let ((hits nil)
146 (r1 "^\\([^ ]+\\) +\\([0-9]+\\) \\([^ ]+\\) "))
147 (with-current-buffer buffer
148 (goto-char (point-min))
149 (while (re-search-forward r1 nil t)
150 (setq hits (cons (cons (string-to-number (match-string 2))
151 (match-string 3))
152 hits)))
153 ;; Return the results
154 (nreverse hits))))
155
156 (provide 'cedet-global)
157
158 ;; arch-tag: 0d0d3ac2-91ef-4820-bb2b-1d59ccf38392
159 ;;; cedet-global.el ends here