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