decorate.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. ;;; semantic/decorate.el --- Utilities for decorating/highlighting tokens.
  2. ;;; Copyright (C) 1999-2003, 2005-2007, 2009-2012
  3. ;;; Free Software Foundation, Inc.
  4. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  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. ;; Text representing a semantic tag is wrapped in an overlay.
  20. ;; This overlay can be used for highlighting, or setting other
  21. ;; editing properties on a tag, such as "read only."
  22. ;;
  23. (require 'semantic)
  24. (require 'pulse)
  25. ;;; Code:
  26. ;;; Highlighting Basics
  27. (defun semantic-highlight-tag (tag &optional face)
  28. "Specify that TAG should be highlighted.
  29. Optional FACE specifies the face to use."
  30. (let ((o (semantic-tag-overlay tag)))
  31. (semantic-overlay-put o 'old-face
  32. (cons (semantic-overlay-get o 'face)
  33. (semantic-overlay-get o 'old-face)))
  34. (semantic-overlay-put o 'face (or face 'semantic-tag-highlight-face))
  35. ))
  36. (defun semantic-unhighlight-tag (tag)
  37. "Unhighlight TAG, restoring its previous face."
  38. (let ((o (semantic-tag-overlay tag)))
  39. (semantic-overlay-put o 'face (car (semantic-overlay-get o 'old-face)))
  40. (semantic-overlay-put o 'old-face (cdr (semantic-overlay-get o 'old-face)))
  41. ))
  42. ;;; Momentary Highlighting - One line
  43. (defun semantic-momentary-highlight-one-tag-line (tag &optional face)
  44. "Highlight the first line of TAG, unhighlighting before next command.
  45. Optional argument FACE specifies the face to do the highlighting."
  46. (save-excursion
  47. ;; Go to first line in tag
  48. (semantic-go-to-tag tag)
  49. (pulse-momentary-highlight-one-line (point))))
  50. ;;; Momentary Highlighting - Whole Tag
  51. (defun semantic-momentary-highlight-tag (tag &optional face)
  52. "Highlight TAG, removing highlighting when the user hits a key.
  53. Optional argument FACE is the face to use for highlighting.
  54. If FACE is not specified, then `highlight' will be used."
  55. (when (semantic-tag-with-position-p tag)
  56. (if (not (semantic-overlay-p (semantic-tag-overlay tag)))
  57. ;; No overlay, but a position. Highlight the first line only.
  58. (semantic-momentary-highlight-one-tag-line tag face)
  59. ;; The tag has an overlay, highlight the whole thing
  60. (pulse-momentary-highlight-overlay (semantic-tag-overlay tag)
  61. face)
  62. )))
  63. (defun semantic-set-tag-face (tag face)
  64. "Specify that TAG should use FACE for display."
  65. (semantic-overlay-put (semantic-tag-overlay tag) 'face face))
  66. (defun semantic-set-tag-invisible (tag &optional visible)
  67. "Enable the text in TAG to be made invisible.
  68. If VISIBLE is non-nil, make the text visible."
  69. (semantic-overlay-put (semantic-tag-overlay tag) 'invisible
  70. (not visible)))
  71. (defun semantic-tag-invisible-p (tag)
  72. "Return non-nil if TAG is invisible."
  73. (semantic-overlay-get (semantic-tag-overlay tag) 'invisible))
  74. (defun semantic-set-tag-intangible (tag &optional tangible)
  75. "Enable the text in TAG to be made intangible.
  76. If TANGIBLE is non-nil, make the text visible.
  77. This function does not have meaning in XEmacs because it seems that
  78. the extent 'intangible' property does not exist."
  79. (semantic-overlay-put (semantic-tag-overlay tag) 'intangible
  80. (not tangible)))
  81. (defun semantic-tag-intangible-p (tag)
  82. "Return non-nil if TAG is intangible.
  83. This function does not have meaning in XEmacs because it seems that
  84. the extent 'intangible' property does not exist."
  85. (semantic-overlay-get (semantic-tag-overlay tag) 'intangible))
  86. (defun semantic-overlay-signal-read-only
  87. (overlay after start end &optional len)
  88. "Hook used in modification hooks to prevent modification.
  89. Allows deletion of the entire text.
  90. Argument OVERLAY, AFTER, START, END, and LEN are passed in by the system."
  91. ;; Stolen blithely from cpp.el in Emacs 21.1
  92. (if (and (not after)
  93. (or (< (semantic-overlay-start overlay) start)
  94. (> (semantic-overlay-end overlay) end)))
  95. (error "This text is read only")))
  96. (defun semantic-set-tag-read-only (tag &optional writable)
  97. "Enable the text in TAG to be made read-only.
  98. Optional argument WRITABLE should be non-nil to make the text writable
  99. instead of read-only."
  100. (let ((o (semantic-tag-overlay tag))
  101. (hook (if writable nil '(semantic-overlay-signal-read-only))))
  102. (if (featurep 'xemacs)
  103. ;; XEmacs extents have a 'read-only' property.
  104. (semantic-overlay-put o 'read-only (not writable))
  105. (semantic-overlay-put o 'modification-hooks hook)
  106. (semantic-overlay-put o 'insert-in-front-hooks hook)
  107. (semantic-overlay-put o 'insert-behind-hooks hook))))
  108. (defun semantic-tag-read-only-p (tag)
  109. "Return non-nil if the current TAG is marked read only."
  110. (let ((o (semantic-tag-overlay tag)))
  111. (if (featurep 'xemacs)
  112. ;; XEmacs extents have a 'read-only' property.
  113. (semantic-overlay-get o 'read-only)
  114. (member 'semantic-overlay-signal-read-only
  115. (semantic-overlay-get o 'modification-hooks)))))
  116. ;;; Secondary overlays
  117. ;;
  118. ;; Some types of decoration require a second overlay to be made.
  119. ;; It could be for images, arrows, or whatever.
  120. ;; We need a way to create such an overlay, and make sure it
  121. ;; gets whacked, but doesn't show up in the master list
  122. ;; of overlays used for searching.
  123. (defun semantic-tag-secondary-overlays (tag)
  124. "Return a list of secondary overlays active on TAG."
  125. (semantic--tag-get-property tag 'secondary-overlays))
  126. (defun semantic-tag-create-secondary-overlay (tag &optional link-hook)
  127. "Create a secondary overlay for TAG.
  128. Returns an overlay. The overlay is also saved in TAG.
  129. LINK-HOOK is a function called whenever TAG is to be linked into
  130. a buffer. It should take TAG and OVERLAY as arguments.
  131. The LINK-HOOK should be used to position and set properties on the
  132. generated secondary overlay."
  133. (if (not (semantic-tag-overlay tag))
  134. ;; do nothing if there is no overlay
  135. nil
  136. (let* ((os (semantic-tag-start tag))
  137. (oe (semantic-tag-end tag))
  138. (o (semantic-make-overlay os oe (semantic-tag-buffer tag) t))
  139. (attr (semantic-tag-secondary-overlays tag))
  140. )
  141. (semantic--tag-put-property tag 'secondary-overlays (cons o attr))
  142. (semantic-overlay-put o 'semantic-secondary t)
  143. (semantic-overlay-put o 'semantic-link-hook link-hook)
  144. (semantic-tag-add-hook tag 'link-hook 'semantic--tag-link-secondary-overlays)
  145. (semantic-tag-add-hook tag 'unlink-hook 'semantic--tag-unlink-secondary-overlays)
  146. (semantic-tag-add-hook tag 'unlink-copy-hook 'semantic--tag-unlink-copy-secondary-overlays)
  147. (run-hook-with-args link-hook tag o)
  148. o)))
  149. (defun semantic-tag-get-secondary-overlay (tag property)
  150. "Return secondary overlays from TAG with PROPERTY.
  151. PROPERTY is a symbol and all overlays with that symbol are returned.."
  152. (let* ((olsearch (semantic-tag-secondary-overlays tag))
  153. (o nil))
  154. (while olsearch
  155. (when (semantic-overlay-get (car olsearch) property)
  156. (setq o (cons (car olsearch) o)))
  157. (setq olsearch (cdr olsearch)))
  158. o))
  159. (defun semantic-tag-delete-secondary-overlay (tag overlay-or-property)
  160. "Delete from TAG the secondary overlay OVERLAY-OR-PROPERTY.
  161. If OVERLAY-OR-PROPERTY is an overlay, delete that overlay.
  162. If OVERLAY-OR-PROPERTY is a symbol, find the overlay with that property."
  163. (let* ((o overlay-or-property))
  164. (if (semantic-overlay-p o)
  165. (setq o (list o))
  166. (setq o (semantic-tag-get-secondary-overlay tag overlay-or-property)))
  167. (while (semantic-overlay-p (car o))
  168. ;; We don't really need to worry about the hooks.
  169. ;; They will clean themselves up eventually ??
  170. (semantic--tag-put-property
  171. tag 'secondary-overlays
  172. (delete (car o) (semantic-tag-secondary-overlays tag)))
  173. (semantic-overlay-delete (car o))
  174. (setq o (cdr o)))))
  175. (defun semantic--tag-unlink-copy-secondary-overlays (tag)
  176. "Unlink secondary overlays from TAG which is a copy.
  177. This means we don't destroy the overlays, only remove reference
  178. from them in TAG."
  179. (let ((ol (semantic-tag-secondary-overlays tag)))
  180. (while ol
  181. ;; Else, remove all traces of ourself from the tag
  182. ;; Note to self: Does this prevent multiple types of secondary
  183. ;; overlays per tag?
  184. (semantic-tag-remove-hook tag 'link-hook 'semantic--tag-link-secondary-overlays)
  185. (semantic-tag-remove-hook tag 'unlink-hook 'semantic--tag-unlink-secondary-overlays)
  186. (semantic-tag-remove-hook tag 'unlink-copy-hook 'semantic--tag-unlink-copy-secondary-overlays)
  187. ;; Next!
  188. (setq ol (cdr ol)))
  189. (semantic--tag-put-property tag 'secondary-overlays nil)
  190. ))
  191. (defun semantic--tag-unlink-secondary-overlays (tag)
  192. "Unlink secondary overlays from TAG."
  193. (let ((ol (semantic-tag-secondary-overlays tag))
  194. (nl nil))
  195. (while ol
  196. (if (semantic-overlay-get (car ol) 'semantic-link-hook)
  197. ;; Only put in a proxy if there is a link-hook. If there is no link-hook
  198. ;; the decorating mode must know when tags are unlinked on its own.
  199. (setq nl (cons (semantic-overlay-get (car ol) 'semantic-link-hook)
  200. nl))
  201. ;; Else, remove all traces of ourself from the tag
  202. ;; Note to self: Does this prevent multiple types of secondary
  203. ;; overlays per tag?
  204. (semantic-tag-remove-hook tag 'link-hook 'semantic--tag-link-secondary-overlays)
  205. (semantic-tag-remove-hook tag 'unlink-hook 'semantic--tag-unlink-secondary-overlays)
  206. (semantic-tag-remove-hook tag 'unlink-copy-hook 'semantic--tag-unlink-copy-secondary-overlays)
  207. )
  208. (semantic-overlay-delete (car ol))
  209. (setq ol (cdr ol)))
  210. (semantic--tag-put-property tag 'secondary-overlays (nreverse nl))
  211. ))
  212. (defun semantic--tag-link-secondary-overlays (tag)
  213. "Unlink secondary overlays from TAG."
  214. (let ((ol (semantic-tag-secondary-overlays tag)))
  215. ;; Wipe out old values.
  216. (semantic--tag-put-property tag 'secondary-overlays nil)
  217. ;; Run all the link hooks.
  218. (while ol
  219. (semantic-tag-create-secondary-overlay tag (car ol))
  220. (setq ol (cdr ol)))
  221. ))
  222. ;;; Secondary Overlay Uses
  223. ;;
  224. ;; States to put on tags that depend on a secondary overlay.
  225. (defun semantic-set-tag-folded (tag &optional folded)
  226. "Fold TAG, such that only the first line of text is shown.
  227. Optional argument FOLDED should be non-nil to fold the tag.
  228. nil implies the tag should be fully shown."
  229. ;; If they are different, do the deed.
  230. (let ((o (semantic-tag-folded-p tag)))
  231. (if (not folded)
  232. ;; We unfold.
  233. (when o
  234. (semantic-tag-delete-secondary-overlay tag 'semantic-folded))
  235. (unless o
  236. ;; Add the foldn
  237. (setq o (semantic-tag-create-secondary-overlay tag))
  238. ;; mark as folded
  239. (semantic-overlay-put o 'semantic-folded t)
  240. ;; Move to cover end of tag
  241. (save-excursion
  242. (goto-char (semantic-tag-start tag))
  243. (end-of-line)
  244. (semantic-overlay-move o (point) (semantic-tag-end tag)))
  245. ;; We need to modify the invisibility spec for this to
  246. ;; work.
  247. (if (or (eq buffer-invisibility-spec t)
  248. (not (assoc 'semantic-fold buffer-invisibility-spec)))
  249. (add-to-invisibility-spec '(semantic-fold . t)))
  250. (semantic-overlay-put o 'invisible 'semantic-fold)
  251. (overlay-put o 'isearch-open-invisible
  252. 'semantic-set-tag-folded-isearch)))
  253. ))
  254. (declare-function semantic-current-tag "semantic/find")
  255. (defun semantic-set-tag-folded-isearch (overlay)
  256. "Called by isearch if it discovers text in the folded region.
  257. OVERLAY is passed in by isearch."
  258. (semantic-set-tag-folded (semantic-current-tag) nil)
  259. )
  260. (defun semantic-tag-folded-p (tag)
  261. "Non-nil if TAG is currently folded."
  262. (semantic-tag-get-secondary-overlay tag 'semantic-folded)
  263. )
  264. (provide 'semantic/decorate)
  265. ;;; semantic/decorate.el ends here