mode.el 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. ;;; semantic/decorate/mode.el --- Minor mode for decorating tags
  2. ;; Copyright (C) 2000-2005, 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  4. ;; Keywords: syntax
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;; A minor mode for use in decorating tags.
  19. ;;
  20. ;; There are two types of decorations that can be performed on a tag.
  21. ;; You can either highlight the full tag, or you can add an
  22. ;; independent decoration on some part of the tag body.
  23. ;;
  24. ;; For independent decoration in particular, managing them so that they
  25. ;; do not get corrupted is challenging. This major mode and
  26. ;; corresponding macros will make handling those types of decorations
  27. ;; easier.
  28. ;;
  29. ;;; Code:
  30. (eval-when-compile (require 'cl))
  31. (require 'semantic)
  32. (require 'semantic/decorate)
  33. (require 'semantic/tag-ls)
  34. (require 'semantic/util-modes)
  35. ;;; Styles List
  36. ;;
  37. (defcustom semantic-decoration-styles nil
  38. "List of active decoration styles.
  39. It is an alist of \(NAME . FLAG) elements, where NAME is a style name
  40. and FLAG is non-nil if the style is enabled.
  41. See also `define-semantic-decoration-style' which will automatically
  42. add items to this list."
  43. :group 'semantic
  44. :type '(repeat (cons (string :tag "Decoration Name")
  45. (boolean :tag "Enabled")))
  46. )
  47. ;;; Misc.
  48. ;;
  49. (defsubst semantic-decorate-style-predicate (style)
  50. "Return the STYLE's predicate function."
  51. (intern (format "%s-p" style)))
  52. (defsubst semantic-decorate-style-highlighter (style)
  53. "Return the STYLE's highlighter function."
  54. (intern (format "%s-highlight" style)))
  55. ;;; Base decoration API
  56. ;;
  57. (defsubst semantic-decoration-p (object)
  58. "Return non-nil if OBJECT is a tag decoration."
  59. (and (semantic-overlay-p object)
  60. (semantic-overlay-get object 'semantic-decoration)))
  61. (defsubst semantic-decoration-set-property (deco property value)
  62. "Set the DECO decoration's PROPERTY to VALUE.
  63. Return DECO."
  64. (assert (semantic-decoration-p deco))
  65. (semantic-overlay-put deco property value)
  66. deco)
  67. (defsubst semantic-decoration-get-property (deco property)
  68. "Return the DECO decoration's PROPERTY value."
  69. (assert (semantic-decoration-p deco))
  70. (semantic-overlay-get deco property))
  71. (defsubst semantic-decoration-set-face (deco face)
  72. "Set the face of the decoration DECO to FACE.
  73. Return DECO."
  74. (semantic-decoration-set-property deco 'face face))
  75. (defsubst semantic-decoration-face (deco)
  76. "Return the face of the decoration DECO."
  77. (semantic-decoration-get-property deco 'face))
  78. (defsubst semantic-decoration-set-priority (deco priority)
  79. "Set the priority of the decoration DECO to PRIORITY.
  80. Return DECO."
  81. (assert (natnump priority))
  82. (semantic-decoration-set-property deco 'priority priority))
  83. (defsubst semantic-decoration-priority (deco)
  84. "Return the priority of the decoration DECO."
  85. (semantic-decoration-get-property deco 'priority))
  86. (defsubst semantic-decoration-move (deco begin end)
  87. "Move the decoration DECO on the region between BEGIN and END.
  88. Return DECO."
  89. (assert (semantic-decoration-p deco))
  90. (semantic-overlay-move deco begin end)
  91. deco)
  92. ;;; Tag decoration
  93. ;;
  94. (defun semantic-decorate-tag (tag begin end &optional face)
  95. "Add a new decoration on TAG on the region between BEGIN and END.
  96. If optional argument FACE is non-nil, set the decoration's face to
  97. FACE.
  98. Return the overlay that makes up the new decoration."
  99. (let ((deco (semantic-tag-create-secondary-overlay tag)))
  100. ;; We do not use the unlink property because we do not want to
  101. ;; save the highlighting information in the DB.
  102. (semantic-overlay-put deco 'semantic-decoration t)
  103. (semantic-decoration-move deco begin end)
  104. (semantic-decoration-set-face deco face)
  105. deco))
  106. (defun semantic-decorate-clear-tag (tag &optional deco)
  107. "Remove decorations from TAG.
  108. If optional argument DECO is non-nil, remove only that decoration."
  109. (assert (or (null deco) (semantic-decoration-p deco)))
  110. ;; Clear primary decorations.
  111. ;; For now, just unhighlight the tag. How to deal with other
  112. ;; primary decorations like invisibility, etc. ? Maybe just
  113. ;; restoring default values will suffice?
  114. (semantic-unhighlight-tag tag)
  115. (semantic-tag-delete-secondary-overlay
  116. tag (or deco 'semantic-decoration)))
  117. (defun semantic-decorate-tag-decoration (tag)
  118. "Return decoration found on TAG."
  119. (semantic-tag-get-secondary-overlay tag 'semantic-decoration))
  120. ;;; Global setup of active decorations
  121. ;;
  122. (defun semantic-decorate-flush-decorations (&optional buffer)
  123. "Flush decorations found in BUFFER.
  124. BUFFER defaults to the current buffer.
  125. Should be used to flush decorations that might remain in BUFFER, for
  126. example, after tags have been refreshed."
  127. (with-current-buffer (or buffer (current-buffer))
  128. (dolist (o (semantic-overlays-in (point-min) (point-max)))
  129. (and (semantic-decoration-p o)
  130. (semantic-overlay-delete o)))))
  131. (defun semantic-decorate-clear-decorations (tag-list)
  132. "Remove decorations found in tags in TAG-LIST."
  133. (dolist (tag tag-list)
  134. (semantic-decorate-clear-tag tag)
  135. ;; recurse over children
  136. (semantic-decorate-clear-decorations
  137. (semantic-tag-components-with-overlays tag))))
  138. (defun semantic-decorate-add-decorations (tag-list)
  139. "Add decorations to tags in TAG-LIST.
  140. Also make sure old decorations in the area are completely flushed."
  141. (dolist (tag tag-list)
  142. ;; Cleanup old decorations.
  143. (when (semantic-decorate-tag-decoration tag)
  144. ;; Note on below comment. This happens more as decorations are refreshed
  145. ;; mid-way through their use. Remove the message.
  146. ;; It would be nice if this never happened, but it still does
  147. ;; once in a while. Print a message to help flush these
  148. ;; situations
  149. ;;(message "Decorations still on %s" (semantic-format-tag-name tag))
  150. (semantic-decorate-clear-tag tag))
  151. ;; Add new decorations.
  152. (dolist (style semantic-decoration-styles)
  153. (let ((pred (semantic-decorate-style-predicate (car style)))
  154. (high (semantic-decorate-style-highlighter (car style))))
  155. (and (cdr style)
  156. (fboundp pred)
  157. (funcall pred tag)
  158. (fboundp high)
  159. (funcall high tag))))
  160. ;; Recurse on the children of all tags
  161. (semantic-decorate-add-decorations
  162. (semantic-tag-components-with-overlays tag))))
  163. ;;; PENDING DECORATIONS
  164. ;;
  165. ;; Activities in Emacs may cause a decoration to change state. Any
  166. ;; such identified change ought to be setup as PENDING. This means
  167. ;; that the next idle step will do the decoration change, but at the
  168. ;; time of the state change, minimal work would be done.
  169. (defvar semantic-decorate-pending-decoration-hook nil
  170. "Normal hook run to perform pending decoration changes.")
  171. (semantic-varalias-obsolete 'semantic-decorate-pending-decoration-hooks
  172. 'semantic-decorate-pending-decoration-hook "23.2")
  173. (defun semantic-decorate-add-pending-decoration (fcn &optional buffer)
  174. "Add a pending decoration change represented by FCN.
  175. Applies only to the current BUFFER.
  176. The setting of FCN will be removed after it is run."
  177. (save-excursion
  178. (when buffer (set-buffer buffer))
  179. (semantic-make-local-hook 'semantic-decorate-flush-pending-decorations)
  180. (add-hook 'semantic-decorate-pending-decoration-hook fcn nil t)))
  181. (defun semantic-decorate-flush-pending-decorations (&optional buffer)
  182. "Flush any pending decorations for BUFFER.
  183. Flush functions from `semantic-decorate-pending-decoration-hook'."
  184. (save-excursion
  185. (when buffer (set-buffer buffer))
  186. (run-hooks 'semantic-decorate-pending-decoration-hook)
  187. ;; Always reset the hooks
  188. (setq semantic-decorate-pending-decoration-hook nil)))
  189. ;;; DECORATION MODE
  190. ;;
  191. ;; Generic mode for handling basic highlighting and decorations.
  192. ;;
  193. ;;;###autoload
  194. (define-minor-mode global-semantic-decoration-mode
  195. "Toggle global use of option `semantic-decoration-mode'.
  196. Decoration mode turns on all active decorations as specified
  197. by `semantic-decoration-styles'."
  198. :global t :group 'semantic :group 'semantic-modes
  199. ;; Not needed because it's autoloaded instead.
  200. ;; :require 'semantic/decorate/mode
  201. (semantic-toggle-minor-mode-globally
  202. 'semantic-decoration-mode (if global-semantic-decoration-mode 1 -1)))
  203. (defcustom semantic-decoration-mode-hook nil
  204. "Hook run at the end of function `semantic-decoration-mode'."
  205. :group 'semantic
  206. :type 'hook)
  207. (define-minor-mode semantic-decoration-mode
  208. "Minor mode for decorating tags.
  209. Decorations are specified in `semantic-decoration-styles'.
  210. You can define new decoration styles with
  211. `define-semantic-decoration-style'.
  212. With prefix argument ARG, turn on if positive, otherwise off. The
  213. minor mode can be turned on only if semantic feature is available and
  214. the current buffer was set up for parsing. Return non-nil if the
  215. minor mode is enabled."
  216. ;;
  217. ;;\\{semantic-decoration-map}"
  218. nil nil nil
  219. (if semantic-decoration-mode
  220. (if (not (and (featurep 'semantic) (semantic-active-p)))
  221. (progn
  222. ;; Disable minor mode if semantic stuff not available
  223. (setq semantic-decoration-mode nil)
  224. (error "Buffer %s was not set up for parsing"
  225. (buffer-name)))
  226. ;; Add hooks
  227. (semantic-make-local-hook 'semantic-after-partial-cache-change-hook)
  228. (add-hook 'semantic-after-partial-cache-change-hook
  229. 'semantic-decorate-tags-after-partial-reparse nil t)
  230. (semantic-make-local-hook 'semantic-after-toplevel-cache-change-hook)
  231. (add-hook 'semantic-after-toplevel-cache-change-hook
  232. 'semantic-decorate-tags-after-full-reparse nil t)
  233. ;; Add decorations to available tags. The above hooks ensure
  234. ;; that new tags will be decorated when they become available.
  235. (semantic-decorate-add-decorations (semantic-fetch-available-tags)))
  236. ;; Remove decorations from available tags.
  237. (semantic-decorate-clear-decorations (semantic-fetch-available-tags))
  238. ;; Cleanup any leftover crap too.
  239. (semantic-decorate-flush-decorations)
  240. ;; Remove hooks
  241. (remove-hook 'semantic-after-partial-cache-change-hook
  242. 'semantic-decorate-tags-after-partial-reparse t)
  243. (remove-hook 'semantic-after-toplevel-cache-change-hook
  244. 'semantic-decorate-tags-after-full-reparse t)))
  245. (semantic-add-minor-mode 'semantic-decoration-mode
  246. "")
  247. (defun semantic-decorate-tags-after-full-reparse (tag-list)
  248. "Add decorations after a complete reparse of the current buffer.
  249. TAG-LIST is the list of tags recently parsed.
  250. Flush all existing decorations and call `semantic-decorate-add-decorations' to
  251. add decorations.
  252. Called from `semantic-after-toplevel-cache-change-hook'."
  253. ;; Flush everything
  254. (semantic-decorate-flush-decorations)
  255. ;; Add it back on
  256. (semantic-decorate-add-decorations tag-list))
  257. (defun semantic-decorate-tags-after-partial-reparse (tag-list)
  258. "Add decorations when new tags are created in the current buffer.
  259. TAG-LIST is the list of newly created tags.
  260. Call `semantic-decorate-add-decorations' to add decorations.
  261. Called from `semantic-after-partial-cache-change-hook'."
  262. (semantic-decorate-add-decorations tag-list))
  263. ;;; Enable/Disable toggling
  264. ;;
  265. (defun semantic-decoration-style-enabled-p (style)
  266. "Return non-nil if STYLE is currently enabled.
  267. Return nil if the style is disabled, or does not exist."
  268. (let ((pair (assoc style semantic-decoration-styles)))
  269. (and pair (cdr pair))))
  270. (defun semantic-toggle-decoration-style (name &optional arg)
  271. "Turn on/off the decoration style with NAME.
  272. Decorations are specified in `semantic-decoration-styles'.
  273. With prefix argument ARG, turn on if positive, otherwise off.
  274. Return non-nil if the decoration style is enabled."
  275. (interactive
  276. (list (completing-read "Decoration style: "
  277. semantic-decoration-styles nil t)
  278. current-prefix-arg))
  279. (setq name (format "%s" name)) ;; Ensure NAME is a string.
  280. (unless (equal name "")
  281. (let* ((style (assoc name semantic-decoration-styles))
  282. (flag (if arg
  283. (> (prefix-numeric-value arg) 0)
  284. (not (cdr style)))))
  285. (unless (eq (cdr style) flag)
  286. ;; Store the new flag.
  287. (setcdr style flag)
  288. ;; Refresh decorations is `semantic-decoration-mode' is on.
  289. (when semantic-decoration-mode
  290. (semantic-decoration-mode -1)
  291. (semantic-decoration-mode 1))
  292. (when (called-interactively-p 'interactive)
  293. (message "Decoration style %s turned %s" (car style)
  294. (if flag "on" "off"))))
  295. flag)))
  296. (defvar semantic-decoration-menu-cache nil
  297. "Cache of the decoration menu.")
  298. (defun semantic-decoration-build-style-menu (style)
  299. "Build a menu item for controlling a specific decoration STYLE."
  300. (vector (car style)
  301. `(lambda () (interactive)
  302. (semantic-toggle-decoration-style
  303. ,(car style)))
  304. :style 'toggle
  305. :selected `(semantic-decoration-style-enabled-p ,(car style))
  306. ))
  307. (defun semantic-build-decoration-mode-menu (&rest ignore)
  308. "Create a menu listing all the known decorations for toggling.
  309. IGNORE any input arguments."
  310. (or semantic-decoration-menu-cache
  311. (setq semantic-decoration-menu-cache
  312. (mapcar 'semantic-decoration-build-style-menu
  313. (reverse semantic-decoration-styles))
  314. )))
  315. ;;; Defining decoration styles
  316. ;;
  317. (defmacro define-semantic-decoration-style (name doc &rest flags)
  318. "Define a new decoration style with NAME.
  319. DOC is a documentation string describing the decoration style NAME.
  320. It is appended to auto-generated doc strings.
  321. An Optional list of FLAGS can also be specified. Flags are:
  322. :enabled <value> - specify the default enabled value for NAME.
  323. This defines two new overload functions respectively called `NAME-p'
  324. and `NAME-highlight', for which you must provide a default
  325. implementation in respectively the functions `NAME-p-default' and
  326. `NAME-highlight-default'. Those functions are passed a tag. `NAME-p'
  327. must return non-nil to indicate that the tag should be decorated by
  328. `NAME-highlight'.
  329. To put primary decorations on a tag `NAME-highlight' must use
  330. functions like `semantic-set-tag-face', `semantic-set-tag-intangible',
  331. etc., found in the semantic-decorate library.
  332. To add other kind of decorations on a tag, `NAME-highlight' must use
  333. `semantic-decorate-tag', and other functions of the semantic
  334. decoration API found in this library."
  335. (let ((predicate (semantic-decorate-style-predicate name))
  336. (highlighter (semantic-decorate-style-highlighter name))
  337. (defaultenable (if (plist-member flags :enabled)
  338. (plist-get flags :enabled)
  339. t))
  340. )
  341. `(progn
  342. ;; Clear the menu cache so that new items are added when
  343. ;; needed.
  344. (setq semantic-decoration-menu-cache nil)
  345. ;; Create an override method to specify if a given tag belongs
  346. ;; to this type of decoration
  347. (define-overloadable-function ,predicate (tag)
  348. ,(format "Return non-nil to decorate TAG with `%s' style.\n%s"
  349. name doc))
  350. ;; Create an override method that will perform the highlight
  351. ;; operation if the -p method returns non-nil.
  352. (define-overloadable-function ,highlighter (tag)
  353. ,(format "Decorate TAG with `%s' style.\n%s"
  354. name doc))
  355. ;; Add this to the list of primary decoration modes.
  356. (add-to-list 'semantic-decoration-styles
  357. (cons ',(symbol-name name)
  358. ,defaultenable))
  359. )))
  360. ;;; Predefined decoration styles
  361. ;;
  362. ;;; Tag boundaries highlighting
  363. ;;
  364. (define-semantic-decoration-style semantic-tag-boundary
  365. "Place an overline in front of each long tag.
  366. Does not provide overlines for prototypes.")
  367. (defface semantic-tag-boundary-face
  368. '((((class color) (background dark))
  369. (:overline "cyan"))
  370. (((class color) (background light))
  371. (:overline "blue")))
  372. "*Face used to show long tags in.
  373. Used by decoration style: `semantic-tag-boundary'."
  374. :group 'semantic-faces)
  375. (defun semantic-tag-boundary-p-default (tag)
  376. "Return non-nil if TAG is a type, or a non-prototype function."
  377. (let ((c (semantic-tag-class tag)))
  378. (and
  379. (or
  380. ;; All types get a line?
  381. (eq c 'type)
  382. ;; Functions which aren't prototypes get a line.
  383. (and (eq c 'function)
  384. (not (semantic-tag-get-attribute tag :prototype-flag)))
  385. )
  386. ;; Note: The below restriction confused users.
  387. ;;
  388. ;; Nothing smaller than a few lines
  389. ;;(> (- (semantic-tag-end tag) (semantic-tag-start tag)) 150)
  390. ;; Random truth
  391. t)
  392. ))
  393. (defun semantic-tag-boundary-highlight-default (tag)
  394. "Highlight the first line of TAG as a boundary."
  395. (when (bufferp (semantic-tag-buffer tag))
  396. (with-current-buffer (semantic-tag-buffer tag)
  397. (semantic-decorate-tag
  398. tag
  399. (semantic-tag-start tag)
  400. (save-excursion
  401. (goto-char (semantic-tag-start tag))
  402. (end-of-line)
  403. (forward-char 1)
  404. (point))
  405. 'semantic-tag-boundary-face))
  406. ))
  407. ;;; Private member highlighting
  408. ;;
  409. (define-semantic-decoration-style semantic-decoration-on-private-members
  410. "Highlight class members that are designated as PRIVATE access."
  411. :enabled nil)
  412. (defface semantic-decoration-on-private-members-face
  413. '((((class color) (background dark))
  414. (:background "#200000"))
  415. (((class color) (background light))
  416. (:background "#8fffff")))
  417. "*Face used to show privately scoped tags in.
  418. Used by the decoration style: `semantic-decoration-on-private-members'."
  419. :group 'semantic-faces)
  420. (defun semantic-decoration-on-private-members-highlight-default (tag)
  421. "Highlight TAG as designated to have PRIVATE access.
  422. Use a primary decoration."
  423. (semantic-set-tag-face
  424. tag 'semantic-decoration-on-private-members-face))
  425. (defun semantic-decoration-on-private-members-p-default (tag)
  426. "Return non-nil if TAG has PRIVATE access."
  427. (and (member (semantic-tag-class tag) '(function variable))
  428. (eq (semantic-tag-protection tag) 'private)))
  429. ;;; Protected member highlighting
  430. ;;
  431. (defface semantic-decoration-on-protected-members-face
  432. '((((class color) (background dark))
  433. (:background "#000020"))
  434. (((class color) (background light))
  435. (:background "#fffff8")))
  436. "*Face used to show protected scoped tags in.
  437. Used by the decoration style: `semantic-decoration-on-protected-members'."
  438. :group 'semantic-faces)
  439. (define-semantic-decoration-style semantic-decoration-on-protected-members
  440. "Highlight class members that are designated as PROTECTED access."
  441. :enabled nil)
  442. (defun semantic-decoration-on-protected-members-p-default (tag)
  443. "Return non-nil if TAG has PROTECTED access."
  444. (and (member (semantic-tag-class tag) '(function variable))
  445. (eq (semantic-tag-protection tag) 'protected)))
  446. (defun semantic-decoration-on-protected-members-highlight-default (tag)
  447. "Highlight TAG as designated to have PROTECTED access.
  448. Use a primary decoration."
  449. (semantic-set-tag-face
  450. tag 'semantic-decoration-on-protected-members-face))
  451. (provide 'semantic/decorate/mode)
  452. ;; Local variables:
  453. ;; generated-autoload-file: "../loaddefs.el"
  454. ;; generated-autoload-load-name: "semantic/decorate/mode"
  455. ;; End:
  456. ;;; semantic/decorate/mode.el ends here