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