Prefer HTTPS to FTP and HTTP in documentation
[emacs.git] / lisp / cedet / semantic / db-mode.el
blob8072ca9e69c19345ea36f74e4b2d92689a252f7c
1 ;;; semantic/db-mode.el --- Semanticdb Minor Mode
3 ;; Copyright (C) 2008-2017 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 <https://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)
43 "List of hooks and values to add/remove when configuring semanticdb.")
45 ;;; SEMANTICDB-MODE
47 ;;;###autoload
48 (defun semanticdb-minor-mode-p ()
49 "Return non-nil if `semanticdb-minor-mode' is active."
50 (member (car (car semanticdb-hooks))
51 (symbol-value (car (cdr (car semanticdb-hooks))))))
53 ;;;###autoload
54 (define-minor-mode global-semanticdb-minor-mode
55 "Toggle Semantic DB mode.
56 With ARG, turn Semantic DB mode on if ARG is positive, off otherwise.
58 In Semantic DB mode, Semantic parsers store results in a
59 database, which can be saved for future Emacs sessions."
60 :global t
61 :group 'semantic
62 (if global-semanticdb-minor-mode
63 ;; Enable
64 (dolist (elt semanticdb-hooks)
65 (add-hook (cadr elt) (car elt)))
66 ;; Disable
67 (dolist (elt semanticdb-hooks)
68 (remove-hook (cadr elt) (car elt)))))
70 (defvaralias 'semanticdb-mode-hook 'global-semanticdb-minor-mode-hook)
71 (defvaralias 'semanticdb-global-mode 'global-semanticdb-minor-mode)
72 (semantic-varalias-obsolete 'semanticdb-mode-hooks
73 'global-semanticdb-minor-mode-hook "23.2")
76 (defun semanticdb-toggle-global-mode ()
77 "Toggle use of the Semantic Database feature.
78 Update the environment of Semantic enabled buffers accordingly."
79 (interactive)
80 (if (semanticdb-minor-mode-p)
81 ;; Save databases before disabling semanticdb.
82 (semanticdb-save-all-db))
83 ;; Toggle semanticdb minor mode.
84 (global-semanticdb-minor-mode 'toggle))
86 ;;; Hook Functions:
88 ;; Functions used in hooks to keep SemanticDB operating.
90 (defun semanticdb-semantic-init-hook-fcn ()
91 "Function saved in `semantic-init-db-hook'.
92 Sets up the semanticdb environment."
93 ;; Only initialize semanticdb if we have a file name.
94 ;; There is no reason to cache a tag table if there is no
95 ;; way to load it back in later.
96 (when (buffer-file-name)
97 (let* ((ans (semanticdb-create-table-for-file (buffer-file-name)))
98 (cdb (car ans))
99 (ctbl (cdr ans))
101 ;; Get the current DB for this directory
102 (setq semanticdb-current-database cdb)
103 ;; We set the major mode because we know what it is.
104 (oset ctbl major-mode major-mode)
105 ;; Local state
106 (setq semanticdb-current-table ctbl)
107 (oset ctbl buffer (current-buffer))
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))
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)
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 ;; Bind into the buffer.
137 (semantic--tag-link-cache-to-buffer)
141 (defun semanticdb-revert-hook ()
142 "Hook run before a revert buffer.
143 We can't track incremental changes due to a revert, so just clear the cache.
144 This will prevent the next batch of hooks from wasting time parsing things
145 that don't need to be parsed."
146 (if (and (semantic-active-p)
147 semantic--buffer-cache
148 semanticdb-current-table)
149 (semantic-clear-toplevel-cache)))
151 (defun semanticdb-kill-hook ()
152 "Function run when a buffer is killed.
153 If there is a semantic cache, slurp out the overlays, and store
154 it in our database. If that buffer has no cache, ignore it, we'll
155 handle it later if need be."
156 (when (and (semantic-active-p)
157 semantic--buffer-cache
158 semanticdb-current-table)
160 ;; Try to get a fast update.
161 (semantic-fetch-tags-fast)
163 ;; If the buffer is in a bad state, don't save anything...
164 (if (semantic-parse-tree-needs-rebuild-p)
165 ;; If this is the case, don't save anything.
166 (progn
167 (semantic-clear-toplevel-cache)
168 (oset semanticdb-current-table pointmax 0)
169 (oset semanticdb-current-table fsize 0)
170 (oset semanticdb-current-table lastmodtime nil)
172 ;; We have a clean buffer, save it off.
173 (condition-case nil
174 (progn
175 (semantic--tag-unlink-cache-from-buffer)
176 ;; Set pointmax only if we had some success in the unlink.
177 (oset semanticdb-current-table pointmax (point-max))
178 (let ((fattr (file-attributes
179 (semanticdb-full-filename
180 semanticdb-current-table))))
181 (oset semanticdb-current-table fsize (nth 7 fattr))
182 (oset semanticdb-current-table lastmodtime (nth 5 fattr))
183 (oset semanticdb-current-table buffer nil)
185 ;; If this messes up, just clear the system
186 (error
187 (semantic-clear-toplevel-cache)
188 (message "semanticdb: Failed to deoverlay tag cache.")))
192 (defun semanticdb-kill-emacs-hook ()
193 "Function called when Emacs is killed.
194 Save all the databases."
195 (semanticdb-save-all-db))
197 ;;; SYNCHRONIZATION HOOKS
199 (defun semanticdb-synchronize-table (new-table)
200 "Function run after parsing.
201 Argument NEW-TABLE is the new table of tags."
202 (when semanticdb-current-table
203 (semanticdb-synchronize semanticdb-current-table new-table)))
205 (defun semanticdb-partial-synchronize-table (new-table)
206 "Function run after parsing.
207 Argument NEW-TABLE is the new table of tags."
208 (when semanticdb-current-table
209 (semanticdb-partial-synchronize semanticdb-current-table new-table)))
212 (provide 'semantic/db-mode)
214 ;; Local variables:
215 ;; generated-autoload-file: "loaddefs.el"
216 ;; generated-autoload-load-name: "semantic/db-mode"
217 ;; End:
219 ;;; semantic/db-mode.el ends here