Commit | Line | Data |
---|---|---|
9573e58b CY |
1 | ;;; idle.el --- Schedule parsing tasks in idle time |
2 | ||
9bf6c65c GM |
3 | ;; Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009 |
4 | ;; Free Software Foundation, Inc. | |
9573e58b CY |
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 | ||
3d9d8486 CY |
40 | (require 'semantic) |
41 | (require 'semantic/ctxt) | |
996bc9bf | 42 | (require 'semantic/format) |
3d9d8486 | 43 | (require 'semantic/tag) |
9573e58b CY |
44 | (require 'timer) |
45 | ||
3d9d8486 CY |
46 | ;; For the semantic-find-tags-by-name macro. |
47 | (eval-when-compile (require 'semantic/find)) | |
48 | ||
3d9d8486 | 49 | (declare-function eldoc-message "eldoc") |
55b522b2 CY |
50 | (declare-function semantic-analyze-interesting-tag "semantic/analyze") |
51 | (declare-function semantic-complete-analyze-inline-idle "semantic/complete") | |
52 | (declare-function semanticdb-deep-find-tags-by-name "semantic/db-find") | |
55b522b2 CY |
53 | (declare-function semanticdb-save-all-db-idle "semantic/db") |
54 | (declare-function semanticdb-typecache-refresh-for-buffer "semantic/db-typecache") | |
55 | (declare-function semantic-decorate-flush-pending-decorations | |
56 | "semantic/decorate/mode") | |
57 | (declare-function pulse-momentary-highlight-region "pulse") | |
58 | (declare-function pulse-momentary-highlight-overlay "pulse") | |
ea041226 | 59 | (declare-function semantic-symref-hits-in-region "semantic/symref/filter") |
3d9d8486 | 60 | |
9573e58b CY |
61 | ;;; Code: |
62 | ||
63 | ;;; TIMER RELATED FUNCTIONS | |
64 | ;; | |
65 | (defvar semantic-idle-scheduler-timer nil | |
66 | "Timer used to schedule tasks in idle time.") | |
67 | ||
68 | (defvar semantic-idle-scheduler-work-timer nil | |
69 | "Timer used to schedule tasks in idle time that may take a while.") | |
70 | ||
71 | (defcustom semantic-idle-scheduler-verbose-flag nil | |
8bf997ef | 72 | "Non-nil means that the idle scheduler should provide debug messages. |
9573e58b CY |
73 | Use this setting to debug idle activities." |
74 | :group 'semantic | |
75 | :type 'boolean) | |
76 | ||
8bf997ef CY |
77 | (defcustom semantic-idle-scheduler-idle-time 1 |
78 | "Time in seconds of idle before scheduling events. | |
9573e58b CY |
79 | This time should be short enough to ensure that idle-scheduler will be |
80 | run as soon as Emacs is idle." | |
81 | :group 'semantic | |
82 | :type 'number | |
83 | :set (lambda (sym val) | |
84 | (set-default sym val) | |
85 | (when (timerp semantic-idle-scheduler-timer) | |
86 | (cancel-timer semantic-idle-scheduler-timer) | |
87 | (setq semantic-idle-scheduler-timer nil) | |
88 | (semantic-idle-scheduler-setup-timers)))) | |
89 | ||
90 | (defcustom semantic-idle-scheduler-work-idle-time 60 | |
8bf997ef | 91 | "Time in seconds of idle before scheduling big work. |
9573e58b CY |
92 | This time should be long enough that once any big work is started, it is |
93 | unlikely the user would be ready to type again right away." | |
94 | :group 'semantic | |
95 | :type 'number | |
96 | :set (lambda (sym val) | |
97 | (set-default sym val) | |
98 | (when (timerp semantic-idle-scheduler-timer) | |
99 | (cancel-timer semantic-idle-scheduler-timer) | |
100 | (setq semantic-idle-scheduler-timer nil) | |
101 | (semantic-idle-scheduler-setup-timers)))) | |
102 | ||
103 | (defun semantic-idle-scheduler-setup-timers () | |
104 | "Lazy initialization of the auto parse idle timer." | |
105 | ;; REFRESH THIS FUNCTION for XEMACS FOIBLES | |
106 | (or (timerp semantic-idle-scheduler-timer) | |
107 | (setq semantic-idle-scheduler-timer | |
108 | (run-with-idle-timer | |
109 | semantic-idle-scheduler-idle-time t | |
110 | #'semantic-idle-scheduler-function))) | |
111 | (or (timerp semantic-idle-scheduler-work-timer) | |
112 | (setq semantic-idle-scheduler-work-timer | |
113 | (run-with-idle-timer | |
114 | semantic-idle-scheduler-work-idle-time t | |
115 | #'semantic-idle-scheduler-work-function))) | |
116 | ) | |
117 | ||
118 | (defun semantic-idle-scheduler-kill-timer () | |
119 | "Kill the auto parse idle timer." | |
120 | (if (timerp semantic-idle-scheduler-timer) | |
121 | (cancel-timer semantic-idle-scheduler-timer)) | |
122 | (setq semantic-idle-scheduler-timer nil)) | |
123 | ||
124 | \f | |
125 | ;;; MINOR MODE | |
126 | ;; | |
127 | ;; The minor mode portion of this code just sets up the minor mode | |
128 | ;; which does the initial scheduling of the idle timers. | |
129 | ;; | |
cd23a4d3 | 130 | ;;;###autoload |
9573e58b CY |
131 | (defcustom global-semantic-idle-scheduler-mode nil |
132 | "*If non-nil, enable global use of idle-scheduler mode." | |
133 | :group 'semantic | |
134 | :group 'semantic-modes | |
135 | :type 'boolean | |
136 | :require 'semantic/idle | |
137 | :initialize 'custom-initialize-default | |
138 | :set (lambda (sym val) | |
139 | (global-semantic-idle-scheduler-mode (if val 1 -1)))) | |
140 | ||
cd23a4d3 | 141 | ;;;###autoload |
9573e58b CY |
142 | (defun global-semantic-idle-scheduler-mode (&optional arg) |
143 | "Toggle global use of option `semantic-idle-scheduler-mode'. | |
144 | The idle scheduler with automatically reparse buffers in idle time, | |
145 | and then schedule other jobs setup with `semantic-idle-scheduler-add'. | |
146 | If ARG is positive, enable, if it is negative, disable. | |
147 | If ARG is nil, then toggle." | |
148 | (interactive "P") | |
149 | (setq global-semantic-idle-scheduler-mode | |
150 | (semantic-toggle-minor-mode-globally | |
151 | 'semantic-idle-scheduler-mode arg))) | |
152 | ||
153 | (defcustom semantic-idle-scheduler-mode-hook nil | |
154 | "*Hook run at the end of function `semantic-idle-scheduler-mode'." | |
155 | :group 'semantic | |
156 | :type 'hook) | |
157 | ||
9573e58b CY |
158 | (defvar semantic-idle-scheduler-mode nil |
159 | "Non-nil if idle-scheduler minor mode is enabled. | |
160 | Use the command `semantic-idle-scheduler-mode' to change this variable.") | |
161 | (make-variable-buffer-local 'semantic-idle-scheduler-mode) | |
162 | ||
163 | (defcustom semantic-idle-scheduler-max-buffer-size 0 | |
164 | "*Maximum size in bytes of buffers where idle-scheduler is enabled. | |
165 | If this value is less than or equal to 0, idle-scheduler is enabled in | |
166 | all buffers regardless of their size." | |
167 | :group 'semantic | |
168 | :type 'number) | |
169 | ||
170 | (defsubst semantic-idle-scheduler-enabled-p () | |
171 | "Return non-nil if idle-scheduler is enabled for this buffer. | |
172 | idle-scheduler is disabled when debugging or if the buffer size | |
173 | exceeds the `semantic-idle-scheduler-max-buffer-size' threshold." | |
174 | (and semantic-idle-scheduler-mode | |
3d9d8486 CY |
175 | (not (and (boundp 'semantic-debug-enabled) |
176 | semantic-debug-enabled)) | |
9573e58b CY |
177 | (not semantic-lex-debug) |
178 | (or (<= semantic-idle-scheduler-max-buffer-size 0) | |
179 | (< (buffer-size) semantic-idle-scheduler-max-buffer-size)))) | |
180 | ||
181 | (defun semantic-idle-scheduler-mode-setup () | |
182 | "Setup option `semantic-idle-scheduler-mode'. | |
183 | The minor mode can be turned on only if semantic feature is available | |
184 | and the current buffer was set up for parsing. When minor mode is | |
185 | enabled parse the current buffer if needed. Return non-nil if the | |
186 | minor mode is enabled." | |
187 | (if semantic-idle-scheduler-mode | |
188 | (if (not (and (featurep 'semantic) (semantic-active-p))) | |
189 | (progn | |
190 | ;; Disable minor mode if semantic stuff not available | |
191 | (setq semantic-idle-scheduler-mode nil) | |
192 | (error "Buffer %s was not set up idle time scheduling" | |
193 | (buffer-name))) | |
194 | (semantic-idle-scheduler-setup-timers))) | |
195 | semantic-idle-scheduler-mode) | |
196 | ||
cd23a4d3 | 197 | ;;;###autoload |
9573e58b CY |
198 | (defun semantic-idle-scheduler-mode (&optional arg) |
199 | "Minor mode to auto parse buffer following a change. | |
200 | When this mode is off, a buffer is only rescanned for tokens when | |
201 | some command requests the list of available tokens. When idle-scheduler | |
202 | is enabled, Emacs periodically checks to see if the buffer is out of | |
203 | date, and reparses while the user is idle (not typing.) | |
204 | ||
205 | With prefix argument ARG, turn on if positive, otherwise off. The | |
206 | minor mode can be turned on only if semantic feature is available and | |
207 | the current buffer was set up for parsing. Return non-nil if the | |
208 | minor mode is enabled." | |
209 | (interactive | |
210 | (list (or current-prefix-arg | |
211 | (if semantic-idle-scheduler-mode 0 1)))) | |
212 | (setq semantic-idle-scheduler-mode | |
213 | (if arg | |
214 | (> | |
215 | (prefix-numeric-value arg) | |
216 | 0) | |
217 | (not semantic-idle-scheduler-mode))) | |
218 | (semantic-idle-scheduler-mode-setup) | |
219 | (run-hooks 'semantic-idle-scheduler-mode-hook) | |
220 | (if (interactive-p) | |
221 | (message "idle-scheduler minor mode %sabled" | |
222 | (if semantic-idle-scheduler-mode "en" "dis"))) | |
223 | (semantic-mode-line-update) | |
224 | semantic-idle-scheduler-mode) | |
225 | ||
226 | (semantic-add-minor-mode 'semantic-idle-scheduler-mode | |
227 | "ARP" | |
228 | nil) | |
9573e58b CY |
229 | \f |
230 | ;;; SERVICES services | |
231 | ;; | |
232 | ;; These are services for managing idle services. | |
233 | ;; | |
234 | (defvar semantic-idle-scheduler-queue nil | |
235 | "List of functions to execute during idle time. | |
236 | These functions will be called in the current buffer after that | |
237 | buffer has had its tags made up to date. These functions | |
238 | will not be called if there are errors parsing the | |
239 | current buffer.") | |
240 | ||
9573e58b CY |
241 | (defun semantic-idle-scheduler-add (function) |
242 | "Schedule FUNCTION to occur during idle time." | |
243 | (add-to-list 'semantic-idle-scheduler-queue function)) | |
244 | ||
9573e58b CY |
245 | (defun semantic-idle-scheduler-remove (function) |
246 | "Unschedule FUNCTION to occur during idle time." | |
247 | (setq semantic-idle-scheduler-queue | |
248 | (delete function semantic-idle-scheduler-queue))) | |
249 | ||
250 | ;;; IDLE Function | |
251 | ;; | |
252 | (defun semantic-idle-core-handler () | |
253 | "Core idle function that handles reparsing. | |
254 | And also manages services that depend on tag values." | |
255 | (when semantic-idle-scheduler-verbose-flag | |
256 | (message "IDLE: Core handler...")) | |
257 | (semantic-exit-on-input 'idle-timer | |
258 | (let* ((inhibit-quit nil) | |
259 | (buffers (delq (current-buffer) | |
260 | (delq nil | |
261 | (mapcar #'(lambda (b) | |
262 | (and (buffer-file-name b) | |
263 | b)) | |
264 | (buffer-list))))) | |
265 | safe ;; This safe is not used, but could be. | |
266 | others | |
267 | mode) | |
268 | (when (semantic-idle-scheduler-enabled-p) | |
269 | (save-excursion | |
270 | ;; First, reparse the current buffer. | |
271 | (setq mode major-mode | |
272 | safe (semantic-safe "Idle Parse Error: %S" | |
273 | ;(error "Goofy error 1") | |
274 | (semantic-idle-scheduler-refresh-tags) | |
275 | ) | |
276 | ) | |
277 | ;; Now loop over other buffers with same major mode, trying to | |
278 | ;; update them as well. Stop on keypress. | |
279 | (dolist (b buffers) | |
280 | (semantic-throw-on-input 'parsing-mode-buffers) | |
281 | (with-current-buffer b | |
282 | (if (eq major-mode mode) | |
283 | (and (semantic-idle-scheduler-enabled-p) | |
284 | (semantic-safe "Idle Parse Error: %S" | |
285 | ;(error "Goofy error") | |
286 | (semantic-idle-scheduler-refresh-tags))) | |
287 | (push (current-buffer) others)))) | |
288 | (setq buffers others)) | |
289 | ;; If re-parse of current buffer completed, evaluate all other | |
290 | ;; services. Stop on keypress. | |
291 | ||
292 | ;; NOTE ON COMMENTED SAFE HERE | |
293 | ;; We used to not execute the services if the buffer wsa | |
294 | ;; unparseable. We now assume that they are lexically | |
295 | ;; safe to do, because we have marked the buffer unparseable | |
296 | ;; if there was a problem. | |
297 | ;;(when safe | |
298 | (dolist (service semantic-idle-scheduler-queue) | |
299 | (save-excursion | |
300 | (semantic-throw-on-input 'idle-queue) | |
301 | (when semantic-idle-scheduler-verbose-flag | |
302 | (message "IDLE: execture service %s..." service)) | |
303 | (semantic-safe (format "Idle Service Error %s: %%S" service) | |
304 | (funcall service)) | |
305 | (when semantic-idle-scheduler-verbose-flag | |
306 | (message "IDLE: execture service %s...done" service)) | |
307 | ))) | |
308 | ;;) | |
309 | ;; Finally loop over remaining buffers, trying to update them as | |
310 | ;; well. Stop on keypress. | |
311 | (save-excursion | |
312 | (dolist (b buffers) | |
313 | (semantic-throw-on-input 'parsing-other-buffers) | |
314 | (with-current-buffer b | |
315 | (and (semantic-idle-scheduler-enabled-p) | |
316 | (semantic-idle-scheduler-refresh-tags))))) | |
317 | )) | |
318 | (when semantic-idle-scheduler-verbose-flag | |
319 | (message "IDLE: Core handler...done"))) | |
320 | ||
321 | (defun semantic-debug-idle-function () | |
322 | "Run the Semantic idle function with debugging turned on." | |
323 | (interactive) | |
324 | (let ((debug-on-error t)) | |
325 | (semantic-idle-core-handler) | |
326 | )) | |
327 | ||
328 | (defun semantic-idle-scheduler-function () | |
329 | "Function run when after `semantic-idle-scheduler-idle-time'. | |
330 | This function will reparse the current buffer, and if successful, | |
331 | call additional functions registered with the timer calls." | |
332 | (when (zerop (recursion-depth)) | |
333 | (let ((debug-on-error nil)) | |
334 | (save-match-data (semantic-idle-core-handler)) | |
335 | ))) | |
336 | ||
337 | \f | |
338 | ;;; WORK FUNCTION | |
339 | ;; | |
340 | ;; Unlike the shorter timer, the WORK timer will kick of tasks that | |
341 | ;; may take a long time to complete. | |
342 | (defcustom semantic-idle-work-parse-neighboring-files-flag t | |
343 | "*Non-nil means to parse files in the same dir as the current buffer. | |
344 | Disable to prevent lots of excessive parsing in idle time." | |
345 | :group 'semantic | |
346 | :type 'boolean) | |
347 | ||
348 | ||
349 | (defun semantic-idle-work-for-one-buffer (buffer) | |
350 | "Do long-processing work for for BUFFER. | |
351 | Uses `semantic-safe' and returns the output. | |
352 | Returns t of all processing succeeded." | |
0816d744 | 353 | (with-current-buffer buffer |
9573e58b CY |
354 | (not (and |
355 | ;; Just in case | |
356 | (semantic-safe "Idle Work Parse Error: %S" | |
357 | (semantic-idle-scheduler-refresh-tags) | |
358 | t) | |
359 | ||
360 | ;; Force all our include files to get read in so we | |
361 | ;; are ready to provide good smart completion and idle | |
362 | ;; summary information | |
363 | (semantic-safe "Idle Work Including Error: %S" | |
364 | ;; Get the include related path. | |
996bc9bf | 365 | (when (and (featurep 'semantic/db) (semanticdb-minor-mode-p)) |
9573e58b CY |
366 | (require 'semantic/db-find) |
367 | (semanticdb-find-translate-path buffer nil) | |
368 | ) | |
369 | t) | |
370 | ||
371 | ;; Pre-build the typecaches as needed. | |
372 | (semantic-safe "Idle Work Typecaching Error: %S" | |
373 | (when (featurep 'semantic/db-typecache) | |
374 | (semanticdb-typecache-refresh-for-buffer buffer)) | |
375 | t) | |
376 | )) | |
377 | )) | |
378 | ||
379 | (defun semantic-idle-work-core-handler () | |
380 | "Core handler for idle work processing of long running tasks. | |
381 | Visits semantic controlled buffers, and makes sure all needed | |
382 | include files have been parsed, and that the typecache is up to date. | |
383 | Uses `semantic-idle-work-for-on-buffer' to do the work." | |
384 | (let ((errbuf nil) | |
385 | (interrupted | |
386 | (semantic-exit-on-input 'idle-work-timer | |
387 | (let* ((inhibit-quit nil) | |
388 | (cb (current-buffer)) | |
389 | (buffers (delq (current-buffer) | |
390 | (delq nil | |
391 | (mapcar #'(lambda (b) | |
392 | (and (buffer-file-name b) | |
393 | b)) | |
394 | (buffer-list))))) | |
395 | safe errbuf) | |
396 | ;; First, handle long tasks in the current buffer. | |
397 | (when (semantic-idle-scheduler-enabled-p) | |
398 | (save-excursion | |
399 | (setq safe (semantic-idle-work-for-one-buffer (current-buffer)) | |
400 | ))) | |
401 | (when (not safe) (push (current-buffer) errbuf)) | |
402 | ||
403 | ;; Now loop over other buffers with same major mode, trying to | |
404 | ;; update them as well. Stop on keypress. | |
405 | (dolist (b buffers) | |
406 | (semantic-throw-on-input 'parsing-mode-buffers) | |
407 | (with-current-buffer b | |
408 | (when (semantic-idle-scheduler-enabled-p) | |
409 | (and (semantic-idle-scheduler-enabled-p) | |
410 | (unless (semantic-idle-work-for-one-buffer (current-buffer)) | |
411 | (push (current-buffer) errbuf))) | |
412 | )) | |
413 | ) | |
414 | ||
996bc9bf | 415 | (when (and (featurep 'semantic/db) (semanticdb-minor-mode-p)) |
55b522b2 CY |
416 | ;; Save everything. |
417 | (semanticdb-save-all-db-idle) | |
9573e58b | 418 | |
55b522b2 CY |
419 | ;; Parse up files near our active buffer |
420 | (when semantic-idle-work-parse-neighboring-files-flag | |
421 | (semantic-safe "Idle Work Parse Neighboring Files: %S" | |
9573e58b CY |
422 | (set-buffer cb) |
423 | (semantic-idle-scheduler-work-parse-neighboring-files)) | |
424 | t) | |
9573e58b | 425 | |
55b522b2 CY |
426 | ;; Save everything... again |
427 | (semanticdb-save-all-db-idle) | |
428 | ) | |
9573e58b CY |
429 | |
430 | ;; Done w/ processing | |
431 | nil)))) | |
432 | ||
433 | ;; Done | |
434 | (if interrupted | |
435 | "Interrupted" | |
436 | (cond ((not errbuf) | |
437 | "done") | |
438 | ((not (cdr errbuf)) | |
439 | (format "done with 1 error in %s" (car errbuf))) | |
440 | (t | |
441 | (format "done with errors in %d buffers." | |
442 | (length errbuf))))))) | |
443 | ||
444 | (defun semantic-debug-idle-work-function () | |
445 | "Run the Semantic idle work function with debugging turned on." | |
446 | (interactive) | |
447 | (let ((debug-on-error t)) | |
448 | (semantic-idle-work-core-handler) | |
449 | )) | |
450 | ||
451 | (defun semantic-idle-scheduler-work-function () | |
452 | "Function run when after `semantic-idle-scheduler-work-idle-time'. | |
453 | This routine handles difficult tasks that require a lot of parsing, such as | |
454 | parsing all the header files used by our active sources, or building up complex | |
455 | datasets." | |
456 | (when semantic-idle-scheduler-verbose-flag | |
457 | (message "Long Work Idle Timer...")) | |
458 | (let ((exit-type (save-match-data | |
459 | (semantic-idle-work-core-handler)))) | |
460 | (when semantic-idle-scheduler-verbose-flag | |
461 | (message "Long Work Idle Timer...%s" exit-type))) | |
462 | ) | |
463 | ||
464 | (defun semantic-idle-scheduler-work-parse-neighboring-files () | |
465 | "Parse all the files in similar directories to buffers being edited." | |
466 | ;; Lets check to see if EDE matters. | |
467 | (let ((ede-auto-add-method 'never)) | |
468 | (dolist (a auto-mode-alist) | |
469 | (when (eq (cdr a) major-mode) | |
470 | (dolist (file (directory-files default-directory t (car a) t)) | |
471 | (semantic-throw-on-input 'parsing-mode-buffers) | |
472 | (save-excursion | |
473 | (semanticdb-file-table-object file) | |
474 | )))) | |
475 | )) | |
476 | ||
9573e58b CY |
477 | \f |
478 | ;;; REPARSING | |
479 | ;; | |
480 | ;; Reparsing is installed as semantic idle service. | |
481 | ;; This part ALWAYS happens, and other services occur | |
482 | ;; afterwards. | |
483 | ||
29e1a603 | 484 | (defvar semantic-before-idle-scheduler-reparse-hook nil |
1ac9ebc8 | 485 | "Hook run before option `semantic-idle-scheduler' begins parsing. |
29e1a603 | 486 | If any hook function throws an error, this variable is reset to nil. |
9573e58b CY |
487 | This hook is not protected from lexical errors.") |
488 | ||
29e1a603 | 489 | (defvar semantic-after-idle-scheduler-reparse-hook nil |
1ac9ebc8 | 490 | "Hook run after option `semantic-idle-scheduler' has parsed. |
29e1a603 | 491 | If any hook function throws an error, this variable is reset to nil. |
9573e58b CY |
492 | This hook is not protected from lexical errors.") |
493 | ||
1ac9ebc8 | 494 | (semantic-varalias-obsolete 'semantic-before-idle-scheduler-reparse-hooks |
eefa91db | 495 | 'semantic-before-idle-scheduler-reparse-hook "23.2") |
1ac9ebc8 | 496 | (semantic-varalias-obsolete 'semantic-after-idle-scheduler-reparse-hooks |
eefa91db | 497 | 'semantic-after-idle-scheduler-reparse-hook "23.2") |
29e1a603 | 498 | |
9573e58b CY |
499 | (defun semantic-idle-scheduler-refresh-tags () |
500 | "Refreshes the current buffer's tags. | |
501 | This is called by `semantic-idle-scheduler-function' to update the | |
502 | tags in the current buffer. | |
503 | ||
504 | Return non-nil if the refresh was successful. | |
505 | Return nil if there is some sort of syntax error preventing a full | |
506 | reparse. | |
507 | ||
508 | Does nothing if the current buffer doesn't need reparsing." | |
509 | ||
510 | (prog1 | |
511 | ;; These checks actually occur in `semantic-fetch-tags', but if we | |
512 | ;; do them here, then all the bovination hooks are not run, and | |
513 | ;; we save lots of time. | |
514 | (cond | |
515 | ;; If the buffer was previously marked unparseable, | |
516 | ;; then don't waste our time. | |
517 | ((semantic-parse-tree-unparseable-p) | |
518 | nil) | |
519 | ;; The parse tree is already ok. | |
520 | ((semantic-parse-tree-up-to-date-p) | |
521 | t) | |
522 | (t | |
523 | ;; If the buffer might need a reparse and it is safe to do so, | |
524 | ;; give it a try. | |
525 | (let* (;(semantic-working-type nil) | |
526 | (inhibit-quit nil) | |
527 | ;; (working-use-echo-area-p | |
528 | ;; (not semantic-idle-scheduler-working-in-modeline-flag)) | |
529 | ;; (working-status-dynamic-type | |
530 | ;; (if semantic-idle-scheduler-no-working-message | |
531 | ;; nil | |
532 | ;; working-status-dynamic-type)) | |
533 | ;; (working-status-percentage-type | |
534 | ;; (if semantic-idle-scheduler-no-working-message | |
535 | ;; nil | |
536 | ;; working-status-percentage-type)) | |
537 | (lexically-safe t) | |
538 | ) | |
539 | ;; Let people hook into this, but don't let them hose | |
540 | ;; us over! | |
541 | (condition-case nil | |
29e1a603 CY |
542 | (run-hooks 'semantic-before-idle-scheduler-reparse-hook) |
543 | (error (setq semantic-before-idle-scheduler-reparse-hook nil))) | |
9573e58b CY |
544 | |
545 | (unwind-protect | |
546 | ;; Perform the parsing. | |
547 | (progn | |
548 | (when semantic-idle-scheduler-verbose-flag | |
549 | (message "IDLE: reparse %s..." (buffer-name))) | |
550 | (when (semantic-lex-catch-errors idle-scheduler | |
551 | (save-excursion (semantic-fetch-tags)) | |
552 | nil) | |
553 | ;; If we are here, it is because the lexical step failed, | |
554 | ;; proably due to unterminated lists or something like that. | |
555 | ||
556 | ;; We do nothing, and just wait for the next idle timer | |
557 | ;; to go off. In the meantime, remember this, and make sure | |
558 | ;; no other idle services can get executed. | |
559 | (setq lexically-safe nil)) | |
560 | (when semantic-idle-scheduler-verbose-flag | |
561 | (message "IDLE: reparse %s...done" (buffer-name)))) | |
562 | ;; Let people hook into this, but don't let them hose | |
563 | ;; us over! | |
564 | (condition-case nil | |
29e1a603 CY |
565 | (run-hooks 'semantic-after-idle-scheduler-reparse-hook) |
566 | (error (setq semantic-after-idle-scheduler-reparse-hook nil)))) | |
9573e58b CY |
567 | ;; Return if we are lexically safe (from prog1) |
568 | lexically-safe))) | |
569 | ||
570 | ;; After updating the tags, handle any pending decorations for this | |
571 | ;; buffer. | |
55b522b2 | 572 | (require 'semantic/decorate/mode) |
9573e58b CY |
573 | (semantic-decorate-flush-pending-decorations (current-buffer)) |
574 | )) | |
575 | ||
576 | \f | |
577 | ;;; IDLE SERVICES | |
578 | ;; | |
579 | ;; Idle Services are minor modes which enable or disable a services in | |
580 | ;; the idle scheduler. Creating a new services only requires calling | |
581 | ;; `semantic-create-idle-services' which does all the setup | |
582 | ;; needed to create the minor mode that will enable or disable | |
583 | ;; a services. The services must provide a single function. | |
584 | ||
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 routines 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 | ||
599 | `(eval-and-compile | |
600 | (defun ,global (&optional arg) | |
af7b5a91 | 601 | ,(concat "Toggle global use of `" (symbol-name mode) "'. |
9573e58b CY |
602 | If ARG is positive, enable, if it is negative, disable. |
603 | If ARG is nil, then toggle.") | |
604 | (interactive "P") | |
605 | (setq ,global | |
606 | (semantic-toggle-minor-mode-globally | |
607 | ',mode arg))) | |
608 | ||
609 | (defcustom ,global nil | |
610 | (concat "*If non-nil, enable global use of `" (symbol-name ',mode) "'. | |
611 | " ,doc) | |
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 summary minor 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 "Setup option `" (symbol-name mode) "'. | |
637 | The minor mode can be turned on only if semantic feature is available | |
638 | and the idle scheduler is active. | |
639 | Return non-nil if the minor mode is enabled.") | |
640 | (if ,mode | |
641 | (if (not (and (featurep 'semantic) (semantic-active-p))) | |
642 | (progn | |
643 | ;; Disable minor mode if semantic stuff not available | |
644 | (setq ,mode nil) | |
645 | (error "Buffer %s was not set up for parsing" | |
646 | (buffer-name))) | |
647 | ;; Enable the mode mode | |
648 | (semantic-idle-scheduler-add #',func) | |
649 | ) | |
650 | ;; Disable the mode mode | |
651 | (semantic-idle-scheduler-remove #',func) | |
652 | ) | |
653 | ,mode) | |
654 | ||
9573e58b CY |
655 | (defun ,mode (&optional arg) |
656 | ,(concat doc " | |
657 | This is a minor mode which performs actions during idle time. | |
658 | With prefix argument ARG, turn on if positive, otherwise off. The | |
659 | minor mode can be turned on only if semantic feature is available and | |
660 | the current buffer was set up for parsing. Return non-nil if the | |
661 | minor mode is enabled.") | |
662 | (interactive | |
663 | (list (or current-prefix-arg | |
664 | (if ,mode 0 1)))) | |
665 | (setq ,mode | |
666 | (if arg | |
667 | (> | |
668 | (prefix-numeric-value arg) | |
669 | 0) | |
670 | (not ,mode))) | |
671 | (,setup) | |
672 | (run-hooks ,hook) | |
673 | (if (interactive-p) | |
674 | (message "%s %sabled" | |
675 | (symbol-name ',mode) | |
676 | (if ,mode "en" "dis"))) | |
677 | (semantic-mode-line-update) | |
678 | ,mode) | |
679 | ||
680 | (semantic-add-minor-mode ',mode | |
681 | "" ; idle schedulers are quiet? | |
682 | ,map) | |
683 | ||
684 | (defun ,func () | |
685 | ,doc | |
686 | ,@forms) | |
687 | ||
688 | ))) | |
689 | (put 'define-semantic-idle-service 'lisp-indent-function 1) | |
690 | ||
691 | \f | |
692 | ;;; SUMMARY MODE | |
693 | ;; | |
694 | ;; A mode similar to eldoc using semantic | |
9573e58b CY |
695 | |
696 | (defcustom semantic-idle-summary-function | |
697 | 'semantic-format-tag-summarize-with-file | |
698 | "*Function to use when displaying tag information during idle time. | |
699 | Some useful functions are found in `semantic-format-tag-functions'." | |
700 | :group 'semantic | |
701 | :type semantic-format-tag-custom-list) | |
702 | ||
703 | (defsubst semantic-idle-summary-find-current-symbol-tag (sym) | |
704 | "Search for a semantic tag with name SYM in database tables. | |
705 | Return the tag found or nil if not found. | |
706 | If semanticdb is not in use, use the current buffer only." | |
55b522b2 CY |
707 | (car (if (and (featurep 'semantic/db) |
708 | semanticdb-current-database | |
709 | (require 'semantic/db-find)) | |
9573e58b CY |
710 | (cdar (semanticdb-deep-find-tags-by-name sym)) |
711 | (semantic-deep-find-tags-by-name sym (current-buffer))))) | |
712 | ||
713 | (defun semantic-idle-summary-current-symbol-info-brutish () | |
714 | "Return a string message describing the current context. | |
715 | Gets a symbol with `semantic-ctxt-current-thing' and then | |
9bf6c65c | 716 | tries to find it with a deep targeted search." |
9573e58b CY |
717 | ;; Try the current "thing". |
718 | (let ((sym (car (semantic-ctxt-current-thing)))) | |
719 | (when sym | |
720 | (semantic-idle-summary-find-current-symbol-tag sym)))) | |
721 | ||
722 | (defun semantic-idle-summary-current-symbol-keyword () | |
723 | "Return a string message describing the current symbol. | |
724 | Returns a value only if it is a keyword." | |
725 | ;; Try the current "thing". | |
726 | (let ((sym (car (semantic-ctxt-current-thing)))) | |
727 | (if (and sym (semantic-lex-keyword-p sym)) | |
728 | (semantic-lex-keyword-get sym 'summary)))) | |
729 | ||
730 | (defun semantic-idle-summary-current-symbol-info-context () | |
731 | "Return a string message describing the current context. | |
732 | Use the semantic analyzer to find the symbol information." | |
733 | (let ((analysis (condition-case nil | |
734 | (semantic-analyze-current-context (point)) | |
735 | (error nil)))) | |
736 | (when analysis | |
55b522b2 | 737 | (require 'semantic/analyze) |
9573e58b CY |
738 | (semantic-analyze-interesting-tag analysis)))) |
739 | ||
740 | (defun semantic-idle-summary-current-symbol-info-default () | |
741 | "Return a string message describing the current context. | |
9bf6c65c | 742 | This function will disable loading of previously unloaded files |
9573e58b CY |
743 | by semanticdb as a time-saving measure." |
744 | (let ( | |
745 | (semanticdb-find-default-throttle | |
746 | (if (featurep 'semantic/db-find) | |
747 | (remq 'unloaded semanticdb-find-default-throttle) | |
748 | nil)) | |
749 | ) | |
750 | (save-excursion | |
751 | ;; use whicever has success first. | |
752 | (or | |
753 | (semantic-idle-summary-current-symbol-keyword) | |
754 | ||
755 | (semantic-idle-summary-current-symbol-info-context) | |
756 | ||
757 | (semantic-idle-summary-current-symbol-info-brutish) | |
758 | )))) | |
759 | ||
760 | (defvar semantic-idle-summary-out-of-context-faces | |
761 | '( | |
762 | font-lock-comment-face | |
763 | font-lock-string-face | |
764 | font-lock-doc-string-face ; XEmacs. | |
765 | font-lock-doc-face ; Emacs 21 and later. | |
766 | ) | |
767 | "List of font-lock faces that indicate a useless summary context. | |
768 | Those are generally faces used to highlight comments. | |
769 | ||
770 | It might be useful to override this variable to add comment faces | |
771 | specific to a major mode. For example, in jde mode: | |
772 | ||
773 | \(defvar-mode-local jde-mode semantic-idle-summary-out-of-context-faces | |
774 | (append (default-value 'semantic-idle-summary-out-of-context-faces) | |
775 | '(jde-java-font-lock-doc-tag-face | |
776 | jde-java-font-lock-link-face | |
777 | jde-java-font-lock-bold-face | |
778 | jde-java-font-lock-underline-face | |
779 | jde-java-font-lock-pre-face | |
780 | jde-java-font-lock-code-face)))") | |
781 | ||
782 | (defun semantic-idle-summary-useful-context-p () | |
783 | "Non-nil of we should show a summary based on context." | |
784 | (if (and (boundp 'font-lock-mode) | |
785 | font-lock-mode | |
786 | (memq (get-text-property (point) 'face) | |
787 | semantic-idle-summary-out-of-context-faces)) | |
788 | ;; The best I can think of at the moment is to disable | |
789 | ;; in comments by detecting with font-lock. | |
790 | nil | |
791 | t)) | |
792 | ||
793 | (define-overloadable-function semantic-idle-summary-current-symbol-info () | |
794 | "Return a string message describing the current context.") | |
795 | ||
796 | (make-obsolete-overload 'semantic-eldoc-current-symbol-info | |
5a916e35 SM |
797 | 'semantic-idle-summary-current-symbol-info |
798 | "23.2") | |
9573e58b CY |
799 | |
800 | (define-semantic-idle-service semantic-idle-summary | |
801 | "Display a tag summary of the lexical token under the cursor. | |
802 | Call `semantic-idle-summary-current-symbol-info' for getting the | |
803 | current tag to display information." | |
804 | (or (eq major-mode 'emacs-lisp-mode) | |
805 | (not (semantic-idle-summary-useful-context-p)) | |
806 | (let* ((found (semantic-idle-summary-current-symbol-info)) | |
807 | (str (cond ((stringp found) found) | |
808 | ((semantic-tag-p found) | |
809 | (funcall semantic-idle-summary-function | |
810 | found nil t)))) | |
811 | ) | |
812 | ;; Show the message with eldoc functions | |
813 | (require 'eldoc) | |
814 | (unless (and str (boundp 'eldoc-echo-area-use-multiline-p) | |
815 | eldoc-echo-area-use-multiline-p) | |
816 | (let ((w (1- (window-width (minibuffer-window))))) | |
817 | (if (> (length str) w) | |
818 | (setq str (substring str 0 w))))) | |
819 | (eldoc-message str)))) | |
9573e58b CY |
820 | \f |
821 | ;;; Current symbol highlight | |
822 | ;; | |
823 | ;; This mode will use context analysis to perform highlighting | |
824 | ;; of all uses of the symbol that is under the cursor. | |
825 | ;; | |
826 | ;; This is to mimic the Eclipse tool of a similar nature. | |
827 | (defvar semantic-idle-summary-highlight-face 'region | |
828 | "Face used for the summary highlight.") | |
829 | ||
830 | (defun semantic-idle-summary-maybe-highlight (tag) | |
831 | "Perhaps add highlighting onto TAG. | |
832 | TAG was found as the thing under point. If it happens to be | |
833 | visible, then highlight it." | |
55b522b2 | 834 | (require 'pulse) |
9573e58b CY |
835 | (let* ((region (when (and (semantic-tag-p tag) |
836 | (semantic-tag-with-position-p tag)) | |
837 | (semantic-tag-overlay tag))) | |
838 | (file (when (and (semantic-tag-p tag) | |
839 | (semantic-tag-with-position-p tag)) | |
840 | (semantic-tag-file-name tag))) | |
841 | (buffer (when file (get-file-buffer file))) | |
842 | ;; We use pulse, but we don't want the flashy version, | |
843 | ;; just the stable version. | |
844 | (pulse-flag nil) | |
845 | ) | |
846 | (cond ((semantic-overlay-p region) | |
0816d744 | 847 | (with-current-buffer (semantic-overlay-buffer region) |
9573e58b CY |
848 | (goto-char (semantic-overlay-start region)) |
849 | (when (pos-visible-in-window-p | |
850 | (point) (get-buffer-window (current-buffer) 'visible)) | |
851 | (if (< (semantic-overlay-end region) (point-at-eol)) | |
852 | (pulse-momentary-highlight-overlay | |
853 | region semantic-idle-summary-highlight-face) | |
854 | ;; Not the same | |
855 | (pulse-momentary-highlight-region | |
856 | (semantic-overlay-start region) | |
857 | (point-at-eol) | |
858 | semantic-idle-summary-highlight-face))) | |
859 | )) | |
860 | ((vectorp region) | |
861 | (let ((start (aref region 0)) | |
862 | (end (aref region 1))) | |
863 | (save-excursion | |
864 | (when buffer (set-buffer buffer)) | |
865 | ;; As a vector, we have no filename. Perhaps it is a | |
866 | ;; local variable? | |
867 | (when (and (<= end (point-max)) | |
868 | (pos-visible-in-window-p | |
869 | start (get-buffer-window (current-buffer) 'visible))) | |
870 | (goto-char start) | |
871 | (when (re-search-forward | |
872 | (regexp-quote (semantic-tag-name tag)) | |
873 | end t) | |
874 | ;; This is likely it, give it a try. | |
875 | (pulse-momentary-highlight-region | |
876 | start (if (<= end (point-at-eol)) end | |
877 | (point-at-eol)) | |
878 | semantic-idle-summary-highlight-face))) | |
879 | )))) | |
880 | nil)) | |
881 | ||
882 | (define-semantic-idle-service semantic-idle-tag-highlight | |
883 | "Highlight the tag, and references of the symbol under point. | |
884 | Call `semantic-analyze-current-context' to find the reference tag. | |
885 | Call `semantic-symref-hits-in-region' to identify local references." | |
55b522b2 | 886 | (require 'pulse) |
9573e58b CY |
887 | (when (semantic-idle-summary-useful-context-p) |
888 | (let* ((ctxt (semantic-analyze-current-context)) | |
889 | (Hbounds (when ctxt (oref ctxt bounds))) | |
890 | (target (when ctxt (car (reverse (oref ctxt prefix))))) | |
891 | (tag (semantic-current-tag)) | |
892 | ;; We use pulse, but we don't want the flashy version, | |
893 | ;; just the stable version. | |
894 | (pulse-flag nil)) | |
895 | (when ctxt | |
896 | ;; Highlight the original tag? Protect against problems. | |
897 | (condition-case nil | |
898 | (semantic-idle-summary-maybe-highlight target) | |
899 | (error nil)) | |
900 | ;; Identify all hits in this current tag. | |
901 | (when (semantic-tag-p target) | |
ea041226 | 902 | (require 'semantic/symref/filter) |
9573e58b CY |
903 | (semantic-symref-hits-in-region |
904 | target (lambda (start end prefix) | |
905 | (when (/= start (car Hbounds)) | |
906 | (pulse-momentary-highlight-region | |
67d3ffe4 | 907 | start end semantic-idle-summary-highlight-face)) |
9573e58b CY |
908 | (semantic-throw-on-input 'symref-highlight) |
909 | ) | |
910 | (semantic-tag-start tag) | |
911 | (semantic-tag-end tag))) | |
912 | )))) | |
913 | ||
914 | \f | |
915 | ;;; Completion Popup Mode | |
916 | ;; | |
917 | ;; This mode uses tooltips to display a (hopefully) short list of possible | |
918 | ;; completions available for the text under point. It provides | |
919 | ;; NO provision for actually filling in the values from those completions. | |
920 | ||
921 | (defun semantic-idle-completion-list-default () | |
922 | "Calculate and display a list of completions." | |
923 | (when (semantic-idle-summary-useful-context-p) | |
924 | ;; This mode can be fragile. Ignore problems. | |
925 | ;; If something doesn't do what you expect, run | |
926 | ;; the below command by hand instead. | |
927 | (condition-case nil | |
928 | (let ( | |
929 | ;; Don't go loading in oodles of header libraries in | |
930 | ;; IDLE time. | |
931 | (semanticdb-find-default-throttle | |
932 | (if (featurep 'semantic/db-find) | |
933 | (remq 'unloaded semanticdb-find-default-throttle) | |
934 | nil)) | |
935 | ) | |
936 | ;; Use idle version. | |
55b522b2 | 937 | (require 'semantic/complete) |
9573e58b CY |
938 | (semantic-complete-analyze-inline-idle) |
939 | ) | |
940 | (error nil)) | |
941 | )) | |
942 | ||
943 | (define-semantic-idle-service semantic-idle-completions | |
944 | "Display a list of possible completions in a tooltip." | |
945 | ;; Add the ability to override sometime. | |
946 | (semantic-idle-completion-list-default)) | |
947 | ||
948 | (provide 'semantic/idle) | |
949 | ||
cd23a4d3 CY |
950 | ;; Local variables: |
951 | ;; generated-autoload-file: "loaddefs.el" | |
952 | ;; generated-autoload-feature: semantic/loaddefs | |
953 | ;; generated-autoload-load-name: "semantic/idle" | |
954 | ;; End: | |
955 | ||
3999968a | 956 | ;; arch-tag: 4bfd54da-5023-4cc1-91ae-e1fefc1a8d1b |
9573e58b | 957 | ;;; semantic-idle.el ends here |