Add 2012 to FSF copyright years for Emacs files (do not merge to trunk)
[bpt/emacs.git] / lisp / cedet / semantic / idle.el
1 ;;; idle.el --- Schedule parsing tasks in idle time
2
3 ;; Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009, 2010, 2011, 2012
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Keywords: syntax
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25 ;;
26 ;; Originally, `semantic-auto-parse-mode' handled refreshing the
27 ;; tags in a buffer in idle time. Other activities can be scheduled
28 ;; in idle time, all of which require up-to-date tag tables.
29 ;; Having a specialized idle time scheduler that first refreshes
30 ;; the tags buffer, and then enables other idle time tasks reduces
31 ;; the amount of work needed. Any specialized idle tasks need not
32 ;; ask for a fresh tags list.
33 ;;
34 ;; NOTE ON SEMANTIC_ANALYZE
35 ;;
36 ;; Some of the idle modes use the semantic analyzer. The analyzer
37 ;; automatically caches the created context, so it is shared amongst
38 ;; all idle modes that will need it.
39
40 (require 'semantic)
41 (require 'semantic/ctxt)
42 (require 'semantic/format)
43 (require 'semantic/tag)
44 (require 'timer)
45
46 ;; For the semantic-find-tags-by-name macro.
47 (eval-when-compile (require 'semantic/find))
48
49 (defvar eldoc-last-message)
50 (declare-function eldoc-message "eldoc")
51 (declare-function semantic-analyze-interesting-tag "semantic/analyze")
52 (declare-function semantic-analyze-unsplit-name "semantic/analyze/fcn")
53 (declare-function semantic-complete-analyze-inline-idle "semantic/complete")
54 (declare-function semanticdb-deep-find-tags-by-name "semantic/db-find")
55 (declare-function semanticdb-save-all-db-idle "semantic/db")
56 (declare-function semanticdb-typecache-refresh-for-buffer "semantic/db-typecache")
57 (declare-function semantic-decorate-flush-pending-decorations
58 "semantic/decorate/mode")
59 (declare-function pulse-momentary-highlight-region "pulse")
60 (declare-function pulse-momentary-highlight-overlay "pulse")
61 (declare-function semantic-symref-hits-in-region "semantic/symref/filter")
62
63 ;;; Code:
64
65 ;;; TIMER RELATED FUNCTIONS
66 ;;
67 (defvar semantic-idle-scheduler-timer nil
68 "Timer used to schedule tasks in idle time.")
69
70 (defvar semantic-idle-scheduler-work-timer nil
71 "Timer used to schedule tasks in idle time that may take a while.")
72
73 (defcustom semantic-idle-scheduler-verbose-flag nil
74 "Non-nil means that the idle scheduler should provide debug messages.
75 Use this setting to debug idle activities."
76 :group 'semantic
77 :type 'boolean)
78
79 (defcustom semantic-idle-scheduler-idle-time 1
80 "Time in seconds of idle before scheduling events.
81 This time should be short enough to ensure that idle-scheduler will be
82 run as soon as Emacs is idle."
83 :group 'semantic
84 :type 'number
85 :set (lambda (sym val)
86 (set-default sym val)
87 (when (timerp semantic-idle-scheduler-timer)
88 (cancel-timer semantic-idle-scheduler-timer)
89 (setq semantic-idle-scheduler-timer nil)
90 (semantic-idle-scheduler-setup-timers))))
91
92 (defcustom semantic-idle-scheduler-work-idle-time 60
93 "Time in seconds of idle before scheduling big work.
94 This time should be long enough that once any big work is started, it is
95 unlikely the user would be ready to type again right away."
96 :group 'semantic
97 :type 'number
98 :set (lambda (sym val)
99 (set-default sym val)
100 (when (timerp semantic-idle-scheduler-timer)
101 (cancel-timer semantic-idle-scheduler-timer)
102 (setq semantic-idle-scheduler-timer nil)
103 (semantic-idle-scheduler-setup-timers))))
104
105 (defun semantic-idle-scheduler-setup-timers ()
106 "Lazy initialization of the auto parse idle timer."
107 ;; REFRESH THIS FUNCTION for XEMACS FOIBLES
108 (or (timerp semantic-idle-scheduler-timer)
109 (setq semantic-idle-scheduler-timer
110 (run-with-idle-timer
111 semantic-idle-scheduler-idle-time t
112 #'semantic-idle-scheduler-function)))
113 (or (timerp semantic-idle-scheduler-work-timer)
114 (setq semantic-idle-scheduler-work-timer
115 (run-with-idle-timer
116 semantic-idle-scheduler-work-idle-time t
117 #'semantic-idle-scheduler-work-function)))
118 )
119
120 (defun semantic-idle-scheduler-kill-timer ()
121 "Kill the auto parse idle timer."
122 (if (timerp semantic-idle-scheduler-timer)
123 (cancel-timer semantic-idle-scheduler-timer))
124 (setq semantic-idle-scheduler-timer nil))
125
126 \f
127 ;;; MINOR MODE
128 ;;
129 ;; The minor mode portion of this code just sets up the minor mode
130 ;; which does the initial scheduling of the idle timers.
131 ;;
132 ;;;###autoload
133 (defcustom global-semantic-idle-scheduler-mode nil
134 "*If non-nil, enable global use of idle-scheduler mode."
135 :group 'semantic
136 :group 'semantic-modes
137 :type 'boolean
138 :require 'semantic/idle
139 :initialize 'custom-initialize-default
140 :set (lambda (sym val)
141 (global-semantic-idle-scheduler-mode (if val 1 -1))))
142
143 (defcustom semantic-idle-scheduler-mode-hook nil
144 "Hook run at the end of the function `semantic-idle-scheduler-mode'."
145 :group 'semantic
146 :type 'hook)
147
148 (defvar semantic-idle-scheduler-mode nil
149 "Non-nil if idle-scheduler minor mode is enabled.
150 Use the command `semantic-idle-scheduler-mode' to change this variable.")
151 (make-variable-buffer-local 'semantic-idle-scheduler-mode)
152
153 (defcustom semantic-idle-scheduler-max-buffer-size 0
154 "*Maximum size in bytes of buffers where idle-scheduler is enabled.
155 If this value is less than or equal to 0, idle-scheduler is enabled in
156 all buffers regardless of their size."
157 :group 'semantic
158 :type 'number)
159
160 (defsubst semantic-idle-scheduler-enabled-p ()
161 "Return non-nil if idle-scheduler is enabled for this buffer.
162 idle-scheduler is disabled when debugging or if the buffer size
163 exceeds the `semantic-idle-scheduler-max-buffer-size' threshold."
164 (and semantic-idle-scheduler-mode
165 (not (and (boundp 'semantic-debug-enabled)
166 semantic-debug-enabled))
167 (not semantic-lex-debug)
168 (or (<= semantic-idle-scheduler-max-buffer-size 0)
169 (< (buffer-size) semantic-idle-scheduler-max-buffer-size))))
170
171 (defun semantic-idle-scheduler-mode-setup ()
172 "Setup option `semantic-idle-scheduler-mode'.
173 The minor mode can be turned on only if semantic feature is available
174 and the current buffer was set up for parsing. When minor mode is
175 enabled parse the current buffer if needed. Return non-nil if the
176 minor mode is enabled."
177 (if semantic-idle-scheduler-mode
178 (if (not (and (featurep 'semantic) (semantic-active-p)))
179 (progn
180 ;; Disable minor mode if semantic stuff not available
181 (setq semantic-idle-scheduler-mode nil)
182 (error "Buffer %s was not set up idle time scheduling"
183 (buffer-name)))
184 (semantic-idle-scheduler-setup-timers)))
185 semantic-idle-scheduler-mode)
186
187 ;;;###autoload
188 (defun semantic-idle-scheduler-mode (&optional arg)
189 "Minor mode to auto parse buffer following a change.
190 When this mode is off, a buffer is only rescanned for tokens when
191 some command requests the list of available tokens. When idle-scheduler
192 is enabled, Emacs periodically checks to see if the buffer is out of
193 date, and reparses while the user is idle (not typing.)
194
195 With prefix argument ARG, turn on if positive, otherwise off. The
196 minor mode can be turned on only if semantic feature is available and
197 the current buffer was set up for parsing. Return non-nil if the
198 minor mode is enabled."
199 (interactive
200 (list (or current-prefix-arg
201 (if semantic-idle-scheduler-mode 0 1))))
202 (setq semantic-idle-scheduler-mode
203 (if arg
204 (>
205 (prefix-numeric-value arg)
206 0)
207 (not semantic-idle-scheduler-mode)))
208 (semantic-idle-scheduler-mode-setup)
209 (run-hooks 'semantic-idle-scheduler-mode-hook)
210 (if (called-interactively-p 'interactive)
211 (message "idle-scheduler minor mode %sabled"
212 (if semantic-idle-scheduler-mode "en" "dis")))
213 (semantic-mode-line-update)
214 semantic-idle-scheduler-mode)
215
216 (semantic-add-minor-mode 'semantic-idle-scheduler-mode
217 "ARP"
218 nil)
219 \f
220 ;;; SERVICES services
221 ;;
222 ;; These are services for managing idle services.
223 ;;
224 (defvar semantic-idle-scheduler-queue nil
225 "List of functions to execute during idle time.
226 These functions will be called in the current buffer after that
227 buffer has had its tags made up to date. These functions
228 will not be called if there are errors parsing the
229 current buffer.")
230
231 (defun semantic-idle-scheduler-add (function)
232 "Schedule FUNCTION to occur during idle time."
233 (add-to-list 'semantic-idle-scheduler-queue function))
234
235 (defun semantic-idle-scheduler-remove (function)
236 "Unschedule FUNCTION to occur during idle time."
237 (setq semantic-idle-scheduler-queue
238 (delete function semantic-idle-scheduler-queue)))
239
240 ;;; IDLE Function
241 ;;
242 (defun semantic-idle-core-handler ()
243 "Core idle function that handles reparsing.
244 And also manages services that depend on tag values."
245 (when semantic-idle-scheduler-verbose-flag
246 (message "IDLE: Core handler..."))
247 (semantic-exit-on-input 'idle-timer
248 (let* ((inhibit-quit nil)
249 (buffers (delq (current-buffer)
250 (delq nil
251 (mapcar #'(lambda (b)
252 (and (buffer-file-name b)
253 b))
254 (buffer-list)))))
255 safe ;; This safe is not used, but could be.
256 others
257 mode)
258 (when (semantic-idle-scheduler-enabled-p)
259 (save-excursion
260 ;; First, reparse the current buffer.
261 (setq mode major-mode
262 safe (semantic-safe "Idle Parse Error: %S"
263 ;(error "Goofy error 1")
264 (semantic-idle-scheduler-refresh-tags)
265 )
266 )
267 ;; Now loop over other buffers with same major mode, trying to
268 ;; update them as well. Stop on keypress.
269 (dolist (b buffers)
270 (semantic-throw-on-input 'parsing-mode-buffers)
271 (with-current-buffer b
272 (if (eq major-mode mode)
273 (and (semantic-idle-scheduler-enabled-p)
274 (semantic-safe "Idle Parse Error: %S"
275 ;(error "Goofy error")
276 (semantic-idle-scheduler-refresh-tags)))
277 (push (current-buffer) others))))
278 (setq buffers others))
279 ;; If re-parse of current buffer completed, evaluate all other
280 ;; services. Stop on keypress.
281
282 ;; NOTE ON COMMENTED SAFE HERE
283 ;; We used to not execute the services if the buffer wsa
284 ;; unparseable. We now assume that they are lexically
285 ;; safe to do, because we have marked the buffer unparseable
286 ;; if there was a problem.
287 ;;(when safe
288 (dolist (service semantic-idle-scheduler-queue)
289 (save-excursion
290 (semantic-throw-on-input 'idle-queue)
291 (when semantic-idle-scheduler-verbose-flag
292 (message "IDLE: execture service %s..." service))
293 (semantic-safe (format "Idle Service Error %s: %%S" service)
294 (funcall service))
295 (when semantic-idle-scheduler-verbose-flag
296 (message "IDLE: execture service %s...done" service))
297 )))
298 ;;)
299 ;; Finally loop over remaining buffers, trying to update them as
300 ;; well. Stop on keypress.
301 (save-excursion
302 (dolist (b buffers)
303 (semantic-throw-on-input 'parsing-other-buffers)
304 (with-current-buffer b
305 (and (semantic-idle-scheduler-enabled-p)
306 (semantic-idle-scheduler-refresh-tags)))))
307 ))
308 (when semantic-idle-scheduler-verbose-flag
309 (message "IDLE: Core handler...done")))
310
311 (defun semantic-debug-idle-function ()
312 "Run the Semantic idle function with debugging turned on."
313 (interactive)
314 (let ((debug-on-error t))
315 (semantic-idle-core-handler)
316 ))
317
318 (defun semantic-idle-scheduler-function ()
319 "Function run when after `semantic-idle-scheduler-idle-time'.
320 This function will reparse the current buffer, and if successful,
321 call additional functions registered with the timer calls."
322 (when (zerop (recursion-depth))
323 (let ((debug-on-error nil))
324 (save-match-data (semantic-idle-core-handler))
325 )))
326
327 \f
328 ;;; WORK FUNCTION
329 ;;
330 ;; Unlike the shorter timer, the WORK timer will kick of tasks that
331 ;; may take a long time to complete.
332 (defcustom semantic-idle-work-parse-neighboring-files-flag nil
333 "*Non-nil means to parse files in the same dir as the current buffer.
334 Disable to prevent lots of excessive parsing in idle time."
335 :group 'semantic
336 :type 'boolean)
337
338 (defcustom semantic-idle-work-update-headers-flag nil
339 "*Non-nil means to parse through header files in idle time.
340 Disable to prevent idle time parsing of many files. If completion
341 is called that work will be done then instead."
342 :group 'semantic
343 :type 'boolean)
344
345 (defun semantic-idle-work-for-one-buffer (buffer)
346 "Do long-processing work for BUFFER.
347 Uses `semantic-safe' and returns the output.
348 Returns t if all processing succeeded."
349 (with-current-buffer buffer
350 (not (and
351 ;; Just in case
352 (semantic-safe "Idle Work Parse Error: %S"
353 (semantic-idle-scheduler-refresh-tags)
354 t)
355
356 ;; Option to disable this work.
357 semantic-idle-work-update-headers-flag
358
359 ;; Force all our include files to get read in so we
360 ;; are ready to provide good smart completion and idle
361 ;; summary information
362 (semantic-safe "Idle Work Including Error: %S"
363 ;; Get the include related path.
364 (when (and (featurep 'semantic/db) (semanticdb-minor-mode-p))
365 (require 'semantic/db-find)
366 (semanticdb-find-translate-path buffer nil)
367 )
368 t)
369
370 ;; Pre-build the typecaches as needed.
371 (semantic-safe "Idle Work Typecaching Error: %S"
372 (when (featurep 'semantic/db-typecache)
373 (semanticdb-typecache-refresh-for-buffer buffer))
374 t)
375 ))
376 ))
377
378 (defun semantic-idle-work-core-handler ()
379 "Core handler for idle work processing of long running tasks.
380 Visits Semantic controlled buffers, and makes sure all needed
381 include files have been parsed, and that the typecache is up to date.
382 Uses `semantic-idle-work-for-on-buffer' to do the work."
383 (let ((errbuf nil)
384 (interrupted
385 (semantic-exit-on-input 'idle-work-timer
386 (let* ((inhibit-quit nil)
387 (cb (current-buffer))
388 (buffers (delq (current-buffer)
389 (delq nil
390 (mapcar #'(lambda (b)
391 (and (buffer-file-name b)
392 b))
393 (buffer-list)))))
394 safe errbuf)
395 ;; First, handle long tasks in the current buffer.
396 (when (semantic-idle-scheduler-enabled-p)
397 (save-excursion
398 (setq safe (semantic-idle-work-for-one-buffer (current-buffer))
399 )))
400 (when (not safe) (push (current-buffer) errbuf))
401
402 ;; Now loop over other buffers with same major mode, trying to
403 ;; update them as well. Stop on keypress.
404 (dolist (b buffers)
405 (semantic-throw-on-input 'parsing-mode-buffers)
406 (with-current-buffer b
407 (when (semantic-idle-scheduler-enabled-p)
408 (and (semantic-idle-scheduler-enabled-p)
409 (unless (semantic-idle-work-for-one-buffer (current-buffer))
410 (push (current-buffer) errbuf)))
411 ))
412 )
413
414 (when (and (featurep 'semantic/db) (semanticdb-minor-mode-p))
415 ;; Save everything.
416 (semanticdb-save-all-db-idle)
417
418 ;; Parse up files near our active buffer
419 (when semantic-idle-work-parse-neighboring-files-flag
420 (semantic-safe "Idle Work Parse Neighboring Files: %S"
421 (set-buffer cb)
422 (semantic-idle-scheduler-work-parse-neighboring-files))
423 t)
424
425 ;; Save everything... again
426 (semanticdb-save-all-db-idle)
427 )
428
429 ;; Done w/ processing
430 nil))))
431
432 ;; Done
433 (if interrupted
434 "Interrupted"
435 (cond ((not errbuf)
436 "done")
437 ((not (cdr errbuf))
438 (format "done with 1 error in %s" (car errbuf)))
439 (t
440 (format "done with errors in %d buffers."
441 (length errbuf)))))))
442
443 (defun semantic-debug-idle-work-function ()
444 "Run the Semantic idle work function with debugging turned on."
445 (interactive)
446 (let ((debug-on-error t))
447 (semantic-idle-work-core-handler)
448 ))
449
450 (defun semantic-idle-scheduler-work-function ()
451 "Function run when after `semantic-idle-scheduler-work-idle-time'.
452 This routine handles difficult tasks that require a lot of parsing, such as
453 parsing all the header files used by our active sources, or building up complex
454 datasets."
455 (when semantic-idle-scheduler-verbose-flag
456 (message "Long Work Idle Timer..."))
457 (let ((exit-type (save-match-data
458 (semantic-idle-work-core-handler))))
459 (when semantic-idle-scheduler-verbose-flag
460 (message "Long Work Idle Timer...%s" exit-type)))
461 )
462
463 (defun semantic-idle-scheduler-work-parse-neighboring-files ()
464 "Parse all the files in similar directories to buffers being edited."
465 ;; Lets check to see if EDE matters.
466 (let ((ede-auto-add-method 'never))
467 (dolist (a auto-mode-alist)
468 (when (eq (cdr a) major-mode)
469 (dolist (file (directory-files default-directory t (car a) t))
470 (semantic-throw-on-input 'parsing-mode-buffers)
471 (save-excursion
472 (semanticdb-file-table-object file)
473 ))))
474 ))
475
476 \f
477 ;;; REPARSING
478 ;;
479 ;; Reparsing is installed as semantic idle service.
480 ;; This part ALWAYS happens, and other services occur
481 ;; afterwards.
482
483 (defvar semantic-before-idle-scheduler-reparse-hook nil
484 "Hook run before option `semantic-idle-scheduler' begins parsing.
485 If any hook function throws an error, this variable is reset to nil.
486 This hook is not protected from lexical errors.")
487
488 (defvar semantic-after-idle-scheduler-reparse-hook nil
489 "Hook run after option `semantic-idle-scheduler' has parsed.
490 If any hook function throws an error, this variable is reset to nil.
491 This hook is not protected from lexical errors.")
492
493 (semantic-varalias-obsolete 'semantic-before-idle-scheduler-reparse-hooks
494 'semantic-before-idle-scheduler-reparse-hook "23.2")
495 (semantic-varalias-obsolete 'semantic-after-idle-scheduler-reparse-hooks
496 'semantic-after-idle-scheduler-reparse-hook "23.2")
497
498 (defun semantic-idle-scheduler-refresh-tags ()
499 "Refreshes the current buffer's tags.
500 This is called by `semantic-idle-scheduler-function' to update the
501 tags in the current buffer.
502
503 Return non-nil if the refresh was successful.
504 Return nil if there is some sort of syntax error preventing a full
505 reparse.
506
507 Does nothing if the current buffer doesn't need reparsing."
508
509 (prog1
510 ;; These checks actually occur in `semantic-fetch-tags', but if we
511 ;; do them here, then all the bovination hooks are not run, and
512 ;; we save lots of time.
513 (cond
514 ;; If the buffer was previously marked unparseable,
515 ;; then don't waste our time.
516 ((semantic-parse-tree-unparseable-p)
517 nil)
518 ;; The parse tree is already ok.
519 ((semantic-parse-tree-up-to-date-p)
520 t)
521 (t
522 ;; If the buffer might need a reparse and it is safe to do so,
523 ;; give it a try.
524 (let* (;(semantic-working-type nil)
525 (inhibit-quit nil)
526 ;; (working-use-echo-area-p
527 ;; (not semantic-idle-scheduler-working-in-modeline-flag))
528 ;; (working-status-dynamic-type
529 ;; (if semantic-idle-scheduler-no-working-message
530 ;; nil
531 ;; working-status-dynamic-type))
532 ;; (working-status-percentage-type
533 ;; (if semantic-idle-scheduler-no-working-message
534 ;; nil
535 ;; working-status-percentage-type))
536 (lexically-safe t)
537 )
538 ;; Let people hook into this, but don't let them hose
539 ;; us over!
540 (condition-case nil
541 (run-hooks 'semantic-before-idle-scheduler-reparse-hook)
542 (error (setq semantic-before-idle-scheduler-reparse-hook nil)))
543
544 (unwind-protect
545 ;; Perform the parsing.
546 (progn
547 (when semantic-idle-scheduler-verbose-flag
548 (message "IDLE: reparse %s..." (buffer-name)))
549 (when (semantic-lex-catch-errors idle-scheduler
550 (save-excursion (semantic-fetch-tags))
551 nil)
552 ;; If we are here, it is because the lexical step failed,
553 ;; proably due to unterminated lists or something like that.
554
555 ;; We do nothing, and just wait for the next idle timer
556 ;; to go off. In the meantime, remember this, and make sure
557 ;; no other idle services can get executed.
558 (setq lexically-safe nil))
559 (when semantic-idle-scheduler-verbose-flag
560 (message "IDLE: reparse %s...done" (buffer-name))))
561 ;; Let people hook into this, but don't let them hose
562 ;; us over!
563 (condition-case nil
564 (run-hooks 'semantic-after-idle-scheduler-reparse-hook)
565 (error (setq semantic-after-idle-scheduler-reparse-hook nil))))
566 ;; Return if we are lexically safe (from prog1)
567 lexically-safe)))
568
569 ;; After updating the tags, handle any pending decorations for this
570 ;; buffer.
571 (require 'semantic/decorate/mode)
572 (semantic-decorate-flush-pending-decorations (current-buffer))
573 ))
574
575 \f
576 ;;; IDLE SERVICES
577 ;;
578 ;; Idle Services are minor modes which enable or disable a services in
579 ;; the idle scheduler. Creating a new services only requires calling
580 ;; `semantic-create-idle-services' which does all the setup
581 ;; needed to create the minor mode that will enable or disable
582 ;; a services. The services must provide a single function.
583
584 ;; FIXME doc is incomplete.
585 (defmacro define-semantic-idle-service (name doc &rest forms)
586 "Create a new idle services with NAME.
587 DOC will be a documentation string describing FORMS.
588 FORMS will be called during idle time after the current buffer's
589 semantic tag information has been updated.
590 This routine creates the following functions and variables:"
591 (let ((global (intern (concat "global-" (symbol-name name) "-mode")))
592 (mode (intern (concat (symbol-name name) "-mode")))
593 (hook (intern (concat (symbol-name name) "-mode-hook")))
594 (map (intern (concat (symbol-name name) "-mode-map")))
595 (setup (intern (concat (symbol-name name) "-mode-setup")))
596 (func (intern (concat (symbol-name name) "-idle-function"))))
597
598 `(eval-and-compile
599 (defun ,global (&optional arg)
600 ,(concat "Toggle " (symbol-name global) ".
601 With ARG, turn the minor mode on if ARG is positive, off otherwise.
602
603 When this minor mode is enabled, `" (symbol-name mode) "' is
604 turned on in every Semantic-supported buffer.")
605 (interactive "P")
606 (setq ,global
607 (semantic-toggle-minor-mode-globally
608 ',mode arg)))
609
610 (defcustom ,global nil
611 ,(concat "Non-nil if `" (symbol-name mode) "' is enabled.")
612 :group 'semantic
613 :group 'semantic-modes
614 :type 'boolean
615 :require 'semantic/idle
616 :initialize 'custom-initialize-default
617 :set (lambda (sym val)
618 (,global (if val 1 -1))))
619
620 (defcustom ,hook nil
621 ,(concat "Hook run at the end of function `" (symbol-name mode) "'.")
622 :group 'semantic
623 :type 'hook)
624
625 (defvar ,map
626 (let ((km (make-sparse-keymap)))
627 km)
628 ,(concat "Keymap for `" (symbol-name mode) "'."))
629
630 (defvar ,mode nil
631 ,(concat "Non-nil if the minor mode `" (symbol-name mode) "' is enabled.
632 Use the command `" (symbol-name mode) "' to change this variable."))
633 (make-variable-buffer-local ',mode)
634
635 (defun ,setup ()
636 ,(concat "Set up `" (symbol-name mode) "'.
637 Return non-nil if the minor mode is enabled.")
638 (if ,mode
639 (if (not (and (featurep 'semantic) (semantic-active-p)))
640 (progn
641 ;; Disable minor mode if semantic stuff not available
642 (setq ,mode nil)
643 (error "Buffer %s was not set up for parsing"
644 (buffer-name)))
645 ;; Enable the mode mode
646 (semantic-idle-scheduler-add #',func)
647 )
648 ;; Disable the mode mode
649 (semantic-idle-scheduler-remove #',func)
650 )
651 ,mode)
652
653 (defun ,mode (&optional arg)
654 ,doc
655 (interactive
656 (list (or current-prefix-arg
657 (if ,mode 0 1))))
658 (setq ,mode
659 (if arg
660 (>
661 (prefix-numeric-value arg)
662 0)
663 (not ,mode)))
664 (,setup)
665 (run-hooks ,hook)
666 (if (called-interactively-p 'interactive)
667 (message "%s %sabled"
668 (symbol-name ',mode)
669 (if ,mode "en" "dis")))
670 (semantic-mode-line-update)
671 ,mode)
672
673 (semantic-add-minor-mode ',mode
674 "" ; idle schedulers are quiet?
675 ,map)
676
677 (defun ,func ()
678 ,(concat "Perform idle activity for the minor mode `"
679 (symbol-name mode) "'.")
680 ,@forms))))
681 (put 'define-semantic-idle-service 'lisp-indent-function 1)
682
683 \f
684 ;;; SUMMARY MODE
685 ;;
686 ;; A mode similar to eldoc using semantic
687 (defcustom semantic-idle-truncate-long-summaries t
688 "Truncate summaries that are too long to fit in the minibuffer.
689 This can prevent minibuffer resizing in idle time."
690 :group 'semantic
691 :type 'boolean)
692
693 (defcustom semantic-idle-summary-function
694 'semantic-format-tag-summarize-with-file
695 "Function to call when displaying tag information during idle time.
696 This function should take a single argument, a Semantic tag, and
697 return a string to display.
698 Some useful functions are found in `semantic-format-tag-functions'."
699 :group 'semantic
700 :type semantic-format-tag-custom-list)
701
702 (defsubst semantic-idle-summary-find-current-symbol-tag (sym)
703 "Search for a semantic tag with name SYM in database tables.
704 Return the tag found or nil if not found.
705 If semanticdb is not in use, use the current buffer only."
706 (car (if (and (featurep 'semantic/db)
707 semanticdb-current-database
708 (require 'semantic/db-find))
709 (cdar (semanticdb-deep-find-tags-by-name sym))
710 (semantic-deep-find-tags-by-name sym (current-buffer)))))
711
712 (defun semantic-idle-summary-current-symbol-info-brutish ()
713 "Return a string message describing the current context.
714 Gets a symbol with `semantic-ctxt-current-thing' and then
715 tries to find it with a deep targeted search."
716 ;; Try the current "thing".
717 (let ((sym (car (semantic-ctxt-current-thing))))
718 (when sym
719 (semantic-idle-summary-find-current-symbol-tag sym))))
720
721 (defun semantic-idle-summary-current-symbol-keyword ()
722 "Return a string message describing the current symbol.
723 Returns a value only if it is a keyword."
724 ;; Try the current "thing".
725 (let ((sym (car (semantic-ctxt-current-thing))))
726 (if (and sym (semantic-lex-keyword-p sym))
727 (semantic-lex-keyword-get sym 'summary))))
728
729 (defun semantic-idle-summary-current-symbol-info-context ()
730 "Return a string message describing the current context.
731 Use the semantic analyzer to find the symbol information."
732 (let ((analysis (condition-case nil
733 (semantic-analyze-current-context (point))
734 (error nil))))
735 (when analysis
736 (require 'semantic/analyze)
737 (semantic-analyze-interesting-tag analysis))))
738
739 (defun semantic-idle-summary-current-symbol-info-default ()
740 "Return a string message describing the current context.
741 This function will disable loading of previously unloaded files
742 by semanticdb as a time-saving measure."
743 (semanticdb-without-unloaded-file-searches
744 (save-excursion
745 ;; use whichever has success first.
746 (or
747 (semantic-idle-summary-current-symbol-keyword)
748
749 (semantic-idle-summary-current-symbol-info-context)
750
751 (semantic-idle-summary-current-symbol-info-brutish)
752 ))))
753
754 (defvar semantic-idle-summary-out-of-context-faces
755 '(
756 font-lock-comment-face
757 font-lock-string-face
758 font-lock-doc-string-face ; XEmacs.
759 font-lock-doc-face ; Emacs 21 and later.
760 )
761 "List of font-lock faces that indicate a useless summary context.
762 Those are generally faces used to highlight comments.
763
764 It might be useful to override this variable to add comment faces
765 specific to a major mode. For example, in jde mode:
766
767 \(defvar-mode-local jde-mode semantic-idle-summary-out-of-context-faces
768 (append (default-value 'semantic-idle-summary-out-of-context-faces)
769 '(jde-java-font-lock-doc-tag-face
770 jde-java-font-lock-link-face
771 jde-java-font-lock-bold-face
772 jde-java-font-lock-underline-face
773 jde-java-font-lock-pre-face
774 jde-java-font-lock-code-face)))")
775
776 (defun semantic-idle-summary-useful-context-p ()
777 "Non-nil if we should show a summary based on context."
778 (if (and (boundp 'font-lock-mode)
779 font-lock-mode
780 (memq (get-text-property (point) 'face)
781 semantic-idle-summary-out-of-context-faces))
782 ;; The best I can think of at the moment is to disable
783 ;; in comments by detecting with font-lock.
784 nil
785 t))
786
787 (define-overloadable-function semantic-idle-summary-current-symbol-info ()
788 "Return a string message describing the current context.")
789
790 (make-obsolete-overload 'semantic-eldoc-current-symbol-info
791 'semantic-idle-summary-current-symbol-info
792 "23.2")
793
794 (defcustom semantic-idle-summary-mode-hook nil
795 "Hook run at the end of `semantic-idle-summary'."
796 :group 'semantic
797 :type 'hook)
798
799 (defun semantic-idle-summary-idle-function ()
800 "Display a tag summary of the lexical token under the cursor.
801 Call `semantic-idle-summary-current-symbol-info' for getting the
802 current tag to display information."
803 (or (eq major-mode 'emacs-lisp-mode)
804 (not (semantic-idle-summary-useful-context-p))
805 (let* ((found (semantic-idle-summary-current-symbol-info))
806 (str (cond ((stringp found) found)
807 ((semantic-tag-p found)
808 (funcall semantic-idle-summary-function
809 found nil t)))))
810 ;; Show the message with eldoc functions
811 (unless (and str (boundp 'eldoc-echo-area-use-multiline-p)
812 eldoc-echo-area-use-multiline-p)
813 (let ((w (1- (window-width (minibuffer-window)))))
814 (if (> (length str) w)
815 (setq str (substring str 0 w)))))
816 ;; I borrowed some bits from eldoc to shorten the
817 ;; message.
818 (when semantic-idle-truncate-long-summaries
819 (let ((ea-width (1- (window-width (minibuffer-window))))
820 (strlen (length str)))
821 (when (> strlen ea-width)
822 (setq str (substring str 0 ea-width)))))
823 ;; Display it
824 (eldoc-message str))))
825
826 (define-minor-mode semantic-idle-summary-mode
827 "Toggle Semantic Idle Summary mode.
828 With ARG, turn Semantic Idle Summary mode on if ARG is positive,
829 off otherwise.
830
831 When this minor mode is enabled, the echo area displays a summary
832 of the lexical token at point whenever Emacs is idle."
833 :group 'semantic
834 :group 'semantic-modes
835 (semantic-idle-summary-mode-setup)
836 (semantic-mode-line-update))
837
838 (defun semantic-idle-summary-refresh-echo-area ()
839 (and semantic-idle-summary-mode
840 eldoc-last-message
841 (if (and (not executing-kbd-macro)
842 (not (and (boundp 'edebug-active) edebug-active))
843 (not cursor-in-echo-area)
844 (not (eq (selected-window) (minibuffer-window))))
845 (eldoc-message eldoc-last-message)
846 (setq eldoc-last-message nil))))
847
848 (defun semantic-idle-summary-mode-setup ()
849 "Set up `semantic-idle-summary-mode'."
850 (if semantic-idle-summary-mode
851 ;; Enable the mode
852 (progn
853 (unless (and (featurep 'semantic) (semantic-active-p))
854 ;; Disable minor mode if semantic stuff not available
855 (setq semantic-idle-summary-mode nil)
856 (error "Buffer %s was not set up for parsing"
857 (buffer-name)))
858 (require 'eldoc)
859 (semantic-idle-scheduler-add 'semantic-idle-summary-idle-function)
860 (add-hook 'pre-command-hook 'semantic-idle-summary-refresh-echo-area t))
861 ;; Disable the mode
862 (semantic-idle-scheduler-remove 'semantic-idle-summary-idle-function)
863 (remove-hook 'pre-command-hook 'semantic-idle-summary-refresh-echo-area t))
864 semantic-idle-summary-mode)
865
866 (semantic-add-minor-mode 'semantic-idle-summary-mode "")
867
868 (define-minor-mode global-semantic-idle-summary-mode
869 "Toggle Global Semantic Idle Summary mode.
870 With ARG, turn Global Semantic Idle Summary mode on if ARG is
871 positive, off otherwise.
872
873 When this minor mode is enabled, `semantic-idle-summary-mode' is
874 turned on in every Semantic-supported buffer."
875 :global t
876 :group 'semantic
877 :group 'semantic-modes
878 (semantic-toggle-minor-mode-globally
879 'semantic-idle-summary-mode
880 (if global-semantic-idle-summary-mode 1 -1)))
881
882 \f
883 ;;; Current symbol highlight
884 ;;
885 ;; This mode will use context analysis to perform highlighting
886 ;; of all uses of the symbol that is under the cursor.
887 ;;
888 ;; This is to mimic the Eclipse tool of a similar nature.
889 (defvar semantic-idle-symbol-highlight-face 'region
890 "Face used for highlighting local symbols.")
891
892 (defun semantic-idle-symbol-maybe-highlight (tag)
893 "Perhaps add highlighting to the symbol represented by TAG.
894 TAG was found as the symbol under point. If it happens to be
895 visible, then highlight it."
896 (require 'pulse)
897 (let* ((region (when (and (semantic-tag-p tag)
898 (semantic-tag-with-position-p tag))
899 (semantic-tag-overlay tag)))
900 (file (when (and (semantic-tag-p tag)
901 (semantic-tag-with-position-p tag))
902 (semantic-tag-file-name tag)))
903 (buffer (when file (get-file-buffer file)))
904 ;; We use pulse, but we don't want the flashy version,
905 ;; just the stable version.
906 (pulse-flag nil)
907 )
908 (cond ((semantic-overlay-p region)
909 (with-current-buffer (semantic-overlay-buffer region)
910 (goto-char (semantic-overlay-start region))
911 (when (pos-visible-in-window-p
912 (point) (get-buffer-window (current-buffer) 'visible))
913 (if (< (semantic-overlay-end region) (point-at-eol))
914 (pulse-momentary-highlight-overlay
915 region semantic-idle-symbol-highlight-face)
916 ;; Not the same
917 (pulse-momentary-highlight-region
918 (semantic-overlay-start region)
919 (point-at-eol)
920 semantic-idle-symbol-highlight-face)))
921 ))
922 ((vectorp region)
923 (let ((start (aref region 0))
924 (end (aref region 1)))
925 (save-excursion
926 (when buffer (set-buffer buffer))
927 ;; As a vector, we have no filename. Perhaps it is a
928 ;; local variable?
929 (when (and (<= end (point-max))
930 (pos-visible-in-window-p
931 start (get-buffer-window (current-buffer) 'visible)))
932 (goto-char start)
933 (when (re-search-forward
934 (regexp-quote (semantic-tag-name tag))
935 end t)
936 ;; This is likely it, give it a try.
937 (pulse-momentary-highlight-region
938 start (if (<= end (point-at-eol)) end
939 (point-at-eol))
940 semantic-idle-symbol-highlight-face)))
941 ))))
942 nil))
943
944 (define-semantic-idle-service semantic-idle-local-symbol-highlight
945 "Highlight the tag and symbol references of the symbol under point.
946 Call `semantic-analyze-current-context' to find the reference tag.
947 Call `semantic-symref-hits-in-region' to identify local references."
948 (require 'pulse)
949 (when (semantic-idle-summary-useful-context-p)
950 (let* ((ctxt
951 (semanticdb-without-unloaded-file-searches
952 (semantic-analyze-current-context)))
953 (Hbounds (when ctxt (oref ctxt bounds)))
954 (target (when ctxt (car (reverse (oref ctxt prefix)))))
955 (tag (semantic-current-tag))
956 ;; We use pulse, but we don't want the flashy version,
957 ;; just the stable version.
958 (pulse-flag nil))
959 (when ctxt
960 ;; Highlight the original tag? Protect against problems.
961 (condition-case nil
962 (semantic-idle-symbol-maybe-highlight target)
963 (error nil))
964 ;; Identify all hits in this current tag.
965 (when (semantic-tag-p target)
966 (require 'semantic/symref/filter)
967 (semantic-symref-hits-in-region
968 target (lambda (start end prefix)
969 (when (/= start (car Hbounds))
970 (pulse-momentary-highlight-region
971 start end semantic-idle-symbol-highlight-face))
972 (semantic-throw-on-input 'symref-highlight)
973 )
974 (semantic-tag-start tag)
975 (semantic-tag-end tag)))
976 ))))
977
978 \f
979 ;;;###autoload
980 (defun global-semantic-idle-scheduler-mode (&optional arg)
981 "Toggle global use of option `semantic-idle-scheduler-mode'.
982 The idle scheduler will automatically reparse buffers in idle time,
983 and then schedule other jobs setup with `semantic-idle-scheduler-add'.
984 If ARG is positive, enable, if it is negative, disable.
985 If ARG is nil, then toggle."
986 (interactive "P")
987 ;; When turning off, disable other idle modes.
988 (when (or (and (numberp arg) (< arg 0))
989 (and (null arg) global-semantic-idle-scheduler-mode))
990 (global-semantic-idle-summary-mode -1)
991 (global-semantic-idle-local-symbol-highlight-mode -1)
992 (global-semantic-idle-completions-mode -1))
993 (setq global-semantic-idle-scheduler-mode
994 (semantic-toggle-minor-mode-globally
995 'semantic-idle-scheduler-mode arg)))
996
997 \f
998 ;;; Completion Popup Mode
999 ;;
1000 ;; This mode uses tooltips to display a (hopefully) short list of possible
1001 ;; completions available for the text under point. It provides
1002 ;; NO provision for actually filling in the values from those completions.
1003 (defun semantic-idle-completions-end-of-symbol-p ()
1004 "Return non-nil if the cursor is at the END of a symbol.
1005 If the cursor is in the middle of a symbol, then we shouldn't be
1006 doing fancy completions."
1007 (not (looking-at "\\w\\|\\s_")))
1008
1009 (defun semantic-idle-completion-list-default ()
1010 "Calculate and display a list of completions."
1011 (when (and (semantic-idle-summary-useful-context-p)
1012 (semantic-idle-completions-end-of-symbol-p))
1013 ;; This mode can be fragile. Ignore problems.
1014 ;; If something doesn't do what you expect, run
1015 ;; the below command by hand instead.
1016 (condition-case nil
1017 (semanticdb-without-unloaded-file-searches
1018 ;; Use idle version.
1019 (semantic-complete-analyze-inline-idle)
1020 )
1021 (error nil))
1022 ))
1023
1024 (define-semantic-idle-service semantic-idle-completions
1025 "Toggle Semantic Idle Completions mode.
1026 With ARG, turn Semantic Idle Completions mode on if ARG is
1027 positive, off otherwise.
1028
1029 This minor mode only takes effect if Semantic is active and
1030 `semantic-idle-scheduler-mode' is enabled.
1031
1032 When enabled, Emacs displays a list of possible completions at
1033 idle time. The method for displaying completions is given by
1034 `semantic-complete-inline-analyzer-idle-displayor-class'; the
1035 default is to show completions inline.
1036
1037 While a completion is displayed, RET accepts the completion; M-n
1038 and M-p cycle through completion alternatives; TAB attempts to
1039 complete as far as possible, and cycles if no additional
1040 completion is possible; and any other command cancels the
1041 completion.
1042
1043 \\{semantic-complete-inline-map}"
1044 ;; Add the ability to override sometime.
1045 (semantic-idle-completion-list-default))
1046
1047 \f
1048 ;;; Breadcrumbs for tag under point
1049 ;;
1050 ;; Service that displays a breadcrumbs indication of the tag under
1051 ;; point and its parents in the header or mode line.
1052 ;;
1053
1054 (defcustom semantic-idle-breadcrumbs-display-function
1055 #'semantic-idle-breadcrumbs--display-in-header-line
1056 "Function to display the tag under point in idle time.
1057 This function should take a list of Semantic tags as its only
1058 argument. The tags are sorted according to their nesting order,
1059 starting with the outermost tag. The function should call
1060 `semantic-idle-breadcrumbs-format-tag-list-function' to convert
1061 the tag list into a string."
1062 :group 'semantic
1063 :type '(choice
1064 (const :tag "Display in header line"
1065 semantic-idle-breadcrumbs--display-in-header-line)
1066 (const :tag "Display in mode line"
1067 semantic-idle-breadcrumbs--display-in-mode-line)
1068 (function :tag "Other function")))
1069
1070 (defcustom semantic-idle-breadcrumbs-format-tag-list-function
1071 #'semantic-idle-breadcrumbs--format-linear
1072 "Function to format the list of tags containing point.
1073 This function should take a list of Semantic tags and an optional
1074 maximum length of the produced string as its arguments. The
1075 maximum length is a hint and can be ignored. When the maximum
1076 length is omitted, an unconstrained string should be
1077 produced. The tags are sorted according to their nesting order,
1078 starting with the outermost tag. Single tags should be formatted
1079 using `semantic-idle-breadcrumbs-format-tag-function' unless
1080 special formatting is required."
1081 :group 'semantic
1082 :type '(choice
1083 (const :tag "Format tags as list, innermost last"
1084 semantic-idle-breadcrumbs--format-linear)
1085 (const :tag "Innermost tag with details, followed by remaining tags"
1086 semantic-idle-breadcrumbs--format-innermost-first)
1087 (function :tag "Other function")))
1088
1089 (defcustom semantic-idle-breadcrumbs-format-tag-function
1090 #'semantic-format-tag-abbreviate
1091 "Function to call to format information about tags.
1092 This function should take a single argument, a Semantic tag, and
1093 return a string to display.
1094 Some useful functions are found in `semantic-format-tag-functions'."
1095 :group 'semantic
1096 :type semantic-format-tag-custom-list)
1097
1098 (defcustom semantic-idle-breadcrumbs-separator 'mode-specific
1099 "Specify how to separate tags in the breadcrumbs string.
1100 An arbitrary string or a mode-specific scope nesting
1101 string (like, for example, \"::\" in C++, or \".\" in Java) can
1102 be used."
1103 :group 'semantic
1104 :type '(choice
1105 (const :tag "Use mode specific separator"
1106 mode-specific)
1107 (string :tag "Specify separator string")))
1108
1109 (defcustom semantic-idle-breadcrumbs-header-line-prefix
1110 semantic-stickyfunc-indent-string ;; TODO not optimal
1111 "String used to indent the breadcrumbs string.
1112 Customize this string to match the space used by scrollbars and
1113 fringe."
1114 :group 'semantic
1115 :type 'string)
1116
1117 (defvar semantic-idle-breadcrumbs-popup-menu nil
1118 "Menu used when a tag displayed by `semantic-idle-breadcrumbs-mode' is clicked.")
1119
1120 (defun semantic-idle-breadcrumbs--popup-menu (event)
1121 "Popup a menu that displays things to do to the clicked tag.
1122 Argument EVENT describes the event that caused this function to
1123 be called."
1124 (interactive "e")
1125 (let ((old-window (selected-window))
1126 (window (semantic-event-window event)))
1127 (select-window window t)
1128 (semantic-popup-menu semantic-idle-breadcrumbs-popup-menu)
1129 (select-window old-window)))
1130
1131 (defmacro semantic-idle-breadcrumbs--tag-function (function)
1132 "Return lambda expression calling FUNCTION when called from a popup."
1133 `(lambda (event)
1134 (interactive "e")
1135 (let* ((old-window (selected-window))
1136 (window (semantic-event-window event))
1137 (column (car (nth 6 (nth 1 event)))) ;; TODO semantic-event-column?
1138 (tag (progn
1139 (select-window window t)
1140 (plist-get
1141 (text-properties-at column header-line-format)
1142 'tag))))
1143 (,function tag)
1144 (select-window old-window)))
1145 )
1146
1147 ;; TODO does this work for mode-line case?
1148 (defvar semantic-idle-breadcrumbs-popup-map
1149 (let ((map (make-sparse-keymap)))
1150 ;; mouse-1 goes to clicked tag
1151 (define-key map
1152 [ header-line mouse-1 ]
1153 (semantic-idle-breadcrumbs--tag-function
1154 semantic-go-to-tag))
1155 ;; mouse-3 pops up a context menu
1156 (define-key map
1157 [ header-line mouse-3 ]
1158 'semantic-idle-breadcrumbs--popup-menu)
1159 map)
1160 "Keymap for semantic idle breadcrumbs minor mode.")
1161
1162 (easy-menu-define
1163 semantic-idle-breadcrumbs-popup-menu
1164 semantic-idle-breadcrumbs-popup-map
1165 "Semantic Breadcrumbs Mode Menu"
1166 (list
1167 "Breadcrumb Tag"
1168 (semantic-menu-item
1169 (vector
1170 "Go to Tag"
1171 (semantic-idle-breadcrumbs--tag-function
1172 semantic-go-to-tag)
1173 :active t
1174 :help "Jump to this tag"))
1175 ;; TODO these entries need minor changes (optional tag argument) in
1176 ;; senator-copy-tag etc
1177 ;; (semantic-menu-item
1178 ;; (vector
1179 ;; "Copy Tag"
1180 ;; (semantic-idle-breadcrumbs--tag-function
1181 ;; senator-copy-tag)
1182 ;; :active t
1183 ;; :help "Copy this tag"))
1184 ;; (semantic-menu-item
1185 ;; (vector
1186 ;; "Kill Tag"
1187 ;; (semantic-idle-breadcrumbs--tag-function
1188 ;; senator-kill-tag)
1189 ;; :active t
1190 ;; :help "Kill tag text to the kill ring, and copy the tag to
1191 ;; the tag ring"))
1192 ;; (semantic-menu-item
1193 ;; (vector
1194 ;; "Copy Tag to Register"
1195 ;; (semantic-idle-breadcrumbs--tag-function
1196 ;; senator-copy-tag-to-register)
1197 ;; :active t
1198 ;; :help "Copy this tag"))
1199 ;; (semantic-menu-item
1200 ;; (vector
1201 ;; "Narrow to Tag"
1202 ;; (semantic-idle-breadcrumbs--tag-function
1203 ;; senator-narrow-to-defun)
1204 ;; :active t
1205 ;; :help "Narrow to the bounds of the current tag"))
1206 ;; (semantic-menu-item
1207 ;; (vector
1208 ;; "Fold Tag"
1209 ;; (semantic-idle-breadcrumbs--tag-function
1210 ;; senator-fold-tag-toggle)
1211 ;; :active t
1212 ;; :style 'toggle
1213 ;; :selected '(let ((tag (semantic-current-tag)))
1214 ;; (and tag (semantic-tag-folded-p tag)))
1215 ;; :help "Fold the current tag to one line"))
1216 "---"
1217 (semantic-menu-item
1218 (vector
1219 "About this Header Line"
1220 (lambda ()
1221 (interactive)
1222 (describe-function 'semantic-idle-breadcrumbs-mode))
1223 :active t
1224 :help "Display help about this header line."))
1225 )
1226 )
1227
1228 (define-semantic-idle-service semantic-idle-breadcrumbs
1229 "Display breadcrumbs for the tag under point and its parents."
1230 (let* ((scope (semantic-calculate-scope))
1231 (tag-list (if scope
1232 ;; If there is a scope, extract the tag and its
1233 ;; parents.
1234 (append (oref scope parents)
1235 (when (oref scope tag)
1236 (list (oref scope tag))))
1237 ;; Fall back to tags by overlay
1238 (semantic-find-tag-by-overlay))))
1239 ;; Display the tags.
1240 (funcall semantic-idle-breadcrumbs-display-function tag-list)))
1241
1242 (defun semantic-idle-breadcrumbs--display-in-header-line (tag-list)
1243 "Display the tags in TAG-LIST in the header line of their buffer."
1244 (let ((width (- (nth 2 (window-edges))
1245 (nth 0 (window-edges)))))
1246 ;; Format TAG-LIST and put the formatted string into the header
1247 ;; line.
1248 (setq header-line-format
1249 (concat
1250 semantic-idle-breadcrumbs-header-line-prefix
1251 (if tag-list
1252 (semantic-idle-breadcrumbs--format-tag-list
1253 tag-list
1254 (- width
1255 (length semantic-idle-breadcrumbs-header-line-prefix)))
1256 (propertize
1257 "<not on tags>"
1258 'face
1259 'font-lock-comment-face)))))
1260
1261 ;; Update the header line.
1262 (force-mode-line-update))
1263
1264 (defun semantic-idle-breadcrumbs--display-in-mode-line (tag-list)
1265 "Display the tags in TAG-LIST in the mode line of their buffer.
1266 TODO THIS FUNCTION DOES NOT WORK YET."
1267
1268 (error "This function does not work yet")
1269
1270 (let ((width (- (nth 2 (window-edges))
1271 (nth 0 (window-edges)))))
1272 (setq mode-line-format
1273 (semantic-idle-breadcrumbs--format-tag-list tag-list width)))
1274
1275 (force-mode-line-update))
1276
1277 (defun semantic-idle-breadcrumbs--format-tag-list (tag-list max-length)
1278 "Format TAG-LIST using configured functions respecting MAX-LENGTH.
1279 If the initial formatting result is longer than MAX-LENGTH, it is
1280 shortened at the beginning."
1281 ;; Format TAG-LIST using the configured formatting function.
1282 (let* ((complete-format (funcall
1283 semantic-idle-breadcrumbs-format-tag-list-function
1284 tag-list max-length))
1285 ;; Determine length of complete format.
1286 (complete-length (length complete-format)))
1287 ;; Shorten string if necessary.
1288 (if (<= complete-length max-length)
1289 complete-format
1290 (concat "... "
1291 (substring
1292 complete-format
1293 (- complete-length (- max-length 4))))))
1294 )
1295
1296 (defun semantic-idle-breadcrumbs--format-linear
1297 (tag-list &optional max-length)
1298 "Format TAG-LIST as a linear list, starting with the outermost tag.
1299 MAX-LENGTH is not used."
1300 (require 'semantic/analyze/fcn)
1301 (let* ((format-pieces (mapcar
1302 #'semantic-idle-breadcrumbs--format-tag
1303 tag-list))
1304 ;; Format tag list, putting configured separators between the
1305 ;; tags.
1306 (complete-format (cond
1307 ;; Mode specific separator.
1308 ((eq semantic-idle-breadcrumbs-separator
1309 'mode-specific)
1310 (semantic-analyze-unsplit-name format-pieces))
1311
1312 ;; Custom separator.
1313 ((stringp semantic-idle-breadcrumbs-separator)
1314 (mapconcat
1315 #'identity
1316 format-pieces
1317 semantic-idle-breadcrumbs-separator)))))
1318 complete-format)
1319 )
1320
1321 (defun semantic-idle-breadcrumbs--format-innermost-first
1322 (tag-list &optional max-length)
1323 "Format TAG-LIST placing the innermost tag first, separated from its parents.
1324 If MAX-LENGTH is non-nil, the innermost tag is shortened."
1325 (let* (;; Separate and format remaining tags. Calculate length of
1326 ;; resulting string.
1327 (rest-tags (butlast tag-list))
1328 (rest-format (if rest-tags
1329 (concat
1330 " | "
1331 (semantic-idle-breadcrumbs--format-linear
1332 rest-tags))
1333 ""))
1334 (rest-length (length rest-format))
1335 ;; Format innermost tag and calculate length of resulting
1336 ;; string.
1337 (inner-format (semantic-idle-breadcrumbs--format-tag
1338 (car (last tag-list))
1339 #'semantic-format-tag-prototype))
1340 (inner-length (length inner-format))
1341 ;; Calculate complete length and shorten string for innermost
1342 ;; tag if MAX-LENGTH is non-nil and the complete string is
1343 ;; too long.
1344 (complete-length (+ inner-length rest-length))
1345 (inner-short (if (and max-length
1346 (<= complete-length max-length))
1347 inner-format
1348 (concat (substring
1349 inner-format
1350 0
1351 (- inner-length
1352 (- complete-length max-length)
1353 4))
1354 " ..."))))
1355 ;; Concat both parts.
1356 (concat inner-short rest-format))
1357 )
1358
1359 (defun semantic-idle-breadcrumbs--format-tag (tag &optional format-function)
1360 "Format TAG using the configured function or FORMAT-FUNCTION.
1361 This function also adds text properties for help-echo, mouse
1362 highlighting and a keymap."
1363 (let ((formatted (funcall
1364 (or format-function
1365 semantic-idle-breadcrumbs-format-tag-function)
1366 tag nil t)))
1367 (add-text-properties
1368 0 (length formatted)
1369 (list
1370 'tag
1371 tag
1372 'help-echo
1373 (format
1374 "Tag %s
1375 Type: %s
1376 mouse-1: jump to tag
1377 mouse-3: popup context menu"
1378 (semantic-tag-name tag)
1379 (semantic-tag-class tag))
1380 'mouse-face
1381 'highlight
1382 'keymap
1383 semantic-idle-breadcrumbs-popup-map)
1384 formatted)
1385 formatted))
1386
1387
1388 (provide 'semantic/idle)
1389
1390 ;; Local variables:
1391 ;; generated-autoload-file: "loaddefs.el"
1392 ;; generated-autoload-load-name: "semantic/idle"
1393 ;; End:
1394
1395 ;; arch-tag: 4bfd54da-5023-4cc1-91ae-e1fefc1a8d1b
1396 ;;; semantic-idle.el ends here