db-mode.el 7.5 KB

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