Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / cedet / semantic / db-mode.el
blobd57dd188659885c6a19435552a85ebf1c0f199f3
1 ;;; semantic/db-mode.el --- Semanticdb Minor Mode
3 ;; Copyright (C) 2008-2014 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <eric@siege-engine.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; Major mode for managing Semantic Databases automatically.
26 ;;; Code:
28 (require 'semantic/db)
30 (declare-function semantic-lex-spp-set-dynamic-table "semantic/lex-spp")
32 ;;; Start/Stop database use
34 (defvar semanticdb-hooks
35 '((semanticdb-semantic-init-hook-fcn semantic-init-db-hook)
36 (semanticdb-synchronize-table semantic-after-toplevel-cache-change-hook)
37 (semanticdb-partial-synchronize-table semantic-after-partial-cache-change-hook)
38 (semanticdb-revert-hook before-revert-hook)
39 (semanticdb-kill-hook kill-buffer-hook)
40 (semanticdb-kill-hook change-major-mode-hook) ;; Not really a kill, but we need the same effect.
41 (semanticdb-kill-emacs-hook kill-emacs-hook)
42 (semanticdb-save-all-db-idle auto-save-hook)
44 "List of hooks and values to add/remove when configuring semanticdb.")
46 ;;; SEMANTICDB-MODE
48 ;;;###autoload
49 (defun semanticdb-minor-mode-p ()
50 "Return non-nil if `semanticdb-minor-mode' is active."
51 (member (car (car semanticdb-hooks))
52 (symbol-value (car (cdr (car semanticdb-hooks))))))
54 ;;;###autoload
55 (define-minor-mode global-semanticdb-minor-mode
56 "Toggle Semantic DB mode.
57 With ARG, turn Semantic DB mode on if ARG is positive, off otherwise.
59 In Semantic DB mode, Semantic parsers store results in a
60 database, which can be saved for future Emacs sessions."
61 :global t
62 :group 'semantic
63 (if global-semanticdb-minor-mode
64 ;; Enable
65 (dolist (elt semanticdb-hooks)
66 (add-hook (cadr elt) (car elt)))
67 ;; Disable
68 (dolist (elt semanticdb-hooks)
69 (remove-hook (cadr elt) (car elt)))))
71 (defvaralias 'semanticdb-mode-hook 'global-semanticdb-minor-mode-hook)
72 (defvaralias 'semanticdb-global-mode 'global-semanticdb-minor-mode)
73 (semantic-varalias-obsolete 'semanticdb-mode-hooks
74 'global-semanticdb-minor-mode-hook "23.2")
77 (defun semanticdb-toggle-global-mode ()
78 "Toggle use of the Semantic Database feature.
79 Update the environment of Semantic enabled buffers accordingly."
80 (interactive)
81 (if (semanticdb-minor-mode-p)
82 ;; Save databases before disabling semanticdb.
83 (semanticdb-save-all-db))
84 ;; Toggle semanticdb minor mode.
85 (global-semanticdb-minor-mode 'toggle))
87 ;;; Hook Functions:
89 ;; Functions used in hooks to keep SemanticDB operating.
91 (defun semanticdb-semantic-init-hook-fcn ()
92 "Function saved in `semantic-init-db-hook'.
93 Sets up the semanticdb environment."
94 ;; Only initialize semanticdb if we have a file name.
95 ;; There is no reason to cache a tag table if there is no
96 ;; way to load it back in later.
97 (when (buffer-file-name)
98 (let* ((ans (semanticdb-create-table-for-file (buffer-file-name)))
99 (cdb (car ans))
100 (ctbl (cdr ans))
102 ;; Get the current DB for this directory
103 (setq semanticdb-current-database cdb)
104 ;; We set the major mode because we know what it is.
105 (oset ctbl major-mode major-mode)
106 ;; Local state
107 (setq semanticdb-current-table ctbl)
108 (oset ctbl buffer (current-buffer))
109 ;; Try to swap in saved tags
110 (if (or (not (slot-boundp ctbl 'tags)) (not (oref ctbl tags))
111 (/= (or (oref ctbl pointmax) 0) (point-max))
113 (semantic-clear-toplevel-cache)
114 ;; Unmatched syntax
115 (condition-case nil
116 (semantic-set-unmatched-syntax-cache
117 (oref ctbl unmatched-syntax))
118 (unbound-slot
119 ;; Old version of the semanticdb table can miss the unmatched
120 ;; syntax slot. If so, just clear the unmatched syntax cache.
121 (semantic-clear-unmatched-syntax-cache)
122 ;; Make sure it has a value.
123 (oset ctbl unmatched-syntax nil)
125 ;; Keep lexical tables up to date. Don't load
126 ;; semantic-spp if it isn't needed.
127 (let ((lt (oref ctbl lexical-table)))
128 (when lt
129 (require 'semantic/lex-spp)
130 (semantic-lex-spp-set-dynamic-table lt)))
131 ;; Set the main tag cache.
132 ;; This must happen after setting up buffer local variables
133 ;; since this will turn around and re-save those variables.
134 (semantic--set-buffer-cache (oref ctbl tags))
135 ;; Don't need it to be dirty. Set dirty due to hooks from above.
136 (oset ctbl dirty nil) ;; Special case here.
137 ;; Bind into the buffer.
138 (semantic--tag-link-cache-to-buffer)
142 (defun semanticdb-revert-hook ()
143 "Hook run before a revert buffer.
144 We can't track incremental changes due to a revert, so just clear the cache.
145 This will prevent the next batch of hooks from wasting time parsing things
146 that don't need to be parsed."
147 (if (and (semantic-active-p)
148 semantic--buffer-cache
149 semanticdb-current-table)
150 (semantic-clear-toplevel-cache)))
152 (defun semanticdb-kill-hook ()
153 "Function run when a buffer is killed.
154 If there is a semantic cache, slurp out the overlays, and store
155 it in our database. If that buffer has no cache, ignore it, we'll
156 handle it later if need be."
157 (when (and (semantic-active-p)
158 semantic--buffer-cache
159 semanticdb-current-table)
161 ;; Try to get a fast update.
162 (semantic-fetch-tags-fast)
164 ;; If the buffer is in a bad state, don't save anything...
165 (if (semantic-parse-tree-needs-rebuild-p)
166 ;; If this is the case, don't save anything.
167 (progn
168 (semantic-clear-toplevel-cache)
169 (oset semanticdb-current-table pointmax 0)
170 (oset semanticdb-current-table fsize 0)
171 (oset semanticdb-current-table lastmodtime nil)
173 ;; We have a clean buffer, save it off.
174 (condition-case nil
175 (progn
176 (semantic--tag-unlink-cache-from-buffer)
177 ;; Set pointmax only if we had some success in the unlink.
178 (oset semanticdb-current-table pointmax (point-max))
179 (let ((fattr (file-attributes
180 (semanticdb-full-filename
181 semanticdb-current-table))))
182 (oset semanticdb-current-table fsize (nth 7 fattr))
183 (oset semanticdb-current-table lastmodtime (nth 5 fattr))
184 (oset semanticdb-current-table buffer nil)
186 ;; If this messes up, just clear the system
187 (error
188 (semantic-clear-toplevel-cache)
189 (message "semanticdb: Failed to deoverlay tag cache.")))
193 (defun semanticdb-kill-emacs-hook ()
194 "Function called when Emacs is killed.
195 Save all the databases."
196 (semanticdb-save-all-db))
198 ;;; SYNCHRONIZATION HOOKS
200 (defun semanticdb-synchronize-table (new-table)
201 "Function run after parsing.
202 Argument NEW-TABLE is the new table of tags."
203 (when semanticdb-current-table
204 (semanticdb-synchronize semanticdb-current-table new-table)))
206 (defun semanticdb-partial-synchronize-table (new-table)
207 "Function run after parsing.
208 Argument NEW-TABLE is the new table of tags."
209 (when semanticdb-current-table
210 (semanticdb-partial-synchronize semanticdb-current-table new-table)))
213 (provide 'semantic/db-mode)
215 ;; Local variables:
216 ;; generated-autoload-file: "loaddefs.el"
217 ;; generated-autoload-load-name: "semantic/db-mode"
218 ;; End:
220 ;;; semantic/db-mode.el ends here