]> code.delx.au - gnu-emacs/blob - lisp/cedet/semantic/db-global.el
CEDET (development tools) package merged.
[gnu-emacs] / lisp / cedet / semantic / db-global.el
1 ;;; semantic/db-global.el --- Semantic database extensions for GLOBAL
2
3 ;;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2008, 2009
4 ;;; Free Software Foundation, Inc.
5
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Keywords: tags
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25 ;;
26 ;; Use GNU Global for by-name database searches.
27 ;;
28 ;; This will work as an "omniscient" database for a given project.
29 ;;
30
31 (require 'cedet-global)
32 (require 'semantic/db-find)
33 (require 'semantic/symref/global)
34
35 (eval-when-compile
36 ;; For generic function searching.
37 (require 'eieio)
38 (require 'eieio-opt)
39 )
40
41 ;;; Code:
42
43 (declare-function data-debug-new-buffer "data-debug")
44 (declare-function data-debug-insert-thing result "data-debug")
45
46 ;;;###autoload
47 (defun semanticdb-enable-gnu-global-databases (mode)
48 "Enable the use of the GNU Global SemanticDB back end for all files of MODE.
49 This will add an instance of a GNU Global database to each buffer
50 in a GNU Global supported hierarchy."
51 (interactive
52 (list (completing-read
53 "Emable in Mode: " obarray
54 #'(lambda (s) (get s 'mode-local-symbol-table))
55 t (symbol-name major-mode))))
56
57 ;; First, make sure the version is ok.
58 (cedet-gnu-global-version-check)
59
60 ;; Make sure mode is a symbol.
61 (when (stringp mode)
62 (setq mode (intern mode)))
63
64 (let ((ih (mode-local-value mode 'semantic-init-mode-hook)))
65 (eval `(setq-mode-local
66 ,mode semantic-init-mode-hook
67 (cons 'semanticdb-enable-gnu-global-hook ih))))
68
69 )
70
71 (defun semanticdb-enable-gnu-global-hook ()
72 "Add support for GNU Global in the current buffer via semantic-init-hook.
73 MODE is the major mode to support."
74 (semanticdb-enable-gnu-global-in-buffer t))
75
76 (defclass semanticdb-project-database-global
77 ;; @todo - convert to one DB per directory.
78 (semanticdb-project-database eieio-instance-tracker)
79 ()
80 "Database representing a GNU Global tags file.")
81
82 (defun semanticdb-enable-gnu-global-in-buffer (&optional dont-err-if-not-available)
83 "Enable a GNU Global database in the current buffer.
84 Argument DONT-ERR-IF-NOT-AVAILABLE will throw an error if GNU Global
85 is not available for this directory."
86 (interactive "P")
87 (if (cedet-gnu-global-root)
88 (setq
89 ;; Add to the system database list.
90 semanticdb-project-system-databases
91 (cons (semanticdb-project-database-global "global")
92 semanticdb-project-system-databases)
93 ;; Apply the throttle.
94 semanticdb-find-default-throttle
95 (append semanticdb-find-default-throttle
96 '(omniscience))
97 )
98 (if dont-err-if-not-available
99 (message "No Global support in %s" default-directory)
100 (error "No Global support in %s" default-directory))
101 ))
102
103 ;;; Classes:
104 (defclass semanticdb-table-global (semanticdb-search-results-table)
105 ((major-mode :initform nil)
106 )
107 "A table for returning search results from GNU Global.")
108
109 (defmethod semanticdb-equivalent-mode ((table semanticdb-table-global) &optional buffer)
110 "Return t, pretend that this table's mode is equivalent to BUFFER.
111 Equivalent modes are specified by by `semantic-equivalent-major-modes'
112 local variable."
113 ;; @todo - hack alert!
114 t)
115
116 ;;; Filename based methods
117 ;;
118 (defmethod semanticdb-get-database-tables ((obj semanticdb-project-database-global))
119 "For a global database, there are no explicit tables.
120 For each file hit, get the traditional semantic table from that file."
121 ;; We need to return something since there is always the "master table"
122 ;; The table can then answer file name type questions.
123 (when (not (slot-boundp obj 'tables))
124 (let ((newtable (semanticdb-table-global "GNU Global Search Table")))
125 (oset obj tables (list newtable))
126 (oset newtable parent-db obj)
127 (oset newtable tags nil)
128 ))
129
130 (call-next-method))
131
132 (defmethod semanticdb-file-table ((obj semanticdb-project-database-global) filename)
133 "From OBJ, return FILENAME's associated table object."
134 ;; We pass in "don't load". I wonder if we need to avoid that or not?
135 (car (semanticdb-get-database-tables obj))
136 )
137
138 ;;; Search Overrides
139 ;;
140 ;; Only NAME based searches work with GLOBAL as that is all it tracks.
141 ;;
142 (defmethod semanticdb-find-tags-by-name-method
143 ((table semanticdb-table-global) name &optional tags)
144 "Find all tags named NAME in TABLE.
145 Return a list of tags."
146 (if tags
147 ;; If TAGS are passed in, then we don't need to do work here.
148 (call-next-method)
149 ;; Call out to GNU Global for some results.
150 (let* ((semantic-symref-tool 'global)
151 (result (semantic-symref-find-tags-by-name name 'project))
152 )
153 (when result
154 ;; We could ask to keep the buffer open, but that annoys
155 ;; people.
156 (semantic-symref-result-get-tags result))
157 )))
158
159 (defmethod semanticdb-find-tags-by-name-regexp-method
160 ((table semanticdb-table-global) regex &optional tags)
161 "Find all tags with name matching REGEX in TABLE.
162 Optional argument TAGS is a list of tags to search.
163 Return a list of tags."
164 (if tags (call-next-method)
165 (let* ((semantic-symref-tool 'global)
166 (result (semantic-symref-find-tags-by-regexp regex 'project))
167 )
168 (when result
169 (semantic-symref-result-get-tags result))
170 )))
171
172 (defmethod semanticdb-find-tags-for-completion-method
173 ((table semanticdb-table-global) prefix &optional tags)
174 "In TABLE, find all occurances of tags matching PREFIX.
175 Optional argument TAGS is a list of tags to search.
176 Returns a table of all matching tags."
177 (if tags (call-next-method)
178 (let* ((semantic-symref-tool 'global)
179 (result (semantic-symref-find-tags-by-completion prefix 'project))
180 (faketags nil)
181 )
182 (when result
183 (dolist (T (oref result :hit-text))
184 ;; We should look up each tag one at a time, but I'm lazy!
185 ;; Doing this may be good enough.
186 (setq faketags (cons
187 (semantic-tag T 'function :faux t)
188 faketags))
189 )
190 faketags))))
191
192 ;;; Deep Searches
193 ;;
194 ;; If your language does not have a `deep' concept, these can be left
195 ;; alone, otherwise replace with implementations similar to those
196 ;; above.
197 ;;
198 (defmethod semanticdb-deep-find-tags-by-name-method
199 ((table semanticdb-table-global) name &optional tags)
200 "Find all tags name NAME in TABLE.
201 Optional argument TAGS is a list of tags t
202 Like `semanticdb-find-tags-by-name-method' for global."
203 (semanticdb-find-tags-by-name-method table name tags))
204
205 (defmethod semanticdb-deep-find-tags-by-name-regexp-method
206 ((table semanticdb-table-global) regex &optional tags)
207 "Find all tags with name matching REGEX in TABLE.
208 Optional argument TAGS is a list of tags to search.
209 Like `semanticdb-find-tags-by-name-method' for global."
210 (semanticdb-find-tags-by-name-regexp-method table regex tags))
211
212 (defmethod semanticdb-deep-find-tags-for-completion-method
213 ((table semanticdb-table-global) prefix &optional tags)
214 "In TABLE, find all occurances of tags matching PREFIX.
215 Optional argument TAGS is a list of tags to search.
216 Like `semanticdb-find-tags-for-completion-method' for global."
217 (semanticdb-find-tags-for-completion-method table prefix tags))
218
219 (provide 'semantic/db-global)
220
221 ;; Local variables:
222 ;; generated-autoload-file: "loaddefs.el"
223 ;; generated-autoload-feature: semantic/loaddefs
224 ;; generated-autoload-load-name: "semantic/db-global"
225 ;; End:
226
227 ;;; semantic/db-global.el ends here