1 ;;; semantic/db.el --- Semantic tag database manager
3 ;; Copyright (C) 2000-2015 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
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/>.
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.
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.")
46 (defgroup semanticdb nil
47 "Parser Generator Persistent Database interface."
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
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)
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)
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)
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
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.")
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
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.")
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."
118 (defmethod semanticdb-in-buffer-p ((obj semanticdb-abstract-table))
119 "Return a nil, meaning abstract table OBJ is not in a buffer."
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'."
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 refers 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."
144 (defmethod semanticdb-dirty-p ((obj semanticdb-abstract-table))
145 "Return non-nil if OBJ is 'dirty'."
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.
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."
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."
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))))
183 (apply 'call-next-method obj strings)
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."
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)
204 (setq idx (funcall semanticdb-default-find-index-class
205 (concat (eieio-object-name obj) " index")
206 ;; Fill in the defaults
212 (defmethod semanticdb-synchronize ((idx semanticdb-abstract-search-index)
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)
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."
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
253 "Non nil if this table needs to be `Saved'.")
254 (db-refs :initform nil
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
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
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
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
277 "List of vectors specifying unmatched syntax.")
279 (lexical-table :initarg :lexical-table
281 :printer semantic-lex-spp-table-write-slot-value
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)
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.
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'."
319 (defmethod semanticdb-set-dirty ((obj semanticdb-table))
320 "Mark the abstract table OBJ dirty."
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 (unless (fboundp 'semanticdb-abstract-table-list-p)
334 (cl-deftype semanticdb-abstract-table-list ()
335 '(list-of semanticdb-abstract-table)))
337 (defclass semanticdb-project-database (eieio-instance-tracker)
338 ((tracking-symbol :initform semanticdb-database-list)
339 (reference-directory :type string
340 :documentation "Directory this database refers to.
341 When a cache directory is specified, then this refers to the directory
342 this database contains symbols for.")
343 (new-table-class :initform semanticdb-table
346 "New tables created for this database are of this class.")
349 :documentation "List of cache information for tools.
350 Any particular tool can cache data to a database at runtime
351 with `semanticdb-cache-get'.
353 Using a semanticdb cache does not save any information to a file,
354 so your cache will need to be recalculated at runtime.
356 Note: This index will not be saved in a persistent file.")
357 (tables :initarg :tables
358 :type semanticdb-abstract-table-list
359 ;; Need this protection so apps don't try to access
360 ;; the tables without using the accessor.
361 :accessor semanticdb-get-database-tables
362 :protection :protected
363 :documentation "List of `semantic-db-table' objects."))
364 "Database of file tables.")
366 (defmethod semanticdb-full-filename ((obj semanticdb-project-database))
367 "Fetch the full filename that OBJ refers to.
368 Abstract tables do not have file names associated with them."
371 (defmethod semanticdb-dirty-p ((DB semanticdb-project-database))
372 "Return non-nil if DB is 'dirty'.
373 A database is dirty if the state of the database changed in a way
374 where it may need to resynchronize with some persistent storage."
376 (tabs (oref DB tables)))
377 (while (and (not dirty) tabs)
378 (setq dirty (semanticdb-dirty-p (car tabs)))
379 (setq tabs (cdr tabs)))
382 (defmethod object-print ((obj semanticdb-project-database) &rest strings)
383 "Pretty printer extension for `semanticdb-project-database'.
384 Adds the number of tables in this file to the object print name."
385 (apply 'call-next-method obj
386 (cons (format " (%d tables%s)"
387 (length (semanticdb-get-database-tables obj))
388 (if (semanticdb-dirty-p obj)
393 (defmethod semanticdb-create-database :STATIC ((dbc semanticdb-project-database) directory)
394 "Create a new semantic database of class DBC for DIRECTORY and return it.
395 If a database for DIRECTORY has already been created, return it.
396 If DIRECTORY doesn't exist, create a new one."
397 (let ((db (semanticdb-directory-loaded-p directory)))
399 (setq db (semanticdb-project-database
400 (file-name-nondirectory directory)
402 ;; Set this up here. We can't put it in the constructor because it
403 ;; would be saved, and we want DB files to be portable.
404 (oset db reference-directory (file-truename directory)))
407 (defmethod semanticdb-flush-database-tables ((db semanticdb-project-database))
408 "Reset the tables in DB to be empty."
409 (oset db tables nil))
411 (defmethod semanticdb-create-table ((db semanticdb-project-database) file)
412 "Create a new table in DB for FILE and return it.
413 The class of DB contains the class name for the type of table to create.
414 If the table for FILE exists, return it.
415 If the table for FILE does not exist, create one."
416 (let ((newtab (semanticdb-file-table db file)))
418 ;; This implementation will satisfy autoloaded classes
420 (setq newtab (funcall (oref db new-table-class)
421 (file-name-nondirectory file)
422 :file (file-name-nondirectory file)
424 (oset newtab parent-db db)
425 (object-add-to-list db 'tables newtab t))
428 (defmethod semanticdb-file-table ((obj semanticdb-project-database) filename)
429 "From OBJ, return FILENAME's associated table object."
430 (object-assoc (file-relative-name (file-truename filename)
431 (oref obj reference-directory))
432 'file (oref obj tables)))
434 ;; DATABASE FUNCTIONS
435 (defun semanticdb-get-database (filename)
436 "Get a database for FILENAME.
437 If one isn't found, create one."
438 (semanticdb-create-database semanticdb-new-database-class (file-truename filename)))
440 (defun semanticdb-directory-loaded-p (path)
441 "Return the project belonging to PATH if it was already loaded."
442 (eieio-instance-tracker-find path 'reference-directory 'semanticdb-database-list))
444 (defun semanticdb-create-table-for-file (filename)
445 "Initialize a database table for FILENAME, and return it.
446 If FILENAME exists in the database already, return that.
447 If there is no database for the table to live in, create one."
450 (dd (file-name-directory (file-truename filename)))
452 ;; Allow a database override function
453 (setq cdb (semanticdb-create-database semanticdb-new-database-class
455 ;; Get a table for this file.
456 (setq tbl (semanticdb-create-table cdb filename))
464 (defclass semanticdb-abstract-cache ()
465 ((table :initarg :table
466 :type semanticdb-abstract-table
468 "Cross reference to the table this belongs to.")
470 "Abstract baseclass for tools to use to cache information in semanticdb.
471 Tools needing a per-file cache must subclass this, and then get one as
472 needed. Cache objects are identified in semanticdb by subclass.
473 In order to keep your cache up to date, be sure to implement
474 `semanticdb-synchronize', and `semanticdb-partial-synchronize'.
475 See the file semantic/scope.el for an example."
478 (defmethod semanticdb-cache-get ((table semanticdb-abstract-table)
480 "Get a cache object on TABLE of class DESIRED-CLASS.
481 This method will create one if none exists with no init arguments
483 (unless (child-of-class-p desired-class 'semanticdb-abstract-cache)
484 (error "Invalid SemanticDB cache"))
485 (let ((cache (oref table cache))
487 (while (and (not obj) cache)
488 (if (eq (eieio-object-class (car cache)) desired-class)
489 (setq obj (car cache)))
490 (setq cache (cdr cache)))
492 obj ;; Just return it.
493 ;; No object, let's create a new one and return that.
494 (setq obj (funcall desired-class "Cache" :table table))
495 (object-add-to-list table 'cache obj)
498 (defmethod semanticdb-cache-remove ((table semanticdb-abstract-table)
500 "Remove from TABLE the cache object CACHE."
501 (object-remove-from-list table 'cache cache))
503 (defmethod semanticdb-synchronize ((cache semanticdb-abstract-cache)
505 "Synchronize a CACHE with some NEW-TAGS."
506 ;; The abstract class will do... NOTHING!
509 (defmethod semanticdb-partial-synchronize ((cache semanticdb-abstract-cache)
511 "Synchronize a CACHE with some changed NEW-TAGS."
512 ;; The abstract class will do... NOTHING!
515 (defclass semanticdb-abstract-db-cache ()
517 :type semanticdb-project-database
519 "Cross reference to the database this belongs to.")
521 "Abstract baseclass for tools to use to cache information in semanticdb.
522 Tools needing a database cache must subclass this, and then get one as
523 needed. Cache objects are identified in semanticdb by subclass.
524 In order to keep your cache up to date, be sure to implement
525 `semanticdb-synchronize', and `semanticdb-partial-synchronize'.
526 See the file semantic/scope.el for an example."
529 (defmethod semanticdb-cache-get ((db semanticdb-project-database)
531 "Get a cache object on DB of class DESIRED-CLASS.
532 This method will create one if none exists with no init arguments
534 (unless (child-of-class-p desired-class 'semanticdb-abstract-cache)
535 (error "Invalid SemanticDB cache"))
536 (let ((cache (oref db cache))
538 (while (and (not obj) cache)
539 (if (eq (eieio-object-class (car cache)) desired-class)
540 (setq obj (car cache)))
541 (setq cache (cdr cache)))
543 obj ;; Just return it.
544 ;; No object, let's create a new one and return that.
545 (setq obj (funcall desired-class "Cache" :db db))
546 (object-add-to-list db 'cache obj)
549 (defmethod semanticdb-cache-remove ((db semanticdb-project-database)
551 "Remove from TABLE the cache object CACHE."
552 (object-remove-from-list db 'cache cache))
555 (defmethod semanticdb-synchronize ((cache semanticdb-abstract-db-cache)
557 "Synchronize a CACHE with some NEW-TAGS."
558 ;; The abstract class will do... NOTHING!
561 (defmethod semanticdb-partial-synchronize ((cache semanticdb-abstract-db-cache)
563 "Synchronize a CACHE with some changed NEW-TAGS."
564 ;; The abstract class will do... NOTHING!
569 (defmethod semanticdb-refresh-table ((obj semanticdb-table) &optional force)
570 "If the tag list associated with OBJ is loaded, refresh it.
571 Optional argument FORCE will force a refresh even if the file in question
572 is not in a buffer. Avoid using FORCE for most uses, as an old cache
573 may be sufficient for the general case. Forced updates can be slow.
574 This will call `semantic-fetch-tags' if that file is in memory."
577 ;; Already in a buffer, just do it.
578 ((semanticdb-in-buffer-p obj)
580 (semanticdb-set-buffer obj)
581 (semantic-fetch-tags)))
583 ;; Not in a buffer. Forcing a load.
585 ;; Patch from Iain Nicol. --
586 ;; @TODO: I wonder if there is a way to recycle
587 ;; semanticdb-create-table-for-file-not-in-buffer
589 (let ((buff (semantic-find-file-noselect
590 (semanticdb-full-filename obj) t)))
592 (semantic-fetch-tags)
593 ;; Kill off the buffer if it didn't exist when we were called.
594 (kill-buffer buff))))))
596 (defmethod semanticdb-needs-refresh-p ((obj semanticdb-table))
597 "Return non-nil of OBJ's tag list is out of date.
598 The file associated with OBJ does not need to be in a buffer."
599 (let* ((ff (semanticdb-full-filename obj))
600 (buff (semanticdb-in-buffer-p obj))
603 (with-current-buffer buff
604 ;; Use semantic's magic tracker to determine of the buffer is up
606 (not (semantic-parse-tree-up-to-date-p))
607 ;; We assume that semanticdb is keeping itself up to date.
608 ;; via all the clever hooks
610 ;; Buffer isn't loaded. The only clue we have is if the file
611 ;; is somehow different from our mark in the semanticdb table.
612 (let* ((stats (file-attributes ff))
613 (actualsize (nth 7 stats))
614 (actualmod (nth 5 stats))
617 (or (not (slot-boundp obj 'tags))
618 ;; (not (oref obj tags)) --> not needed anymore?
619 (/= (or (oref obj fsize) 0) actualsize)
620 (not (equal (oref obj lastmodtime) actualmod))
627 (defmethod semanticdb-synchronize ((table semanticdb-abstract-table)
629 "Synchronize the table TABLE with some NEW-TAGS."
630 (oset table tags new-tags)
631 (oset table pointmax (point-max))
632 (let ((fattr (file-attributes (semanticdb-full-filename table))))
633 (oset table fsize (nth 7 fattr))
634 (oset table lastmodtime (nth 5 fattr))
636 ;; Assume it is now up to date.
637 (oset table unmatched-syntax semantic-unmatched-syntax-cache)
638 ;; The lexical table should be good too.
639 (when (featurep 'semantic/lex-spp)
640 (oset table lexical-table (semantic-lex-spp-save-table)))
641 ;; this implies dirtiness
642 (semanticdb-set-dirty table)
644 ;; Synchronize the index
645 (when (slot-boundp table 'index)
646 (let ((idx (oref table index)))
647 (when idx (semanticdb-synchronize idx new-tags))))
649 ;; Synchronize application caches.
650 (dolist (C (oref table cache))
651 (semanticdb-synchronize C new-tags)
654 ;; Update cross references
655 (semanticdb-refresh-references table)
658 (defmethod semanticdb-partial-synchronize ((table semanticdb-abstract-table)
660 "Synchronize the table TABLE where some NEW-TAGS changed."
661 ;; You might think we need to reset the tags, but since the partial
662 ;; parser splices the lists, we don't need to do anything
663 ;;(oset table tags new-tags)
664 ;; We do need to mark ourselves dirty.
665 (semanticdb-set-dirty table)
667 ;; The lexical table may be modified.
668 (when (featurep 'semantic/lex-spp)
669 (oset table lexical-table (semantic-lex-spp-save-table)))
671 ;; Incremental parser doesn't monkey around with this.
672 (oset table unmatched-syntax semantic-unmatched-syntax-cache)
674 ;; Synchronize the index
675 (when (slot-boundp table 'index)
676 (let ((idx (oref table index)))
677 (when idx (semanticdb-partial-synchronize idx new-tags))))
679 ;; Synchronize application caches.
680 (dolist (C (oref table cache))
681 (semanticdb-synchronize C new-tags)
684 ;; Update cross references
685 (when (semantic-find-tags-by-class 'include new-tags)
686 (semanticdb-refresh-references table))
691 (defmethod semanticdb-save-db ((DB semanticdb-project-database)
692 &optional suppress-questions)
693 "Cause a database to save itself.
694 The database base class does not save itself persistently.
695 Subclasses could save themselves to a file, or to a database, or other
699 (defun semanticdb-save-current-db ()
700 "Save the current tag database."
702 (unless noninteractive
703 (message "Saving current tag summaries..."))
704 (semanticdb-save-db semanticdb-current-database)
705 (unless noninteractive
706 (message "Saving current tag summaries...done")))
708 ;; This prevents Semanticdb from querying multiple times if the users
709 ;; answers "no" to creating the Semanticdb directory.
710 (defvar semanticdb--inhibit-create-file-directory)
712 (defun semanticdb-save-all-db ()
713 "Save all semantic tag databases."
715 (unless noninteractive
716 (message "Saving tag summaries..."))
717 (let ((semanticdb--inhibit-make-directory noninteractive))
718 (mapc 'semanticdb-save-db semanticdb-database-list))
719 (unless noninteractive
720 (message "Saving tag summaries...done")))
722 (defun semanticdb-save-all-db-idle ()
723 "Save all semantic tag databases from idle time.
724 Exit the save between databases if there is user input."
725 (semantic-safe "Auto-DB Save: %S"
726 (semantic-exit-on-input 'semanticdb-idle-save
728 (semantic-throw-on-input 'semanticdb-idle-save)
729 (semanticdb-save-db db t))
730 semanticdb-database-list))
733 ;;; Directory Project support
735 (defvar semanticdb-project-predicate-functions nil
736 "List of predicates to try that indicate a directory belongs to a project.
737 This list is used when `semanticdb-persistent-path' contains the value
738 'project. If the predicate list is nil, then presume all paths are valid.
740 Project Management software (such as EDE and JDE) should add their own
741 predicates with `add-hook' to this variable, and semanticdb will save tag
742 caches in directories controlled by them.")
744 (defmethod semanticdb-write-directory-p ((obj semanticdb-project-database))
745 "Return non-nil if OBJ should be written to disk.
746 Uses `semanticdb-persistent-path' to determine the return value."
751 ;; What is the current database, are two tables of an equivalent mode,
752 ;; and what databases are a part of the same project.
753 (defun semanticdb-current-database ()
754 "Return the currently active database."
755 (or semanticdb-current-database
756 (and default-directory
757 (semanticdb-create-database semanticdb-new-database-class
762 (defvar semanticdb-match-any-mode nil
763 "Non-nil to temporarily search any major mode for a tag.
764 If a particular major mode wants to search any mode, put the
765 `semantic-match-any-mode' symbol onto the symbol of that major mode.
766 Do not set the value of this variable permanently.")
768 (defmacro semanticdb-with-match-any-mode (&rest body)
769 "A Semanticdb search occurring withing BODY will search tags in all modes.
770 This temporarily sets `semanticdb-match-any-mode' while executing BODY."
771 `(let ((semanticdb-match-any-mode t))
773 (put 'semanticdb-with-match-any-mode 'lisp-indent-function 0)
775 (defmethod semanticdb-equivalent-mode-for-search (table &optional buffer)
776 "Return non-nil if TABLE's mode is equivalent to BUFFER.
777 See `semanticdb-equivalent-mode' for details.
778 This version is used during searches. Major-modes that opt
779 to set the `semantic-match-any-mode' property will be able to search
780 all files of any type."
781 (or (get major-mode 'semantic-match-any-mode)
782 semanticdb-match-any-mode
783 (semanticdb-equivalent-mode table buffer))
786 (defmethod semanticdb-equivalent-mode ((table semanticdb-abstract-table) &optional buffer)
787 "Return non-nil if TABLE's mode is equivalent to BUFFER.
788 Equivalent modes are specified by the `semantic-equivalent-major-modes'
792 (defmethod semanticdb-equivalent-mode ((table semanticdb-table) &optional buffer)
793 "Return non-nil if TABLE's mode is equivalent to BUFFER.
794 Equivalent modes are specified by the `semantic-equivalent-major-modes'
797 (if buffer (set-buffer buffer))
799 ;; nil major mode in table means we don't know yet. Assume yes for now?
800 (null (oref table major-mode))
801 ;; nil means the same as major-mode
802 (and (not semantic-equivalent-major-modes)
803 (mode-local-use-bindings-p major-mode (oref table major-mode)))
804 (and semantic-equivalent-major-modes
805 (member (oref table major-mode) semantic-equivalent-major-modes))
812 ;; These routines determine associations between a file, and multiple
813 ;; associated databases.
815 (defcustom semanticdb-project-roots nil
816 "*List of directories, where each directory is the root of some project.
817 All subdirectories of a root project are considered a part of one project.
818 Values in this string can be overridden by project management programs
819 via the `semanticdb-project-root-functions' variable."
821 :type '(repeat string))
823 (defvar semanticdb-project-root-functions nil
824 "List of functions used to determine a given directories project root.
825 Functions in this variable can override `semanticdb-project-roots'.
826 Functions set in the variable are given one argument (a directory) and
827 must return a string, (the root directory) or a list of strings (multiple
828 root directories in a more complex system). This variable should be used
829 by project management programs like EDE or JDE.")
831 (defvar semanticdb-project-system-databases nil
832 "List of databases containing system library information.
833 Mode authors can create their own system databases which know
834 detailed information about the system libraries for querying purposes.
835 Put those into this variable as a buffer-local, or mode-local
837 (make-variable-buffer-local 'semanticdb-project-system-databases)
839 (defvar semanticdb-search-system-databases t
840 "Non nil if search routines are to include a system database.")
842 (defun semanticdb-current-database-list (&optional dir)
843 "Return a list of databases associated with the current buffer.
844 If optional argument DIR is non-nil, then use DIR as the starting directory.
845 If this buffer has a database, but doesn't have a project associated
847 First, it checks `semanticdb-project-root-functions', and if that
848 has no results, it checks `semanticdb-project-roots'. If that fails,
849 it returns the results of function `semanticdb-current-database'.
850 Always append `semanticdb-project-system-databases' if
851 `semanticdb-search-system' is non-nil."
852 (let ((root nil) ; found root directory
853 (dbs nil) ; collected databases
854 (roots semanticdb-project-roots) ;all user roots
855 (dir (file-truename (or dir default-directory)))
857 ;; Find the root based on project functions.
858 (setq root (run-hook-with-args-until-success
859 'semanticdb-project-root-functions
862 (setq root (file-truename root))
863 ;; Else, Find roots based on strings
865 (let ((r (file-truename (car roots))))
866 (if (string-match (concat "^" (regexp-quote r)) dir)
868 (setq roots (cdr roots))))
870 ;; If no roots are found, use this directory.
871 (unless root (setq root dir))
873 ;; Find databases based on the root directory.
875 ;; The rootlist allows the root functions to possibly
876 ;; return several roots which are in different areas but
877 ;; all apart of the same system.
878 (let ((regexp (concat "^" (regexp-quote root)))
879 (adb semanticdb-database-list) ; all databases
882 ;; I don't like this part, but close enough.
883 (if (and (slot-boundp (car adb) 'reference-directory)
884 (string-match regexp (oref (car adb) reference-directory)))
885 (setq dbs (cons (car adb) dbs)))
886 (setq adb (cdr adb))))
888 ;; Add in system databases
889 (when semanticdb-search-system-databases
890 (setq dbs (nconc dbs semanticdb-project-system-databases)))
895 ;;; Generic Accessor Routines
897 ;; These routines can be used to get at tags in files w/out
898 ;; having to know a lot about semanticDB.
899 (defvar semanticdb-file-table-hash (make-hash-table :test 'equal)
900 "Hash table mapping file names to database tables.")
902 (defun semanticdb-file-table-object-from-hash (file)
903 "Retrieve a DB table from the hash for FILE.
904 Does not use `file-truename'."
905 (gethash file semanticdb-file-table-hash 'no-hit))
907 (defun semanticdb-file-table-object-put-hash (file dbtable)
908 "For FILE, associate DBTABLE in the hash table."
909 (puthash file dbtable semanticdb-file-table-hash))
912 (defun semanticdb-file-table-object (file &optional dontload)
913 "Return a semanticdb table belonging to FILE, make it up to date.
914 If file has database tags available in the database, return it.
915 If file does not have tags available, and DONTLOAD is nil,
916 then load the tags for FILE, and create a new table object for it.
917 DONTLOAD does not affect the creation of new database objects."
918 ;; (message "Object Translate: %s" file)
919 (when (and file (file-exists-p file) (file-regular-p file))
920 (let* ((default-directory (file-name-directory file))
921 (tab (semanticdb-file-table-object-from-hash file))
924 ;; If it is not in the cache, then extract the more traditional
925 ;; way by getting the database, and finding a table in that database.
926 ;; Once we have a table, add it to the hash.
927 (when (eq tab 'no-hit)
928 (setq fullfile (file-truename file))
929 (let ((db (or ;; This line will pick up system databases.
930 (semanticdb-directory-loaded-p default-directory)
931 ;; this line will make a new one if needed.
932 (semanticdb-get-database default-directory))))
933 (setq tab (semanticdb-file-table db fullfile))
935 (semanticdb-file-table-object-put-hash file tab)
936 (when (not (string= fullfile file))
937 (semanticdb-file-table-object-put-hash fullfile tab)
943 ;; Is this in a buffer?
944 ;;(find-buffer-visiting (semanticdb-full-filename tab))
945 (semanticdb-in-buffer-p tab)
948 ;;(set-buffer (find-buffer-visiting (semanticdb-full-filename tab)))
949 (semanticdb-set-buffer tab)
950 (semantic-fetch-tags)
954 ;; If we have table, and we don't want to load it, just return it.
957 ;; Is table fully loaded, or just a proxy?
958 (number-or-marker-p (oref tab pointmax))
959 ;; Is this table up to date with the file?
960 (not (semanticdb-needs-refresh-p tab)))
963 ((or (and fullfile (get-file-buffer fullfile))
964 (get-file-buffer file))
965 ;; are these two calls this faster than `find-buffer-visiting'?
967 ;; If FILE is being visited, but none of the above state is
968 ;; true (meaning, there is no table object associated with it)
969 ;; then it is a file not supported by Semantic, and can be safely
972 ((not dontload) ;; We must load the file.
973 ;; Full file should have been set by now. Debug why not?
974 (when (and (not tab) (not fullfile))
975 ;; This case is if a 'nil is erroneously put into the hash table. This
977 (setq fullfile (file-truename file))
980 ;; If we have a table, but no fullfile, that's ok. Let's get the filename
981 ;; from the table which is pre-truenamed.
982 (when (and (not fullfile) tab)
983 (setq fullfile (semanticdb-full-filename tab)))
985 (setq tab (semanticdb-create-table-for-file-not-in-buffer fullfile))
987 ;; Save the new table.
988 (semanticdb-file-table-object-put-hash file tab)
989 (when (not (string= fullfile file))
990 (semanticdb-file-table-object-put-hash fullfile tab)
995 ;; Full file should have been set by now. Debug why not?
996 ;; One person found this. Is it a file that failed to parse
999 (setq fullfile (file-truename file)))
1001 ;; We were asked not to load the file in and parse it.
1002 ;; Instead just create a database table with no tags
1003 ;; and a claim of being empty.
1005 ;; This will give us a starting point for storing
1006 ;; database cross-references so when it is loaded,
1007 ;; the cross-references will fire and caches will
1009 (let ((ans (semanticdb-create-table-for-file file)))
1010 (setq tab (cdr ans))
1012 ;; Save the new table.
1013 (semanticdb-file-table-object-put-hash file tab)
1014 (when (not (string= fullfile file))
1015 (semanticdb-file-table-object-put-hash fullfile tab)
1022 (defvar semanticdb-out-of-buffer-create-table-fcn nil
1023 "When non-nil, a function for creating a semanticdb table.
1024 This should take a filename to be parsed.")
1025 (make-variable-buffer-local 'semanticdb-out-of-buffer-create-table-fcn)
1027 (defun semanticdb-create-table-for-file-not-in-buffer (filename)
1028 "Create a table for the file FILENAME.
1029 If there are no language specific configurations, this
1030 function will read in the buffer, parse it, and kill the buffer."
1031 (if (and semanticdb-out-of-buffer-create-table-fcn
1032 (not (file-remote-p filename)))
1033 ;; Use external parser only of the file is accessible to the
1034 ;; local file system.
1035 (funcall semanticdb-out-of-buffer-create-table-fcn filename)
1037 (let* ( ;; Remember the buffer to kill
1038 (kill-buffer-flag (find-buffer-visiting filename))
1039 (buffer-to-kill (or kill-buffer-flag
1040 (semantic-find-file-noselect filename t))))
1042 ;; This shouldn't ever be set. Debug some issue here?
1043 ;; (when kill-buffer-flag (debug))
1045 (set-buffer buffer-to-kill)
1046 ;; Find file should automatically do this for us.
1047 ;; Sometimes the DB table doesn't contains tags and needs
1048 ;; a refresh. For example, when the file is loaded for
1049 ;; the first time, and the idle scheduler didn't get a
1050 ;; chance to trigger a parse before the file buffer is
1052 (when semanticdb-current-table
1053 (semantic-fetch-tags))
1055 semanticdb-current-table
1056 (when (not kill-buffer-flag)
1057 ;; If we had to find the file, then we should kill it
1058 ;; to keep the master buffer list clean.
1059 (kill-buffer buffer-to-kill)
1063 (defun semanticdb-file-stream (file)
1064 "Return a list of tags belonging to FILE.
1065 If file has database tags available in the database, return them.
1066 If file does not have tags available, then load the file, and create them."
1067 (let ((table (semanticdb-file-table-object file)))
1069 (semanticdb-get-tags table))))
1071 (provide 'semantic/db)
1074 ;; generated-autoload-file: "loaddefs.el"
1075 ;; generated-autoload-load-name: "semantic/db"
1078 ;;; semantic/db.el ends here