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 (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")
64 ;;; TIMER RELATED FUNCTIONS
66 (defvar semantic-idle-scheduler-timer nil
67 "Timer used to schedule tasks in idle time.")
69 (defvar semantic-idle-scheduler-work-timer nil
70 "Timer used to schedule tasks in idle time that may take a while.")
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."
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."
84 :set
(lambda (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))))
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."
97 :set
(lambda (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))))
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
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
115 semantic-idle-scheduler-work-idle-time t
116 #'semantic-idle-scheduler-work-function
)))
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
))
128 ;; The minor mode portion of this code just sets up the minor mode
129 ;; which does the initial scheduling of the idle timers.
132 (defcustom global-semantic-idle-scheduler-mode nil
133 "*If non-nil, enable global use of idle-scheduler mode."
135 :group
'semantic-modes
137 :require
'semantic
/idle
138 :initialize
'custom-initialize-default
139 :set
(lambda (sym val
)
140 (global-semantic-idle-scheduler-mode (if val
1 -
1))))
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."
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
)))
160 (defcustom semantic-idle-scheduler-mode-hook nil
161 "Hook run at the end of the function `semantic-idle-scheduler-mode'."
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
)
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."
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
))))
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)))
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"
201 (semantic-idle-scheduler-setup-timers)))
202 semantic-idle-scheduler-mode
)
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.)
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."
217 (list (or current-prefix-arg
218 (if semantic-idle-scheduler-mode
0 1))))
219 (setq semantic-idle-scheduler-mode
222 (prefix-numeric-value arg
)
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
)
233 (semantic-add-minor-mode 'semantic-idle-scheduler-mode
237 ;;; SERVICES services
239 ;; These are services for managing idle services.
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
248 (defun semantic-idle-scheduler-add (function)
249 "Schedule FUNCTION to occur during idle time."
250 (add-to-list 'semantic-idle-scheduler-queue function
))
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
)))
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)
268 (mapcar #'(lambda (b)
269 (and (buffer-file-name b
)
272 safe
;; This safe is not used, but could be.
275 (when (semantic-idle-scheduler-enabled-p)
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)
284 ;; Now loop over other buffers with same major mode, trying to
285 ;; update them as well. Stop on keypress.
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.
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.
305 (dolist (service semantic-idle-scheduler-queue
)
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
)
312 (when semantic-idle-scheduler-verbose-flag
313 (message "IDLE: execture service %s...done" service
))
316 ;; Finally loop over remaining buffers, trying to update them as
317 ;; well. Stop on keypress.
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)))))
325 (when semantic-idle-scheduler-verbose-flag
326 (message "IDLE: Core handler...done")))
328 (defun semantic-debug-idle-function ()
329 "Run the Semantic idle function with debugging turned on."
331 (let ((debug-on-error t
))
332 (semantic-idle-core-handler)
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))
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."
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
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 (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.")
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.")
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")
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.
511 Return non-nil if the refresh was successful.
512 Return nil if there is some sort of syntax error preventing a full
515 Does nothing if the current buffer doesn't need reparsing."
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.
522 ;; If the buffer was previously marked unparseable,
523 ;; then don't waste our time.
524 ((semantic-parse-tree-unparseable-p)
526 ;; The parse tree is already ok.
527 ((semantic-parse-tree-up-to-date-p)
530 ;; If the buffer might need a reparse and it is safe to do so,
532 (let* (;(semantic-working-type 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
539 ;; working-status-dynamic-type))
540 ;; (working-status-percentage-type
541 ;; (if semantic-idle-scheduler-no-working-message
543 ;; working-status-percentage-type))
546 ;; Let people hook into this, but don't let them hose
549 (run-hooks 'semantic-before-idle-scheduler-reparse-hook
)
550 (error (setq semantic-before-idle-scheduler-reparse-hook nil
)))
553 ;; Perform the parsing.
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))
560 ;; If we are here, it is because the lexical step failed,
561 ;; proably due to unterminated lists or something like that.
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
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)
577 ;; After updating the tags, handle any pending decorations for this
579 (require 'semantic
/decorate
/mode
)
580 (semantic-decorate-flush-pending-decorations (current-buffer))
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.
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"))))
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.
610 When this minor mode is enabled, `" (symbol-name mode
) "' is
611 turned on in every Semantic-supported buffer.")
614 (semantic-toggle-minor-mode-globally
617 (defcustom ,global nil
618 ,(concat "Non-nil if `" (symbol-name mode
) "' is enabled.")
620 :group
'semantic-modes
622 :require
'semantic
/idle
623 :initialize
'custom-initialize-default
624 :set
(lambda (sym val
)
625 (,global
(if val
1 -
1))))
628 ,(concat "Hook run at the end of function `" (symbol-name mode
) "'.")
633 (let ((km (make-sparse-keymap)))
635 ,(concat "Keymap for `" (symbol-name mode
) "'."))
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
)
643 ,(concat "Set up `" (symbol-name mode
) "'.
644 Return non-nil if the minor mode is enabled.")
646 (if (not (and (featurep 'semantic
) (semantic-active-p)))
648 ;; Disable minor mode if semantic stuff not available
650 (error "Buffer %s was not set up for parsing"
652 ;; Enable the mode mode
653 (semantic-idle-scheduler-add #',func
)
655 ;; Disable the mode mode
656 (semantic-idle-scheduler-remove #',func
)
660 (defun ,mode
(&optional arg
)
663 (list (or current-prefix-arg
668 (prefix-numeric-value arg
)
673 (if (called-interactively-p 'interactive
)
674 (message "%s %sabled"
676 (if ,mode
"en" "dis")))
677 (semantic-mode-line-update)
680 (semantic-add-minor-mode ',mode
681 "" ; idle schedulers are quiet?
685 ,(concat "Perform idle activity for the minor mode `"
686 (symbol-name mode
) "'.")
688 (put 'define-semantic-idle-service
'lisp-indent-function
1)
693 ;; A mode similar to eldoc using semantic
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'."
702 :type semantic-format-tag-custom-list
)
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)))))
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))))
721 (semantic-idle-summary-find-current-symbol-tag sym
))))
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
))))
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))
738 (require 'semantic
/analyze
)
739 (semantic-analyze-interesting-tag analysis
))))
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."
746 (semanticdb-find-default-throttle
747 (if (featurep 'semantic
/db-find
)
748 (remq 'unloaded semanticdb-find-default-throttle
)
752 ;; use whicever has success first.
754 (semantic-idle-summary-current-symbol-keyword)
756 (semantic-idle-summary-current-symbol-info-context)
758 (semantic-idle-summary-current-symbol-info-brutish)
761 (defvar semantic-idle-summary-out-of-context-faces
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.
768 "List of font-lock faces that indicate a useless summary context.
769 Those are generally faces used to highlight comments.
771 It might be useful to override this variable to add comment faces
772 specific to a major mode. For example, in jde mode:
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)))")
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
)
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.
794 (define-overloadable-function semantic-idle-summary-current-symbol-info
()
795 "Return a string message describing the current context.")
797 (make-obsolete-overload 'semantic-eldoc-current-symbol-info
798 'semantic-idle-summary-current-symbol-info
801 (defcustom semantic-idle-summary-mode-hook nil
802 "Hook run at the end of `semantic-idle-summary'."
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
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
))))
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,
830 When this minor mode is enabled, the echo area displays a summary
831 of the lexical token at point whenever Emacs is idle."
833 :group
'semantic-modes
834 (semantic-idle-summary-mode-setup)
835 (semantic-mode-line-update))
837 (defun semantic-idle-summary-refresh-echo-area ()
838 (and semantic-idle-summary-mode
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
))))
847 (defun semantic-idle-summary-mode-setup ()
848 "Set up `semantic-idle-summary-mode'."
849 (if semantic-idle-summary-mode
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"
858 (semantic-idle-scheduler-add 'semantic-idle-summary-idle-function
)
859 (add-hook 'pre-command-hook
'semantic-idle-summary-refresh-echo-area t
))
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
)
865 (semantic-add-minor-mode 'semantic-idle-summary-mode
"")
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.
872 When this minor mode is enabled, `semantic-idle-summary-mode' is
873 turned on in every Semantic-supported buffer."
876 :group
'semantic-modes
877 (semantic-toggle-minor-mode-globally
878 'semantic-idle-summary-mode
879 (if global-semantic-idle-summary-mode
1 -
1)))
882 ;;; Current symbol highlight
884 ;; This mode will use context analysis to perform highlighting
885 ;; of all uses of the symbol that is under the cursor.
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.")
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."
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.
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
)
916 (pulse-momentary-highlight-region
917 (semantic-overlay-start region
)
919 semantic-idle-summary-highlight-face
)))
922 (let ((start (aref region
0))
923 (end (aref region
1)))
925 (when buffer
(set-buffer buffer
))
926 ;; As a vector, we have no filename. Perhaps it is a
928 (when (and (<= end
(point-max))
929 (pos-visible-in-window-p
930 start
(get-buffer-window (current-buffer) 'visible
)))
932 (when (re-search-forward
933 (regexp-quote (semantic-tag-name tag
))
935 ;; This is likely it, give it a try.
936 (pulse-momentary-highlight-region
937 start
(if (<= end
(point-at-eol)) end
939 semantic-idle-summary-highlight-face
)))
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."
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.
957 ;; Highlight the original tag? Protect against problems.
959 (semantic-idle-summary-maybe-highlight target
)
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
)
971 (semantic-tag-start tag
)
972 (semantic-tag-end tag
)))
976 ;;; Completion Popup Mode
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.
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.
990 ;; Don't go loading in oodles of header libraries in
992 (semanticdb-find-default-throttle
993 (if (featurep 'semantic
/db-find
)
994 (remq 'unloaded semanticdb-find-default-throttle
)
998 (require 'semantic
/complete
)
999 (semantic-complete-analyze-inline-idle)
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.
1009 This minor mode only takes effect if Semantic is active and
1010 `semantic-idle-scheduler-mode' is enabled.
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.
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
1023 \\{semantic-complete-inline-map}"
1024 ;; Add the ability to override sometime.
1025 (semantic-idle-completion-list-default))
1027 (provide 'semantic
/idle
)
1030 ;; generated-autoload-file: "loaddefs.el"
1031 ;; generated-autoload-load-name: "semantic/idle"
1034 ;; arch-tag: 4bfd54da-5023-4cc1-91ae-e1fefc1a8d1b
1035 ;;; semantic-idle.el ends here