]> code.delx.au - gnu-emacs/blob - lisp/cedet/semantic/db.el
Update copyright year to 2014 by running admin/update-copyright.
[gnu-emacs] / lisp / cedet / semantic / db.el
1 ;;; semantic/db.el --- Semantic tag database manager
2
3 ;; Copyright (C) 2000-2014 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 (eieio-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 (eieio--object-class (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 (eieio--object-class (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 (save-excursion
564 (semanticdb-set-buffer obj)
565 (semantic-fetch-tags)))
566 ;;
567 ;; Not in a buffer. Forcing a load.
568 (force
569 ;; Patch from Iain Nicol. --
570 ;; @TODO: I wonder if there is a way to recycle
571 ;; semanticdb-create-table-for-file-not-in-buffer
572 (save-excursion
573 (let ((buff (semantic-find-file-noselect
574 (semanticdb-full-filename obj) t)))
575 (set-buffer buff)
576 (semantic-fetch-tags)
577 ;; Kill off the buffer if it didn't exist when we were called.
578 (kill-buffer buff))))))
579
580 (defmethod semanticdb-needs-refresh-p ((obj semanticdb-table))
581 "Return non-nil of OBJ's tag list is out of date.
582 The file associated with OBJ does not need to be in a buffer."
583 (let* ((ff (semanticdb-full-filename obj))
584 (buff (semanticdb-in-buffer-p obj))
585 )
586 (if buff
587 (with-current-buffer buff
588 ;; Use semantic's magic tracker to determine of the buffer is up
589 ;; to date or not.
590 (not (semantic-parse-tree-up-to-date-p))
591 ;; We assume that semanticdb is keeping itself up to date.
592 ;; via all the clever hooks
593 )
594 ;; Buffer isn't loaded. The only clue we have is if the file
595 ;; is somehow different from our mark in the semanticdb table.
596 (let* ((stats (file-attributes ff))
597 (actualsize (nth 7 stats))
598 (actualmod (nth 5 stats))
599 )
600
601 (or (not (slot-boundp obj 'tags))
602 ;; (not (oref obj tags)) --> not needed anymore?
603 (/= (or (oref obj fsize) 0) actualsize)
604 (not (equal (oref obj lastmodtime) actualmod))
605 )
606 ))))
607
608 \f
609 ;;; Synchronization
610 ;;
611 (defmethod semanticdb-synchronize ((table semanticdb-abstract-table)
612 new-tags)
613 "Synchronize the table TABLE with some NEW-TAGS."
614 (oset table tags new-tags)
615 (oset table pointmax (point-max))
616 (let ((fattr (file-attributes (semanticdb-full-filename table))))
617 (oset table fsize (nth 7 fattr))
618 (oset table lastmodtime (nth 5 fattr))
619 )
620 ;; Assume it is now up to date.
621 (oset table unmatched-syntax semantic-unmatched-syntax-cache)
622 ;; The lexical table should be good too.
623 (when (featurep 'semantic/lex-spp)
624 (oset table lexical-table (semantic-lex-spp-save-table)))
625 ;; this implies dirtiness
626 (semanticdb-set-dirty table)
627
628 ;; Synchronize the index
629 (when (slot-boundp table 'index)
630 (let ((idx (oref table index)))
631 (when idx (semanticdb-synchronize idx new-tags))))
632
633 ;; Synchronize application caches.
634 (dolist (C (oref table cache))
635 (semanticdb-synchronize C new-tags)
636 )
637
638 ;; Update cross references
639 (semanticdb-refresh-references table)
640 )
641
642 (defmethod semanticdb-partial-synchronize ((table semanticdb-abstract-table)
643 new-tags)
644 "Synchronize the table TABLE where some NEW-TAGS changed."
645 ;; You might think we need to reset the tags, but since the partial
646 ;; parser splices the lists, we don't need to do anything
647 ;;(oset table tags new-tags)
648 ;; We do need to mark ourselves dirty.
649 (semanticdb-set-dirty table)
650
651 ;; The lexical table may be modified.
652 (when (featurep 'semantic/lex-spp)
653 (oset table lexical-table (semantic-lex-spp-save-table)))
654
655 ;; Incremental parser doesn't monkey around with this.
656 (oset table unmatched-syntax semantic-unmatched-syntax-cache)
657
658 ;; Synchronize the index
659 (when (slot-boundp table 'index)
660 (let ((idx (oref table index)))
661 (when idx (semanticdb-partial-synchronize idx new-tags))))
662
663 ;; Synchronize application caches.
664 (dolist (C (oref table cache))
665 (semanticdb-synchronize C new-tags)
666 )
667
668 ;; Update cross references
669 (when (semantic-find-tags-by-class 'include new-tags)
670 (semanticdb-refresh-references table))
671 )
672
673 ;;; SAVE/LOAD
674 ;;
675 (defmethod semanticdb-save-db ((DB semanticdb-project-database)
676 &optional suppress-questions)
677 "Cause a database to save itself.
678 The database base class does not save itself persistently.
679 Subclasses could save themselves to a file, or to a database, or other
680 form."
681 nil)
682
683 (defun semanticdb-save-current-db ()
684 "Save the current tag database."
685 (interactive)
686 (unless noninteractive
687 (message "Saving current tag summaries..."))
688 (semanticdb-save-db semanticdb-current-database)
689 (unless noninteractive
690 (message "Saving current tag summaries...done")))
691
692 ;; This prevents Semanticdb from querying multiple times if the users
693 ;; answers "no" to creating the Semanticdb directory.
694 (defvar semanticdb--inhibit-create-file-directory)
695
696 (defun semanticdb-save-all-db ()
697 "Save all semantic tag databases."
698 (interactive)
699 (unless noninteractive
700 (message "Saving tag summaries..."))
701 (let ((semanticdb--inhibit-make-directory noninteractive))
702 (mapc 'semanticdb-save-db semanticdb-database-list))
703 (unless noninteractive
704 (message "Saving tag summaries...done")))
705
706 (defun semanticdb-save-all-db-idle ()
707 "Save all semantic tag databases from idle time.
708 Exit the save between databases if there is user input."
709 (semantic-safe "Auto-DB Save: %S"
710 (semantic-exit-on-input 'semanticdb-idle-save
711 (mapc (lambda (db)
712 (semantic-throw-on-input 'semanticdb-idle-save)
713 (semanticdb-save-db db t))
714 semanticdb-database-list))
715 ))
716
717 ;;; Directory Project support
718 ;;
719 (defvar semanticdb-project-predicate-functions nil
720 "List of predicates to try that indicate a directory belongs to a project.
721 This list is used when `semanticdb-persistent-path' contains the value
722 'project. If the predicate list is nil, then presume all paths are valid.
723
724 Project Management software (such as EDE and JDE) should add their own
725 predicates with `add-hook' to this variable, and semanticdb will save tag
726 caches in directories controlled by them.")
727
728 (defmethod semanticdb-write-directory-p ((obj semanticdb-project-database))
729 "Return non-nil if OBJ should be written to disk.
730 Uses `semanticdb-persistent-path' to determine the return value."
731 nil)
732
733 ;;; Utilities
734 ;;
735 ;; What is the current database, are two tables of an equivalent mode,
736 ;; and what databases are a part of the same project.
737 (defun semanticdb-current-database ()
738 "Return the currently active database."
739 (or semanticdb-current-database
740 (and default-directory
741 (semanticdb-create-database semanticdb-new-database-class
742 default-directory)
743 )
744 nil))
745
746 (defvar semanticdb-match-any-mode nil
747 "Non-nil to temporarily search any major mode for a tag.
748 If a particular major mode wants to search any mode, put the
749 `semantic-match-any-mode' symbol onto the symbol of that major mode.
750 Do not set the value of this variable permanently.")
751
752 (defmacro semanticdb-with-match-any-mode (&rest body)
753 "A Semanticdb search occurring withing BODY will search tags in all modes.
754 This temporarily sets `semanticdb-match-any-mode' while executing BODY."
755 `(let ((semanticdb-match-any-mode t))
756 ,@body))
757 (put 'semanticdb-with-match-any-mode 'lisp-indent-function 0)
758
759 (defmethod semanticdb-equivalent-mode-for-search (table &optional buffer)
760 "Return non-nil if TABLE's mode is equivalent to BUFFER.
761 See `semanticdb-equivalent-mode' for details.
762 This version is used during searches. Major-modes that opt
763 to set the `semantic-match-any-mode' property will be able to search
764 all files of any type."
765 (or (get major-mode 'semantic-match-any-mode)
766 semanticdb-match-any-mode
767 (semanticdb-equivalent-mode table buffer))
768 )
769
770 (defmethod semanticdb-equivalent-mode ((table semanticdb-abstract-table) &optional buffer)
771 "Return non-nil if TABLE's mode is equivalent to BUFFER.
772 Equivalent modes are specified by the `semantic-equivalent-major-modes'
773 local variable."
774 nil)
775
776 (defmethod semanticdb-equivalent-mode ((table semanticdb-table) &optional buffer)
777 "Return non-nil if TABLE's mode is equivalent to BUFFER.
778 Equivalent modes are specified by the `semantic-equivalent-major-modes'
779 local variable."
780 (save-excursion
781 (if buffer (set-buffer buffer))
782 (or
783 ;; nil major mode in table means we don't know yet. Assume yes for now?
784 (null (oref table major-mode))
785 ;; nil means the same as major-mode
786 (and (not semantic-equivalent-major-modes)
787 (mode-local-use-bindings-p major-mode (oref table major-mode)))
788 (and semantic-equivalent-major-modes
789 (member (oref table major-mode) semantic-equivalent-major-modes))
790 )
791 ))
792
793
794 ;;; Associations
795 ;;
796 ;; These routines determine associations between a file, and multiple
797 ;; associated databases.
798
799 (defcustom semanticdb-project-roots nil
800 "*List of directories, where each directory is the root of some project.
801 All subdirectories of a root project are considered a part of one project.
802 Values in this string can be overridden by project management programs
803 via the `semanticdb-project-root-functions' variable."
804 :group 'semanticdb
805 :type '(repeat string))
806
807 (defvar semanticdb-project-root-functions nil
808 "List of functions used to determine a given directories project root.
809 Functions in this variable can override `semanticdb-project-roots'.
810 Functions set in the variable are given one argument (a directory) and
811 must return a string, (the root directory) or a list of strings (multiple
812 root directories in a more complex system). This variable should be used
813 by project management programs like EDE or JDE.")
814
815 (defvar semanticdb-project-system-databases nil
816 "List of databases containing system library information.
817 Mode authors can create their own system databases which know
818 detailed information about the system libraries for querying purposes.
819 Put those into this variable as a buffer-local, or mode-local
820 value.")
821 (make-variable-buffer-local 'semanticdb-project-system-databases)
822
823 (defvar semanticdb-search-system-databases t
824 "Non nil if search routines are to include a system database.")
825
826 (defun semanticdb-current-database-list (&optional dir)
827 "Return a list of databases associated with the current buffer.
828 If optional argument DIR is non-nil, then use DIR as the starting directory.
829 If this buffer has a database, but doesn't have a project associated
830 with it, return nil.
831 First, it checks `semanticdb-project-root-functions', and if that
832 has no results, it checks `semanticdb-project-roots'. If that fails,
833 it returns the results of function `semanticdb-current-database'.
834 Always append `semanticdb-project-system-databases' if
835 `semanticdb-search-system' is non-nil."
836 (let ((root nil) ; found root directory
837 (dbs nil) ; collected databases
838 (roots semanticdb-project-roots) ;all user roots
839 (dir (file-truename (or dir default-directory)))
840 )
841 ;; Find the root based on project functions.
842 (setq root (run-hook-with-args-until-success
843 'semanticdb-project-root-functions
844 dir))
845 (if root
846 (setq root (file-truename root))
847 ;; Else, Find roots based on strings
848 (while roots
849 (let ((r (file-truename (car roots))))
850 (if (string-match (concat "^" (regexp-quote r)) dir)
851 (setq root r)))
852 (setq roots (cdr roots))))
853
854 ;; If no roots are found, use this directory.
855 (unless root (setq root dir))
856
857 ;; Find databases based on the root directory.
858 (when root
859 ;; The rootlist allows the root functions to possibly
860 ;; return several roots which are in different areas but
861 ;; all apart of the same system.
862 (let ((regexp (concat "^" (regexp-quote root)))
863 (adb semanticdb-database-list) ; all databases
864 )
865 (while adb
866 ;; I don't like this part, but close enough.
867 (if (and (slot-boundp (car adb) 'reference-directory)
868 (string-match regexp (oref (car adb) reference-directory)))
869 (setq dbs (cons (car adb) dbs)))
870 (setq adb (cdr adb))))
871 )
872 ;; Add in system databases
873 (when semanticdb-search-system-databases
874 (setq dbs (nconc dbs semanticdb-project-system-databases)))
875 ;; Return
876 dbs))
877
878 \f
879 ;;; Generic Accessor Routines
880 ;;
881 ;; These routines can be used to get at tags in files w/out
882 ;; having to know a lot about semanticDB.
883 (defvar semanticdb-file-table-hash (make-hash-table :test 'equal)
884 "Hash table mapping file names to database tables.")
885
886 (defun semanticdb-file-table-object-from-hash (file)
887 "Retrieve a DB table from the hash for FILE.
888 Does not use `file-truename'."
889 (gethash file semanticdb-file-table-hash 'no-hit))
890
891 (defun semanticdb-file-table-object-put-hash (file dbtable)
892 "For FILE, associate DBTABLE in the hash table."
893 (puthash file dbtable semanticdb-file-table-hash))
894
895 ;;;###autoload
896 (defun semanticdb-file-table-object (file &optional dontload)
897 "Return a semanticdb table belonging to FILE, make it up to date.
898 If file has database tags available in the database, return it.
899 If file does not have tags available, and DONTLOAD is nil,
900 then load the tags for FILE, and create a new table object for it.
901 DONTLOAD does not affect the creation of new database objects."
902 ;; (message "Object Translate: %s" file)
903 (when (and file (file-exists-p file) (file-regular-p file))
904 (let* ((default-directory (file-name-directory file))
905 (tab (semanticdb-file-table-object-from-hash file))
906 (fullfile nil))
907
908 ;; If it is not in the cache, then extract the more traditional
909 ;; way by getting the database, and finding a table in that database.
910 ;; Once we have a table, add it to the hash.
911 (when (eq tab 'no-hit)
912 (setq fullfile (file-truename file))
913 (let ((db (or ;; This line will pick up system databases.
914 (semanticdb-directory-loaded-p default-directory)
915 ;; this line will make a new one if needed.
916 (semanticdb-get-database default-directory))))
917 (setq tab (semanticdb-file-table db fullfile))
918 (when tab
919 (semanticdb-file-table-object-put-hash file tab)
920 (when (not (string= fullfile file))
921 (semanticdb-file-table-object-put-hash fullfile tab)
922 ))
923 ))
924
925 (cond
926 ((and tab
927 ;; Is this in a buffer?
928 ;;(find-buffer-visiting (semanticdb-full-filename tab))
929 (semanticdb-in-buffer-p tab)
930 )
931 (save-excursion
932 ;;(set-buffer (find-buffer-visiting (semanticdb-full-filename tab)))
933 (semanticdb-set-buffer tab)
934 (semantic-fetch-tags)
935 ;; Return the table.
936 tab))
937 ((and tab dontload)
938 ;; If we have table, and we don't want to load it, just return it.
939 tab)
940 ((and tab
941 ;; Is table fully loaded, or just a proxy?
942 (number-or-marker-p (oref tab pointmax))
943 ;; Is this table up to date with the file?
944 (not (semanticdb-needs-refresh-p tab)))
945 ;; A-ok!
946 tab)
947 ((or (and fullfile (get-file-buffer fullfile))
948 (get-file-buffer file))
949 ;; are these two calls this faster than `find-buffer-visiting'?
950
951 ;; If FILE is being visited, but none of the above state is
952 ;; true (meaning, there is no table object associated with it)
953 ;; then it is a file not supported by Semantic, and can be safely
954 ;; ignored.
955 nil)
956 ((not dontload) ;; We must load the file.
957 ;; Full file should have been set by now. Debug why not?
958 (when (and (not tab) (not fullfile))
959 ;; This case is if a 'nil is erroneously put into the hash table. This
960 ;; would need fixing
961 (setq fullfile (file-truename file))
962 )
963
964 ;; If we have a table, but no fullfile, that's ok. Let's get the filename
965 ;; from the table which is pre-truenamed.
966 (when (and (not fullfile) tab)
967 (setq fullfile (semanticdb-full-filename tab)))
968
969 (setq tab (semanticdb-create-table-for-file-not-in-buffer fullfile))
970
971 ;; Save the new table.
972 (semanticdb-file-table-object-put-hash file tab)
973 (when (not (string= fullfile file))
974 (semanticdb-file-table-object-put-hash fullfile tab)
975 )
976 ;; Done!
977 tab)
978 (t
979 ;; Full file should have been set by now. Debug why not?
980 ;; One person found this. Is it a file that failed to parse
981 ;; in the past?
982 (when (not fullfile)
983 (setq fullfile (file-truename file)))
984
985 ;; We were asked not to load the file in and parse it.
986 ;; Instead just create a database table with no tags
987 ;; and a claim of being empty.
988 ;;
989 ;; This will give us a starting point for storing
990 ;; database cross-references so when it is loaded,
991 ;; the cross-references will fire and caches will
992 ;; be cleaned.
993 (let ((ans (semanticdb-create-table-for-file file)))
994 (setq tab (cdr ans))
995
996 ;; Save the new table.
997 (semanticdb-file-table-object-put-hash file tab)
998 (when (not (string= fullfile file))
999 (semanticdb-file-table-object-put-hash fullfile tab)
1000 )
1001 ;; Done!
1002 tab))
1003 )
1004 )))
1005
1006 (defvar semanticdb-out-of-buffer-create-table-fcn nil
1007 "When non-nil, a function for creating a semanticdb table.
1008 This should take a filename to be parsed.")
1009 (make-variable-buffer-local 'semanticdb-out-of-buffer-create-table-fcn)
1010
1011 (defun semanticdb-create-table-for-file-not-in-buffer (filename)
1012 "Create a table for the file FILENAME.
1013 If there are no language specific configurations, this
1014 function will read in the buffer, parse it, and kill the buffer."
1015 (if (and semanticdb-out-of-buffer-create-table-fcn
1016 (not (file-remote-p filename)))
1017 ;; Use external parser only of the file is accessible to the
1018 ;; local file system.
1019 (funcall semanticdb-out-of-buffer-create-table-fcn filename)
1020 (save-excursion
1021 (let* ( ;; Remember the buffer to kill
1022 (kill-buffer-flag (find-buffer-visiting filename))
1023 (buffer-to-kill (or kill-buffer-flag
1024 (semantic-find-file-noselect filename t))))
1025
1026 ;; This shouldn't ever be set. Debug some issue here?
1027 ;; (when kill-buffer-flag (debug))
1028
1029 (set-buffer buffer-to-kill)
1030 ;; Find file should automatically do this for us.
1031 ;; Sometimes the DB table doesn't contains tags and needs
1032 ;; a refresh. For example, when the file is loaded for
1033 ;; the first time, and the idle scheduler didn't get a
1034 ;; chance to trigger a parse before the file buffer is
1035 ;; killed.
1036 (when semanticdb-current-table
1037 (semantic-fetch-tags))
1038 (prog1
1039 semanticdb-current-table
1040 (when (not kill-buffer-flag)
1041 ;; If we had to find the file, then we should kill it
1042 ;; to keep the master buffer list clean.
1043 (kill-buffer buffer-to-kill)
1044 )))))
1045 )
1046
1047 (defun semanticdb-file-stream (file)
1048 "Return a list of tags belonging to FILE.
1049 If file has database tags available in the database, return them.
1050 If file does not have tags available, then load the file, and create them."
1051 (let ((table (semanticdb-file-table-object file)))
1052 (when table
1053 (semanticdb-get-tags table))))
1054
1055 (provide 'semantic/db)
1056
1057 ;; Local variables:
1058 ;; generated-autoload-file: "loaddefs.el"
1059 ;; generated-autoload-load-name: "semantic/db"
1060 ;; End:
1061
1062 ;;; semantic/db.el ends here