* cedet/srecode/map.el (srecode-get-maps):
[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
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 ;;;###autoload
132 (defcustom global-semantic-idle-scheduler-mode nil
133 "*If non-nil, enable global use of idle-scheduler mode."
134 :group 'semantic
135 :group 'semantic-modes
136 :type 'boolean
137 :require 'semantic/idle
138 :initialize 'custom-initialize-default
139 :set (lambda (sym val)
140 (global-semantic-idle-scheduler-mode (if val 1 -1))))
141
142 ;;;###autoload
143 (defun global-semantic-idle-scheduler-mode (&optional arg)
144 "Toggle global use of option `semantic-idle-scheduler-mode'.
145 The idle scheduler with automatically reparse buffers in idle time,
146 and then schedule other jobs setup with `semantic-idle-scheduler-add'.
147 If ARG is positive, enable, if it is negative, disable.
148 If ARG is nil, then toggle."
149 (interactive "P")
150 ;; When turning off, disable other idle modes.
151 (when (or (and (numberp arg) (< arg 0))
152 (and (null arg) global-semantic-idle-scheduler-mode))
153 (global-semantic-idle-summary-mode -1)
154 (global-semantic-idle-tag-highlight-mode -1)
155 (global-semantic-idle-completions-mode -1))
156 (setq global-semantic-idle-scheduler-mode
157 (semantic-toggle-minor-mode-globally
158 'semantic-idle-scheduler-mode arg)))
159
160 (defcustom semantic-idle-scheduler-mode-hook nil
161 "Hook run at the end of the function `semantic-idle-scheduler-mode'."
162 :group 'semantic
163 :type 'hook)
164
165 (defvar semantic-idle-scheduler-mode nil
166 "Non-nil if idle-scheduler minor mode is enabled.
167 Use the command `semantic-idle-scheduler-mode' to change this variable.")
168 (make-variable-buffer-local 'semantic-idle-scheduler-mode)
169
170 (defcustom semantic-idle-scheduler-max-buffer-size 0
171 "*Maximum size in bytes of buffers where idle-scheduler is enabled.
172 If this value is less than or equal to 0, idle-scheduler is enabled in
173 all buffers regardless of their size."
174 :group 'semantic
175 :type 'number)
176
177 (defsubst semantic-idle-scheduler-enabled-p ()
178 "Return non-nil if idle-scheduler is enabled for this buffer.
179 idle-scheduler is disabled when debugging or if the buffer size
180 exceeds the `semantic-idle-scheduler-max-buffer-size' threshold."
181 (and semantic-idle-scheduler-mode
182 (not (and (boundp 'semantic-debug-enabled)
183 semantic-debug-enabled))
184 (not semantic-lex-debug)
185 (or (<= semantic-idle-scheduler-max-buffer-size 0)
186 (< (buffer-size) semantic-idle-scheduler-max-buffer-size))))
187
188 (defun semantic-idle-scheduler-mode-setup ()
189 "Setup option `semantic-idle-scheduler-mode'.
190 The minor mode can be turned on only if semantic feature is available
191 and the current buffer was set up for parsing. When minor mode is
192 enabled parse the current buffer if needed. Return non-nil if the
193 minor mode is enabled."
194 (if semantic-idle-scheduler-mode
195 (if (not (and (featurep 'semantic) (semantic-active-p)))
196 (progn
197 ;; Disable minor mode if semantic stuff not available
198 (setq semantic-idle-scheduler-mode nil)
199 (error "Buffer %s was not set up idle time scheduling"
200 (buffer-name)))
201 (semantic-idle-scheduler-setup-timers)))
202 semantic-idle-scheduler-mode)
203
204 ;;;###autoload
205 (defun semantic-idle-scheduler-mode (&optional arg)
206 "Minor mode to auto parse buffer following a change.
207 When this mode is off, a buffer is only rescanned for tokens when
208 some command requests the list of available tokens. When idle-scheduler
209 is enabled, Emacs periodically checks to see if the buffer is out of
210 date, and reparses while the user is idle (not typing.)
211
212 With prefix argument ARG, turn on if positive, otherwise off. The
213 minor mode can be turned on only if semantic feature is available and
214 the current buffer was set up for parsing. Return non-nil if the
215 minor mode is enabled."
216 (interactive
217 (list (or current-prefix-arg
218 (if semantic-idle-scheduler-mode 0 1))))
219 (setq semantic-idle-scheduler-mode
220 (if arg
221 (>
222 (prefix-numeric-value arg)
223 0)
224 (not semantic-idle-scheduler-mode)))
225 (semantic-idle-scheduler-mode-setup)
226 (run-hooks 'semantic-idle-scheduler-mode-hook)
227 (if (called-interactively-p 'interactive)
228 (message "idle-scheduler minor mode %sabled"
229 (if semantic-idle-scheduler-mode "en" "dis")))
230 (semantic-mode-line-update)
231 semantic-idle-scheduler-mode)
232
233 (semantic-add-minor-mode 'semantic-idle-scheduler-mode
234 "ARP"
235 nil)
236 \f
237 ;;; SERVICES services
238 ;;
239 ;; These are services for managing idle services.
240 ;;
241 (defvar semantic-idle-scheduler-queue nil
242 "List of functions to execute during idle time.
243 These functions will be called in the current buffer after that
244 buffer has had its tags made up to date. These functions
245 will not be called if there are errors parsing the
246 current buffer.")
247
248 (defun semantic-idle-scheduler-add (function)
249 "Schedule FUNCTION to occur during idle time."
250 (add-to-list 'semantic-idle-scheduler-queue function))
251
252 (defun semantic-idle-scheduler-remove (function)
253 "Unschedule FUNCTION to occur during idle time."
254 (setq semantic-idle-scheduler-queue
255 (delete function semantic-idle-scheduler-queue)))
256
257 ;;; IDLE Function
258 ;;
259 (defun semantic-idle-core-handler ()
260 "Core idle function that handles reparsing.
261 And also manages services that depend on tag values."
262 (when semantic-idle-scheduler-verbose-flag
263 (message "IDLE: Core handler..."))
264 (semantic-exit-on-input 'idle-timer
265 (let* ((inhibit-quit nil)
266 (buffers (delq (current-buffer)
267 (delq nil
268 (mapcar #'(lambda (b)
269 (and (buffer-file-name b)
270 b))
271 (buffer-list)))))
272 safe ;; This safe is not used, but could be.
273 others
274 mode)
275 (when (semantic-idle-scheduler-enabled-p)
276 (save-excursion
277 ;; First, reparse the current buffer.
278 (setq mode major-mode
279 safe (semantic-safe "Idle Parse Error: %S"
280 ;(error "Goofy error 1")
281 (semantic-idle-scheduler-refresh-tags)
282 )
283 )
284 ;; Now loop over other buffers with same major mode, trying to
285 ;; update them as well. Stop on keypress.
286 (dolist (b buffers)
287 (semantic-throw-on-input 'parsing-mode-buffers)
288 (with-current-buffer b
289 (if (eq major-mode mode)
290 (and (semantic-idle-scheduler-enabled-p)
291 (semantic-safe "Idle Parse Error: %S"
292 ;(error "Goofy error")
293 (semantic-idle-scheduler-refresh-tags)))
294 (push (current-buffer) others))))
295 (setq buffers others))
296 ;; If re-parse of current buffer completed, evaluate all other
297 ;; services. Stop on keypress.
298
299 ;; NOTE ON COMMENTED SAFE HERE
300 ;; We used to not execute the services if the buffer wsa
301 ;; unparseable. We now assume that they are lexically
302 ;; safe to do, because we have marked the buffer unparseable
303 ;; if there was a problem.
304 ;;(when safe
305 (dolist (service semantic-idle-scheduler-queue)
306 (save-excursion
307 (semantic-throw-on-input 'idle-queue)
308 (when semantic-idle-scheduler-verbose-flag
309 (message "IDLE: execture service %s..." service))
310 (semantic-safe (format "Idle Service Error %s: %%S" service)
311 (funcall service))
312 (when semantic-idle-scheduler-verbose-flag
313 (message "IDLE: execture service %s...done" service))
314 )))
315 ;;)
316 ;; Finally loop over remaining buffers, trying to update them as
317 ;; well. Stop on keypress.
318 (save-excursion
319 (dolist (b buffers)
320 (semantic-throw-on-input 'parsing-other-buffers)
321 (with-current-buffer b
322 (and (semantic-idle-scheduler-enabled-p)
323 (semantic-idle-scheduler-refresh-tags)))))
324 ))
325 (when semantic-idle-scheduler-verbose-flag
326 (message "IDLE: Core handler...done")))
327
328 (defun semantic-debug-idle-function ()
329 "Run the Semantic idle function with debugging turned on."
330 (interactive)
331 (let ((debug-on-error t))
332 (semantic-idle-core-handler)
333 ))
334
335 (defun semantic-idle-scheduler-function ()
336 "Function run when after `semantic-idle-scheduler-idle-time'.
337 This function will reparse the current buffer, and if successful,
338 call additional functions registered with the timer calls."
339 (when (zerop (recursion-depth))
340 (let ((debug-on-error nil))
341 (save-match-data (semantic-idle-core-handler))
342 )))
343
344 \f
345 ;;; WORK FUNCTION
346 ;;
347 ;; Unlike the shorter timer, the WORK timer will kick of tasks that
348 ;; may take a long time to complete.
349 (defcustom semantic-idle-work-parse-neighboring-files-flag t
350 "*Non-nil means to parse files in the same dir as the current buffer.
351 Disable to prevent lots of excessive parsing in idle time."
352 :group 'semantic
353 :type 'boolean)
354
355
356 (defun semantic-idle-work-for-one-buffer (buffer)
357 "Do long-processing work for for BUFFER.
358 Uses `semantic-safe' and returns the output.
359 Returns t of all processing succeeded."
360 (with-current-buffer buffer
361 (not (and
362 ;; Just in case
363 (semantic-safe "Idle Work Parse Error: %S"
364 (semantic-idle-scheduler-refresh-tags)
365 t)
366
367 ;; Force all our include files to get read in so we
368 ;; are ready to provide good smart completion and idle
369 ;; summary information
370 (semantic-safe "Idle Work Including Error: %S"
371 ;; Get the include related path.
372 (when (and (featurep 'semantic/db) (semanticdb-minor-mode-p))
373 (require 'semantic/db-find)
374 (semanticdb-find-translate-path buffer nil)
375 )
376 t)
377
378 ;; Pre-build the typecaches as needed.
379 (semantic-safe "Idle Work Typecaching Error: %S"
380 (when (featurep 'semantic/db-typecache)
381 (semanticdb-typecache-refresh-for-buffer buffer))
382 t)
383 ))
384 ))
385
386 (defun semantic-idle-work-core-handler ()
387 "Core handler for idle work processing of long running tasks.
388 Visits semantic controlled buffers, and makes sure all needed
389 include files have been parsed, and that the typecache is up to date.
390 Uses `semantic-idle-work-for-on-buffer' to do the work."
391 (let ((errbuf nil)
392 (interrupted
393 (semantic-exit-on-input 'idle-work-timer
394 (let* ((inhibit-quit nil)
395 (cb (current-buffer))
396 (buffers (delq (current-buffer)
397 (delq nil
398 (mapcar #'(lambda (b)
399 (and (buffer-file-name b)
400 b))
401 (buffer-list)))))
402 safe errbuf)
403 ;; First, handle long tasks in the current buffer.
404 (when (semantic-idle-scheduler-enabled-p)
405 (save-excursion
406 (setq safe (semantic-idle-work-for-one-buffer (current-buffer))
407 )))
408 (when (not safe) (push (current-buffer) errbuf))
409
410 ;; Now loop over other buffers with same major mode, trying to
411 ;; update them as well. Stop on keypress.
412 (dolist (b buffers)
413 (semantic-throw-on-input 'parsing-mode-buffers)
414 (with-current-buffer b
415 (when (semantic-idle-scheduler-enabled-p)
416 (and (semantic-idle-scheduler-enabled-p)
417 (unless (semantic-idle-work-for-one-buffer (current-buffer))
418 (push (current-buffer) errbuf)))
419 ))
420 )
421
422 (when (and (featurep 'semantic/db) (semanticdb-minor-mode-p))
423 ;; Save everything.
424 (semanticdb-save-all-db-idle)
425
426 ;; Parse up files near our active buffer
427 (when semantic-idle-work-parse-neighboring-files-flag
428 (semantic-safe "Idle Work Parse Neighboring Files: %S"
429 (set-buffer cb)
430 (semantic-idle-scheduler-work-parse-neighboring-files))
431 t)
432
433 ;; Save everything... again
434 (semanticdb-save-all-db-idle)
435 )
436
437 ;; Done w/ processing
438 nil))))
439
440 ;; Done
441 (if interrupted
442 "Interrupted"
443 (cond ((not errbuf)
444 "done")
445 ((not (cdr errbuf))
446 (format "done with 1 error in %s" (car errbuf)))
447 (t
448 (format "done with errors in %d buffers."
449 (length errbuf)))))))
450
451 (defun semantic-debug-idle-work-function ()
452 "Run the Semantic idle work function with debugging turned on."
453 (interactive)
454 (let ((debug-on-error t))
455 (semantic-idle-work-core-handler)
456 ))
457
458 (defun semantic-idle-scheduler-work-function ()
459 "Function run when after `semantic-idle-scheduler-work-idle-time'.
460 This routine handles difficult tasks that require a lot of parsing, such as
461 parsing all the header files used by our active sources, or building up complex
462 datasets."
463 (when semantic-idle-scheduler-verbose-flag
464 (message "Long Work Idle Timer..."))
465 (let ((exit-type (save-match-data
466 (semantic-idle-work-core-handler))))
467 (when semantic-idle-scheduler-verbose-flag
468 (message "Long Work Idle Timer...%s" exit-type)))
469 )
470
471 (defun semantic-idle-scheduler-work-parse-neighboring-files ()
472 "Parse all the files in similar directories to buffers being edited."
473 ;; Lets check to see if EDE matters.
474 (let ((ede-auto-add-method 'never))
475 (dolist (a auto-mode-alist)
476 (when (eq (cdr a) major-mode)
477 (dolist (file (directory-files default-directory t (car a) t))
478 (semantic-throw-on-input 'parsing-mode-buffers)
479 (save-excursion
480 (semanticdb-file-table-object file)
481 ))))
482 ))
483
484 \f
485 ;;; REPARSING
486 ;;
487 ;; Reparsing is installed as semantic idle service.
488 ;; This part ALWAYS happens, and other services occur
489 ;; afterwards.
490
491 (defvar semantic-before-idle-scheduler-reparse-hook nil
492 "Hook run before option `semantic-idle-scheduler' begins parsing.
493 If any hook function throws an error, this variable is reset to nil.
494 This hook is not protected from lexical errors.")
495
496 (defvar semantic-after-idle-scheduler-reparse-hook nil
497 "Hook run after option `semantic-idle-scheduler' has parsed.
498 If any hook function throws an error, this variable is reset to nil.
499 This hook is not protected from lexical errors.")
500
501 (semantic-varalias-obsolete 'semantic-before-idle-scheduler-reparse-hooks
502 'semantic-before-idle-scheduler-reparse-hook "23.2")
503 (semantic-varalias-obsolete 'semantic-after-idle-scheduler-reparse-hooks
504 'semantic-after-idle-scheduler-reparse-hook "23.2")
505
506 (defun semantic-idle-scheduler-refresh-tags ()
507 "Refreshes the current buffer's tags.
508 This is called by `semantic-idle-scheduler-function' to update the
509 tags in the current buffer.
510
511 Return non-nil if the refresh was successful.
512 Return nil if there is some sort of syntax error preventing a full
513 reparse.
514
515 Does nothing if the current buffer doesn't need reparsing."
516
517 (prog1
518 ;; These checks actually occur in `semantic-fetch-tags', but if we
519 ;; do them here, then all the bovination hooks are not run, and
520 ;; we save lots of time.
521 (cond
522 ;; If the buffer was previously marked unparseable,
523 ;; then don't waste our time.
524 ((semantic-parse-tree-unparseable-p)
525 nil)
526 ;; The parse tree is already ok.
527 ((semantic-parse-tree-up-to-date-p)
528 t)
529 (t
530 ;; If the buffer might need a reparse and it is safe to do so,
531 ;; give it a try.
532 (let* (;(semantic-working-type nil)
533 (inhibit-quit nil)
534 ;; (working-use-echo-area-p
535 ;; (not semantic-idle-scheduler-working-in-modeline-flag))
536 ;; (working-status-dynamic-type
537 ;; (if semantic-idle-scheduler-no-working-message
538 ;; nil
539 ;; working-status-dynamic-type))
540 ;; (working-status-percentage-type
541 ;; (if semantic-idle-scheduler-no-working-message
542 ;; nil
543 ;; working-status-percentage-type))
544 (lexically-safe t)
545 )
546 ;; Let people hook into this, but don't let them hose
547 ;; us over!
548 (condition-case nil
549 (run-hooks 'semantic-before-idle-scheduler-reparse-hook)
550 (error (setq semantic-before-idle-scheduler-reparse-hook nil)))
551
552 (unwind-protect
553 ;; Perform the parsing.
554 (progn
555 (when semantic-idle-scheduler-verbose-flag
556 (message "IDLE: reparse %s..." (buffer-name)))
557 (when (semantic-lex-catch-errors idle-scheduler
558 (save-excursion (semantic-fetch-tags))
559 nil)
560 ;; If we are here, it is because the lexical step failed,
561 ;; proably due to unterminated lists or something like that.
562
563 ;; We do nothing, and just wait for the next idle timer
564 ;; to go off. In the meantime, remember this, and make sure
565 ;; no other idle services can get executed.
566 (setq lexically-safe nil))
567 (when semantic-idle-scheduler-verbose-flag
568 (message "IDLE: reparse %s...done" (buffer-name))))
569 ;; Let people hook into this, but don't let them hose
570 ;; us over!
571 (condition-case nil
572 (run-hooks 'semantic-after-idle-scheduler-reparse-hook)
573 (error (setq semantic-after-idle-scheduler-reparse-hook nil))))
574 ;; Return if we are lexically safe (from prog1)
575 lexically-safe)))
576
577 ;; After updating the tags, handle any pending decorations for this
578 ;; buffer.
579 (require 'semantic/decorate/mode)
580 (semantic-decorate-flush-pending-decorations (current-buffer))
581 ))
582
583 \f
584 ;;; IDLE SERVICES
585 ;;
586 ;; Idle Services are minor modes which enable or disable a services in
587 ;; the idle scheduler. Creating a new services only requires calling
588 ;; `semantic-create-idle-services' which does all the setup
589 ;; needed to create the minor mode that will enable or disable
590 ;; a services. The services must provide a single function.
591
592 (defmacro define-semantic-idle-service (name doc &rest forms)
593 "Create a new idle services with NAME.
594 DOC will be a documentation string describing FORMS.
595 FORMS will be called during idle time after the current buffer's
596 semantic tag information has been updated.
597 This routines creates the following functions and variables:"
598 (let ((global (intern (concat "global-" (symbol-name name) "-mode")))
599 (mode (intern (concat (symbol-name name) "-mode")))
600 (hook (intern (concat (symbol-name name) "-mode-hook")))
601 (map (intern (concat (symbol-name name) "-mode-map")))
602 (setup (intern (concat (symbol-name name) "-mode-setup")))
603 (func (intern (concat (symbol-name name) "-idle-function"))))
604
605 `(eval-and-compile
606 (defun ,global (&optional arg)
607 ,(concat "Toggle " (symbol-name global) ".
608 With ARG, turn the minor mode on if ARG is positive, off otherwise.
609
610 When this minor mode is enabled, `" (symbol-name mode) "' is
611 turned on in every Semantic-supported buffer.")
612 (interactive "P")
613 (setq ,global
614 (semantic-toggle-minor-mode-globally
615 ',mode arg)))
616
617 (defcustom ,global nil
618 ,(concat "Non-nil if `" (symbol-name mode) "' is enabled.")
619 :group 'semantic
620 :group 'semantic-modes
621 :type 'boolean
622 :require 'semantic/idle
623 :initialize 'custom-initialize-default
624 :set (lambda (sym val)
625 (,global (if val 1 -1))))
626
627 (defcustom ,hook nil
628 ,(concat "Hook run at the end of function `" (symbol-name mode) "'.")
629 :group 'semantic
630 :type 'hook)
631
632 (defvar ,map
633 (let ((km (make-sparse-keymap)))
634 km)
635 ,(concat "Keymap for `" (symbol-name mode) "'."))
636
637 (defvar ,mode nil
638 ,(concat "Non-nil if the minor mode `" (symbol-name mode) "' is enabled.
639 Use the command `" (symbol-name mode) "' to change this variable."))
640 (make-variable-buffer-local ',mode)
641
642 (defun ,setup ()
643 ,(concat "Set up `" (symbol-name mode) "'.
644 Return non-nil if the minor mode is enabled.")
645 (if ,mode
646 (if (not (and (featurep 'semantic) (semantic-active-p)))
647 (progn
648 ;; Disable minor mode if semantic stuff not available
649 (setq ,mode nil)
650 (error "Buffer %s was not set up for parsing"
651 (buffer-name)))
652 ;; Enable the mode mode
653 (semantic-idle-scheduler-add #',func)
654 )
655 ;; Disable the mode mode
656 (semantic-idle-scheduler-remove #',func)
657 )
658 ,mode)
659
660 (defun ,mode (&optional arg)
661 ,doc
662 (interactive
663 (list (or current-prefix-arg
664 (if ,mode 0 1))))
665 (setq ,mode
666 (if arg
667 (>
668 (prefix-numeric-value arg)
669 0)
670 (not ,mode)))
671 (,setup)
672 (run-hooks ,hook)
673 (if (called-interactively-p 'interactive)
674 (message "%s %sabled"
675 (symbol-name ',mode)
676 (if ,mode "en" "dis")))
677 (semantic-mode-line-update)
678 ,mode)
679
680 (semantic-add-minor-mode ',mode
681 "" ; idle schedulers are quiet?
682 ,map)
683
684 (defun ,func ()
685 ,(concat "Perform idle activity for the minor mode `"
686 (symbol-name mode) "'.")
687 ,@forms))))
688 (put 'define-semantic-idle-service 'lisp-indent-function 1)
689
690 \f
691 ;;; SUMMARY MODE
692 ;;
693 ;; A mode similar to eldoc using semantic
694
695 (defcustom semantic-idle-summary-function
696 'semantic-format-tag-summarize-with-file
697 "Function to call when displaying tag information during idle time.
698 This function should take a single argument, a Semantic tag, and
699 return a string to display.
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 tries to find it with a deep targeted 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 function 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 "23.2")
800
801 (defcustom semantic-idle-summary-mode-hook nil
802 "Hook run at the end of `semantic-idle-summary'."
803 :group 'semantic
804 :type 'hook)
805
806 (defun semantic-idle-summary-idle-function ()
807 "Display a tag summary of the lexical token under the cursor.
808 Call `semantic-idle-summary-current-symbol-info' for getting the
809 current tag to display information."
810 (or (eq major-mode 'emacs-lisp-mode)
811 (not (semantic-idle-summary-useful-context-p))
812 (let* ((found (semantic-idle-summary-current-symbol-info))
813 (str (cond ((stringp found) found)
814 ((semantic-tag-p found)
815 (funcall semantic-idle-summary-function
816 found nil t)))))
817 ;; Show the message with eldoc functions
818 (unless (and str (boundp 'eldoc-echo-area-use-multiline-p)
819 eldoc-echo-area-use-multiline-p)
820 (let ((w (1- (window-width (minibuffer-window)))))
821 (if (> (length str) w)
822 (setq str (substring str 0 w)))))
823 (eldoc-message str))))
824
825 (define-minor-mode semantic-idle-summary-mode
826 "Toggle Semantic Idle Summary mode.
827 With ARG, turn Semantic Idle Summary mode on if ARG is positive,
828 off otherwise.
829
830 When this minor mode is enabled, the echo area displays a summary
831 of the lexical token at point whenever Emacs is idle."
832 :group 'semantic
833 :group 'semantic-modes
834 (semantic-idle-summary-mode-setup)
835 (semantic-mode-line-update))
836
837 (defun semantic-idle-summary-refresh-echo-area ()
838 (and semantic-idle-summary-mode
839 eldoc-last-message
840 (if (and (not executing-kbd-macro)
841 (not (and (boundp 'edebug-active) edebug-active))
842 (not cursor-in-echo-area)
843 (not (eq (selected-window) (minibuffer-window))))
844 (eldoc-message eldoc-last-message)
845 (setq eldoc-last-message nil))))
846
847 (defun semantic-idle-summary-mode-setup ()
848 "Set up `semantic-idle-summary-mode'."
849 (if semantic-idle-summary-mode
850 ;; Enable the mode
851 (progn
852 (unless (and (featurep 'semantic) (semantic-active-p))
853 ;; Disable minor mode if semantic stuff not available
854 (setq semantic-idle-summary-mode nil)
855 (error "Buffer %s was not set up for parsing"
856 (buffer-name)))
857 (require 'eldoc)
858 (semantic-idle-scheduler-add 'semantic-idle-summary-idle-function)
859 (add-hook 'pre-command-hook 'semantic-idle-summary-refresh-echo-area t))
860 ;; Disable the mode
861 (semantic-idle-scheduler-remove 'semantic-idle-summary-idle-function)
862 (remove-hook 'pre-command-hook 'semantic-idle-summary-refresh-echo-area t))
863 semantic-idle-summary-mode)
864
865 (semantic-add-minor-mode 'semantic-idle-summary-mode "")
866
867 (define-minor-mode global-semantic-idle-summary-mode
868 "Toggle Global Semantic Idle Summary mode.
869 With ARG, turn Global Semantic Idle Summary mode on if ARG is
870 positive, off otherwise.
871
872 When this minor mode is enabled, `semantic-idle-summary-mode' is
873 turned on in every Semantic-supported buffer."
874 :global t
875 :group 'semantic
876 :group 'semantic-modes
877 (semantic-toggle-minor-mode-globally
878 'semantic-idle-summary-mode
879 (if global-semantic-idle-summary-mode 1 -1)))
880
881 \f
882 ;;; Current symbol highlight
883 ;;
884 ;; This mode will use context analysis to perform highlighting
885 ;; of all uses of the symbol that is under the cursor.
886 ;;
887 ;; This is to mimic the Eclipse tool of a similar nature.
888 (defvar semantic-idle-summary-highlight-face 'region
889 "Face used for the summary highlight.")
890
891 (defun semantic-idle-summary-maybe-highlight (tag)
892 "Perhaps add highlighting onto TAG.
893 TAG was found as the thing under point. If it happens to be
894 visible, then highlight it."
895 (require 'pulse)
896 (let* ((region (when (and (semantic-tag-p tag)
897 (semantic-tag-with-position-p tag))
898 (semantic-tag-overlay tag)))
899 (file (when (and (semantic-tag-p tag)
900 (semantic-tag-with-position-p tag))
901 (semantic-tag-file-name tag)))
902 (buffer (when file (get-file-buffer file)))
903 ;; We use pulse, but we don't want the flashy version,
904 ;; just the stable version.
905 (pulse-flag nil)
906 )
907 (cond ((semantic-overlay-p region)
908 (with-current-buffer (semantic-overlay-buffer region)
909 (goto-char (semantic-overlay-start region))
910 (when (pos-visible-in-window-p
911 (point) (get-buffer-window (current-buffer) 'visible))
912 (if (< (semantic-overlay-end region) (point-at-eol))
913 (pulse-momentary-highlight-overlay
914 region semantic-idle-summary-highlight-face)
915 ;; Not the same
916 (pulse-momentary-highlight-region
917 (semantic-overlay-start region)
918 (point-at-eol)
919 semantic-idle-summary-highlight-face)))
920 ))
921 ((vectorp region)
922 (let ((start (aref region 0))
923 (end (aref region 1)))
924 (save-excursion
925 (when buffer (set-buffer buffer))
926 ;; As a vector, we have no filename. Perhaps it is a
927 ;; local variable?
928 (when (and (<= end (point-max))
929 (pos-visible-in-window-p
930 start (get-buffer-window (current-buffer) 'visible)))
931 (goto-char start)
932 (when (re-search-forward
933 (regexp-quote (semantic-tag-name tag))
934 end t)
935 ;; This is likely it, give it a try.
936 (pulse-momentary-highlight-region
937 start (if (<= end (point-at-eol)) end
938 (point-at-eol))
939 semantic-idle-summary-highlight-face)))
940 ))))
941 nil))
942
943 (define-semantic-idle-service semantic-idle-tag-highlight
944 "Highlight the tag, and references of the symbol under point.
945 Call `semantic-analyze-current-context' to find the reference tag.
946 Call `semantic-symref-hits-in-region' to identify local references."
947 (require 'pulse)
948 (when (semantic-idle-summary-useful-context-p)
949 (let* ((ctxt (semantic-analyze-current-context))
950 (Hbounds (when ctxt (oref ctxt bounds)))
951 (target (when ctxt (car (reverse (oref ctxt prefix)))))
952 (tag (semantic-current-tag))
953 ;; We use pulse, but we don't want the flashy version,
954 ;; just the stable version.
955 (pulse-flag nil))
956 (when ctxt
957 ;; Highlight the original tag? Protect against problems.
958 (condition-case nil
959 (semantic-idle-summary-maybe-highlight target)
960 (error nil))
961 ;; Identify all hits in this current tag.
962 (when (semantic-tag-p target)
963 (require 'semantic/symref/filter)
964 (semantic-symref-hits-in-region
965 target (lambda (start end prefix)
966 (when (/= start (car Hbounds))
967 (pulse-momentary-highlight-region
968 start end semantic-idle-summary-highlight-face))
969 (semantic-throw-on-input 'symref-highlight)
970 )
971 (semantic-tag-start tag)
972 (semantic-tag-end tag)))
973 ))))
974
975 \f
976 ;;; Completion Popup Mode
977 ;;
978 ;; This mode uses tooltips to display a (hopefully) short list of possible
979 ;; completions available for the text under point. It provides
980 ;; NO provision for actually filling in the values from those completions.
981
982 (defun semantic-idle-completion-list-default ()
983 "Calculate and display a list of completions."
984 (when (semantic-idle-summary-useful-context-p)
985 ;; This mode can be fragile. Ignore problems.
986 ;; If something doesn't do what you expect, run
987 ;; the below command by hand instead.
988 (condition-case nil
989 (let (
990 ;; Don't go loading in oodles of header libraries in
991 ;; IDLE time.
992 (semanticdb-find-default-throttle
993 (if (featurep 'semantic/db-find)
994 (remq 'unloaded semanticdb-find-default-throttle)
995 nil))
996 )
997 ;; Use idle version.
998 (require 'semantic/complete)
999 (semantic-complete-analyze-inline-idle)
1000 )
1001 (error nil))
1002 ))
1003
1004 (define-semantic-idle-service semantic-idle-completions
1005 "Toggle Semantic Idle Completions mode.
1006 With ARG, turn Semantic Idle Completions mode on if ARG is
1007 positive, off otherwise.
1008
1009 This minor mode only takes effect if Semantic is active and
1010 `semantic-idle-scheduler-mode' is enabled.
1011
1012 When enabled, Emacs displays a list of possible completions at
1013 idle time. The method for displaying completions is given by
1014 `semantic-complete-inline-analyzer-idle-displayor-class'; the
1015 default is to show completions inline.
1016
1017 While a completion is displayed, RET accepts the completion; M-n
1018 and M-p cycle through completion alternatives; TAB attempts to
1019 complete as far as possible, and cycles if no additional
1020 completion is possible; and any other command cancels the
1021 completion.
1022
1023 \\{semantic-complete-inline-map}"
1024 ;; Add the ability to override sometime.
1025 (semantic-idle-completion-list-default))
1026
1027 (provide 'semantic/idle)
1028
1029 ;; Local variables:
1030 ;; generated-autoload-file: "loaddefs.el"
1031 ;; generated-autoload-load-name: "semantic/idle"
1032 ;; End:
1033
1034 ;; arch-tag: 4bfd54da-5023-4cc1-91ae-e1fefc1a8d1b
1035 ;;; semantic-idle.el ends here