1 ;;; idle.el --- Schedule parsing tasks in idle time
3 ;; Copyright (C) 2003-2006, 2008-2012 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Originally, `semantic-auto-parse-mode' handled refreshing the
26 ;; tags in a buffer in idle time. Other activities can be scheduled
27 ;; in idle time, all of which require up-to-date tag tables.
28 ;; Having a specialized idle time scheduler that first refreshes
29 ;; the tags buffer, and then enables other idle time tasks reduces
30 ;; the amount of work needed. Any specialized idle tasks need not
31 ;; ask for a fresh tags list.
33 ;; NOTE ON SEMANTIC_ANALYZE
35 ;; Some of the idle modes use the semantic analyzer. The analyzer
36 ;; automatically caches the created context, so it is shared amongst
37 ;; all idle modes that will need it.
40 (require 'semantic
/ctxt
)
41 (require 'semantic
/format
)
42 (require 'semantic
/tag
)
45 ;; For the semantic-find-tags-by-name macro.
46 (eval-when-compile (require 'semantic
/find
))
48 (defvar eldoc-last-message
)
49 (declare-function eldoc-message
"eldoc")
50 (declare-function semantic-analyze-interesting-tag
"semantic/analyze")
51 (declare-function semantic-analyze-unsplit-name
"semantic/analyze/fcn")
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 semantic-idle-scheduler-mode-hook nil
133 "Hook run at the end of the function `semantic-idle-scheduler-mode'."
137 (defvar semantic-idle-scheduler-mode nil
138 "Non-nil if idle-scheduler minor mode is enabled.
139 Use the command `semantic-idle-scheduler-mode' to change this variable.")
140 (make-variable-buffer-local 'semantic-idle-scheduler-mode
)
142 (defcustom semantic-idle-scheduler-max-buffer-size
0
143 "*Maximum size in bytes of buffers where idle-scheduler is enabled.
144 If this value is less than or equal to 0, idle-scheduler is enabled in
145 all buffers regardless of their size."
149 (defsubst semantic-idle-scheduler-enabled-p
()
150 "Return non-nil if idle-scheduler is enabled for this buffer.
151 idle-scheduler is disabled when debugging or if the buffer size
152 exceeds the `semantic-idle-scheduler-max-buffer-size' threshold."
153 (and semantic-idle-scheduler-mode
154 (not (and (boundp 'semantic-debug-enabled
)
155 semantic-debug-enabled
))
156 (not semantic-lex-debug
)
157 (or (<= semantic-idle-scheduler-max-buffer-size
0)
158 (< (buffer-size) semantic-idle-scheduler-max-buffer-size
))))
161 (define-minor-mode semantic-idle-scheduler-mode
162 "Minor mode to auto parse buffer following a change.
163 When this mode is off, a buffer is only rescanned for tokens when
164 some command requests the list of available tokens. When idle-scheduler
165 is enabled, Emacs periodically checks to see if the buffer is out of
166 date, and reparses while the user is idle (not typing.)
168 With prefix argument ARG, turn on if positive, otherwise off. The
169 minor mode can be turned on only if semantic feature is available and
170 the current buffer was set up for parsing. Return non-nil if the
171 minor mode is enabled."
173 (if semantic-idle-scheduler-mode
174 (if (not (and (featurep 'semantic
) (semantic-active-p)))
176 ;; Disable minor mode if semantic stuff not available
177 (setq semantic-idle-scheduler-mode nil
)
178 (error "Buffer %s was not set up idle time scheduling"
180 (semantic-idle-scheduler-setup-timers))))
182 (semantic-add-minor-mode 'semantic-idle-scheduler-mode
185 ;;; SERVICES services
187 ;; These are services for managing idle services.
189 (defvar semantic-idle-scheduler-queue nil
190 "List of functions to execute during idle time.
191 These functions will be called in the current buffer after that
192 buffer has had its tags made up to date. These functions
193 will not be called if there are errors parsing the
196 (defun semantic-idle-scheduler-add (function)
197 "Schedule FUNCTION to occur during idle time."
198 (add-to-list 'semantic-idle-scheduler-queue function
))
200 (defun semantic-idle-scheduler-remove (function)
201 "Unschedule FUNCTION to occur during idle time."
202 (setq semantic-idle-scheduler-queue
203 (delete function semantic-idle-scheduler-queue
)))
207 (defun semantic-idle-core-handler ()
208 "Core idle function that handles reparsing.
209 And also manages services that depend on tag values."
210 (when semantic-idle-scheduler-verbose-flag
211 (message "IDLE: Core handler..."))
212 (semantic-exit-on-input 'idle-timer
213 (let* ((inhibit-quit nil
)
214 (buffers (delq (current-buffer)
216 (mapcar #'(lambda (b)
217 (and (buffer-file-name b
)
220 safe
;; This safe is not used, but could be.
223 (when (semantic-idle-scheduler-enabled-p)
225 ;; First, reparse the current buffer.
226 (setq mode major-mode
227 safe
(semantic-safe "Idle Parse Error: %S"
228 ;(error "Goofy error 1")
229 (semantic-idle-scheduler-refresh-tags)
232 ;; Now loop over other buffers with same major mode, trying to
233 ;; update them as well. Stop on keypress.
235 (semantic-throw-on-input 'parsing-mode-buffers
)
236 (with-current-buffer b
237 (if (eq major-mode mode
)
238 (and (semantic-idle-scheduler-enabled-p)
239 (semantic-safe "Idle Parse Error: %S"
240 ;(error "Goofy error")
241 (semantic-idle-scheduler-refresh-tags)))
242 (push (current-buffer) others
))))
243 (setq buffers others
))
244 ;; If re-parse of current buffer completed, evaluate all other
245 ;; services. Stop on keypress.
247 ;; NOTE ON COMMENTED SAFE HERE
248 ;; We used to not execute the services if the buffer was
249 ;; unparsable. We now assume that they are lexically
250 ;; safe to do, because we have marked the buffer unparsable
251 ;; if there was a problem.
253 (dolist (service semantic-idle-scheduler-queue
)
255 (semantic-throw-on-input 'idle-queue
)
256 (when semantic-idle-scheduler-verbose-flag
257 (message "IDLE: execute service %s..." service
))
258 (semantic-safe (format "Idle Service Error %s: %%S" service
)
260 (when semantic-idle-scheduler-verbose-flag
261 (message "IDLE: execute service %s...done" service
))
264 ;; Finally loop over remaining buffers, trying to update them as
265 ;; well. Stop on keypress.
268 (semantic-throw-on-input 'parsing-other-buffers
)
269 (with-current-buffer b
270 (and (semantic-idle-scheduler-enabled-p)
271 (semantic-idle-scheduler-refresh-tags)))))
273 (when semantic-idle-scheduler-verbose-flag
274 (message "IDLE: Core handler...done")))
276 (defun semantic-debug-idle-function ()
277 "Run the Semantic idle function with debugging turned on."
279 (let ((debug-on-error t
))
280 (semantic-idle-core-handler)
283 (defun semantic-idle-scheduler-function ()
284 "Function run when after `semantic-idle-scheduler-idle-time'.
285 This function will reparse the current buffer, and if successful,
286 call additional functions registered with the timer calls."
287 (when (zerop (recursion-depth))
288 (let ((debug-on-error nil
))
289 (save-match-data (semantic-idle-core-handler))
295 ;; Unlike the shorter timer, the WORK timer will kick of tasks that
296 ;; may take a long time to complete.
297 (defcustom semantic-idle-work-parse-neighboring-files-flag nil
298 "*Non-nil means to parse files in the same dir as the current buffer.
299 Disable to prevent lots of excessive parsing in idle time."
303 (defcustom semantic-idle-work-update-headers-flag nil
304 "*Non-nil means to parse through header files in idle time.
305 Disable to prevent idle time parsing of many files. If completion
306 is called that work will be done then instead."
310 (defun semantic-idle-work-for-one-buffer (buffer)
311 "Do long-processing work for BUFFER.
312 Uses `semantic-safe' and returns the output.
313 Returns t if all processing succeeded."
314 (with-current-buffer buffer
317 (semantic-safe "Idle Work Parse Error: %S"
318 (semantic-idle-scheduler-refresh-tags)
321 ;; Option to disable this work.
322 semantic-idle-work-update-headers-flag
324 ;; Force all our include files to get read in so we
325 ;; are ready to provide good smart completion and idle
326 ;; summary information
327 (semantic-safe "Idle Work Including Error: %S"
328 ;; Get the include related path.
329 (when (and (featurep 'semantic
/db
) (semanticdb-minor-mode-p))
330 (require 'semantic
/db-find
)
331 (semanticdb-find-translate-path buffer nil
)
335 ;; Pre-build the typecaches as needed.
336 (semantic-safe "Idle Work Typecaching Error: %S"
337 (when (featurep 'semantic
/db-typecache
)
338 (semanticdb-typecache-refresh-for-buffer buffer
))
343 (defun semantic-idle-work-core-handler ()
344 "Core handler for idle work processing of long running tasks.
345 Visits Semantic controlled buffers, and makes sure all needed
346 include files have been parsed, and that the typecache is up to date.
347 Uses `semantic-idle-work-for-on-buffer' to do the work."
350 (semantic-exit-on-input 'idle-work-timer
351 (let* ((inhibit-quit nil
)
352 (cb (current-buffer))
353 (buffers (delq (current-buffer)
355 (mapcar #'(lambda (b)
356 (and (buffer-file-name b
)
360 ;; First, handle long tasks in the current buffer.
361 (when (semantic-idle-scheduler-enabled-p)
363 (setq safe
(semantic-idle-work-for-one-buffer (current-buffer))
365 (when (not safe
) (push (current-buffer) errbuf
))
367 ;; Now loop over other buffers with same major mode, trying to
368 ;; update them as well. Stop on keypress.
370 (semantic-throw-on-input 'parsing-mode-buffers
)
371 (with-current-buffer b
372 (when (semantic-idle-scheduler-enabled-p)
373 (and (semantic-idle-scheduler-enabled-p)
374 (unless (semantic-idle-work-for-one-buffer (current-buffer))
375 (push (current-buffer) errbuf
)))
379 (when (and (featurep 'semantic
/db
) (semanticdb-minor-mode-p))
381 (semanticdb-save-all-db-idle)
383 ;; Parse up files near our active buffer
384 (when semantic-idle-work-parse-neighboring-files-flag
385 (semantic-safe "Idle Work Parse Neighboring Files: %S"
387 (semantic-idle-scheduler-work-parse-neighboring-files))
390 ;; Save everything... again
391 (semanticdb-save-all-db-idle)
394 ;; Done w/ processing
403 (format "done with 1 error in %s" (car errbuf
)))
405 (format "done with errors in %d buffers."
406 (length errbuf
)))))))
408 (defun semantic-debug-idle-work-function ()
409 "Run the Semantic idle work function with debugging turned on."
411 (let ((debug-on-error t
))
412 (semantic-idle-work-core-handler)
415 (defun semantic-idle-scheduler-work-function ()
416 "Function run when after `semantic-idle-scheduler-work-idle-time'.
417 This routine handles difficult tasks that require a lot of parsing, such as
418 parsing all the header files used by our active sources, or building up complex
420 (when semantic-idle-scheduler-verbose-flag
421 (message "Long Work Idle Timer..."))
422 (let ((exit-type (save-match-data
423 (semantic-idle-work-core-handler))))
424 (when semantic-idle-scheduler-verbose-flag
425 (message "Long Work Idle Timer...%s" exit-type
)))
428 (defun semantic-idle-scheduler-work-parse-neighboring-files ()
429 "Parse all the files in similar directories to buffers being edited."
430 ;; Let's check to see if EDE matters.
431 (let ((ede-auto-add-method 'never
))
432 (dolist (a auto-mode-alist
)
433 (when (eq (cdr a
) major-mode
)
434 (dolist (file (directory-files default-directory t
(car a
) t
))
435 (semantic-throw-on-input 'parsing-mode-buffers
)
437 (semanticdb-file-table-object file
)
444 ;; Reparsing is installed as semantic idle service.
445 ;; This part ALWAYS happens, and other services occur
448 (defvar semantic-before-idle-scheduler-reparse-hook nil
449 "Hook run before option `semantic-idle-scheduler' begins parsing.
450 If any hook function throws an error, this variable is reset to nil.
451 This hook is not protected from lexical errors.")
453 (defvar semantic-after-idle-scheduler-reparse-hook nil
454 "Hook run after option `semantic-idle-scheduler' has parsed.
455 If any hook function throws an error, this variable is reset to nil.
456 This hook is not protected from lexical errors.")
458 (semantic-varalias-obsolete 'semantic-before-idle-scheduler-reparse-hooks
459 'semantic-before-idle-scheduler-reparse-hook
"23.2")
460 (semantic-varalias-obsolete 'semantic-after-idle-scheduler-reparse-hooks
461 'semantic-after-idle-scheduler-reparse-hook
"23.2")
463 (defun semantic-idle-scheduler-refresh-tags ()
464 "Refreshes the current buffer's tags.
465 This is called by `semantic-idle-scheduler-function' to update the
466 tags in the current buffer.
468 Return non-nil if the refresh was successful.
469 Return nil if there is some sort of syntax error preventing a full
472 Does nothing if the current buffer doesn't need reparsing."
475 ;; These checks actually occur in `semantic-fetch-tags', but if we
476 ;; do them here, then all the bovination hooks are not run, and
477 ;; we save lots of time.
479 ;; If the buffer was previously marked unparsable,
480 ;; then don't waste our time.
481 ((semantic-parse-tree-unparseable-p)
483 ;; The parse tree is already ok.
484 ((semantic-parse-tree-up-to-date-p)
487 ;; If the buffer might need a reparse and it is safe to do so,
489 (let* (;(semantic-working-type nil)
491 ;; (working-use-echo-area-p
492 ;; (not semantic-idle-scheduler-working-in-modeline-flag))
493 ;; (working-status-dynamic-type
494 ;; (if semantic-idle-scheduler-no-working-message
496 ;; working-status-dynamic-type))
497 ;; (working-status-percentage-type
498 ;; (if semantic-idle-scheduler-no-working-message
500 ;; working-status-percentage-type))
503 ;; Let people hook into this, but don't let them hose
506 (run-hooks 'semantic-before-idle-scheduler-reparse-hook
)
507 (error (setq semantic-before-idle-scheduler-reparse-hook nil
)))
510 ;; Perform the parsing.
512 (when semantic-idle-scheduler-verbose-flag
513 (message "IDLE: reparse %s..." (buffer-name)))
514 (when (semantic-lex-catch-errors idle-scheduler
515 (save-excursion (semantic-fetch-tags))
517 ;; If we are here, it is because the lexical step failed,
518 ;; probably due to unterminated lists or something like that.
520 ;; We do nothing, and just wait for the next idle timer
521 ;; to go off. In the meantime, remember this, and make sure
522 ;; no other idle services can get executed.
523 (setq lexically-safe nil
))
524 (when semantic-idle-scheduler-verbose-flag
525 (message "IDLE: reparse %s...done" (buffer-name))))
526 ;; Let people hook into this, but don't let them hose
529 (run-hooks 'semantic-after-idle-scheduler-reparse-hook
)
530 (error (setq semantic-after-idle-scheduler-reparse-hook nil
))))
531 ;; Return if we are lexically safe (from prog1)
534 ;; After updating the tags, handle any pending decorations for this
536 (require 'semantic
/decorate
/mode
)
537 (semantic-decorate-flush-pending-decorations (current-buffer))
543 ;; Idle Services are minor modes which enable or disable a services in
544 ;; the idle scheduler. Creating a new services only requires calling
545 ;; `semantic-create-idle-services' which does all the setup
546 ;; needed to create the minor mode that will enable or disable
547 ;; a services. The services must provide a single function.
549 ;; FIXME doc is incomplete.
550 (defmacro define-semantic-idle-service
(name doc
&rest forms
)
551 "Create a new idle services with NAME.
552 DOC will be a documentation string describing FORMS.
553 FORMS will be called during idle time after the current buffer's
554 semantic tag information has been updated.
555 This routine creates the following functions and variables:"
556 (let ((global (intern (concat "global-" (symbol-name name
) "-mode")))
557 (mode (intern (concat (symbol-name name
) "-mode")))
558 (hook (intern (concat (symbol-name name
) "-mode-hook")))
559 (map (intern (concat (symbol-name name
) "-mode-map")))
560 (func (intern (concat (symbol-name name
) "-idle-function"))))
563 (define-minor-mode ,global
564 ,(concat "Toggle " (symbol-name global
) ".
565 With ARG, turn the minor mode on if ARG is positive, off otherwise.
567 When this minor mode is enabled, `" (symbol-name mode
) "' is
568 turned on in every Semantic-supported buffer.")
571 :group
'semantic-modes
572 :require
'semantic
/idle
573 (semantic-toggle-minor-mode-globally
574 ',mode
(if ,global
1 -
1)))
576 ;; FIXME: Get rid of this when define-minor-mode does it for us.
578 ,(concat "Hook run at the end of function `" (symbol-name mode
) "'.")
583 (let ((km (make-sparse-keymap)))
585 ,(concat "Keymap for `" (symbol-name mode
) "'."))
587 (define-minor-mode ,mode
591 (if (not (and (featurep 'semantic
) (semantic-active-p)))
593 ;; Disable minor mode if semantic stuff not available
595 (error "Buffer %s was not set up for parsing"
597 ;; Enable the mode mode
598 (semantic-idle-scheduler-add #',func
))
599 ;; Disable the mode mode
600 (semantic-idle-scheduler-remove #',func
)))
602 (semantic-add-minor-mode ',mode
603 "") ; idle schedulers are quiet?
606 ,(concat "Perform idle activity for the minor mode `"
607 (symbol-name mode
) "'.")
609 (put 'define-semantic-idle-service
'lisp-indent-function
1)
614 ;; A mode similar to eldoc using semantic
615 (defcustom semantic-idle-truncate-long-summaries t
616 "Truncate summaries that are too long to fit in the minibuffer.
617 This can prevent minibuffer resizing in idle time."
621 (defcustom semantic-idle-summary-function
622 'semantic-format-tag-summarize-with-file
623 "Function to call when displaying tag information during idle time.
624 This function should take a single argument, a Semantic tag, and
625 return a string to display.
626 Some useful functions are found in `semantic-format-tag-functions'."
628 :type semantic-format-tag-custom-list
)
630 (defsubst semantic-idle-summary-find-current-symbol-tag
(sym)
631 "Search for a semantic tag with name SYM in database tables.
632 Return the tag found or nil if not found.
633 If semanticdb is not in use, use the current buffer only."
634 (car (if (and (featurep 'semantic
/db
)
635 semanticdb-current-database
636 (require 'semantic
/db-find
))
637 (cdar (semanticdb-deep-find-tags-by-name sym
))
638 (semantic-deep-find-tags-by-name sym
(current-buffer)))))
640 (defun semantic-idle-summary-current-symbol-info-brutish ()
641 "Return a string message describing the current context.
642 Gets a symbol with `semantic-ctxt-current-thing' and then
643 tries to find it with a deep targeted search."
644 ;; Try the current "thing".
645 (let ((sym (car (semantic-ctxt-current-thing))))
647 (semantic-idle-summary-find-current-symbol-tag sym
))))
649 (defun semantic-idle-summary-current-symbol-keyword ()
650 "Return a string message describing the current symbol.
651 Returns a value only if it is a keyword."
652 ;; Try the current "thing".
653 (let ((sym (car (semantic-ctxt-current-thing))))
654 (if (and sym
(semantic-lex-keyword-p sym
))
655 (semantic-lex-keyword-get sym
'summary
))))
657 (defun semantic-idle-summary-current-symbol-info-context ()
658 "Return a string message describing the current context.
659 Use the semantic analyzer to find the symbol information."
660 (let ((analysis (condition-case nil
661 (semantic-analyze-current-context (point))
664 (require 'semantic
/analyze
)
665 (semantic-analyze-interesting-tag analysis
))))
667 (defun semantic-idle-summary-current-symbol-info-default ()
668 "Return a string message describing the current context.
669 This function will disable loading of previously unloaded files
670 by semanticdb as a time-saving measure."
671 (semanticdb-without-unloaded-file-searches
673 ;; use whichever has success first.
675 (semantic-idle-summary-current-symbol-keyword)
677 (semantic-idle-summary-current-symbol-info-context)
679 (semantic-idle-summary-current-symbol-info-brutish)
682 (defvar semantic-idle-summary-out-of-context-faces
684 font-lock-comment-face
685 font-lock-string-face
686 font-lock-doc-string-face
; XEmacs.
687 font-lock-doc-face
; Emacs 21 and later.
689 "List of font-lock faces that indicate a useless summary context.
690 Those are generally faces used to highlight comments.
692 It might be useful to override this variable to add comment faces
693 specific to a major mode. For example, in jde mode:
695 \(defvar-mode-local jde-mode semantic-idle-summary-out-of-context-faces
696 (append (default-value 'semantic-idle-summary-out-of-context-faces)
697 '(jde-java-font-lock-doc-tag-face
698 jde-java-font-lock-link-face
699 jde-java-font-lock-bold-face
700 jde-java-font-lock-underline-face
701 jde-java-font-lock-pre-face
702 jde-java-font-lock-code-face)))")
704 (defun semantic-idle-summary-useful-context-p ()
705 "Non-nil if we should show a summary based on context."
706 (if (and (boundp 'font-lock-mode
)
708 (memq (get-text-property (point) 'face
)
709 semantic-idle-summary-out-of-context-faces
))
710 ;; The best I can think of at the moment is to disable
711 ;; in comments by detecting with font-lock.
715 (define-overloadable-function semantic-idle-summary-current-symbol-info
()
716 "Return a string message describing the current context.")
718 (make-obsolete-overload 'semantic-eldoc-current-symbol-info
719 'semantic-idle-summary-current-symbol-info
722 (defcustom semantic-idle-summary-mode-hook nil
723 "Hook run at the end of `semantic-idle-summary'."
727 (defun semantic-idle-summary-idle-function ()
728 "Display a tag summary of the lexical token under the cursor.
729 Call `semantic-idle-summary-current-symbol-info' for getting the
730 current tag to display information."
731 (or (eq major-mode
'emacs-lisp-mode
)
732 (not (semantic-idle-summary-useful-context-p))
733 (let* ((found (semantic-idle-summary-current-symbol-info))
734 (str (cond ((stringp found
) found
)
735 ((semantic-tag-p found
)
736 (funcall semantic-idle-summary-function
738 ;; Show the message with eldoc functions
739 (unless (and str
(boundp 'eldoc-echo-area-use-multiline-p
)
740 eldoc-echo-area-use-multiline-p
)
741 (let ((w (1- (window-width (minibuffer-window)))))
742 (if (> (length str
) w
)
743 (setq str
(substring str
0 w
)))))
744 ;; I borrowed some bits from eldoc to shorten the
746 (when semantic-idle-truncate-long-summaries
747 (let ((ea-width (1- (window-width (minibuffer-window))))
748 (strlen (length str
)))
749 (when (> strlen ea-width
)
750 (setq str
(substring str
0 ea-width
)))))
752 (eldoc-message str
))))
754 (define-minor-mode semantic-idle-summary-mode
755 "Toggle Semantic Idle Summary mode.
756 With ARG, turn Semantic Idle Summary mode on if ARG is positive,
759 When this minor mode is enabled, the echo area displays a summary
760 of the lexical token at point whenever Emacs is idle."
762 :group
'semantic-modes
763 (if semantic-idle-summary-mode
766 (unless (and (featurep 'semantic
) (semantic-active-p))
767 ;; Disable minor mode if semantic stuff not available
768 (setq semantic-idle-summary-mode nil
)
769 (error "Buffer %s was not set up for parsing"
772 (semantic-idle-scheduler-add 'semantic-idle-summary-idle-function
)
773 (add-hook 'pre-command-hook
'semantic-idle-summary-refresh-echo-area t
))
775 (semantic-idle-scheduler-remove 'semantic-idle-summary-idle-function
)
776 (remove-hook 'pre-command-hook
'semantic-idle-summary-refresh-echo-area t
)))
778 (defun semantic-idle-summary-refresh-echo-area ()
779 (and semantic-idle-summary-mode
781 (if (and (not executing-kbd-macro
)
782 (not (and (boundp 'edebug-active
) edebug-active
))
783 (not cursor-in-echo-area
)
784 (not (eq (selected-window) (minibuffer-window))))
785 (eldoc-message eldoc-last-message
)
786 (setq eldoc-last-message nil
))))
788 (semantic-add-minor-mode 'semantic-idle-summary-mode
"")
790 (define-minor-mode global-semantic-idle-summary-mode
791 "Toggle Global Semantic Idle Summary mode.
792 With ARG, turn Global Semantic Idle Summary mode on if ARG is
793 positive, off otherwise.
795 When this minor mode is enabled, `semantic-idle-summary-mode' is
796 turned on in every Semantic-supported buffer."
799 :group
'semantic-modes
800 (semantic-toggle-minor-mode-globally
801 'semantic-idle-summary-mode
802 (if global-semantic-idle-summary-mode
1 -
1)))
805 ;;; Current symbol highlight
807 ;; This mode will use context analysis to perform highlighting
808 ;; of all uses of the symbol that is under the cursor.
810 ;; This is to mimic the Eclipse tool of a similar nature.
811 (defvar semantic-idle-symbol-highlight-face
'region
812 "Face used for highlighting local symbols.")
814 (defun semantic-idle-symbol-maybe-highlight (tag)
815 "Perhaps add highlighting to the symbol represented by TAG.
816 TAG was found as the symbol under point. If it happens to be
817 visible, then highlight it."
819 (let* ((region (when (and (semantic-tag-p tag
)
820 (semantic-tag-with-position-p tag
))
821 (semantic-tag-overlay tag
)))
822 (file (when (and (semantic-tag-p tag
)
823 (semantic-tag-with-position-p tag
))
824 (semantic-tag-file-name tag
)))
825 (buffer (when file
(get-file-buffer file
)))
826 ;; We use pulse, but we don't want the flashy version,
827 ;; just the stable version.
830 (cond ((semantic-overlay-p region
)
831 (with-current-buffer (semantic-overlay-buffer region
)
832 (goto-char (semantic-overlay-start region
))
833 (when (pos-visible-in-window-p
834 (point) (get-buffer-window (current-buffer) 'visible
))
835 (if (< (semantic-overlay-end region
) (point-at-eol))
836 (pulse-momentary-highlight-overlay
837 region semantic-idle-symbol-highlight-face
)
839 (pulse-momentary-highlight-region
840 (semantic-overlay-start region
)
842 semantic-idle-symbol-highlight-face
)))
845 (let ((start (aref region
0))
846 (end (aref region
1)))
848 (when buffer
(set-buffer buffer
))
849 ;; As a vector, we have no filename. Perhaps it is a
851 (when (and (<= end
(point-max))
852 (pos-visible-in-window-p
853 start
(get-buffer-window (current-buffer) 'visible
)))
855 (when (re-search-forward
856 (regexp-quote (semantic-tag-name tag
))
858 ;; This is likely it, give it a try.
859 (pulse-momentary-highlight-region
860 start
(if (<= end
(point-at-eol)) end
862 semantic-idle-symbol-highlight-face
)))
866 (define-semantic-idle-service semantic-idle-local-symbol-highlight
867 "Highlight the tag and symbol references of the symbol under point.
868 Call `semantic-analyze-current-context' to find the reference tag.
869 Call `semantic-symref-hits-in-region' to identify local references."
871 (when (semantic-idle-summary-useful-context-p)
873 (semanticdb-without-unloaded-file-searches
874 (semantic-analyze-current-context)))
875 (Hbounds (when ctxt
(oref ctxt bounds
)))
876 (target (when ctxt
(car (reverse (oref ctxt prefix
)))))
877 (tag (semantic-current-tag))
878 ;; We use pulse, but we don't want the flashy version,
879 ;; just the stable version.
882 ;; Highlight the original tag? Protect against problems.
884 (semantic-idle-symbol-maybe-highlight target
)
886 ;; Identify all hits in this current tag.
887 (when (semantic-tag-p target
)
888 (require 'semantic
/symref
/filter
)
889 (semantic-symref-hits-in-region
890 target
(lambda (start end prefix
)
891 (when (/= start
(car Hbounds
))
892 (pulse-momentary-highlight-region
893 start end semantic-idle-symbol-highlight-face
))
894 (semantic-throw-on-input 'symref-highlight
)
896 (semantic-tag-start tag
)
897 (semantic-tag-end tag
)))
902 (define-minor-mode global-semantic-idle-scheduler-mode
903 "Toggle global use of option `semantic-idle-scheduler-mode'.
904 The idle scheduler will automatically reparse buffers in idle time,
905 and then schedule other jobs setup with `semantic-idle-scheduler-add'.
906 If ARG is positive or nil, enable, if it is negative, disable."
909 :group
'semantic-modes
910 ;; When turning off, disable other idle modes.
911 (when (null global-semantic-idle-scheduler-mode
)
912 (global-semantic-idle-summary-mode -
1)
913 (global-semantic-idle-local-symbol-highlight-mode -
1)
914 (global-semantic-idle-completions-mode -
1))
915 (semantic-toggle-minor-mode-globally
916 'semantic-idle-scheduler-mode
917 (if global-semantic-idle-scheduler-mode
1 -
1)))
920 ;;; Completion Popup Mode
922 ;; This mode uses tooltips to display a (hopefully) short list of possible
923 ;; completions available for the text under point. It provides
924 ;; NO provision for actually filling in the values from those completions.
925 (defun semantic-idle-completions-end-of-symbol-p ()
926 "Return non-nil if the cursor is at the END of a symbol.
927 If the cursor is in the middle of a symbol, then we shouldn't be
928 doing fancy completions."
929 (not (looking-at "\\w\\|\\s_")))
931 (defun semantic-idle-completion-list-default ()
932 "Calculate and display a list of completions."
933 (when (and (semantic-idle-summary-useful-context-p)
934 (semantic-idle-completions-end-of-symbol-p))
935 ;; This mode can be fragile. Ignore problems.
936 ;; If something doesn't do what you expect, run
937 ;; the below command by hand instead.
939 (semanticdb-without-unloaded-file-searches
941 (semantic-complete-analyze-inline-idle)
946 (define-semantic-idle-service semantic-idle-completions
947 "Toggle Semantic Idle Completions mode.
948 With ARG, turn Semantic Idle Completions mode on if ARG is
949 positive, off otherwise.
951 This minor mode only takes effect if Semantic is active and
952 `semantic-idle-scheduler-mode' is enabled.
954 When enabled, Emacs displays a list of possible completions at
955 idle time. The method for displaying completions is given by
956 `semantic-complete-inline-analyzer-idle-displayor-class'; the
957 default is to show completions inline.
959 While a completion is displayed, RET accepts the completion; M-n
960 and M-p cycle through completion alternatives; TAB attempts to
961 complete as far as possible, and cycles if no additional
962 completion is possible; and any other command cancels the
965 \\{semantic-complete-inline-map}"
966 ;; Add the ability to override sometime.
967 (semantic-idle-completion-list-default))
970 ;;; Breadcrumbs for tag under point
972 ;; Service that displays a breadcrumbs indication of the tag under
973 ;; point and its parents in the header or mode line.
976 (defcustom semantic-idle-breadcrumbs-display-function
977 #'semantic-idle-breadcrumbs--display-in-header-line
978 "Function to display the tag under point in idle time.
979 This function should take a list of Semantic tags as its only
980 argument. The tags are sorted according to their nesting order,
981 starting with the outermost tag. The function should call
982 `semantic-idle-breadcrumbs-format-tag-list-function' to convert
983 the tag list into a string."
986 (const :tag
"Display in header line"
987 semantic-idle-breadcrumbs--display-in-header-line
)
988 (const :tag
"Display in mode line"
989 semantic-idle-breadcrumbs--display-in-mode-line
)
990 (function :tag
"Other function")))
992 (defcustom semantic-idle-breadcrumbs-format-tag-list-function
993 #'semantic-idle-breadcrumbs--format-linear
994 "Function to format the list of tags containing point.
995 This function should take a list of Semantic tags and an optional
996 maximum length of the produced string as its arguments. The
997 maximum length is a hint and can be ignored. When the maximum
998 length is omitted, an unconstrained string should be
999 produced. The tags are sorted according to their nesting order,
1000 starting with the outermost tag. Single tags should be formatted
1001 using `semantic-idle-breadcrumbs-format-tag-function' unless
1002 special formatting is required."
1005 (const :tag
"Format tags as list, innermost last"
1006 semantic-idle-breadcrumbs--format-linear
)
1007 (const :tag
"Innermost tag with details, followed by remaining tags"
1008 semantic-idle-breadcrumbs--format-innermost-first
)
1009 (function :tag
"Other function")))
1011 (defcustom semantic-idle-breadcrumbs-format-tag-function
1012 #'semantic-format-tag-abbreviate
1013 "Function to call to format information about tags.
1014 This function should take a single argument, a Semantic tag, and
1015 return a string to display.
1016 Some useful functions are found in `semantic-format-tag-functions'."
1018 :type semantic-format-tag-custom-list
)
1020 (defcustom semantic-idle-breadcrumbs-separator
'mode-specific
1021 "Specify how to separate tags in the breadcrumbs string.
1022 An arbitrary string or a mode-specific scope nesting
1023 string (like, for example, \"::\" in C++, or \".\" in Java) can
1027 (const :tag
"Use mode specific separator"
1029 (string :tag
"Specify separator string")))
1031 (defcustom semantic-idle-breadcrumbs-header-line-prefix
1032 semantic-stickyfunc-indent-string
;; TODO not optimal
1033 "String used to indent the breadcrumbs string.
1034 Customize this string to match the space used by scrollbars and
1039 (defvar semantic-idle-breadcrumbs-popup-menu nil
1040 "Menu used when a tag displayed by `semantic-idle-breadcrumbs-mode' is clicked.")
1042 (defun semantic-idle-breadcrumbs--popup-menu (event)
1043 "Popup a menu that displays things to do to the clicked tag.
1044 Argument EVENT describes the event that caused this function to
1047 (let ((old-window (selected-window))
1048 (window (semantic-event-window event
)))
1049 (select-window window t
)
1050 (semantic-popup-menu semantic-idle-breadcrumbs-popup-menu
)
1051 (select-window old-window
)))
1053 (defmacro semantic-idle-breadcrumbs--tag-function
(function)
1054 "Return lambda expression calling FUNCTION when called from a popup."
1057 (let* ((old-window (selected-window))
1058 (window (semantic-event-window event
))
1059 (column (car (nth 6 (nth 1 event
)))) ;; TODO semantic-event-column?
1061 (select-window window t
)
1063 (text-properties-at column header-line-format
)
1066 (select-window old-window
)))
1069 ;; TODO does this work for mode-line case?
1070 (defvar semantic-idle-breadcrumbs-popup-map
1071 (let ((map (make-sparse-keymap)))
1072 ;; mouse-1 goes to clicked tag
1074 [ header-line mouse-1
]
1075 (semantic-idle-breadcrumbs--tag-function
1076 semantic-go-to-tag
))
1077 ;; mouse-3 pops up a context menu
1079 [ header-line mouse-3
]
1080 'semantic-idle-breadcrumbs--popup-menu
)
1082 "Keymap for semantic idle breadcrumbs minor mode.")
1085 semantic-idle-breadcrumbs-popup-menu
1086 semantic-idle-breadcrumbs-popup-map
1087 "Semantic Breadcrumbs Mode Menu"
1093 (semantic-idle-breadcrumbs--tag-function
1096 :help
"Jump to this tag"))
1097 ;; TODO these entries need minor changes (optional tag argument) in
1098 ;; senator-copy-tag etc
1099 ;; (semantic-menu-item
1102 ;; (semantic-idle-breadcrumbs--tag-function
1103 ;; senator-copy-tag)
1105 ;; :help "Copy this tag"))
1106 ;; (semantic-menu-item
1109 ;; (semantic-idle-breadcrumbs--tag-function
1110 ;; senator-kill-tag)
1112 ;; :help "Kill tag text to the kill ring, and copy the tag to
1114 ;; (semantic-menu-item
1116 ;; "Copy Tag to Register"
1117 ;; (semantic-idle-breadcrumbs--tag-function
1118 ;; senator-copy-tag-to-register)
1120 ;; :help "Copy this tag"))
1121 ;; (semantic-menu-item
1124 ;; (semantic-idle-breadcrumbs--tag-function
1125 ;; senator-narrow-to-defun)
1127 ;; :help "Narrow to the bounds of the current tag"))
1128 ;; (semantic-menu-item
1131 ;; (semantic-idle-breadcrumbs--tag-function
1132 ;; senator-fold-tag-toggle)
1135 ;; :selected '(let ((tag (semantic-current-tag)))
1136 ;; (and tag (semantic-tag-folded-p tag)))
1137 ;; :help "Fold the current tag to one line"))
1141 "About this Header Line"
1144 (describe-function 'semantic-idle-breadcrumbs-mode
))
1146 :help
"Display help about this header line."))
1150 (define-semantic-idle-service semantic-idle-breadcrumbs
1151 "Display breadcrumbs for the tag under point and its parents."
1152 (let* ((scope (semantic-calculate-scope))
1154 ;; If there is a scope, extract the tag and its
1156 (append (oref scope parents
)
1157 (when (oref scope tag
)
1158 (list (oref scope tag
))))
1159 ;; Fall back to tags by overlay
1160 (semantic-find-tag-by-overlay))))
1161 ;; Display the tags.
1162 (funcall semantic-idle-breadcrumbs-display-function tag-list
)))
1164 (defun semantic-idle-breadcrumbs--display-in-header-line (tag-list)
1165 "Display the tags in TAG-LIST in the header line of their buffer."
1166 (let ((width (- (nth 2 (window-edges))
1167 (nth 0 (window-edges)))))
1168 ;; Format TAG-LIST and put the formatted string into the header
1170 (setq header-line-format
1172 semantic-idle-breadcrumbs-header-line-prefix
1174 (semantic-idle-breadcrumbs--format-tag-list
1177 (length semantic-idle-breadcrumbs-header-line-prefix
)))
1181 'font-lock-comment-face
)))))
1183 ;; Update the header line.
1184 (force-mode-line-update))
1186 (defun semantic-idle-breadcrumbs--display-in-mode-line (tag-list)
1187 "Display the tags in TAG-LIST in the mode line of their buffer.
1188 TODO THIS FUNCTION DOES NOT WORK YET."
1190 (error "This function does not work yet")
1192 (let ((width (- (nth 2 (window-edges))
1193 (nth 0 (window-edges)))))
1194 (setq mode-line-format
1195 (semantic-idle-breadcrumbs--format-tag-list tag-list width
)))
1197 (force-mode-line-update))
1199 (defun semantic-idle-breadcrumbs--format-tag-list (tag-list max-length
)
1200 "Format TAG-LIST using configured functions respecting MAX-LENGTH.
1201 If the initial formatting result is longer than MAX-LENGTH, it is
1202 shortened at the beginning."
1203 ;; Format TAG-LIST using the configured formatting function.
1204 (let* ((complete-format (funcall
1205 semantic-idle-breadcrumbs-format-tag-list-function
1206 tag-list max-length
))
1207 ;; Determine length of complete format.
1208 (complete-length (length complete-format
)))
1209 ;; Shorten string if necessary.
1210 (if (<= complete-length max-length
)
1215 (- complete-length
(- max-length
4))))))
1218 (defun semantic-idle-breadcrumbs--format-linear
1219 (tag-list &optional max-length
)
1220 "Format TAG-LIST as a linear list, starting with the outermost tag.
1221 MAX-LENGTH is not used."
1222 (require 'semantic
/analyze
/fcn
)
1223 (let* ((format-pieces (mapcar
1224 #'semantic-idle-breadcrumbs--format-tag
1226 ;; Format tag list, putting configured separators between the
1228 (complete-format (cond
1229 ;; Mode specific separator.
1230 ((eq semantic-idle-breadcrumbs-separator
1232 (semantic-analyze-unsplit-name format-pieces
))
1234 ;; Custom separator.
1235 ((stringp semantic-idle-breadcrumbs-separator
)
1239 semantic-idle-breadcrumbs-separator
)))))
1243 (defun semantic-idle-breadcrumbs--format-innermost-first
1244 (tag-list &optional max-length
)
1245 "Format TAG-LIST placing the innermost tag first, separated from its parents.
1246 If MAX-LENGTH is non-nil, the innermost tag is shortened."
1247 (let* (;; Separate and format remaining tags. Calculate length of
1248 ;; resulting string.
1249 (rest-tags (butlast tag-list
))
1250 (rest-format (if rest-tags
1253 (semantic-idle-breadcrumbs--format-linear
1256 (rest-length (length rest-format
))
1257 ;; Format innermost tag and calculate length of resulting
1259 (inner-format (semantic-idle-breadcrumbs--format-tag
1260 (car (last tag-list
))
1261 #'semantic-format-tag-prototype
))
1262 (inner-length (length inner-format
))
1263 ;; Calculate complete length and shorten string for innermost
1264 ;; tag if MAX-LENGTH is non-nil and the complete string is
1266 (complete-length (+ inner-length rest-length
))
1267 (inner-short (if (and max-length
1268 (<= complete-length max-length
))
1274 (- complete-length max-length
)
1277 ;; Concat both parts.
1278 (concat inner-short rest-format
))
1281 (defun semantic-idle-breadcrumbs--format-tag (tag &optional format-function
)
1282 "Format TAG using the configured function or FORMAT-FUNCTION.
1283 This function also adds text properties for help-echo, mouse
1284 highlighting and a keymap."
1285 (let ((formatted (funcall
1287 semantic-idle-breadcrumbs-format-tag-function
)
1289 (add-text-properties
1290 0 (length formatted
)
1298 mouse-1: jump to tag
1299 mouse-3: popup context menu"
1300 (semantic-tag-name tag
)
1301 (semantic-tag-class tag
))
1305 semantic-idle-breadcrumbs-popup-map
)
1310 (provide 'semantic
/idle
)
1313 ;; generated-autoload-file: "loaddefs.el"
1314 ;; generated-autoload-load-name: "semantic/idle"
1317 ;;; semantic/idle.el ends here