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