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