util-modes.el 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. ;;; semantic/util-modes.el --- Semantic minor modes
  2. ;; Copyright (C) 2000-2005, 2007-2012 Free Software Foundation, Inc.
  3. ;; Authors: Eric M. Ludlam <zappo@gnu.org>
  4. ;; David Ponce <david@dponce.com>
  5. ;; Keywords: syntax
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;
  19. ;; Semantic utility minor modes.
  20. ;;
  21. ;;; Code:
  22. ;; FIXME: compiling util-modes.el seems to require loading util-modes.el,
  23. ;; so if the previous compilation generated a file that fails to load,
  24. ;; recompiling fails to fix the problem.
  25. (require 'semantic)
  26. ;;; Group for all semantic enhancing modes
  27. (defgroup semantic-modes nil
  28. "Minor modes associated with the Semantic architecture."
  29. :group 'semantic)
  30. ;;;;
  31. ;;;; Semantic minor modes stuff
  32. ;;;;
  33. (defcustom semantic-update-mode-line t
  34. "If non-nil, show enabled minor modes in the mode line.
  35. Only minor modes that are not turned on globally are shown in the mode
  36. line."
  37. :group 'semantic
  38. :type 'boolean
  39. :require 'semantic/util-modes
  40. :initialize 'custom-initialize-default
  41. :set (lambda (sym val)
  42. (set-default sym val)
  43. ;; Update status of all Semantic enabled buffers
  44. (semantic-mode-line-update)))
  45. (defcustom semantic-mode-line-prefix
  46. (propertize "S" 'face 'bold)
  47. "Prefix added to minor mode indicators in the mode line."
  48. :group 'semantic
  49. :type 'string
  50. :require 'semantic/util-modes
  51. :initialize 'custom-initialize-default)
  52. (defvar semantic-minor-modes-format nil
  53. "Mode line format showing Semantic minor modes which are locally enabled.
  54. It is displayed in the mode line.")
  55. (put 'semantic-minor-modes-format 'risky-local-variable t)
  56. (defvar semantic-minor-mode-alist nil
  57. "Alist saying how to show Semantic minor modes in the mode line.
  58. Like variable `minor-mode-alist'.")
  59. (defun semantic-mode-line-update ()
  60. "Update mode line format of Semantic minor modes.
  61. Only minor modes that are locally enabled are shown in the mode line."
  62. (setq semantic-minor-modes-format nil)
  63. (dolist (x semantic-minor-mode-alist)
  64. (setq minor-mode-alist (delq (assq (car x) minor-mode-alist)
  65. minor-mode-alist)))
  66. (when semantic-update-mode-line
  67. (let ((locals '()))
  68. ;; Select the minor modes that aren't enabled globally and who
  69. ;; have a non-empty "name".
  70. (dolist (x semantic-minor-mode-alist)
  71. (unless (or (memq (car x) semantic-init-hook)
  72. (not (string-match "^[ ]*\\(.+\\)" (cadr x))))
  73. (push (list (car x) (concat "/" (match-string 1 (cadr x)))) locals)))
  74. ;; Then build the format spec.
  75. (when locals
  76. (let ((prefix (if (string-match "^[ ]*\\(.+\\)"
  77. semantic-mode-line-prefix)
  78. (match-string 1 semantic-mode-line-prefix)
  79. "S")))
  80. (setq semantic-minor-modes-format
  81. `((:eval (if (or ,@(mapcar 'car locals))
  82. ,(concat " " prefix)))))
  83. ;; It would be easier to just put `locals' inside
  84. ;; semantic-minor-modes-format, but then things like
  85. ;; mode-line-minor-mode-help can't find the right major mode
  86. ;; any more. So instead, we carefully put the minor modes
  87. ;; in minor-mode-alist.
  88. (let* ((elem (or (assq 'semantic-minor-modes-format
  89. minor-mode-alist)
  90. ;; FIXME: This entry is meaningless for
  91. ;; mode-line-minor-mode-help.
  92. '(semantic-minor-modes-format
  93. semantic-minor-modes-format)))
  94. (tail (or (memq elem minor-mode-alist)
  95. (setq minor-mode-alist
  96. (cons elem minor-mode-alist)))))
  97. (setcdr tail (nconc locals (cdr tail)))))))))
  98. (defun semantic-desktop-ignore-this-minor-mode (buffer)
  99. "Installed as a minor-mode initializer for Desktop mode.
  100. BUFFER is the buffer to not initialize a Semantic minor mode in."
  101. nil)
  102. (defun semantic-add-minor-mode (toggle name)
  103. "Register a new Semantic minor mode.
  104. TOGGLE is a symbol which is the name of a buffer-local variable that
  105. is toggled on or off to say whether the minor mode is active or not.
  106. It is also an interactive function to toggle the mode.
  107. NAME specifies what will appear in the mode line when the minor mode
  108. is active. NAME should be either a string starting with a space, or a
  109. symbol whose value is such a string."
  110. ;; Record how to display this minor mode in the mode line
  111. (let ((mm (assq toggle semantic-minor-mode-alist)))
  112. (if mm
  113. (setcdr mm (list name))
  114. (setq semantic-minor-mode-alist (cons (list toggle name)
  115. semantic-minor-mode-alist))))
  116. (semantic-mode-line-update)
  117. ;; Semantic minor modes don't work w/ Desktop restore.
  118. ;; This line will disable this minor mode from being restored
  119. ;; by Desktop.
  120. (when (boundp 'desktop-minor-mode-handlers)
  121. (add-to-list 'desktop-minor-mode-handlers
  122. (cons toggle 'semantic-desktop-ignore-this-minor-mode))))
  123. (defun semantic-toggle-minor-mode-globally (mode &optional arg)
  124. "Toggle minor mode MODE in every Semantic enabled buffer.
  125. Return non-nil if MODE is turned on in every Semantic enabled buffer.
  126. If ARG is positive, enable, if it is negative, disable.
  127. MODE must be a valid minor mode defined in `minor-mode-alist' and must be
  128. too an interactive function used to toggle the mode."
  129. ;; FIXME: All callers should pass a -1 or +1 argument.
  130. (or (and (fboundp mode) (or (assq mode minor-mode-alist) ;Needed?
  131. (assq mode semantic-minor-mode-alist)))
  132. (error "Semantic minor mode %s not found" mode))
  133. ;; Add or remove the MODE toggle function from `semantic-init-hook'.
  134. (cond
  135. ;; Turn off if ARG < 0
  136. ((< arg 0) (remove-hook 'semantic-init-hook mode))
  137. ;; Turn on if ARG > 0
  138. ((> arg 0) (add-hook 'semantic-init-hook mode))
  139. ;; Otherwise just check MODE state
  140. (t
  141. (error "semantic-toggle-minor-mode-globally: arg should be -1 or 1")))
  142. ;; Update the minor mode format.
  143. (semantic-mode-line-update)
  144. ;; Then turn MODE on or off in every Semantic enabled buffer.
  145. (semantic-map-buffers #'(lambda () (funcall mode arg))))
  146. ;;;;
  147. ;;;; Minor mode to highlight areas that a user edits.
  148. ;;;;
  149. ;;;###autoload
  150. (define-minor-mode global-semantic-highlight-edits-mode
  151. "Toggle global use of option `semantic-highlight-edits-mode'.
  152. If ARG is positive or nil, enable, if it is negative, disable."
  153. :global t :group 'semantic :group 'semantic-modes
  154. (semantic-toggle-minor-mode-globally
  155. 'semantic-highlight-edits-mode
  156. (if global-semantic-highlight-edits-mode 1 -1)))
  157. (defcustom semantic-highlight-edits-mode-hook nil
  158. "Hook run at the end of function `semantic-highlight-edits-mode'."
  159. :group 'semantic
  160. :type 'hook)
  161. (defface semantic-highlight-edits-face
  162. '((((class color) (background dark))
  163. ;; Put this back to something closer to black later.
  164. (:background "gray20"))
  165. (((class color) (background light))
  166. (:background "gray90")))
  167. "Face used to show dirty tokens in `semantic-highlight-edits-mode'."
  168. :group 'semantic-faces)
  169. (defun semantic-highlight-edits-new-change-hook-fcn (overlay)
  170. "Function set into `semantic-edits-new-change-hook'.
  171. Argument OVERLAY is the overlay created to mark the change.
  172. This function will set the face property on this overlay."
  173. (semantic-overlay-put overlay 'face 'semantic-highlight-edits-face))
  174. (defvar semantic-highlight-edits-mode-map
  175. (let ((km (make-sparse-keymap)))
  176. km)
  177. "Keymap for highlight-edits minor mode.")
  178. ;;;###autoload
  179. (define-minor-mode semantic-highlight-edits-mode
  180. "Minor mode for highlighting changes made in a buffer.
  181. Changes are tracked by semantic so that the incremental parser can work
  182. properly.
  183. This mode will highlight those changes as they are made, and clear them
  184. when the incremental parser accounts for those edits.
  185. With prefix argument ARG, turn on if positive, otherwise off. The
  186. minor mode can be turned on only if semantic feature is available and
  187. the current buffer was set up for parsing. Return non-nil if the
  188. minor mode is enabled."
  189. :keymap semantic-highlight-edits-mode-map
  190. (if semantic-highlight-edits-mode
  191. (if (not (and (featurep 'semantic) (semantic-active-p)))
  192. (progn
  193. ;; Disable minor mode if semantic stuff not available
  194. (setq semantic-highlight-edits-mode nil)
  195. (error "Buffer %s was not set up for parsing"
  196. (buffer-name)))
  197. (semantic-make-local-hook 'semantic-edits-new-change-hooks)
  198. (add-hook 'semantic-edits-new-change-hooks
  199. 'semantic-highlight-edits-new-change-hook-fcn nil t))
  200. ;; Remove hooks
  201. (remove-hook 'semantic-edits-new-change-hooks
  202. 'semantic-highlight-edits-new-change-hook-fcn t)))
  203. (semantic-add-minor-mode 'semantic-highlight-edits-mode
  204. "e")
  205. ;;;;
  206. ;;;; Minor mode to show unmatched-syntax elements
  207. ;;;;
  208. ;;;###autoload
  209. (define-minor-mode global-semantic-show-unmatched-syntax-mode
  210. "Toggle global use of option `semantic-show-unmatched-syntax-mode'.
  211. If ARG is positive or nil, enable, if it is negative, disable."
  212. :global t :group 'semantic :group 'semantic-modes
  213. ;; Not needed because it's autoloaded instead.
  214. ;; :require 'semantic/util-modes
  215. (semantic-toggle-minor-mode-globally
  216. 'semantic-show-unmatched-syntax-mode
  217. (if global-semantic-show-unmatched-syntax-mode 1 -1)))
  218. (defcustom semantic-show-unmatched-syntax-mode-hook nil
  219. "Hook run at the end of function `semantic-show-unmatched-syntax-mode'."
  220. :group 'semantic
  221. :type 'hook)
  222. (defface semantic-unmatched-syntax-face
  223. '((((class color) (background dark))
  224. (:underline "red"))
  225. (((class color) (background light))
  226. (:underline "red")))
  227. "Face used to show unmatched syntax in.
  228. The face is used in `semantic-show-unmatched-syntax-mode'."
  229. :group 'semantic-faces)
  230. (defsubst semantic-unmatched-syntax-overlay-p (overlay)
  231. "Return non-nil if OVERLAY is an unmatched syntax one."
  232. (eq (semantic-overlay-get overlay 'semantic) 'unmatched))
  233. (defun semantic-showing-unmatched-syntax-p ()
  234. "Return non-nil if an unmatched syntax overlay was found in buffer."
  235. (let ((ol (semantic-overlays-in (point-min) (point-max)))
  236. found)
  237. (while (and ol (not found))
  238. (setq found (semantic-unmatched-syntax-overlay-p (car ol))
  239. ol (cdr ol)))
  240. found))
  241. (defun semantic-show-unmatched-lex-tokens-fetch ()
  242. "Fetch a list of unmatched lexical tokens from the current buffer.
  243. Uses the overlays which have accurate bounds, and rebuilds what was
  244. originally passed in."
  245. (let ((ol (semantic-overlays-in (point-min) (point-max)))
  246. (ustc nil))
  247. (while ol
  248. (if (semantic-unmatched-syntax-overlay-p (car ol))
  249. (setq ustc (cons (cons 'thing
  250. (cons (semantic-overlay-start (car ol))
  251. (semantic-overlay-end (car ol))))
  252. ustc)))
  253. (setq ol (cdr ol)))
  254. (nreverse ustc))
  255. )
  256. (defun semantic-clean-unmatched-syntax-in-region (beg end)
  257. "Remove all unmatched syntax overlays between BEG and END."
  258. (let ((ol (semantic-overlays-in beg end)))
  259. (while ol
  260. (if (semantic-unmatched-syntax-overlay-p (car ol))
  261. (semantic-overlay-delete (car ol)))
  262. (setq ol (cdr ol)))))
  263. (defsubst semantic-clean-unmatched-syntax-in-buffer ()
  264. "Remove all unmatched syntax overlays found in current buffer."
  265. (semantic-clean-unmatched-syntax-in-region
  266. (point-min) (point-max)))
  267. (defsubst semantic-clean-token-of-unmatched-syntax (token)
  268. "Clean the area covered by TOKEN of unmatched syntax markers."
  269. (semantic-clean-unmatched-syntax-in-region
  270. (semantic-tag-start token) (semantic-tag-end token)))
  271. (defun semantic-show-unmatched-syntax (syntax)
  272. "Function set into `semantic-unmatched-syntax-hook'.
  273. This will highlight elements in SYNTAX as unmatched syntax."
  274. ;; This is called when `semantic-show-unmatched-syntax-mode' is
  275. ;; enabled. Highlight the unmatched syntax, and then add a semantic
  276. ;; property to that overlay so we can add it to the official list of
  277. ;; semantic supported overlays. This gets it cleaned up for errors,
  278. ;; buffer cleaning, and the like.
  279. (semantic-clean-unmatched-syntax-in-buffer) ;Clear previous highlighting
  280. (if syntax
  281. (let (o)
  282. (while syntax
  283. (setq o (semantic-make-overlay (semantic-lex-token-start (car syntax))
  284. (semantic-lex-token-end (car syntax))))
  285. (semantic-overlay-put o 'semantic 'unmatched)
  286. (semantic-overlay-put o 'face 'semantic-unmatched-syntax-face)
  287. (setq syntax (cdr syntax))))
  288. ))
  289. (defun semantic-next-unmatched-syntax (point &optional bound)
  290. "Find the next overlay for unmatched syntax after POINT.
  291. Do not search past BOUND if non-nil."
  292. (save-excursion
  293. (goto-char point)
  294. (let ((os point) (ol nil))
  295. (while (and os (< os (or bound (point-max))) (not ol))
  296. (setq os (semantic-overlay-next-change os))
  297. (when os
  298. ;; Get overlays at position
  299. (setq ol (semantic-overlays-at os))
  300. ;; find the overlay that belongs to semantic
  301. ;; and starts at the found position.
  302. (while (and ol (listp ol))
  303. (and (semantic-unmatched-syntax-overlay-p (car ol))
  304. (setq ol (car ol)))
  305. (if (listp ol)
  306. (setq ol (cdr ol))))))
  307. ol)))
  308. (defvar semantic-show-unmatched-syntax-mode-map
  309. (let ((km (make-sparse-keymap)))
  310. (define-key km "\C-c,`" 'semantic-show-unmatched-syntax-next)
  311. km)
  312. "Keymap for command `semantic-show-unmatched-syntax-mode'.")
  313. ;;;###autoload
  314. (define-minor-mode semantic-show-unmatched-syntax-mode
  315. "Minor mode to highlight unmatched lexical syntax tokens.
  316. When a parser executes, some elements in the buffer may not match any
  317. parser rules. These text characters are considered unmatched syntax.
  318. Often time, the display of unmatched syntax can expose coding
  319. problems before the compiler is run.
  320. With prefix argument ARG, turn on if positive, otherwise off. The
  321. minor mode can be turned on only if semantic feature is available and
  322. the current buffer was set up for parsing. Return non-nil if the
  323. minor mode is enabled.
  324. \\{semantic-show-unmatched-syntax-mode-map}"
  325. :keymap semantic-show-unmatched-syntax-mode-map
  326. (if semantic-show-unmatched-syntax-mode
  327. (if (not (and (featurep 'semantic) (semantic-active-p)))
  328. (progn
  329. ;; Disable minor mode if semantic stuff not available
  330. (setq semantic-show-unmatched-syntax-mode nil)
  331. (error "Buffer %s was not set up for parsing"
  332. (buffer-name)))
  333. ;; Add hooks
  334. (semantic-make-local-hook 'semantic-unmatched-syntax-hook)
  335. (add-hook 'semantic-unmatched-syntax-hook
  336. 'semantic-show-unmatched-syntax nil t)
  337. (semantic-make-local-hook 'semantic-pre-clean-token-hooks)
  338. (add-hook 'semantic-pre-clean-token-hooks
  339. 'semantic-clean-token-of-unmatched-syntax nil t)
  340. ;; Show unmatched syntax elements
  341. (if (not (semantic--umatched-syntax-needs-refresh-p))
  342. (semantic-show-unmatched-syntax
  343. (semantic-unmatched-syntax-tokens))))
  344. ;; Remove hooks
  345. (remove-hook 'semantic-unmatched-syntax-hook
  346. 'semantic-show-unmatched-syntax t)
  347. (remove-hook 'semantic-pre-clean-token-hooks
  348. 'semantic-clean-token-of-unmatched-syntax t)
  349. ;; Cleanup unmatched-syntax highlighting
  350. (semantic-clean-unmatched-syntax-in-buffer)))
  351. (semantic-add-minor-mode 'semantic-show-unmatched-syntax-mode
  352. "u")
  353. (defun semantic-show-unmatched-syntax-next ()
  354. "Move forward to the next occurrence of unmatched syntax."
  355. (interactive)
  356. (let ((o (semantic-next-unmatched-syntax (point))))
  357. (if o
  358. (goto-char (semantic-overlay-start o)))))
  359. ;;;;
  360. ;;;; Minor mode to display the parser state in the modeline.
  361. ;;;;
  362. ;;;###autoload
  363. (define-minor-mode global-semantic-show-parser-state-mode
  364. "Toggle global use of option `semantic-show-parser-state-mode'.
  365. If ARG is positive or nil, enable, if it is negative, disable."
  366. :global t :group 'semantic
  367. ;; Not needed because it's autoloaded instead.
  368. ;; :require 'semantic/util-modes
  369. (semantic-toggle-minor-mode-globally
  370. 'semantic-show-parser-state-mode
  371. (if global-semantic-show-parser-state-mode 1 -1)))
  372. (defcustom semantic-show-parser-state-mode-hook nil
  373. "Hook run at the end of function `semantic-show-parser-state-mode'."
  374. :group 'semantic
  375. :type 'hook)
  376. (defvar semantic-show-parser-state-mode-map
  377. (let ((km (make-sparse-keymap)))
  378. km)
  379. "Keymap for show-parser-state minor mode.")
  380. ;;;###autoload
  381. (define-minor-mode semantic-show-parser-state-mode
  382. "Minor mode for displaying parser cache state in the modeline.
  383. The cache can be in one of three states. They are
  384. Up to date, Partial reparse needed, and Full reparse needed.
  385. The state is indicated in the modeline with the following characters:
  386. `-' -> The cache is up to date.
  387. `!' -> The cache requires a full update.
  388. `~' -> The cache needs to be incrementally parsed.
  389. `%' -> The cache is not currently parsable.
  390. `@' -> Auto-parse in progress (not set here.)
  391. With prefix argument ARG, turn on if positive, otherwise off. The
  392. minor mode can be turned on only if semantic feature is available and
  393. the current buffer was set up for parsing. Return non-nil if the
  394. minor mode is enabled."
  395. :keymap semantic-show-parser-state-mode-map
  396. (if semantic-show-parser-state-mode
  397. (if (not (and (featurep 'semantic) (semantic-active-p)))
  398. (progn
  399. ;; Disable minor mode if semantic stuff not available
  400. (setq semantic-show-parser-state-mode nil)
  401. (error "Buffer %s was not set up for parsing"
  402. (buffer-name)))
  403. ;; Set up mode line
  404. (when (not
  405. (memq 'semantic-show-parser-state-string mode-line-modified))
  406. (setq mode-line-modified
  407. (append mode-line-modified
  408. '(semantic-show-parser-state-string))))
  409. ;; Add hooks
  410. (semantic-make-local-hook 'semantic-edits-new-change-hooks)
  411. (add-hook 'semantic-edits-new-change-hooks
  412. 'semantic-show-parser-state-marker nil t)
  413. (semantic-make-local-hook 'semantic-edits-incremental-reparse-failed-hook)
  414. (add-hook 'semantic-edits-incremental-reparse-failed-hook
  415. 'semantic-show-parser-state-marker nil t)
  416. (semantic-make-local-hook 'semantic-after-partial-cache-change-hook)
  417. (add-hook 'semantic-after-partial-cache-change-hook
  418. 'semantic-show-parser-state-marker nil t)
  419. (semantic-make-local-hook 'semantic-after-toplevel-cache-change-hook)
  420. (add-hook 'semantic-after-toplevel-cache-change-hook
  421. 'semantic-show-parser-state-marker nil t)
  422. (semantic-show-parser-state-marker)
  423. (semantic-make-local-hook 'semantic-before-auto-parse-hooks)
  424. (add-hook 'semantic-before-auto-parse-hooks
  425. 'semantic-show-parser-state-auto-marker nil t)
  426. (semantic-make-local-hook 'semantic-after-auto-parse-hooks)
  427. (add-hook 'semantic-after-auto-parse-hooks
  428. 'semantic-show-parser-state-marker nil t)
  429. (semantic-make-local-hook 'semantic-before-idle-scheduler-reparse-hook)
  430. (add-hook 'semantic-before-idle-scheduler-reparse-hook
  431. 'semantic-show-parser-state-auto-marker nil t)
  432. (semantic-make-local-hook 'semantic-after-idle-scheduler-reparse-hook)
  433. (add-hook 'semantic-after-idle-scheduler-reparse-hook
  434. 'semantic-show-parser-state-marker nil t))
  435. ;; Remove parts of mode line
  436. (setq mode-line-modified
  437. (delq 'semantic-show-parser-state-string mode-line-modified))
  438. ;; Remove hooks
  439. (remove-hook 'semantic-edits-new-change-hooks
  440. 'semantic-show-parser-state-marker t)
  441. (remove-hook 'semantic-edits-incremental-reparse-failed-hook
  442. 'semantic-show-parser-state-marker t)
  443. (remove-hook 'semantic-after-partial-cache-change-hook
  444. 'semantic-show-parser-state-marker t)
  445. (remove-hook 'semantic-after-toplevel-cache-change-hook
  446. 'semantic-show-parser-state-marker t)
  447. (remove-hook 'semantic-before-auto-parse-hooks
  448. 'semantic-show-parser-state-auto-marker t)
  449. (remove-hook 'semantic-after-auto-parse-hooks
  450. 'semantic-show-parser-state-marker t)
  451. (remove-hook 'semantic-before-idle-scheduler-reparse-hook
  452. 'semantic-show-parser-state-auto-marker t)
  453. (remove-hook 'semantic-after-idle-scheduler-reparse-hook
  454. 'semantic-show-parser-state-marker t)))
  455. (semantic-add-minor-mode 'semantic-show-parser-state-mode
  456. "")
  457. (defvar semantic-show-parser-state-string nil
  458. "String showing the parser state for this buffer.
  459. See `semantic-show-parser-state-marker' for details.")
  460. (make-variable-buffer-local 'semantic-show-parser-state-string)
  461. (defun semantic-show-parser-state-marker (&rest ignore)
  462. "Set `semantic-show-parser-state-string' to indicate parser state.
  463. This marker is one of the following:
  464. `-' -> The cache is up to date.
  465. `!' -> The cache requires a full update.
  466. `~' -> The cache needs to be incrementally parsed.
  467. `%' -> The cache is not currently parsable.
  468. `@' -> Auto-parse in progress (not set here.)
  469. Arguments IGNORE are ignored, and accepted so this can be used as a hook
  470. in many situations."
  471. (setq semantic-show-parser-state-string
  472. (cond ((semantic-parse-tree-needs-rebuild-p)
  473. "!")
  474. ((semantic-parse-tree-needs-update-p)
  475. "^")
  476. ((semantic-parse-tree-unparseable-p)
  477. "%")
  478. (t
  479. "-")))
  480. ;;(message "Setup mode line indicator to [%s]" semantic-show-parser-state-string)
  481. )
  482. (defun semantic-show-parser-state-auto-marker ()
  483. "Hook function run before an autoparse.
  484. Set up `semantic-show-parser-state-marker' to show `@'
  485. to indicate a parse in progress."
  486. (unless (semantic-parse-tree-up-to-date-p)
  487. (setq semantic-show-parser-state-string "@")
  488. ;; For testing.
  489. ;;(sit-for 1)
  490. ))
  491. ;;;;
  492. ;;;; Minor mode to make function decls sticky.
  493. ;;;;
  494. ;;;###autoload
  495. (define-minor-mode global-semantic-stickyfunc-mode
  496. "Toggle global use of option `semantic-stickyfunc-mode'.
  497. If ARG is positive or nil, enable, if it is negative, disable."
  498. :global t :group 'semantic :group 'semantic-modes
  499. ;; Not needed because it's autoloaded instead.
  500. ;; :require 'semantic/util-modes
  501. (semantic-toggle-minor-mode-globally
  502. 'semantic-stickyfunc-mode (if global-semantic-stickyfunc-mode 1 -1)))
  503. (defcustom semantic-stickyfunc-mode-hook nil
  504. "Hook run at the end of function `semantic-stickyfunc-mode'."
  505. :group 'semantic
  506. :type 'hook)
  507. (defvar semantic-stickyfunc-mode-map
  508. (let ((km (make-sparse-keymap)))
  509. (define-key km [ header-line down-mouse-1 ] 'semantic-stickyfunc-menu)
  510. km)
  511. "Keymap for stickyfunc minor mode.")
  512. (defvar semantic-stickyfunc-popup-menu nil
  513. "Menu used if the user clicks on the header line used by stickyfunc mode.")
  514. (easy-menu-define
  515. semantic-stickyfunc-popup-menu
  516. semantic-stickyfunc-mode-map
  517. "Stickyfunc Menu"
  518. '("Stickyfunc Mode" :visible (progn nil)
  519. [ "Copy Headerline Tag" senator-copy-tag
  520. :active (semantic-current-tag)
  521. :help "Copy the current tag to the tag ring"]
  522. [ "Kill Headerline Tag" senator-kill-tag
  523. :active (semantic-current-tag)
  524. :help "Kill tag text to the kill ring, and copy the tag to the tag ring"
  525. ]
  526. [ "Copy Headerline Tag to Register" senator-copy-tag-to-register
  527. :active (semantic-current-tag)
  528. :help "Copy the current tag to a register"
  529. ]
  530. [ "Narrow To Headerline Tag" senator-narrow-to-defun
  531. :active (semantic-current-tag)
  532. :help "Narrow to the bounds of the current tag"]
  533. [ "Fold Headerline Tag" senator-fold-tag-toggle
  534. :active (semantic-current-tag)
  535. :style toggle
  536. :selected (let ((tag (semantic-current-tag)))
  537. (and tag (semantic-tag-folded-p tag)))
  538. :help "Fold the current tag to one line"
  539. ]
  540. "---"
  541. [ "About This Header Line"
  542. (lambda () (interactive)
  543. (describe-function 'semantic-stickyfunc-mode)) t])
  544. )
  545. (defcustom semantic-stickyfunc-indent-string
  546. (if (and window-system (not (featurep 'xemacs)))
  547. (concat
  548. (condition-case nil
  549. ;; Test scroll bar location
  550. (let ((charwidth (frame-char-width))
  551. (scrollpos (frame-parameter (selected-frame)
  552. 'vertical-scroll-bars))
  553. )
  554. (if (or (eq scrollpos 'left)
  555. ;; Now wait a minute. If you turn scroll-bar-mode
  556. ;; on, then off, the new value is t, not left.
  557. ;; Will this mess up older emacs where the default
  558. ;; was on the right? I don't think so since they don't
  559. ;; support a header line.
  560. (eq scrollpos t))
  561. (let ((w (when (boundp 'scroll-bar-width)
  562. (symbol-value 'scroll-bar-width))))
  563. (if (not w)
  564. (setq w (frame-parameter (selected-frame)
  565. 'scroll-bar-width)))
  566. ;; in 21.2, the frame parameter is sometimes empty
  567. ;; so we need to get the value here.
  568. (if (not w)
  569. (setq w (+ (get 'scroll-bar-width 'x-frame-parameter)
  570. ;; In 21.4, or perhaps 22.1 the x-frame
  571. ;; parameter is different from the frame
  572. ;; parameter by only 1 pixel.
  573. 1)))
  574. (if (not w)
  575. " "
  576. (setq w (+ 2 w)) ; Some sort of border around
  577. ; the scrollbar.
  578. (make-string (/ w charwidth) ? )))
  579. ""))
  580. (error ""))
  581. (condition-case nil
  582. ;; Test fringe size.
  583. (let* ((f (window-fringes))
  584. (fw (car f))
  585. (numspace (/ fw (frame-char-width)))
  586. )
  587. (make-string numspace ? ))
  588. (error
  589. ;; Well, the fancy new Emacs functions failed. Try older
  590. ;; tricks.
  591. (condition-case nil
  592. ;; I'm not so sure what's up with the 21.1-21.3 fringe.
  593. ;; It looks to be about 1 space wide.
  594. (if (get 'fringe 'face)
  595. " "
  596. "")
  597. (error ""))))
  598. )
  599. ;; Not Emacs or a window system means no scrollbar or fringe,
  600. ;; and perhaps not even a header line to worry about.
  601. "")
  602. "String used to indent the stickyfunc header.
  603. Customize this string to match the space used by scrollbars and
  604. fringe so it does not appear that the code is moving left/right
  605. when it lands in the sticky line."
  606. :group 'semantic
  607. :type 'string)
  608. (defvar semantic-stickyfunc-old-hlf nil
  609. "Value of the header line when entering stickyfunc mode.")
  610. (defconst semantic-stickyfunc-header-line-format
  611. (cond ((featurep 'xemacs)
  612. nil)
  613. ((>= emacs-major-version 22)
  614. '(:eval (list
  615. ;; Magic bit I found on emacswiki.
  616. (propertize " " 'display '((space :align-to 0)))
  617. (semantic-stickyfunc-fetch-stickyline))))
  618. ((= emacs-major-version 21)
  619. '(:eval (list semantic-stickyfunc-indent-string
  620. (semantic-stickyfunc-fetch-stickyline))))
  621. (t nil))
  622. "The header line format used by stickyfunc mode.")
  623. ;;;###autoload
  624. (define-minor-mode semantic-stickyfunc-mode
  625. "Minor mode to show the title of a tag in the header line.
  626. Enables/disables making the header line of functions sticky.
  627. A function (or other tag class specified by
  628. `semantic-stickyfunc-sticky-classes') has a header line, meaning the
  629. first line which describes the rest of the construct. This first
  630. line is what is displayed in the header line.
  631. With prefix argument ARG, turn on if positive, otherwise off. The
  632. minor mode can be turned on only if semantic feature is available and
  633. the current buffer was set up for parsing. Return non-nil if the
  634. minor mode is enabled."
  635. ;; Don't need indicator. It's quite visible
  636. :keymap semantic-stickyfunc-mode-map
  637. (if semantic-stickyfunc-mode
  638. (progn
  639. (unless (and (featurep 'semantic) (semantic-active-p))
  640. ;; Disable minor mode if semantic stuff not available
  641. (setq semantic-stickyfunc-mode nil)
  642. (error "Buffer %s was not set up for parsing" (buffer-name)))
  643. (unless (boundp 'default-header-line-format)
  644. ;; Disable if there are no header lines to use.
  645. (setq semantic-stickyfunc-mode nil)
  646. (error "Sticky Function mode requires Emacs 21"))
  647. ;; Enable the mode
  648. ;; Save previous buffer local value of header line format.
  649. (when (and (local-variable-p 'header-line-format (current-buffer))
  650. (not (eq header-line-format
  651. semantic-stickyfunc-header-line-format)))
  652. (set (make-local-variable 'semantic-stickyfunc-old-hlf)
  653. header-line-format))
  654. (setq header-line-format semantic-stickyfunc-header-line-format))
  655. ;; Disable sticky func mode
  656. ;; Restore previous buffer local value of header line format if
  657. ;; the current one is the sticky func one.
  658. (when (eq header-line-format semantic-stickyfunc-header-line-format)
  659. (kill-local-variable 'header-line-format)
  660. (when (local-variable-p 'semantic-stickyfunc-old-hlf (current-buffer))
  661. (setq header-line-format semantic-stickyfunc-old-hlf)
  662. (kill-local-variable 'semantic-stickyfunc-old-hlf)))))
  663. (defvar semantic-stickyfunc-sticky-classes
  664. '(function type)
  665. "List of tag classes which stickyfunc will display in the header line.")
  666. (make-variable-buffer-local 'semantic-stickyfunc-sticky-classes)
  667. (defcustom semantic-stickyfunc-show-only-functions-p nil
  668. "Non-nil means don't show lines that aren't part of a tag.
  669. If this is nil, then comments or other text between tags that is
  670. 1 line above the top of the current window will be shown."
  671. :group 'semantic
  672. :type 'boolean)
  673. (defun semantic-stickyfunc-tag-to-stick ()
  674. "Return the tag to stick at the current point."
  675. (let ((tags (nreverse (semantic-find-tag-by-overlay (point)))))
  676. ;; Get rid of non-matching tags.
  677. (while (and tags
  678. (not (member
  679. (semantic-tag-class (car tags))
  680. semantic-stickyfunc-sticky-classes))
  681. )
  682. (setq tags (cdr tags)))
  683. (car tags)))
  684. (defun semantic-stickyfunc-fetch-stickyline ()
  685. "Make the function at the top of the current window sticky.
  686. Capture its function declaration, and place it in the header line.
  687. If there is no function, disable the header line."
  688. (save-excursion
  689. (goto-char (window-start (selected-window)))
  690. (let* ((noshow (bobp))
  691. (str
  692. (progn
  693. (forward-line -1)
  694. (end-of-line)
  695. ;; Capture this function
  696. (let* ((tag (semantic-stickyfunc-tag-to-stick)))
  697. ;; TAG is nil if there was nothing of the appropriate type there.
  698. (if (not tag)
  699. ;; Set it to be the text under the header line
  700. (if noshow
  701. ""
  702. (if semantic-stickyfunc-show-only-functions-p ""
  703. (buffer-substring (point-at-bol) (point-at-eol))
  704. ))
  705. ;; Go get the first line of this tag.
  706. (goto-char (semantic-tag-start tag))
  707. ;; Klaus Berndl <klaus.berndl@sdm.de>:
  708. ;; goto the tag name; this is especially needed for languages
  709. ;; like c++ where a often used style is like:
  710. ;; void
  711. ;; ClassX::methodM(arg1...)
  712. ;; {
  713. ;; ...
  714. ;; }
  715. ;; Without going to the tag-name we would get"void" in the
  716. ;; header line which is IMHO not really useful
  717. (search-forward (semantic-tag-name tag) nil t)
  718. (buffer-substring (point-at-bol) (point-at-eol))
  719. ))))
  720. (start 0))
  721. (while (string-match "%" str start)
  722. (setq str (replace-match "%%" t t str 0)
  723. start (1+ (match-end 0)))
  724. )
  725. ;; In 21.4 (or 22.1) the header doesn't expand tabs. Hmmmm.
  726. ;; We should replace them here.
  727. ;;
  728. ;; This hack assumes that tabs are kept smartly at tab boundaries
  729. ;; instead of in a tab boundary where it might only represent 4 spaces.
  730. (while (string-match "\t" str start)
  731. (setq str (replace-match " " t t str 0)))
  732. str)))
  733. (defun semantic-stickyfunc-menu (event)
  734. "Popup a menu that can help a user understand stickyfunc-mode.
  735. Argument EVENT describes the event that caused this function to be called."
  736. (interactive "e")
  737. (let* ((startwin (selected-window))
  738. (win (car (car (cdr event))))
  739. )
  740. (select-window win t)
  741. (save-excursion
  742. (goto-char (window-start win))
  743. (sit-for 0)
  744. (popup-menu semantic-stickyfunc-popup-menu event)
  745. )
  746. (select-window startwin)))
  747. (semantic-add-minor-mode 'semantic-stickyfunc-mode
  748. "") ;; Don't need indicator. It's quite visible
  749. ;;;;
  750. ;;;; Minor mode to make highlight the current function
  751. ;;;;
  752. ;; Highlight the first like of the function we are in if it is different
  753. ;; from the tag going off the top of the screen.
  754. ;;;###autoload
  755. (define-minor-mode global-semantic-highlight-func-mode
  756. "Toggle global use of option `semantic-highlight-func-mode'.
  757. If ARG is positive or nil, enable, if it is negative, disable."
  758. :global t :group 'semantic :group 'semantic-modes
  759. ;; Not needed because it's autoloaded instead.
  760. ;; :require 'semantic/util-modes
  761. (semantic-toggle-minor-mode-globally
  762. 'semantic-highlight-func-mode
  763. (if global-semantic-highlight-func-mode 1 -1)))
  764. (defcustom semantic-highlight-func-mode-hook nil
  765. "Hook run at the end of function `semantic-highlight-func-mode'."
  766. :group 'semantic
  767. :type 'hook)
  768. (defvar semantic-highlight-func-mode-map
  769. (let ((km (make-sparse-keymap))
  770. (m3 (if (featurep 'xemacs) [ button3 ] [ mouse-3 ]))
  771. )
  772. (define-key km m3 'semantic-highlight-func-menu)
  773. km)
  774. "Keymap for highlight-func minor mode.")
  775. (defvar semantic-highlight-func-popup-menu nil
  776. "Menu used if the user clicks on the header line used by `semantic-highlight-func-mode'.")
  777. (easy-menu-define
  778. semantic-highlight-func-popup-menu
  779. semantic-highlight-func-mode-map
  780. "Highlight-Func Menu"
  781. '("Highlight-Func Mode" :visible (progn nil)
  782. [ "Copy Tag" senator-copy-tag
  783. :active (semantic-current-tag)
  784. :help "Copy the current tag to the tag ring"]
  785. [ "Kill Tag" senator-kill-tag
  786. :active (semantic-current-tag)
  787. :help "Kill tag text to the kill ring, and copy the tag to the tag ring"
  788. ]
  789. [ "Copy Tag to Register" senator-copy-tag-to-register
  790. :active (semantic-current-tag)
  791. :help "Copy the current tag to a register"
  792. ]
  793. [ "Narrow To Tag" senator-narrow-to-defun
  794. :active (semantic-current-tag)
  795. :help "Narrow to the bounds of the current tag"]
  796. [ "Fold Tag" senator-fold-tag-toggle
  797. :active (semantic-current-tag)
  798. :style toggle
  799. :selected (let ((tag (semantic-stickyfunc-tag-to-stick)))
  800. (and tag (semantic-tag-folded-p tag)))
  801. :help "Fold the current tag to one line"
  802. ]
  803. "---"
  804. [ "About This Tag" semantic-describe-tag t])
  805. )
  806. (defun semantic-highlight-func-menu (event)
  807. "Popup a menu that displays things to do to the current tag.
  808. Argument EVENT describes the event that caused this function to be called."
  809. (interactive "e")
  810. (let* ((startwin (selected-window))
  811. (win (semantic-event-window event))
  812. )
  813. (select-window win t)
  814. (save-excursion
  815. ;(goto-char (window-start win))
  816. (mouse-set-point event)
  817. (sit-for 0)
  818. (semantic-popup-menu semantic-highlight-func-popup-menu)
  819. )
  820. (select-window startwin)))
  821. (defvar semantic-highlight-func-ct-overlay nil
  822. "Overlay used to highlight the tag the cursor is in.")
  823. (make-variable-buffer-local 'semantic-highlight-func-ct-overlay)
  824. (defface semantic-highlight-func-current-tag-face
  825. '((((class color) (background dark))
  826. ;; Put this back to something closer to black later.
  827. (:background "gray20"))
  828. (((class color) (background light))
  829. (:background "gray90")))
  830. "Face used to show the top of current function."
  831. :group 'semantic-faces)
  832. ;;;###autoload
  833. (define-minor-mode semantic-highlight-func-mode
  834. "Minor mode to highlight the first line of the current tag.
  835. Enables/disables making the current function's first line light up.
  836. A function (or other tag class specified by
  837. `semantic-stickyfunc-sticky-classes') is highlighted, meaning the
  838. first line which describes the rest of the construct.
  839. See `semantic-stickyfunc-mode' for putting a function in the
  840. header line. This mode recycles the stickyfunc configuration
  841. classes list.
  842. With prefix argument ARG, turn on if positive, otherwise off. The
  843. minor mode can be turned on only if semantic feature is available and
  844. the current buffer was set up for parsing. Return non-nil if the
  845. minor mode is enabled."
  846. :lighter nil ;; Don't need indicator. It's quite visible.
  847. (if semantic-highlight-func-mode
  848. (progn
  849. (unless (and (featurep 'semantic) (semantic-active-p))
  850. ;; Disable minor mode if semantic stuff not available
  851. (setq semantic-highlight-func-mode nil)
  852. (error "Buffer %s was not set up for parsing" (buffer-name)))
  853. ;; Setup our hook
  854. (add-hook 'post-command-hook
  855. 'semantic-highlight-func-highlight-current-tag nil t))
  856. ;; Disable highlight func mode
  857. (remove-hook 'post-command-hook
  858. 'semantic-highlight-func-highlight-current-tag t)
  859. (semantic-highlight-func-highlight-current-tag t)))
  860. (defun semantic-highlight-func-highlight-current-tag (&optional disable)
  861. "Highlight the current tag under point.
  862. Optional argument DISABLE will turn off any active highlight.
  863. If the current tag for this buffer is different from the last time this
  864. function was called, move the overlay."
  865. (when (and (not (minibufferp))
  866. (or (not semantic-highlight-func-ct-overlay)
  867. (eq (semantic-overlay-buffer
  868. semantic-highlight-func-ct-overlay)
  869. (current-buffer))))
  870. (let* ((tag (semantic-stickyfunc-tag-to-stick))
  871. (ol semantic-highlight-func-ct-overlay))
  872. (when (not ol)
  873. ;; No overlay in this buffer. Make one.
  874. (setq ol (semantic-make-overlay (point-min) (point-min)
  875. (current-buffer) t nil))
  876. (semantic-overlay-put ol 'highlight-func t)
  877. (semantic-overlay-put ol 'face 'semantic-highlight-func-current-tag-face)
  878. (semantic-overlay-put ol 'keymap semantic-highlight-func-mode-map)
  879. (semantic-overlay-put ol 'help-echo
  880. "Current Function : mouse-3 - Context menu")
  881. (setq semantic-highlight-func-ct-overlay ol)
  882. )
  883. ;; TAG is nil if there was nothing of the appropriate type there.
  884. (if (or (not tag) disable)
  885. ;; No tag, make the overlay go away.
  886. (progn
  887. (semantic-overlay-put ol 'tag nil)
  888. (semantic-overlay-move ol (point-min) (point-min) (current-buffer))
  889. )
  890. ;; We have a tag, if it is the same, do nothing.
  891. (unless (eq (semantic-overlay-get ol 'tag) tag)
  892. (save-excursion
  893. (goto-char (semantic-tag-start tag))
  894. (search-forward (semantic-tag-name tag) nil t)
  895. (semantic-overlay-put ol 'tag tag)
  896. (semantic-overlay-move ol (point-at-bol) (point-at-eol))
  897. )
  898. )
  899. )))
  900. nil)
  901. (semantic-add-minor-mode 'semantic-highlight-func-mode
  902. "") ;; Don't need indicator. It's quite visible
  903. (provide 'semantic/util-modes)
  904. ;; Local variables:
  905. ;; generated-autoload-file: "loaddefs.el"
  906. ;; generated-autoload-load-name: "semantic/util-modes"
  907. ;; End:
  908. ;;; semantic/util-modes.el ends here