* cedet/semantic/tag-write.el (semantic-tag-write-list-slot-value):
[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)
3d9d8486 34(require 'semantic/loaddefs)
9d389824
CY
35
36;;; Compatibility
37;;
38(if (featurep 'xemacs)
39 (progn
40 (defalias 'semantic-buffer-local-value 'symbol-value-in-buffer)
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
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)))
50 (when rest
51 (set-extent-property ol 'start-open (car rest))
52 (setq rest (cdr rest)))
53 (when rest
54 (set-extent-property ol 'end-open (car rest)))
55 ol)))
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
62 (lambda (pos)
63 (condition-case nil
64 (extent-list nil pos pos)
65 (error nil))
66 ))
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))
84 (keyboard-quit)
85 c)))
86 event))
87 (defun semantic-popup-menu (menu)
88 "Blockinig version of `popup-menu'"
89 (popup-menu menu)
90 ;; Wait...
91 (while (popup-up-p) (dispatch-event (next-event))))
92 )
93 ;; Emacs Bindings
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))))
117 )
118
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)
123 )
124
125(if (featurep 'xemacs)
126 (defalias 'semantic-mode-line-update 'redraw-modeline)
127 (defalias 'semantic-mode-line-update 'force-mode-line-update))
128
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)
133 'run-mode-hooks
134 'run-hooks))
135
136;; Fancy compat useage now handled in cedet-compat
137(defalias 'semantic-subst-char-in-string 'subst-char-in-string)
138
139
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)))
144
145(defalias 'semantic-compile-warn
146 (eval-when-compile
147 (if (fboundp 'byte-compile-warn)
148 'byte-compile-warn
149 'message)))
150
151(if (not (fboundp 'string-to-number))
152 (defalias 'string-to-number 'string-to-int))
153
154;;; Menu Item compatibility
155;;
156(defun semantic-menu-item (item)
157 "Build an XEmacs compatible menu item from vector ITEM.
158That is remove the unsupported :help stuff."
159 (if (featurep 'xemacs)
160 (let ((n (length item))
161 (i 0)
162 slot l)
163 (while (< i n)
164 (setq slot (aref item i))
165 (if (and (keywordp slot)
166 (eq slot :help))
167 (setq i (1+ i))
168 (setq l (cons slot l)))
169 (setq i (1+ i)))
170 (apply #'vector (nreverse l)))
171 item))
172
173;;; Positional Data Cache
174;;
175(defvar semantic-cache-data-overlays nil
176 "List of all overlays waiting to be flushed.")
177
178(defun semantic-cache-data-to-buffer (buffer start end value name &optional lifespan)
179 "In BUFFER over the region START END, remember VALUE.
180NAME specifies a special name that can be searched for later to
181recover the cached data with `semantic-get-cache-data'.
182LIFESPAN indicates how long the data cache will be remembered.
183The default LIFESPAN is 'end-of-command.
184Possible Lifespans are:
185 'end-of-command - Remove the cache at the end of the currently
186 executing command.
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"
193 lifespan))
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)
202 ))
203
204(defun semantic-cache-data-post-command-hook ()
205 "Flush `semantic-cache-data-overlays' based 'lifespan property.
206Remove self from `post-command-hook' if it is empty."
207 (let ((newcache nil)
208 (oldcache semantic-cache-data-overlays))
209 (while oldcache
210 (let* ((o (car oldcache))
211 (life (semantic-overlay-get o 'lifespan))
212 )
213 (if (or (eq life 'end-of-command)
214 (and (eq life 'exit-cache-zone)
215 (not (member o (semantic-overlays-at (point))))))
216 (progn
217 ;;(message "Removing from cache: %s" o)
218 (semantic-overlay-delete o)
219 )
220 (setq newcache (cons o newcache))))
221 (setq oldcache (cdr oldcache)))
222 (setq semantic-cache-data-overlays (nreverse newcache)))
223
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)))
228
229(defun semantic-get-cache-data (name &optional point)
230 "Get cached data with NAME from optional POINT."
231 (save-excursion
232 (if point (goto-char point))
233 (let ((o (semantic-overlays-at (point)))
234 (ans nil))
235 (while (and (not ans) o)
236 (if (equal (semantic-overlay-get (car o) 'cache-name) name)
237 (setq ans (car o))
238 (setq o (cdr o))))
239 (when ans
240 (semantic-overlay-get ans 'cached-value)))))
241
242(defun semantic-test-data-cache ()
243 "Test the data cache."
244 (interactive)
245 (let ((data '(a b c)))
246 (save-excursion
247 (set-buffer (get-buffer-create " *semantic-test-data-cache*"))
248 (erase-buffer)
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"))
256 )))
257
258;;; Obsoleting various functions & variables
259;;
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)))
265 name)))
266
267(defun semantic-alias-obsolete (oldfnalias newfn)
268 "Make OLDFNALIAS an alias for NEWFN.
269Mark OLDFNALIAS as obsolete, such that the byte compiler
270will 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))
279 )
280 (make-obsolete-overload oldfnalias newfn)
281 (semantic-compile-warn
282 "%s: `%s' obsoletes overload `%s'"
283 byte-compile-current-file
284 newfn
285 (semantic-overload-symbol-from-function oldfnalias))
286 ))
287
288(defun semantic-varalias-obsolete (oldvaralias newvar)
289 "Make OLDVARALIAS an alias for variable NEWVAR.
290Mark OLDVARALIAS as obsolete, such that the byte compiler
291will throw a warning when it encounters this symbol."
292 (make-obsolete-variable oldvaralias newvar)
293 (condition-case nil
294 (defvaralias oldvaralias newvar)
295 (error
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'"
301 newvar oldvaralias)
302 ))))
303\f
304;;; Help debugging
305;;
306(defmacro semantic-safe (format &rest body)
307 "Turn into a FORMAT message any error caught during eval of BODY.
308Return the value of last BODY form or nil if an error occurred.
309FORMAT can have a %s escape which will be replaced with the actual
310error message.
311If `debug-on-error' is set, errors are not caught, so that you can
312debug them.
313Avoid using a large BODY since it is duplicated."
314 ;;(declare (debug t) (indent 1))
315 `(if debug-on-error
316 ;;(let ((inhibit-quit nil)) ,@body)
317 ;; Note to self: Doing the above screws up the wisent parser.
318 (progn ,@body)
319 (condition-case err
320 (progn ,@body)
321 (error
322 (message ,format (format "%S - %s" (current-buffer)
323 (error-message-string err)))
324 nil))))
325(put 'semantic-safe 'lisp-indent-function 1)
326
327;;; Misc utilities
328;;
329(defsubst semantic-map-buffers (function)
330 "Run FUNCTION for each Semantic enabled buffer found.
331FUNCTION 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))
334
335(defalias 'semantic-map-mode-buffers
336 'mode-local-map-mode-buffers)
337
338(semantic-alias-obsolete 'semantic-fetch-overload
339 'fetch-overload)
340
341(semantic-alias-obsolete 'define-mode-overload-implementation
342 'define-mode-local-override)
343
344(semantic-alias-obsolete 'semantic-with-mode-bindings
345 'with-mode-local)
346
347(semantic-alias-obsolete 'define-semantic-child-mode
348 'define-child-mode)
349
350(defun semantic-install-function-overrides (overrides &optional transient mode)
351 "Install the function OVERRIDES in the specified environment.
352OVERRIDES must be an alist ((OVERLOAD . FUNCTION) ...) where OVERLOAD
353is a symbol identifying an overloadable entry, and FUNCTION is the
354function to override it with.
355If optional argument TRANSIENT is non-nil, installed overrides can in
356turn be overridden by next installation.
357If optional argument MODE is non-nil, it must be a major mode symbol.
358OVERRIDES will be installed globally for this major mode. If MODE is
359nil, OVERRIDES will be installed locally in the current buffer. This
360later installation should be done in MODE hook."
361 (mode-local-bind
362 ;; Add the semantic- prefix to OVERLOAD short names.
363 (mapcar
364 #'(lambda (e)
365 (let ((name (symbol-name (car e))))
366 (if (string-match "^semantic-" name)
367 e
368 (cons (intern (format "semantic-%s" name)) (cdr e)))))
369 overrides)
370 (list 'constant-flag (not transient)
371 'override-flag t)
372 mode))
373\f
374;;; User Interrupt handling
375;;
376(defvar semantic-current-input-throw-symbol nil
377 "The current throw symbol for `semantic-exit-on-input'.")
378
379(defmacro semantic-exit-on-input (symbol &rest forms)
380 "Using SYMBOL as an argument to `throw', execute FORMS.
381If FORMS includes a call to `semantic-thow-on-input', then
382if a user presses any key during execution, this form macro
383will exit with the value passed to `semantic-throw-on-input'.
384If FORMS completes, then the return value is the same as `progn'."
385 `(let ((semantic-current-input-throw-symbol ,symbol))
386 (catch ,symbol
387 ,@forms)))
388(put 'semantic-exit-on-input 'lisp-indent-function 1)
389
390(defmacro semantic-throw-on-input (from)
391 "Exit with `throw' when in `semantic-exit-on-input' on user input.
392FROM is an indication of where this function is called from as a value
393to pass to `throw'. It is recommended to use the name of the function
394calling this one."
395 `(when (and semantic-current-input-throw-symbol
396 (or (input-pending-p) (accept-process-output)))
397 (throw semantic-current-input-throw-symbol ,from)))
398
399(defun semantic-test-throw-on-input ()
400 "Test that throw on input will work."
401 (interactive)
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))
407 (while t
408 (message "Looping ... press a key to test")
409 (semantic-throw-on-input 'test-inner-loop))
410 'exit)))
411 (when (input-pending-p)
412 (if (fboundp 'read-event)
413 (read-event)
414 (read-char)))
415 )
416\f
417;;; Special versions of Find File
418;;
419(defun semantic-find-file-noselect (file &optional nowarn rawfile wildcards)
420 "Call `find-file-noselect' with various features turned off.
421Use this when referencing a file that will be soon deleted.
422FILE, 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
437 (auto-insert nil)
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?
442 nil
443 ;; Emacs 23 has the spiffy :safe option, nil otherwise.
444 (if (>= emacs-major-version 22)
445 nil
446 :safe)))
447 ;; ... or eval variables
448 (enable-local-eval nil)
449 )
450 (if (featurep 'xemacs)
451 (find-file-noselect file nowarn rawfile)
452 (find-file-noselect file nowarn rawfile wildcards))
453 ))
454
455\f
456;;; Editor goodies ;-)
457;;
458(defconst semantic-fw-font-lock-keywords
459 (eval-when-compile
460 (let* (
461 ;; Variable declarations
462 (vl nil)
463 (kv (if vl (regexp-opt vl t) ""))
464 ;; Function declarations
465 (vf '(
466 "define-lex"
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"
488 ))
489 (kf (if vf (regexp-opt vf t) ""))
490 ;; Regexp depths
491 (kv-depth (if kv (regexp-opt-depth kv) nil))
492 (kf-depth (if kf (regexp-opt-depth kf) nil))
493 )
494 `((,(concat
495 ;; Declarative things
496 "(\\(" kv "\\|" kf "\\)"
497 ;; Whitespaces & names
498 "\\>[ \t]*\\(\\sw+\\)?[ \t]*\\(\\sw+\\)?"
499 )
500 (1 font-lock-keyword-face)
501 (,(+ 1 kv-depth kf-depth 1)
502 (cond ((match-beginning 2)
503 font-lock-type-face)
504 ((match-beginning ,(+ 1 kv-depth 1))
505 font-lock-function-name-face)
506 )
507 nil t)
508 (,(+ 1 kv-depth kf-depth 1 1)
509 (cond ((match-beginning 2)
510 font-lock-variable-name-face)
511 )
512 nil t)))
513 ))
514 "Highlighted Semantic keywords.")
515
516;; (when (fboundp 'font-lock-add-keywords)
517;; (font-lock-add-keywords 'emacs-lisp-mode
518;; semantic-fw-font-lock-keywords))
519\f
520;;; Interfacing with edebug
521;;
522(defun semantic-fw-add-edebug-spec ()
523 (def-edebug-spec semantic-exit-on-input 'def-body))
524
525(add-hook 'edebug-setup-hook 'semantic-fw-add-edebug-spec)
526
527(provide 'semantic/fw)
528
529;;; semantic-fw.el ends here