Add 2012 to FSF copyright years for Emacs files (do not merge to trunk)
[bpt/emacs.git] / lisp / cedet / semantic / db-mode.el
CommitLineData
dbce5569
CY
1;;; semantic/db-mode.el --- Semanticdb Minor Mode
2
49f70d46 3;; Copyright (C) 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
dbce5569
CY
4
5;; Author: Eric M. Ludlam <eric@siege-engine.com>
6
7;; This file is part of GNU Emacs.
8
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.
13
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.
18
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/>.
21
22;;; Commentary:
23;;
24;; Major mode for managing Semantic Databases automatically.
25
dbce5569 26;;; Code:
dbce5569 27
b90caf50 28(require 'semantic/db)
691a065e
CY
29
30(declare-function semantic-lex-spp-set-dynamic-table "semantic/lex-spp")
dbce5569 31
dbce5569
CY
32;;; Start/Stop database use
33;;
34(defvar semanticdb-hooks
29e1a603 35 '((semanticdb-semantic-init-hook-fcn semantic-init-db-hook)
dbce5569
CY
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)
43 )
44 "List of hooks and values to add/remove when configuring semanticdb.")
45
46;;; SEMANTICDB-MODE
47;;
996bc9bf 48;;;###autoload
dbce5569
CY
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))))))
53
d046becf 54;;;###autoload
a2095e2e
CY
55(define-minor-mode global-semanticdb-minor-mode
56 "Toggle Semantic DB mode.
57With ARG, turn Semantic DB mode on if ARG is positive, off otherwise.
58
59In Semantic DB mode, Semantic parsers store results in a
60database, 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 (add-hook (cadr elt) (car elt)))))
70
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
eefa91db 74 'global-semanticdb-minor-mode-hook "23.2")
a2095e2e 75
dbce5569
CY
76
77(defun semanticdb-toggle-global-mode ()
78 "Toggle use of the Semantic Database feature.
79Update 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))
86
87;;; Hook Functions:
88;;
89;; Functions used in hooks to keep SemanticDB operating.
90;;
91(defun semanticdb-semantic-init-hook-fcn ()
29e1a603 92 "Function saved in `semantic-init-db-hook'.
dbce5569
CY
93Sets 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))
101 )
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 ;; Try to swap in saved tags
109 (if (or (not (slot-boundp ctbl 'tags)) (not (oref ctbl tags))
110 (/= (or (oref ctbl pointmax) 0) (point-max))
111 )
112 (semantic-clear-toplevel-cache)
113 ;; Unmatched syntax
114 (condition-case nil
115 (semantic-set-unmatched-syntax-cache
116 (oref ctbl unmatched-syntax))
117 (unbound-slot
118 ;; Old version of the semanticdb table can miss the unmatched
119 ;; syntax slot. If so, just clear the unmatched syntax cache.
120 (semantic-clear-unmatched-syntax-cache)
121 ;; Make sure it has a value.
122 (oset ctbl unmatched-syntax nil)
123 ))
124 ;; Keep lexical tables up to date. Don't load
125 ;; semantic-spp if it isn't needed.
126 (let ((lt (oref ctbl lexical-table)))
127 (when lt
128 (require 'semantic/lex-spp)
129 (semantic-lex-spp-set-dynamic-table lt)))
130 ;; Set the main tag cache.
131 ;; This must happen after setting up buffer local variables
132 ;; since this will turn around and re-save those variables.
133 (semantic--set-buffer-cache (oref ctbl tags))
134 ;; Don't need it to be dirty. Set dirty due to hooks from above.
135 (oset ctbl dirty nil) ;; Special case here.
136 (oset ctbl buffer (current-buffer))
137 ;; Bind into the buffer.
138 (semantic--tag-link-cache-to-buffer)
139 )
140 )))
141
142(defun semanticdb-revert-hook ()
143 "Hook run before a revert buffer.
144We can't track incremental changes due to a revert, so just clear the cache.
145This will prevent the next batch of hooks from wasting time parsing things
146that 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)))
151
152(defun semanticdb-kill-hook ()
153 "Function run when a buffer is killed.
154If there is a semantic cache, slurp out the overlays, and store
155it in our database. If that buffer has no cache, ignore it, we'll
156handle it later if need be."
157 (when (and (semantic-active-p)
158 semantic--buffer-cache
159 semanticdb-current-table)
160
161 ;; Try to get a fast update.
162 (semantic-fetch-tags-fast)
163
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)
172 )
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)
185 ))
186 ;; If this messes up, just clear the system
187 (error
188 (semantic-clear-toplevel-cache)
189 (message "semanticdb: Failed to deoverlay tag cache.")))
190 )
191 ))
192
193(defun semanticdb-kill-emacs-hook ()
194 "Function called when Emacs is killed.
195Save all the databases."
196 (semanticdb-save-all-db))
197
198;;; SYNCHRONIZATION HOOKS
199;;
200(defun semanticdb-synchronize-table (new-table)
201 "Function run after parsing.
202Argument NEW-TABLE is the new table of tags."
203 (when semanticdb-current-table
204 (semanticdb-synchronize semanticdb-current-table new-table)))
205
206(defun semanticdb-partial-synchronize-table (new-table)
207 "Function run after parsing.
208Argument NEW-TABLE is the new table of tags."
209 (when semanticdb-current-table
210 (semanticdb-partial-synchronize semanticdb-current-table new-table)))
211
212
213(provide 'semantic/db-mode)
214
996bc9bf
CY
215;; Local variables:
216;; generated-autoload-file: "loaddefs.el"
996bc9bf
CY
217;; generated-autoload-load-name: "semantic/db-mode"
218;; End:
219
3999968a 220;; arch-tag: f5da903d-2d34-4adf-8572-e60340e1ad59
dbce5569 221;;; semantic/db-mode.el ends here