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