Use define-minor-mode in CEDET where applicable.
[bpt/emacs.git] / lisp / cedet / semantic / idle.el
1 ;;; idle.el --- Schedule parsing tasks in idle time
2
3 ;; Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009, 2010
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 ;; Originally, `semantic-auto-parse-mode' handled refreshing the
27 ;; tags in a buffer in idle time. Other activities can be scheduled
28 ;; in idle time, all of which require up-to-date tag tables.
29 ;; Having a specialized idle time scheduler that first refreshes
30 ;; the tags buffer, and then enables other idle time tasks reduces
31 ;; the amount of work needed. Any specialized idle tasks need not
32 ;; ask for a fresh tags list.
33 ;;
34 ;; NOTE ON SEMANTIC_ANALYZE
35 ;;
36 ;; Some of the idle modes use the semantic analyzer. The analyzer
37 ;; automatically caches the created context, so it is shared amongst
38 ;; all idle modes that will need it.
39
40 (require 'semantic)
41 (require 'semantic/ctxt)
42 (require 'semantic/format)
43 (require 'semantic/tag)
44 (require 'timer)
45
46 ;; For the semantic-find-tags-by-name macro.
47 (eval-when-compile (require 'semantic/find))
48
49 (defvar eldoc-last-message)
50 (declare-function eldoc-message "eldoc")
51 (declare-function semantic-analyze-interesting-tag "semantic/analyze")
52 (declare-function semantic-complete-analyze-inline-idle "semantic/complete")
53 (declare-function semanticdb-deep-find-tags-by-name "semantic/db-find")
54 (declare-function semanticdb-save-all-db-idle "semantic/db")
55 (declare-function semanticdb-typecache-refresh-for-buffer "semantic/db-typecache")
56 (declare-function semantic-decorate-flush-pending-decorations
57 "semantic/decorate/mode")
58 (declare-function pulse-momentary-highlight-region "pulse")
59 (declare-function pulse-momentary-highlight-overlay "pulse")
60 (declare-function semantic-symref-hits-in-region "semantic/symref/filter")
61
62 ;;; Code:
63
64 ;;; TIMER RELATED FUNCTIONS
65 ;;
66 (defvar semantic-idle-scheduler-timer nil
67 "Timer used to schedule tasks in idle time.")
68
69 (defvar semantic-idle-scheduler-work-timer nil
70 "Timer used to schedule tasks in idle time that may take a while.")
71
72 (defcustom semantic-idle-scheduler-verbose-flag nil
73 "Non-nil means that the idle scheduler should provide debug messages.
74 Use this setting to debug idle activities."
75 :group 'semantic
76 :type 'boolean)
77
78 (defcustom semantic-idle-scheduler-idle-time 1
79 "Time in seconds of idle before scheduling events.
80 This time should be short enough to ensure that idle-scheduler will be
81 run as soon as Emacs is idle."
82 :group 'semantic
83 :type 'number
84 :set (lambda (sym val)
85 (set-default sym val)
86 (when (timerp semantic-idle-scheduler-timer)
87 (cancel-timer semantic-idle-scheduler-timer)
88 (setq semantic-idle-scheduler-timer nil)
89 (semantic-idle-scheduler-setup-timers))))
90
91 (defcustom semantic-idle-scheduler-work-idle-time 60
92 "Time in seconds of idle before scheduling big work.
93 This time should be long enough that once any big work is started, it is
94 unlikely the user would be ready to type again right away."
95 :group 'semantic
96 :type 'number
97 :set (lambda (sym val)
98 (set-default sym val)
99 (when (timerp semantic-idle-scheduler-timer)
100 (cancel-timer semantic-idle-scheduler-timer)
101 (setq semantic-idle-scheduler-timer nil)
102 (semantic-idle-scheduler-setup-timers))))
103
104 (defun semantic-idle-scheduler-setup-timers ()
105 "Lazy initialization of the auto parse idle timer."
106 ;; REFRESH THIS FUNCTION for XEMACS FOIBLES
107 (or (timerp semantic-idle-scheduler-timer)
108 (setq semantic-idle-scheduler-timer
109 (run-with-idle-timer
110 semantic-idle-scheduler-idle-time t
111 #'semantic-idle-scheduler-function)))
112 (or (timerp semantic-idle-scheduler-work-timer)
113 (setq semantic-idle-scheduler-work-timer
114 (run-with-idle-timer
115 semantic-idle-scheduler-work-idle-time t
116 #'semantic-idle-scheduler-work-function)))
117 )
118
119 (defun semantic-idle-scheduler-kill-timer ()
120 "Kill the auto parse idle timer."
121 (if (timerp semantic-idle-scheduler-timer)
122 (cancel-timer semantic-idle-scheduler-timer))
123 (setq semantic-idle-scheduler-timer nil))
124
125 \f
126 ;;; MINOR MODE
127 ;;
128 ;; The minor mode portion of this code just sets up the minor mode
129 ;; which does the initial scheduling of the idle timers.
130 ;;
131
132 (defcustom semantic-idle-scheduler-mode-hook nil
133 "Hook run at the end of the function `semantic-idle-scheduler-mode'."
134 :group 'semantic
135 :type 'hook)
136
137 (defvar semantic-idle-scheduler-mode nil
138 "Non-nil if idle-scheduler minor mode is enabled.
139 Use the command `semantic-idle-scheduler-mode' to change this variable.")
140 (make-variable-buffer-local 'semantic-idle-scheduler-mode)
141
142 (defcustom semantic-idle-scheduler-max-buffer-size 0
143 "*Maximum size in bytes of buffers where idle-scheduler is enabled.
144 If this value is less than or equal to 0, idle-scheduler is enabled in
145 all buffers regardless of their size."
146 :group 'semantic
147 :type 'number)
148
149 (defsubst semantic-idle-scheduler-enabled-p ()
150 "Return non-nil if idle-scheduler is enabled for this buffer.
151 idle-scheduler is disabled when debugging or if the buffer size
152 exceeds the `semantic-idle-scheduler-max-buffer-size' threshold."
153 (and semantic-idle-scheduler-mode
154 (not (and (boundp 'semantic-debug-enabled)
155 semantic-debug-enabled))
156 (not semantic-lex-debug)
157 (or (<= semantic-idle-scheduler-max-buffer-size 0)
158 (< (buffer-size) semantic-idle-scheduler-max-buffer-size))))
159
160 ;;;###autoload
161 (define-minor-mode semantic-idle-scheduler-mode
162 "Minor mode to auto parse buffer following a change.
163 When this mode is off, a buffer is only rescanned for tokens when
164 some command requests the list of available tokens. When idle-scheduler
165 is enabled, Emacs periodically checks to see if the buffer is out of
166 date, and reparses while the user is idle (not typing.)
167
168 With prefix argument ARG, turn on if positive, otherwise off. The
169 minor mode can be turned on only if semantic feature is available and
170 the current buffer was set up for parsing. Return non-nil if the
171 minor mode is enabled."
172 nil nil nil
173 (if semantic-idle-scheduler-mode
174 (if (not (and (featurep 'semantic) (semantic-active-p)))
175 (progn
176 ;; Disable minor mode if semantic stuff not available
177 (setq semantic-idle-scheduler-mode nil)
178 (error "Buffer %s was not set up idle time scheduling"
179 (buffer-name)))
180 (semantic-idle-scheduler-setup-timers)))
181 (semantic-mode-line-update))
182
183 (semantic-add-minor-mode 'semantic-idle-scheduler-mode
184 "ARP")
185 \f
186 ;;; SERVICES services
187 ;;
188 ;; These are services for managing idle services.
189 ;;
190 (defvar semantic-idle-scheduler-queue nil
191 "List of functions to execute during idle time.
192 These functions will be called in the current buffer after that
193 buffer has had its tags made up to date. These functions
194 will not be called if there are errors parsing the
195 current buffer.")
196
197 (defun semantic-idle-scheduler-add (function)
198 "Schedule FUNCTION to occur during idle time."
199 (add-to-list 'semantic-idle-scheduler-queue function))
200
201 (defun semantic-idle-scheduler-remove (function)
202 "Unschedule FUNCTION to occur during idle time."
203 (setq semantic-idle-scheduler-queue
204 (delete function semantic-idle-scheduler-queue)))
205
206 ;;; IDLE Function
207 ;;
208 (defun semantic-idle-core-handler ()
209 "Core idle function that handles reparsing.
210 And also manages services that depend on tag values."
211 (when semantic-idle-scheduler-verbose-flag
212 (message "IDLE: Core handler..."))
213 (semantic-exit-on-input 'idle-timer
214 (let* ((inhibit-quit nil)
215 (buffers (delq (current-buffer)
216 (delq nil
217 (mapcar #'(lambda (b)
218 (and (buffer-file-name b)
219 b))
220 (buffer-list)))))
221 safe ;; This safe is not used, but could be.
222 others
223 mode)
224 (when (semantic-idle-scheduler-enabled-p)
225 (save-excursion
226 ;; First, reparse the current buffer.
227 (setq mode major-mode
228 safe (semantic-safe "Idle Parse Error: %S"
229 ;(error "Goofy error 1")
230 (semantic-idle-scheduler-refresh-tags)
231 )
232 )
233 ;; Now loop over other buffers with same major mode, trying to
234 ;; update them as well. Stop on keypress.
235 (dolist (b buffers)
236 (semantic-throw-on-input 'parsing-mode-buffers)
237 (with-current-buffer b
238 (if (eq major-mode mode)
239 (and (semantic-idle-scheduler-enabled-p)
240 (semantic-safe "Idle Parse Error: %S"
241 ;(error "Goofy error")
242 (semantic-idle-scheduler-refresh-tags)))
243 (push (current-buffer) others))))
244 (setq buffers others))
245 ;; If re-parse of current buffer completed, evaluate all other
246 ;; services. Stop on keypress.
247
248 ;; NOTE ON COMMENTED SAFE HERE
249 ;; We used to not execute the services if the buffer wsa
250 ;; unparseable. We now assume that they are lexically
251 ;; safe to do, because we have marked the buffer unparseable
252 ;; if there was a problem.
253 ;;(when safe
254 (dolist (service semantic-idle-scheduler-queue)
255 (save-excursion
256 (semantic-throw-on-input 'idle-queue)
257 (when semantic-idle-scheduler-verbose-flag
258 (message "IDLE: execture service %s..." service))
259 (semantic-safe (format "Idle Service Error %s: %%S" service)
260 (funcall service))
261 (when semantic-idle-scheduler-verbose-flag
262 (message "IDLE: execture service %s...done" service))
263 )))
264 ;;)
265 ;; Finally loop over remaining buffers, trying to update them as
266 ;; well. Stop on keypress.
267 (save-excursion
268 (dolist (b buffers)
269 (semantic-throw-on-input 'parsing-other-buffers)
270 (with-current-buffer b
271 (and (semantic-idle-scheduler-enabled-p)
272 (semantic-idle-scheduler-refresh-tags)))))
273 ))
274 (when semantic-idle-scheduler-verbose-flag
275 (message "IDLE: Core handler...done")))
276
277 (defun semantic-debug-idle-function ()
278 "Run the Semantic idle function with debugging turned on."
279 (interactive)
280 (let ((debug-on-error t))
281 (semantic-idle-core-handler)
282 ))
283
284 (defun semantic-idle-scheduler-function ()
285 "Function run when after `semantic-idle-scheduler-idle-time'.
286 This function will reparse the current buffer, and if successful,
287 call additional functions registered with the timer calls."
288 (when (zerop (recursion-depth))
289 (let ((debug-on-error nil))
290 (save-match-data (semantic-idle-core-handler))
291 )))
292
293 \f
294 ;;; WORK FUNCTION
295 ;;
296 ;; Unlike the shorter timer, the WORK timer will kick of tasks that
297 ;; may take a long time to complete.
298 (defcustom semantic-idle-work-parse-neighboring-files-flag t
299 "*Non-nil means to parse files in the same dir as the current buffer.
300 Disable to prevent lots of excessive parsing in idle time."
301 :group 'semantic
302 :type 'boolean)
303
304
305 (defun semantic-idle-work-for-one-buffer (buffer)
306 "Do long-processing work for BUFFER.
307 Uses `semantic-safe' and returns the output.
308 Returns t if all processing succeeded."
309 (with-current-buffer buffer
310 (not (and
311 ;; Just in case
312 (semantic-safe "Idle Work Parse Error: %S"
313 (semantic-idle-scheduler-refresh-tags)
314 t)
315
316 ;; Force all our include files to get read in so we
317 ;; are ready to provide good smart completion and idle
318 ;; summary information
319 (semantic-safe "Idle Work Including Error: %S"
320 ;; Get the include related path.
321 (when (and (featurep 'semantic/db) (semanticdb-minor-mode-p))
322 (require 'semantic/db-find)
323 (semanticdb-find-translate-path buffer nil)
324 )
325 t)
326
327 ;; Pre-build the typecaches as needed.
328 (semantic-safe "Idle Work Typecaching Error: %S"
329 (when (featurep 'semantic/db-typecache)
330 (semanticdb-typecache-refresh-for-buffer buffer))
331 t)
332 ))
333 ))
334
335 (defun semantic-idle-work-core-handler ()
336 "Core handler for idle work processing of long running tasks.
337 Visits Semantic controlled buffers, and makes sure all needed
338 include files have been parsed, and that the typecache is up to date.
339 Uses `semantic-idle-work-for-on-buffer' to do the work."
340 (let ((errbuf nil)
341 (interrupted
342 (semantic-exit-on-input 'idle-work-timer
343 (let* ((inhibit-quit nil)
344 (cb (current-buffer))
345 (buffers (delq (current-buffer)
346 (delq nil
347 (mapcar #'(lambda (b)
348 (and (buffer-file-name b)
349 b))
350 (buffer-list)))))
351 safe errbuf)
352 ;; First, handle long tasks in the current buffer.
353 (when (semantic-idle-scheduler-enabled-p)
354 (save-excursion
355 (setq safe (semantic-idle-work-for-one-buffer (current-buffer))
356 )))
357 (when (not safe) (push (current-buffer) errbuf))
358
359 ;; Now loop over other buffers with same major mode, trying to
360 ;; update them as well. Stop on keypress.
361 (dolist (b buffers)
362 (semantic-throw-on-input 'parsing-mode-buffers)
363 (with-current-buffer b
364 (when (semantic-idle-scheduler-enabled-p)
365 (and (semantic-idle-scheduler-enabled-p)
366 (unless (semantic-idle-work-for-one-buffer (current-buffer))
367 (push (current-buffer) errbuf)))
368 ))
369 )
370
371 (when (and (featurep 'semantic/db) (semanticdb-minor-mode-p))
372 ;; Save everything.
373 (semanticdb-save-all-db-idle)
374
375 ;; Parse up files near our active buffer
376 (when semantic-idle-work-parse-neighboring-files-flag
377 (semantic-safe "Idle Work Parse Neighboring Files: %S"
378 (set-buffer cb)
379 (semantic-idle-scheduler-work-parse-neighboring-files))
380 t)
381
382 ;; Save everything... again
383 (semanticdb-save-all-db-idle)
384 )
385
386 ;; Done w/ processing
387 nil))))
388
389 ;; Done
390 (if interrupted
391 "Interrupted"
392 (cond ((not errbuf)
393 "done")
394 ((not (cdr errbuf))
395 (format "done with 1 error in %s" (car errbuf)))
396 (t
397 (format "done with errors in %d buffers."
398 (length errbuf)))))))
399
400 (defun semantic-debug-idle-work-function ()
401 "Run the Semantic idle work function with debugging turned on."
402 (interactive)
403 (let ((debug-on-error t))
404 (semantic-idle-work-core-handler)
405 ))
406
407 (defun semantic-idle-scheduler-work-function ()
408 "Function run when after `semantic-idle-scheduler-work-idle-time'.
409 This routine handles difficult tasks that require a lot of parsing, such as
410 parsing all the header files used by our active sources, or building up complex
411 datasets."
412 (when semantic-idle-scheduler-verbose-flag
413 (message "Long Work Idle Timer..."))
414 (let ((exit-type (save-match-data
415 (semantic-idle-work-core-handler))))
416 (when semantic-idle-scheduler-verbose-flag
417 (message "Long Work Idle Timer...%s" exit-type)))
418 )
419
420 (defun semantic-idle-scheduler-work-parse-neighboring-files ()
421 "Parse all the files in similar directories to buffers being edited."
422 ;; Lets check to see if EDE matters.
423 (let ((ede-auto-add-method 'never))
424 (dolist (a auto-mode-alist)
425 (when (eq (cdr a) major-mode)
426 (dolist (file (directory-files default-directory t (car a) t))
427 (semantic-throw-on-input 'parsing-mode-buffers)
428 (save-excursion
429 (semanticdb-file-table-object file)
430 ))))
431 ))
432
433 \f
434 ;;; REPARSING
435 ;;
436 ;; Reparsing is installed as semantic idle service.
437 ;; This part ALWAYS happens, and other services occur
438 ;; afterwards.
439
440 (defvar semantic-before-idle-scheduler-reparse-hook nil
441 "Hook run before option `semantic-idle-scheduler' begins parsing.
442 If any hook function throws an error, this variable is reset to nil.
443 This hook is not protected from lexical errors.")
444
445 (defvar semantic-after-idle-scheduler-reparse-hook nil
446 "Hook run after option `semantic-idle-scheduler' has parsed.
447 If any hook function throws an error, this variable is reset to nil.
448 This hook is not protected from lexical errors.")
449
450 (semantic-varalias-obsolete 'semantic-before-idle-scheduler-reparse-hooks
451 'semantic-before-idle-scheduler-reparse-hook "23.2")
452 (semantic-varalias-obsolete 'semantic-after-idle-scheduler-reparse-hooks
453 'semantic-after-idle-scheduler-reparse-hook "23.2")
454
455 (defun semantic-idle-scheduler-refresh-tags ()
456 "Refreshes the current buffer's tags.
457 This is called by `semantic-idle-scheduler-function' to update the
458 tags in the current buffer.
459
460 Return non-nil if the refresh was successful.
461 Return nil if there is some sort of syntax error preventing a full
462 reparse.
463
464 Does nothing if the current buffer doesn't need reparsing."
465
466 (prog1
467 ;; These checks actually occur in `semantic-fetch-tags', but if we
468 ;; do them here, then all the bovination hooks are not run, and
469 ;; we save lots of time.
470 (cond
471 ;; If the buffer was previously marked unparseable,
472 ;; then don't waste our time.
473 ((semantic-parse-tree-unparseable-p)
474 nil)
475 ;; The parse tree is already ok.
476 ((semantic-parse-tree-up-to-date-p)
477 t)
478 (t
479 ;; If the buffer might need a reparse and it is safe to do so,
480 ;; give it a try.
481 (let* (;(semantic-working-type nil)
482 (inhibit-quit nil)
483 ;; (working-use-echo-area-p
484 ;; (not semantic-idle-scheduler-working-in-modeline-flag))
485 ;; (working-status-dynamic-type
486 ;; (if semantic-idle-scheduler-no-working-message
487 ;; nil
488 ;; working-status-dynamic-type))
489 ;; (working-status-percentage-type
490 ;; (if semantic-idle-scheduler-no-working-message
491 ;; nil
492 ;; working-status-percentage-type))
493 (lexically-safe t)
494 )
495 ;; Let people hook into this, but don't let them hose
496 ;; us over!
497 (condition-case nil
498 (run-hooks 'semantic-before-idle-scheduler-reparse-hook)
499 (error (setq semantic-before-idle-scheduler-reparse-hook nil)))
500
501 (unwind-protect
502 ;; Perform the parsing.
503 (progn
504 (when semantic-idle-scheduler-verbose-flag
505 (message "IDLE: reparse %s..." (buffer-name)))
506 (when (semantic-lex-catch-errors idle-scheduler
507 (save-excursion (semantic-fetch-tags))
508 nil)
509 ;; If we are here, it is because the lexical step failed,
510 ;; proably due to unterminated lists or something like that.
511
512 ;; We do nothing, and just wait for the next idle timer
513 ;; to go off. In the meantime, remember this, and make sure
514 ;; no other idle services can get executed.
515 (setq lexically-safe nil))
516 (when semantic-idle-scheduler-verbose-flag
517 (message "IDLE: reparse %s...done" (buffer-name))))
518 ;; Let people hook into this, but don't let them hose
519 ;; us over!
520 (condition-case nil
521 (run-hooks 'semantic-after-idle-scheduler-reparse-hook)
522 (error (setq semantic-after-idle-scheduler-reparse-hook nil))))
523 ;; Return if we are lexically safe (from prog1)
524 lexically-safe)))
525
526 ;; After updating the tags, handle any pending decorations for this
527 ;; buffer.
528 (require 'semantic/decorate/mode)
529 (semantic-decorate-flush-pending-decorations (current-buffer))
530 ))
531
532 \f
533 ;;; IDLE SERVICES
534 ;;
535 ;; Idle Services are minor modes which enable or disable a services in
536 ;; the idle scheduler. Creating a new services only requires calling
537 ;; `semantic-create-idle-services' which does all the setup
538 ;; needed to create the minor mode that will enable or disable
539 ;; a services. The services must provide a single function.
540
541 ;; FIXME doc is incomplete.
542 (defmacro define-semantic-idle-service (name doc &rest forms)
543 "Create a new idle services with NAME.
544 DOC will be a documentation string describing FORMS.
545 FORMS will be called during idle time after the current buffer's
546 semantic tag information has been updated.
547 This routine creates the following functions and variables:"
548 (let ((global (intern (concat "global-" (symbol-name name) "-mode")))
549 (mode (intern (concat (symbol-name name) "-mode")))
550 (hook (intern (concat (symbol-name name) "-mode-hook")))
551 (map (intern (concat (symbol-name name) "-mode-map")))
552 (func (intern (concat (symbol-name name) "-idle-function"))))
553
554 `(eval-and-compile
555 (define-minor-mode ,global
556 ,(concat "Toggle " (symbol-name global) ".
557 With ARG, turn the minor mode on if ARG is positive, off otherwise.
558
559 When this minor mode is enabled, `" (symbol-name mode) "' is
560 turned on in every Semantic-supported buffer.")
561 :global t
562 :group 'semantic
563 :group 'semantic-modes
564 :require 'semantic/idle
565 (semantic-toggle-minor-mode-globally
566 ',mode (if ,global 1 -1)))
567
568 ;; FIXME: Get rid of this when define-minor-mode does it for us.
569 (defcustom ,hook nil
570 ,(concat "Hook run at the end of function `" (symbol-name mode) "'.")
571 :group 'semantic
572 :type 'hook)
573
574 (defvar ,map
575 (let ((km (make-sparse-keymap)))
576 km)
577 ,(concat "Keymap for `" (symbol-name mode) "'."))
578
579 (define-minor-mode ,mode
580 ,doc
581 :keymap ,map
582 (if ,mode
583 (if (not (and (featurep 'semantic) (semantic-active-p)))
584 (progn
585 ;; Disable minor mode if semantic stuff not available
586 (setq ,mode nil)
587 (error "Buffer %s was not set up for parsing"
588 (buffer-name)))
589 ;; Enable the mode mode
590 (semantic-idle-scheduler-add #',func))
591 ;; Disable the mode mode
592 (semantic-idle-scheduler-remove #',func))
593 (semantic-mode-line-update))
594
595 (semantic-add-minor-mode ',mode
596 "") ; idle schedulers are quiet?
597
598 (defun ,func ()
599 ,(concat "Perform idle activity for the minor mode `"
600 (symbol-name mode) "'.")
601 ,@forms))))
602 (put 'define-semantic-idle-service 'lisp-indent-function 1)
603
604 \f
605 ;;; SUMMARY MODE
606 ;;
607 ;; A mode similar to eldoc using semantic
608
609 (defcustom semantic-idle-summary-function
610 'semantic-format-tag-summarize-with-file
611 "Function to call when displaying tag information during idle time.
612 This function should take a single argument, a Semantic tag, and
613 return a string to display.
614 Some useful functions are found in `semantic-format-tag-functions'."
615 :group 'semantic
616 :type semantic-format-tag-custom-list)
617
618 (defsubst semantic-idle-summary-find-current-symbol-tag (sym)
619 "Search for a semantic tag with name SYM in database tables.
620 Return the tag found or nil if not found.
621 If semanticdb is not in use, use the current buffer only."
622 (car (if (and (featurep 'semantic/db)
623 semanticdb-current-database
624 (require 'semantic/db-find))
625 (cdar (semanticdb-deep-find-tags-by-name sym))
626 (semantic-deep-find-tags-by-name sym (current-buffer)))))
627
628 (defun semantic-idle-summary-current-symbol-info-brutish ()
629 "Return a string message describing the current context.
630 Gets a symbol with `semantic-ctxt-current-thing' and then
631 tries to find it with a deep targeted search."
632 ;; Try the current "thing".
633 (let ((sym (car (semantic-ctxt-current-thing))))
634 (when sym
635 (semantic-idle-summary-find-current-symbol-tag sym))))
636
637 (defun semantic-idle-summary-current-symbol-keyword ()
638 "Return a string message describing the current symbol.
639 Returns a value only if it is a keyword."
640 ;; Try the current "thing".
641 (let ((sym (car (semantic-ctxt-current-thing))))
642 (if (and sym (semantic-lex-keyword-p sym))
643 (semantic-lex-keyword-get sym 'summary))))
644
645 (defun semantic-idle-summary-current-symbol-info-context ()
646 "Return a string message describing the current context.
647 Use the semantic analyzer to find the symbol information."
648 (let ((analysis (condition-case nil
649 (semantic-analyze-current-context (point))
650 (error nil))))
651 (when analysis
652 (require 'semantic/analyze)
653 (semantic-analyze-interesting-tag analysis))))
654
655 (defun semantic-idle-summary-current-symbol-info-default ()
656 "Return a string message describing the current context.
657 This function will disable loading of previously unloaded files
658 by semanticdb as a time-saving measure."
659 (let (
660 (semanticdb-find-default-throttle
661 (if (featurep 'semantic/db-find)
662 (remq 'unloaded semanticdb-find-default-throttle)
663 nil))
664 )
665 (save-excursion
666 ;; use whicever has success first.
667 (or
668 (semantic-idle-summary-current-symbol-keyword)
669
670 (semantic-idle-summary-current-symbol-info-context)
671
672 (semantic-idle-summary-current-symbol-info-brutish)
673 ))))
674
675 (defvar semantic-idle-summary-out-of-context-faces
676 '(
677 font-lock-comment-face
678 font-lock-string-face
679 font-lock-doc-string-face ; XEmacs.
680 font-lock-doc-face ; Emacs 21 and later.
681 )
682 "List of font-lock faces that indicate a useless summary context.
683 Those are generally faces used to highlight comments.
684
685 It might be useful to override this variable to add comment faces
686 specific to a major mode. For example, in jde mode:
687
688 \(defvar-mode-local jde-mode semantic-idle-summary-out-of-context-faces
689 (append (default-value 'semantic-idle-summary-out-of-context-faces)
690 '(jde-java-font-lock-doc-tag-face
691 jde-java-font-lock-link-face
692 jde-java-font-lock-bold-face
693 jde-java-font-lock-underline-face
694 jde-java-font-lock-pre-face
695 jde-java-font-lock-code-face)))")
696
697 (defun semantic-idle-summary-useful-context-p ()
698 "Non-nil if we should show a summary based on context."
699 (if (and (boundp 'font-lock-mode)
700 font-lock-mode
701 (memq (get-text-property (point) 'face)
702 semantic-idle-summary-out-of-context-faces))
703 ;; The best I can think of at the moment is to disable
704 ;; in comments by detecting with font-lock.
705 nil
706 t))
707
708 (define-overloadable-function semantic-idle-summary-current-symbol-info ()
709 "Return a string message describing the current context.")
710
711 (make-obsolete-overload 'semantic-eldoc-current-symbol-info
712 'semantic-idle-summary-current-symbol-info
713 "23.2")
714
715 (defcustom semantic-idle-summary-mode-hook nil
716 "Hook run at the end of `semantic-idle-summary'."
717 :group 'semantic
718 :type 'hook)
719
720 (defun semantic-idle-summary-idle-function ()
721 "Display a tag summary of the lexical token under the cursor.
722 Call `semantic-idle-summary-current-symbol-info' for getting the
723 current tag to display information."
724 (or (eq major-mode 'emacs-lisp-mode)
725 (not (semantic-idle-summary-useful-context-p))
726 (let* ((found (semantic-idle-summary-current-symbol-info))
727 (str (cond ((stringp found) found)
728 ((semantic-tag-p found)
729 (funcall semantic-idle-summary-function
730 found nil t)))))
731 ;; Show the message with eldoc functions
732 (unless (and str (boundp 'eldoc-echo-area-use-multiline-p)
733 eldoc-echo-area-use-multiline-p)
734 (let ((w (1- (window-width (minibuffer-window)))))
735 (if (> (length str) w)
736 (setq str (substring str 0 w)))))
737 (eldoc-message str))))
738
739 (define-minor-mode semantic-idle-summary-mode
740 "Toggle Semantic Idle Summary mode.
741 With ARG, turn Semantic Idle Summary mode on if ARG is positive,
742 off otherwise.
743
744 When this minor mode is enabled, the echo area displays a summary
745 of the lexical token at point whenever Emacs is idle."
746 :group 'semantic
747 :group 'semantic-modes
748 (if semantic-idle-summary-mode
749 ;; Enable the mode
750 (progn
751 (unless (and (featurep 'semantic) (semantic-active-p))
752 ;; Disable minor mode if semantic stuff not available
753 (setq semantic-idle-summary-mode nil)
754 (error "Buffer %s was not set up for parsing"
755 (buffer-name)))
756 (require 'eldoc)
757 (semantic-idle-scheduler-add 'semantic-idle-summary-idle-function)
758 (add-hook 'pre-command-hook 'semantic-idle-summary-refresh-echo-area t))
759 ;; Disable the mode
760 (semantic-idle-scheduler-remove 'semantic-idle-summary-idle-function)
761 (remove-hook 'pre-command-hook 'semantic-idle-summary-refresh-echo-area t))
762 (semantic-mode-line-update))
763
764 (defun semantic-idle-summary-refresh-echo-area ()
765 (and semantic-idle-summary-mode
766 eldoc-last-message
767 (if (and (not executing-kbd-macro)
768 (not (and (boundp 'edebug-active) edebug-active))
769 (not cursor-in-echo-area)
770 (not (eq (selected-window) (minibuffer-window))))
771 (eldoc-message eldoc-last-message)
772 (setq eldoc-last-message nil))))
773
774 (semantic-add-minor-mode 'semantic-idle-summary-mode "")
775
776 (define-minor-mode global-semantic-idle-summary-mode
777 "Toggle Global Semantic Idle Summary mode.
778 With ARG, turn Global Semantic Idle Summary mode on if ARG is
779 positive, off otherwise.
780
781 When this minor mode is enabled, `semantic-idle-summary-mode' is
782 turned on in every Semantic-supported buffer."
783 :global t
784 :group 'semantic
785 :group 'semantic-modes
786 (semantic-toggle-minor-mode-globally
787 'semantic-idle-summary-mode
788 (if global-semantic-idle-summary-mode 1 -1)))
789
790 \f
791 ;;; Current symbol highlight
792 ;;
793 ;; This mode will use context analysis to perform highlighting
794 ;; of all uses of the symbol that is under the cursor.
795 ;;
796 ;; This is to mimic the Eclipse tool of a similar nature.
797 (defvar semantic-idle-summary-highlight-face 'region
798 "Face used for the summary highlight.")
799
800 (defun semantic-idle-summary-maybe-highlight (tag)
801 "Perhaps add highlighting onto TAG.
802 TAG was found as the thing under point. If it happens to be
803 visible, then highlight it."
804 (require 'pulse)
805 (let* ((region (when (and (semantic-tag-p tag)
806 (semantic-tag-with-position-p tag))
807 (semantic-tag-overlay tag)))
808 (file (when (and (semantic-tag-p tag)
809 (semantic-tag-with-position-p tag))
810 (semantic-tag-file-name tag)))
811 (buffer (when file (get-file-buffer file)))
812 ;; We use pulse, but we don't want the flashy version,
813 ;; just the stable version.
814 (pulse-flag nil)
815 )
816 (cond ((semantic-overlay-p region)
817 (with-current-buffer (semantic-overlay-buffer region)
818 (goto-char (semantic-overlay-start region))
819 (when (pos-visible-in-window-p
820 (point) (get-buffer-window (current-buffer) 'visible))
821 (if (< (semantic-overlay-end region) (point-at-eol))
822 (pulse-momentary-highlight-overlay
823 region semantic-idle-summary-highlight-face)
824 ;; Not the same
825 (pulse-momentary-highlight-region
826 (semantic-overlay-start region)
827 (point-at-eol)
828 semantic-idle-summary-highlight-face)))
829 ))
830 ((vectorp region)
831 (let ((start (aref region 0))
832 (end (aref region 1)))
833 (save-excursion
834 (when buffer (set-buffer buffer))
835 ;; As a vector, we have no filename. Perhaps it is a
836 ;; local variable?
837 (when (and (<= end (point-max))
838 (pos-visible-in-window-p
839 start (get-buffer-window (current-buffer) 'visible)))
840 (goto-char start)
841 (when (re-search-forward
842 (regexp-quote (semantic-tag-name tag))
843 end t)
844 ;; This is likely it, give it a try.
845 (pulse-momentary-highlight-region
846 start (if (<= end (point-at-eol)) end
847 (point-at-eol))
848 semantic-idle-summary-highlight-face)))
849 ))))
850 nil))
851
852 (define-semantic-idle-service semantic-idle-tag-highlight
853 "Highlight the tag, and references of the symbol under point.
854 Call `semantic-analyze-current-context' to find the reference tag.
855 Call `semantic-symref-hits-in-region' to identify local references."
856 (require 'pulse)
857 (when (semantic-idle-summary-useful-context-p)
858 (let* ((ctxt (semantic-analyze-current-context))
859 (Hbounds (when ctxt (oref ctxt bounds)))
860 (target (when ctxt (car (reverse (oref ctxt prefix)))))
861 (tag (semantic-current-tag))
862 ;; We use pulse, but we don't want the flashy version,
863 ;; just the stable version.
864 (pulse-flag nil))
865 (when ctxt
866 ;; Highlight the original tag? Protect against problems.
867 (condition-case nil
868 (semantic-idle-summary-maybe-highlight target)
869 (error nil))
870 ;; Identify all hits in this current tag.
871 (when (semantic-tag-p target)
872 (require 'semantic/symref/filter)
873 (semantic-symref-hits-in-region
874 target (lambda (start end prefix)
875 (when (/= start (car Hbounds))
876 (pulse-momentary-highlight-region
877 start end semantic-idle-summary-highlight-face))
878 (semantic-throw-on-input 'symref-highlight)
879 )
880 (semantic-tag-start tag)
881 (semantic-tag-end tag)))
882 ))))
883
884 \f
885 ;;;###autoload
886 (define-minor-mode global-semantic-idle-scheduler-mode
887 "Toggle global use of option `semantic-idle-scheduler-mode'.
888 The idle scheduler will automatically reparse buffers in idle time,
889 and then schedule other jobs setup with `semantic-idle-scheduler-add'.
890 If ARG is positive or nil, enable, if it is negative, disable."
891 :global t
892 :group 'semantic
893 :group 'semantic-modes
894 ;; When turning off, disable other idle modes.
895 (when (null global-semantic-idle-scheduler-mode)
896 (global-semantic-idle-summary-mode -1)
897 (global-semantic-idle-tag-highlight-mode -1)
898 (global-semantic-idle-completions-mode -1))
899 (semantic-toggle-minor-mode-globally
900 'semantic-idle-scheduler-mode
901 (if global-semantic-idle-scheduler-mode 1 -1)))
902
903 \f
904 ;;; Completion Popup Mode
905 ;;
906 ;; This mode uses tooltips to display a (hopefully) short list of possible
907 ;; completions available for the text under point. It provides
908 ;; NO provision for actually filling in the values from those completions.
909
910 (defun semantic-idle-completion-list-default ()
911 "Calculate and display a list of completions."
912 (when (semantic-idle-summary-useful-context-p)
913 ;; This mode can be fragile. Ignore problems.
914 ;; If something doesn't do what you expect, run
915 ;; the below command by hand instead.
916 (condition-case nil
917 (let (
918 ;; Don't go loading in oodles of header libraries in
919 ;; IDLE time.
920 (semanticdb-find-default-throttle
921 (if (featurep 'semantic/db-find)
922 (remq 'unloaded semanticdb-find-default-throttle)
923 nil))
924 )
925 ;; Use idle version.
926 (require 'semantic/complete)
927 (semantic-complete-analyze-inline-idle)
928 )
929 (error nil))
930 ))
931
932 (define-semantic-idle-service semantic-idle-completions
933 "Toggle Semantic Idle Completions mode.
934 With ARG, turn Semantic Idle Completions mode on if ARG is
935 positive, off otherwise.
936
937 This minor mode only takes effect if Semantic is active and
938 `semantic-idle-scheduler-mode' is enabled.
939
940 When enabled, Emacs displays a list of possible completions at
941 idle time. The method for displaying completions is given by
942 `semantic-complete-inline-analyzer-idle-displayor-class'; the
943 default is to show completions inline.
944
945 While a completion is displayed, RET accepts the completion; M-n
946 and M-p cycle through completion alternatives; TAB attempts to
947 complete as far as possible, and cycles if no additional
948 completion is possible; and any other command cancels the
949 completion.
950
951 \\{semantic-complete-inline-map}"
952 ;; Add the ability to override sometime.
953 (semantic-idle-completion-list-default))
954
955 (provide 'semantic/idle)
956
957 ;; Local variables:
958 ;; generated-autoload-file: "loaddefs.el"
959 ;; generated-autoload-load-name: "semantic/idle"
960 ;; End:
961
962 ;; arch-tag: 4bfd54da-5023-4cc1-91ae-e1fefc1a8d1b
963 ;;; semantic-idle.el ends here