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