1 ;;; idle.el --- Schedule parsing tasks in idle time
3 ;;; Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009
4 ;;; Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
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.
34 ;; NOTE ON SEMANTIC_ANALYZE
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.
41 (require 'semantic/ctxt)
42 (require 'semantic/format)
43 (require 'semantic/tag)
46 ;; For the semantic-find-tags-by-name macro.
47 (eval-when-compile (require 'semantic/find))
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")
63 ;;; TIMER RELATED FUNCTIONS
65 (defvar semantic-idle-scheduler-timer nil
66 "Timer used to schedule tasks in idle time.")
68 (defvar semantic-idle-scheduler-work-timer nil
69 "Timer used to schedule tasks in idle time that may take a while.")
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."
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."
83 :set (lambda (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))))
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."
96 :set (lambda (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))))
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
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
114 semantic-idle-scheduler-work-idle-time t
115 #'semantic-idle-scheduler-work-function)))
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))
127 ;; The minor mode portion of this code just sets up the minor mode
128 ;; which does the initial scheduling of the idle timers.
131 (defcustom global-semantic-idle-scheduler-mode nil
132 "*If non-nil, enable global use of idle-scheduler mode."
134 :group 'semantic-modes
136 :require 'semantic/idle
137 :initialize 'custom-initialize-default
138 :set (lambda (sym val)
139 (global-semantic-idle-scheduler-mode (if val 1 -1))))
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."
149 (setq global-semantic-idle-scheduler-mode
150 (semantic-toggle-minor-mode-globally
151 'semantic-idle-scheduler-mode arg)))
153 (defcustom semantic-idle-scheduler-mode-hook nil
154 "*Hook run at the end of function `semantic-idle-scheduler-mode'."
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)
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."
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))))
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)))
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"
194 (semantic-idle-scheduler-setup-timers)))
195 semantic-idle-scheduler-mode)
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.)
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."
210 (list (or current-prefix-arg
211 (if semantic-idle-scheduler-mode 0 1))))
212 (setq semantic-idle-scheduler-mode
215 (prefix-numeric-value arg)
217 (not semantic-idle-scheduler-mode)))
218 (semantic-idle-scheduler-mode-setup)
219 (run-hooks 'semantic-idle-scheduler-mode-hook)
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)
226 (semantic-add-minor-mode 'semantic-idle-scheduler-mode
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)
236 ;;; SERVICES services
238 ;; These are services for managing idle services.
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
247 (defun semantic-idle-scheduler-add (function)
248 "Schedule FUNCTION to occur during idle time."
249 (add-to-list 'semantic-idle-scheduler-queue function))
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)))
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)
267 (mapcar #'(lambda (b)
268 (and (buffer-file-name b)
271 safe ;; This safe is not used, but could be.
274 (when (semantic-idle-scheduler-enabled-p)
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)
283 ;; Now loop over other buffers with same major mode, trying to
284 ;; update them as well. Stop on keypress.
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.
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.
304 (dolist (service semantic-idle-scheduler-queue)
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)
311 (when semantic-idle-scheduler-verbose-flag
312 (message "IDLE: execture service %s...done" service))
315 ;; Finally loop over remaining buffers, trying to update them as
316 ;; well. Stop on keypress.
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)))))
324 (when semantic-idle-scheduler-verbose-flag
325 (message "IDLE: Core handler...done")))
327 (defun semantic-debug-idle-function ()
328 "Run the Semantic idle function with debugging turned on."
330 (let ((debug-on-error t))
331 (semantic-idle-core-handler)
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))
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."
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."
363 (semantic-safe "Idle Work Parse Error: %S"
364 (semantic-idle-scheduler-refresh-tags)
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)
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))
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."
393 (semantic-exit-on-input 'idle-work-timer
394 (let* ((inhibit-quit nil)
395 (cb (current-buffer))
396 (buffers (delq (current-buffer)
398 (mapcar #'(lambda (b)
399 (and (buffer-file-name b)
403 ;; First, handle long tasks in the current buffer.
404 (when (semantic-idle-scheduler-enabled-p)
406 (setq safe (semantic-idle-work-for-one-buffer (current-buffer))
408 (when (not safe) (push (current-buffer) errbuf))
410 ;; Now loop over other buffers with same major mode, trying to
411 ;; update them as well. Stop on keypress.
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)))
422 (when (and (featurep 'semantic/db) (semanticdb-minor-mode-p))
424 (semanticdb-save-all-db-idle)
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"
430 (semantic-idle-scheduler-work-parse-neighboring-files))
433 ;; Save everything... again
434 (semanticdb-save-all-db-idle)
437 ;; Done w/ processing
446 (format "done with 1 error in %s" (car errbuf)))
448 (format "done with errors in %d buffers."
449 (length errbuf)))))))
451 (defun semantic-debug-idle-work-function ()
452 "Run the Semantic idle work function with debugging turned on."
454 (let ((debug-on-error t))
455 (semantic-idle-work-core-handler)
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
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)))
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)
480 (semanticdb-file-table-object file)
487 ;; Reparsing is installed as semantic idle service.
488 ;; This part ALWAYS happens, and other services occur
491 ;; (defcustom semantic-idle-scheduler-no-working-message t
492 ;; "*If non-nil, disable display of working messages during parse."
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."
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.")
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.")
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.
518 Return non-nil if the refresh was successful.
519 Return nil if there is some sort of syntax error preventing a full
522 Does nothing if the current buffer doesn't need reparsing."
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.
529 ;; If the buffer was previously marked unparseable,
530 ;; then don't waste our time.
531 ((semantic-parse-tree-unparseable-p)
533 ;; The parse tree is already ok.
534 ((semantic-parse-tree-up-to-date-p)
537 ;; If the buffer might need a reparse and it is safe to do so,
539 (let* (;(semantic-working-type 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
546 ;; working-status-dynamic-type))
547 ;; (working-status-percentage-type
548 ;; (if semantic-idle-scheduler-no-working-message
550 ;; working-status-percentage-type))
553 ;; Let people hook into this, but don't let them hose
556 (run-hooks 'semantic-before-idle-scheduler-reparse-hooks)
557 (error (setq semantic-before-idle-scheduler-reparse-hooks nil)))
560 ;; Perform the parsing.
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))
567 ;; If we are here, it is because the lexical step failed,
568 ;; proably due to unterminated lists or something like that.
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
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)
584 ;; After updating the tags, handle any pending decorations for this
586 (require 'semantic/decorate/mode)
587 (semantic-decorate-flush-pending-decorations (current-buffer))
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.
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")))
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.")
620 (semantic-toggle-minor-mode-globally
623 (defcustom ,global nil
624 (concat "*If non-nil, enable global use of `" (symbol-name ',mode) "'.
627 :group 'semantic-modes
629 :require 'semantic/idle
630 :initialize 'custom-initialize-default
631 :set (lambda (sym val)
632 (,global (if val 1 -1))))
635 (concat "*Hook run at the end of function `" (symbol-name ',mode) "'.")
640 (let ((km (make-sparse-keymap)))
642 (concat "Keymap for `" (symbol-name ',mode) "'."))
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)
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.")
655 (if (not (and (featurep 'semantic) (semantic-active-p)))
657 ;; Disable minor mode if semantic stuff not available
659 (error "Buffer %s was not set up for parsing"
661 ;; Enable the mode mode
662 (semantic-idle-scheduler-add #',func)
664 ;; Disable the mode mode
665 (semantic-idle-scheduler-remove #',func)
669 (defun ,mode (&optional arg)
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.")
677 (list (or current-prefix-arg
682 (prefix-numeric-value arg)
688 (message "%s %sabled"
690 (if ,mode "en" "dis")))
691 (semantic-mode-line-update)
694 (semantic-add-minor-mode ',mode
695 "" ; idle schedulers are quiet?
703 (put 'define-semantic-idle-service 'lisp-indent-function 1)
708 ;; A mode similar to eldoc using semantic
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'."
715 :type semantic-format-tag-custom-list)
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)))))
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))))
734 (semantic-idle-summary-find-current-symbol-tag sym))))
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))))
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))
751 (require 'semantic/analyze)
752 (semantic-analyze-interesting-tag analysis))))
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."
759 (semanticdb-find-default-throttle
760 (if (featurep 'semantic/db-find)
761 (remq 'unloaded semanticdb-find-default-throttle)
765 ;; use whicever has success first.
767 (semantic-idle-summary-current-symbol-keyword)
769 (semantic-idle-summary-current-symbol-info-context)
771 (semantic-idle-summary-current-symbol-info-brutish)
774 (defvar semantic-idle-summary-out-of-context-faces
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.
781 "List of font-lock faces that indicate a useless summary context.
782 Those are generally faces used to highlight comments.
784 It might be useful to override this variable to add comment faces
785 specific to a major mode. For example, in jde mode:
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)))")
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)
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.
807 (define-overloadable-function semantic-idle-summary-current-symbol-info ()
808 "Return a string message describing the current context.")
810 (make-obsolete-overload 'semantic-eldoc-current-symbol-info
811 'semantic-idle-summary-current-symbol-info)
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
825 ;; Show the message with eldoc functions
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))))
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)
839 ;;; Current symbol highlight
841 ;; This mode will use context analysis to perform highlighting
842 ;; of all uses of the symbol that is under the cursor.
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.")
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."
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.
864 (cond ((semantic-overlay-p region)
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)
874 (pulse-momentary-highlight-region
875 (semantic-overlay-start region)
877 semantic-idle-summary-highlight-face)))
880 (let ((start (aref region 0))
881 (end (aref region 1)))
883 (when buffer (set-buffer buffer))
884 ;; As a vector, we have no filename. Perhaps it is a
886 (when (and (<= end (point-max))
887 (pos-visible-in-window-p
888 start (get-buffer-window (current-buffer) 'visible)))
890 (when (re-search-forward
891 (regexp-quote (semantic-tag-name tag))
893 ;; This is likely it, give it a try.
894 (pulse-momentary-highlight-region
895 start (if (<= end (point-at-eol)) end
897 semantic-idle-summary-highlight-face)))
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."
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.
915 ;; Highlight the original tag? Protect against problems.
917 (semantic-idle-summary-maybe-highlight target)
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
927 (semantic-throw-on-input 'symref-highlight)
929 (semantic-tag-start tag)
930 (semantic-tag-end tag)))
934 ;;; Completion Popup Mode
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.
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.
948 ;; Don't go loading in oodles of header libraries in
950 (semanticdb-find-default-throttle
951 (if (featurep 'semantic/db-find)
952 (remq 'unloaded semanticdb-find-default-throttle)
956 (require 'semantic/complete)
957 (semantic-complete-analyze-inline-idle)
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))
967 (provide 'semantic/idle)
970 ;; generated-autoload-file: "loaddefs.el"
971 ;; generated-autoload-feature: semantic/loaddefs
972 ;; generated-autoload-load-name: "semantic/idle"
975 ;;; semantic-idle.el ends here