]> code.delx.au - gnu-emacs/blob - lisp/cedet/semantic/fw.el
479044ec518de706658eaf6221025e87f9a4e0bc
[gnu-emacs] / lisp / cedet / semantic / fw.el
1 ;;; semantic/fw.el --- Framework for Semantic
2
3 ;;; Copyright (C) 1999-2011 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 (defalias 'semantic-buffer-local-value 'buffer-local-value)
38 (defalias 'semantic-overlay-live-p 'overlay-buffer)
39 (defalias 'semantic-make-overlay 'make-overlay)
40 (defalias 'semantic-overlay-put 'overlay-put)
41 (defalias 'semantic-overlay-get 'overlay-get)
42 (defalias 'semantic-overlay-properties 'overlay-properties)
43 (defalias 'semantic-overlay-move 'move-overlay)
44 (defalias 'semantic-overlay-delete 'delete-overlay)
45 (defalias 'semantic-overlays-at 'overlays-at)
46 (defalias 'semantic-overlays-in 'overlays-in)
47 (defalias 'semantic-overlay-buffer 'overlay-buffer)
48 (defalias 'semantic-overlay-start 'overlay-start)
49 (defalias 'semantic-overlay-end 'overlay-end)
50 (defalias 'semantic-overlay-size 'overlay-size)
51 (defalias 'semantic-overlay-next-change 'next-overlay-change)
52 (defalias 'semantic-overlay-previous-change 'previous-overlay-change)
53 (defalias 'semantic-overlay-lists 'overlay-lists)
54 (defalias 'semantic-overlay-p 'overlayp)
55 (defalias 'semantic-read-event 'read-event)
56 (defalias 'semantic-popup-menu 'popup-menu)
57 (defalias 'semantic-make-local-hook 'identity)
58 (defalias 'semantic-mode-line-update 'force-mode-line-update)
59 (defalias 'semantic-run-mode-hooks 'run-mode-hooks)
60 (defalias 'semantic-compile-warn 'byte-compile-warn)
61 (defalias 'semantic-menu-item 'identity)
62
63 (defun semantic-event-window (event)
64 "Extract the window from EVENT."
65 (car (car (cdr event))))
66
67 (defun semantic-delete-overlay-maybe (overlay)
68 "Delete OVERLAY if it is a semantic token overlay."
69 (if (semantic-overlay-get overlay 'semantic)
70 (semantic-overlay-delete overlay)))
71
72 ;;; Positional Data Cache
73 ;;
74 (defvar semantic-cache-data-overlays nil
75 "List of all overlays waiting to be flushed.")
76
77 (defun semantic-cache-data-to-buffer (buffer start end value name &optional lifespan)
78 "In BUFFER over the region START END, remember VALUE.
79 NAME specifies a special name that can be searched for later to
80 recover the cached data with `semantic-get-cache-data'.
81 LIFESPAN indicates how long the data cache will be remembered.
82 The default LIFESPAN is 'end-of-command.
83 Possible Lifespans are:
84 'end-of-command - Remove the cache at the end of the currently
85 executing command.
86 'exit-cache-zone - Remove when point leaves the overlay at the
87 end of the currently executing command."
88 ;; Check if LIFESPAN is valid before to create any overlay
89 (or lifespan (setq lifespan 'end-of-command))
90 (or (memq lifespan '(end-of-command exit-cache-zone))
91 (error "semantic-cache-data-to-buffer: Unknown LIFESPAN: %s"
92 lifespan))
93 (let ((o (semantic-make-overlay start end buffer)))
94 (semantic-overlay-put o 'cache-name name)
95 (semantic-overlay-put o 'cached-value value)
96 (semantic-overlay-put o 'lifespan lifespan)
97 (setq semantic-cache-data-overlays
98 (cons o semantic-cache-data-overlays))
99 ;;(message "Adding to cache: %s" o)
100 (add-hook 'post-command-hook 'semantic-cache-data-post-command-hook)
101 ))
102
103 (defun semantic-cache-data-post-command-hook ()
104 "Flush `semantic-cache-data-overlays' based 'lifespan property.
105 Remove self from `post-command-hook' if it is empty."
106 (let ((newcache nil)
107 (oldcache semantic-cache-data-overlays))
108 (while oldcache
109 (let* ((o (car oldcache))
110 (life (semantic-overlay-get o 'lifespan))
111 )
112 (if (or (eq life 'end-of-command)
113 (and (eq life 'exit-cache-zone)
114 (not (member o (semantic-overlays-at (point))))))
115 (progn
116 ;;(message "Removing from cache: %s" o)
117 (semantic-overlay-delete o)
118 )
119 (setq newcache (cons o newcache))))
120 (setq oldcache (cdr oldcache)))
121 (setq semantic-cache-data-overlays (nreverse newcache)))
122
123 ;; Remove ourselves if we have removed all overlays.
124 (unless semantic-cache-data-overlays
125 (remove-hook 'post-command-hook
126 'semantic-cache-data-post-command-hook)))
127
128 (defun semantic-get-cache-data (name &optional point)
129 "Get cached data with NAME from optional POINT."
130 (save-excursion
131 (if point (goto-char point))
132 (let ((o (semantic-overlays-at (point)))
133 (ans nil))
134 (while (and (not ans) o)
135 (if (equal (semantic-overlay-get (car o) 'cache-name) name)
136 (setq ans (car o))
137 (setq o (cdr o))))
138 (when ans
139 (semantic-overlay-get ans 'cached-value)))))
140
141 ;;; Obsoleting various functions & variables
142 ;;
143 (defun semantic-overload-symbol-from-function (name)
144 "Return the symbol for overload used by NAME, the defined symbol."
145 (let ((sym-name (symbol-name name)))
146 (if (string-match "^semantic-" sym-name)
147 (intern (substring sym-name (match-end 0)))
148 name)))
149
150 (defun semantic-alias-obsolete (oldfnalias newfn when)
151 "Make OLDFNALIAS an alias for NEWFN.
152 Mark OLDFNALIAS as obsolete, such that the byte compiler
153 will throw a warning when it encounters this symbol."
154 (defalias oldfnalias newfn)
155 (make-obsolete oldfnalias newfn when)
156 (when (and (function-overload-p newfn)
157 (not (overload-obsoleted-by newfn))
158 ;; Only throw this warning when byte compiling things.
159 (boundp 'byte-compile-current-file)
160 byte-compile-current-file
161 (not (string-match "cedet" byte-compile-current-file))
162 )
163 (make-obsolete-overload oldfnalias newfn when)
164 (semantic-compile-warn
165 "%s: `%s' obsoletes overload `%s'"
166 byte-compile-current-file
167 newfn
168 (semantic-overload-symbol-from-function oldfnalias))
169 ))
170
171 (defun semantic-varalias-obsolete (oldvaralias newvar when)
172 "Make OLDVARALIAS an alias for variable NEWVAR.
173 Mark OLDVARALIAS as obsolete, such that the byte compiler
174 will throw a warning when it encounters this symbol."
175 (make-obsolete-variable oldvaralias newvar when)
176 (condition-case nil
177 (defvaralias oldvaralias newvar)
178 (error
179 ;; Only throw this warning when byte compiling things.
180 (when (and (boundp 'byte-compile-current-file)
181 byte-compile-current-file)
182 (semantic-compile-warn
183 "variable `%s' obsoletes, but isn't alias of `%s'"
184 newvar oldvaralias)
185 ))))
186 \f
187 ;;; Help debugging
188 ;;
189 (defmacro semantic-safe (format &rest body)
190 "Turn into a FORMAT message any error caught during eval of BODY.
191 Return the value of last BODY form or nil if an error occurred.
192 FORMAT can have a %s escape which will be replaced with the actual
193 error message.
194 If `debug-on-error' is set, errors are not caught, so that you can
195 debug them.
196 Avoid using a large BODY since it is duplicated."
197 ;;(declare (debug t) (indent 1))
198 `(if debug-on-error
199 ;;(let ((inhibit-quit nil)) ,@body)
200 ;; Note to self: Doing the above screws up the wisent parser.
201 (progn ,@body)
202 (condition-case err
203 (progn ,@body)
204 (error
205 (message ,format (format "%S - %s" (current-buffer)
206 (error-message-string err)))
207 nil))))
208 (put 'semantic-safe 'lisp-indent-function 1)
209
210 ;;; Misc utilities
211 ;;
212 (defsubst semantic-map-buffers (function)
213 "Run FUNCTION for each Semantic enabled buffer found.
214 FUNCTION does not have arguments. When FUNCTION is entered
215 `current-buffer' is a selected Semantic enabled buffer."
216 (mode-local-map-file-buffers function #'semantic-active-p))
217
218 (defalias 'semantic-map-mode-buffers 'mode-local-map-mode-buffers)
219
220 (semantic-alias-obsolete 'define-mode-overload-implementation
221 'define-mode-local-override "23.2")
222
223 (defun semantic-install-function-overrides (overrides &optional transient mode)
224 "Install the function OVERRIDES in the specified environment.
225 OVERRIDES must be an alist ((OVERLOAD . FUNCTION) ...) where OVERLOAD
226 is a symbol identifying an overloadable entry, and FUNCTION is the
227 function to override it with.
228 If optional argument TRANSIENT is non-nil, installed overrides can in
229 turn be overridden by next installation.
230 If optional argument MODE is non-nil, it must be a major mode symbol.
231 OVERRIDES will be installed globally for this major mode. If MODE is
232 nil, OVERRIDES will be installed locally in the current buffer. This
233 later installation should be done in MODE hook."
234 (mode-local-bind
235 ;; Add the semantic- prefix to OVERLOAD short names.
236 (mapcar
237 #'(lambda (e)
238 (let ((name (symbol-name (car e))))
239 (if (string-match "^semantic-" name)
240 e
241 (cons (intern (format "semantic-%s" name)) (cdr e)))))
242 overrides)
243 (list 'constant-flag (not transient)
244 'override-flag t)
245 mode))
246 \f
247 ;;; User Interrupt handling
248 ;;
249 (defvar semantic-current-input-throw-symbol nil
250 "The current throw symbol for `semantic-exit-on-input'.")
251
252 (defmacro semantic-exit-on-input (symbol &rest forms)
253 "Using SYMBOL as an argument to `throw', execute FORMS.
254 If FORMS includes a call to `semantic-throw-on-input', then
255 if a user presses any key during execution, this form macro
256 will exit with the value passed to `semantic-throw-on-input'.
257 If FORMS completes, then the return value is the same as `progn'."
258 `(let ((semantic-current-input-throw-symbol ,symbol))
259 (catch ,symbol
260 ,@forms)))
261 (put 'semantic-exit-on-input 'lisp-indent-function 1)
262
263 (defmacro semantic-throw-on-input (from)
264 "Exit with `throw' when in `semantic-exit-on-input' on user input.
265 FROM is an indication of where this function is called from as a value
266 to pass to `throw'. It is recommended to use the name of the function
267 calling this one."
268 `(when (and semantic-current-input-throw-symbol
269 (or (input-pending-p) (accept-process-output)))
270 (throw semantic-current-input-throw-symbol ,from)))
271
272 \f
273 ;;; Special versions of Find File
274 ;;
275 (defun semantic-find-file-noselect (file &optional nowarn rawfile wildcards)
276 "Call `find-file-noselect' with various features turned off.
277 Use this when referencing a file that will be soon deleted.
278 FILE, NOWARN, RAWFILE, and WILDCARDS are passed into `find-file-noselect'"
279 (let* ((recentf-exclude '( (lambda (f) t) ))
280 ;; This is a brave statement. Don't waste time loading in
281 ;; lots of modes. Especially decoration mode can waste a lot
282 ;; of time for a buffer we intend to kill.
283 (semantic-init-hook nil)
284 ;; This disables the part of EDE that asks questions
285 (ede-auto-add-method 'never)
286 ;; Ask font-lock to not colorize these buffers, nor to
287 ;; whine about it either.
288 (font-lock-maximum-size 0)
289 (font-lock-verbose nil)
290 ;; Disable revision control
291 (vc-handled-backends nil)
292 ;; Don't prompt to insert a template if we visit an empty file
293 (auto-insert nil)
294 ;; We don't want emacs to query about unsafe local variables
295 (enable-local-variables
296 (if (featurep 'xemacs)
297 ;; XEmacs only has nil as an option?
298 nil
299 ;; Emacs 23 has the spiffy :safe option, nil otherwise.
300 (if (>= emacs-major-version 22)
301 nil
302 :safe)))
303 ;; ... or eval variables
304 (enable-local-eval nil)
305 )
306 (save-match-data
307 (if (featurep 'xemacs)
308 (find-file-noselect file nowarn rawfile)
309 (find-file-noselect file nowarn rawfile wildcards)))
310 ))
311
312 ;;; Database restriction settings
313 ;;
314 (defmacro semanticdb-without-unloaded-file-searches (forms)
315 "Execute FORMS with `unloaded' removed from the current throttle."
316 `(let ((semanticdb-find-default-throttle
317 (if (featurep 'semantic/db-find)
318 (remq 'unloaded semanticdb-find-default-throttle)
319 nil)))
320 ,forms))
321 (put 'semanticdb-without-unloaded-file-searches 'lisp-indent-function 1)
322
323 \f
324 ;; ;;; Editor goodies ;-)
325 ;; ;;
326 ;; (defconst semantic-fw-font-lock-keywords
327 ;; (eval-when-compile
328 ;; (let* (
329 ;; ;; Variable declarations
330 ;; (vl nil)
331 ;; (kv (if vl (regexp-opt vl t) ""))
332 ;; ;; Function declarations
333 ;; (vf '(
334 ;; "define-lex"
335 ;; "define-lex-analyzer"
336 ;; "define-lex-block-analyzer"
337 ;; "define-lex-regex-analyzer"
338 ;; "define-lex-spp-macro-declaration-analyzer"
339 ;; "define-lex-spp-macro-undeclaration-analyzer"
340 ;; "define-lex-spp-include-analyzer"
341 ;; "define-lex-simple-regex-analyzer"
342 ;; "define-lex-keyword-type-analyzer"
343 ;; "define-lex-sexp-type-analyzer"
344 ;; "define-lex-regex-type-analyzer"
345 ;; "define-lex-string-type-analyzer"
346 ;; "define-lex-block-type-analyzer"
347 ;; ;;"define-mode-overload-implementation"
348 ;; ;;"define-semantic-child-mode"
349 ;; "define-semantic-idle-service"
350 ;; "define-semantic-decoration-style"
351 ;; "define-wisent-lexer"
352 ;; "semantic-alias-obsolete"
353 ;; "semantic-varalias-obsolete"
354 ;; "semantic-make-obsolete-overload"
355 ;; "defcustom-mode-local-semantic-dependency-system-include-path"
356 ;; ))
357 ;; (kf (if vf (regexp-opt vf t) ""))
358 ;; ;; Regexp depths
359 ;; (kv-depth (if kv (regexp-opt-depth kv) nil))
360 ;; (kf-depth (if kf (regexp-opt-depth kf) nil))
361 ;; )
362 ;; `((,(concat
363 ;; ;; Declarative things
364 ;; "(\\(" kv "\\|" kf "\\)"
365 ;; ;; Whitespaces & names
366 ;; "\\>[ \t]*\\(\\sw+\\)?[ \t]*\\(\\sw+\\)?"
367 ;; )
368 ;; (1 font-lock-keyword-face)
369 ;; (,(+ 1 kv-depth kf-depth 1)
370 ;; (cond ((match-beginning 2)
371 ;; font-lock-type-face)
372 ;; ((match-beginning ,(+ 1 kv-depth 1))
373 ;; font-lock-function-name-face)
374 ;; )
375 ;; nil t)
376 ;; (,(+ 1 kv-depth kf-depth 1 1)
377 ;; (cond ((match-beginning 2)
378 ;; font-lock-variable-name-face)
379 ;; )
380 ;; nil t)))
381 ;; ))
382 ;; "Highlighted Semantic keywords.")
383
384 ;; (when (fboundp 'font-lock-add-keywords)
385 ;; (font-lock-add-keywords 'emacs-lisp-mode
386 ;; semantic-fw-font-lock-keywords))
387 \f
388 ;;; Interfacing with edebug
389 ;;
390 (defun semantic-fw-add-edebug-spec ()
391 (def-edebug-spec semantic-exit-on-input 'def-body))
392
393 (add-hook 'edebug-setup-hook 'semantic-fw-add-edebug-spec)
394
395 (provide 'semantic/fw)
396
397 ;;; semantic/fw.el ends here