comparison lisp/cedet/semantic/idle.el @ 105260:bbd7017a25d9

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