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