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