Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / cedet / semantic / db-file.el
blob785b5c704d9914201af226a1fca5a10853000bd5
1 ;;; semantic/db-file.el --- Save a semanticdb to a cache file.
3 ;;; Copyright (C) 2000-2005, 2007-2014 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Keywords: tags
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; A set of semanticdb classes for persistently saving caches on disk.
28 (require 'semantic/db)
29 (require 'cedet-files)
30 (require 'data-debug)
32 (defvar semanticdb-file-version semantic-version
33 "Version of semanticdb we are writing files to disk with.")
34 (defvar semanticdb-file-incompatible-version "1.4"
35 "Version of semanticdb we are not reverse compatible with.")
37 ;;; Settings
39 (defcustom semanticdb-default-file-name "semantic.cache"
40 "File name of the semantic tag cache."
41 :group 'semanticdb
42 :type 'string)
44 (defcustom semanticdb-default-save-directory
45 (locate-user-emacs-file "semanticdb" ".semanticdb")
46 "Directory name where semantic cache files are stored.
47 By default, it is either ~/.emacs.d/semanticdb, or ~/.semanticdb depending
48 on which exists.
49 If this value is nil, files are saved in the current directory. If the value
50 is a valid directory, then it overrides `semanticdb-default-file-name' and
51 stores caches in a coded file name in this directory."
52 :group 'semanticdb
53 :type '(choice :tag "Default-Directory"
54 :menu-tag "Default-Directory"
55 (const :tag "Use current directory" :value nil)
56 (directory)))
58 (defcustom semanticdb-persistent-path '(always)
59 "List of valid paths that semanticdb will cache tags to.
60 When `global-semanticdb-minor-mode' is active, tag lists will
61 be saved to disk when Emacs exits. Not all directories will have
62 tags that should be saved.
63 The value should be a list of valid paths. A path can be a string,
64 indicating a directory in which to save a variable. An element in the
65 list can also be a symbol. Valid symbols are `never', which will
66 disable any saving anywhere, `always', which enables saving
67 everywhere, or `project', which enables saving in any directory that
68 passes a list of predicates in `semanticdb-project-predicate-functions'."
69 :group 'semanticdb
70 :type nil)
72 (define-obsolete-variable-alias 'semanticdb-save-database-hooks
73 'semanticdb-save-database-functions "24.3")
74 (defcustom semanticdb-save-database-functions nil
75 "Abnormal hook run after a database is saved.
76 Each function is called with one argument, the object representing
77 the database recently written."
78 :group 'semanticdb
79 :type 'hook)
81 (defvar semanticdb-dir-sep-char (if (boundp 'directory-sep-char)
82 (symbol-value 'directory-sep-char)
83 ?/)
84 "Character used for directory separation.
85 Obsoleted in some versions of Emacs. Needed in others.
86 NOTE: This should get deleted from semantic soon.")
88 (defun semanticdb-fix-pathname (dir)
89 "If DIR is broken, fix it.
90 Force DIR to end with a /.
91 Note: Same as `file-name-as-directory'.
92 NOTE: This should get deleted from semantic soon."
93 (file-name-as-directory dir))
94 ;; I didn't initially know about the above fcn. Keep the below as a
95 ;; reference. Delete it someday once I've proven everything is the same.
96 ;; (if (not (= semanticdb-dir-sep-char (aref path (1- (length path)))))
97 ;; (concat path (list semanticdb-dir-sep-char))
98 ;; path))
100 ;;; Classes
102 ;;;###autoload
103 (defclass semanticdb-project-database-file (semanticdb-project-database
104 eieio-persistent)
105 ((file-header-line :initform ";; SEMANTICDB Tags save file")
106 (do-backups :initform nil)
107 (semantic-tag-version :initarg :semantic-tag-version
108 :initform "1.4"
109 :documentation
110 "The version of the tags saved.
111 The default value is 1.4. In semantic 1.4 there was no versioning, so
112 when those files are loaded, this becomes the version number.
113 To save the version number, we must hand-set this version string.")
114 (semanticdb-version :initarg :semanticdb-version
115 :initform "1.4"
116 :documentation
117 "The version of the object system saved.
118 The default value is 1.4. In semantic 1.4, there was no versioning,
119 so when those files are loaded, this becomes the version number.
120 To save the version number, we must hand-set this version string.")
122 "Database of file tables saved to disk.")
124 ;;; Code:
126 (defmethod semanticdb-create-database :STATIC ((dbc semanticdb-project-database-file)
127 directory)
128 "Create a new semantic database for DIRECTORY and return it.
129 If a database for DIRECTORY has already been loaded, return it.
130 If a database for DIRECTORY exists, then load that database, and return it.
131 If DIRECTORY doesn't exist, create a new one."
132 ;; Make sure this is fully expanded so we don't get duplicates.
133 (setq directory (file-truename directory))
134 (let* ((fn (semanticdb-cache-filename dbc directory))
135 (db (or (semanticdb-file-loaded-p fn)
136 (if (file-exists-p fn)
137 (progn
138 (semanticdb-load-database fn))))))
139 (unless db
140 (setq db (make-instance
141 dbc ; Create the database requested. Perhaps
142 (concat (file-name-nondirectory
143 (directory-file-name
144 directory))
145 "/")
146 :file fn :tables nil
147 :semantic-tag-version semantic-tag-version
148 :semanticdb-version semanticdb-file-version)))
149 ;; Set this up here. We can't put it in the constructor because it
150 ;; would be saved, and we want DB files to be portable.
151 (oset db reference-directory directory)
152 db))
154 ;;; File IO
156 (declare-function inversion-test "inversion")
158 (defun semanticdb-load-database (filename)
159 "Load the database FILENAME."
160 (condition-case foo
161 (let* ((r (eieio-persistent-read filename semanticdb-project-database-file))
162 (c (semanticdb-get-database-tables r))
163 (tv (oref r semantic-tag-version))
164 (fv (oref r semanticdb-version))
166 ;; Restore the parent-db connection
167 (while c
168 (oset (car c) parent-db r)
169 (setq c (cdr c)))
170 (unless (and (equal semanticdb-file-version fv)
171 (equal semantic-tag-version tv))
172 ;; Try not to load inversion unless we need it:
173 (require 'inversion)
174 (if (not (inversion-test 'semanticdb-file fv))
175 (when (inversion-test 'semantic-tag tv)
176 ;; Incompatible version. Flush tables.
177 (semanticdb-flush-database-tables r)
178 ;; Reset the version to new version.
179 (oset r semantic-tag-version semantic-tag-version)
180 ;; Warn user
181 (message "Semanticdb file is old. Starting over for %s"
182 filename))
183 ;; Version is not ok. Flush whole system
184 (message "semanticdb file is old. Starting over for %s"
185 filename)
186 ;; This database is so old, we need to replace it.
187 ;; We also need to delete it from the instance tracker.
188 (delete-instance r)
189 (setq r nil)))
191 (error (message "Cache Error: [%s] %s, Restart"
192 filename foo)
193 nil)))
195 (defun semanticdb-file-loaded-p (filename)
196 "Return the project belonging to FILENAME if it was already loaded."
197 (eieio-instance-tracker-find filename 'file 'semanticdb-database-list))
199 (defmethod semanticdb-file-directory-exists-p ((DB semanticdb-project-database-file)
200 &optional suppress-questions)
201 "Does the directory the database DB needs to write to exist?
202 If SUPPRESS-QUESTIONS, then do not ask to create the directory."
203 (let ((dest (file-name-directory (oref DB file)))
205 (cond ((null dest)
206 ;; @TODO - If it was never set up... what should we do ?
207 nil)
208 ((file-exists-p dest) t)
209 ((or suppress-questions
210 (and (boundp 'semanticdb--inhibit-make-directory)
211 semanticdb--inhibit-make-directory))
212 nil)
213 ((y-or-n-p (format "Create directory %s for SemanticDB? " dest))
214 (make-directory dest t)
217 (if (boundp 'semanticdb--inhibit-make-directory)
218 (setq semanticdb--inhibit-make-directory t))
219 nil))))
221 (defmethod semanticdb-save-db ((DB semanticdb-project-database-file)
222 &optional
223 suppress-questions)
224 "Write out the database DB to its file.
225 If DB is not specified, then use the current database."
226 (let ((objname (oref DB file)))
227 (when (and (semanticdb-dirty-p DB)
228 (semanticdb-live-p DB)
229 (semanticdb-file-directory-exists-p DB suppress-questions)
230 (semanticdb-write-directory-p DB)
232 ;;(message "Saving tag summary for %s..." objname)
233 (condition-case foo
234 (eieio-persistent-save (or DB semanticdb-current-database))
235 (file-error ; System error saving? Ignore it.
236 (message "%S: %s" foo objname))
237 (error
238 (cond
239 ((and (listp foo)
240 (stringp (nth 1 foo))
241 (string-match "write[- ]protected" (nth 1 foo)))
242 (message (nth 1 foo)))
243 ((and (listp foo)
244 (stringp (nth 1 foo))
245 (string-match "no such directory" (nth 1 foo)))
246 (message (nth 1 foo)))
248 ;; @todo - It should ask if we are not called from a hook.
249 ;; How?
250 (if (or suppress-questions
251 (y-or-n-p (format "Skip Error: %s ?" (car (cdr foo)))))
252 (message "Save Error: %S: %s" (car (cdr foo))
253 objname)
254 (error "%S" (car (cdr foo))))))))
255 (run-hook-with-args 'semanticdb-save-database-functions
256 (or DB semanticdb-current-database))
257 ;;(message "Saving tag summary for %s...done" objname)
261 (defmethod semanticdb-live-p ((obj semanticdb-project-database))
262 "Return non-nil if the file associated with OBJ is live.
263 Live databases are objects associated with existing directories."
264 (and (slot-boundp obj 'reference-directory)
265 (file-exists-p (oref obj reference-directory))))
267 (defmethod semanticdb-live-p ((obj semanticdb-table))
268 "Return non-nil if the file associated with OBJ is live.
269 Live files are either buffers in Emacs, or files existing on the filesystem."
270 (let ((full-filename (semanticdb-full-filename obj)))
271 (or (find-buffer-visiting full-filename)
272 (file-exists-p full-filename))))
274 (defvar semanticdb-data-debug-on-write-error nil
275 "Run the data debugger on tables that issue errors.
276 This variable is set to nil after the first error is encountered
277 to prevent overload.")
279 (declare-function data-debug-insert-thing "data-debug")
281 (defmethod object-write ((obj semanticdb-table))
282 "When writing a table, we have to make sure we deoverlay it first.
283 Restore the overlays after writing.
284 Argument OBJ is the object to write."
285 (when (semanticdb-live-p obj)
286 (when (semanticdb-in-buffer-p obj)
287 (with-current-buffer (semanticdb-in-buffer-p obj)
289 ;; Make sure all our tag lists are up to date.
290 (semantic-fetch-tags)
292 ;; Try to get an accurate unmatched syntax table.
293 (when (and (boundp semantic-show-unmatched-syntax-mode)
294 semantic-show-unmatched-syntax-mode)
295 ;; Only do this if the user runs unmatched syntax
296 ;; mode display entries.
297 (oset obj unmatched-syntax
298 (semantic-show-unmatched-lex-tokens-fetch))
301 ;; Make sure pointmax is up to date
302 (oset obj pointmax (point-max))
305 ;; Make sure that the file size and other attributes are
306 ;; up to date.
307 (let ((fattr (file-attributes (semanticdb-full-filename obj))))
308 (oset obj fsize (nth 7 fattr))
309 (oset obj lastmodtime (nth 5 fattr))
312 ;; Do it!
313 (condition-case tableerror
314 (call-next-method)
315 (error
316 (when semanticdb-data-debug-on-write-error
317 (require 'data-debug)
318 (data-debug-new-buffer (concat "*SEMANTICDB ERROR*"))
319 (data-debug-insert-thing obj "*" "")
320 (setq semanticdb-data-debug-on-write-error nil))
321 (message "Error Writing Table: %s" (eieio-object-name obj))
322 (error "%S" (car (cdr tableerror)))))
324 ;; Clear the dirty bit.
325 (oset obj dirty nil)
328 ;;; State queries
330 (defmethod semanticdb-write-directory-p ((obj semanticdb-project-database-file))
331 "Return non-nil if OBJ should be written to disk.
332 Uses `semanticdb-persistent-path' to determine the return value."
333 (let ((path semanticdb-persistent-path))
334 (catch 'found
335 (while path
336 (cond ((stringp (car path))
337 (if (string= (oref obj reference-directory) (car path))
338 (throw 'found t)))
339 ((eq (car path) 'project)
340 ;; @TODO - EDE causes us to go in here and disable
341 ;; the old default 'always save' setting.
343 ;; With new default 'always' should I care?
344 (if semanticdb-project-predicate-functions
345 (if (run-hook-with-args-until-success
346 'semanticdb-project-predicate-functions
347 (oref obj reference-directory))
348 (throw 'found t))
349 ;; If the mode is 'project, and there are no project
350 ;; modes, then just always save the file. If users
351 ;; wish to restrict the search, modify
352 ;; `semanticdb-persistent-path' to include desired paths.
353 (if (= (length semanticdb-persistent-path) 1)
354 (throw 'found t))
356 ((eq (car path) 'never)
357 (throw 'found nil))
358 ((eq (car path) 'always)
359 (throw 'found t))
360 (t (error "Invalid path %S" (car path))))
361 (setq path (cdr path)))
362 (call-next-method))
365 ;;; Filename manipulation
367 (defmethod semanticdb-file-table ((obj semanticdb-project-database-file) filename)
368 "From OBJ, return FILENAME's associated table object."
369 ;; Cheater option. In this case, we always have files directly
370 ;; under ourselves. The main project type may not.
371 (object-assoc (file-name-nondirectory filename) 'file (oref obj tables)))
373 (defmethod semanticdb-file-name-non-directory :STATIC
374 ((dbclass semanticdb-project-database-file))
375 "Return the file name DBCLASS will use.
376 File name excludes any directory part."
377 semanticdb-default-file-name)
379 (defmethod semanticdb-file-name-directory :STATIC
380 ((dbclass semanticdb-project-database-file) directory)
381 "Return the relative directory to where DBCLASS will save its cache file.
382 The returned path is related to DIRECTORY."
383 (if semanticdb-default-save-directory
384 (let ((file (cedet-directory-name-to-file-name directory)))
385 ;; Now create a filename for the cache file in
386 ;; ;`semanticdb-default-save-directory'.
387 (expand-file-name
388 file (file-name-as-directory semanticdb-default-save-directory)))
389 directory))
391 (defmethod semanticdb-cache-filename :STATIC
392 ((dbclass semanticdb-project-database-file) path)
393 "For DBCLASS, return a file to a cache file belonging to PATH.
394 This could be a cache file in the current directory, or an encoded file
395 name in a secondary directory."
396 ;; Use concat and not expand-file-name, because the dir part
397 ;; may include some of the file name.
398 (concat (semanticdb-file-name-directory dbclass path)
399 (semanticdb-file-name-non-directory dbclass)))
401 (defmethod semanticdb-full-filename ((obj semanticdb-project-database-file))
402 "Fetch the full filename that OBJ refers to."
403 (oref obj file))
405 ;;; FLUSH OLD FILES
407 (defun semanticdb-cleanup-cache-files (&optional noerror)
408 "Cleanup any cache files associated with directories that no longer exist.
409 Optional NOERROR prevents errors from being displayed."
410 (interactive)
411 (when (and (not semanticdb-default-save-directory)
412 (not noerror))
413 (error "No default save directory for semantic-save files"))
415 (when semanticdb-default-save-directory
417 ;; Calculate all the cache files we have.
418 (let* ((regexp (regexp-quote semanticdb-default-file-name))
419 (files (directory-files semanticdb-default-save-directory
420 t regexp))
421 (orig nil)
422 (to-delete nil))
423 (dolist (F files)
424 (setq orig (cedet-file-name-to-directory-name
425 (file-name-nondirectory F)))
426 (when (not (file-exists-p (file-name-directory orig)))
427 (setq to-delete (cons F to-delete))
429 (if to-delete
430 (save-window-excursion
431 (let ((buff (get-buffer-create "*Semanticdb Delete*")))
432 (with-current-buffer buff
433 (erase-buffer)
434 (insert "The following Cache files appear to be obsolete.\n\n")
435 (dolist (F to-delete)
436 (insert F "\n")))
437 (pop-to-buffer buff t t)
438 (fit-window-to-buffer (get-buffer-window buff) nil 1)
439 (when (y-or-n-p "Delete Old Cache Files? ")
440 (mapc (lambda (F)
441 (message "Deleting to %s..." F)
442 (delete-file F))
443 to-delete)
444 (message "done."))
446 ;; No files to delete
447 (when (not noerror)
448 (message "No obsolete semanticdb.cache files."))
449 ))))
451 (provide 'semantic/db-file)
453 ;; Local variables:
454 ;; generated-autoload-file: "loaddefs.el"
455 ;; generated-autoload-load-name: "semantic/db-file"
456 ;; End:
458 ;;; semantic/db-file.el ends here