]> code.delx.au - gnu-emacs/blob - lisp/cedet/semantic/idle.el
* files.el (auto-mode-alist): Use emacs-lisp-mode for Project.ede.
[gnu-emacs] / 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 (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 2
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 \f
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 (semantic-alias-obsolete 'semantic-auto-parse-mode
231 'semantic-idle-scheduler-mode)
232 (semantic-alias-obsolete 'global-semantic-auto-parse-mode
233 'global-semantic-idle-scheduler-mode)
234
235 \f
236 ;;; SERVICES services
237 ;;
238 ;; These are services for managing idle services.
239 ;;
240 (defvar semantic-idle-scheduler-queue nil
241 "List of functions to execute during idle time.
242 These functions will be called in the current buffer after that
243 buffer has had its tags made up to date. These functions
244 will not be called if there are errors parsing the
245 current buffer.")
246
247 (defun semantic-idle-scheduler-add (function)
248 "Schedule FUNCTION to occur during idle time."
249 (add-to-list 'semantic-idle-scheduler-queue function))
250
251 (defun semantic-idle-scheduler-remove (function)
252 "Unschedule FUNCTION to occur during idle time."
253 (setq semantic-idle-scheduler-queue
254 (delete function semantic-idle-scheduler-queue)))
255
256 ;;; IDLE Function
257 ;;
258 (defun semantic-idle-core-handler ()
259 "Core idle function that handles reparsing.
260 And also manages services that depend on tag values."
261 (when semantic-idle-scheduler-verbose-flag
262 (message "IDLE: Core handler..."))
263 (semantic-exit-on-input 'idle-timer
264 (let* ((inhibit-quit nil)
265 (buffers (delq (current-buffer)
266 (delq nil
267 (mapcar #'(lambda (b)
268 (and (buffer-file-name b)
269 b))
270 (buffer-list)))))
271 safe ;; This safe is not used, but could be.
272 others
273 mode)
274 (when (semantic-idle-scheduler-enabled-p)
275 (save-excursion
276 ;; First, reparse the current buffer.
277 (setq mode major-mode
278 safe (semantic-safe "Idle Parse Error: %S"
279 ;(error "Goofy error 1")
280 (semantic-idle-scheduler-refresh-tags)
281 )
282 )
283 ;; Now loop over other buffers with same major mode, trying to
284 ;; update them as well. Stop on keypress.
285 (dolist (b buffers)
286 (semantic-throw-on-input 'parsing-mode-buffers)
287 (with-current-buffer b
288 (if (eq major-mode mode)
289 (and (semantic-idle-scheduler-enabled-p)
290 (semantic-safe "Idle Parse Error: %S"
291 ;(error "Goofy error")
292 (semantic-idle-scheduler-refresh-tags)))
293 (push (current-buffer) others))))
294 (setq buffers others))
295 ;; If re-parse of current buffer completed, evaluate all other
296 ;; services. Stop on keypress.
297
298 ;; NOTE ON COMMENTED SAFE HERE
299 ;; We used to not execute the services if the buffer wsa
300 ;; unparseable. We now assume that they are lexically
301 ;; safe to do, because we have marked the buffer unparseable
302 ;; if there was a problem.
303 ;;(when safe
304 (dolist (service semantic-idle-scheduler-queue)
305 (save-excursion
306 (semantic-throw-on-input 'idle-queue)
307 (when semantic-idle-scheduler-verbose-flag
308 (message "IDLE: execture service %s..." service))
309 (semantic-safe (format "Idle Service Error %s: %%S" service)
310 (funcall service))
311 (when semantic-idle-scheduler-verbose-flag
312 (message "IDLE: execture service %s...done" service))
313 )))
314 ;;)
315 ;; Finally loop over remaining buffers, trying to update them as
316 ;; well. Stop on keypress.
317 (save-excursion
318 (dolist (b buffers)
319 (semantic-throw-on-input 'parsing-other-buffers)
320 (with-current-buffer b
321 (and (semantic-idle-scheduler-enabled-p)
322 (semantic-idle-scheduler-refresh-tags)))))
323 ))
324 (when semantic-idle-scheduler-verbose-flag
325 (message "IDLE: Core handler...done")))
326
327 (defun semantic-debug-idle-function ()
328 "Run the Semantic idle function with debugging turned on."
329 (interactive)
330 (let ((debug-on-error t))
331 (semantic-idle-core-handler)
332 ))
333
334 (defun semantic-idle-scheduler-function ()
335 "Function run when after `semantic-idle-scheduler-idle-time'.
336 This function will reparse the current buffer, and if successful,
337 call additional functions registered with the timer calls."
338 (when (zerop (recursion-depth))
339 (let ((debug-on-error nil))
340 (save-match-data (semantic-idle-core-handler))
341 )))
342
343 \f
344 ;;; WORK FUNCTION
345 ;;
346 ;; Unlike the shorter timer, the WORK timer will kick of tasks that
347 ;; may take a long time to complete.
348 (defcustom semantic-idle-work-parse-neighboring-files-flag t
349 "*Non-nil means to parse files in the same dir as the current buffer.
350 Disable to prevent lots of excessive parsing in idle time."
351 :group 'semantic
352 :type 'boolean)
353
354
355 (defun semantic-idle-work-for-one-buffer (buffer)
356 "Do long-processing work for for BUFFER.
357 Uses `semantic-safe' and returns the output.
358 Returns t of all processing succeeded."
359 (save-excursion
360 (set-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 ;; (defcustom semantic-idle-scheduler-no-working-message t
492 ;; "*If non-nil, disable display of working messages during parse."
493 ;; :group 'semantic
494 ;; :type 'boolean)
495
496 ;; (defcustom semantic-idle-scheduler-working-in-modeline-flag nil
497 ;; "*Non-nil means show working messages in the mode line.
498 ;; Typically, parsing will show messages in the minibuffer.
499 ;; This will move the parse message into the modeline."
500 ;; :group 'semantic
501 ;; :type 'boolean)
502
503 (defvar semantic-before-idle-scheduler-reparse-hooks nil
504 "Hooks run before option `semantic-idle-scheduler' begins parsing.
505 If any hook throws an error, this variable is reset to nil.
506 This hook is not protected from lexical errors.")
507
508 (defvar semantic-after-idle-scheduler-reparse-hooks nil
509 "Hooks run after option `semantic-idle-scheduler' has parsed.
510 If any hook throws an error, this variable is reset to nil.
511 This hook is not protected from lexical errors.")
512
513 (defun semantic-idle-scheduler-refresh-tags ()
514 "Refreshes the current buffer's tags.
515 This is called by `semantic-idle-scheduler-function' to update the
516 tags in the current buffer.
517
518 Return non-nil if the refresh was successful.
519 Return nil if there is some sort of syntax error preventing a full
520 reparse.
521
522 Does nothing if the current buffer doesn't need reparsing."
523
524 (prog1
525 ;; These checks actually occur in `semantic-fetch-tags', but if we
526 ;; do them here, then all the bovination hooks are not run, and
527 ;; we save lots of time.
528 (cond
529 ;; If the buffer was previously marked unparseable,
530 ;; then don't waste our time.
531 ((semantic-parse-tree-unparseable-p)
532 nil)
533 ;; The parse tree is already ok.
534 ((semantic-parse-tree-up-to-date-p)
535 t)
536 (t
537 ;; If the buffer might need a reparse and it is safe to do so,
538 ;; give it a try.
539 (let* (;(semantic-working-type nil)
540 (inhibit-quit nil)
541 ;; (working-use-echo-area-p
542 ;; (not semantic-idle-scheduler-working-in-modeline-flag))
543 ;; (working-status-dynamic-type
544 ;; (if semantic-idle-scheduler-no-working-message
545 ;; nil
546 ;; working-status-dynamic-type))
547 ;; (working-status-percentage-type
548 ;; (if semantic-idle-scheduler-no-working-message
549 ;; nil
550 ;; working-status-percentage-type))
551 (lexically-safe t)
552 )
553 ;; Let people hook into this, but don't let them hose
554 ;; us over!
555 (condition-case nil
556 (run-hooks 'semantic-before-idle-scheduler-reparse-hooks)
557 (error (setq semantic-before-idle-scheduler-reparse-hooks nil)))
558
559 (unwind-protect
560 ;; Perform the parsing.
561 (progn
562 (when semantic-idle-scheduler-verbose-flag
563 (message "IDLE: reparse %s..." (buffer-name)))
564 (when (semantic-lex-catch-errors idle-scheduler
565 (save-excursion (semantic-fetch-tags))
566 nil)
567 ;; If we are here, it is because the lexical step failed,
568 ;; proably due to unterminated lists or something like that.
569
570 ;; We do nothing, and just wait for the next idle timer
571 ;; to go off. In the meantime, remember this, and make sure
572 ;; no other idle services can get executed.
573 (setq lexically-safe nil))
574 (when semantic-idle-scheduler-verbose-flag
575 (message "IDLE: reparse %s...done" (buffer-name))))
576 ;; Let people hook into this, but don't let them hose
577 ;; us over!
578 (condition-case nil
579 (run-hooks 'semantic-after-idle-scheduler-reparse-hooks)
580 (error (setq semantic-after-idle-scheduler-reparse-hooks nil))))
581 ;; Return if we are lexically safe (from prog1)
582 lexically-safe)))
583
584 ;; After updating the tags, handle any pending decorations for this
585 ;; buffer.
586 (require 'semantic/decorate/mode)
587 (semantic-decorate-flush-pending-decorations (current-buffer))
588 ))
589
590 \f
591 ;;; IDLE SERVICES
592 ;;
593 ;; Idle Services are minor modes which enable or disable a services in
594 ;; the idle scheduler. Creating a new services only requires calling
595 ;; `semantic-create-idle-services' which does all the setup
596 ;; needed to create the minor mode that will enable or disable
597 ;; a services. The services must provide a single function.
598
599 (defmacro define-semantic-idle-service (name doc &rest forms)
600 "Create a new idle services with NAME.
601 DOC will be a documentation string describing FORMS.
602 FORMS will be called during idle time after the current buffer's
603 semantic tag information has been updated.
604 This routines creates the following functions and variables:"
605 (let ((global (intern (concat "global-" (symbol-name name) "-mode")))
606 (mode (intern (concat (symbol-name name) "-mode")))
607 (hook (intern (concat (symbol-name name) "-mode-hook")))
608 (map (intern (concat (symbol-name name) "-mode-map")))
609 (setup (intern (concat (symbol-name name) "-mode-setup")))
610 (func (intern (concat (symbol-name name) "-idle-function")))
611 )
612
613 `(eval-and-compile
614 (defun ,global (&optional arg)
615 ,(concat "Toggle global use of option `" (symbol-name mode) "'.
616 If ARG is positive, enable, if it is negative, disable.
617 If ARG is nil, then toggle.")
618 (interactive "P")
619 (setq ,global
620 (semantic-toggle-minor-mode-globally
621 ',mode arg)))
622
623 (defcustom ,global nil
624 (concat "*If non-nil, enable global use of `" (symbol-name ',mode) "'.
625 " ,doc)
626 :group 'semantic
627 :group 'semantic-modes
628 :type 'boolean
629 :require 'semantic/idle
630 :initialize 'custom-initialize-default
631 :set (lambda (sym val)
632 (,global (if val 1 -1))))
633
634 (defcustom ,hook nil
635 (concat "*Hook run at the end of function `" (symbol-name ',mode) "'.")
636 :group 'semantic
637 :type 'hook)
638
639 (defvar ,map
640 (let ((km (make-sparse-keymap)))
641 km)
642 (concat "Keymap for `" (symbol-name ',mode) "'."))
643
644 (defvar ,mode nil
645 (concat "Non-nil if summary minor mode is enabled.
646 Use the command `" (symbol-name ',mode) "' to change this variable."))
647 (make-variable-buffer-local ',mode)
648
649 (defun ,setup ()
650 ,(concat "Setup option `" (symbol-name mode) "'.
651 The minor mode can be turned on only if semantic feature is available
652 and the idle scheduler is active.
653 Return non-nil if the minor mode is enabled.")
654 (if ,mode
655 (if (not (and (featurep 'semantic) (semantic-active-p)))
656 (progn
657 ;; Disable minor mode if semantic stuff not available
658 (setq ,mode nil)
659 (error "Buffer %s was not set up for parsing"
660 (buffer-name)))
661 ;; Enable the mode mode
662 (semantic-idle-scheduler-add #',func)
663 )
664 ;; Disable the mode mode
665 (semantic-idle-scheduler-remove #',func)
666 )
667 ,mode)
668
669 (defun ,mode (&optional arg)
670 ,(concat doc "
671 This is a minor mode which performs actions during idle time.
672 With prefix argument ARG, turn on if positive, otherwise off. The
673 minor mode can be turned on only if semantic feature is available and
674 the current buffer was set up for parsing. Return non-nil if the
675 minor mode is enabled.")
676 (interactive
677 (list (or current-prefix-arg
678 (if ,mode 0 1))))
679 (setq ,mode
680 (if arg
681 (>
682 (prefix-numeric-value arg)
683 0)
684 (not ,mode)))
685 (,setup)
686 (run-hooks ,hook)
687 (if (interactive-p)
688 (message "%s %sabled"
689 (symbol-name ',mode)
690 (if ,mode "en" "dis")))
691 (semantic-mode-line-update)
692 ,mode)
693
694 (semantic-add-minor-mode ',mode
695 "" ; idle schedulers are quiet?
696 ,map)
697
698 (defun ,func ()
699 ,doc
700 ,@forms)
701
702 )))
703 (put 'define-semantic-idle-service 'lisp-indent-function 1)
704
705 \f
706 ;;; SUMMARY MODE
707 ;;
708 ;; A mode similar to eldoc using semantic
709
710 (defcustom semantic-idle-summary-function
711 'semantic-format-tag-summarize-with-file
712 "*Function to use when displaying tag information during idle time.
713 Some useful functions are found in `semantic-format-tag-functions'."
714 :group 'semantic
715 :type semantic-format-tag-custom-list)
716
717 (defsubst semantic-idle-summary-find-current-symbol-tag (sym)
718 "Search for a semantic tag with name SYM in database tables.
719 Return the tag found or nil if not found.
720 If semanticdb is not in use, use the current buffer only."
721 (car (if (and (featurep 'semantic/db)
722 semanticdb-current-database
723 (require 'semantic/db-find))
724 (cdar (semanticdb-deep-find-tags-by-name sym))
725 (semantic-deep-find-tags-by-name sym (current-buffer)))))
726
727 (defun semantic-idle-summary-current-symbol-info-brutish ()
728 "Return a string message describing the current context.
729 Gets a symbol with `semantic-ctxt-current-thing' and then
730 trys to find it with a deep targetted search."
731 ;; Try the current "thing".
732 (let ((sym (car (semantic-ctxt-current-thing))))
733 (when sym
734 (semantic-idle-summary-find-current-symbol-tag sym))))
735
736 (defun semantic-idle-summary-current-symbol-keyword ()
737 "Return a string message describing the current symbol.
738 Returns a value only if it is a keyword."
739 ;; Try the current "thing".
740 (let ((sym (car (semantic-ctxt-current-thing))))
741 (if (and sym (semantic-lex-keyword-p sym))
742 (semantic-lex-keyword-get sym 'summary))))
743
744 (defun semantic-idle-summary-current-symbol-info-context ()
745 "Return a string message describing the current context.
746 Use the semantic analyzer to find the symbol information."
747 (let ((analysis (condition-case nil
748 (semantic-analyze-current-context (point))
749 (error nil))))
750 (when analysis
751 (require 'semantic/analyze)
752 (semantic-analyze-interesting-tag analysis))))
753
754 (defun semantic-idle-summary-current-symbol-info-default ()
755 "Return a string message describing the current context.
756 This functin will disable loading of previously unloaded files
757 by semanticdb as a time-saving measure."
758 (let (
759 (semanticdb-find-default-throttle
760 (if (featurep 'semantic/db-find)
761 (remq 'unloaded semanticdb-find-default-throttle)
762 nil))
763 )
764 (save-excursion
765 ;; use whicever has success first.
766 (or
767 (semantic-idle-summary-current-symbol-keyword)
768
769 (semantic-idle-summary-current-symbol-info-context)
770
771 (semantic-idle-summary-current-symbol-info-brutish)
772 ))))
773
774 (defvar semantic-idle-summary-out-of-context-faces
775 '(
776 font-lock-comment-face
777 font-lock-string-face
778 font-lock-doc-string-face ; XEmacs.
779 font-lock-doc-face ; Emacs 21 and later.
780 )
781 "List of font-lock faces that indicate a useless summary context.
782 Those are generally faces used to highlight comments.
783
784 It might be useful to override this variable to add comment faces
785 specific to a major mode. For example, in jde mode:
786
787 \(defvar-mode-local jde-mode semantic-idle-summary-out-of-context-faces
788 (append (default-value 'semantic-idle-summary-out-of-context-faces)
789 '(jde-java-font-lock-doc-tag-face
790 jde-java-font-lock-link-face
791 jde-java-font-lock-bold-face
792 jde-java-font-lock-underline-face
793 jde-java-font-lock-pre-face
794 jde-java-font-lock-code-face)))")
795
796 (defun semantic-idle-summary-useful-context-p ()
797 "Non-nil of we should show a summary based on context."
798 (if (and (boundp 'font-lock-mode)
799 font-lock-mode
800 (memq (get-text-property (point) 'face)
801 semantic-idle-summary-out-of-context-faces))
802 ;; The best I can think of at the moment is to disable
803 ;; in comments by detecting with font-lock.
804 nil
805 t))
806
807 (define-overloadable-function semantic-idle-summary-current-symbol-info ()
808 "Return a string message describing the current context.")
809
810 (make-obsolete-overload 'semantic-eldoc-current-symbol-info
811 'semantic-idle-summary-current-symbol-info)
812
813 (define-semantic-idle-service semantic-idle-summary
814 "Display a tag summary of the lexical token under the cursor.
815 Call `semantic-idle-summary-current-symbol-info' for getting the
816 current tag to display information."
817 (or (eq major-mode 'emacs-lisp-mode)
818 (not (semantic-idle-summary-useful-context-p))
819 (let* ((found (semantic-idle-summary-current-symbol-info))
820 (str (cond ((stringp found) found)
821 ((semantic-tag-p found)
822 (funcall semantic-idle-summary-function
823 found nil t))))
824 )
825 ;; Show the message with eldoc functions
826 (require 'eldoc)
827 (unless (and str (boundp 'eldoc-echo-area-use-multiline-p)
828 eldoc-echo-area-use-multiline-p)
829 (let ((w (1- (window-width (minibuffer-window)))))
830 (if (> (length str) w)
831 (setq str (substring str 0 w)))))
832 (eldoc-message str))))
833
834 (semantic-alias-obsolete 'semantic-summary-mode
835 'semantic-idle-summary-mode)
836 (semantic-alias-obsolete 'global-semantic-summary-mode
837 'global-semantic-idle-summary-mode)
838 \f
839 ;;; Current symbol highlight
840 ;;
841 ;; This mode will use context analysis to perform highlighting
842 ;; of all uses of the symbol that is under the cursor.
843 ;;
844 ;; This is to mimic the Eclipse tool of a similar nature.
845 (defvar semantic-idle-summary-highlight-face 'region
846 "Face used for the summary highlight.")
847
848 (defun semantic-idle-summary-maybe-highlight (tag)
849 "Perhaps add highlighting onto TAG.
850 TAG was found as the thing under point. If it happens to be
851 visible, then highlight it."
852 (require 'pulse)
853 (let* ((region (when (and (semantic-tag-p tag)
854 (semantic-tag-with-position-p tag))
855 (semantic-tag-overlay tag)))
856 (file (when (and (semantic-tag-p tag)
857 (semantic-tag-with-position-p tag))
858 (semantic-tag-file-name tag)))
859 (buffer (when file (get-file-buffer file)))
860 ;; We use pulse, but we don't want the flashy version,
861 ;; just the stable version.
862 (pulse-flag nil)
863 )
864 (cond ((semantic-overlay-p region)
865 (save-excursion
866 (set-buffer (semantic-overlay-buffer region))
867 (goto-char (semantic-overlay-start region))
868 (when (pos-visible-in-window-p
869 (point) (get-buffer-window (current-buffer) 'visible))
870 (if (< (semantic-overlay-end region) (point-at-eol))
871 (pulse-momentary-highlight-overlay
872 region semantic-idle-summary-highlight-face)
873 ;; Not the same
874 (pulse-momentary-highlight-region
875 (semantic-overlay-start region)
876 (point-at-eol)
877 semantic-idle-summary-highlight-face)))
878 ))
879 ((vectorp region)
880 (let ((start (aref region 0))
881 (end (aref region 1)))
882 (save-excursion
883 (when buffer (set-buffer buffer))
884 ;; As a vector, we have no filename. Perhaps it is a
885 ;; local variable?
886 (when (and (<= end (point-max))
887 (pos-visible-in-window-p
888 start (get-buffer-window (current-buffer) 'visible)))
889 (goto-char start)
890 (when (re-search-forward
891 (regexp-quote (semantic-tag-name tag))
892 end t)
893 ;; This is likely it, give it a try.
894 (pulse-momentary-highlight-region
895 start (if (<= end (point-at-eol)) end
896 (point-at-eol))
897 semantic-idle-summary-highlight-face)))
898 ))))
899 nil))
900
901 (define-semantic-idle-service semantic-idle-tag-highlight
902 "Highlight the tag, and references of the symbol under point.
903 Call `semantic-analyze-current-context' to find the reference tag.
904 Call `semantic-symref-hits-in-region' to identify local references."
905 (require 'pulse)
906 (when (semantic-idle-summary-useful-context-p)
907 (let* ((ctxt (semantic-analyze-current-context))
908 (Hbounds (when ctxt (oref ctxt bounds)))
909 (target (when ctxt (car (reverse (oref ctxt prefix)))))
910 (tag (semantic-current-tag))
911 ;; We use pulse, but we don't want the flashy version,
912 ;; just the stable version.
913 (pulse-flag nil))
914 (when ctxt
915 ;; Highlight the original tag? Protect against problems.
916 (condition-case nil
917 (semantic-idle-summary-maybe-highlight target)
918 (error nil))
919 ;; Identify all hits in this current tag.
920 (when (semantic-tag-p target)
921 (require 'semantic/symref/filter)
922 (semantic-symref-hits-in-region
923 target (lambda (start end prefix)
924 (when (/= start (car Hbounds))
925 (pulse-momentary-highlight-region
926 start end))
927 (semantic-throw-on-input 'symref-highlight)
928 )
929 (semantic-tag-start tag)
930 (semantic-tag-end tag)))
931 ))))
932
933 \f
934 ;;; Completion Popup Mode
935 ;;
936 ;; This mode uses tooltips to display a (hopefully) short list of possible
937 ;; completions available for the text under point. It provides
938 ;; NO provision for actually filling in the values from those completions.
939
940 (defun semantic-idle-completion-list-default ()
941 "Calculate and display a list of completions."
942 (when (semantic-idle-summary-useful-context-p)
943 ;; This mode can be fragile. Ignore problems.
944 ;; If something doesn't do what you expect, run
945 ;; the below command by hand instead.
946 (condition-case nil
947 (let (
948 ;; Don't go loading in oodles of header libraries in
949 ;; IDLE time.
950 (semanticdb-find-default-throttle
951 (if (featurep 'semantic/db-find)
952 (remq 'unloaded semanticdb-find-default-throttle)
953 nil))
954 )
955 ;; Use idle version.
956 (require 'semantic/complete)
957 (semantic-complete-analyze-inline-idle)
958 )
959 (error nil))
960 ))
961
962 (define-semantic-idle-service semantic-idle-completions
963 "Display a list of possible completions in a tooltip."
964 ;; Add the ability to override sometime.
965 (semantic-idle-completion-list-default))
966
967 (provide 'semantic/idle)
968
969 ;; Local variables:
970 ;; generated-autoload-file: "loaddefs.el"
971 ;; generated-autoload-feature: semantic/loaddefs
972 ;; generated-autoload-load-name: "semantic/idle"
973 ;; End:
974
975 ;;; semantic-idle.el ends here