]> code.delx.au - gnu-emacs/blob - lisp/cedet/semantic/decorate/mode.el
CEDET (development tools) package merged.
[gnu-emacs] / lisp / cedet / semantic / decorate / mode.el
1 ;;; semantic/decorate/mode.el --- Minor mode for decorating tags
2
3 ;;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008
4 ;;; Free Software Foundation, Inc.
5
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Keywords: syntax
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25 ;;
26 ;; A minor mode for use in decorating tags.
27 ;;
28 ;; There are two types of decorations that can be performed on a tag.
29 ;; You can either highlight the full tag, or you can add an
30 ;; independent decoration on some part of the tag body.
31 ;;
32 ;; For independent decoration in particular, managing them so that they
33 ;; do not get corrupted is challenging. This major mode and
34 ;; corresponding macros will make handling those types of decorations
35 ;; easier.
36 ;;
37
38 ;;; Code:
39 (require 'semantic)
40 (require 'semantic/decorate)
41 (require 'semantic/tag-ls)
42 (require 'semantic/util-modes)
43
44 ;;; Styles List
45 ;;
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."
52 :group 'semantic
53 :type '(repeat (cons (string :tag "Decoration Name")
54 (boolean :tag "Enabled")))
55 )
56
57 ;;; Misc.
58 ;;
59 (defsubst semantic-decorate-style-predicate (style)
60 "Return the STYLE's predicate function."
61 (intern (format "%s-p" style)))
62
63 (defsubst semantic-decorate-style-highlighter (style)
64 "Return the STYLE's highlighter function."
65 (intern (format "%s-highlight" style)))
66
67 ;;; Base decoration API
68 ;;
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)))
73
74 (defsubst semantic-decoration-set-property (deco property value)
75 "Set the DECO decoration's PROPERTY to VALUE.
76 Return DECO."
77 (assert (semantic-decoration-p deco))
78 (semantic-overlay-put deco property value)
79 deco)
80
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))
85
86 (defsubst semantic-decoration-set-face (deco face)
87 "Set the face of the decoration DECO to FACE.
88 Return DECO."
89 (semantic-decoration-set-property deco 'face face))
90
91 (defsubst semantic-decoration-face (deco)
92 "Return the face of the decoration DECO."
93 (semantic-decoration-get-property deco 'face))
94
95 (defsubst semantic-decoration-set-priority (deco priority)
96 "Set the priority of the decoration DECO to PRIORITY.
97 Return DECO."
98 (assert (natnump priority))
99 (semantic-decoration-set-property deco 'priority priority))
100
101 (defsubst semantic-decoration-priority (deco)
102 "Return the priority of the decoration DECO."
103 (semantic-decoration-get-property deco 'priority))
104
105 (defsubst semantic-decoration-move (deco begin end)
106 "Move the decoration DECO on the region between BEGIN and END.
107 Return DECO."
108 (assert (semantic-decoration-p deco))
109 (semantic-overlay-move deco begin end)
110 deco)
111 \f
112 ;;; Tag decoration
113 ;;
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
117 FACE.
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)
125 deco))
126
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)))
138
139 (defun semantic-decorate-tag-decoration (tag)
140 "Return decoration found on TAG."
141 (semantic-tag-get-secondary-overlay tag 'semantic-decoration))
142 \f
143 ;;; Global setup of active decorations
144 ;;
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)))))
154
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))))
162
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.
171
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
174 ;; situations
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))))
181 (and (cdr style)
182 (fboundp pred)
183 (funcall pred tag)
184 (fboundp high)
185 (funcall high tag))))
186 ;; Recurse on the children of all tags
187 (semantic-decorate-add-decorations
188 (semantic-tag-components-with-overlays tag))))
189 \f
190 ;;; PENDING DECORATIONS
191 ;;
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.")
198
199 (semantic-varalias-obsolete 'semantic-decorate-pending-decoration-hooks
200 'semantic-decorate-pending-decoration-hook)
201
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."
206 (save-excursion
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)))
210
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'."
214 (save-excursion
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)))
219
220 \f
221 ;;; DECORATION MODE
222 ;;
223 ;; Generic mode for handling basic highlighting and decorations.
224 ;;
225
226 (defcustom global-semantic-decoration-mode nil
227 "*If non-nil, enable global use of command `semantic-decoration-mode'.
228 When this mode is activated, decorations specified by
229 `semantic-decoration-styles'."
230 :group 'semantic
231 :group 'semantic-modes
232 :type 'boolean
233 :require 'semantic/decorate/mode
234 :initialize 'custom-initialize-default
235 :set (lambda (sym val)
236 (global-semantic-decoration-mode (if val 1 -1))))
237
238 ;;;###autoload
239 (defun global-semantic-decoration-mode (&optional arg)
240 "Toggle global use of option `semantic-decoration-mode'.
241 Decoration mode turns on all active decorations as specified
242 by `semantic-decoration-styles'.
243 If ARG is positive, enable, if it is negative, disable.
244 If ARG is nil, then toggle."
245 (interactive "P")
246 (setq global-semantic-decoration-mode
247 (semantic-toggle-minor-mode-globally
248 'semantic-decoration-mode arg)))
249
250 (defcustom semantic-decoration-mode-hook nil
251 "Hook run at the end of function `semantic-decoration-mode'."
252 :group 'semantic
253 :type 'hook)
254
255 ;;;;###autoload
256 (defvar semantic-decoration-mode nil
257 "Non-nil if command `semantic-decoration-mode' is enabled.
258 Use the command `semantic-decoration-mode' to change this variable.")
259 (make-variable-buffer-local 'semantic-decoration-mode)
260
261 (defun semantic-decoration-mode-setup ()
262 "Setup the `semantic-decoration-mode' minor mode.
263 The minor mode can be turned on only if the semantic feature is available
264 and the current buffer was set up for parsing. Return non-nil if the
265 minor mode is enabled."
266 (if semantic-decoration-mode
267 (if (not (and (featurep 'semantic) (semantic-active-p)))
268 (progn
269 ;; Disable minor mode if semantic stuff not available
270 (setq semantic-decoration-mode nil)
271 (error "Buffer %s was not set up for parsing"
272 (buffer-name)))
273 ;; Add hooks
274 (semantic-make-local-hook 'semantic-after-partial-cache-change-hook)
275 (add-hook 'semantic-after-partial-cache-change-hook
276 'semantic-decorate-tags-after-partial-reparse nil t)
277 (semantic-make-local-hook 'semantic-after-toplevel-cache-change-hook)
278 (add-hook 'semantic-after-toplevel-cache-change-hook
279 'semantic-decorate-tags-after-full-reparse nil t)
280 ;; Add decorations to available tags. The above hooks ensure
281 ;; that new tags will be decorated when they become available.
282 (semantic-decorate-add-decorations (semantic-fetch-available-tags))
283 )
284 ;; Remove decorations from available tags.
285 (semantic-decorate-clear-decorations (semantic-fetch-available-tags))
286 ;; Cleanup any leftover crap too.
287 (semantic-decorate-flush-decorations)
288 ;; Remove hooks
289 (remove-hook 'semantic-after-partial-cache-change-hook
290 'semantic-decorate-tags-after-partial-reparse t)
291 (remove-hook 'semantic-after-toplevel-cache-change-hook
292 'semantic-decorate-tags-after-full-reparse t)
293 )
294 semantic-decoration-mode)
295
296 (defun semantic-decoration-mode (&optional arg)
297 "Minor mode for decorating tags.
298 Decorations are specified in `semantic-decoration-styles'.
299 You can define new decoration styles with
300 `define-semantic-decoration-style'.
301 With prefix argument ARG, turn on if positive, otherwise off. The
302 minor mode can be turned on only if semantic feature is available and
303 the current buffer was set up for parsing. Return non-nil if the
304 minor mode is enabled."
305 ;;
306 ;;\\{semantic-decoration-map}"
307 (interactive
308 (list (or current-prefix-arg
309 (if semantic-decoration-mode 0 1))))
310 (setq semantic-decoration-mode
311 (if arg
312 (>
313 (prefix-numeric-value arg)
314 0)
315 (not semantic-decoration-mode)))
316 (semantic-decoration-mode-setup)
317 (run-hooks 'semantic-decoration-mode-hook)
318 (if (interactive-p)
319 (message "decoration-mode minor mode %sabled"
320 (if semantic-decoration-mode "en" "dis")))
321 (semantic-mode-line-update)
322 semantic-decoration-mode)
323
324 (semantic-add-minor-mode 'semantic-decoration-mode
325 ""
326 nil)
327
328 (defun semantic-decorate-tags-after-full-reparse (tag-list)
329 "Add decorations after a complete reparse of the current buffer.
330 TAG-LIST is the list of tags recently parsed.
331 Flush all existing decorations and call `semantic-decorate-add-decorations' to
332 add decorations.
333 Called from `semantic-after-toplevel-cache-change-hook'."
334 ;; Flush everything
335 (semantic-decorate-flush-decorations)
336 ;; Add it back on
337 (semantic-decorate-add-decorations tag-list))
338
339 (defun semantic-decorate-tags-after-partial-reparse (tag-list)
340 "Add decorations when new tags are created in the current buffer.
341 TAG-LIST is the list of newly created tags.
342 Call `semantic-decorate-add-decorations' to add decorations.
343 Called from `semantic-after-partial-cache-change-hook'."
344 (semantic-decorate-add-decorations tag-list))
345
346 \f
347 ;;; Enable/Disable toggling
348 ;;
349 (defun semantic-decoration-style-enabled-p (style)
350 "Return non-nil if STYLE is currently enabled.
351 Return nil if the style is disabled, or does not exist."
352 (let ((pair (assoc style semantic-decoration-styles)))
353 (and pair (cdr pair))))
354
355 (defun semantic-toggle-decoration-style (name &optional arg)
356 "Turn on/off the decoration style with NAME.
357 Decorations are specified in `semantic-decoration-styles'.
358 With prefix argument ARG, turn on if positive, otherwise off.
359 Return non-nil if the decoration style is enabled."
360 (interactive
361 (list (completing-read "Decoration style: "
362 semantic-decoration-styles nil t)
363 current-prefix-arg))
364 (setq name (format "%s" name)) ;; Ensure NAME is a string.
365 (unless (equal name "")
366 (let* ((style (assoc name semantic-decoration-styles))
367 (flag (if arg
368 (> (prefix-numeric-value arg) 0)
369 (not (cdr style)))))
370 (unless (eq (cdr style) flag)
371 ;; Store the new flag.
372 (setcdr style flag)
373 ;; Refresh decorations is `semantic-decoration-mode' is on.
374 (when semantic-decoration-mode
375 (semantic-decoration-mode -1)
376 (semantic-decoration-mode 1))
377 (when (interactive-p)
378 (message "Decoration style %s turned %s" (car style)
379 (if flag "on" "off"))))
380 flag)))
381
382 (defvar semantic-decoration-menu-cache nil
383 "Cache of the decoration menu.")
384
385 (defun semantic-decoration-build-style-menu (style)
386 "Build a menu item for controlling a specific decoration STYLE."
387 (vector (car style)
388 `(lambda () (interactive)
389 (semantic-toggle-decoration-style
390 ,(car style)))
391 :style 'toggle
392 :selected `(semantic-decoration-style-enabled-p ,(car style))
393 ))
394
395 (defun semantic-build-decoration-mode-menu (&rest ignore)
396 "Create a menu listing all the known decorations for toggling.
397 IGNORE any input arguments."
398 (or semantic-decoration-menu-cache
399 (setq semantic-decoration-menu-cache
400 (mapcar 'semantic-decoration-build-style-menu
401 (reverse semantic-decoration-styles))
402 )))
403
404 \f
405 ;;; Defining decoration styles
406 ;;
407 (defmacro define-semantic-decoration-style (name doc &rest flags)
408 "Define a new decoration style with NAME.
409 DOC is a documentation string describing the decoration style NAME.
410 It is appended to auto-generated doc strings.
411 An Optional list of FLAGS can also be specified. Flags are:
412 :enabled <value> - specify the default enabled value for NAME.
413
414
415 This defines two new overload functions respectively called `NAME-p'
416 and `NAME-highlight', for which you must provide a default
417 implementation in respectively the functions `NAME-p-default' and
418 `NAME-highlight-default'. Those functions are passed a tag. `NAME-p'
419 must return non-nil to indicate that the tag should be decorated by
420 `NAME-highlight'.
421
422 To put primary decorations on a tag `NAME-highlight' must use
423 functions like `semantic-set-tag-face', `semantic-set-tag-intangible',
424 etc., found in the semantic-decorate library.
425
426 To add other kind of decorations on a tag, `NAME-highlight' must use
427 `semantic-decorate-tag', and other functions of the semantic
428 decoration API found in this library."
429 (let ((predicate (semantic-decorate-style-predicate name))
430 (highlighter (semantic-decorate-style-highlighter name))
431 (defaultenable (if (plist-member flags :enabled)
432 (plist-get flags :enabled)
433 t))
434 )
435 `(progn
436 ;; Clear the menu cache so that new items are added when
437 ;; needed.
438 (setq semantic-decoration-menu-cache nil)
439 ;; Create an override method to specify if a given tag belongs
440 ;; to this type of decoration
441 (define-overloadable-function ,predicate (tag)
442 ,(format "Return non-nil to decorate TAG with `%s' style.\n%s"
443 name doc))
444 ;; Create an override method that will perform the highlight
445 ;; operation if the -p method returns non-nil.
446 (define-overloadable-function ,highlighter (tag)
447 ,(format "Decorate TAG with `%s' style.\n%s"
448 name doc))
449 ;; Add this to the list of primary decoration modes.
450 (add-to-list 'semantic-decoration-styles
451 (cons ',(symbol-name name)
452 ,defaultenable))
453 )))
454 \f
455 ;;; Predefined decoration styles
456 ;;
457
458 ;;; Tag boundaries highlighting
459 ;;
460 (define-semantic-decoration-style semantic-tag-boundary
461 "Place an overline in front of each long tag.
462 Does not provide overlines for prototypes.")
463
464 (defface semantic-tag-boundary-face
465 '((((class color) (background dark))
466 (:overline "cyan"))
467 (((class color) (background light))
468 (:overline "blue")))
469 "*Face used to show long tags in.
470 Used by decoration style: `semantic-tag-boundary'."
471 :group 'semantic-faces)
472
473 (defun semantic-tag-boundary-p-default (tag)
474 "Return non-nil if TAG is a type, or a non-prototype function."
475 (let ((c (semantic-tag-class tag)))
476 (and
477 (or
478 ;; All types get a line?
479 (eq c 'type)
480 ;; Functions which aren't prototypes get a line.
481 (and (eq c 'function)
482 (not (semantic-tag-get-attribute tag :prototype-flag)))
483 )
484 ;; Note: The below restriction confused users.
485 ;;
486 ;; Nothing smaller than a few lines
487 ;;(> (- (semantic-tag-end tag) (semantic-tag-start tag)) 150)
488 ;; Random truth
489 t)
490 ))
491
492 (defun semantic-tag-boundary-highlight-default (tag)
493 "Highlight the first line of TAG as a boundary."
494 (when (bufferp (semantic-tag-buffer tag))
495 (with-current-buffer (semantic-tag-buffer tag)
496 (semantic-decorate-tag
497 tag
498 (semantic-tag-start tag)
499 (save-excursion
500 (goto-char (semantic-tag-start tag))
501 (end-of-line)
502 (forward-char 1)
503 (point))
504 'semantic-tag-boundary-face))
505 ))
506
507 ;;; Private member highlighting
508 ;;
509 (define-semantic-decoration-style semantic-decoration-on-private-members
510 "Highlight class members that are designated as PRIVATE access."
511 :enabled nil)
512
513 (defface semantic-decoration-on-private-members-face
514 '((((class color) (background dark))
515 (:background "#200000"))
516 (((class color) (background light))
517 (:background "#8fffff")))
518 "*Face used to show privately scoped tags in.
519 Used by the decoration style: `semantic-decoration-on-private-members'."
520 :group 'semantic-faces)
521
522 (defun semantic-decoration-on-private-members-highlight-default (tag)
523 "Highlight TAG as designated to have PRIVATE access.
524 Use a primary decoration."
525 (semantic-set-tag-face
526 tag 'semantic-decoration-on-private-members-face))
527
528 (defun semantic-decoration-on-private-members-p-default (tag)
529 "Return non-nil if TAG has PRIVATE access."
530 (and (member (semantic-tag-class tag) '(function variable))
531 (eq (semantic-tag-protection tag) 'private)))
532
533 ;;; Protected member highlighting
534 ;;
535 (defface semantic-decoration-on-protected-members-face
536 '((((class color) (background dark))
537 (:background "#000020"))
538 (((class color) (background light))
539 (:background "#fffff8")))
540 "*Face used to show protected scoped tags in.
541 Used by the decoration style: `semantic-decoration-on-protected-members'."
542 :group 'semantic-faces)
543
544 (define-semantic-decoration-style semantic-decoration-on-protected-members
545 "Highlight class members that are designated as PROTECTED access."
546 :enabled nil)
547
548 (defun semantic-decoration-on-protected-members-p-default (tag)
549 "Return non-nil if TAG has PROTECTED access."
550 (and (member (semantic-tag-class tag) '(function variable))
551 (eq (semantic-tag-protection tag) 'protected)))
552
553 (defun semantic-decoration-on-protected-members-highlight-default (tag)
554 "Highlight TAG as designated to have PROTECTED access.
555 Use a primary decoration."
556 (semantic-set-tag-face
557 tag 'semantic-decoration-on-protected-members-face))
558
559 (provide 'semantic/decorate/mode)
560
561 ;; Local variables:
562 ;; generated-autoload-file: "../loaddefs.el"
563 ;; generated-autoload-feature: semantic/loaddefs
564 ;; generated-autoload-load-name: "semantic/decorate/mode"
565 ;; End:
566
567 ;;; semantic/decorate/mode.el ends here