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