Gnus -- Minor tweak define #'time-to-seconds
[packages] / xemacs-packages / semantic / semantic-fw.el
1 ;;; semantic-fw.el --- Framework for Semantic
2
3 ;;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Eric M. Ludlam
4
5 ;; X-CVS: $Id: semantic-fw.el,v 1.1 2007-11-26 15:10:37 michaels Exp $
6
7 ;; This file is not part of GNU Emacs.
8
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)
12 ;; any later version.
13
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.
18
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.
23
24 ;;; Commentary:
25 ;;
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.
30
31 ;;; No Requirements.
32
33 ;;; Code:
34 ;;
35 (require 'mode-local)
36
37 ;;; Compatibility
38 ;;
39 (if (featurep 'xemacs)
40     (progn
41       (defalias 'semantic-overlay-live-p
42         (lambda (o)
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))
70                     (keyboard-quit)
71                   c)))
72           event))
73       )
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)
92   )
93
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)
98   )
99
100 (if (featurep 'xemacs)
101     (defalias 'semantic-mode-line-update 'redraw-modeline)
102   (defalias 'semantic-mode-line-update 'force-mode-line-update))
103
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)
108       'run-mode-hooks
109     'run-hooks))
110
111 ;; Fancy compat useage now handled in cedet-compat
112 (defalias 'semantic-subst-char-in-string 'subst-char-in-string)
113
114
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)))
119
120 (defalias 'semantic-compile-warn
121   (eval-when-compile
122     (if (fboundp 'byte-compile-warn)
123         'byte-compile-warn
124       'message)))
125
126 (if (not (fboundp 'string-to-number))
127     (defalias 'string-to-number 'string-to-int))
128
129 ;;; Positional Data Cache
130 ;;
131 (defvar semantic-cache-data-overlays nil
132   "List of all overlays waiting to be flushed.")
133
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
142                     executing command.
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"
149              lifespan))
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)
158     ))
159
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."
163   (let ((newcache nil)
164         (oldcache semantic-cache-data-overlays))
165     (while oldcache
166       (let* ((o    (car oldcache))
167              (life (semantic-overlay-get o 'lifespan))
168              )
169         (if (or (eq life 'end-of-command)
170                 (and (eq life 'exit-cache-zone)
171                      (not (member o (semantic-overlays-at (point))))))
172             (progn
173               ;;(message "Removing from cache: %s" o)
174               (semantic-overlay-delete o)
175               )
176           (setq newcache (cons o newcache))))
177       (setq oldcache (cdr oldcache)))
178     (setq semantic-cache-data-overlays (nreverse newcache)))
179
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)))
184
185 (defun semantic-get-cache-data (name &optional point)
186   "Get cached data with NAME from optional POINT."
187   (save-excursion
188     (if point (goto-char point))
189     (let ((o (semantic-overlays-at (point)))
190           (ans nil))
191       (while (and (not ans) o)
192         (if (equal (semantic-overlay-get (car o) 'cache-name) name)
193             (setq ans (car o))
194           (setq o (cdr o))))
195       (when ans
196         (semantic-overlay-get ans 'cached-value)))))
197
198 (defun semantic-test-data-cache ()
199   "Test the data cache."
200   (interactive)
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."))
207     ))
208
209 ;;; Semantic autoloads
210 ;;
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)
214 \f
215 ;;; Help debugging
216 ;;
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
221 error message.
222 If `debug-on-error' is set, errors are not caught, so that you can
223 debug them.
224 Avoid using a large BODY since it is duplicated."
225   ;;(declare (debug t) (indent 1))
226   `(if debug-on-error
227        ;;(let ((inhibit-quit nil)) ,@body)
228        ;; Note to self: Doing the above screws up the wisent parser.
229        (progn ,@body)
230      (condition-case err
231          (progn ,@body)
232        (error
233         (message ,format (format "%S - %s" (current-buffer)
234                                  (error-message-string err)))
235         nil))))
236 (put 'semantic-safe 'lisp-indent-function 1)
237
238 ;;; Misc utilities
239 ;;
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))
245
246 (defalias 'semantic-map-mode-buffers
247   'mode-local-map-mode-buffers)
248
249 (semantic-alias-obsolete 'semantic-fetch-overload
250                          'fetch-overload)
251
252 (semantic-alias-obsolete 'define-mode-overload-implementation
253                          'define-mode-local-override)
254
255 (semantic-alias-obsolete 'semantic-with-mode-bindings
256                          'with-mode-local)
257
258 (semantic-alias-obsolete 'define-semantic-child-mode
259                          'define-child-mode)
260
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."
272   (mode-local-bind
273    ;; Add the semantic- prefix to OVERLOAD short names.
274    (mapcar
275     #'(lambda (e)
276         (let ((name (symbol-name (car e))))
277           (if (string-match "^semantic-" name)
278               e
279             (cons (intern (format "semantic-%s" name)) (cdr e)))))
280     overrides)
281    (list 'constant-flag (not transient)
282          'override-flag t)
283    mode))
284 \f
285 ;;; User Interrupt handling
286 ;;
287 (defvar semantic-current-input-throw-symbol nil
288   "The current throw symbol for `semantic-exit-on-input'.")
289
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))
297      (catch ,symbol
298        ,@forms)))
299 (put 'semantic-exit-on-input 'lisp-indent-function 1)
300
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
305 calling this one."
306   `(when (and semantic-current-input-throw-symbol (input-pending-p))
307      (throw semantic-current-input-throw-symbol ,from)))
308
309 (defun semantic-test-throw-on-input ()
310   "Test that throw on input will work."
311   (interactive)
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))
317                (while (sit-for 0)
318                  (message "Looping ...")
319                  (semantic-throw-on-input 'test-inner-loop))
320                'exit))))
321 \f
322 ;;; Editor goodies ;-)
323 ;;
324 (defconst semantic-fw-font-lock-keywords
325   (eval-when-compile
326     (let* (
327            ;; Variable declarations
328            (vl nil)
329            (kv (if vl (regexp-opt vl t) ""))
330            ;; Function declarations
331            (vf '(
332                  "define-lex"
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"
352                  ))
353            (kf (if vf (regexp-opt vf t) ""))
354            ;; Regexp depths
355            (kv-depth (if kv (regexp-opt-depth kv) nil))
356            (kf-depth (if kf (regexp-opt-depth kf) nil))
357            )
358       `((,(concat
359            ;; Declarative things
360            "(\\(" kv "\\|" kf "\\)"
361            ;; Whitespaces & names
362            "\\>[ \t]*\\(\\sw+\\)?[ \t]*\\(\\sw+\\)?"
363            )
364          (1 font-lock-keyword-face)
365          (,(+ 1 kv-depth kf-depth 1)
366           (cond ((match-beginning 2)
367                  font-lock-type-face)
368                 ((match-beginning ,(+ 1 kv-depth 1))
369                  font-lock-function-name-face)
370                 )
371           nil t)
372          (,(+ 1 kv-depth kf-depth 1 1)
373           (cond ((match-beginning 2)
374                  font-lock-variable-name-face)
375                 )
376           nil t)))
377       ))
378   "Highlighted Semantic keywords.")
379
380 (when (fboundp 'font-lock-add-keywords)
381   (font-lock-add-keywords 'emacs-lisp-mode
382                           semantic-fw-font-lock-keywords)
383   )
384 \f
385 ;;; Interfacing with edebug
386 ;;
387 (add-hook
388  'edebug-setup-hook
389  #'(lambda ()
390
391      (def-edebug-spec semantic-exit-on-input
392        (symbolp def-body)
393        )
394
395      ))
396
397 (provide 'semantic-fw)
398
399 ;;; semantic-fw.el ends here