1 ;;; semantic/db-global.el --- Semantic database extensions for GLOBAL
3 ;; Copyright (C) 2002-2006, 2008-2011
4 ;; Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
26 ;; Use GNU Global for by-name database searches.
28 ;; This will work as an "omniscient" database for a given project.
31 (require 'cedet-global)
32 (require 'semantic/db-find)
33 (require 'semantic/symref/global)
36 ;; For generic function searching.
44 (defun semanticdb-enable-gnu-global-databases (mode)
45 "Enable the use of the GNU Global SemanticDB back end for all files of MODE.
46 This will add an instance of a GNU Global database to each buffer
47 in a GNU Global supported hierarchy."
49 (list (completing-read
50 "Enable in Mode: " obarray
51 #'(lambda (s) (get s 'mode-local-symbol-table))
52 t (symbol-name major-mode))))
54 ;; First, make sure the version is ok.
55 (cedet-gnu-global-version-check)
57 ;; Make sure mode is a symbol.
59 (setq mode (intern mode)))
61 (let ((ih (mode-local-value mode 'semantic-init-mode-hook)))
62 (eval `(setq-mode-local
63 ,mode semantic-init-mode-hook
64 (cons 'semanticdb-enable-gnu-global-hook ih))))
68 (defun semanticdb-enable-gnu-global-hook ()
69 "Add support for GNU Global in the current buffer via `semantic-init-hook'.
70 MODE is the major mode to support."
71 (semanticdb-enable-gnu-global-in-buffer t))
73 (defclass semanticdb-project-database-global
74 ;; @todo - convert to one DB per directory.
75 (semanticdb-project-database eieio-instance-tracker)
77 "Database representing a GNU Global tags file.")
79 (defun semanticdb-enable-gnu-global-in-buffer (&optional dont-err-if-not-available)
80 "Enable a GNU Global database in the current buffer.
81 When GNU Global is not available for this directory, display a message
82 if optional DONT-ERR-IF-NOT-AVAILABLE is non-nil; else throw an error."
84 (if (cedet-gnu-global-root)
86 ;; Add to the system database list.
87 semanticdb-project-system-databases
88 (cons (semanticdb-project-database-global "global")
89 semanticdb-project-system-databases)
90 ;; Apply the throttle.
91 semanticdb-find-default-throttle
92 (append semanticdb-find-default-throttle
95 (if dont-err-if-not-available
96 nil; (message "No Global support in %s" default-directory)
97 (error "No Global support in %s" default-directory))
101 (defclass semanticdb-table-global (semanticdb-search-results-table)
102 ((major-mode :initform nil)
104 "A table for returning search results from GNU Global.")
106 (defmethod semanticdb-equivalent-mode ((table semanticdb-table-global) &optional buffer)
107 "Return t, pretend that this table's mode is equivalent to BUFFER.
108 Equivalent modes are specified by the `semantic-equivalent-major-modes'
110 ;; @todo - hack alert!
113 ;;; Filename based methods
115 (defmethod semanticdb-get-database-tables ((obj semanticdb-project-database-global))
116 "For a global database, there are no explicit tables.
117 For each file hit, get the traditional semantic table from that file."
118 ;; We need to return something since there is always the "master table"
119 ;; The table can then answer file name type questions.
120 (when (not (slot-boundp obj 'tables))
121 (let ((newtable (semanticdb-table-global "GNU Global Search Table")))
122 (oset obj tables (list newtable))
123 (oset newtable parent-db obj)
124 (oset newtable tags nil)
129 (defmethod semanticdb-file-table ((obj semanticdb-project-database-global) filename)
130 "From OBJ, return FILENAME's associated table object."
131 ;; We pass in "don't load". I wonder if we need to avoid that or not?
132 (car (semanticdb-get-database-tables obj))
137 ;; Only NAME based searches work with GLOBAL as that is all it tracks.
139 (defmethod semanticdb-find-tags-by-name-method
140 ((table semanticdb-table-global) name &optional tags)
141 "Find all tags named NAME in TABLE.
142 Return a list of tags."
144 ;; If TAGS are passed in, then we don't need to do work here.
146 ;; Call out to GNU Global for some results.
147 (let* ((semantic-symref-tool 'global)
148 (result (semantic-symref-find-tags-by-name name 'project))
151 ;; We could ask to keep the buffer open, but that annoys
153 (semantic-symref-result-get-tags result))
156 (defmethod semanticdb-find-tags-by-name-regexp-method
157 ((table semanticdb-table-global) regex &optional tags)
158 "Find all tags with name matching REGEX in TABLE.
159 Optional argument TAGS is a list of tags to search.
160 Return a list of tags."
161 (if tags (call-next-method)
162 (let* ((semantic-symref-tool 'global)
163 (result (semantic-symref-find-tags-by-regexp regex 'project))
166 (semantic-symref-result-get-tags result))
169 (defmethod semanticdb-find-tags-for-completion-method
170 ((table semanticdb-table-global) prefix &optional tags)
171 "In TABLE, find all occurrences of tags matching PREFIX.
172 Optional argument TAGS is a list of tags to search.
173 Returns a table of all matching tags."
174 (if tags (call-next-method)
175 (let* ((semantic-symref-tool 'global)
176 (result (semantic-symref-find-tags-by-completion prefix 'project))
180 (dolist (T (oref result :hit-text))
181 ;; We should look up each tag one at a time, but I'm lazy!
182 ;; Doing this may be good enough.
184 (semantic-tag T 'function :faux t)
191 ;; If your language does not have a `deep' concept, these can be left
192 ;; alone, otherwise replace with implementations similar to those
195 (defmethod semanticdb-deep-find-tags-by-name-method
196 ((table semanticdb-table-global) name &optional tags)
197 "Find all tags name NAME in TABLE.
198 Optional argument TAGS is a list of tags to search.
199 Like `semanticdb-find-tags-by-name-method' for global."
200 (semanticdb-find-tags-by-name-method table name tags))
202 (defmethod semanticdb-deep-find-tags-by-name-regexp-method
203 ((table semanticdb-table-global) regex &optional tags)
204 "Find all tags with name matching REGEX in TABLE.
205 Optional argument TAGS is a list of tags to search.
206 Like `semanticdb-find-tags-by-name-method' for global."
207 (semanticdb-find-tags-by-name-regexp-method table regex tags))
209 (defmethod semanticdb-deep-find-tags-for-completion-method
210 ((table semanticdb-table-global) prefix &optional tags)
211 "In TABLE, find all occurrences of tags matching PREFIX.
212 Optional argument TAGS is a list of tags to search.
213 Like `semanticdb-find-tags-for-completion-method' for global."
214 (semanticdb-find-tags-for-completion-method table prefix tags))
216 (provide 'semantic/db-global)
219 ;; generated-autoload-file: "loaddefs.el"
220 ;; generated-autoload-load-name: "semantic/db-global"
223 ;;; semantic/db-global.el ends here