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