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,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 ;;; Semantic autoloads
211 ;; Load semantic-loaddefs after compatibility code, to allow to use it
212 ;; in autoloads without infinite recursive load problems.
213 ;; (load "semantic-loaddefs" nil t)
217 (defmacro semantic-safe (format &rest body)
218 "Turn into a FORMAT message any error caught during eval of BODY.
219 Return the value of last BODY form or nil if an error occurred.
220 FORMAT can have a %s escape which will be replaced with the actual
222 If `debug-on-error' is set, errors are not caught, so that you can
224 Avoid using a large BODY since it is duplicated."
225 ;;(declare (debug t) (indent 1))
227 ;;(let ((inhibit-quit nil)) ,@body)
228 ;; Note to self: Doing the above screws up the wisent parser.
233 (message ,format (format "%S - %s" (current-buffer)
234 (error-message-string err)))
236 (put 'semantic-safe 'lisp-indent-function 1)
240 (defsubst semantic-map-buffers (function)
241 "Run FUNCTION for each Semantic enabled buffer found.
242 FUNCTION does not have arguments. When FUNCTION is entered
243 `current-buffer' is a selected Semantic enabled buffer."
244 (mode-local-map-file-buffers function #'semantic-active-p))
246 (defalias 'semantic-map-mode-buffers
247 'mode-local-map-mode-buffers)
249 (semantic-alias-obsolete 'semantic-fetch-overload
252 (semantic-alias-obsolete 'define-mode-overload-implementation
253 'define-mode-local-override)
255 (semantic-alias-obsolete 'semantic-with-mode-bindings
258 (semantic-alias-obsolete 'define-semantic-child-mode
261 (defun semantic-install-function-overrides (overrides &optional transient mode)
262 "Install the function OVERRIDES in the specified environment.
263 OVERRIDES must be an alist ((OVERLOAD . FUNCTION) ...) where OVERLOAD
264 is a symbol identifying an overloadable entry, and FUNCTION is the
265 function to override it with.
266 If optional argument TRANSIENT is non-nil, installed overrides can in
267 turn be overridden by next installation.
268 If optional argument MODE is non-nil, it must be a major mode symbol.
269 OVERRIDES will be installed globally for this major mode. If MODE is
270 nil, OVERRIDES will be installed locally in the current buffer. This
271 later installation should be done in MODE hook."
273 ;; Add the semantic- prefix to OVERLOAD short names.
276 (let ((name (symbol-name (car e))))
277 (if (string-match "^semantic-" name)
279 (cons (intern (format "semantic-%s" name)) (cdr e)))))
281 (list 'constant-flag (not transient)
285 ;;; User Interrupt handling
287 (defvar semantic-current-input-throw-symbol nil
288 "The current throw symbol for `semantic-exit-on-input'.")
290 (defmacro semantic-exit-on-input (symbol &rest forms)
291 "Using SYMBOL as an argument to `throw', execute FORMS.
292 If FORMS includes a call to `semantic-thow-on-input', then
293 if a user presses any key during execution, this form macro
294 will exit with the value passed to `semantic-throw-on-input'.
295 If FORMS completes, then the return value is the same as `progn'."
296 `(let ((semantic-current-input-throw-symbol ,symbol))
299 (put 'semantic-exit-on-input 'lisp-indent-function 1)
301 (defmacro semantic-throw-on-input (from)
302 "Exit with `throw' when in `semantic-exit-on-input' on user input.
303 FROM is an indication of where this function is called from as a value
304 to pass to `throw'. It is recommended to use the name of the function
306 `(when (and semantic-current-input-throw-symbol (input-pending-p))
307 (throw semantic-current-input-throw-symbol ,from)))
309 (defun semantic-test-throw-on-input ()
310 "Test that throw on input will work."
312 (semantic-throw-on-input 'done-die)
313 (message "Exit Code: %s"
314 (semantic-exit-on-input 'testing
315 (let ((inhibit-quit nil)
316 (message-log-max nil))
318 (message "Looping ...")
319 (semantic-throw-on-input 'test-inner-loop))
322 ;;; Editor goodies ;-)
324 (defconst semantic-fw-font-lock-keywords
327 ;; Variable declarations
329 (kv (if vl (regexp-opt vl t) ""))
330 ;; Function declarations
333 "define-lex-analyzer"
334 "define-lex-block-analyzer"
335 "define-lex-regex-analyzer"
336 "define-lex-spp-macro-declaration-analyzer"
337 "define-lex-spp-macro-undeclaration-analyzer"
338 "define-lex-simple-regex-analyzer"
339 "define-lex-keyword-type-analyzer"
340 "define-lex-sexp-type-analyzer"
341 "define-lex-regex-type-analyzer"
342 "define-lex-string-type-analyzer"
343 "define-lex-block-type-analyzer"
344 ;;"define-mode-overload-implementation"
345 ;;"define-semantic-child-mode"
346 "define-semantic-idle-service"
347 "define-semantic-decoration-style"
348 "define-wisent-lexer"
349 "semantic-alias-obsolete"
350 "semantic-varalias-obsolete"
351 "semantic-make-obsolete-overload"
353 (kf (if vf (regexp-opt vf t) ""))
355 (kv-depth (if kv (regexp-opt-depth kv) nil))
356 (kf-depth (if kf (regexp-opt-depth kf) nil))
359 ;; Declarative things
360 "(\\(" kv "\\|" kf "\\)"
361 ;; Whitespaces & names
362 "\\>[ \t]*\\(\\sw+\\)?[ \t]*\\(\\sw+\\)?"
364 (1 font-lock-keyword-face)
365 (,(+ 1 kv-depth kf-depth 1)
366 (cond ((match-beginning 2)
368 ((match-beginning ,(+ 1 kv-depth 1))
369 font-lock-function-name-face)
372 (,(+ 1 kv-depth kf-depth 1 1)
373 (cond ((match-beginning 2)
374 font-lock-variable-name-face)
378 "Highlighted Semantic keywords.")
380 (when (fboundp 'font-lock-add-keywords)
381 (font-lock-add-keywords 'emacs-lisp-mode
382 semantic-fw-font-lock-keywords)
385 ;;; Interfacing with edebug
391 (def-edebug-spec semantic-exit-on-input
397 (provide 'semantic-fw)
399 ;;; semantic-fw.el ends here