semantic.el 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. ;;; semantic.el --- Semantic buffer evaluator.
  2. ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  4. ;; Keywords: syntax tools
  5. ;; Version: 2.0
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;
  19. ;; API for providing the semantic content of a buffer.
  20. ;;
  21. ;; The Semantic API provides an interface to a series of different parser
  22. ;; implementations. Each parser outputs a parse tree in a similar format
  23. ;; designed to handle typical functional and object oriented languages.
  24. ;;
  25. ;; To enable Semantic, turn on `semantic-mode', a global minor mode
  26. ;; (M-x semantic-mode RET, or "Source Code Parsers" from the Tools
  27. ;; menu). To enable it at startup, put (semantic-mode 1) in your init
  28. ;; file.
  29. (require 'cedet)
  30. (require 'semantic/tag)
  31. (require 'semantic/lex)
  32. (defvar semantic-version "2.0"
  33. "Current version of Semantic.")
  34. (declare-function inversion-test "inversion")
  35. (declare-function semanticdb-load-ebrowse-caches "semantic/db-ebrowse")
  36. (defun semantic-require-version (major minor &optional beta)
  37. "Non-nil if this version of Semantic does not satisfy a specific version.
  38. Arguments can be:
  39. (MAJOR MINOR &optional BETA)
  40. Values MAJOR and MINOR must be integers. BETA can be an integer, or
  41. excluded if a released version is required.
  42. It is assumed that if the current version is newer than that specified,
  43. everything passes. Exceptions occur when known incompatibilities are
  44. introduced."
  45. (require 'inversion)
  46. (inversion-test 'semantic
  47. (concat major "." minor
  48. (when beta (concat "beta" beta)))))
  49. (defgroup semantic nil
  50. "Parser Generator and parser framework."
  51. :group 'tools)
  52. (defgroup semantic-faces nil
  53. "Faces used for Semantic enabled tools."
  54. :group 'semantic)
  55. (require 'semantic/fw)
  56. ;;; Code:
  57. ;;
  58. ;;; Variables and Configuration
  59. ;;
  60. (defvar semantic--parse-table nil
  61. "Variable that defines how to parse top level items in a buffer.
  62. This variable is for internal use only, and its content depends on the
  63. external parser used.")
  64. (make-variable-buffer-local 'semantic--parse-table)
  65. (semantic-varalias-obsolete 'semantic-toplevel-bovine-table
  66. 'semantic--parse-table "23.2")
  67. (defvar semantic-symbol->name-assoc-list
  68. '((type . "Types")
  69. (variable . "Variables")
  70. (function . "Functions")
  71. (include . "Dependencies")
  72. (package . "Provides"))
  73. "Association between symbols returned, and a string.
  74. The string is used to represent a group of objects of the given type.
  75. It is sometimes useful for a language to use a different string
  76. in place of the default, even though that language will still
  77. return a symbol. For example, Java return's includes, but the
  78. string can be replaced with `Imports'.")
  79. (make-variable-buffer-local 'semantic-symbol->name-assoc-list)
  80. (defvar semantic-symbol->name-assoc-list-for-type-parts nil
  81. "Like `semantic-symbol->name-assoc-list' for type parts.
  82. Some tags that have children (see `semantic-tag-children-compatibility')
  83. will want to define the names of classes of tags differently than at
  84. the top level. For example, in C++, a Function may be called a
  85. Method. In addition, there may be new types of tags that exist only
  86. in classes, such as protection labels.")
  87. (make-variable-buffer-local 'semantic-symbol->name-assoc-list-for-type-parts)
  88. (defvar semantic-case-fold nil
  89. "Value for `case-fold-search' when parsing.")
  90. (make-variable-buffer-local 'semantic-case-fold)
  91. (defvar semantic-expand-nonterminal nil
  92. "Function to call for each nonterminal production.
  93. Return a list of non-terminals derived from the first argument, or nil
  94. if it does not need to be expanded.
  95. Languages with compound definitions should use this function to expand
  96. from one compound symbol into several. For example, in C the definition
  97. int a, b;
  98. is easily parsed into one tag. This function should take this
  99. compound tag and turn it into two tags, one for A, and the other for B.")
  100. (make-variable-buffer-local 'semantic-expand-nonterminal)
  101. (defvar semantic--buffer-cache nil
  102. "A cache of the fully parsed buffer.
  103. If no significant changes have been made (based on the state) then
  104. this is returned instead of re-parsing the buffer.
  105. DO NOT USE THIS VARIABLE IN PROGRAMS.
  106. If you need a tag list, use `semantic-fetch-tags'. If you need the
  107. cached values for some reason, chances are you can add a hook to
  108. `semantic-after-toplevel-cache-change-hook'.")
  109. (make-variable-buffer-local 'semantic--buffer-cache)
  110. (semantic-varalias-obsolete 'semantic-toplevel-bovine-cache
  111. 'semantic--buffer-cache "23.2")
  112. (defvar semantic-unmatched-syntax-cache nil
  113. "A cached copy of unmatched syntax tokens.")
  114. (make-variable-buffer-local 'semantic-unmatched-syntax-cache)
  115. (defvar semantic-unmatched-syntax-cache-check nil
  116. "Non-nil if the unmatched syntax cache is out of date.
  117. This is tracked with `semantic-change-function'.")
  118. (make-variable-buffer-local 'semantic-unmatched-syntax-cache-check)
  119. (defvar semantic-edits-are-safe nil
  120. "When non-nil, modifications do not require a reparse.
  121. This prevents tags from being marked dirty, and it prevents top level
  122. edits from causing a cache check.
  123. Use this when writing programs that could cause a full reparse, but
  124. will not change the tag structure, such as adding or updating
  125. `top-level' comments.")
  126. (defvar semantic-unmatched-syntax-hook nil
  127. "Hooks run when Semantic detects syntax not matched in a grammar.
  128. Each individual piece of syntax (such as a symbol or punctuation
  129. character) is called with this hook when it doesn't match in the
  130. grammar, and multiple unmatched syntax elements are not grouped
  131. together. Each hook is called with one argument, which is a list
  132. of syntax tokens created by the semantic lexer. Use the functions
  133. `semantic-lex-token-start', `semantic-lex-token-end' and
  134. `semantic-lex-token-text' to get information about these tokens.
  135. The current buffer is the buffer these tokens are derived from.")
  136. (defvar semantic--before-fetch-tags-hook nil
  137. "Hooks run before a buffer is parsed for tags.
  138. It is called before any request for tags is made via the function
  139. `semantic-fetch-tags' by an application.
  140. If any hook returns a nil value, the cached value is returned
  141. immediately, even if it is empty.")
  142. (semantic-varalias-obsolete 'semantic-before-toplevel-bovination-hook
  143. 'semantic--before-fetch-tags-hook "23.2")
  144. (defvar semantic-after-toplevel-bovinate-hook nil
  145. "Hooks run after a toplevel parse.
  146. It is not run if the toplevel parse command is called, and buffer does
  147. not need to be fully reparsed.
  148. For language specific hooks, make sure you define this as a local hook.
  149. This hook should not be used any more.
  150. Use `semantic-after-toplevel-cache-change-hook' instead.")
  151. (make-obsolete-variable 'semantic-after-toplevel-bovinate-hook nil "23.2")
  152. (defvar semantic-after-toplevel-cache-change-hook nil
  153. "Hooks run after the buffer tag list has changed.
  154. This list will change when a buffer is reparsed, or when the tag list
  155. in a buffer is cleared. It is *NOT* called if the current tag list is
  156. partially reparsed.
  157. Hook functions must take one argument, which is the new list of tags
  158. associated with this buffer.
  159. For language specific hooks, make sure you define this as a local hook.")
  160. (defvar semantic-before-toplevel-cache-flush-hook nil
  161. "Hooks run before the toplevel tag cache is flushed.
  162. For language specific hooks, make sure you define this as a local
  163. hook. This hook is called before a corresponding
  164. `semantic-after-toplevel-cache-change-hook' which is also called
  165. during a flush when the cache is given a new value of nil.")
  166. (defcustom semantic-dump-parse nil
  167. "When non-nil, dump parsing information."
  168. :group 'semantic
  169. :type 'boolean)
  170. (defvar semantic-parser-name "LL"
  171. "Optional name of the parser used to parse input stream.")
  172. (make-variable-buffer-local 'semantic-parser-name)
  173. (defvar semantic--completion-cache nil
  174. "Internal variable used by `semantic-complete-symbol'.")
  175. (make-variable-buffer-local 'semantic--completion-cache)
  176. ;;; Parse tree state management API
  177. ;;
  178. (defvar semantic-parse-tree-state 'needs-rebuild
  179. "State of the current parse tree.")
  180. (make-variable-buffer-local 'semantic-parse-tree-state)
  181. (defmacro semantic-parse-tree-unparseable ()
  182. "Indicate that the current buffer is unparseable.
  183. It is also true that the parse tree will need either updating or
  184. a rebuild. This state will be changed when the user edits the buffer."
  185. `(setq semantic-parse-tree-state 'unparseable))
  186. (defmacro semantic-parse-tree-unparseable-p ()
  187. "Return non-nil if the current buffer has been marked unparseable."
  188. `(eq semantic-parse-tree-state 'unparseable))
  189. (defmacro semantic-parse-tree-set-needs-update ()
  190. "Indicate that the current parse tree needs to be updated.
  191. The parse tree can be updated by `semantic-parse-changes'."
  192. `(setq semantic-parse-tree-state 'needs-update))
  193. (defmacro semantic-parse-tree-needs-update-p ()
  194. "Return non-nil if the current parse tree needs to be updated."
  195. `(eq semantic-parse-tree-state 'needs-update))
  196. (defmacro semantic-parse-tree-set-needs-rebuild ()
  197. "Indicate that the current parse tree needs to be rebuilt.
  198. The parse tree must be rebuilt by `semantic-parse-region'."
  199. `(setq semantic-parse-tree-state 'needs-rebuild))
  200. (defmacro semantic-parse-tree-needs-rebuild-p ()
  201. "Return non-nil if the current parse tree needs to be rebuilt."
  202. `(eq semantic-parse-tree-state 'needs-rebuild))
  203. (defmacro semantic-parse-tree-set-up-to-date ()
  204. "Indicate that the current parse tree is up to date."
  205. `(setq semantic-parse-tree-state nil))
  206. (defmacro semantic-parse-tree-up-to-date-p ()
  207. "Return non-nil if the current parse tree is up to date."
  208. `(null semantic-parse-tree-state))
  209. ;;; Interfacing with the system
  210. ;;
  211. (defcustom semantic-inhibit-functions nil
  212. "List of functions to call with no arguments before Semantic is setup.
  213. If any of these functions returns non-nil, the current buffer is not
  214. setup to use Semantic."
  215. :group 'semantic
  216. :type 'hook)
  217. (defcustom semantic-new-buffer-setup-functions
  218. '((c-mode . semantic-default-c-setup)
  219. (c++-mode . semantic-default-c-setup)
  220. (html-mode . semantic-default-html-setup)
  221. (java-mode . wisent-java-default-setup)
  222. (js-mode . wisent-javascript-setup-parser)
  223. (python-mode . wisent-python-default-setup)
  224. (scheme-mode . semantic-default-scheme-setup)
  225. (srecode-template-mode . srecode-template-setup-parser)
  226. (makefile-automake-mode . semantic-default-make-setup)
  227. (makefile-gmake-mode . semantic-default-make-setup)
  228. (makefile-makepp-mode . semantic-default-make-setup)
  229. (makefile-bsdmake-mode . semantic-default-make-setup)
  230. (makefile-imake-mode . semantic-default-make-setup)
  231. (makefile-mode . semantic-default-make-setup))
  232. "Alist of functions to call to set up Semantic parsing in the buffer.
  233. Each element has the form (MODE . FN), where MODE is a value of
  234. `major-mode' for the buffer and FN is the corresponding function
  235. to call, with no arguments, to set up the parser.
  236. These functions are called by `semantic-new-buffer-fcn', before
  237. `semantic-inhibit-functions'."
  238. :group 'semantic
  239. :type '(alist :key-type symbol :value-type function))
  240. (defvar semantic-init-hook nil
  241. "Hook run when a buffer is initialized with a parsing table.")
  242. (defvar semantic-init-mode-hook nil
  243. "Hook run when a buffer of a particular mode is initialized.")
  244. (make-variable-buffer-local 'semantic-init-mode-hook)
  245. (defvar semantic-init-db-hook nil
  246. "Hook run when a buffer is initialized with a parsing table for DBs.
  247. This hook is for database functions which intend to swap in a tag table.
  248. This guarantees that the DB will go before other modes that require
  249. a parse of the buffer.")
  250. (semantic-varalias-obsolete 'semantic-init-hooks
  251. 'semantic-init-hook "23.2")
  252. (semantic-varalias-obsolete 'semantic-init-mode-hooks
  253. 'semantic-init-mode-hook "23.2")
  254. (semantic-varalias-obsolete 'semantic-init-db-hooks
  255. 'semantic-init-db-hook "23.2")
  256. (defvar semantic-new-buffer-fcn-was-run nil
  257. "Non-nil after `semantic-new-buffer-fcn' has been executed.")
  258. (make-variable-buffer-local 'semantic-new-buffer-fcn-was-run)
  259. (defsubst semantic-active-p ()
  260. "Return non-nil if the current buffer was set up for parsing."
  261. semantic-new-buffer-fcn-was-run)
  262. (defsubst semantic--umatched-syntax-needs-refresh-p ()
  263. "Return non-nil if the unmatched syntax cache needs a refresh.
  264. That is, if it is dirty or if the current parse tree isn't up to date."
  265. (or semantic-unmatched-syntax-cache-check
  266. (not (semantic-parse-tree-up-to-date-p))))
  267. (defun semantic-new-buffer-fcn ()
  268. "Setup the current buffer to use Semantic.
  269. If the major mode is ready for Semantic, and no
  270. `semantic-inhibit-functions' disabled it, the current buffer is setup
  271. to use Semantic, and `semantic-init-hook' is run."
  272. ;; In upstream Semantic, the parser setup functions are called from
  273. ;; mode hooks. In the version bundled with Emacs, we do it here.
  274. (let ((entry (assq major-mode semantic-new-buffer-setup-functions)))
  275. (when entry
  276. (funcall (cdr entry))))
  277. ;; Do stuff if semantic was activated by a mode hook in this buffer,
  278. ;; and not afterwards disabled.
  279. (when (and semantic--parse-table
  280. (not (semantic-active-p))
  281. (not (run-hook-with-args-until-success
  282. 'semantic-inhibit-functions)))
  283. ;; Make sure that if this buffer is cloned, our tags and overlays
  284. ;; don't go along for the ride.
  285. (add-hook 'clone-indirect-buffer-hook 'semantic-clear-toplevel-cache
  286. nil t)
  287. ;; Specify that this function has done its work. At this point
  288. ;; we can consider that semantic is active in this buffer.
  289. (setq semantic-new-buffer-fcn-was-run t)
  290. ;; Here are some buffer local variables we can initialize ourselves
  291. ;; of a mode does not choose to do so.
  292. (semantic-lex-init)
  293. ;; Force this buffer to have its cache refreshed.
  294. (semantic-clear-toplevel-cache)
  295. ;; Call DB hooks before regular init hooks
  296. (run-hooks 'semantic-init-db-hook)
  297. ;; Set up semantic modes
  298. (run-hooks 'semantic-init-hook)
  299. ;; Set up major-mode specific semantic modes
  300. (run-hooks 'semantic-init-mode-hook)))
  301. (defun semantic-fetch-tags-fast ()
  302. "For use in a hook. When only a partial reparse is needed, reparse."
  303. (condition-case nil
  304. (if (semantic-parse-tree-needs-update-p)
  305. (semantic-fetch-tags))
  306. (error nil))
  307. semantic--buffer-cache)
  308. ;;; Parsing Commands
  309. ;;
  310. (eval-when-compile
  311. (condition-case nil (require 'pp) (error nil)))
  312. (defvar semantic-edebug nil
  313. "When non-nil, activate the interactive parsing debugger.
  314. Do not set this yourself. Call `semantic-debug'.")
  315. (defun semantic-elapsed-time (start end)
  316. "Copied from elp.el. Was `elp-elapsed-time'.
  317. Arguments START and END bound the time being calculated."
  318. (float-time (time-subtract end start)))
  319. (defun bovinate (&optional clear)
  320. "Parse the current buffer. Show output in a temp buffer.
  321. Optional argument CLEAR will clear the cache before parsing.
  322. If CLEAR is negative, it will do a full reparse, and also not display
  323. the output buffer."
  324. (interactive "P")
  325. (if clear (semantic-clear-toplevel-cache))
  326. (if (eq clear '-) (setq clear -1))
  327. (let* ((start (current-time))
  328. (out (semantic-fetch-tags))
  329. (end (current-time)))
  330. (message "Retrieving tags took %.2f seconds."
  331. (semantic-elapsed-time start end))
  332. (when (or (null clear) (not (listp clear)))
  333. (pop-to-buffer "*Parser Output*")
  334. (require 'pp)
  335. (erase-buffer)
  336. (insert (pp-to-string out))
  337. (goto-char (point-min)))))
  338. ;;; Functions of the parser plug-in API
  339. ;;
  340. ;; Overload these functions to create new types of parsers.
  341. ;;
  342. (define-overloadable-function semantic-parse-stream (stream nonterminal)
  343. "Parse STREAM, starting at the first NONTERMINAL rule.
  344. For bovine and wisent based parsers, STREAM is from the output of
  345. `semantic-lex', and NONTERMINAL is a rule in the appropriate language
  346. specific rules file.
  347. The default parser table used for bovine or wisent based parsers is
  348. `semantic--parse-table'.
  349. Must return a list: (STREAM TAGS) where STREAM is the unused elements
  350. from STREAM, and TAGS is the list of semantic tags found; usually only
  351. one tag is returned with the exception of compound statements.")
  352. (define-overloadable-function semantic-parse-changes ()
  353. "Reparse changes in the current buffer.
  354. The list of changes are tracked as a series of overlays in the buffer.
  355. When overloading this function, use `semantic-changes-in-region' to
  356. analyze.")
  357. (define-overloadable-function semantic-parse-region
  358. (start end &optional nonterminal depth returnonerror)
  359. "Parse the area between START and END, and return any tags found.
  360. If END needs to be extended due to a lexical token being too large, it
  361. will be silently ignored.
  362. Optional arguments:
  363. NONTERMINAL is the rule to start parsing at.
  364. DEPTH specifies the lexical depth to descend for parsers that use
  365. lexical analysis as their first step.
  366. RETURNONERROR specifies that parsing should stop on the first
  367. unmatched syntax encountered. When nil, parsing skips the syntax,
  368. adding it to the unmatched syntax cache.
  369. Must return a list of semantic tags which have been cooked
  370. \(repositioned properly) but which DO NOT HAVE OVERLAYS associated
  371. with them. When overloading this function, use `semantic--tag-expand'
  372. to cook raw tags.")
  373. (defun semantic-parse-region-default
  374. (start end &optional nonterminal depth returnonerror)
  375. "Parse the area between START and END, and return any tags found.
  376. If END needs to be extended due to a lexical token being too large,
  377. it will be silently ignored.
  378. Optional arguments:
  379. NONTERMINAL is the rule to start parsing at if it is known.
  380. DEPTH specifies the lexical depth to scan.
  381. RETURNONERROR specifies that parsing should end when encountering
  382. unterminated syntax."
  383. (when (or (null semantic--parse-table) (eq semantic--parse-table t))
  384. ;; If there is no table, or it was set to t, then we are here by
  385. ;; some other mistake. Do not throw an error deep in the parser.
  386. (error "No support found to parse buffer %S" (buffer-name)))
  387. (save-restriction
  388. (widen)
  389. (when (or (< end start) (> end (point-max)))
  390. (error "Invalid parse region bounds %S, %S" start end))
  391. (nreverse
  392. (semantic-repeat-parse-whole-stream
  393. (or (cdr (assq start semantic-lex-block-streams))
  394. (semantic-lex start end depth))
  395. nonterminal returnonerror))))
  396. ;;; Parsing functions
  397. ;;
  398. (defun semantic-set-unmatched-syntax-cache (unmatched-syntax)
  399. "Set the unmatched syntax cache.
  400. Argument UNMATCHED-SYNTAX is the syntax to set into the cache."
  401. ;; This function is not actually called by the main parse loop.
  402. ;; This is intended for use by semanticdb.
  403. (setq semantic-unmatched-syntax-cache unmatched-syntax
  404. semantic-unmatched-syntax-cache-check nil)
  405. ;; Refresh the display of unmatched syntax tokens if enabled
  406. (run-hook-with-args 'semantic-unmatched-syntax-hook
  407. semantic-unmatched-syntax-cache))
  408. (defun semantic-clear-unmatched-syntax-cache ()
  409. "Clear the cache of unmatched syntax tokens."
  410. (setq semantic-unmatched-syntax-cache nil
  411. semantic-unmatched-syntax-cache-check t))
  412. (defun semantic-unmatched-syntax-tokens ()
  413. "Return the list of unmatched syntax tokens."
  414. ;; If the cache need refresh then do a full re-parse.
  415. (if (semantic--umatched-syntax-needs-refresh-p)
  416. ;; To avoid a recursive call, temporarily disable
  417. ;; `semantic-unmatched-syntax-hook'.
  418. (let (semantic-unmatched-syntax-hook)
  419. (condition-case nil
  420. (progn
  421. (semantic-clear-toplevel-cache)
  422. (semantic-fetch-tags))
  423. (quit
  424. (message "semantic-unmatched-syntax-tokens:\
  425. parsing of buffer canceled"))
  426. )))
  427. semantic-unmatched-syntax-cache)
  428. (defun semantic-clear-toplevel-cache ()
  429. "Clear the toplevel tag cache for the current buffer.
  430. Clearing the cache will force a complete reparse next time a tag list
  431. is requested."
  432. (interactive)
  433. (run-hooks 'semantic-before-toplevel-cache-flush-hook)
  434. (setq semantic--buffer-cache nil)
  435. (semantic-clear-unmatched-syntax-cache)
  436. (semantic-clear-parser-warnings)
  437. ;; Nuke all semantic overlays. This is faster than deleting based
  438. ;; on our data structure.
  439. (let ((l (semantic-overlay-lists)))
  440. (mapc 'semantic-delete-overlay-maybe (car l))
  441. (mapc 'semantic-delete-overlay-maybe (cdr l))
  442. )
  443. (semantic-parse-tree-set-needs-rebuild)
  444. ;; Remove this hook which tracks if a buffer is up to date or not.
  445. (remove-hook 'after-change-functions 'semantic-change-function t)
  446. ;; Old model. Delete someday.
  447. ;;(run-hooks 'semantic-after-toplevel-bovinate-hook)
  448. (run-hook-with-args 'semantic-after-toplevel-cache-change-hook
  449. semantic--buffer-cache)
  450. (setq semantic--completion-cache nil))
  451. (defvar semantic-bovinate-nonterminal-check-obarray)
  452. (defun semantic--set-buffer-cache (tagtable)
  453. "Set the toplevel tag cache to TAGTABLE."
  454. (setq semantic--buffer-cache tagtable
  455. semantic-unmatched-syntax-cache-check nil)
  456. ;; This is specific to the bovine parser.
  457. (set (make-local-variable 'semantic-bovinate-nonterminal-check-obarray)
  458. nil)
  459. (semantic-parse-tree-set-up-to-date)
  460. (semantic-make-local-hook 'after-change-functions)
  461. (add-hook 'after-change-functions 'semantic-change-function nil t)
  462. (run-hook-with-args 'semantic-after-toplevel-cache-change-hook
  463. semantic--buffer-cache)
  464. (setq semantic--completion-cache nil)
  465. ;; Refresh the display of unmatched syntax tokens if enabled
  466. (run-hook-with-args 'semantic-unmatched-syntax-hook
  467. semantic-unmatched-syntax-cache)
  468. ;; Old Semantic 1.3 hook API. Maybe useful forever?
  469. (run-hooks 'semantic-after-toplevel-bovinate-hook)
  470. )
  471. (defvar semantic-working-type 'percent
  472. "*The type of working message to use when parsing.
  473. 'percent means we are doing a linear parse through the buffer.
  474. 'dynamic means we are reparsing specific tags.")
  475. (semantic-varalias-obsolete 'semantic-bovination-working-type
  476. 'semantic-working-type "23.2")
  477. (defvar semantic-minimum-working-buffer-size (* 1024 5)
  478. "*The minimum size of a buffer before working messages are displayed.
  479. Buffers smaller than this will parse silently.
  480. Buffers larger than this will display the working progress bar.")
  481. (defsubst semantic-parser-working-message (&optional arg)
  482. "Return the message string displayed while parsing.
  483. If optional argument ARG is non-nil it is appended to the message
  484. string."
  485. (concat "Parsing"
  486. (if arg (format " %s" arg))
  487. (if semantic-parser-name (format " (%s)" semantic-parser-name))
  488. "..."))
  489. ;;; Application Parser Entry Points
  490. ;;
  491. ;; The best way to call the parser from programs is via
  492. ;; `semantic-fetch-tags'. This, in turn, uses other internal
  493. ;; API functions which plug-in parsers can take advantage of.
  494. (defun semantic-fetch-tags ()
  495. "Fetch semantic tags from the current buffer.
  496. If the buffer cache is up to date, return that.
  497. If the buffer cache is out of date, attempt an incremental reparse.
  498. If the buffer has not been parsed before, or if the incremental reparse
  499. fails, then parse the entire buffer.
  500. If a lexical error had been previously discovered and the buffer
  501. was marked unparseable, then do nothing, and return the cache."
  502. (and
  503. ;; Is this a semantic enabled buffer?
  504. (semantic-active-p)
  505. ;; Application hooks say the buffer is safe for parsing
  506. (run-hook-with-args-until-failure
  507. 'semantic-before-toplevel-bovination-hook)
  508. (run-hook-with-args-until-failure
  509. 'semantic--before-fetch-tags-hook)
  510. ;; If the buffer was previously marked unparseable,
  511. ;; then don't waste our time.
  512. (not (semantic-parse-tree-unparseable-p))
  513. ;; The parse tree actually needs to be refreshed
  514. (not (semantic-parse-tree-up-to-date-p))
  515. ;; So do it!
  516. (let* ((gc-cons-threshold (max gc-cons-threshold 10000000))
  517. (semantic-lex-block-streams nil)
  518. (res nil))
  519. (garbage-collect)
  520. (cond
  521. ;;;; Try the incremental parser to do a fast update.
  522. ((semantic-parse-tree-needs-update-p)
  523. (setq res (semantic-parse-changes))
  524. (if (semantic-parse-tree-needs-rebuild-p)
  525. ;; If the partial reparse fails, jump to a full reparse.
  526. (semantic-fetch-tags)
  527. ;; Clear the cache of unmatched syntax tokens
  528. ;;
  529. ;; NOTE TO SELF:
  530. ;;
  531. ;; Move this into the incremental parser. This is a bug.
  532. ;;
  533. (semantic-clear-unmatched-syntax-cache)
  534. (run-hook-with-args ;; Let hooks know the updated tags
  535. 'semantic-after-partial-cache-change-hook res))
  536. (setq semantic--completion-cache nil))
  537. ;;;; Parse the whole system.
  538. ((semantic-parse-tree-needs-rebuild-p)
  539. ;; Use Emacs's built-in progress-reporter
  540. (let ((semantic--progress-reporter
  541. (and (>= (point-max) semantic-minimum-working-buffer-size)
  542. (eq semantic-working-type 'percent)
  543. (make-progress-reporter
  544. (semantic-parser-working-message (buffer-name))
  545. 0 100))))
  546. (setq res (semantic-parse-region (point-min) (point-max)))
  547. (if semantic--progress-reporter
  548. (progress-reporter-done semantic--progress-reporter)))
  549. ;; Clear the caches when we see there were no errors.
  550. ;; But preserve the unmatched syntax cache and warnings!
  551. (let (semantic-unmatched-syntax-cache
  552. semantic-unmatched-syntax-cache-check
  553. semantic-parser-warnings)
  554. (semantic-clear-toplevel-cache))
  555. ;; Set up the new overlays
  556. (semantic--tag-link-list-to-buffer res)
  557. ;; Set up the cache with the new results
  558. (semantic--set-buffer-cache res)
  559. ))))
  560. ;; Always return the current parse tree.
  561. semantic--buffer-cache)
  562. (defun semantic-refresh-tags-safe ()
  563. "Refresh the current buffer's tags safely.
  564. Return non-nil if the refresh was successful.
  565. Return nil if there is some sort of syntax error preventing a reparse.
  566. Does nothing if the current buffer doesn't need reparsing."
  567. ;; These checks actually occur in `semantic-fetch-tags', but if we
  568. ;; do them here, then all the bovination hooks are not run, and
  569. ;; we save lots of time.
  570. (cond
  571. ;; If the buffer was previously marked unparseable,
  572. ;; then don't waste our time.
  573. ((semantic-parse-tree-unparseable-p)
  574. nil)
  575. ;; The parse tree is already ok.
  576. ((semantic-parse-tree-up-to-date-p)
  577. t)
  578. (t
  579. (let* ((inhibit-quit nil)
  580. (lexically-safe t)
  581. )
  582. (unwind-protect
  583. ;; Perform the parsing.
  584. (progn
  585. (when (semantic-lex-catch-errors safe-refresh
  586. (save-excursion (semantic-fetch-tags))
  587. nil)
  588. ;; If we are here, it is because the lexical step failed,
  589. ;; probably due to unterminated lists or something like that.
  590. ;; We do nothing, and just wait for the next idle timer
  591. ;; to go off. In the meantime, remember this, and make sure
  592. ;; no other idle services can get executed.
  593. (setq lexically-safe nil))
  594. )
  595. )
  596. ;; Return if we are lexically safe
  597. lexically-safe))))
  598. (defun semantic-bovinate-toplevel (&optional ignored)
  599. "Backward compatibility function."
  600. (semantic-fetch-tags))
  601. (make-obsolete 'semantic-bovinate-toplevel 'semantic-fetch-tags "23.2")
  602. ;; Another approach is to let Emacs call the parser on idle time, when
  603. ;; needed, use `semantic-fetch-available-tags' to only retrieve
  604. ;; available tags, and setup the `semantic-after-*-hook' hooks to
  605. ;; synchronize with new tags when they become available.
  606. (defsubst semantic-fetch-available-tags ()
  607. "Fetch available semantic tags from the current buffer.
  608. That is, return tags currently in the cache without parsing the
  609. current buffer.
  610. Parse operations happen asynchronously when needed on Emacs idle time.
  611. Use the `semantic-after-toplevel-cache-change-hook' and
  612. `semantic-after-partial-cache-change-hook' hooks to synchronize with
  613. new tags when they become available."
  614. semantic--buffer-cache)
  615. ;;; Iterative parser helper function
  616. ;;
  617. ;; Iterative parsers are better than rule-based iterative functions
  618. ;; in that they can handle obscure errors more cleanly.
  619. ;;
  620. ;; `semantic-repeat-parse-whole-stream' abstracts this action for
  621. ;; other parser centric routines.
  622. ;;
  623. (defun semantic-repeat-parse-whole-stream
  624. (stream nonterm &optional returnonerror)
  625. "Iteratively parse the entire stream STREAM starting with NONTERM.
  626. Optional argument RETURNONERROR indicates that the parser should exit
  627. with the current results on a parse error.
  628. This function returns semantic tags without overlays."
  629. (let ((result nil)
  630. (case-fold-search semantic-case-fold)
  631. nontermsym tag)
  632. (while stream
  633. (setq nontermsym (semantic-parse-stream stream nonterm)
  634. tag (car (cdr nontermsym)))
  635. (if (not nontermsym)
  636. (error "Parse error @ %d" (car (cdr (car stream)))))
  637. (if (eq (car nontermsym) stream)
  638. (error "Parser error: Infinite loop?"))
  639. (if tag
  640. (if (car tag)
  641. (setq tag (mapcar
  642. #'(lambda (tag)
  643. ;; Set the 'reparse-symbol property to
  644. ;; NONTERM unless it was already setup
  645. ;; by a tag expander
  646. (or (semantic--tag-get-property
  647. tag 'reparse-symbol)
  648. (semantic--tag-put-property
  649. tag 'reparse-symbol nonterm))
  650. tag)
  651. (semantic--tag-expand tag))
  652. result (append tag result))
  653. ;; No error in this case, a purposeful nil means don't
  654. ;; store anything.
  655. )
  656. (if returnonerror
  657. (setq stream nil)
  658. ;; The current item in the stream didn't match, so add it to
  659. ;; the list of syntax items which didn't match.
  660. (setq semantic-unmatched-syntax-cache
  661. (cons (car stream) semantic-unmatched-syntax-cache))
  662. ))
  663. ;; Designated to ignore.
  664. (setq stream (car nontermsym))
  665. (if stream
  666. ;; Use Emacs's built-in progress reporter:
  667. (and (boundp 'semantic--progress-reporter)
  668. semantic--progress-reporter
  669. (eq semantic-working-type 'percent)
  670. (progress-reporter-update
  671. semantic--progress-reporter
  672. (/ (* 100 (semantic-lex-token-start (car stream)))
  673. (point-max))))))
  674. result))
  675. ;;; Parsing Warnings:
  676. ;;
  677. ;; Parsing a buffer may result in non-critical things that we should
  678. ;; alert the user to without interrupting the normal flow.
  679. ;;
  680. ;; Any parser can use this API to provide a list of warnings during a
  681. ;; parse which a user may want to investigate.
  682. (defvar semantic-parser-warnings nil
  683. "A list of parser warnings since the last full reparse.")
  684. (make-variable-buffer-local 'semantic-parser-warnings)
  685. (defun semantic-clear-parser-warnings ()
  686. "Clear the current list of parser warnings for this buffer."
  687. (setq semantic-parser-warnings nil))
  688. (defun semantic-push-parser-warning (warning start end)
  689. "Add a parser WARNING that covers text from START to END."
  690. (setq semantic-parser-warnings
  691. (cons (cons warning (cons start end))
  692. semantic-parser-warnings)))
  693. (defun semantic-dump-parser-warnings ()
  694. "Dump any parser warnings."
  695. (interactive)
  696. (if semantic-parser-warnings
  697. (let ((pw semantic-parser-warnings))
  698. (pop-to-buffer "*Parser Warnings*")
  699. (require 'pp)
  700. (erase-buffer)
  701. (insert (pp-to-string pw))
  702. (goto-char (point-min)))
  703. (message "No parser warnings.")))
  704. ;;; Compatibility:
  705. ;;
  706. ;; Semantic 1.x parser action helper functions, used by some parsers.
  707. ;; Please move away from these functions, and try using semantic 2.x
  708. ;; interfaces instead.
  709. ;;
  710. (defsubst semantic-bovinate-region-until-error
  711. (start end nonterm &optional depth)
  712. "NOTE: Use `semantic-parse-region' instead.
  713. Bovinate between START and END starting with NONTERM.
  714. Optional DEPTH specifies how many levels of parenthesis to enter.
  715. This command will parse until an error is encountered, and return
  716. the list of everything found until that moment.
  717. This is meant for finding variable definitions at the beginning of
  718. code blocks in methods. If `bovine-inner-scope' can also support
  719. commands, use `semantic-bovinate-from-nonterminal-full'."
  720. (semantic-parse-region start end nonterm depth t))
  721. (make-obsolete 'semantic-bovinate-region-until-error
  722. 'semantic-parse-region "23.2")
  723. (defsubst semantic-bovinate-from-nonterminal
  724. (start end nonterm &optional depth length)
  725. "Bovinate from within a nonterminal lambda from START to END.
  726. Argument NONTERM is the nonterminal symbol to start with.
  727. Optional argument DEPTH is the depth of lists to dive into. When used
  728. in a `lambda' of a MATCH-LIST, there is no need to include a START and
  729. END part.
  730. Optional argument LENGTH specifies we are only interested in LENGTH
  731. tokens."
  732. (car-safe (cdr (semantic-parse-stream
  733. (semantic-lex start end (or depth 1) length)
  734. nonterm))))
  735. (defsubst semantic-bovinate-from-nonterminal-full
  736. (start end nonterm &optional depth)
  737. "NOTE: Use `semantic-parse-region' instead.
  738. Bovinate from within a nonterminal lambda from START to END.
  739. Iterates until all the space between START and END is exhausted.
  740. Argument NONTERM is the nonterminal symbol to start with.
  741. If NONTERM is nil, use `bovine-block-toplevel'.
  742. Optional argument DEPTH is the depth of lists to dive into.
  743. When used in a `lambda' of a MATCH-LIST, there is no need to include
  744. a START and END part."
  745. (semantic-parse-region start end nonterm (or depth 1)))
  746. (make-obsolete 'semantic-bovinate-from-nonterminal-full
  747. 'semantic-parse-region "23.2")
  748. ;;; User interface
  749. (defun semantic-force-refresh ()
  750. "Force a full refresh of the current buffer's tags.
  751. Throw away all the old tags, and recreate the tag database."
  752. (interactive)
  753. (semantic-clear-toplevel-cache)
  754. (semantic-fetch-tags)
  755. (message "Buffer reparsed."))
  756. (defvar semantic-mode-map
  757. (let ((map (make-sparse-keymap)))
  758. ;; Key bindings:
  759. ;; (define-key km "f" 'senator-search-set-tag-class-filter)
  760. ;; (define-key km "i" 'senator-isearch-toggle-semantic-mode)
  761. (define-key map "\C-c,j" 'semantic-complete-jump-local)
  762. (define-key map "\C-c,J" 'semantic-complete-jump)
  763. (define-key map "\C-c,m" 'semantic-complete-jump-local-members)
  764. (define-key map "\C-c,g" 'semantic-symref-symbol)
  765. (define-key map "\C-c,G" 'semantic-symref)
  766. (define-key map "\C-c,p" 'senator-previous-tag)
  767. (define-key map "\C-c,n" 'senator-next-tag)
  768. (define-key map "\C-c,u" 'senator-go-to-up-reference)
  769. (define-key map "\C-c, " 'semantic-complete-analyze-inline)
  770. (define-key map "\C-c,\C-w" 'senator-kill-tag)
  771. (define-key map "\C-c,\M-w" 'senator-copy-tag)
  772. (define-key map "\C-c,\C-y" 'senator-yank-tag)
  773. (define-key map "\C-c,r" 'senator-copy-tag-to-register)
  774. (define-key map "\C-c,," 'semantic-force-refresh)
  775. (define-key map [?\C-c ?, up] 'senator-transpose-tags-up)
  776. (define-key map [?\C-c ?, down] 'senator-transpose-tags-down)
  777. (define-key map "\C-c,l" 'semantic-analyze-possible-completions)
  778. ;; This hack avoids showing the CEDET menu twice if ede-minor-mode
  779. ;; and Semantic are both enabled. Is there a better way?
  780. (define-key map [menu-bar cedet-menu]
  781. (list 'menu-item "Development" cedet-menu-map
  782. :enable (quote (not (bound-and-true-p global-ede-mode)))))
  783. ;; (define-key km "-" 'senator-fold-tag)
  784. ;; (define-key km "+" 'senator-unfold-tag)
  785. map))
  786. ;; Activate the Semantic items in cedet-menu-map
  787. (let ((navigate-menu (make-sparse-keymap "Navigate Tags"))
  788. (edit-menu (make-sparse-keymap "Edit Tags")))
  789. ;; Edit Tags submenu:
  790. (define-key edit-menu [semantic-analyze-possible-completions]
  791. '(menu-item "List Completions" semantic-analyze-possible-completions
  792. :help "Display a list of completions for the tag at point"))
  793. (define-key edit-menu [semantic-complete-analyze-inline]
  794. '(menu-item "Complete Tag Inline" semantic-complete-analyze-inline
  795. :help "Display inline completion for the tag at point"))
  796. (define-key edit-menu [semantic-completion-separator]
  797. '("--"))
  798. (define-key edit-menu [senator-transpose-tags-down]
  799. '(menu-item "Transpose Tags Down" senator-transpose-tags-down
  800. :active (semantic-current-tag)
  801. :help "Transpose the current tag and the next tag"))
  802. (define-key edit-menu [senator-transpose-tags-up]
  803. '(menu-item "Transpose Tags Up" senator-transpose-tags-up
  804. :active (semantic-current-tag)
  805. :help "Transpose the current tag and the previous tag"))
  806. (define-key edit-menu [semantic-edit-separator]
  807. '("--"))
  808. (define-key edit-menu [senator-yank-tag]
  809. '(menu-item "Yank Tag" senator-yank-tag
  810. :active (not (ring-empty-p senator-tag-ring))
  811. :help "Yank the head of the tag ring into the buffer"))
  812. (define-key edit-menu [senator-copy-tag-to-register]
  813. '(menu-item "Copy Tag To Register" senator-copy-tag-to-register
  814. :active (semantic-current-tag)
  815. :help "Yank the head of the tag ring into the buffer"))
  816. (define-key edit-menu [senator-copy-tag]
  817. '(menu-item "Copy Tag" senator-copy-tag
  818. :active (semantic-current-tag)
  819. :help "Copy the current tag to the tag ring"))
  820. (define-key edit-menu [senator-kill-tag]
  821. '(menu-item "Kill Tag" senator-kill-tag
  822. :active (semantic-current-tag)
  823. :help "Kill the current tag, and copy it to the tag ring"))
  824. ;; Navigate Tags submenu:
  825. (define-key navigate-menu [senator-narrow-to-defun]
  826. '(menu-item "Narrow to Tag" senator-narrow-to-defun
  827. :active (semantic-current-tag)
  828. :help "Narrow the buffer to the bounds of the current tag"))
  829. (define-key navigate-menu [semantic-narrow-to-defun-separator]
  830. '("--"))
  831. (define-key navigate-menu [semantic-symref-symbol]
  832. '(menu-item "Find Tag References..." semantic-symref-symbol
  833. :help "Read a tag and list the references to it"))
  834. (define-key navigate-menu [semantic-complete-jump]
  835. '(menu-item "Find Tag Globally..." semantic-complete-jump
  836. :help "Read a tag name and find it in the current project"))
  837. (define-key navigate-menu [semantic-complete-jump-local-members]
  838. '(menu-item "Find Local Members ..." semantic-complete-jump-local-members
  839. :help "Read a tag name and find a local member with that name"))
  840. (define-key navigate-menu [semantic-complete-jump-local]
  841. '(menu-item "Find Tag in This Buffer..." semantic-complete-jump-local
  842. :help "Read a tag name and find it in this buffer"))
  843. (define-key navigate-menu [semantic-navigation-separator]
  844. '("--"))
  845. (define-key navigate-menu [senator-go-to-up-reference]
  846. '(menu-item "Parent Tag" senator-go-to-up-reference
  847. :help "Navigate up one reference by tag"))
  848. (define-key navigate-menu [senator-next-tag]
  849. '(menu-item "Next Tag" senator-next-tag
  850. :help "Go to the next tag"))
  851. (define-key navigate-menu [senator-previous-tag]
  852. '(menu-item "Previous Tag" senator-previous-tag
  853. :help "Go to the previous tag"))
  854. ;; Top level menu items:
  855. (define-key cedet-menu-map [semantic-force-refresh]
  856. '(menu-item "Reparse Buffer" semantic-force-refresh
  857. :help "Force a full reparse of the current buffer"
  858. :visible semantic-mode))
  859. (define-key cedet-menu-map [semantic-edit-menu]
  860. `(menu-item "Edit Tags" ,edit-menu
  861. :visible semantic-mode))
  862. (define-key cedet-menu-map [navigate-menu]
  863. `(menu-item "Navigate Tags" ,navigate-menu
  864. :visible semantic-mode))
  865. (define-key cedet-menu-map [semantic-options-separator]
  866. '("--"))
  867. (define-key cedet-menu-map [global-semantic-highlight-func-mode]
  868. '(menu-item "Highlight Current Function" global-semantic-highlight-func-mode
  869. :help "Highlight the tag at point"
  870. :visible semantic-mode
  871. :button (:toggle . global-semantic-highlight-func-mode)))
  872. (define-key cedet-menu-map [global-semantic-decoration-mode]
  873. '(menu-item "Decorate Tags" global-semantic-decoration-mode
  874. :help "Decorate tags based on tag attributes"
  875. :visible semantic-mode
  876. :button (:toggle . (bound-and-true-p
  877. global-semantic-decoration-mode))))
  878. (define-key cedet-menu-map [global-semantic-idle-completions-mode]
  879. '(menu-item "Show Tag Completions" global-semantic-idle-completions-mode
  880. :help "Show tag completions when idle"
  881. :visible semantic-mode
  882. :enable global-semantic-idle-scheduler-mode
  883. :button (:toggle . global-semantic-idle-completions-mode)))
  884. (define-key cedet-menu-map [global-semantic-idle-summary-mode]
  885. '(menu-item "Show Tag Summaries" global-semantic-idle-summary-mode
  886. :help "Show tag summaries when idle"
  887. :visible semantic-mode
  888. :enable global-semantic-idle-scheduler-mode
  889. :button (:toggle . global-semantic-idle-summary-mode)))
  890. (define-key cedet-menu-map [global-semantic-idle-scheduler-mode]
  891. '(menu-item "Reparse When Idle" global-semantic-idle-scheduler-mode
  892. :help "Keep a buffer's parse tree up to date when idle"
  893. :visible semantic-mode
  894. :button (:toggle . global-semantic-idle-scheduler-mode)))
  895. (define-key cedet-menu-map [global-semanticdb-minor-mode]
  896. '(menu-item "Semantic Database" global-semanticdb-minor-mode
  897. :help "Store tag information in a database"
  898. :visible semantic-mode
  899. :button (:toggle . global-semanticdb-minor-mode))))
  900. ;; The `semantic-mode' command, in conjunction with the
  901. ;; `semantic-default-submodes' variable, toggles Semantic's various
  902. ;; auxiliary minor modes.
  903. (defvar semantic-load-system-cache-loaded nil
  904. "Non-nil when the Semantic system caches have been loaded.
  905. Prevent this load system from loading files in twice.")
  906. (defconst semantic-submode-list
  907. '(global-semantic-highlight-func-mode
  908. global-semantic-decoration-mode
  909. global-semantic-stickyfunc-mode
  910. global-semantic-idle-completions-mode
  911. global-semantic-idle-scheduler-mode
  912. global-semanticdb-minor-mode
  913. global-semantic-idle-summary-mode
  914. global-semantic-mru-bookmark-mode)
  915. "List of auxiliary minor modes in the Semantic package.")
  916. ;;;###autoload
  917. (defcustom semantic-default-submodes
  918. '(global-semantic-idle-scheduler-mode global-semanticdb-minor-mode)
  919. "List of auxiliary Semantic minor modes enabled by `semantic-mode'.
  920. The possible elements of this list include the following:
  921. `global-semanticdb-minor-mode' - Maintain tag database.
  922. `global-semantic-idle-scheduler-mode' - Reparse buffer when idle.
  923. `global-semantic-idle-summary-mode' - Show summary of tag at point.
  924. `global-semantic-idle-completions-mode' - Show completions when idle.
  925. `global-semantic-decoration-mode' - Additional tag decorations.
  926. `global-semantic-highlight-func-mode' - Highlight the current tag.
  927. `global-semantic-stickyfunc-mode' - Show current fun in header line.
  928. `global-semantic-mru-bookmark-mode' - Provide `switch-to-buffer'-like
  929. keybinding for tag names."
  930. :group 'semantic
  931. :type `(set ,@(mapcar (lambda (c) (list 'const c))
  932. semantic-submode-list)))
  933. ;;;###autoload
  934. (define-minor-mode semantic-mode
  935. "Toggle parser features (Semantic mode).
  936. With a prefix argument ARG, enable Semantic mode if ARG is
  937. positive, and disable it otherwise. If called from Lisp, enable
  938. Semantic mode if ARG is omitted or nil.
  939. In Semantic mode, Emacs parses the buffers you visit for their
  940. semantic content. This information is used by a variety of
  941. auxiliary minor modes, listed in `semantic-default-submodes';
  942. all the minor modes in this list are also enabled when you enable
  943. Semantic mode.
  944. \\{semantic-mode-map}"
  945. :global t
  946. :group 'semantic
  947. (if semantic-mode
  948. ;; Turn on Semantic mode
  949. (progn
  950. ;; Enable all the global auxiliary minor modes in
  951. ;; `semantic-submode-list'.
  952. (dolist (mode semantic-submode-list)
  953. (if (memq mode semantic-default-submodes)
  954. (funcall mode 1)))
  955. (unless semantic-load-system-cache-loaded
  956. (setq semantic-load-system-cache-loaded t)
  957. (when (and (boundp 'semanticdb-default-system-save-directory)
  958. (stringp semanticdb-default-system-save-directory)
  959. (file-exists-p semanticdb-default-system-save-directory))
  960. (require 'semantic/db-ebrowse)
  961. (semanticdb-load-ebrowse-caches)))
  962. (add-hook 'mode-local-init-hook 'semantic-new-buffer-fcn)
  963. ;; Add semantic-ia-complete-symbol to
  964. ;; completion-at-point-functions, so that it is run from
  965. ;; M-TAB.
  966. (add-hook 'completion-at-point-functions
  967. 'semantic-completion-at-point-function)
  968. (if global-ede-mode
  969. (define-key cedet-menu-map [cedet-menu-separator] '("--")))
  970. (dolist (b (buffer-list))
  971. (with-current-buffer b
  972. (semantic-new-buffer-fcn))))
  973. ;; Disable all Semantic features.
  974. (remove-hook 'mode-local-init-hook 'semantic-new-buffer-fcn)
  975. (remove-hook 'completion-at-point-functions
  976. 'semantic-completion-at-point-function)
  977. (define-key cedet-menu-map [cedet-menu-separator] nil)
  978. (define-key cedet-menu-map [semantic-options-separator] nil)
  979. ;; FIXME: handle semanticdb-load-ebrowse-caches
  980. (dolist (mode semantic-submode-list)
  981. (if (and (boundp mode) (eval mode))
  982. (funcall mode -1)))))
  983. (defun semantic-completion-at-point-function ()
  984. 'semantic-ia-complete-symbol)
  985. ;;; Autoload some functions that are not in semantic/loaddefs
  986. (autoload 'global-semantic-idle-completions-mode "semantic/idle"
  987. "Toggle global use of `semantic-idle-completions-mode'.
  988. If ARG is positive, enable, if it is negative, disable.
  989. If ARG is nil, then toggle." t nil)
  990. (autoload 'semantic-idle-completions-mode "semantic/idle"
  991. "Display a list of possible completions in a tooltip.
  992. This is a minor mode which performs actions during idle time.
  993. With prefix argument ARG, turn on if positive, otherwise off. The
  994. minor mode can be turned on only if semantic feature is available and
  995. the current buffer was set up for parsing. Return non-nil if the
  996. minor mode is enabled." t nil)
  997. (autoload 'global-semantic-idle-summary-mode "semantic/idle"
  998. "Toggle global use of `semantic-idle-summary-mode'.
  999. If ARG is positive, enable, if it is negative, disable.
  1000. If ARG is nil, then toggle." t nil)
  1001. (autoload 'semantic-idle-summary-mode "semantic/idle"
  1002. "Display a tag summary of the lexical token under the cursor.
  1003. Call `semantic-idle-summary-current-symbol-info' for getting the
  1004. current tag to display information.
  1005. This is a minor mode which performs actions during idle time.
  1006. With prefix argument ARG, turn on if positive, otherwise off. The
  1007. minor mode can be turned on only if semantic feature is available and
  1008. the current buffer was set up for parsing. Return non-nil if the
  1009. minor mode is enabled." t nil)
  1010. (autoload 'srecode-template-setup-parser "srecode/srecode-template"
  1011. "Set up buffer for parsing SRecode template files." t nil)
  1012. (provide 'semantic)
  1013. ;; Semantic-util is a part of the semantic API. Include it last
  1014. ;; because it depends on semantic.
  1015. (require 'semantic/util)
  1016. ;; (require 'semantic/load)
  1017. ;;; semantic.el ends here