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