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