fw.el 15 KB

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