]> code.delx.au - gnu-emacs/blob - lisp/cedet/semantic/db.el
Merge from origin/emacs-24
[gnu-emacs] / lisp / cedet / semantic / db.el
1 ;;; semantic/db.el --- Semantic tag database manager
2
3 ;; Copyright (C) 2000-2015 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 (cl-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 (cl-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 ;; 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 (cl-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))))
138
139 (cl-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)
143
144 (cl-defmethod semanticdb-dirty-p ((obj semanticdb-abstract-table))
145 "Return non-nil if OBJ is 'dirty'."
146 nil)
147
148 (cl-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)
154
155 (cl-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)
162
163 (cl-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).
166
167 The default is to just return (OBJ TAG).
168
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))
173
174 (cl-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 (cl-call-next-method obj (format " (%d tags)" (length (semanticdb-get-tags obj))))
182 ;; Pass through.
183 (apply 'call-next-method obj strings)
184 ))
185
186 ;;; Index Cache
187 ;;
188 (defclass semanticdb-abstract-search-index ()
189 ((table :initarg :table
190 :type semanticdb-abstract-table
191 :documentation "XRef to the table this belongs to.")
192 )
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)
197
198 (cl-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
208 ))
209 (oset obj index idx)
210 idx)))
211
212 (cl-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!
216 )
217
218 (cl-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!
222 )
223
224
225 ;;; SEARCH RESULTS TABLE
226 ;;
227 ;; Needed for system databases that may not provide
228 ;; a semanticdb-table associated with a file.
229 ;;
230 (defclass semanticdb-search-results-table (semanticdb-abstract-table)
231 ()
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.")
235
236 (cl-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)
240
241 ;;; CONCRETE TABLE CLASSES
242 ;;
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.")
278
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.")
285 )
286 "A single table of tags derived from file.")
287
288 (cl-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))))
295
296 (cl-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))))
304
305 (cl-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)))
309
310 (cl-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)))
314
315 (cl-defmethod semanticdb-dirty-p ((obj semanticdb-table))
316 "Return non-nil if OBJ is 'dirty'."
317 (oref obj dirty))
318
319 (cl-defmethod semanticdb-set-dirty ((obj semanticdb-table))
320 "Mark the abstract table OBJ dirty."
321 (oset obj dirty t)
322 )
323
324 (cl-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))))
330
331 ;;; DATABASE BASE CLASS
332 ;;
333 (unless (fboundp 'semanticdb-abstract-table-list-p)
334 (cl-deftype semanticdb-abstract-table-list ()
335 '(list-of semanticdb-abstract-table)))
336
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
344 :type class
345 :documentation
346 "New tables created for this database are of this class.")
347 (cache :type list
348 :initform nil
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'.
352
353 Using a semanticdb cache does not save any information to a file,
354 so your cache will need to be recalculated at runtime.
355
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.")
365
366 (cl-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."
369 nil)
370
371 (cl-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."
375 (let ((dirty nil)
376 (tabs (oref DB tables)))
377 (while (and (not dirty) tabs)
378 (setq dirty (semanticdb-dirty-p (car tabs)))
379 (setq tabs (cdr tabs)))
380 dirty))
381
382 (cl-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)
389 " DIRTY" "")
390 )
391 strings)))
392
393 (cl-defmethod semanticdb-create-database ((dbc (subclass 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)))
398 (unless db
399 (setq db (semanticdb-project-database
400 (file-name-nondirectory directory)
401 :tables nil))
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)))
405 db))
406
407 (cl-defmethod semanticdb-flush-database-tables ((db semanticdb-project-database))
408 "Reset the tables in DB to be empty."
409 (oset db tables nil))
410
411 (cl-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)))
417 (unless newtab
418 ;; This implementation will satisfy autoloaded classes
419 ;; for tables.
420 (setq newtab (funcall (oref db new-table-class)
421 (file-name-nondirectory file)
422 :file (file-name-nondirectory file)
423 ))
424 (oset newtab parent-db db)
425 (object-add-to-list db 'tables newtab t))
426 newtab))
427
428 (cl-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)))
433
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)))
439
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))
443
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."
448 (let ((cdb nil)
449 (tbl nil)
450 (dd (file-name-directory (file-truename filename)))
451 )
452 ;; Allow a database override function
453 (setq cdb (semanticdb-create-database semanticdb-new-database-class
454 dd))
455 ;; Get a table for this file.
456 (setq tbl (semanticdb-create-table cdb filename))
457
458 ;; Return the pair.
459 (cons cdb tbl)
460 ))
461
462 ;;; Cache Cache.
463 ;;
464 (defclass semanticdb-abstract-cache ()
465 ((table :initarg :table
466 :type semanticdb-abstract-table
467 :documentation
468 "Cross reference to the table this belongs to.")
469 )
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."
476 :abstract t)
477
478 (cl-defmethod semanticdb-cache-get ((table semanticdb-abstract-table)
479 desired-class)
480 "Get a cache object on TABLE of class DESIRED-CLASS.
481 This method will create one if none exists with no init arguments
482 other than :table."
483 (unless (child-of-class-p desired-class 'semanticdb-abstract-cache)
484 (error "Invalid SemanticDB cache"))
485 (let ((cache (oref table cache))
486 (obj nil))
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)))
491 (if obj
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)
496 obj)))
497
498 (cl-defmethod semanticdb-cache-remove ((table semanticdb-abstract-table)
499 cache)
500 "Remove from TABLE the cache object CACHE."
501 (object-remove-from-list table 'cache cache))
502
503 (cl-defmethod semanticdb-synchronize ((cache semanticdb-abstract-cache)
504 new-tags)
505 "Synchronize a CACHE with some NEW-TAGS."
506 ;; The abstract class will do... NOTHING!
507 )
508
509 (cl-defmethod semanticdb-partial-synchronize ((cache semanticdb-abstract-cache)
510 new-tags)
511 "Synchronize a CACHE with some changed NEW-TAGS."
512 ;; The abstract class will do... NOTHING!
513 )
514
515 (defclass semanticdb-abstract-db-cache ()
516 ((db :initarg :db
517 :type semanticdb-project-database
518 :documentation
519 "Cross reference to the database this belongs to.")
520 )
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."
527 :abstract t)
528
529 (cl-defmethod semanticdb-cache-get ((db semanticdb-project-database)
530 desired-class)
531 "Get a cache object on DB of class DESIRED-CLASS.
532 This method will create one if none exists with no init arguments
533 other than :table."
534 (unless (child-of-class-p desired-class 'semanticdb-abstract-cache)
535 (error "Invalid SemanticDB cache"))
536 (let ((cache (oref db cache))
537 (obj nil))
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)))
542 (if obj
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)
547 obj)))
548
549 (cl-defmethod semanticdb-cache-remove ((db semanticdb-project-database)
550 cache)
551 "Remove from TABLE the cache object CACHE."
552 (object-remove-from-list db 'cache cache))
553
554
555 (cl-defmethod semanticdb-synchronize ((cache semanticdb-abstract-db-cache)
556 new-tags)
557 "Synchronize a CACHE with some NEW-TAGS."
558 ;; The abstract class will do... NOTHING!
559 )
560
561 (cl-defmethod semanticdb-partial-synchronize ((cache semanticdb-abstract-db-cache)
562 new-tags)
563 "Synchronize a CACHE with some changed NEW-TAGS."
564 ;; The abstract class will do... NOTHING!
565 )
566
567 ;;; REFRESH
568
569 (cl-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."
575 (cond
576 ;;
577 ;; Already in a buffer, just do it.
578 ((semanticdb-in-buffer-p obj)
579 (save-excursion
580 (semanticdb-set-buffer obj)
581 (semantic-fetch-tags)))
582 ;;
583 ;; Not in a buffer. Forcing a load.
584 (force
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
588 (save-excursion
589 (let ((buff (semantic-find-file-noselect
590 (semanticdb-full-filename obj) t)))
591 (set-buffer buff)
592 (semantic-fetch-tags)
593 ;; Kill off the buffer if it didn't exist when we were called.
594 (kill-buffer buff))))))
595
596 (cl-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))
601 )
602 (if buff
603 (with-current-buffer buff
604 ;; Use semantic's magic tracker to determine of the buffer is up
605 ;; to date or not.
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
609 )
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))
615 )
616
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))
621 )
622 ))))
623
624 \f
625 ;;; Synchronization
626 ;;
627 (cl-defmethod semanticdb-synchronize ((table semanticdb-abstract-table)
628 new-tags)
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))
635 )
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)
643
644 ;; Synchronize the index
645 (when (slot-boundp table 'index)
646 (let ((idx (oref table index)))
647 (when idx (semanticdb-synchronize idx new-tags))))
648
649 ;; Synchronize application caches.
650 (dolist (C (oref table cache))
651 (semanticdb-synchronize C new-tags)
652 )
653
654 ;; Update cross references
655 (semanticdb-refresh-references table)
656 )
657
658 (cl-defmethod semanticdb-partial-synchronize ((table semanticdb-abstract-table)
659 new-tags)
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)
666
667 ;; The lexical table may be modified.
668 (when (featurep 'semantic/lex-spp)
669 (oset table lexical-table (semantic-lex-spp-save-table)))
670
671 ;; Incremental parser doesn't monkey around with this.
672 (oset table unmatched-syntax semantic-unmatched-syntax-cache)
673
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))))
678
679 ;; Synchronize application caches.
680 (dolist (C (oref table cache))
681 (semanticdb-synchronize C new-tags)
682 )
683
684 ;; Update cross references
685 (when (semantic-find-tags-by-class 'include new-tags)
686 (semanticdb-refresh-references table))
687 )
688
689 ;;; SAVE/LOAD
690 ;;
691 (cl-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
696 form."
697 nil)
698
699 (defun semanticdb-save-current-db ()
700 "Save the current tag database."
701 (interactive)
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")))
707
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)
711
712 (defun semanticdb-save-all-db ()
713 "Save all semantic tag databases."
714 (interactive)
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")))
721
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
727 (mapc (lambda (db)
728 (semantic-throw-on-input 'semanticdb-idle-save)
729 (semanticdb-save-db db t))
730 semanticdb-database-list))
731 ))
732
733 ;;; Directory Project support
734 ;;
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.
739
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.")
743
744 (cl-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."
747 nil)
748
749 ;;; Utilities
750 ;;
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
758 default-directory)
759 )
760 nil))
761
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.")
767
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))
772 ,@body))
773 (put 'semanticdb-with-match-any-mode 'lisp-indent-function 0)
774
775 (cl-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))
784 )
785
786 (cl-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'
789 local variable."
790 nil)
791
792 (cl-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'
795 local variable."
796 (save-excursion
797 (if buffer (set-buffer buffer))
798 (or
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))
806 )
807 ))
808
809
810 ;;; Associations
811 ;;
812 ;; These routines determine associations between a file, and multiple
813 ;; associated databases.
814
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."
820 :group 'semanticdb
821 :type '(repeat string))
822
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.")
830
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
836 value.")
837 (make-variable-buffer-local 'semanticdb-project-system-databases)
838
839 (defvar semanticdb-search-system-databases t
840 "Non nil if search routines are to include a system database.")
841
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
846 with it, return nil.
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)))
856 )
857 ;; Find the root based on project functions.
858 (setq root (run-hook-with-args-until-success
859 'semanticdb-project-root-functions
860 dir))
861 (if root
862 (setq root (file-truename root))
863 ;; Else, Find roots based on strings
864 (while roots
865 (let ((r (file-truename (car roots))))
866 (if (string-match (concat "^" (regexp-quote r)) dir)
867 (setq root r)))
868 (setq roots (cdr roots))))
869
870 ;; If no roots are found, use this directory.
871 (unless root (setq root dir))
872
873 ;; Find databases based on the root directory.
874 (when root
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
880 )
881 (while adb
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))))
887 )
888 ;; Add in system databases
889 (when semanticdb-search-system-databases
890 (setq dbs (nconc dbs semanticdb-project-system-databases)))
891 ;; Return
892 dbs))
893
894 \f
895 ;;; Generic Accessor Routines
896 ;;
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.")
901
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))
906
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))
910
911 ;;;###autoload
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))
922 (fullfile nil))
923
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))
934 (when tab
935 (semanticdb-file-table-object-put-hash file tab)
936 (when (not (string= fullfile file))
937 (semanticdb-file-table-object-put-hash fullfile tab)
938 ))
939 ))
940
941 (cond
942 ((and tab
943 ;; Is this in a buffer?
944 ;;(find-buffer-visiting (semanticdb-full-filename tab))
945 (semanticdb-in-buffer-p tab)
946 )
947 (save-excursion
948 ;;(set-buffer (find-buffer-visiting (semanticdb-full-filename tab)))
949 (semanticdb-set-buffer tab)
950 (semantic-fetch-tags)
951 ;; Return the table.
952 tab))
953 ((and tab dontload)
954 ;; If we have table, and we don't want to load it, just return it.
955 tab)
956 ((and tab
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)))
961 ;; A-ok!
962 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'?
966
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
970 ;; ignored.
971 nil)
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
976 ;; would need fixing
977 (setq fullfile (file-truename file))
978 )
979
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)))
984
985 (setq tab (semanticdb-create-table-for-file-not-in-buffer fullfile))
986
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)
991 )
992 ;; Done!
993 tab)
994 (t
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
997 ;; in the past?
998 (when (not fullfile)
999 (setq fullfile (file-truename file)))
1000
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.
1004 ;;
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
1008 ;; be cleaned.
1009 (let ((ans (semanticdb-create-table-for-file file)))
1010 (setq tab (cdr ans))
1011
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)
1016 )
1017 ;; Done!
1018 tab))
1019 )
1020 )))
1021
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)
1026
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)
1036 (save-excursion
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))))
1041
1042 ;; This shouldn't ever be set. Debug some issue here?
1043 ;; (when kill-buffer-flag (debug))
1044
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
1051 ;; killed.
1052 (when semanticdb-current-table
1053 (semantic-fetch-tags))
1054 (prog1
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)
1060 )))))
1061 )
1062
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)))
1068 (when table
1069 (semanticdb-get-tags table))))
1070
1071 (provide 'semantic/db)
1072
1073 ;; Local variables:
1074 ;; generated-autoload-file: "loaddefs.el"
1075 ;; generated-autoload-load-name: "semantic/db"
1076 ;; End:
1077
1078 ;;; semantic/db.el ends here