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