cedet/semantic.el: Change requires to use semantic/FOO format.
[bpt/emacs.git] / lisp / cedet / semantic / fw.el
CommitLineData
9d389824
CY
1;;; semantic-fw.el --- Framework for Semantic
2
3;;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4;;; 2007, 2008, 2009 Free Software Foundation, Inc.
5
6;; Author: Eric M. Ludlam <zappo@gnu.org>
7
8;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23;;; Commentary:
24;;
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.
29
30;;; Code:
31;;
32(require 'mode-local)
33(require 'eieio)
34
35;;; Compatibility
36;;
37(if (featurep 'xemacs)
38 (progn
39 (defalias 'semantic-buffer-local-value 'symbol-value-in-buffer)
40 (defalias 'semantic-overlay-live-p
41 (lambda (o)
42 (and (extent-live-p o)
43 (not (extent-detached-p o))
44 (bufferp (extent-buffer o)))))
45 (defalias 'semantic-make-overlay
46 (lambda (beg end &optional buffer &rest rest)
47 "Xemacs `make-extent', supporting the front/rear advance options."
48 (let ((ol (make-extent beg end buffer)))
49 (when rest
50 (set-extent-property ol 'start-open (car rest))
51 (setq rest (cdr rest)))
52 (when rest
53 (set-extent-property ol 'end-open (car rest)))
54 ol)))
55 (defalias 'semantic-overlay-put 'set-extent-property)
56 (defalias 'semantic-overlay-get 'extent-property)
57 (defalias 'semantic-overlay-properties 'extent-properties)
58 (defalias 'semantic-overlay-move 'set-extent-endpoints)
59 (defalias 'semantic-overlay-delete 'delete-extent)
60 (defalias 'semantic-overlays-at
61 (lambda (pos)
62 (condition-case nil
63 (extent-list nil pos pos)
64 (error nil))
65 ))
66 (defalias 'semantic-overlays-in
67 (lambda (beg end) (extent-list nil beg end)))
68 (defalias 'semantic-overlay-buffer 'extent-buffer)
69 (defalias 'semantic-overlay-start 'extent-start-position)
70 (defalias 'semantic-overlay-end 'extent-end-position)
71 (defalias 'semantic-overlay-size 'extent-length)
72 (defalias 'semantic-overlay-next-change 'next-extent-change)
73 (defalias 'semantic-overlay-previous-change 'previous-extent-change)
74 (defalias 'semantic-overlay-lists
75 (lambda () (list (extent-list))))
76 (defalias 'semantic-overlay-p 'extentp)
77 (defalias 'semantic-event-window 'event-window)
78 (defun semantic-read-event ()
79 (let ((event (next-command-event)))
80 (if (key-press-event-p event)
81 (let ((c (event-to-character event)))
82 (if (char-equal c (quit-char))
83 (keyboard-quit)
84 c)))
85 event))
86 (defun semantic-popup-menu (menu)
87 "Blockinig version of `popup-menu'"
88 (popup-menu menu)
89 ;; Wait...
90 (while (popup-up-p) (dispatch-event (next-event))))
91 )
92 ;; Emacs Bindings
93 (defalias 'semantic-buffer-local-value 'buffer-local-value)
94 (defalias 'semantic-overlay-live-p 'overlay-buffer)
95 (defalias 'semantic-make-overlay 'make-overlay)
96 (defalias 'semantic-overlay-put 'overlay-put)
97 (defalias 'semantic-overlay-get 'overlay-get)
98 (defalias 'semantic-overlay-properties 'overlay-properties)
99 (defalias 'semantic-overlay-move 'move-overlay)
100 (defalias 'semantic-overlay-delete 'delete-overlay)
101 (defalias 'semantic-overlays-at 'overlays-at)
102 (defalias 'semantic-overlays-in 'overlays-in)
103 (defalias 'semantic-overlay-buffer 'overlay-buffer)
104 (defalias 'semantic-overlay-start 'overlay-start)
105 (defalias 'semantic-overlay-end 'overlay-end)
106 (defalias 'semantic-overlay-size 'overlay-size)
107 (defalias 'semantic-overlay-next-change 'next-overlay-change)
108 (defalias 'semantic-overlay-previous-change 'previous-overlay-change)
109 (defalias 'semantic-overlay-lists 'overlay-lists)
110 (defalias 'semantic-overlay-p 'overlayp)
111 (defalias 'semantic-read-event 'read-event)
112 (defalias 'semantic-popup-menu 'popup-menu)
113 (defun semantic-event-window (event)
114 "Extract the window from EVENT."
115 (car (car (cdr event))))
116 )
117
118(if (and (not (featurep 'xemacs))
119 (>= emacs-major-version 21))
120 (defalias 'semantic-make-local-hook 'identity)
121 (defalias 'semantic-make-local-hook 'make-local-hook)
122 )
123
124(if (featurep 'xemacs)
125 (defalias 'semantic-mode-line-update 'redraw-modeline)
126 (defalias 'semantic-mode-line-update 'force-mode-line-update))
127
128;; Since Emacs 22 major mode functions should use `run-mode-hooks' to
129;; run major mode hooks.
130(defalias 'semantic-run-mode-hooks
131 (if (fboundp 'run-mode-hooks)
132 'run-mode-hooks
133 'run-hooks))
134
135;; Fancy compat useage now handled in cedet-compat
136(defalias 'semantic-subst-char-in-string 'subst-char-in-string)
137
138
139(defun semantic-delete-overlay-maybe (overlay)
140 "Delete OVERLAY if it is a semantic token overlay."
141 (if (semantic-overlay-get overlay 'semantic)
142 (semantic-overlay-delete overlay)))
143
144(defalias 'semantic-compile-warn
145 (eval-when-compile
146 (if (fboundp 'byte-compile-warn)
147 'byte-compile-warn
148 'message)))
149
150(if (not (fboundp 'string-to-number))
151 (defalias 'string-to-number 'string-to-int))
152
153;;; Menu Item compatibility
154;;
155(defun semantic-menu-item (item)
156 "Build an XEmacs compatible menu item from vector ITEM.
157That is remove the unsupported :help stuff."
158 (if (featurep 'xemacs)
159 (let ((n (length item))
160 (i 0)
161 slot l)
162 (while (< i n)
163 (setq slot (aref item i))
164 (if (and (keywordp slot)
165 (eq slot :help))
166 (setq i (1+ i))
167 (setq l (cons slot l)))
168 (setq i (1+ i)))
169 (apply #'vector (nreverse l)))
170 item))
171
172;;; Positional Data Cache
173;;
174(defvar semantic-cache-data-overlays nil
175 "List of all overlays waiting to be flushed.")
176
177(defun semantic-cache-data-to-buffer (buffer start end value name &optional lifespan)
178 "In BUFFER over the region START END, remember VALUE.
179NAME specifies a special name that can be searched for later to
180recover the cached data with `semantic-get-cache-data'.
181LIFESPAN indicates how long the data cache will be remembered.
182The default LIFESPAN is 'end-of-command.
183Possible Lifespans are:
184 'end-of-command - Remove the cache at the end of the currently
185 executing command.
186 'exit-cache-zone - Remove when point leaves the overlay at the
187 end of the currently executing command."
188 ;; Check if LIFESPAN is valid before to create any overlay
189 (or lifespan (setq lifespan 'end-of-command))
190 (or (memq lifespan '(end-of-command exit-cache-zone))
191 (error "semantic-cache-data-to-buffer: Unknown LIFESPAN: %s"
192 lifespan))
193 (let ((o (semantic-make-overlay start end buffer)))
194 (semantic-overlay-put o 'cache-name name)
195 (semantic-overlay-put o 'cached-value value)
196 (semantic-overlay-put o 'lifespan lifespan)
197 (setq semantic-cache-data-overlays
198 (cons o semantic-cache-data-overlays))
199 ;;(message "Adding to cache: %s" o)
200 (add-hook 'post-command-hook 'semantic-cache-data-post-command-hook)
201 ))
202
203(defun semantic-cache-data-post-command-hook ()
204 "Flush `semantic-cache-data-overlays' based 'lifespan property.
205Remove self from `post-command-hook' if it is empty."
206 (let ((newcache nil)
207 (oldcache semantic-cache-data-overlays))
208 (while oldcache
209 (let* ((o (car oldcache))
210 (life (semantic-overlay-get o 'lifespan))
211 )
212 (if (or (eq life 'end-of-command)
213 (and (eq life 'exit-cache-zone)
214 (not (member o (semantic-overlays-at (point))))))
215 (progn
216 ;;(message "Removing from cache: %s" o)
217 (semantic-overlay-delete o)
218 )
219 (setq newcache (cons o newcache))))
220 (setq oldcache (cdr oldcache)))
221 (setq semantic-cache-data-overlays (nreverse newcache)))
222
223 ;; Remove ourselves if we have removed all overlays.
224 (unless semantic-cache-data-overlays
225 (remove-hook 'post-command-hook
226 'semantic-cache-data-post-command-hook)))
227
228(defun semantic-get-cache-data (name &optional point)
229 "Get cached data with NAME from optional POINT."
230 (save-excursion
231 (if point (goto-char point))
232 (let ((o (semantic-overlays-at (point)))
233 (ans nil))
234 (while (and (not ans) o)
235 (if (equal (semantic-overlay-get (car o) 'cache-name) name)
236 (setq ans (car o))
237 (setq o (cdr o))))
238 (when ans
239 (semantic-overlay-get ans 'cached-value)))))
240
241(defun semantic-test-data-cache ()
242 "Test the data cache."
243 (interactive)
244 (let ((data '(a b c)))
245 (save-excursion
246 (set-buffer (get-buffer-create " *semantic-test-data-cache*"))
247 (erase-buffer)
248 (insert "The Moose is Loose")
249 (goto-char (point-min))
250 (semantic-cache-data-to-buffer (current-buffer) (point) (+ (point) 5)
251 data 'moose 'exit-cache-zone)
252 (if (equal (semantic-get-cache-data 'moose) data)
253 (message "Successfully retrieved cached data.")
254 (error "Failed to retrieve cached data"))
255 )))
256
257;;; Obsoleting various functions & variables
258;;
259(defun semantic-overload-symbol-from-function (name)
260 "Return the symbol for overload used by NAME, the defined symbol."
261 (let ((sym-name (symbol-name name)))
262 (if (string-match "^semantic-" sym-name)
263 (intern (substring sym-name (match-end 0)))
264 name)))
265
266(defun semantic-alias-obsolete (oldfnalias newfn)
267 "Make OLDFNALIAS an alias for NEWFN.
268Mark OLDFNALIAS as obsolete, such that the byte compiler
269will throw a warning when it encounters this symbol."
270 (defalias oldfnalias newfn)
271 (make-obsolete oldfnalias newfn)
272 (when (and (function-overload-p newfn)
273 (not (overload-obsoleted-by newfn))
274 ;; Only throw this warning when byte compiling things.
275 (boundp 'byte-compile-current-file)
276 byte-compile-current-file
277 (not (string-match "cedet" byte-compile-current-file))
278 )
279 (make-obsolete-overload oldfnalias newfn)
280 (semantic-compile-warn
281 "%s: `%s' obsoletes overload `%s'"
282 byte-compile-current-file
283 newfn
284 (semantic-overload-symbol-from-function oldfnalias))
285 ))
286
287(defun semantic-varalias-obsolete (oldvaralias newvar)
288 "Make OLDVARALIAS an alias for variable NEWVAR.
289Mark OLDVARALIAS as obsolete, such that the byte compiler
290will throw a warning when it encounters this symbol."
291 (make-obsolete-variable oldvaralias newvar)
292 (condition-case nil
293 (defvaralias oldvaralias newvar)
294 (error
295 ;; Only throw this warning when byte compiling things.
296 (when (and (boundp 'byte-compile-current-file)
297 byte-compile-current-file)
298 (semantic-compile-warn
299 "variable `%s' obsoletes, but isn't alias of `%s'"
300 newvar oldvaralias)
301 ))))
302\f
303;;; Help debugging
304;;
305(defmacro semantic-safe (format &rest body)
306 "Turn into a FORMAT message any error caught during eval of BODY.
307Return the value of last BODY form or nil if an error occurred.
308FORMAT can have a %s escape which will be replaced with the actual
309error message.
310If `debug-on-error' is set, errors are not caught, so that you can
311debug them.
312Avoid using a large BODY since it is duplicated."
313 ;;(declare (debug t) (indent 1))
314 `(if debug-on-error
315 ;;(let ((inhibit-quit nil)) ,@body)
316 ;; Note to self: Doing the above screws up the wisent parser.
317 (progn ,@body)
318 (condition-case err
319 (progn ,@body)
320 (error
321 (message ,format (format "%S - %s" (current-buffer)
322 (error-message-string err)))
323 nil))))
324(put 'semantic-safe 'lisp-indent-function 1)
325
326;;; Misc utilities
327;;
328(defsubst semantic-map-buffers (function)
329 "Run FUNCTION for each Semantic enabled buffer found.
330FUNCTION does not have arguments. When FUNCTION is entered
331`current-buffer' is a selected Semantic enabled buffer."
332 (mode-local-map-file-buffers function #'semantic-active-p))
333
334(defalias 'semantic-map-mode-buffers
335 'mode-local-map-mode-buffers)
336
337(semantic-alias-obsolete 'semantic-fetch-overload
338 'fetch-overload)
339
340(semantic-alias-obsolete 'define-mode-overload-implementation
341 'define-mode-local-override)
342
343(semantic-alias-obsolete 'semantic-with-mode-bindings
344 'with-mode-local)
345
346(semantic-alias-obsolete 'define-semantic-child-mode
347 'define-child-mode)
348
349(defun semantic-install-function-overrides (overrides &optional transient mode)
350 "Install the function OVERRIDES in the specified environment.
351OVERRIDES must be an alist ((OVERLOAD . FUNCTION) ...) where OVERLOAD
352is a symbol identifying an overloadable entry, and FUNCTION is the
353function to override it with.
354If optional argument TRANSIENT is non-nil, installed overrides can in
355turn be overridden by next installation.
356If optional argument MODE is non-nil, it must be a major mode symbol.
357OVERRIDES will be installed globally for this major mode. If MODE is
358nil, OVERRIDES will be installed locally in the current buffer. This
359later installation should be done in MODE hook."
360 (mode-local-bind
361 ;; Add the semantic- prefix to OVERLOAD short names.
362 (mapcar
363 #'(lambda (e)
364 (let ((name (symbol-name (car e))))
365 (if (string-match "^semantic-" name)
366 e
367 (cons (intern (format "semantic-%s" name)) (cdr e)))))
368 overrides)
369 (list 'constant-flag (not transient)
370 'override-flag t)
371 mode))
372\f
373;;; User Interrupt handling
374;;
375(defvar semantic-current-input-throw-symbol nil
376 "The current throw symbol for `semantic-exit-on-input'.")
377
378(defmacro semantic-exit-on-input (symbol &rest forms)
379 "Using SYMBOL as an argument to `throw', execute FORMS.
380If FORMS includes a call to `semantic-thow-on-input', then
381if a user presses any key during execution, this form macro
382will exit with the value passed to `semantic-throw-on-input'.
383If FORMS completes, then the return value is the same as `progn'."
384 `(let ((semantic-current-input-throw-symbol ,symbol))
385 (catch ,symbol
386 ,@forms)))
387(put 'semantic-exit-on-input 'lisp-indent-function 1)
388
389(defmacro semantic-throw-on-input (from)
390 "Exit with `throw' when in `semantic-exit-on-input' on user input.
391FROM is an indication of where this function is called from as a value
392to pass to `throw'. It is recommended to use the name of the function
393calling this one."
394 `(when (and semantic-current-input-throw-symbol
395 (or (input-pending-p) (accept-process-output)))
396 (throw semantic-current-input-throw-symbol ,from)))
397
398(defun semantic-test-throw-on-input ()
399 "Test that throw on input will work."
400 (interactive)
401 (semantic-throw-on-input 'done-die)
402 (message "Exit Code: %s"
403 (semantic-exit-on-input 'testing
404 (let ((inhibit-quit nil)
405 (message-log-max nil))
406 (while t
407 (message "Looping ... press a key to test")
408 (semantic-throw-on-input 'test-inner-loop))
409 'exit)))
410 (when (input-pending-p)
411 (if (fboundp 'read-event)
412 (read-event)
413 (read-char)))
414 )
415\f
416;;; Special versions of Find File
417;;
418(defun semantic-find-file-noselect (file &optional nowarn rawfile wildcards)
419 "Call `find-file-noselect' with various features turned off.
420Use this when referencing a file that will be soon deleted.
421FILE, NOWARN, RAWFILE, and WILDCARDS are passed into `find-file-noselect'"
422 (let* ((recentf-exclude '( (lambda (f) t) ))
423 ;; This is a brave statement. Don't waste time loading in
424 ;; lots of modes. Especially decoration mode can waste a lot
425 ;; of time for a buffer we intend to kill.
426 (semantic-init-hooks nil)
427 ;; This disables the part of EDE that asks questions
428 (ede-auto-add-method 'never)
429 ;; Ask font-lock to not colorize these buffers, nor to
430 ;; whine about it either.
431 (font-lock-maximum-size 0)
432 (font-lock-verbose nil)
433 ;; Disable revision control
434 (vc-handled-backends nil)
435 ;; Don't prompt to insert a template if we visit an empty file
436 (auto-insert nil)
437 ;; We don't want emacs to query about unsafe local variables
438 (enable-local-variables
439 (if (featurep 'xemacs)
440 ;; XEmacs only has nil as an option?
441 nil
442 ;; Emacs 23 has the spiffy :safe option, nil otherwise.
443 (if (>= emacs-major-version 22)
444 nil
445 :safe)))
446 ;; ... or eval variables
447 (enable-local-eval nil)
448 )
449 (if (featurep 'xemacs)
450 (find-file-noselect file nowarn rawfile)
451 (find-file-noselect file nowarn rawfile wildcards))
452 ))
453
454\f
455;;; Editor goodies ;-)
456;;
457(defconst semantic-fw-font-lock-keywords
458 (eval-when-compile
459 (let* (
460 ;; Variable declarations
461 (vl nil)
462 (kv (if vl (regexp-opt vl t) ""))
463 ;; Function declarations
464 (vf '(
465 "define-lex"
466 "define-lex-analyzer"
467 "define-lex-block-analyzer"
468 "define-lex-regex-analyzer"
469 "define-lex-spp-macro-declaration-analyzer"
470 "define-lex-spp-macro-undeclaration-analyzer"
471 "define-lex-spp-include-analyzer"
472 "define-lex-simple-regex-analyzer"
473 "define-lex-keyword-type-analyzer"
474 "define-lex-sexp-type-analyzer"
475 "define-lex-regex-type-analyzer"
476 "define-lex-string-type-analyzer"
477 "define-lex-block-type-analyzer"
478 ;;"define-mode-overload-implementation"
479 ;;"define-semantic-child-mode"
480 "define-semantic-idle-service"
481 "define-semantic-decoration-style"
482 "define-wisent-lexer"
483 "semantic-alias-obsolete"
484 "semantic-varalias-obsolete"
485 "semantic-make-obsolete-overload"
486 "defcustom-mode-local-semantic-dependency-system-include-path"
487 ))
488 (kf (if vf (regexp-opt vf t) ""))
489 ;; Regexp depths
490 (kv-depth (if kv (regexp-opt-depth kv) nil))
491 (kf-depth (if kf (regexp-opt-depth kf) nil))
492 )
493 `((,(concat
494 ;; Declarative things
495 "(\\(" kv "\\|" kf "\\)"
496 ;; Whitespaces & names
497 "\\>[ \t]*\\(\\sw+\\)?[ \t]*\\(\\sw+\\)?"
498 )
499 (1 font-lock-keyword-face)
500 (,(+ 1 kv-depth kf-depth 1)
501 (cond ((match-beginning 2)
502 font-lock-type-face)
503 ((match-beginning ,(+ 1 kv-depth 1))
504 font-lock-function-name-face)
505 )
506 nil t)
507 (,(+ 1 kv-depth kf-depth 1 1)
508 (cond ((match-beginning 2)
509 font-lock-variable-name-face)
510 )
511 nil t)))
512 ))
513 "Highlighted Semantic keywords.")
514
515;; (when (fboundp 'font-lock-add-keywords)
516;; (font-lock-add-keywords 'emacs-lisp-mode
517;; semantic-fw-font-lock-keywords))
518\f
519;;; Interfacing with edebug
520;;
521(defun semantic-fw-add-edebug-spec ()
522 (def-edebug-spec semantic-exit-on-input 'def-body))
523
524(add-hook 'edebug-setup-hook 'semantic-fw-add-edebug-spec)
525
526(provide 'semantic/fw)
527
528;;; semantic-fw.el ends here