]> code.delx.au - gnu-emacs/blob - lisp/cedet/semantic/db.el
Minor whitespace changes and `require' fixes.
[gnu-emacs] / lisp / cedet / semantic / db.el
1 ;;; semantic/db.el --- Semantic tag database manager
2
3 ;;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;;; 2008, 2009 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 ;; Maintain a database of tags for a group of files and enable
27 ;; queries into the database.
28 ;;
29 ;; By default, assume one database per directory.
30 ;;
31
32 (require 'eieio)
33 (require 'eieio-base)
34 (require 'semantic)
35 (eval-when-compile
36 (require 'semantic/lex-spp))
37
38 ;;; Variables:
39 (defgroup semanticdb nil
40 "Parser Generator Persistent Database interface."
41 :group 'semantic
42 )
43 ;;; Code:
44 (defvar semanticdb-database-list nil
45 "List of all active databases.")
46
47 (defvar semanticdb-new-database-class 'semanticdb-project-database-file
48 "The default type of database created for new files.
49 This can be changed on a per file basis, so that some directories
50 are saved using one mechanism, and some directories via a different
51 mechanism.")
52 (make-variable-buffer-local 'semanticdb-new-database-class)
53
54 (defvar semanticdb-default-find-index-class 'semanticdb-find-search-index
55 "The default type of search index to use for a `semanticdb-table's.
56 This can be changed to try out new types of search indicies.")
57 (make-variable-buffer-local 'semanticdb-default-find=index-class)
58
59 ;;;###autoload
60 (defvar semanticdb-current-database nil
61 "For a given buffer, this is the currently active database.")
62 (make-variable-buffer-local 'semanticdb-current-database)
63
64 ;;;###autoload
65 (defvar semanticdb-current-table nil
66 "For a given buffer, this is the currently active database table.")
67 (make-variable-buffer-local 'semanticdb-current-table)
68
69 ;;; ABSTRACT CLASSES
70 ;;
71 (defclass semanticdb-abstract-table ()
72 ((parent-db ;; :initarg :parent-db
73 ;; Do not set an initarg, or you get circular writes to disk.
74 :documentation "Database Object containing this table.")
75 (major-mode :initarg :major-mode
76 :initform nil
77 :documentation "Major mode this table belongs to.
78 Sometimes it is important for a program to know if a given table has the
79 same major mode as the current buffer.")
80 (tags :initarg :tags
81 :accessor semanticdb-get-tags
82 :printer semantic-tag-write-list-slot-value
83 :documentation "The tags belonging to this table.")
84 (index :type semanticdb-abstract-search-index
85 :documentation "The search index.
86 Used by semanticdb-find to store additional information about
87 this table for searching purposes.
88
89 Note: This index will not be saved in a persistent file.")
90 (cache :type list
91 :initform nil
92 :documentation "List of cache information for tools.
93 Any particular tool can cache data to a database at runtime
94 with `semanticdb-cache-get'.
95
96 Using a semanticdb cache does not save any information to a file,
97 so your cache will need to be recalculated at runtime. Caches can be
98 referenced even when the file is not in a buffer.
99
100 Note: This index will not be saved in a persistent file.")
101 )
102 "A simple table for semantic tags.
103 This table is the root of tables, and contains the minimum needed
104 for a new table not associated with a buffer."
105 :abstract t)
106
107 (defmethod semanticdb-in-buffer-p ((obj semanticdb-abstract-table))
108 "Return a nil, meaning abstract table OBJ is not in a buffer."
109 nil)
110
111 (defmethod semanticdb-get-buffer ((obj semanticdb-abstract-table))
112 "Return a buffer associated with OBJ.
113 If the buffer is not in memory, load it with `find-file-noselect'."
114 nil)
115
116 (defmethod semanticdb-full-filename ((obj semanticdb-abstract-table))
117 "Fetch the full filename that OBJ refers to.
118 Abstract tables do not have file names associated with them."
119 nil)
120
121 (defmethod semanticdb-dirty-p ((obj semanticdb-abstract-table))
122 "Return non-nil if OBJ is 'dirty'."
123 nil)
124
125 (defmethod semanticdb-set-dirty ((obj semanticdb-abstract-table))
126 "Mark the abstract table OBJ dirty.
127 Abstract tables can not be marked dirty, as there is nothing
128 for them to synchronize against."
129 ;; The abstract table can not be dirty.
130 nil)
131
132 (defmethod semanticdb-normalize-tags ((obj semanticdb-abstract-table) tags)
133 "For the table OBJ, convert a list of TAGS, into standardized form.
134 The default is to return TAGS.
135 Some databases may default to searching and providing simplified tags
136 based on whichever technique used. This method provides a hook for
137 them to convert TAG into a more complete form."
138 tags)
139
140 (defmethod semanticdb-normalize-one-tag ((obj semanticdb-abstract-table) tag)
141 "For the table OBJ, convert a TAG, into standardized form.
142 This method returns a list of the form (DATABASE . NEWTAG).
143
144 The default is to just return (OBJ TAG).
145
146 Some databases may default to searching and providing simplified tags
147 based on whichever technique used. This method provides a hook for
148 them to convert TAG into a more complete form."
149 (cons obj tag))
150
151 (defmethod object-print ((obj semanticdb-abstract-table) &rest strings)
152 "Pretty printer extension for `semanticdb-table'.
153 Adds the number of tags in this file to the object print name."
154 (apply 'call-next-method obj
155 (cons (format " (%d tags)"
156 (length (semanticdb-get-tags obj))
157 )
158 strings)))
159
160 ;;; Index Cache
161 ;;
162 (defclass semanticdb-abstract-search-index ()
163 ((table :initarg :table
164 :type semanticdb-abstract-table
165 :documentation "XRef to the table this belongs to.")
166 )
167 "A place where semanticdb-find can store search index information.
168 The search index will store data about which other tables might be
169 needed, or perhaps create hash or index tables for the current buffer."
170 :abstract t)
171
172 (defmethod semanticdb-get-table-index ((obj semanticdb-abstract-table))
173 "Return the search index for the table OBJ.
174 If one doesn't exist, create it."
175 (if (slot-boundp obj 'index)
176 (oref obj index)
177 (let ((idx nil))
178 (setq idx (funcall semanticdb-default-find-index-class
179 (concat (object-name obj) " index")
180 ;; Fill in the defaults
181 :table obj
182 ))
183 (oset obj index idx)
184 idx)))
185
186 (defmethod semanticdb-synchronize ((idx semanticdb-abstract-search-index)
187 new-tags)
188 "Synchronize the search index IDX with some NEW-TAGS."
189 ;; The abstract class will do... NOTHING!
190 )
191
192 (defmethod semanticdb-partial-synchronize ((idx semanticdb-abstract-search-index)
193 new-tags)
194 "Synchronize the search index IDX with some changed NEW-TAGS."
195 ;; The abstract class will do... NOTHING!
196 )
197
198
199 ;;; SEARCH RESULTS TABLE
200 ;;
201 ;; Needed for system databases that may not provide
202 ;; a semanticdb-table associated with a file.
203 ;;
204 (defclass semanticdb-search-results-table (semanticdb-abstract-table)
205 (
206 )
207 "Table used for search results when there is no file or table association.
208 Examples include search results from external sources such as from
209 Emacs' own symbol table, or from external libraries.")
210
211 (defmethod semanticdb-refresh-table ((obj semanticdb-search-results-table) &optional force)
212 "If the tag list associated with OBJ is loaded, refresh it.
213 This will call `semantic-fetch-tags' if that file is in memory."
214 nil)
215
216 ;;; CONCRETE TABLE CLASSES
217 ;;
218 (defclass semanticdb-table (semanticdb-abstract-table)
219 ((file :initarg :file
220 :documentation "File name relative to the parent database.
221 This is for the file whose tags are stored in this TABLE object.")
222 (buffer :initform nil
223 :documentation "The buffer associated with this table.
224 If nil, the table's buffer is no in Emacs. If it has a value, then
225 it is in Emacs.")
226 (dirty :initform nil
227 :documentation
228 "Non nil if this table needs to be `Saved'.")
229 (db-refs :initform nil
230 :documentation
231 "List of `semanticdb-table' objects refering to this one.
232 These aren't saved, but are instead recalculated after load.
233 See the file semanticdb-ref.el for how this slot is used.")
234 (pointmax :initarg :pointmax
235 :initform nil
236 :documentation "Size of buffer when written to disk.
237 Checked on retrieval to make sure the file is the same.")
238 (fsize :initarg :fsize
239 :initform nil
240 :documentation "Size of the file when it was last referenced.
241 Checked when deciding if a loaded table needs updating from changes
242 outside of Semantic's control.")
243 (lastmodtime :initarg :lastmodtime
244 :initform nil
245 :documentation "Last modification time of the file referenced.
246 Checked when deciding if a loaded table needs updating from changes outside of
247 Semantic's control.")
248 ;; @todo - need to add `last parsed time', so we can also have
249 ;; refresh checks if spp tables or the parser gets rebuilt.
250 (unmatched-syntax :initarg :unmatched-syntax
251 :documentation
252 "List of vectors specifying unmatched syntax.")
253
254 (lexical-table :initarg :lexical-table
255 :initform nil
256 :printer semantic-lex-spp-table-write-slot-value
257 :documentation
258 "Table that might be needed by the lexical analyzer.
259 For C/C++, the C preprocessor macros can be saved here.")
260 )
261 "A single table of tags derived from file.")
262
263 (defmethod semanticdb-in-buffer-p ((obj semanticdb-table))
264 "Return a buffer associated with OBJ.
265 If the buffer is in memory, return that buffer."
266 (let ((buff (oref obj buffer)))
267 (if (buffer-live-p buff)
268 buff
269 (oset obj buffer nil))))
270
271 (defmethod semanticdb-get-buffer ((obj semanticdb-table))
272 "Return a buffer associated with OBJ.
273 If the buffer is in memory, return that buffer.
274 If the buffer is not in memory, load it with `find-file-noselect'."
275 (or (semanticdb-in-buffer-p obj)
276 (find-file-noselect (semanticdb-full-filename obj) t)))
277
278 (defmethod semanticdb-set-buffer ((obj semanticdb-table))
279 "Set the current buffer to be a buffer owned by OBJ.
280 If OBJ's file is not loaded, read it in first."
281 (set-buffer (semanticdb-get-buffer obj)))
282
283 (defmethod semanticdb-full-filename ((obj semanticdb-table))
284 "Fetch the full filename that OBJ refers to."
285 (expand-file-name (oref obj file)
286 (oref (oref obj parent-db) reference-directory)))
287
288 (defmethod semanticdb-dirty-p ((obj semanticdb-table))
289 "Return non-nil if OBJ is 'dirty'."
290 (oref obj dirty))
291
292 (defmethod semanticdb-set-dirty ((obj semanticdb-table))
293 "Mark the abstract table OBJ dirty."
294 (oset obj dirty t)
295 )
296
297 (defmethod object-print ((obj semanticdb-table) &rest strings)
298 "Pretty printer extension for `semanticdb-table'.
299 Adds the number of tags in this file to the object print name."
300 (apply 'call-next-method obj
301 (cons (if (oref obj dirty) ", DIRTY" "") strings)))
302
303 ;;; DATABASE BASE CLASS
304 ;;
305 (defclass semanticdb-project-database (eieio-instance-tracker)
306 ((tracking-symbol :initform semanticdb-database-list)
307 (reference-directory :type string
308 :documentation "Directory this database refers to.
309 When a cache directory is specified, then this refers to the directory
310 this database contains symbols for.")
311 (new-table-class :initform semanticdb-table
312 :type class
313 :documentation
314 "New tables created for this database are of this class.")
315 (cache :type list
316 :initform nil
317 :documentation "List of cache information for tools.
318 Any particular tool can cache data to a database at runtime
319 with `semanticdb-cache-get'.
320
321 Using a semanticdb cache does not save any information to a file,
322 so your cache will need to be recalculated at runtime.
323
324 Note: This index will not be saved in a persistent file.")
325 (tables :initarg :tables
326 :type list
327 ;; Need this protection so apps don't try to access
328 ;; the tables without using the accessor.
329 :accessor semanticdb-get-database-tables
330 :protection :protected
331 :documentation "List of `semantic-db-table' objects."))
332 "Database of file tables.")
333
334 (defmethod semanticdb-full-filename ((obj semanticdb-project-database))
335 "Fetch the full filename that OBJ refers to.
336 Abstract tables do not have file names associated with them."
337 nil)
338
339 (defmethod semanticdb-dirty-p ((DB semanticdb-project-database))
340 "Return non-nil if DB is 'dirty'.
341 A database is dirty if the state of the database changed in a way
342 where it may need to resynchronize with some persistent storage."
343 (let ((dirty nil)
344 (tabs (oref DB tables)))
345 (while (and (not dirty) tabs)
346 (setq dirty (semanticdb-dirty-p (car tabs)))
347 (setq tabs (cdr tabs)))
348 dirty))
349
350 (defmethod object-print ((obj semanticdb-project-database) &rest strings)
351 "Pretty printer extension for `semanticdb-project-database'.
352 Adds the number of tables in this file to the object print name."
353 (apply 'call-next-method obj
354 (cons (format " (%d tables%s)"
355 (length (semanticdb-get-database-tables obj))
356 (if (semanticdb-dirty-p obj)
357 " DIRTY" "")
358 )
359 strings)))
360
361 (defmethod semanticdb-create-database :STATIC ((dbc semanticdb-project-database) directory)
362 "Create a new semantic database of class DBC for DIRECTORY and return it.
363 If a database for DIRECTORY has already been created, return it.
364 If DIRECTORY doesn't exist, create a new one."
365 (let ((db (semanticdb-directory-loaded-p directory)))
366 (unless db
367 (setq db (semanticdb-project-database
368 (file-name-nondirectory directory)
369 :tables nil))
370 ;; Set this up here. We can't put it in the constructor because it
371 ;; would be saved, and we want DB files to be portable.
372 (oset db reference-directory (file-truename directory)))
373 db))
374
375 (defmethod semanticdb-flush-database-tables ((db semanticdb-project-database))
376 "Reset the tables in DB to be empty."
377 (oset db tables nil))
378
379 (defmethod semanticdb-create-table ((db semanticdb-project-database) file)
380 "Create a new table in DB for FILE and return it.
381 The class of DB contains the class name for the type of table to create.
382 If the table for FILE exists, return it.
383 If the table for FILE does not exist, create one."
384 (let ((newtab (semanticdb-file-table db file)))
385 (unless newtab
386 ;; This implementation will satisfy autoloaded classes
387 ;; for tables.
388 (setq newtab (funcall (oref db new-table-class)
389 (file-name-nondirectory file)
390 :file (file-name-nondirectory file)
391 ))
392 (oset newtab parent-db db)
393 (object-add-to-list db 'tables newtab t))
394 newtab))
395
396 (defmethod semanticdb-file-table ((obj semanticdb-project-database) filename)
397 "From OBJ, return FILENAME's associated table object."
398 (object-assoc (file-relative-name (file-truename filename)
399 (oref obj reference-directory))
400 'file (oref obj tables)))
401
402 ;; DATABASE FUNCTIONS
403 (defun semanticdb-get-database (filename)
404 "Get a database for FILENAME.
405 If one isn't found, create one."
406 (semanticdb-create-database semanticdb-new-database-class (file-truename filename)))
407
408 (defun semanticdb-directory-loaded-p (path)
409 "Return the project belonging to PATH if it was already loaded."
410 (eieio-instance-tracker-find path 'reference-directory 'semanticdb-database-list))
411
412 (defun semanticdb-create-table-for-file (filename)
413 "Initialize a database table for FILENAME, and return it.
414 If FILENAME exists in the database already, return that.
415 If there is no database for the table to live in, create one."
416 (let ((cdb nil)
417 (tbl nil)
418 (dd (file-name-directory filename))
419 )
420 ;; Allow a database override function
421 (setq cdb (semanticdb-create-database semanticdb-new-database-class
422 dd))
423 ;; Get a table for this file.
424 (setq tbl (semanticdb-create-table cdb filename))
425
426 ;; Return the pair.
427 (cons cdb tbl)
428 ))
429
430 ;;; Cache Cache.
431 ;;
432 (defclass semanticdb-abstract-cache ()
433 ((table :initarg :table
434 :type semanticdb-abstract-table
435 :documentation
436 "Cross reference to the table this belongs to.")
437 )
438 "Abstract baseclass for tools to use to cache information in semanticdb.
439 Tools needing a per-file cache must subclass this, and then get one as
440 needed. Cache objects are identified in semanticdb by subclass.
441 In order to keep your cache up to date, be sure to implement
442 `semanticdb-synchronize', and `semanticdb-partial-synchronize'.
443 See the file semantic-scope.el for an example."
444 :abstract t)
445
446 (defmethod semanticdb-cache-get ((table semanticdb-abstract-table)
447 desired-class)
448 "Get a cache object on TABLE of class DESIRED-CLASS.
449 This method will create one if none exists with no init arguments
450 other than :table."
451 (assert (child-of-class-p desired-class 'semanticdb-abstract-cache))
452 (let ((cache (oref table cache))
453 (obj nil))
454 (while (and (not obj) cache)
455 (if (eq (object-class-fast (car cache)) desired-class)
456 (setq obj (car cache)))
457 (setq cache (cdr cache)))
458 (if obj
459 obj ;; Just return it.
460 ;; No object, lets create a new one and return that.
461 (setq obj (funcall desired-class "Cache" :table table))
462 (object-add-to-list table 'cache obj)
463 obj)))
464
465 (defmethod semanticdb-cache-remove ((table semanticdb-abstract-table)
466 cache)
467 "Remove from TABLE the cache object CACHE."
468 (object-remove-from-list table 'cache cache))
469
470 (defmethod semanticdb-synchronize ((cache semanticdb-abstract-cache)
471 new-tags)
472 "Synchronize a CACHE with some NEW-TAGS."
473 ;; The abstract class will do... NOTHING!
474 )
475
476 (defmethod semanticdb-partial-synchronize ((cache semanticdb-abstract-cache)
477 new-tags)
478 "Synchronize a CACHE with some changed NEW-TAGS."
479 ;; The abstract class will do... NOTHING!
480 )
481
482 (defclass semanticdb-abstract-db-cache ()
483 ((db :initarg :db
484 :type semanticdb-project-database
485 :documentation
486 "Cross reference to the database this belongs to.")
487 )
488 "Abstract baseclass for tools to use to cache information in semanticdb.
489 Tools needing a database cache must subclass this, and then get one as
490 needed. Cache objects are identified in semanticdb by subclass.
491 In order to keep your cache up to date, be sure to implement
492 `semanticdb-synchronize', and `semanticdb-partial-synchronize'.
493 See the file semantic-scope.el for an example."
494 :abstract t)
495
496 (defmethod semanticdb-cache-get ((db semanticdb-project-database)
497 desired-class)
498 "Get a cache object on DB of class DESIRED-CLASS.
499 This method will create one if none exists with no init arguments
500 other than :table."
501 (assert (child-of-class-p desired-class 'semanticdb-abstract-db-cache))
502 (let ((cache (oref db cache))
503 (obj nil))
504 (while (and (not obj) cache)
505 (if (eq (object-class-fast (car cache)) desired-class)
506 (setq obj (car cache)))
507 (setq cache (cdr cache)))
508 (if obj
509 obj ;; Just return it.
510 ;; No object, lets create a new one and return that.
511 (setq obj (funcall desired-class "Cache" :db db))
512 (object-add-to-list db 'cache obj)
513 obj)))
514
515 (defmethod semanticdb-cache-remove ((db semanticdb-project-database)
516 cache)
517 "Remove from TABLE the cache object CACHE."
518 (object-remove-from-list db 'cache cache))
519
520
521 (defmethod semanticdb-synchronize ((cache semanticdb-abstract-db-cache)
522 new-tags)
523 "Synchronize a CACHE with some NEW-TAGS."
524 ;; The abstract class will do... NOTHING!
525 )
526
527 (defmethod semanticdb-partial-synchronize ((cache semanticdb-abstract-db-cache)
528 new-tags)
529 "Synchronize a CACHE with some changed NEW-TAGS."
530 ;; The abstract class will do... NOTHING!
531 )
532
533 ;;; REFRESH
534
535 (defmethod semanticdb-refresh-table ((obj semanticdb-table) &optional force)
536 "If the tag list associated with OBJ is loaded, refresh it.
537 Optional argument FORCE will force a refresh even if the file in question
538 is not in a buffer. Avoid using FORCE for most uses, as an old cache
539 may be sufficient for the general case. Forced updates can be slow.
540 This will call `semantic-fetch-tags' if that file is in memory."
541 (when (or (semanticdb-in-buffer-p obj) force)
542 (save-excursion
543 (semanticdb-set-buffer obj)
544 (semantic-fetch-tags))))
545
546 (defmethod semanticdb-needs-refresh-p ((obj semanticdb-table))
547 "Return non-nil of OBJ's tag list is out of date.
548 The file associated with OBJ does not need to be in a buffer."
549 (let* ((ff (semanticdb-full-filename obj))
550 (buff (semanticdb-in-buffer-p obj))
551 )
552 (if buff
553 (save-excursion
554 (set-buffer buff)
555 ;; Use semantic's magic tracker to determine of the buffer is up
556 ;; to date or not.
557 (not (semantic-parse-tree-up-to-date-p))
558 ;; We assume that semanticdb is keeping itself up to date.
559 ;; via all the clever hooks
560 )
561 ;; Buffer isn't loaded. The only clue we have is if the file
562 ;; is somehow different from our mark in the semanticdb table.
563 (let* ((stats (file-attributes ff))
564 (actualsize (nth 7 stats))
565 (actualmod (nth 5 stats))
566 )
567
568 (or (not (slot-boundp obj 'tags))
569 ;; (not (oref obj tags)) --> not needed anymore?
570 (/= (or (oref obj fsize) 0) actualsize)
571 (not (equal (oref obj lastmodtime) actualmod))
572 )
573 ))))
574
575 \f
576 ;;; Synchronization
577 ;;
578 (defmethod semanticdb-synchronize ((table semanticdb-abstract-table)
579 new-tags)
580 "Synchronize the table TABLE with some NEW-TAGS."
581 (oset table tags new-tags)
582 (oset table pointmax (point-max))
583 (let ((fattr (file-attributes (semanticdb-full-filename table))))
584 (oset table fsize (nth 7 fattr))
585 (oset table lastmodtime (nth 5 fattr))
586 )
587 ;; Assume it is now up to date.
588 (oset table unmatched-syntax semantic-unmatched-syntax-cache)
589 ;; The lexical table should be good too.
590 (when (featurep 'semantic/lex-spp)
591 (oset table lexical-table (semantic-lex-spp-save-table)))
592 ;; this implies dirtyness
593 (semanticdb-set-dirty table)
594
595 ;; Synchronize the index
596 (when (slot-boundp table 'index)
597 (let ((idx (oref table index)))
598 (when idx (semanticdb-synchronize idx new-tags))))
599
600 ;; Synchronize application caches.
601 (dolist (C (oref table cache))
602 (semanticdb-synchronize C new-tags)
603 )
604
605 ;; Update cross references
606 ;; (semanticdb-refresh-references table)
607 )
608
609 (defmethod semanticdb-partial-synchronize ((table semanticdb-abstract-table)
610 new-tags)
611 "Synchronize the table TABLE where some NEW-TAGS changed."
612 ;; You might think we need to reset the tags, but since the partial
613 ;; parser splices the lists, we don't need to do anything
614 ;;(oset table tags new-tags)
615 ;; We do need to mark ourselves dirty.
616 (semanticdb-set-dirty table)
617
618 ;; The lexical table may be modified.
619 (when (featurep 'semantic/lex-spp)
620 (oset table lexical-table (semantic-lex-spp-save-table)))
621
622 ;; Incremental parser doesn't mokey around with this.
623 (oset table unmatched-syntax semantic-unmatched-syntax-cache)
624
625 ;; Synchronize the index
626 (when (slot-boundp table 'index)
627 (let ((idx (oref table index)))
628 (when idx (semanticdb-partial-synchronize idx new-tags))))
629
630 ;; Synchronize application caches.
631 (dolist (C (oref table cache))
632 (semanticdb-synchronize C new-tags)
633 )
634
635 ;; Update cross references
636 ;;(when (semantic-find-tags-by-class 'include new-tags)
637 ;; (semanticdb-refresh-references table))
638 )
639
640 ;;; SAVE/LOAD
641 ;;
642 (defmethod semanticdb-save-db ((DB semanticdb-project-database)
643 &optional supress-questions)
644 "Cause a database to save itself.
645 The database base class does not save itself persistently.
646 Subclasses could save themselves to a file, or to a database, or other
647 form."
648 nil)
649
650 (defun semanticdb-save-current-db ()
651 "Save the current tag database."
652 (interactive)
653 (message "Saving current tag summaries...")
654 (semanticdb-save-db semanticdb-current-database)
655 (message "Saving current tag summaries...done"))
656
657 (defun semanticdb-save-all-db ()
658 "Save all semantic tag databases."
659 (interactive)
660 (message "Saving tag summaries...")
661 (mapc 'semanticdb-save-db semanticdb-database-list)
662 (message "Saving tag summaries...done"))
663
664 (defun semanticdb-save-all-db-idle ()
665 "Save all semantic tag databases from idle time.
666 Exit the save between databases if there is user input."
667 (semantic-safe "Auto-DB Save: %S"
668 (semantic-exit-on-input 'semanticdb-idle-save
669 (mapc (lambda (db)
670 (semantic-throw-on-input 'semanticdb-idle-save)
671 (semanticdb-save-db db t))
672 semanticdb-database-list))
673 ))
674
675 ;;; Directory Project support
676 ;;
677 (defvar semanticdb-project-predicate-functions nil
678 "List of predicates to try that indicate a directory belongs to a project.
679 This list is used when `semanticdb-persistent-path' contains the value
680 'project. If the predicate list is nil, then presume all paths are valid.
681
682 Project Management software (such as EDE and JDE) should add their own
683 predicates with `add-hook' to this variable, and semanticdb will save tag
684 caches in directories controlled by them.")
685
686 (defmethod semanticdb-write-directory-p ((obj semanticdb-project-database))
687 "Return non-nil if OBJ should be written to disk.
688 Uses `semanticdb-persistent-path' to determine the return value."
689 nil)
690
691 ;;; Utilities
692 ;;
693 ;; What is the current database, are two tables of an equivalent mode,
694 ;; and what databases are a part of the same project.
695 (defun semanticdb-current-database ()
696 "Return the currently active database."
697 (or semanticdb-current-database
698 (and default-directory
699 (semanticdb-create-database semanticdb-new-database-class
700 default-directory)
701 )
702 nil))
703
704 (defvar semanticdb-match-any-mode nil
705 "Non-nil to temporarilly search any major mode for a tag.
706 If a particular major mode wants to search any mode, put the
707 `semantic-match-any-mode' symbol onto the symbol of that major mode.
708 Do not set the value of this variable permanently.")
709
710 (defmacro semanticdb-with-match-any-mode (&rest body)
711 "A Semanticdb search occuring withing BODY will search tags in all modes.
712 This temporarilly sets `semanticdb-match-any-mode' while executing BODY."
713 `(let ((semanticdb-match-any-mode t))
714 ,@body))
715 (put 'semanticdb-with-match-any-mode 'lisp-indent-function 0)
716
717 (defmethod semanticdb-equivalent-mode-for-search (table &optional buffer)
718 "Return non-nil if TABLE's mode is equivalent to BUFFER.
719 See `semanticdb-equivalent-mode' for details.
720 This version is used during searches. Major-modes that opt
721 to set the `semantic-match-any-mode' property will be able to search
722 all files of any type."
723 (or (get major-mode 'semantic-match-any-mode)
724 semanticdb-match-any-mode
725 (semanticdb-equivalent-mode table buffer))
726 )
727
728 (defmethod semanticdb-equivalent-mode ((table semanticdb-abstract-table) &optional buffer)
729 "Return non-nil if TABLE's mode is equivalent to BUFFER.
730 Equivalent modes are specified by by `semantic-equivalent-major-modes'
731 local variable."
732 nil)
733
734 (defmethod semanticdb-equivalent-mode ((table semanticdb-table) &optional buffer)
735 "Return non-nil if TABLE's mode is equivalent to BUFFER.
736 Equivalent modes are specified by by `semantic-equivalent-major-modes'
737 local variable."
738 (save-excursion
739 (if buffer (set-buffer buffer))
740 (or
741 ;; nil major mode in table means we don't know yet. Assume yes for now?
742 (null (oref table major-mode))
743 ;; nil means the same as major-mode
744 (and (not semantic-equivalent-major-modes)
745 (mode-local-use-bindings-p major-mode (oref table major-mode)))
746 (and semantic-equivalent-major-modes
747 (member (oref table major-mode) semantic-equivalent-major-modes))
748 )
749 ))
750
751
752 ;;; Associations
753 ;;
754 ;; These routines determine associations between a file, and multiple
755 ;; associated databases.
756
757 (defcustom semanticdb-project-roots nil
758 "*List of directories, where each directory is the root of some project.
759 All subdirectories of a root project are considered a part of one project.
760 Values in this string can be overriden by project management programs
761 via the `semanticdb-project-root-functions' variable."
762 :group 'semanticdb
763 :type '(repeat string))
764
765 (defvar semanticdb-project-root-functions nil
766 "List of functions used to determine a given directories project root.
767 Functions in this variable can override `semanticdb-project-roots'.
768 Functions set in the variable are given one argument (a directory) and
769 must return a string, (the root directory) or a list of strings (multiple
770 root directories in a more complex system). This variable should be used
771 by project management programs like EDE or JDE.")
772
773 (defvar semanticdb-project-system-databases nil
774 "List of databases containing system library information.
775 Mode authors can create their own system databases which know
776 detailed information about the system libraries for querying purposes.
777 Put those into this variable as a buffer-local, or mode-local
778 value.")
779 (make-variable-buffer-local 'semanticdb-project-system-databases)
780
781 (defvar semanticdb-search-system-databases t
782 "Non nil if search routines are to include a system database.")
783
784 (defun semanticdb-current-database-list (&optional dir)
785 "Return a list of databases associated with the current buffer.
786 If optional argument DIR is non-nil, then use DIR as the starting directory.
787 If this buffer has a database, but doesn't have a project associated
788 with it, return nil.
789 First, it checks `semanticdb-project-root-functions', and if that
790 has no results, it checks `semanticdb-project-roots'. If that fails,
791 it returns the results of function `semanticdb-current-database'.
792 Always append `semanticdb-project-system-databases' if
793 `semanticdb-search-system' is non-nil."
794 (let ((root nil) ; found root directory
795 (dbs nil) ; collected databases
796 (roots semanticdb-project-roots) ;all user roots
797 (dir (file-truename (or dir default-directory)))
798 )
799 ;; Find the root based on project functions.
800 (setq root (run-hook-with-args-until-success
801 'semanticdb-project-root-functions
802 dir))
803 ;; Find roots based on strings
804 (while (and roots (not root))
805 (let ((r (file-truename (car roots))))
806 (if (string-match (concat "^" (regexp-quote r)) dir)
807 (setq root r)))
808 (setq roots (cdr roots)))
809
810 ;; If no roots are found, use this directory.
811 (unless root (setq root dir))
812
813 ;; Find databases based on the root directory.
814 (when root
815 ;; The rootlist allows the root functions to possibly
816 ;; return several roots which are in different areas but
817 ;; all apart of the same system.
818 (let ((regexp (concat "^" (regexp-quote root)))
819 (adb semanticdb-database-list) ; all databases
820 )
821 (while adb
822 ;; I don't like this part, but close enough.
823 (if (and (slot-boundp (car adb) 'reference-directory)
824 (string-match regexp (oref (car adb) reference-directory)))
825 (setq dbs (cons (car adb) dbs)))
826 (setq adb (cdr adb))))
827 )
828 ;; Add in system databases
829 (when semanticdb-search-system-databases
830 (setq dbs (nconc dbs semanticdb-project-system-databases)))
831 ;; Return
832 dbs))
833
834 \f
835 ;;; Generic Accessor Routines
836 ;;
837 ;; These routines can be used to get at tags in files w/out
838 ;; having to know a lot about semanticDB.
839 (defvar semanticdb-file-table-hash (make-hash-table :test 'equal)
840 "Hash table mapping file names to database tables.")
841
842 (defun semanticdb-file-table-object-from-hash (file)
843 "Retrieve a DB table from the hash for FILE.
844 Does not use `file-truename'."
845 (gethash file semanticdb-file-table-hash 'no-hit))
846
847 (defun semanticdb-file-table-object-put-hash (file dbtable)
848 "For FILE, associate DBTABLE in the hash table."
849 (puthash file dbtable semanticdb-file-table-hash))
850
851 ;;;###autoload
852 (defun semanticdb-file-table-object (file &optional dontload)
853 "Return a semanticdb table belonging to FILE, make it up to date.
854 If file has database tags available in the database, return it.
855 If file does not have tags available, and DONTLOAD is nil,
856 then load the tags for FILE, and create a new table object for it.
857 DONTLOAD does not affect the creation of new database objects."
858 ;; (message "Object Translate: %s" file)
859 (when (file-exists-p file)
860 (let* ((default-directory (file-name-directory file))
861 (tab (semanticdb-file-table-object-from-hash file))
862 (fullfile nil))
863
864 ;; If it is not in the cache, then extract the more traditional
865 ;; way by getting the database, and finding a table in that database.
866 ;; Once we have a table, add it to the hash.
867 (when (eq tab 'no-hit)
868 (setq fullfile (file-truename file))
869 (let ((db (or ;; This line will pick up system databases.
870 (semanticdb-directory-loaded-p default-directory)
871 ;; this line will make a new one if needed.
872 (semanticdb-get-database default-directory))))
873 (setq tab (semanticdb-file-table db fullfile))
874 (when tab
875 (semanticdb-file-table-object-put-hash file tab)
876 (when (not (string= fullfile file))
877 (semanticdb-file-table-object-put-hash fullfile tab)
878 ))
879 ))
880
881 (cond
882 ((and tab
883 ;; Is this in a buffer?
884 ;;(find-buffer-visiting (semanticdb-full-filename tab))
885 (semanticdb-in-buffer-p tab)
886 )
887 (save-excursion
888 ;;(set-buffer (find-buffer-visiting (semanticdb-full-filename tab)))
889 (semanticdb-set-buffer tab)
890 (semantic-fetch-tags)
891 ;; Return the table.
892 tab))
893 ((and tab dontload)
894 ;; If we have table, and we don't want to load it, just return it.
895 tab)
896 ((and tab
897 ;; Is table fully loaded, or just a proxy?
898 (number-or-marker-p (oref tab pointmax))
899 ;; Is this table up to date with the file?
900 (not (semanticdb-needs-refresh-p tab)))
901 ;; A-ok!
902 tab)
903 ((or (and fullfile (get-file-buffer fullfile))
904 (get-file-buffer file))
905 ;; are these two calls this faster than `find-buffer-visiting'?
906
907 ;; If FILE is being visited, but none of the above state is
908 ;; true (meaning, there is no table object associated with it)
909 ;; then it is a file not supported by Semantic, and can be safely
910 ;; ignored.
911 nil)
912 ((not dontload) ;; We must load the file.
913 ;; Full file should have been set by now. Debug why not?
914 (when (and (not tab) (not fullfile))
915 ;; This case is if a 'nil is erroneously put into the hash table. This
916 ;; would need fixing
917 (setq fullfile (file-truename file))
918 )
919
920 ;; If we have a table, but no fullfile, that's ok. Lets get the filename
921 ;; from the table which is pre-truenamed.
922 (when (and (not fullfile) tab)
923 (setq fullfile (semanticdb-full-filename tab)))
924
925 (setq tab (semanticdb-create-table-for-file-not-in-buffer fullfile))
926
927 ;; Save the new table.
928 (semanticdb-file-table-object-put-hash file tab)
929 (when (not (string= fullfile file))
930 (semanticdb-file-table-object-put-hash fullfile tab)
931 )
932 ;; Done!
933 tab)
934 (t
935 ;; Full file should have been set by now. Debug why not?
936 ;; One person found this. Is it a file that failed to parse
937 ;; in the past?
938 (when (not fullfile)
939 (setq fullfile (file-truename file)))
940
941 ;; We were asked not to load the file in and parse it.
942 ;; Instead just create a database table with no tags
943 ;; and a claim of being empty.
944 ;;
945 ;; This will give us a starting point for storing
946 ;; database cross-references so when it is loaded,
947 ;; the cross-references will fire and caches will
948 ;; be cleaned.
949 (let ((ans (semanticdb-create-table-for-file file)))
950 (setq tab (cdr ans))
951
952 ;; Save the new table.
953 (semanticdb-file-table-object-put-hash file tab)
954 (when (not (string= fullfile file))
955 (semanticdb-file-table-object-put-hash fullfile tab)
956 )
957 ;; Done!
958 tab))
959 )
960 )))
961
962 (defvar semanticdb-out-of-buffer-create-table-fcn nil
963 "When non-nil, a function for creating a semanticdb table.
964 This should take a filename to be parsed.")
965 (make-variable-buffer-local 'semanticdb-out-of-buffer-create-table-fcn)
966
967 (defun semanticdb-create-table-for-file-not-in-buffer (filename)
968 "Create a table for the file FILENAME.
969 If there are no language specific configurations, this
970 function will read in the buffer, parse it, and kill the buffer."
971 (if (and semanticdb-out-of-buffer-create-table-fcn
972 (not (file-remote-p filename)))
973 ;; Use external parser only of the file is accessible to the
974 ;; local file system.
975 (funcall semanticdb-out-of-buffer-create-table-fcn filename)
976 (save-excursion
977 (let* ( ;; Remember the buffer to kill
978 (kill-buffer-flag (find-buffer-visiting filename))
979 (buffer-to-kill (or kill-buffer-flag
980 (semantic-find-file-noselect filename t))))
981
982 ;; This shouldn't ever be set. Debug some issue here?
983 ;; (when kill-buffer-flag (debug))
984
985 (set-buffer buffer-to-kill)
986 ;; Find file should automatically do this for us.
987 ;; Sometimes the DB table doesn't contains tags and needs
988 ;; a refresh. For example, when the file is loaded for
989 ;; the first time, and the idle scheduler didn't get a
990 ;; chance to trigger a parse before the file buffer is
991 ;; killed.
992 (when semanticdb-current-table
993 (semantic-fetch-tags))
994 (prog1
995 semanticdb-current-table
996 (when (not kill-buffer-flag)
997 ;; If we had to find the file, then we should kill it
998 ;; to keep the master buffer list clean.
999 (kill-buffer buffer-to-kill)
1000 )))))
1001 )
1002
1003 (defun semanticdb-file-stream (file)
1004 "Return a list of tags belonging to FILE.
1005 If file has database tags available in the database, return them.
1006 If file does not have tags available, then load the file, and create them."
1007 (let ((table (semanticdb-file-table-object file)))
1008 (when table
1009 (semanticdb-get-tags table))))
1010
1011 (provide 'semantic/db)
1012
1013 ;; Local variables:
1014 ;; generated-autoload-file: "loaddefs.el"
1015 ;; generated-autoload-feature: semantic/loaddefs
1016 ;; generated-autoload-load-name: "semantic/db"
1017 ;; End:
1018
1019 ;;; semantic/db.el ends here