lisp/cedet/semantic/db-global.el: Add local vars for autoloading.
[bpt/emacs.git] / lisp / cedet / semantic / db-global.el
CommitLineData
20bfd709
CY
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)
691a065e 32(require 'semantic/db-find)
20bfd709
CY
33(require 'semantic/symref/global)
34
35(eval-when-compile
36 ;; For generic function searching.
37 (require 'eieio)
38 (require 'eieio-opt)
39 )
691a065e 40
20bfd709 41;;; Code:
691a065e
CY
42
43(declare-function data-debug-new-buffer "data-debug")
44(declare-function data-debug-insert-thing result "data-debug")
45
ad75612d 46;;;###autoload
20bfd709
CY
47(defun semanticdb-enable-gnu-global-databases (mode)
48 "Enable the use of the GNU Global SemanticDB back end for all files of MODE.
49This will add an instance of a GNU Global database to each buffer
50in 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-hooks)))
65 (eval `(setq-mode-local
66 ,mode semantic-init-mode-hooks
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.
73MODE is the major mode to support."
74 (semanticdb-enable-gnu-global-in-buffer t))
75
691a065e
CY
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
20bfd709
CY
82(defun semanticdb-enable-gnu-global-in-buffer (&optional dont-err-if-not-available)
83 "Enable a GNU Global database in the current buffer.
84Argument DONT-ERR-IF-NOT-AVAILABLE will throw an error if GNU Global
85is 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
20bfd709
CY
109(defmethod semanticdb-equivalent-mode ((table semanticdb-table-global) &optional buffer)
110 "Return t, pretend that this table's mode is equivalent to BUFFER.
111Equivalent modes are specified by by `semantic-equivalent-major-modes'
112local 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.
120For 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.
145Return 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.
162Optional argument TAGS is a list of tags to search.
163Return a list of tags."
164 (if tags (call-next-method)
165 ;; YOUR IMPLEMENTATION HERE
166 (let* ((semantic-symref-tool 'global)
167 (result (semantic-symref-find-tags-by-regexp regex 'project))
168 )
169 (when result
170 (semantic-symref-result-get-tags result))
171 )))
172
173(defmethod semanticdb-find-tags-for-completion-method
174 ((table semanticdb-table-global) prefix &optional tags)
175 "In TABLE, find all occurances of tags matching PREFIX.
176Optional argument TAGS is a list of tags to search.
177Returns a table of all matching tags."
178 (if tags (call-next-method)
179 (let* ((semantic-symref-tool 'global)
180 (result (semantic-symref-find-tags-by-completion prefix 'project))
181 (faketags nil)
182 )
183 (when result
184 (dolist (T (oref result :hit-text))
185 ;; We should look up each tag one at a time, but I'm lazy!
186 ;; Doing this may be good enough.
187 (setq faketags (cons
188 (semantic-tag T 'function :faux t)
189 faketags))
190 )
191 faketags))))
192
193;;; Deep Searches
194;;
195;; If your language does not have a `deep' concept, these can be left
196;; alone, otherwise replace with implementations similar to those
197;; above.
198;;
199(defmethod semanticdb-deep-find-tags-by-name-method
200 ((table semanticdb-table-global) name &optional tags)
201 "Find all tags name NAME in TABLE.
202Optional argument TAGS is a list of tags t
203Like `semanticdb-find-tags-by-name-method' for global."
204 (semanticdb-find-tags-by-name-method table name tags))
205
206(defmethod semanticdb-deep-find-tags-by-name-regexp-method
207 ((table semanticdb-table-global) regex &optional tags)
208 "Find all tags with name matching REGEX in TABLE.
209Optional argument TAGS is a list of tags to search.
210Like `semanticdb-find-tags-by-name-method' for global."
211 (semanticdb-find-tags-by-name-regexp-method table regex tags))
212
213(defmethod semanticdb-deep-find-tags-for-completion-method
214 ((table semanticdb-table-global) prefix &optional tags)
215 "In TABLE, find all occurances of tags matching PREFIX.
216Optional argument TAGS is a list of tags to search.
217Like `semanticdb-find-tags-for-completion-method' for global."
218 (semanticdb-find-tags-for-completion-method table prefix tags))
219
220;;; TEST
221;;
222;; Here is a testing fcn to try out searches via the GNU Global database.
223(defvar semanticdb-test-gnu-global-startfile "~/src/global-5.7.3/global/global.c"
224 "File to use for testing.")
225
226(defun semanticdb-test-gnu-global (searchfor &optional standardfile)
227 "Test the GNU Global semanticdb.
228Argument SEARCHFOR is the text to search for.
229If optional arg STANDARDFILE is non nil, use a standard file w/ global enabled."
230 (interactive "sSearch For Tag: \nP")
231
232 (require 'data-debug)
233 (save-excursion
234 (when standardfile
235 (set-buffer (find-file-noselect semanticdb-test-gnu-global-startfile)))
236
237 (condition-case err
238 (semanticdb-enable-gnu-global-in-buffer)
239 (error (if standardfile
240 (error err)
241 (set-buffer (find-file-noselect semanticdb-test-gnu-global-startfile))
242 (semanticdb-enable-gnu-global-in-buffer))))
243
244 (let* ((db (semanticdb-project-database-global "global"))
245 (tab (semanticdb-file-table db (buffer-file-name)))
246 (result (semanticdb-deep-find-tags-for-completion-method tab searchfor))
247 )
248 (data-debug-new-buffer "*SemanticDB Gnu Global Result*")
249 (data-debug-insert-thing result "?" "")
250 )))
251
252(provide 'semantic/db-global)
253
ad75612d
CY
254;; Local variables:
255;; generated-autoload-file: "loaddefs.el"
256;; generated-autoload-feature: semantic/loaddefs
257;; generated-autoload-load-name: "semantic/db-global"
258;; End:
259
20bfd709 260;;; semantic/db-global.el ends here