1 ;;; semantic-fw.el --- Framework for Semantic
3 ;;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Eric M. Ludlam
5 ;; X-CVS: $Id: semantic-fw.el.upstream,v 1.1 2007-11-26 15:10:37 michaels Exp $
7 ;; This file is not part of GNU Emacs.
9 ;; Semantic is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; This software is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
26 ;; Semantic has several core features shared across it's lex/parse/util
27 ;; stages. This used to clutter semantic.el some. These routines are all
28 ;; simple things that are not parser specific, but aid in making
29 ;; semantic flexible and compatible amongst different Emacs platforms.
39 (if (featurep 'xemacs)
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 'make-extent)
47 (defalias 'semantic-overlay-put 'set-extent-property)
48 (defalias 'semantic-overlay-get 'extent-property)
49 (defalias 'semantic-overlay-properties 'extent-properties)
50 (defalias 'semantic-overlay-move 'set-extent-endpoints)
51 (defalias 'semantic-overlay-delete 'delete-extent)
52 (defalias 'semantic-overlays-at
53 (lambda (pos) (extent-list nil pos pos)))
54 (defalias 'semantic-overlays-in
55 (lambda (beg end) (extent-list nil beg end)))
56 (defalias 'semantic-overlay-buffer 'extent-buffer)
57 (defalias 'semantic-overlay-start 'extent-start-position)
58 (defalias 'semantic-overlay-end 'extent-end-position)
59 (defalias 'semantic-overlay-size 'extent-length)
60 (defalias 'semantic-overlay-next-change 'next-extent-change)
61 (defalias 'semantic-overlay-previous-change 'previous-extent-change)
62 (defalias 'semantic-overlay-lists
63 (lambda () (list (extent-list))))
64 (defalias 'semantic-overlay-p 'extentp)
65 (defun semantic-read-event ()
66 (let ((event (next-command-event)))
67 (if (key-press-event-p event)
68 (let ((c (event-to-character event)))
69 (if (char-equal c (quit-char))
74 (defalias 'semantic-overlay-live-p 'overlay-buffer)
75 (defalias 'semantic-make-overlay 'make-overlay)
76 (defalias 'semantic-overlay-put 'overlay-put)
77 (defalias 'semantic-overlay-get 'overlay-get)
78 (defalias 'semantic-overlay-properties 'overlay-properties)
79 (defalias 'semantic-overlay-move 'move-overlay)
80 (defalias 'semantic-overlay-delete 'delete-overlay)
81 (defalias 'semantic-overlays-at 'overlays-at)
82 (defalias 'semantic-overlays-in 'overlays-in)
83 (defalias 'semantic-overlay-buffer 'overlay-buffer)
84 (defalias 'semantic-overlay-start 'overlay-start)
85 (defalias 'semantic-overlay-end 'overlay-end)
86 (defalias 'semantic-overlay-size 'overlay-size)
87 (defalias 'semantic-overlay-next-change 'next-overlay-change)
88 (defalias 'semantic-overlay-previous-change 'previous-overlay-change)
89 (defalias 'semantic-overlay-lists 'overlay-lists)
90 (defalias 'semantic-overlay-p 'overlayp)
91 (defalias 'semantic-read-event 'read-event)
94 (if (and (not (featurep 'xemacs))
95 (>= emacs-major-version 21))
96 (defalias 'semantic-make-local-hook 'identity)
97 (defalias 'semantic-make-local-hook 'make-local-hook)
100 (if (featurep 'xemacs)
101 (defalias 'semantic-mode-line-update 'redraw-modeline)
102 (defalias 'semantic-mode-line-update 'force-mode-line-update))
104 ;; Since Emacs 22 major mode functions should use `run-mode-hooks' to
105 ;; run major mode hooks.
106 (defalias 'semantic-run-mode-hooks
107 (if (fboundp 'run-mode-hooks)
111 ;; Fancy compat useage now handled in cedet-compat
112 (defalias 'semantic-subst-char-in-string 'subst-char-in-string)
115 (defun semantic-delete-overlay-maybe (overlay)
116 "Delete OVERLAY if it is a semantic token overlay."
117 (if (semantic-overlay-get overlay 'semantic)
118 (semantic-overlay-delete overlay)))
120 (defalias 'semantic-compile-warn
122 (if (fboundp 'byte-compile-warn)
126 (if (not (fboundp 'string-to-number))
127 (defalias 'string-to-number 'string-to-int))
129 ;;; Positional Data Cache
131 (defvar semantic-cache-data-overlays nil
132 "List of all overlays waiting to be flushed.")
134 (defun semantic-cache-data-to-buffer (buffer start end value name &optional lifespan)
135 "In BUFFER over the region START END, remember VALUE.
136 NAME specifies a special name that can be searched for later to
137 recover the cached data with `semantic-get-cache-data'.
138 LIFESPAN indicates how long the data cache will be remembered.
139 The default LIFESPAN is 'end-of-command.
140 Possible Lifespans are:
141 'end-of-command - Remove the cache at the end of the currently
143 'exit-cache-zone - Remove when point leaves the overlay at the
144 end of the currently executing command."
145 ;; Check if LIFESPAN is valid before to create any overlay
146 (or lifespan (setq lifespan 'end-of-command))
147 (or (memq lifespan '(end-of-command exit-cache-zone))
148 (error "semantic-cache-data-to-buffer: Unknown LIFESPAN: %s"
150 (let ((o (semantic-make-overlay start end buffer)))
151 (semantic-overlay-put o 'cache-name name)
152 (semantic-overlay-put o 'cached-value value)
153 (semantic-overlay-put o 'lifespan lifespan)
154 (setq semantic-cache-data-overlays
155 (cons o semantic-cache-data-overlays))
156 ;;(message "Adding to cache: %s" o)
157 (add-hook 'post-command-hook 'semantic-cache-data-post-command-hook)
160 (defun semantic-cache-data-post-command-hook ()
161 "Flush `semantic-cache-data-overlays' based 'lifespan property.
162 Remove self from `post-command-hook' if it is empty."
164 (oldcache semantic-cache-data-overlays))
166 (let* ((o (car oldcache))
167 (life (semantic-overlay-get o 'lifespan))
169 (if (or (eq life 'end-of-command)
170 (and (eq life 'exit-cache-zone)
171 (not (member o (semantic-overlays-at (point))))))
173 ;;(message "Removing from cache: %s" o)
174 (semantic-overlay-delete o)
176 (setq newcache (cons o newcache))))
177 (setq oldcache (cdr oldcache)))
178 (setq semantic-cache-data-overlays (nreverse newcache)))
180 ;; Remove ourselves if we have removed all overlays.
181 (unless semantic-cache-data-overlays
182 (remove-hook 'post-command-hook
183 'semantic-cache-data-post-command-hook)))
185 (defun semantic-get-cache-data (name &optional point)
186 "Get cached data with NAME from optional POINT."
188 (if point (goto-char point))
189 (let ((o (semantic-overlays-at (point)))
191 (while (and (not ans) o)
192 (if (equal (semantic-overlay-get (car o) 'cache-name) name)
196 (semantic-overlay-get ans 'cached-value)))))
198 (defun semantic-test-data-cache ()
199 "Test the data cache."
201 (let ((data '(a b c)))
202 (semantic-cache-data-to-buffer (current-buffer) (point) (+ (point) 5)
203 data 'moose 'exit-cache-zone)
204 (if (equal (semantic-get-cache-data 'moose) data)
205 (message "Successfully retrieved cached data.")
206 (message "Failed to retrieve cached data."))
209 ;;; Obsoleting various functions & variables
211 (defun semantic-overload-symbol-from-function (name)
212 "Return the symbol for overload used by NAME, the defined symbol."
213 (let ((sym-name (symbol-name name)))
214 (if (string-match "^semantic-" sym-name)
215 (intern (substring sym-name (match-end 0)))
218 (defun semantic-alias-obsolete (oldfnalias newfn)
219 "Make OLDFNALIAS an alias for NEWFN.
220 Mark OLDFNALIAS as obsolete, such that the byte compiler
221 will throw a warning when it encounters this symbol."
222 (defalias oldfnalias newfn)
223 (make-obsolete oldfnalias newfn)
224 (when (and (function-overload-p newfn)
225 (not (overload-obsoleted-by newfn))
226 ;; Only throw this warning when byte compiling things.
227 (boundp 'byte-compile-current-file)
228 byte-compile-current-file
229 (not (string-match "cedet" byte-compile-current-file))
231 (make-obsolete-overload oldfnalias newfn)
232 (semantic-compile-warn
233 "%s: `%s' obsoletes overload `%s'"
234 byte-compile-current-file
236 (semantic-overload-symbol-from-function oldfnalias))
239 (defun semantic-varalias-obsolete (oldvaralias newvar)
240 "Make OLDVARALIAS an alias for variable NEWVAR.
241 Mark OLDVARALIAS as obsolete, such that the byte compiler
242 will throw a warning when it encounters this symbol."
243 (make-obsolete-variable oldvaralias newvar)
245 (defvaralias oldvaralias newvar)
247 ;; Only throw this warning when byte compiling things.
248 (when (and (boundp 'byte-compile-current-file)
249 byte-compile-current-file)
250 (semantic-compile-warn
251 "variable `%s' obsoletes, but isn't alias of `%s'"
255 ;;; Semantic autoloads
257 ;; Load semantic-loaddefs after compatibility code, to allow to use it
258 ;; in autoloads without infinite recursive load problems.
259 (load "semantic-loaddefs" nil t)
263 (defmacro semantic-safe (format &rest body)
264 "Turn into a FORMAT message any error caught during eval of BODY.
265 Return the value of last BODY form or nil if an error occurred.
266 FORMAT can have a %s escape which will be replaced with the actual
268 If `debug-on-error' is set, errors are not caught, so that you can
270 Avoid using a large BODY since it is duplicated."
271 ;;(declare (debug t) (indent 1))
273 ;;(let ((inhibit-quit nil)) ,@body)
274 ;; Note to self: Doing the above screws up the wisent parser.
279 (message ,format (format "%S - %s" (current-buffer)
280 (error-message-string err)))
282 (put 'semantic-safe 'lisp-indent-function 1)
286 (defsubst semantic-map-buffers (function)
287 "Run FUNCTION for each Semantic enabled buffer found.
288 FUNCTION does not have arguments. When FUNCTION is entered
289 `current-buffer' is a selected Semantic enabled buffer."
290 (mode-local-map-file-buffers function #'semantic-active-p))
292 (defalias 'semantic-map-mode-buffers
293 'mode-local-map-mode-buffers)
295 (semantic-alias-obsolete 'semantic-fetch-overload
298 (semantic-alias-obsolete 'define-mode-overload-implementation
299 'define-mode-local-override)
301 (semantic-alias-obsolete 'semantic-with-mode-bindings
304 (semantic-alias-obsolete 'define-semantic-child-mode
307 (defun semantic-install-function-overrides (overrides &optional transient mode)
308 "Install the function OVERRIDES in the specified environment.
309 OVERRIDES must be an alist ((OVERLOAD . FUNCTION) ...) where OVERLOAD
310 is a symbol identifying an overloadable entry, and FUNCTION is the
311 function to override it with.
312 If optional argument TRANSIENT is non-nil, installed overrides can in
313 turn be overridden by next installation.
314 If optional argument MODE is non-nil, it must be a major mode symbol.
315 OVERRIDES will be installed globally for this major mode. If MODE is
316 nil, OVERRIDES will be installed locally in the current buffer. This
317 later installation should be done in MODE hook."
319 ;; Add the semantic- prefix to OVERLOAD short names.
322 (let ((name (symbol-name (car e))))
323 (if (string-match "^semantic-" name)
325 (cons (intern (format "semantic-%s" name)) (cdr e)))))
327 (list 'constant-flag (not transient)
331 ;;; User Interrupt handling
333 (defvar semantic-current-input-throw-symbol nil
334 "The current throw symbol for `semantic-exit-on-input'.")
336 (defmacro semantic-exit-on-input (symbol &rest forms)
337 "Using SYMBOL as an argument to `throw', execute FORMS.
338 If FORMS includes a call to `semantic-thow-on-input', then
339 if a user presses any key during execution, this form macro
340 will exit with the value passed to `semantic-throw-on-input'.
341 If FORMS completes, then the return value is the same as `progn'."
342 `(let ((semantic-current-input-throw-symbol ,symbol))
345 (put 'semantic-exit-on-input 'lisp-indent-function 1)
347 (defmacro semantic-throw-on-input (from)
348 "Exit with `throw' when in `semantic-exit-on-input' on user input.
349 FROM is an indication of where this function is called from as a value
350 to pass to `throw'. It is recommended to use the name of the function
352 `(when (and semantic-current-input-throw-symbol (input-pending-p))
353 (throw semantic-current-input-throw-symbol ,from)))
355 (defun semantic-test-throw-on-input ()
356 "Test that throw on input will work."
358 (semantic-throw-on-input 'done-die)
359 (message "Exit Code: %s"
360 (semantic-exit-on-input 'testing
361 (let ((inhibit-quit nil)
362 (message-log-max nil))
364 (message "Looping ...")
365 (semantic-throw-on-input 'test-inner-loop))
368 ;;; Editor goodies ;-)
370 (defconst semantic-fw-font-lock-keywords
373 ;; Variable declarations
375 (kv (if vl (regexp-opt vl t) ""))
376 ;; Function declarations
379 "define-lex-analyzer"
380 "define-lex-block-analyzer"
381 "define-lex-regex-analyzer"
382 "define-lex-spp-macro-declaration-analyzer"
383 "define-lex-spp-macro-undeclaration-analyzer"
384 "define-lex-simple-regex-analyzer"
385 "define-lex-keyword-type-analyzer"
386 "define-lex-sexp-type-analyzer"
387 "define-lex-regex-type-analyzer"
388 "define-lex-string-type-analyzer"
389 "define-lex-block-type-analyzer"
390 ;;"define-mode-overload-implementation"
391 ;;"define-semantic-child-mode"
392 "define-semantic-idle-service"
393 "define-semantic-decoration-style"
394 "define-wisent-lexer"
395 "semantic-alias-obsolete"
396 "semantic-varalias-obsolete"
397 "semantic-make-obsolete-overload"
399 (kf (if vf (regexp-opt vf t) ""))
401 (kv-depth (if kv (regexp-opt-depth kv) nil))
402 (kf-depth (if kf (regexp-opt-depth kf) nil))
405 ;; Declarative things
406 "(\\(" kv "\\|" kf "\\)"
407 ;; Whitespaces & names
408 "\\>[ \t]*\\(\\sw+\\)?[ \t]*\\(\\sw+\\)?"
410 (1 font-lock-keyword-face)
411 (,(+ 1 kv-depth kf-depth 1)
412 (cond ((match-beginning 2)
414 ((match-beginning ,(+ 1 kv-depth 1))
415 font-lock-function-name-face)
418 (,(+ 1 kv-depth kf-depth 1 1)
419 (cond ((match-beginning 2)
420 font-lock-variable-name-face)
424 "Highlighted Semantic keywords.")
426 (when (fboundp 'font-lock-add-keywords)
427 (font-lock-add-keywords 'emacs-lisp-mode
428 semantic-fw-font-lock-keywords)
431 ;;; Interfacing with edebug
437 (def-edebug-spec semantic-exit-on-input
443 (provide 'semantic-fw)
445 ;;; semantic-fw.el ends here