1 ;;; semantic-fw.el --- Framework for Semantic
3 ;;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;;; 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Semantic has several core features shared across it's lex/parse/util
26 ;; stages. This used to clutter semantic.el some. These routines are all
27 ;; simple things that are not parser specific, but aid in making
28 ;; semantic flexible and compatible amongst different Emacs platforms.
34 (require 'semantic/loaddefs)
38 (if (featurep 'xemacs)
40 (defalias 'semantic-buffer-local-value 'symbol-value-in-buffer)
41 (defalias 'semantic-overlay-live-p
43 (and (extent-live-p o)
44 (not (extent-detached-p o))
45 (bufferp (extent-buffer o)))))
46 (defalias 'semantic-make-overlay
47 (lambda (beg end &optional buffer &rest rest)
48 "Xemacs `make-extent', supporting the front/rear advance options."
49 (let ((ol (make-extent beg end buffer)))
51 (set-extent-property ol 'start-open (car rest))
52 (setq rest (cdr rest)))
54 (set-extent-property ol 'end-open (car rest)))
56 (defalias 'semantic-overlay-put 'set-extent-property)
57 (defalias 'semantic-overlay-get 'extent-property)
58 (defalias 'semantic-overlay-properties 'extent-properties)
59 (defalias 'semantic-overlay-move 'set-extent-endpoints)
60 (defalias 'semantic-overlay-delete 'delete-extent)
61 (defalias 'semantic-overlays-at
64 (extent-list nil pos pos)
67 (defalias 'semantic-overlays-in
68 (lambda (beg end) (extent-list nil beg end)))
69 (defalias 'semantic-overlay-buffer 'extent-buffer)
70 (defalias 'semantic-overlay-start 'extent-start-position)
71 (defalias 'semantic-overlay-end 'extent-end-position)
72 (defalias 'semantic-overlay-size 'extent-length)
73 (defalias 'semantic-overlay-next-change 'next-extent-change)
74 (defalias 'semantic-overlay-previous-change 'previous-extent-change)
75 (defalias 'semantic-overlay-lists
76 (lambda () (list (extent-list))))
77 (defalias 'semantic-overlay-p 'extentp)
78 (defalias 'semantic-event-window 'event-window)
79 (defun semantic-read-event ()
80 (let ((event (next-command-event)))
81 (if (key-press-event-p event)
82 (let ((c (event-to-character event)))
83 (if (char-equal c (quit-char))
87 (defun semantic-popup-menu (menu)
88 "Blockinig version of `popup-menu'"
91 (while (popup-up-p) (dispatch-event (next-event))))
94 (defalias 'semantic-buffer-local-value 'buffer-local-value)
95 (defalias 'semantic-overlay-live-p 'overlay-buffer)
96 (defalias 'semantic-make-overlay 'make-overlay)
97 (defalias 'semantic-overlay-put 'overlay-put)
98 (defalias 'semantic-overlay-get 'overlay-get)
99 (defalias 'semantic-overlay-properties 'overlay-properties)
100 (defalias 'semantic-overlay-move 'move-overlay)
101 (defalias 'semantic-overlay-delete 'delete-overlay)
102 (defalias 'semantic-overlays-at 'overlays-at)
103 (defalias 'semantic-overlays-in 'overlays-in)
104 (defalias 'semantic-overlay-buffer 'overlay-buffer)
105 (defalias 'semantic-overlay-start 'overlay-start)
106 (defalias 'semantic-overlay-end 'overlay-end)
107 (defalias 'semantic-overlay-size 'overlay-size)
108 (defalias 'semantic-overlay-next-change 'next-overlay-change)
109 (defalias 'semantic-overlay-previous-change 'previous-overlay-change)
110 (defalias 'semantic-overlay-lists 'overlay-lists)
111 (defalias 'semantic-overlay-p 'overlayp)
112 (defalias 'semantic-read-event 'read-event)
113 (defalias 'semantic-popup-menu 'popup-menu)
114 (defun semantic-event-window (event)
115 "Extract the window from EVENT."
116 (car (car (cdr event))))
119 (if (and (not (featurep 'xemacs))
120 (>= emacs-major-version 21))
121 (defalias 'semantic-make-local-hook 'identity)
122 (defalias 'semantic-make-local-hook 'make-local-hook)
125 (if (featurep 'xemacs)
126 (defalias 'semantic-mode-line-update 'redraw-modeline)
127 (defalias 'semantic-mode-line-update 'force-mode-line-update))
129 ;; Since Emacs 22 major mode functions should use `run-mode-hooks' to
130 ;; run major mode hooks.
131 (defalias 'semantic-run-mode-hooks
132 (if (fboundp 'run-mode-hooks)
136 ;; Fancy compat useage now handled in cedet-compat
137 (defalias 'semantic-subst-char-in-string 'subst-char-in-string)
140 (defun semantic-delete-overlay-maybe (overlay)
141 "Delete OVERLAY if it is a semantic token overlay."
142 (if (semantic-overlay-get overlay 'semantic)
143 (semantic-overlay-delete overlay)))
145 (defalias 'semantic-compile-warn
147 (if (fboundp 'byte-compile-warn)
151 (if (not (fboundp 'string-to-number))
152 (defalias 'string-to-number 'string-to-int))
154 ;;; Menu Item compatibility
156 (defun semantic-menu-item (item)
157 "Build an XEmacs compatible menu item from vector ITEM.
158 That is remove the unsupported :help stuff."
159 (if (featurep 'xemacs)
160 (let ((n (length item))
164 (setq slot (aref item i))
165 (if (and (keywordp slot)
168 (setq l (cons slot l)))
170 (apply #'vector (nreverse l)))
173 ;;; Positional Data Cache
175 (defvar semantic-cache-data-overlays nil
176 "List of all overlays waiting to be flushed.")
178 (defun semantic-cache-data-to-buffer (buffer start end value name &optional lifespan)
179 "In BUFFER over the region START END, remember VALUE.
180 NAME specifies a special name that can be searched for later to
181 recover the cached data with `semantic-get-cache-data'.
182 LIFESPAN indicates how long the data cache will be remembered.
183 The default LIFESPAN is 'end-of-command.
184 Possible Lifespans are:
185 'end-of-command - Remove the cache at the end of the currently
187 'exit-cache-zone - Remove when point leaves the overlay at the
188 end of the currently executing command."
189 ;; Check if LIFESPAN is valid before to create any overlay
190 (or lifespan (setq lifespan 'end-of-command))
191 (or (memq lifespan '(end-of-command exit-cache-zone))
192 (error "semantic-cache-data-to-buffer: Unknown LIFESPAN: %s"
194 (let ((o (semantic-make-overlay start end buffer)))
195 (semantic-overlay-put o 'cache-name name)
196 (semantic-overlay-put o 'cached-value value)
197 (semantic-overlay-put o 'lifespan lifespan)
198 (setq semantic-cache-data-overlays
199 (cons o semantic-cache-data-overlays))
200 ;;(message "Adding to cache: %s" o)
201 (add-hook 'post-command-hook 'semantic-cache-data-post-command-hook)
204 (defun semantic-cache-data-post-command-hook ()
205 "Flush `semantic-cache-data-overlays' based 'lifespan property.
206 Remove self from `post-command-hook' if it is empty."
208 (oldcache semantic-cache-data-overlays))
210 (let* ((o (car oldcache))
211 (life (semantic-overlay-get o 'lifespan))
213 (if (or (eq life 'end-of-command)
214 (and (eq life 'exit-cache-zone)
215 (not (member o (semantic-overlays-at (point))))))
217 ;;(message "Removing from cache: %s" o)
218 (semantic-overlay-delete o)
220 (setq newcache (cons o newcache))))
221 (setq oldcache (cdr oldcache)))
222 (setq semantic-cache-data-overlays (nreverse newcache)))
224 ;; Remove ourselves if we have removed all overlays.
225 (unless semantic-cache-data-overlays
226 (remove-hook 'post-command-hook
227 'semantic-cache-data-post-command-hook)))
229 (defun semantic-get-cache-data (name &optional point)
230 "Get cached data with NAME from optional POINT."
232 (if point (goto-char point))
233 (let ((o (semantic-overlays-at (point)))
235 (while (and (not ans) o)
236 (if (equal (semantic-overlay-get (car o) 'cache-name) name)
240 (semantic-overlay-get ans 'cached-value)))))
242 (defun semantic-test-data-cache ()
243 "Test the data cache."
245 (let ((data '(a b c)))
247 (set-buffer (get-buffer-create " *semantic-test-data-cache*"))
249 (insert "The Moose is Loose")
250 (goto-char (point-min))
251 (semantic-cache-data-to-buffer (current-buffer) (point) (+ (point) 5)
252 data 'moose 'exit-cache-zone)
253 (if (equal (semantic-get-cache-data 'moose) data)
254 (message "Successfully retrieved cached data.")
255 (error "Failed to retrieve cached data"))
258 ;;; Obsoleting various functions & variables
260 (defun semantic-overload-symbol-from-function (name)
261 "Return the symbol for overload used by NAME, the defined symbol."
262 (let ((sym-name (symbol-name name)))
263 (if (string-match "^semantic-" sym-name)
264 (intern (substring sym-name (match-end 0)))
267 (defun semantic-alias-obsolete (oldfnalias newfn)
268 "Make OLDFNALIAS an alias for NEWFN.
269 Mark OLDFNALIAS as obsolete, such that the byte compiler
270 will throw a warning when it encounters this symbol."
271 (defalias oldfnalias newfn)
272 (make-obsolete oldfnalias newfn)
273 (when (and (function-overload-p newfn)
274 (not (overload-obsoleted-by newfn))
275 ;; Only throw this warning when byte compiling things.
276 (boundp 'byte-compile-current-file)
277 byte-compile-current-file
278 (not (string-match "cedet" byte-compile-current-file))
280 (make-obsolete-overload oldfnalias newfn)
281 (semantic-compile-warn
282 "%s: `%s' obsoletes overload `%s'"
283 byte-compile-current-file
285 (semantic-overload-symbol-from-function oldfnalias))
288 (defun semantic-varalias-obsolete (oldvaralias newvar)
289 "Make OLDVARALIAS an alias for variable NEWVAR.
290 Mark OLDVARALIAS as obsolete, such that the byte compiler
291 will throw a warning when it encounters this symbol."
292 (make-obsolete-variable oldvaralias newvar)
294 (defvaralias oldvaralias newvar)
296 ;; Only throw this warning when byte compiling things.
297 (when (and (boundp 'byte-compile-current-file)
298 byte-compile-current-file)
299 (semantic-compile-warn
300 "variable `%s' obsoletes, but isn't alias of `%s'"
306 (defmacro semantic-safe (format &rest body)
307 "Turn into a FORMAT message any error caught during eval of BODY.
308 Return the value of last BODY form or nil if an error occurred.
309 FORMAT can have a %s escape which will be replaced with the actual
311 If `debug-on-error' is set, errors are not caught, so that you can
313 Avoid using a large BODY since it is duplicated."
314 ;;(declare (debug t) (indent 1))
316 ;;(let ((inhibit-quit nil)) ,@body)
317 ;; Note to self: Doing the above screws up the wisent parser.
322 (message ,format (format "%S - %s" (current-buffer)
323 (error-message-string err)))
325 (put 'semantic-safe 'lisp-indent-function 1)
329 (defsubst semantic-map-buffers (function)
330 "Run FUNCTION for each Semantic enabled buffer found.
331 FUNCTION does not have arguments. When FUNCTION is entered
332 `current-buffer' is a selected Semantic enabled buffer."
333 (mode-local-map-file-buffers function #'semantic-active-p))
335 (defalias 'semantic-map-mode-buffers
336 'mode-local-map-mode-buffers)
338 (semantic-alias-obsolete 'semantic-fetch-overload
341 (semantic-alias-obsolete 'define-mode-overload-implementation
342 'define-mode-local-override)
344 (semantic-alias-obsolete 'semantic-with-mode-bindings
347 (semantic-alias-obsolete 'define-semantic-child-mode
350 (defun semantic-install-function-overrides (overrides &optional transient mode)
351 "Install the function OVERRIDES in the specified environment.
352 OVERRIDES must be an alist ((OVERLOAD . FUNCTION) ...) where OVERLOAD
353 is a symbol identifying an overloadable entry, and FUNCTION is the
354 function to override it with.
355 If optional argument TRANSIENT is non-nil, installed overrides can in
356 turn be overridden by next installation.
357 If optional argument MODE is non-nil, it must be a major mode symbol.
358 OVERRIDES will be installed globally for this major mode. If MODE is
359 nil, OVERRIDES will be installed locally in the current buffer. This
360 later installation should be done in MODE hook."
362 ;; Add the semantic- prefix to OVERLOAD short names.
365 (let ((name (symbol-name (car e))))
366 (if (string-match "^semantic-" name)
368 (cons (intern (format "semantic-%s" name)) (cdr e)))))
370 (list 'constant-flag (not transient)
374 ;;; User Interrupt handling
376 (defvar semantic-current-input-throw-symbol nil
377 "The current throw symbol for `semantic-exit-on-input'.")
379 (defmacro semantic-exit-on-input (symbol &rest forms)
380 "Using SYMBOL as an argument to `throw', execute FORMS.
381 If FORMS includes a call to `semantic-thow-on-input', then
382 if a user presses any key during execution, this form macro
383 will exit with the value passed to `semantic-throw-on-input'.
384 If FORMS completes, then the return value is the same as `progn'."
385 `(let ((semantic-current-input-throw-symbol ,symbol))
388 (put 'semantic-exit-on-input 'lisp-indent-function 1)
390 (defmacro semantic-throw-on-input (from)
391 "Exit with `throw' when in `semantic-exit-on-input' on user input.
392 FROM is an indication of where this function is called from as a value
393 to pass to `throw'. It is recommended to use the name of the function
395 `(when (and semantic-current-input-throw-symbol
396 (or (input-pending-p) (accept-process-output)))
397 (throw semantic-current-input-throw-symbol ,from)))
399 (defun semantic-test-throw-on-input ()
400 "Test that throw on input will work."
402 (semantic-throw-on-input 'done-die)
403 (message "Exit Code: %s"
404 (semantic-exit-on-input 'testing
405 (let ((inhibit-quit nil)
406 (message-log-max nil))
408 (message "Looping ... press a key to test")
409 (semantic-throw-on-input 'test-inner-loop))
411 (when (input-pending-p)
412 (if (fboundp 'read-event)
417 ;;; Special versions of Find File
419 (defun semantic-find-file-noselect (file &optional nowarn rawfile wildcards)
420 "Call `find-file-noselect' with various features turned off.
421 Use this when referencing a file that will be soon deleted.
422 FILE, NOWARN, RAWFILE, and WILDCARDS are passed into `find-file-noselect'"
423 (let* ((recentf-exclude '( (lambda (f) t) ))
424 ;; This is a brave statement. Don't waste time loading in
425 ;; lots of modes. Especially decoration mode can waste a lot
426 ;; of time for a buffer we intend to kill.
427 (semantic-init-hooks nil)
428 ;; This disables the part of EDE that asks questions
429 (ede-auto-add-method 'never)
430 ;; Ask font-lock to not colorize these buffers, nor to
431 ;; whine about it either.
432 (font-lock-maximum-size 0)
433 (font-lock-verbose nil)
434 ;; Disable revision control
435 (vc-handled-backends nil)
436 ;; Don't prompt to insert a template if we visit an empty file
438 ;; We don't want emacs to query about unsafe local variables
439 (enable-local-variables
440 (if (featurep 'xemacs)
441 ;; XEmacs only has nil as an option?
443 ;; Emacs 23 has the spiffy :safe option, nil otherwise.
444 (if (>= emacs-major-version 22)
447 ;; ... or eval variables
448 (enable-local-eval nil)
450 (if (featurep 'xemacs)
451 (find-file-noselect file nowarn rawfile)
452 (find-file-noselect file nowarn rawfile wildcards))
456 ;;; Editor goodies ;-)
458 (defconst semantic-fw-font-lock-keywords
461 ;; Variable declarations
463 (kv (if vl (regexp-opt vl t) ""))
464 ;; Function declarations
467 "define-lex-analyzer"
468 "define-lex-block-analyzer"
469 "define-lex-regex-analyzer"
470 "define-lex-spp-macro-declaration-analyzer"
471 "define-lex-spp-macro-undeclaration-analyzer"
472 "define-lex-spp-include-analyzer"
473 "define-lex-simple-regex-analyzer"
474 "define-lex-keyword-type-analyzer"
475 "define-lex-sexp-type-analyzer"
476 "define-lex-regex-type-analyzer"
477 "define-lex-string-type-analyzer"
478 "define-lex-block-type-analyzer"
479 ;;"define-mode-overload-implementation"
480 ;;"define-semantic-child-mode"
481 "define-semantic-idle-service"
482 "define-semantic-decoration-style"
483 "define-wisent-lexer"
484 "semantic-alias-obsolete"
485 "semantic-varalias-obsolete"
486 "semantic-make-obsolete-overload"
487 "defcustom-mode-local-semantic-dependency-system-include-path"
489 (kf (if vf (regexp-opt vf t) ""))
491 (kv-depth (if kv (regexp-opt-depth kv) nil))
492 (kf-depth (if kf (regexp-opt-depth kf) nil))
495 ;; Declarative things
496 "(\\(" kv "\\|" kf "\\)"
497 ;; Whitespaces & names
498 "\\>[ \t]*\\(\\sw+\\)?[ \t]*\\(\\sw+\\)?"
500 (1 font-lock-keyword-face)
501 (,(+ 1 kv-depth kf-depth 1)
502 (cond ((match-beginning 2)
504 ((match-beginning ,(+ 1 kv-depth 1))
505 font-lock-function-name-face)
508 (,(+ 1 kv-depth kf-depth 1 1)
509 (cond ((match-beginning 2)
510 font-lock-variable-name-face)
514 "Highlighted Semantic keywords.")
516 ;; (when (fboundp 'font-lock-add-keywords)
517 ;; (font-lock-add-keywords 'emacs-lisp-mode
518 ;; semantic-fw-font-lock-keywords))
520 ;;; Interfacing with edebug
522 (defun semantic-fw-add-edebug-spec ()
523 (def-edebug-spec semantic-exit-on-input 'def-body))
525 (add-hook 'edebug-setup-hook 'semantic-fw-add-edebug-spec)
527 (provide 'semantic/fw)
529 ;;; semantic-fw.el ends here