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