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