comparison lisp/cedet/semantic/decorate/mode.el @ 104451:2858c6bcc446

lisp/cedet/semantic/decorate/include.el: lisp/cedet/semantic/decorate/mode.el: New files.
author Chong Yidong <cyd@stupidchicken.com>
date Sat, 05 Sep 2009 20:45:54 +0000
parents
children 801834237f9c
comparison
equal deleted inserted replaced
104450:08a15f853c45 104451:2858c6bcc446
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 (eval-when-compile (require 'cl))
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
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
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
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-hooks nil
198 "Functions to call with pending decoration changes.")
199
200 (defun semantic-decorate-add-pending-decoration (fcn &optional buffer)
201 "Add a pending decoration change represented by FCN.
202 Applies only to the current BUFFER.
203 The setting of FCN will be removed after it is run."
204 (save-excursion
205 (when buffer (set-buffer buffer))
206 (semantic-make-local-hook 'semantic-decorate-flush-pending-decorations)
207 (add-hook 'semantic-decorate-pending-decoration-hooks fcn nil t)))
208
209 ;;;;###autoload
210 (defun semantic-decorate-flush-pending-decorations (&optional buffer)
211 "Flush any pending decorations for BUFFER.
212 Flush functions from `semantic-decorate-pending-decoration-hooks'."
213 (save-excursion
214 (when buffer (set-buffer buffer))
215 (run-hooks 'semantic-decorate-pending-decoration-hooks)
216 ;; Always reset the hooks
217 (setq semantic-decorate-pending-decoration-hooks nil)))
218
219
220 ;;; DECORATION MODE
221 ;;
222 ;; Generic mode for handling basic highlighting and decorations.
223 ;;
224
225 (defcustom global-semantic-decoration-mode nil
226 "*If non-nil, enable global use of command `semantic-decoration-mode'.
227 When this mode is activated, decorations specified by
228 `semantic-decoration-styles'."
229 :group 'semantic
230 :group 'semantic-modes
231 :type 'boolean
232 :require 'semantic/decorate/mode
233 :initialize 'custom-initialize-default
234 :set (lambda (sym val)
235 (global-semantic-decoration-mode (if val 1 -1))))
236
237 (defun global-semantic-decoration-mode (&optional arg)
238 "Toggle global use of option `semantic-decoration-mode'.
239 Decoration mode turns on all active decorations as specified
240 by `semantic-decoration-styles'.
241 If ARG is positive, enable, if it is negative, disable.
242 If ARG is nil, then toggle."
243 (interactive "P")
244 (setq global-semantic-decoration-mode
245 (semantic-toggle-minor-mode-globally
246 'semantic-decoration-mode arg)))
247
248 (defcustom semantic-decoration-mode-hook nil
249 "*Hook run at the end of function `semantic-decoration-mode'."
250 :group 'semantic
251 :type 'hook)
252
253 ;;;;###autoload
254 (defvar semantic-decoration-mode nil
255 "Non-nil if command `semantic-decoration-mode' is enabled.
256 Use the command `semantic-decoration-mode' to change this variable.")
257 (make-variable-buffer-local 'semantic-decoration-mode)
258
259 (defun semantic-decoration-mode-setup ()
260 "Setup the `semantic-decoration-mode' minor mode.
261 The minor mode can be turned on only if the semantic feature is available
262 and the current buffer was set up for parsing. Return non-nil if the
263 minor mode is enabled."
264 (if semantic-decoration-mode
265 (if (not (and (featurep 'semantic) (semantic-active-p)))
266 (progn
267 ;; Disable minor mode if semantic stuff not available
268 (setq semantic-decoration-mode nil)
269 (error "Buffer %s was not set up for parsing"
270 (buffer-name)))
271 ;; Add hooks
272 (semantic-make-local-hook 'semantic-after-partial-cache-change-hook)
273 (add-hook 'semantic-after-partial-cache-change-hook
274 'semantic-decorate-tags-after-partial-reparse nil t)
275 (semantic-make-local-hook 'semantic-after-toplevel-cache-change-hook)
276 (add-hook 'semantic-after-toplevel-cache-change-hook
277 'semantic-decorate-tags-after-full-reparse nil t)
278 ;; Add decorations to available tags. The above hooks ensure
279 ;; that new tags will be decorated when they become available.
280 (semantic-decorate-add-decorations (semantic-fetch-available-tags))
281 )
282 ;; Remove decorations from available tags.
283 (semantic-decorate-clear-decorations (semantic-fetch-available-tags))
284 ;; Cleanup any leftover crap too.
285 (semantic-decorate-flush-decorations)
286 ;; Remove hooks
287 (remove-hook 'semantic-after-partial-cache-change-hook
288 'semantic-decorate-tags-after-partial-reparse t)
289 (remove-hook 'semantic-after-toplevel-cache-change-hook
290 'semantic-decorate-tags-after-full-reparse t)
291 )
292 semantic-decoration-mode)
293
294 ;;;;###autoload
295 (defun semantic-decoration-mode (&optional arg)
296 "Minor mode for decorating tags.
297 Decorations are specified in `semantic-decoration-styles'.
298 You can define new decoration styles with
299 `define-semantic-decoration-style'.
300 With prefix argument ARG, turn on if positive, otherwise off. The
301 minor mode can be turned on only if semantic feature is available and
302 the current buffer was set up for parsing. Return non-nil if the
303 minor mode is enabled."
304 ;;
305 ;;\\{semantic-decoration-map}"
306 (interactive
307 (list (or current-prefix-arg
308 (if semantic-decoration-mode 0 1))))
309 (setq semantic-decoration-mode
310 (if arg
311 (>
312 (prefix-numeric-value arg)
313 0)
314 (not semantic-decoration-mode)))
315 (semantic-decoration-mode-setup)
316 (run-hooks 'semantic-decoration-mode-hook)
317 (if (interactive-p)
318 (message "decoration-mode minor mode %sabled"
319 (if semantic-decoration-mode "en" "dis")))
320 (semantic-mode-line-update)
321 semantic-decoration-mode)
322
323 (semantic-add-minor-mode 'semantic-decoration-mode
324 ""
325 nil)
326
327 (defun semantic-decorate-tags-after-full-reparse (tag-list)
328 "Add decorations after a complete reparse of the current buffer.
329 TAG-LIST is the list of tags recently parsed.
330 Flush all existing decorations and call `semantic-decorate-add-decorations' to
331 add decorations.
332 Called from `semantic-after-toplevel-cache-change-hook'."
333 ;; Flush everything
334 (semantic-decorate-flush-decorations)
335 ;; Add it back on
336 (semantic-decorate-add-decorations tag-list))
337
338 (defun semantic-decorate-tags-after-partial-reparse (tag-list)
339 "Add decorations when new tags are created in the current buffer.
340 TAG-LIST is the list of newly created tags.
341 Call `semantic-decorate-add-decorations' to add decorations.
342 Called from `semantic-after-partial-cache-change-hook'."
343 (semantic-decorate-add-decorations tag-list))
344
345
346 ;;; Enable/Disable toggling
347 ;;
348 (defun semantic-decoration-style-enabled-p (style)
349 "Return non-nil if STYLE is currently enabled.
350 Return nil if the style is disabled, or does not exist."
351 (let ((pair (assoc style semantic-decoration-styles)))
352 (and pair (cdr pair))))
353
354 (defun semantic-toggle-decoration-style (name &optional arg)
355 "Turn on/off the decoration style with NAME.
356 Decorations are specified in `semantic-decoration-styles'.
357 With prefix argument ARG, turn on if positive, otherwise off.
358 Return non-nil if the decoration style is enabled."
359 (interactive
360 (list (completing-read "Decoration style: "
361 semantic-decoration-styles nil t)
362 current-prefix-arg))
363 (setq name (format "%s" name)) ;; Ensure NAME is a string.
364 (unless (equal name "")
365 (let* ((style (assoc name semantic-decoration-styles))
366 (flag (if arg
367 (> (prefix-numeric-value arg) 0)
368 (not (cdr style)))))
369 (unless (eq (cdr style) flag)
370 ;; Store the new flag.
371 (setcdr style flag)
372 ;; Refresh decorations is `semantic-decoration-mode' is on.
373 (when semantic-decoration-mode
374 (semantic-decoration-mode -1)
375 (semantic-decoration-mode 1))
376 (when (interactive-p)
377 (message "Decoration style %s turned %s" (car style)
378 (if flag "on" "off"))))
379 flag)))
380
381 (defvar semantic-decoration-menu-cache nil
382 "Cache of the decoration menu.")
383
384 (defun semantic-decoration-build-style-menu (style)
385 "Build a menu item for controlling a specific decoration STYLE."
386 (vector (car style)
387 `(lambda () (interactive)
388 (semantic-toggle-decoration-style
389 ,(car style)))
390 :style 'toggle
391 :selected `(semantic-decoration-style-enabled-p ,(car style))
392 ))
393
394 ;;;;###autoload
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
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
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 ;;; semantic/decorate/mode.el ends here
562