Merge changes from CEDET upstream.
[bpt/emacs.git] / lisp / cedet / semantic.el
CommitLineData
d3d82e7b
CY
1;;; semantic.el --- Semantic buffer evaluator.
2
44e97401 3;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
d3d82e7b
CY
4
5;; Author: Eric M. Ludlam <zappo@gnu.org>
dd9af436
CY
6;; Keywords: syntax tools
7;; Version: 2.0
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
62a81506 41(defvar semantic-version "2.1beta"
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
d3d82e7b 314(defvar semantic-new-buffer-fcn-was-run nil
db9e401b 315 "Non-nil after `semantic-new-buffer-fcn' has been executed.")
d3d82e7b
CY
316(make-variable-buffer-local 'semantic-new-buffer-fcn-was-run)
317
318(defsubst semantic-active-p ()
319 "Return non-nil if the current buffer was set up for parsing."
320 semantic-new-buffer-fcn-was-run)
321
bf659b3f
DE
322(defsubst semantic-error-if-unparsed ()
323 "Raise an error if current buffer was not parsed by Semantic."
324 (unless semantic-new-buffer-fcn-was-run
325 (error "Buffer was not parsed by Semantic.")))
326
d3d82e7b
CY
327(defsubst semantic--umatched-syntax-needs-refresh-p ()
328 "Return non-nil if the unmatched syntax cache needs a refresh.
db9e401b 329That is, if it is dirty or if the current parse tree isn't up to date."
d3d82e7b
CY
330 (or semantic-unmatched-syntax-cache-check
331 (not (semantic-parse-tree-up-to-date-p))))
332
333(defun semantic-new-buffer-fcn ()
334 "Setup the current buffer to use Semantic.
335If the major mode is ready for Semantic, and no
336`semantic-inhibit-functions' disabled it, the current buffer is setup
337to use Semantic, and `semantic-init-hook' is run."
82481502
CY
338 ;; In upstream Semantic, the parser setup functions are called from
339 ;; mode hooks. In the version bundled with Emacs, we do it here.
340 (let ((entry (assq major-mode semantic-new-buffer-setup-functions)))
341 (when entry
342 (funcall (cdr entry))))
d3d82e7b
CY
343 ;; Do stuff if semantic was activated by a mode hook in this buffer,
344 ;; and not afterwards disabled.
345 (when (and semantic--parse-table
346 (not (semantic-active-p))
347 (not (run-hook-with-args-until-success
348 'semantic-inhibit-functions)))
349 ;; Make sure that if this buffer is cloned, our tags and overlays
350 ;; don't go along for the ride.
351 (add-hook 'clone-indirect-buffer-hook 'semantic-clear-toplevel-cache
352 nil t)
cd1181db 353 ;; Specify that this function has done its work. At this point
d3d82e7b
CY
354 ;; we can consider that semantic is active in this buffer.
355 (setq semantic-new-buffer-fcn-was-run t)
356 ;; Here are some buffer local variables we can initialize ourselves
357 ;; of a mode does not choose to do so.
358 (semantic-lex-init)
359 ;; Force this buffer to have its cache refreshed.
360 (semantic-clear-toplevel-cache)
361 ;; Call DB hooks before regular init hooks
b82525f2 362 (run-hooks 'semantic-init-db-hook)
d3d82e7b 363 ;; Set up semantic modes
b82525f2 364 (run-hooks 'semantic-init-hook)
d3d82e7b 365 ;; Set up major-mode specific semantic modes
b82525f2 366 (run-hooks 'semantic-init-mode-hook)))
d3d82e7b
CY
367
368(defun semantic-fetch-tags-fast ()
369 "For use in a hook. When only a partial reparse is needed, reparse."
370 (condition-case nil
371 (if (semantic-parse-tree-needs-update-p)
372 (semantic-fetch-tags))
373 (error nil))
374 semantic--buffer-cache)
d3d82e7b
CY
375\f
376;;; Parsing Commands
377;;
378(eval-when-compile
379 (condition-case nil (require 'pp) (error nil)))
380
381(defvar semantic-edebug nil
382 "When non-nil, activate the interactive parsing debugger.
383Do not set this yourself. Call `semantic-debug'.")
384
385(defun semantic-elapsed-time (start end)
db9e401b 386 "Copied from elp.el. Was `elp-elapsed-time'.
cd1181db 387Arguments START and END bound the time being calculated."
98768aeb 388 (float-time (time-subtract end start)))
d3d82e7b
CY
389
390(defun bovinate (&optional clear)
391 "Parse the current buffer. Show output in a temp buffer.
392Optional argument CLEAR will clear the cache before parsing.
393If CLEAR is negative, it will do a full reparse, and also not display
394the output buffer."
395 (interactive "P")
396 (if clear (semantic-clear-toplevel-cache))
397 (if (eq clear '-) (setq clear -1))
398 (let* ((start (current-time))
399 (out (semantic-fetch-tags))
400 (end (current-time)))
401 (message "Retrieving tags took %.2f seconds."
402 (semantic-elapsed-time start end))
403 (when (or (null clear) (not (listp clear)))
404 (pop-to-buffer "*Parser Output*")
405 (require 'pp)
406 (erase-buffer)
407 (insert (pp-to-string out))
408 (goto-char (point-min)))))
409\f
410;;; Functions of the parser plug-in API
411;;
412;; Overload these functions to create new types of parsers.
413;;
414(define-overloadable-function semantic-parse-stream (stream nonterminal)
415 "Parse STREAM, starting at the first NONTERMINAL rule.
416For bovine and wisent based parsers, STREAM is from the output of
db9e401b 417`semantic-lex', and NONTERMINAL is a rule in the appropriate language
d3d82e7b
CY
418specific rules file.
419The default parser table used for bovine or wisent based parsers is
420`semantic--parse-table'.
421
422Must return a list: (STREAM TAGS) where STREAM is the unused elements
db9e401b
JB
423from STREAM, and TAGS is the list of semantic tags found; usually only
424one tag is returned with the exception of compound statements.")
d3d82e7b
CY
425
426(define-overloadable-function semantic-parse-changes ()
427 "Reparse changes in the current buffer.
428The list of changes are tracked as a series of overlays in the buffer.
429When overloading this function, use `semantic-changes-in-region' to
430analyze.")
431
432(define-overloadable-function semantic-parse-region
433 (start end &optional nonterminal depth returnonerror)
434 "Parse the area between START and END, and return any tags found.
435If END needs to be extended due to a lexical token being too large, it
436will be silently ignored.
437
438Optional arguments:
439NONTERMINAL is the rule to start parsing at.
cd1181db 440DEPTH specifies the lexical depth to descend for parsers that use
d3d82e7b
CY
441lexical analysis as their first step.
442RETURNONERROR specifies that parsing should stop on the first
443unmatched syntax encountered. When nil, parsing skips the syntax,
444adding it to the unmatched syntax cache.
445
ee7683eb 446Must return a list of semantic tags which have been cooked
d3d82e7b
CY
447\(repositioned properly) but which DO NOT HAVE OVERLAYS associated
448with them. When overloading this function, use `semantic--tag-expand'
449to cook raw tags.")
450
451(defun semantic-parse-region-default
452 (start end &optional nonterminal depth returnonerror)
453 "Parse the area between START and END, and return any tags found.
db9e401b
JB
454If END needs to be extended due to a lexical token being too large,
455it will be silently ignored.
d3d82e7b
CY
456Optional arguments:
457NONTERMINAL is the rule to start parsing at if it is known.
458DEPTH specifies the lexical depth to scan.
459RETURNONERROR specifies that parsing should end when encountering
460unterminated syntax."
461 (when (or (null semantic--parse-table) (eq semantic--parse-table t))
462 ;; If there is no table, or it was set to t, then we are here by
463 ;; some other mistake. Do not throw an error deep in the parser.
464 (error "No support found to parse buffer %S" (buffer-name)))
465 (save-restriction
466 (widen)
467 (when (or (< end start) (> end (point-max)))
468 (error "Invalid parse region bounds %S, %S" start end))
469 (nreverse
470 (semantic-repeat-parse-whole-stream
471 (or (cdr (assq start semantic-lex-block-streams))
472 (semantic-lex start end depth))
473 nonterminal returnonerror))))
474\f
475;;; Parsing functions
476;;
477(defun semantic-set-unmatched-syntax-cache (unmatched-syntax)
478 "Set the unmatched syntax cache.
479Argument UNMATCHED-SYNTAX is the syntax to set into the cache."
480 ;; This function is not actually called by the main parse loop.
481 ;; This is intended for use by semanticdb.
482 (setq semantic-unmatched-syntax-cache unmatched-syntax
483 semantic-unmatched-syntax-cache-check nil)
484 ;; Refresh the display of unmatched syntax tokens if enabled
485 (run-hook-with-args 'semantic-unmatched-syntax-hook
486 semantic-unmatched-syntax-cache))
487
488(defun semantic-clear-unmatched-syntax-cache ()
489 "Clear the cache of unmatched syntax tokens."
490 (setq semantic-unmatched-syntax-cache nil
491 semantic-unmatched-syntax-cache-check t))
492
493(defun semantic-unmatched-syntax-tokens ()
494 "Return the list of unmatched syntax tokens."
495 ;; If the cache need refresh then do a full re-parse.
496 (if (semantic--umatched-syntax-needs-refresh-p)
497 ;; To avoid a recursive call, temporarily disable
498 ;; `semantic-unmatched-syntax-hook'.
499 (let (semantic-unmatched-syntax-hook)
500 (condition-case nil
501 (progn
502 (semantic-clear-toplevel-cache)
503 (semantic-fetch-tags))
504 (quit
505 (message "semantic-unmatched-syntax-tokens:\
506 parsing of buffer canceled"))
507 )))
508 semantic-unmatched-syntax-cache)
509
510(defun semantic-clear-toplevel-cache ()
511 "Clear the toplevel tag cache for the current buffer.
512Clearing the cache will force a complete reparse next time a tag list
513is requested."
514 (interactive)
515 (run-hooks 'semantic-before-toplevel-cache-flush-hook)
516 (setq semantic--buffer-cache nil)
517 (semantic-clear-unmatched-syntax-cache)
518 (semantic-clear-parser-warnings)
519 ;; Nuke all semantic overlays. This is faster than deleting based
520 ;; on our data structure.
521 (let ((l (semantic-overlay-lists)))
522 (mapc 'semantic-delete-overlay-maybe (car l))
523 (mapc 'semantic-delete-overlay-maybe (cdr l))
524 )
525 (semantic-parse-tree-set-needs-rebuild)
526 ;; Remove this hook which tracks if a buffer is up to date or not.
527 (remove-hook 'after-change-functions 'semantic-change-function t)
528 ;; Old model. Delete someday.
529 ;;(run-hooks 'semantic-after-toplevel-bovinate-hook)
530
531 (run-hook-with-args 'semantic-after-toplevel-cache-change-hook
532 semantic--buffer-cache)
8bf997ef
CY
533
534 (setq semantic--completion-cache nil))
d3d82e7b
CY
535
536(defvar semantic-bovinate-nonterminal-check-obarray)
537
538(defun semantic--set-buffer-cache (tagtable)
db9e401b 539 "Set the toplevel tag cache to TAGTABLE."
d3d82e7b 540 (setq semantic--buffer-cache tagtable
06511291
CY
541 semantic-unmatched-syntax-cache-check nil)
542 ;; This is specific to the bovine parser.
543 (set (make-local-variable 'semantic-bovinate-nonterminal-check-obarray)
544 nil)
d3d82e7b
CY
545 (semantic-parse-tree-set-up-to-date)
546 (semantic-make-local-hook 'after-change-functions)
547 (add-hook 'after-change-functions 'semantic-change-function nil t)
548 (run-hook-with-args 'semantic-after-toplevel-cache-change-hook
549 semantic--buffer-cache)
8bf997ef 550 (setq semantic--completion-cache nil)
d3d82e7b
CY
551 ;; Refresh the display of unmatched syntax tokens if enabled
552 (run-hook-with-args 'semantic-unmatched-syntax-hook
553 semantic-unmatched-syntax-cache)
554 ;; Old Semantic 1.3 hook API. Maybe useful forever?
555 (run-hooks 'semantic-after-toplevel-bovinate-hook)
556 )
557
558(defvar semantic-working-type 'percent
fb7ada5f 559 "The type of working message to use when parsing.
d3d82e7b
CY
560'percent means we are doing a linear parse through the buffer.
561'dynamic means we are reparsing specific tags.")
562(semantic-varalias-obsolete 'semantic-bovination-working-type
eefa91db 563 'semantic-working-type "23.2")
d3d82e7b
CY
564
565(defvar semantic-minimum-working-buffer-size (* 1024 5)
fb7ada5f 566 "The minimum size of a buffer before working messages are displayed.
db9e401b 567Buffers smaller than this will parse silently.
bd2afec2 568Buffers larger than this will display the working progress bar.")
d3d82e7b
CY
569
570(defsubst semantic-parser-working-message (&optional arg)
571 "Return the message string displayed while parsing.
572If optional argument ARG is non-nil it is appended to the message
573string."
17e1f4bc
CY
574 (concat "Parsing"
575 (if arg (format " %s" arg))
576 (if semantic-parser-name (format " (%s)" semantic-parser-name))
577 "..."))
d3d82e7b
CY
578\f
579;;; Application Parser Entry Points
580;;
581;; The best way to call the parser from programs is via
582;; `semantic-fetch-tags'. This, in turn, uses other internal
583;; API functions which plug-in parsers can take advantage of.
584
585(defun semantic-fetch-tags ()
586 "Fetch semantic tags from the current buffer.
587If the buffer cache is up to date, return that.
588If the buffer cache is out of date, attempt an incremental reparse.
589If the buffer has not been parsed before, or if the incremental reparse
590fails, then parse the entire buffer.
bd2afec2 591If a lexical error had been previously discovered and the buffer
d3d82e7b
CY
592was marked unparseable, then do nothing, and return the cache."
593 (and
594 ;; Is this a semantic enabled buffer?
595 (semantic-active-p)
596 ;; Application hooks say the buffer is safe for parsing
597 (run-hook-with-args-until-failure
598 'semantic-before-toplevel-bovination-hook)
599 (run-hook-with-args-until-failure
600 'semantic--before-fetch-tags-hook)
601 ;; If the buffer was previously marked unparseable,
602 ;; then don't waste our time.
603 (not (semantic-parse-tree-unparseable-p))
604 ;; The parse tree actually needs to be refreshed
605 (not (semantic-parse-tree-up-to-date-p))
606 ;; So do it!
607 (let* ((gc-cons-threshold (max gc-cons-threshold 10000000))
608 (semantic-lex-block-streams nil)
609 (res nil))
610 (garbage-collect)
611 (cond
612
613;;;; Try the incremental parser to do a fast update.
614 ((semantic-parse-tree-needs-update-p)
615 (setq res (semantic-parse-changes))
616 (if (semantic-parse-tree-needs-rebuild-p)
617 ;; If the partial reparse fails, jump to a full reparse.
618 (semantic-fetch-tags)
619 ;; Clear the cache of unmatched syntax tokens
620 ;;
621 ;; NOTE TO SELF:
622 ;;
623 ;; Move this into the incremental parser. This is a bug.
624 ;;
625 (semantic-clear-unmatched-syntax-cache)
626 (run-hook-with-args ;; Let hooks know the updated tags
627 'semantic-after-partial-cache-change-hook res))
8bf997ef 628 (setq semantic--completion-cache nil))
d3d82e7b
CY
629
630;;;; Parse the whole system.
631 ((semantic-parse-tree-needs-rebuild-p)
62a81506
CY
632 ;; Use Emacs's built-in progress-reporter (only interactive).
633 (if noninteractive
634 (setq res (semantic-parse-region (point-min) (point-max)))
635 (let ((semantic--progress-reporter
636 (and (>= (point-max) semantic-minimum-working-buffer-size)
637 (eq semantic-working-type 'percent)
638 (make-progress-reporter
639 (semantic-parser-working-message (buffer-name))
640 0 100))))
641 (setq res (semantic-parse-region (point-min) (point-max)))
642 (if semantic--progress-reporter
643 (progress-reporter-done semantic--progress-reporter))))
d3d82e7b
CY
644
645 ;; Clear the caches when we see there were no errors.
646 ;; But preserve the unmatched syntax cache and warnings!
647 (let (semantic-unmatched-syntax-cache
648 semantic-unmatched-syntax-cache-check
649 semantic-parser-warnings)
650 (semantic-clear-toplevel-cache))
651 ;; Set up the new overlays
652 (semantic--tag-link-list-to-buffer res)
653 ;; Set up the cache with the new results
654 (semantic--set-buffer-cache res)
655 ))))
656
657 ;; Always return the current parse tree.
658 semantic--buffer-cache)
659
660(defun semantic-refresh-tags-safe ()
db9e401b 661 "Refresh the current buffer's tags safely.
d3d82e7b
CY
662
663Return non-nil if the refresh was successful.
664Return nil if there is some sort of syntax error preventing a reparse.
665
666Does nothing if the current buffer doesn't need reparsing."
667
668 ;; These checks actually occur in `semantic-fetch-tags', but if we
669 ;; do them here, then all the bovination hooks are not run, and
670 ;; we save lots of time.
671 (cond
672 ;; If the buffer was previously marked unparseable,
673 ;; then don't waste our time.
674 ((semantic-parse-tree-unparseable-p)
675 nil)
676 ;; The parse tree is already ok.
677 ((semantic-parse-tree-up-to-date-p)
678 t)
679 (t
680 (let* ((inhibit-quit nil)
681 (lexically-safe t)
682 )
683
684 (unwind-protect
685 ;; Perform the parsing.
686 (progn
687 (when (semantic-lex-catch-errors safe-refresh
688 (save-excursion (semantic-fetch-tags))
689 nil)
690 ;; If we are here, it is because the lexical step failed,
c80e3b4a 691 ;; probably due to unterminated lists or something like that.
d3d82e7b
CY
692
693 ;; We do nothing, and just wait for the next idle timer
694 ;; to go off. In the meantime, remember this, and make sure
695 ;; no other idle services can get executed.
696 (setq lexically-safe nil))
697 )
698 )
699 ;; Return if we are lexically safe
700 lexically-safe))))
701
702(defun semantic-bovinate-toplevel (&optional ignored)
db9e401b 703 "Backward compatibility function."
d3d82e7b 704 (semantic-fetch-tags))
e6e267fc 705(make-obsolete 'semantic-bovinate-toplevel 'semantic-fetch-tags "23.2")
d3d82e7b
CY
706
707;; Another approach is to let Emacs call the parser on idle time, when
708;; needed, use `semantic-fetch-available-tags' to only retrieve
709;; available tags, and setup the `semantic-after-*-hook' hooks to
710;; synchronize with new tags when they become available.
711
712(defsubst semantic-fetch-available-tags ()
713 "Fetch available semantic tags from the current buffer.
714That is, return tags currently in the cache without parsing the
715current buffer.
716Parse operations happen asynchronously when needed on Emacs idle time.
717Use the `semantic-after-toplevel-cache-change-hook' and
718`semantic-after-partial-cache-change-hook' hooks to synchronize with
719new tags when they become available."
720 semantic--buffer-cache)
721\f
722;;; Iterative parser helper function
723;;
724;; Iterative parsers are better than rule-based iterative functions
725;; in that they can handle obscure errors more cleanly.
726;;
727;; `semantic-repeat-parse-whole-stream' abstracts this action for
728;; other parser centric routines.
729;;
730(defun semantic-repeat-parse-whole-stream
731 (stream nonterm &optional returnonerror)
732 "Iteratively parse the entire stream STREAM starting with NONTERM.
733Optional argument RETURNONERROR indicates that the parser should exit
734with the current results on a parse error.
735This function returns semantic tags without overlays."
736 (let ((result nil)
737 (case-fold-search semantic-case-fold)
738 nontermsym tag)
739 (while stream
740 (setq nontermsym (semantic-parse-stream stream nonterm)
741 tag (car (cdr nontermsym)))
742 (if (not nontermsym)
743 (error "Parse error @ %d" (car (cdr (car stream)))))
744 (if (eq (car nontermsym) stream)
745 (error "Parser error: Infinite loop?"))
746 (if tag
747 (if (car tag)
748 (setq tag (mapcar
749 #'(lambda (tag)
750 ;; Set the 'reparse-symbol property to
751 ;; NONTERM unless it was already setup
752 ;; by a tag expander
753 (or (semantic--tag-get-property
754 tag 'reparse-symbol)
755 (semantic--tag-put-property
756 tag 'reparse-symbol nonterm))
757 tag)
758 (semantic--tag-expand tag))
759 result (append tag result))
760 ;; No error in this case, a purposeful nil means don't
761 ;; store anything.
762 )
763 (if returnonerror
764 (setq stream nil)
765 ;; The current item in the stream didn't match, so add it to
766 ;; the list of syntax items which didn't match.
767 (setq semantic-unmatched-syntax-cache
768 (cons (car stream) semantic-unmatched-syntax-cache))
769 ))
770 ;; Designated to ignore.
771 (setq stream (car nontermsym))
772 (if stream
44e97401 773 ;; Use Emacs's built-in progress reporter:
d3d82e7b
CY
774 (and (boundp 'semantic--progress-reporter)
775 semantic--progress-reporter
aa8724ae 776 (eq semantic-working-type 'percent)
d3d82e7b
CY
777 (progress-reporter-update
778 semantic--progress-reporter
779 (/ (* 100 (semantic-lex-token-start (car stream)))
780 (point-max))))))
781 result))
782\f
783;;; Parsing Warnings:
784;;
785;; Parsing a buffer may result in non-critical things that we should
786;; alert the user to without interrupting the normal flow.
787;;
788;; Any parser can use this API to provide a list of warnings during a
789;; parse which a user may want to investigate.
790(defvar semantic-parser-warnings nil
791 "A list of parser warnings since the last full reparse.")
792(make-variable-buffer-local 'semantic-parser-warnings)
793
794(defun semantic-clear-parser-warnings ()
795 "Clear the current list of parser warnings for this buffer."
796 (setq semantic-parser-warnings nil))
797
798(defun semantic-push-parser-warning (warning start end)
799 "Add a parser WARNING that covers text from START to END."
800 (setq semantic-parser-warnings
801 (cons (cons warning (cons start end))
802 semantic-parser-warnings)))
803
804(defun semantic-dump-parser-warnings ()
805 "Dump any parser warnings."
806 (interactive)
807 (if semantic-parser-warnings
808 (let ((pw semantic-parser-warnings))
809 (pop-to-buffer "*Parser Warnings*")
810 (require 'pp)
811 (erase-buffer)
812 (insert (pp-to-string pw))
813 (goto-char (point-min)))
814 (message "No parser warnings.")))
815
816
817\f
818;;; Compatibility:
819;;
820;; Semantic 1.x parser action helper functions, used by some parsers.
821;; Please move away from these functions, and try using semantic 2.x
822;; interfaces instead.
823;;
824(defsubst semantic-bovinate-region-until-error
825 (start end nonterm &optional depth)
826 "NOTE: Use `semantic-parse-region' instead.
827
828Bovinate between START and END starting with NONTERM.
829Optional DEPTH specifies how many levels of parenthesis to enter.
830This command will parse until an error is encountered, and return
831the list of everything found until that moment.
832This is meant for finding variable definitions at the beginning of
833code blocks in methods. If `bovine-inner-scope' can also support
834commands, use `semantic-bovinate-from-nonterminal-full'."
835 (semantic-parse-region start end nonterm depth t))
836(make-obsolete 'semantic-bovinate-region-until-error
e6e267fc 837 'semantic-parse-region "23.2")
d3d82e7b
CY
838
839(defsubst semantic-bovinate-from-nonterminal
840 (start end nonterm &optional depth length)
841 "Bovinate from within a nonterminal lambda from START to END.
842Argument NONTERM is the nonterminal symbol to start with.
843Optional argument DEPTH is the depth of lists to dive into. When used
844in a `lambda' of a MATCH-LIST, there is no need to include a START and
845END part.
846Optional argument LENGTH specifies we are only interested in LENGTH
847tokens."
848 (car-safe (cdr (semantic-parse-stream
849 (semantic-lex start end (or depth 1) length)
850 nonterm))))
851
852(defsubst semantic-bovinate-from-nonterminal-full
853 (start end nonterm &optional depth)
854 "NOTE: Use `semantic-parse-region' instead.
855
856Bovinate from within a nonterminal lambda from START to END.
857Iterates until all the space between START and END is exhausted.
858Argument NONTERM is the nonterminal symbol to start with.
859If NONTERM is nil, use `bovine-block-toplevel'.
860Optional argument DEPTH is the depth of lists to dive into.
861When used in a `lambda' of a MATCH-LIST, there is no need to include
862a START and END part."
863 (semantic-parse-region start end nonterm (or depth 1)))
864(make-obsolete 'semantic-bovinate-from-nonterminal-full
e6e267fc 865 'semantic-parse-region "23.2")
d3d82e7b 866
b82525f2
CY
867;;; User interface
868
8bf997ef
CY
869(defun semantic-force-refresh ()
870 "Force a full refresh of the current buffer's tags.
871Throw away all the old tags, and recreate the tag database."
872 (interactive)
873 (semantic-clear-toplevel-cache)
715f35a5
CY
874 (semantic-fetch-tags)
875 (message "Buffer reparsed."))
8bf997ef
CY
876
877(defvar semantic-mode-map
715f35a5 878 (let ((map (make-sparse-keymap)))
8bf997ef 879 ;; Key bindings:
8bf997ef
CY
880 ;; (define-key km "f" 'senator-search-set-tag-class-filter)
881 ;; (define-key km "i" 'senator-isearch-toggle-semantic-mode)
882 (define-key map "\C-c,j" 'semantic-complete-jump-local)
883 (define-key map "\C-c,J" 'semantic-complete-jump)
dd9af436 884 (define-key map "\C-c,m" 'semantic-complete-jump-local-members)
8bf997ef
CY
885 (define-key map "\C-c,g" 'semantic-symref-symbol)
886 (define-key map "\C-c,G" 'semantic-symref)
887 (define-key map "\C-c,p" 'senator-previous-tag)
888 (define-key map "\C-c,n" 'senator-next-tag)
889 (define-key map "\C-c,u" 'senator-go-to-up-reference)
890 (define-key map "\C-c, " 'semantic-complete-analyze-inline)
891 (define-key map "\C-c,\C-w" 'senator-kill-tag)
892 (define-key map "\C-c,\M-w" 'senator-copy-tag)
893 (define-key map "\C-c,\C-y" 'senator-yank-tag)
894 (define-key map "\C-c,r" 'senator-copy-tag-to-register)
dd9af436 895 (define-key map "\C-c,," 'semantic-force-refresh)
8bf997ef
CY
896 (define-key map [?\C-c ?, up] 'senator-transpose-tags-up)
897 (define-key map [?\C-c ?, down] 'senator-transpose-tags-down)
898 (define-key map "\C-c,l" 'semantic-analyze-possible-completions)
715f35a5
CY
899 ;; This hack avoids showing the CEDET menu twice if ede-minor-mode
900 ;; and Semantic are both enabled. Is there a better way?
901 (define-key map [menu-bar cedet-menu]
902 (list 'menu-item "Development" cedet-menu-map
903 :enable (quote (not (bound-and-true-p global-ede-mode)))))
8bf997ef
CY
904 ;; (define-key km "-" 'senator-fold-tag)
905 ;; (define-key km "+" 'senator-unfold-tag)
906 map))
907
715f35a5
CY
908;; Activate the Semantic items in cedet-menu-map
909(let ((navigate-menu (make-sparse-keymap "Navigate Tags"))
910 (edit-menu (make-sparse-keymap "Edit Tags")))
911
912 ;; Edit Tags submenu:
913 (define-key edit-menu [semantic-analyze-possible-completions]
914 '(menu-item "List Completions" semantic-analyze-possible-completions
bf659b3f 915 :enable (semantic-active-p)
715f35a5
CY
916 :help "Display a list of completions for the tag at point"))
917 (define-key edit-menu [semantic-complete-analyze-inline]
918 '(menu-item "Complete Tag Inline" semantic-complete-analyze-inline
bf659b3f 919 :enable (semantic-active-p)
715f35a5
CY
920 :help "Display inline completion for the tag at point"))
921 (define-key edit-menu [semantic-completion-separator]
922 '("--"))
923 (define-key edit-menu [senator-transpose-tags-down]
924 '(menu-item "Transpose Tags Down" senator-transpose-tags-down
bf659b3f
DE
925 :enable (and (semantic-active-p)
926 (semantic-current-tag))
715f35a5
CY
927 :help "Transpose the current tag and the next tag"))
928 (define-key edit-menu [senator-transpose-tags-up]
929 '(menu-item "Transpose Tags Up" senator-transpose-tags-up
bf659b3f
DE
930 :enable (and (semantic-active-p)
931 (semantic-current-tag))
715f35a5
CY
932 :help "Transpose the current tag and the previous tag"))
933 (define-key edit-menu [semantic-edit-separator]
934 '("--"))
935 (define-key edit-menu [senator-yank-tag]
936 '(menu-item "Yank Tag" senator-yank-tag
bf659b3f 937 :enable (not (ring-empty-p senator-tag-ring))
715f35a5
CY
938 :help "Yank the head of the tag ring into the buffer"))
939 (define-key edit-menu [senator-copy-tag-to-register]
940 '(menu-item "Copy Tag To Register" senator-copy-tag-to-register
bf659b3f
DE
941 :enable (and (semantic-active-p)
942 (semantic-current-tag))
715f35a5
CY
943 :help "Yank the head of the tag ring into the buffer"))
944 (define-key edit-menu [senator-copy-tag]
945 '(menu-item "Copy Tag" senator-copy-tag
bf659b3f
DE
946 :enable (and (semantic-active-p)
947 (semantic-current-tag))
715f35a5
CY
948 :help "Copy the current tag to the tag ring"))
949 (define-key edit-menu [senator-kill-tag]
950 '(menu-item "Kill Tag" senator-kill-tag
bf659b3f
DE
951 :enable (and (semantic-active-p)
952 (semantic-current-tag))
715f35a5
CY
953 :help "Kill the current tag, and copy it to the tag ring"))
954
955 ;; Navigate Tags submenu:
956 (define-key navigate-menu [senator-narrow-to-defun]
957 '(menu-item "Narrow to Tag" senator-narrow-to-defun
bf659b3f
DE
958 :enable (and (semantic-active-p)
959 (semantic-current-tag))
715f35a5
CY
960 :help "Narrow the buffer to the bounds of the current tag"))
961 (define-key navigate-menu [semantic-narrow-to-defun-separator]
962 '("--"))
963 (define-key navigate-menu [semantic-symref-symbol]
964 '(menu-item "Find Tag References..." semantic-symref-symbol
bf659b3f 965 :enable (semantic-active-p)
715f35a5
CY
966 :help "Read a tag and list the references to it"))
967 (define-key navigate-menu [semantic-complete-jump]
968 '(menu-item "Find Tag Globally..." semantic-complete-jump
bf659b3f 969 :enable (semantic-active-p)
715f35a5 970 :help "Read a tag name and find it in the current project"))
dd9af436
CY
971 (define-key navigate-menu [semantic-complete-jump-local-members]
972 '(menu-item "Find Local Members ..." semantic-complete-jump-local-members
bf659b3f 973 :enable (semantic-active-p)
dd9af436 974 :help "Read a tag name and find a local member with that name"))
715f35a5
CY
975 (define-key navigate-menu [semantic-complete-jump-local]
976 '(menu-item "Find Tag in This Buffer..." semantic-complete-jump-local
bf659b3f 977 :enable (semantic-active-p)
715f35a5
CY
978 :help "Read a tag name and find it in this buffer"))
979 (define-key navigate-menu [semantic-navigation-separator]
980 '("--"))
981 (define-key navigate-menu [senator-go-to-up-reference]
982 '(menu-item "Parent Tag" senator-go-to-up-reference
bf659b3f 983 :enable (semantic-active-p)
cd1181db 984 :help "Navigate up one reference by tag"))
715f35a5
CY
985 (define-key navigate-menu [senator-next-tag]
986 '(menu-item "Next Tag" senator-next-tag
bf659b3f 987 :enable (semantic-active-p)
715f35a5
CY
988 :help "Go to the next tag"))
989 (define-key navigate-menu [senator-previous-tag]
990 '(menu-item "Previous Tag" senator-previous-tag
bf659b3f 991 :enable (semantic-active-p)
715f35a5
CY
992 :help "Go to the previous tag"))
993
994 ;; Top level menu items:
995 (define-key cedet-menu-map [semantic-force-refresh]
996 '(menu-item "Reparse Buffer" semantic-force-refresh
cd1181db 997 :help "Force a full reparse of the current buffer"
bf659b3f
DE
998 :visible semantic-mode
999 :enable (semantic-active-p)))
715f35a5 1000 (define-key cedet-menu-map [semantic-edit-menu]
a2095e2e
CY
1001 `(menu-item "Edit Tags" ,edit-menu
1002 :visible semantic-mode))
715f35a5 1003 (define-key cedet-menu-map [navigate-menu]
a2095e2e
CY
1004 `(menu-item "Navigate Tags" ,navigate-menu
1005 :visible semantic-mode))
715f35a5
CY
1006 (define-key cedet-menu-map [semantic-options-separator]
1007 '("--"))
1008 (define-key cedet-menu-map [global-semantic-highlight-func-mode]
a2095e2e
CY
1009 '(menu-item "Highlight Current Function" global-semantic-highlight-func-mode
1010 :help "Highlight the tag at point"
1011 :visible semantic-mode
1012 :button (:toggle . global-semantic-highlight-func-mode)))
62a81506
CY
1013 (define-key cedet-menu-map [global-semantic-stickyfunc-mode]
1014 '(menu-item "Stick Top Tag to Headerline" global-semantic-stickyfunc-mode
1015 :help "Stick the tag scrolled off the top of the buffer into the header line"
1016 :visible semantic-mode
1017 :button (:toggle . (bound-and-true-p
1018 global-semantic-stickyfunc-mode))))
715f35a5 1019 (define-key cedet-menu-map [global-semantic-decoration-mode]
a2095e2e
CY
1020 '(menu-item "Decorate Tags" global-semantic-decoration-mode
1021 :help "Decorate tags based on tag attributes"
1022 :visible semantic-mode
1023 :button (:toggle . (bound-and-true-p
1024 global-semantic-decoration-mode))))
715f35a5 1025 (define-key cedet-menu-map [global-semantic-idle-completions-mode]
a2095e2e
CY
1026 '(menu-item "Show Tag Completions" global-semantic-idle-completions-mode
1027 :help "Show tag completions when idle"
1028 :visible semantic-mode
725bff06 1029 :enable global-semantic-idle-scheduler-mode
a2095e2e 1030 :button (:toggle . global-semantic-idle-completions-mode)))
715f35a5 1031 (define-key cedet-menu-map [global-semantic-idle-summary-mode]
a2095e2e
CY
1032 '(menu-item "Show Tag Summaries" global-semantic-idle-summary-mode
1033 :help "Show tag summaries when idle"
1034 :visible semantic-mode
725bff06 1035 :enable global-semantic-idle-scheduler-mode
a2095e2e 1036 :button (:toggle . global-semantic-idle-summary-mode)))
715f35a5 1037 (define-key cedet-menu-map [global-semantic-idle-scheduler-mode]
a2095e2e
CY
1038 '(menu-item "Reparse When Idle" global-semantic-idle-scheduler-mode
1039 :help "Keep a buffer's parse tree up to date when idle"
1040 :visible semantic-mode
725bff06
CY
1041 :button (:toggle . global-semantic-idle-scheduler-mode)))
1042 (define-key cedet-menu-map [global-semanticdb-minor-mode]
1043 '(menu-item "Semantic Database" global-semanticdb-minor-mode
1044 :help "Store tag information in a database"
1045 :visible semantic-mode
1046 :button (:toggle . global-semanticdb-minor-mode))))
715f35a5 1047
da6062e6 1048;; The `semantic-mode' command, in conjunction with the
715f35a5 1049;; `semantic-default-submodes' variable, toggles Semantic's various
4963739e 1050;; auxiliary minor modes.
b82525f2
CY
1051
1052(defvar semantic-load-system-cache-loaded nil
db9e401b 1053 "Non-nil when the Semantic system caches have been loaded.
b82525f2
CY
1054Prevent this load system from loading files in twice.")
1055
1056(defconst semantic-submode-list
1057 '(global-semantic-highlight-func-mode
1058 global-semantic-decoration-mode
1059 global-semantic-stickyfunc-mode
1060 global-semantic-idle-completions-mode
1061 global-semantic-idle-scheduler-mode
1062 global-semanticdb-minor-mode
1063 global-semantic-idle-summary-mode
62a81506
CY
1064 global-semantic-mru-bookmark-mode
1065 global-cedet-m3-minor-mode
1066 global-semantic-idle-local-symbol-highlight-mode
1067 global-semantic-highlight-edits-mode
1068 global-semantic-show-unmatched-syntax-mode
1069 global-semantic-show-parser-state-mode)
bd2afec2 1070 "List of auxiliary minor modes in the Semantic package.")
b82525f2
CY
1071
1072;;;###autoload
1073(defcustom semantic-default-submodes
1074 '(global-semantic-idle-scheduler-mode global-semanticdb-minor-mode)
bd2afec2 1075 "List of auxiliary Semantic minor modes enabled by `semantic-mode'.
b82525f2
CY
1076The possible elements of this list include the following:
1077
725bff06
CY
1078 `global-semanticdb-minor-mode' - Maintain tag database.
1079 `global-semantic-idle-scheduler-mode' - Reparse buffer when idle.
1080 `global-semantic-idle-summary-mode' - Show summary of tag at point.
1081 `global-semantic-idle-completions-mode' - Show completions when idle.
1082 `global-semantic-decoration-mode' - Additional tag decorations.
1083 `global-semantic-highlight-func-mode' - Highlight the current tag.
1084 `global-semantic-stickyfunc-mode' - Show current fun in header line.
1085 `global-semantic-mru-bookmark-mode' - Provide `switch-to-buffer'-like
62a81506
CY
1086 keybinding for tag names.
1087 `global-cedet-m3-minor-mode' - A mouse 3 context menu.
1088 `global-semantic-idle-local-symbol-highlight-mode' - Highlight references
1089 of the symbol under point.
1090The following modes are more targeted at people who want to see
1091 some internal information of the semantic parser in action:
1092 `global-semantic-highlight-edits-mode' - Visualize incremental parser by
1093 highlighting not-yet parsed changes.
1094 `global-semantic-show-unmatched-syntax-mode' - Highlight unmatched lexical
1095 syntax tokens.
1096 `global-semantic-show-parser-state-mode' - Display the parser cache state."
b82525f2
CY
1097 :group 'semantic
1098 :type `(set ,@(mapcar (lambda (c) (list 'const c))
1099 semantic-submode-list)))
1100
1101;;;###autoload
1102(define-minor-mode semantic-mode
ac6c8639
CY
1103 "Toggle parser features (Semantic mode).
1104With a prefix argument ARG, enable Semantic mode if ARG is
1105positive, and disable it otherwise. If called from Lisp, enable
1106Semantic mode if ARG is omitted or nil.
b82525f2
CY
1107
1108In Semantic mode, Emacs parses the buffers you visit for their
1109semantic content. This information is used by a variety of
4963739e 1110auxiliary minor modes, listed in `semantic-default-submodes';
b82525f2 1111all the minor modes in this list are also enabled when you enable
8bf997ef
CY
1112Semantic mode.
1113
1114\\{semantic-mode-map}"
1115 :global t
b82525f2
CY
1116 :group 'semantic
1117 (if semantic-mode
1118 ;; Turn on Semantic mode
1119 (progn
4963739e 1120 ;; Enable all the global auxiliary minor modes in
8bf997ef 1121 ;; `semantic-submode-list'.
b82525f2
CY
1122 (dolist (mode semantic-submode-list)
1123 (if (memq mode semantic-default-submodes)
1124 (funcall mode 1)))
1125 (unless semantic-load-system-cache-loaded
1126 (setq semantic-load-system-cache-loaded t)
1127 (when (and (boundp 'semanticdb-default-system-save-directory)
1128 (stringp semanticdb-default-system-save-directory)
1129 (file-exists-p semanticdb-default-system-save-directory))
17e1f4bc 1130 (require 'semantic/db-ebrowse)
b82525f2 1131 (semanticdb-load-ebrowse-caches)))
d436f538 1132 (add-hook 'mode-local-init-hook 'semantic-new-buffer-fcn)
dd9af436
CY
1133 ;; Add semantic-ia-complete-symbol to
1134 ;; completion-at-point-functions, so that it is run from
1135 ;; M-TAB.
1136 (add-hook 'completion-at-point-functions
1137 'semantic-completion-at-point-function)
ebf5c4f5
CY
1138 (if global-ede-mode
1139 (define-key cedet-menu-map [cedet-menu-separator] '("--")))
d436f538
CY
1140 (dolist (b (buffer-list))
1141 (with-current-buffer b
1142 (semantic-new-buffer-fcn))))
62a81506
CY
1143 ;; Disable Semantic features. Removing everything Semantic has
1144 ;; introduced in the buffer is pretty much futile, but we have to
1145 ;; clean the hooks and delete Semantic-related overlays, so that
1146 ;; Semantic can be re-activated cleanly.
b82525f2 1147 (remove-hook 'mode-local-init-hook 'semantic-new-buffer-fcn)
dd9af436
CY
1148 (remove-hook 'completion-at-point-functions
1149 'semantic-completion-at-point-function)
62a81506
CY
1150 (remove-hook 'after-change-functions
1151 'semantic-change-function)
ebf5c4f5
CY
1152 (define-key cedet-menu-map [cedet-menu-separator] nil)
1153 (define-key cedet-menu-map [semantic-options-separator] nil)
b82525f2 1154 ;; FIXME: handle semanticdb-load-ebrowse-caches
b82525f2
CY
1155 (dolist (mode semantic-submode-list)
1156 (if (and (boundp mode) (eval mode))
62a81506
CY
1157 (funcall mode -1)))
1158 ;; Unlink buffer and clear cache
1159 (semantic--tag-unlink-cache-from-buffer)
1160 (setq semantic--buffer-cache nil)
1161 ;; Make sure we run the setup function if Semantic gets
1162 ;; re-activated.
1163 (setq semantic-new-buffer-fcn-was-run nil)))
b82525f2 1164
dd9af436
CY
1165(defun semantic-completion-at-point-function ()
1166 'semantic-ia-complete-symbol)
1167
af7b5a91
CY
1168;;; Autoload some functions that are not in semantic/loaddefs
1169
1170(autoload 'global-semantic-idle-completions-mode "semantic/idle"
1171 "Toggle global use of `semantic-idle-completions-mode'.
1172If ARG is positive, enable, if it is negative, disable.
1173If ARG is nil, then toggle." t nil)
1174
1175(autoload 'semantic-idle-completions-mode "semantic/idle"
1176 "Display a list of possible completions in a tooltip.
1177
1178This is a minor mode which performs actions during idle time.
1179With prefix argument ARG, turn on if positive, otherwise off. The
1180minor mode can be turned on only if semantic feature is available and
1181the current buffer was set up for parsing. Return non-nil if the
1182minor mode is enabled." t nil)
1183
1184(autoload 'global-semantic-idle-summary-mode "semantic/idle"
1185 "Toggle global use of `semantic-idle-summary-mode'.
1186If ARG is positive, enable, if it is negative, disable.
1187If ARG is nil, then toggle." t nil)
1188
1189(autoload 'semantic-idle-summary-mode "semantic/idle"
1190 "Display a tag summary of the lexical token under the cursor.
1191Call `semantic-idle-summary-current-symbol-info' for getting the
1192current tag to display information.
1193
1194This is a minor mode which performs actions during idle time.
1195With prefix argument ARG, turn on if positive, otherwise off. The
1196minor mode can be turned on only if semantic feature is available and
1197the current buffer was set up for parsing. Return non-nil if the
1198minor mode is enabled." t nil)
b82525f2 1199
62a81506
CY
1200(autoload 'global-semantic-idle-local-symbol-highlight-mode "semantic/idle"
1201 "Highlight the tag and symbol references of the symbol under point.
1202Call `semantic-analyze-current-context' to find the reference tag.
1203Call `semantic-symref-hits-in-region' to identify local references." t nil)
1204
e6e267fc
CY
1205(autoload 'srecode-template-setup-parser "srecode/srecode-template"
1206 "Set up buffer for parsing SRecode template files." t nil)
1207
d3d82e7b
CY
1208(provide 'semantic)
1209
d3d82e7b
CY
1210;; Semantic-util is a part of the semantic API. Include it last
1211;; because it depends on semantic.
9d389824 1212(require 'semantic/util)
a60f2e7b 1213
b82525f2
CY
1214;; (require 'semantic/load)
1215
a60f2e7b 1216;;; semantic.el ends here