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