lisp/cedet/semantic/complete.el: Add local vars for autoloading.
[bpt/emacs.git] / lisp / cedet / semantic / complete.el
1 ;;; semantic/complete.el --- Routines for performing tag completion
2
3 ;;; Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009
4 ;;; Free Software Foundation, Inc.
5
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Keywords: syntax
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25 ;;
26 ;; Completion of tags by name using tables of semantic generated tags.
27 ;;
28 ;; While it would be a simple matter of flattening all tag known
29 ;; tables to perform completion across them using `all-completions',
30 ;; or `try-completion', that process would be slow. In particular,
31 ;; when a system database is included in the mix, the potential for a
32 ;; ludicrous number of options becomes apparent.
33 ;;
34 ;; As such, dynamically searching across tables using a prefix,
35 ;; regular expression, or other feature is needed to help find symbols
36 ;; quickly without resorting to "show me every possible option now".
37 ;;
38 ;; In addition, some symbol names will appear in multiple locations.
39 ;; If it is important to distiguish, then a way to provide a choice
40 ;; over these locations is important as well.
41 ;;
42 ;; Beyond brute force offers for completion of plain strings,
43 ;; using the smarts of semantic-analyze to provide reduced lists of
44 ;; symbols, or fancy tabbing to zoom into files to show multiple hits
45 ;; of the same name can be provided.
46 ;;
47 ;;; How it works:
48 ;;
49 ;; There are several parts of any completion engine. They are:
50 ;;
51 ;; A. Collection of possible hits
52 ;; B. Typing or selecting an option
53 ;; C. Displaying possible unique completions
54 ;; D. Using the result
55 ;;
56 ;; Here, we will treat each section separately (excluding D)
57 ;; They can then be strung together in user-visible commands to
58 ;; fullfill specific needs.
59 ;;
60 ;; COLLECTORS:
61 ;;
62 ;; A collector is an object which represents the means by which tags
63 ;; to complete on are collected. It's first job is to find all the
64 ;; tags which are to be completed against. It can also rename
65 ;; some tags if needed so long as `semantic-tag-clone' is used.
66 ;;
67 ;; Some collectors will gather all tags to complete against first
68 ;; (for in buffer queries, or other small list situations). It may
69 ;; choose to do a broad search on each completion request. Built in
70 ;; functionality automatically focuses the cache in as the user types.
71 ;;
72 ;; A collector choosing to create and rename tags could choose a
73 ;; plain name format, a postfix name such as method:class, or a
74 ;; prefix name such as class.method.
75 ;;
76 ;; DISPLAYORS
77 ;;
78 ;; A displayor is in charge if showing the user interesting things
79 ;; about available completions, and can optionally provide a focus.
80 ;; The simplest display just lists all available names in a separate
81 ;; window. It may even choose to show short names when there are
82 ;; many to choose from, or long names when there are fewer.
83 ;;
84 ;; A complex displayor could opt to help the user 'focus' on some
85 ;; range. For example, if 4 tags all have the same name, subsequent
86 ;; calls to the displayor may opt to show each tag one at a time in
87 ;; the buffer. When the user likes one, selection would cause the
88 ;; 'focus' item to be selected.
89 ;;
90 ;; CACHE FORMAT
91 ;;
92 ;; The format of the tag lists used to perform the completions are in
93 ;; semanticdb "find" format, like this:
94 ;;
95 ;; ( ( DBTABLE1 TAG1 TAG2 ...)
96 ;; ( DBTABLE2 TAG1 TAG2 ...)
97 ;; ... )
98 ;;
99 ;; INLINE vs MINIBUFFER
100 ;;
101 ;; Two major ways completion is used in Emacs is either through a
102 ;; minibuffer query, or via completion in a normal editing buffer,
103 ;; encompassing some small range of characters.
104 ;;
105 ;; Structure for both types of completion are provided here.
106 ;; `semantic-complete-read-tag-engine' will use the minibuffer.
107 ;; `semantic-complete-inline-tag-engine' will complete text in
108 ;; a buffer.
109
110 (require 'eieio)
111 (require 'eieio-opt)
112 (require 'semantic)
113 (require 'semantic/analyze)
114 (require 'semantic/ctxt)
115 (require 'semantic/decorate)
116 (require 'semantic/format)
117 (require 'semantic/tag)
118
119 (eval-when-compile
120 ;; For the semantic-find-tags-for-completion macro.
121 (require 'semantic/find))
122
123 (eval-when-compile
124 (condition-case nil
125 ;; Tooltip not available in older emacsen.
126 (require 'tooltip)
127 (error nil))
128 )
129
130 ;;; Code:
131
132 ;;; Compatibility
133 ;;
134 (if (fboundp 'minibuffer-contents)
135 (eval-and-compile (defalias 'semantic-minibuffer-contents 'minibuffer-contents))
136 (eval-and-compile (defalias 'semantic-minibuffer-contents 'buffer-string)))
137 (if (fboundp 'delete-minibuffer-contents)
138 (eval-and-compile (defalias 'semantic-delete-minibuffer-contents 'delete-minibuffer-contents))
139 (eval-and-compile (defalias 'semantic-delete-minibuffer-contents 'erase-buffer)))
140
141 (defvar semantic-complete-inline-overlay nil
142 "The overlay currently active while completing inline.")
143
144 (defun semantic-completion-inline-active-p ()
145 "Non-nil if inline completion is active."
146 (when (and semantic-complete-inline-overlay
147 (not (semantic-overlay-live-p semantic-complete-inline-overlay)))
148 (semantic-overlay-delete semantic-complete-inline-overlay)
149 (setq semantic-complete-inline-overlay nil))
150 semantic-complete-inline-overlay)
151
152 ;;; ------------------------------------------------------------
153 ;;; MINIBUFFER or INLINE utils
154 ;;
155 (defun semantic-completion-text ()
156 "Return the text that is currently in the completion buffer.
157 For a minibuffer prompt, this is the minibuffer text.
158 For inline completion, this is the text wrapped in the inline completion
159 overlay."
160 (if semantic-complete-inline-overlay
161 (semantic-complete-inline-text)
162 (semantic-minibuffer-contents)))
163
164 (defun semantic-completion-delete-text ()
165 "Delete the text that is actively being completed.
166 Presumably if you call this you will insert something new there."
167 (if semantic-complete-inline-overlay
168 (semantic-complete-inline-delete-text)
169 (semantic-delete-minibuffer-contents)))
170
171 (defun semantic-completion-message (fmt &rest args)
172 "Display the string FMT formatted with ARGS at the end of the minibuffer."
173 (if semantic-complete-inline-overlay
174 (apply 'message fmt args)
175 (message (concat (buffer-string) (apply 'format fmt args)))))
176
177 ;;; ------------------------------------------------------------
178 ;;; MINIBUFFER: Option Selection harnesses
179 ;;
180 (defvar semantic-completion-collector-engine nil
181 "The tag collector for the current completion operation.
182 Value should be an object of a subclass of
183 `semantic-completion-engine-abstract'.")
184
185 (defvar semantic-completion-display-engine nil
186 "The tag display engine for the current completion operation.
187 Value should be a ... what?")
188
189 (defvar semantic-complete-key-map
190 (let ((km (make-sparse-keymap)))
191 (define-key km " " 'semantic-complete-complete-space)
192 (define-key km "\t" 'semantic-complete-complete-tab)
193 (define-key km "\C-m" 'semantic-complete-done)
194 (define-key km "\C-g" 'abort-recursive-edit)
195 (define-key km "\M-n" 'next-history-element)
196 (define-key km "\M-p" 'previous-history-element)
197 (define-key km "\C-n" 'next-history-element)
198 (define-key km "\C-p" 'previous-history-element)
199 ;; Add history navigation
200 km)
201 "Keymap used while completing across a list of tags.")
202
203 (defvar semantic-completion-default-history nil
204 "Default history variable for any unhistoried prompt.
205 Keeps STRINGS only in the history.")
206
207
208 (defun semantic-complete-read-tag-engine (collector displayor prompt
209 default-tag initial-input
210 history)
211 "Read a semantic tag, and return a tag for the selection.
212 Argument COLLECTOR is an object which can be used to to calculate
213 a list of possible hits. See `semantic-completion-collector-engine'
214 for details on COLLECTOR.
215 Argumeng DISPLAYOR is an object used to display a list of possible
216 completions for a given prefix. See`semantic-completion-display-engine'
217 for details on DISPLAYOR.
218 PROMPT is a string to prompt with.
219 DEFAULT-TAG is a semantic tag or string to use as the default value.
220 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.
221 HISTORY is a symbol representing a variable to story the history in."
222 (let* ((semantic-completion-collector-engine collector)
223 (semantic-completion-display-engine displayor)
224 (semantic-complete-active-default nil)
225 (semantic-complete-current-matched-tag nil)
226 (default-as-tag (semantic-complete-default-to-tag default-tag))
227 (default-as-string (when (semantic-tag-p default-as-tag)
228 (semantic-tag-name default-as-tag)))
229 )
230
231 (when default-as-string
232 ;; Add this to the prompt.
233 ;;
234 ;; I really want to add a lookup of the symbol in those
235 ;; tags available to the collector and only add it if it
236 ;; is available as a possibility, but I'm too lazy right
237 ;; now.
238 ;;
239
240 ;; @todo - move from () to into the editable area
241 (if (string-match ":" prompt)
242 (setq prompt (concat
243 (substring prompt 0 (match-beginning 0))
244 " (" default-as-string ")"
245 (substring prompt (match-beginning 0))))
246 (setq prompt (concat prompt " (" default-as-string "): "))))
247 ;;
248 ;; Perform the Completion
249 ;;
250 (unwind-protect
251 (read-from-minibuffer prompt
252 initial-input
253 semantic-complete-key-map
254 nil
255 (or history
256 'semantic-completion-default-history)
257 default-tag)
258 (semantic-collector-cleanup semantic-completion-collector-engine)
259 (semantic-displayor-cleanup semantic-completion-display-engine)
260 )
261 ;;
262 ;; Extract the tag from the completion machinery.
263 ;;
264 semantic-complete-current-matched-tag
265 ))
266
267 \f
268 ;;; Util for basic completion prompts
269 ;;
270
271 (defvar semantic-complete-active-default nil
272 "The current default tag calculated for this prompt.")
273
274 (defun semantic-complete-default-to-tag (default)
275 "Convert a calculated or passed in DEFAULT into a tag."
276 (if (semantic-tag-p default)
277 ;; Just return what was passed in.
278 (setq semantic-complete-active-default default)
279 ;; If none was passed in, guess.
280 (if (null default)
281 (setq default (semantic-ctxt-current-thing)))
282 (if (null default)
283 ;; Do nothing
284 nil
285 ;; Turn default into something useful.
286 (let ((str
287 (cond
288 ;; Semantic-ctxt-current-symbol will return a list of
289 ;; strings. Technically, we should use the analyzer to
290 ;; fully extract what we need, but for now, just grab the
291 ;; first string
292 ((and (listp default) (stringp (car default)))
293 (car default))
294 ((stringp default)
295 default)
296 ((symbolp default)
297 (symbol-name default))
298 (t
299 (signal 'wrong-type-argument
300 (list default 'semantic-tag-p)))))
301 (tag nil))
302 ;; Now that we have that symbol string, look it up using the active
303 ;; collector. If we get a match, use it.
304 (save-excursion
305 (semantic-collector-calculate-completions
306 semantic-completion-collector-engine
307 str nil))
308 ;; Do we have the perfect match???
309 (let ((ml (semantic-collector-current-exact-match
310 semantic-completion-collector-engine)))
311 (when ml
312 ;; We don't care about uniqueness. Just guess for convenience
313 (setq tag (semanticdb-find-result-nth-in-buffer ml 0))))
314 ;; save it
315 (setq semantic-complete-active-default tag)
316 ;; Return it.. .whatever it may be
317 tag))))
318
319 \f
320 ;;; Prompt Return Value
321 ;;
322 ;; Getting a return value out of this completion prompt is a bit
323 ;; challenging. The read command returns the string typed in.
324 ;; We need to convert this into a valid tag. We can exit the minibuffer
325 ;; for different reasons. If we purposely exit, we must make sure
326 ;; the focused tag is calculated... preferably once.
327 (defvar semantic-complete-current-matched-tag nil
328 "Variable used to pass the tags being matched to the prompt.")
329
330 ;; semantic-displayor-focus-abstract-child-p is part of the
331 ;; semantic-displayor-focus-abstract class, defined later in this
332 ;; file.
333 (declare-function semantic-displayor-focus-abstract-child-p "semantic/complete")
334
335 (defun semantic-complete-current-match ()
336 "Calculate a match from the current completion environment.
337 Save this in our completion variable. Make sure that variable
338 is cleared if any other keypress is made.
339 Return value can be:
340 tag - a single tag that has been matched.
341 string - a message to show in the minibuffer."
342 ;; Query the environment for an active completion.
343 (let ((collector semantic-completion-collector-engine)
344 (displayor semantic-completion-display-engine)
345 (contents (semantic-completion-text))
346 matchlist
347 answer)
348 (if (string= contents "")
349 ;; The user wants the defaults!
350 (setq answer semantic-complete-active-default)
351 ;; This forces a full calculation of completion on CR.
352 (save-excursion
353 (semantic-collector-calculate-completions collector contents nil))
354 (semantic-complete-try-completion)
355 (cond
356 ;; Input match displayor focus entry
357 ((setq answer (semantic-displayor-current-focus displayor))
358 ;; We have answer, continue
359 )
360 ;; One match from the collector
361 ((setq matchlist (semantic-collector-current-exact-match collector))
362 (if (= (semanticdb-find-result-length matchlist) 1)
363 (setq answer (semanticdb-find-result-nth-in-buffer matchlist 0))
364 (if (semantic-displayor-focus-abstract-child-p displayor)
365 ;; For focusing displayors, we can claim this is
366 ;; not unique. Multiple focuses can choose the correct
367 ;; one.
368 (setq answer "Not Unique")
369 ;; If we don't have a focusing displayor, we need to do something
370 ;; graceful. First, see if all the matches have the same name.
371 (let ((allsame t)
372 (firstname (semantic-tag-name
373 (car
374 (semanticdb-find-result-nth matchlist 0)))
375 )
376 (cnt 1)
377 (max (semanticdb-find-result-length matchlist)))
378 (while (and allsame (< cnt max))
379 (if (not (string=
380 firstname
381 (semantic-tag-name
382 (car
383 (semanticdb-find-result-nth matchlist cnt)))))
384 (setq allsame nil))
385 (setq cnt (1+ cnt))
386 )
387 ;; Now we know if they are all the same. If they are, just
388 ;; accept the first, otherwise complain.
389 (if allsame
390 (setq answer (semanticdb-find-result-nth-in-buffer
391 matchlist 0))
392 (setq answer "Not Unique"))
393 ))))
394 ;; No match
395 (t
396 (setq answer "No Match")))
397 )
398 ;; Set it into our completion target.
399 (when (semantic-tag-p answer)
400 (setq semantic-complete-current-matched-tag answer)
401 ;; Make sure it is up to date by clearing it if the user dares
402 ;; to touch the keyboard.
403 (add-hook 'pre-command-hook
404 (lambda () (setq semantic-complete-current-matched-tag nil)))
405 )
406 ;; Return it
407 answer
408 ))
409
410 \f
411 ;;; Keybindings
412 ;;
413 ;; Keys are bound to to perform completion using our mechanisms.
414 ;; Do that work here.
415 (defun semantic-complete-done ()
416 "Accept the current input."
417 (interactive)
418 (let ((ans (semantic-complete-current-match)))
419 (if (stringp ans)
420 (semantic-completion-message (concat " [" ans "]"))
421 (exit-minibuffer)))
422 )
423
424 (defun semantic-complete-complete-space ()
425 "Complete the partial input in the minibuffer."
426 (interactive)
427 (semantic-complete-do-completion t))
428
429 (defun semantic-complete-complete-tab ()
430 "Complete the partial input in the minibuffer as far as possible."
431 (interactive)
432 (semantic-complete-do-completion))
433
434 ;;; Completion Functions
435 ;;
436 ;; Thees routines are functional entry points to performing completion.
437 ;;
438 (defun semantic-complete-hack-word-boundaries (original new)
439 "Return a string to use for completion.
440 ORIGINAL is the text in the minibuffer.
441 NEW is the new text to insert into the minibuffer.
442 Within the difference bounds of ORIGINAL and NEW, shorten NEW
443 to the nearest word boundary, and return that."
444 (save-match-data
445 (let* ((diff (substring new (length original)))
446 (end (string-match "\\>" diff))
447 (start (string-match "\\<" diff)))
448 (cond
449 ((and start (> start 0))
450 ;; If start is greater than 0, include only the new
451 ;; white-space stuff
452 (concat original (substring diff 0 start)))
453 (end
454 (concat original (substring diff 0 end)))
455 (t new)))))
456
457 (defun semantic-complete-try-completion (&optional partial)
458 "Try a completion for the current minibuffer.
459 If PARTIAL, do partial completion stopping at spaces."
460 (let ((comp (semantic-collector-try-completion
461 semantic-completion-collector-engine
462 (semantic-completion-text))))
463 (cond
464 ((null comp)
465 (semantic-completion-message " [No Match]")
466 (ding)
467 )
468 ((stringp comp)
469 (if (string= (semantic-completion-text) comp)
470 (when partial
471 ;; Minibuffer isn't changing AND the text is not unique.
472 ;; Test for partial completion over a word separator character.
473 ;; If there is one available, use that so that SPC can
474 ;; act like a SPC insert key.
475 (let ((newcomp (semantic-collector-current-whitespace-completion
476 semantic-completion-collector-engine)))
477 (when newcomp
478 (semantic-completion-delete-text)
479 (insert newcomp))
480 ))
481 (when partial
482 (let ((orig (semantic-completion-text)))
483 ;; For partial completion, we stop and step over
484 ;; word boundaries. Use this nifty function to do
485 ;; that calculation for us.
486 (setq comp
487 (semantic-complete-hack-word-boundaries orig comp))))
488 ;; Do the replacement.
489 (semantic-completion-delete-text)
490 (insert comp))
491 )
492 ((and (listp comp) (semantic-tag-p (car comp)))
493 (unless (string= (semantic-completion-text)
494 (semantic-tag-name (car comp)))
495 ;; A fully unique completion was available.
496 (semantic-completion-delete-text)
497 (insert (semantic-tag-name (car comp))))
498 ;; The match is complete
499 (if (= (length comp) 1)
500 (semantic-completion-message " [Complete]")
501 (semantic-completion-message " [Complete, but not unique]"))
502 )
503 (t nil))))
504
505 (defun semantic-complete-do-completion (&optional partial inline)
506 "Do a completion for the current minibuffer.
507 If PARTIAL, do partial completion stopping at spaces.
508 if INLINE, then completion is happening inline in a buffer."
509 (let* ((collector semantic-completion-collector-engine)
510 (displayor semantic-completion-display-engine)
511 (contents (semantic-completion-text))
512 (ans nil))
513
514 (save-excursion
515 (semantic-collector-calculate-completions collector contents partial))
516 (let* ((na (semantic-complete-next-action partial)))
517 (cond
518 ;; We're all done, but only from a very specific
519 ;; area of completion.
520 ((eq na 'done)
521 (semantic-completion-message " [Complete]")
522 (setq ans 'done))
523 ;; Perform completion
524 ((or (eq na 'complete)
525 (eq na 'complete-whitespace))
526 (semantic-complete-try-completion partial)
527 (setq ans 'complete))
528 ;; We need to display the completions.
529 ;; Set the completions into the display engine
530 ((or (eq na 'display) (eq na 'displayend))
531 (semantic-displayor-set-completions
532 displayor
533 (or
534 (and (not (eq na 'displayend))
535 (semantic-collector-current-exact-match collector))
536 (semantic-collector-all-completions collector contents))
537 contents)
538 ;; Ask the displayor to display them.
539 (semantic-displayor-show-request displayor))
540 ((eq na 'scroll)
541 (semantic-displayor-scroll-request displayor)
542 )
543 ((eq na 'focus)
544 (semantic-displayor-focus-next displayor)
545 (semantic-displayor-focus-request displayor)
546 )
547 ((eq na 'empty)
548 (semantic-completion-message " [No Match]"))
549 (t nil)))
550 ans))
551
552 \f
553 ;;; ------------------------------------------------------------
554 ;;; INLINE: tag completion harness
555 ;;
556 ;; Unlike the minibuffer, there is no mode nor other traditional
557 ;; means of reading user commands in completion mode. Instead
558 ;; we use a pre-command-hook to inset in our commands, and to
559 ;; push ourselves out of this mode on alternate keypresses.
560 (defvar semantic-complete-inline-map
561 (let ((km (make-sparse-keymap)))
562 (define-key km "\C-i" 'semantic-complete-inline-TAB)
563 (define-key km "\M-p" 'semantic-complete-inline-up)
564 (define-key km "\M-n" 'semantic-complete-inline-down)
565 (define-key km "\C-m" 'semantic-complete-inline-done)
566 (define-key km "\C-\M-c" 'semantic-complete-inline-exit)
567 (define-key km "\C-g" 'semantic-complete-inline-quit)
568 (define-key km "?"
569 (lambda () (interactive)
570 (describe-variable 'semantic-complete-inline-map)))
571 km)
572 "Keymap used while performing Semantic inline completion.
573 \\{semantic-complete-inline-map}")
574
575 (defface semantic-complete-inline-face
576 '((((class color) (background dark))
577 (:underline "yellow"))
578 (((class color) (background light))
579 (:underline "brown")))
580 "*Face used to show the region being completed inline.
581 The face is used in `semantic-complete-inline-tag-engine'."
582 :group 'semantic-faces)
583
584 (defun semantic-complete-inline-text ()
585 "Return the text that is being completed inline.
586 Similar to `minibuffer-contents' when completing in the minibuffer."
587 (let ((s (semantic-overlay-start semantic-complete-inline-overlay))
588 (e (semantic-overlay-end semantic-complete-inline-overlay)))
589 (if (= s e)
590 ""
591 (buffer-substring-no-properties s e ))))
592
593 (defun semantic-complete-inline-delete-text ()
594 "Delete the text currently being completed in the current buffer."
595 (delete-region
596 (semantic-overlay-start semantic-complete-inline-overlay)
597 (semantic-overlay-end semantic-complete-inline-overlay)))
598
599 (defun semantic-complete-inline-done ()
600 "This completion thing is DONE, OR, insert a newline."
601 (interactive)
602 (let* ((displayor semantic-completion-display-engine)
603 (tag (semantic-displayor-current-focus displayor)))
604 (if tag
605 (let ((txt (semantic-completion-text)))
606 (insert (substring (semantic-tag-name tag)
607 (length txt)))
608 (semantic-complete-inline-exit))
609
610 ;; Get whatever binding RET usually has.
611 (let ((fcn
612 (condition-case nil
613 (lookup-key (current-active-maps) (this-command-keys))
614 (error
615 ;; I don't know why, but for some reason the above
616 ;; throws an error sometimes.
617 (lookup-key (current-global-map) (this-command-keys))
618 ))))
619 (when fcn
620 (funcall fcn)))
621 )))
622
623 (defun semantic-complete-inline-quit ()
624 "Quit an inline edit."
625 (interactive)
626 (semantic-complete-inline-exit)
627 (keyboard-quit))
628
629 (defun semantic-complete-inline-exit ()
630 "Exit inline completion mode."
631 (interactive)
632 ;; Remove this hook FIRST!
633 (remove-hook 'pre-command-hook 'semantic-complete-pre-command-hook)
634
635 (condition-case nil
636 (progn
637 (when semantic-completion-collector-engine
638 (semantic-collector-cleanup semantic-completion-collector-engine))
639 (when semantic-completion-display-engine
640 (semantic-displayor-cleanup semantic-completion-display-engine))
641
642 (when semantic-complete-inline-overlay
643 (let ((wc (semantic-overlay-get semantic-complete-inline-overlay
644 'window-config-start))
645 (buf (semantic-overlay-buffer semantic-complete-inline-overlay))
646 )
647 (semantic-overlay-delete semantic-complete-inline-overlay)
648 (setq semantic-complete-inline-overlay nil)
649 ;; DONT restore the window configuration if we just
650 ;; switched windows!
651 (when (eq buf (current-buffer))
652 (set-window-configuration wc))
653 ))
654
655 (setq semantic-completion-collector-engine nil
656 semantic-completion-display-engine nil))
657 (error nil))
658
659 ;; Remove this hook LAST!!!
660 ;; This will force us back through this function if there was
661 ;; some sort of error above.
662 (remove-hook 'post-command-hook 'semantic-complete-post-command-hook)
663
664 ;;(message "Exiting inline completion.")
665 )
666
667 (defun semantic-complete-pre-command-hook ()
668 "Used to redefine what commands are being run while completing.
669 When installed as a `pre-command-hook' the special keymap
670 `semantic-complete-inline-map' is queried to replace commands normally run.
671 Commands which edit what is in the region of interest operate normally.
672 Commands which would take us out of the region of interest, or our
673 quit hook, will exit this completion mode."
674 (let ((fcn (lookup-key semantic-complete-inline-map
675 (this-command-keys) nil)))
676 (cond ((commandp fcn)
677 (setq this-command fcn))
678 (t nil)))
679 )
680
681 (defun semantic-complete-post-command-hook ()
682 "Used to determine if we need to exit inline completion mode.
683 If completion mode is active, check to see if we are within
684 the bounds of `semantic-complete-inline-overlay', or within
685 a reasonable distance."
686 (condition-case nil
687 ;; Exit if something bad happened.
688 (if (not semantic-complete-inline-overlay)
689 (progn
690 ;;(message "Inline Hook installed, but overlay deleted.")
691 (semantic-complete-inline-exit))
692 ;; Exit if commands caused us to exit the area of interest
693 (let ((s (semantic-overlay-start semantic-complete-inline-overlay))
694 (e (semantic-overlay-end semantic-complete-inline-overlay))
695 (b (semantic-overlay-buffer semantic-complete-inline-overlay))
696 (txt nil)
697 )
698 (cond
699 ;; EXIT when we are no longer in a good place.
700 ((or (not (eq b (current-buffer)))
701 (< (point) s)
702 (> (point) e))
703 ;;(message "Exit: %S %S %S" s e (point))
704 (semantic-complete-inline-exit)
705 )
706 ;; Exit if the user typed in a character that is not part
707 ;; of the symbol being completed.
708 ((and (setq txt (semantic-completion-text))
709 (not (string= txt ""))
710 (and (/= (point) s)
711 (save-excursion
712 (forward-char -1)
713 (not (looking-at "\\(\\w\\|\\s_\\)")))))
714 ;;(message "Non symbol character.")
715 (semantic-complete-inline-exit))
716 ((lookup-key semantic-complete-inline-map
717 (this-command-keys) nil)
718 ;; If the last command was one of our completion commands,
719 ;; then do nothing.
720 nil
721 )
722 (t
723 ;; Else, show completions now
724 (semantic-complete-inline-force-display)
725
726 ))))
727 ;; If something goes terribly wrong, clean up after ourselves.
728 (error (semantic-complete-inline-exit))))
729
730 (defun semantic-complete-inline-force-display ()
731 "Force the display of whatever the current completions are.
732 DO NOT CALL THIS IF THE INLINE COMPLETION ENGINE IS NOT ACTIVE."
733 (condition-case e
734 (save-excursion
735 (let ((collector semantic-completion-collector-engine)
736 (displayor semantic-completion-display-engine)
737 (contents (semantic-completion-text)))
738 (when collector
739 (semantic-collector-calculate-completions
740 collector contents nil)
741 (semantic-displayor-set-completions
742 displayor
743 (semantic-collector-all-completions collector contents)
744 contents)
745 ;; Ask the displayor to display them.
746 (semantic-displayor-show-request displayor))
747 ))
748 (error (message "Bug Showing Completions: %S" e))))
749
750 (defun semantic-complete-inline-tag-engine
751 (collector displayor buffer start end)
752 "Perform completion based on semantic tags in a buffer.
753 Argument COLLECTOR is an object which can be used to to calculate
754 a list of possible hits. See `semantic-completion-collector-engine'
755 for details on COLLECTOR.
756 Argumeng DISPLAYOR is an object used to display a list of possible
757 completions for a given prefix. See`semantic-completion-display-engine'
758 for details on DISPLAYOR.
759 BUFFER is the buffer in which completion will take place.
760 START is a location for the start of the full symbol.
761 If the symbol being completed is \"foo.ba\", then START
762 is on the \"f\" character.
763 END is at the end of the current symbol being completed."
764 ;; Set us up for doing completion
765 (setq semantic-completion-collector-engine collector
766 semantic-completion-display-engine displayor)
767 ;; Create an overlay
768 (setq semantic-complete-inline-overlay
769 (semantic-make-overlay start end buffer nil t))
770 (semantic-overlay-put semantic-complete-inline-overlay
771 'face
772 'semantic-complete-inline-face)
773 (semantic-overlay-put semantic-complete-inline-overlay
774 'window-config-start
775 (current-window-configuration))
776 ;; Install our command hooks
777 (add-hook 'pre-command-hook 'semantic-complete-pre-command-hook)
778 (add-hook 'post-command-hook 'semantic-complete-post-command-hook)
779 ;; Go!
780 (semantic-complete-inline-force-display)
781 )
782
783 ;;; Inline Completion Keymap Functions
784 ;;
785 (defun semantic-complete-inline-TAB ()
786 "Perform inline completion."
787 (interactive)
788 (let ((cmpl (semantic-complete-do-completion nil t)))
789 (cond
790 ((eq cmpl 'complete)
791 (semantic-complete-inline-force-display))
792 ((eq cmpl 'done)
793 (semantic-complete-inline-done))
794 ))
795 )
796
797 (defun semantic-complete-inline-down()
798 "Focus forwards through the displayor."
799 (interactive)
800 (let ((displayor semantic-completion-display-engine))
801 (semantic-displayor-focus-next displayor)
802 (semantic-displayor-focus-request displayor)
803 ))
804
805 (defun semantic-complete-inline-up ()
806 "Focus backwards through the displayor."
807 (interactive)
808 (let ((displayor semantic-completion-display-engine))
809 (semantic-displayor-focus-previous displayor)
810 (semantic-displayor-focus-request displayor)
811 ))
812
813 \f
814 ;;; ------------------------------------------------------------
815 ;;; Interactions between collection and displaying
816 ;;
817 ;; Functional routines used to help collectors communicate with
818 ;; the current displayor, or for the previous section.
819
820 (defun semantic-complete-next-action (partial)
821 "Determine what the next completion action should be.
822 PARTIAL is non-nil if we are doing partial completion.
823 First, the collector can determine if we should perform a completion or not.
824 If there is nothing to complete, then the displayor determines if we are
825 to show a completion list, scroll, or perhaps do a focus (if it is capable.)
826 Expected return values are:
827 done -> We have a singular match
828 empty -> There are no matches to the current text
829 complete -> Perform a completion action
830 complete-whitespace -> Complete next whitespace type character.
831 display -> Show the list of completions
832 scroll -> The completions have been shown, and the user keeps hitting
833 the complete button. If possible, scroll the completions
834 focus -> The displayor knows how to shift focus among possible completions.
835 Let it do that.
836 displayend -> Whatever options the displayor had for repeating options, there
837 are none left. Try something new."
838 (let ((ans1 (semantic-collector-next-action
839 semantic-completion-collector-engine
840 partial))
841 (ans2 (semantic-displayor-next-action
842 semantic-completion-display-engine))
843 )
844 (cond
845 ;; No collector answer, use displayor answer.
846 ((not ans1)
847 ans2)
848 ;; Displayor selection of 'scroll, 'display, or 'focus trumps
849 ;; 'done
850 ((and (eq ans1 'done) ans2)
851 ans2)
852 ;; Use ans1 when we have it.
853 (t
854 ans1))))
855
856
857 \f
858 ;;; ------------------------------------------------------------
859 ;;; Collection Engines
860 ;;
861 ;; Collection engines can scan tags from the current environment and
862 ;; provide lists of possible completions.
863 ;;
864 ;; General features of the abstract collector:
865 ;; * Cache completion lists between uses
866 ;; * Cache itself per buffer. Handle reparse hooks
867 ;;
868 ;; Key Interface Functions to implement:
869 ;; * semantic-collector-next-action
870 ;; * semantic-collector-calculate-completions
871 ;; * semantic-collector-try-completion
872 ;; * semantic-collector-all-completions
873
874 (defvar semantic-collector-per-buffer-list nil
875 "List of collectors active in this buffer.")
876 (make-variable-buffer-local 'semantic-collector-per-buffer-list)
877
878 (defvar semantic-collector-list nil
879 "List of global collectors active this session.")
880
881 (defclass semantic-collector-abstract ()
882 ((buffer :initarg :buffer
883 :type buffer
884 :documentation "Originating buffer for this collector.
885 Some collectors use a given buffer as a starting place while looking up
886 tags.")
887 (cache :initform nil
888 :type (or null semanticdb-find-result-with-nil)
889 :documentation "Cache of tags.
890 These tags are re-used during a completion session.
891 Sometimes these tags are cached between completion sessions.")
892 (last-all-completions :initarg nil
893 :type semanticdb-find-result-with-nil
894 :documentation "Last result of `all-completions'.
895 This result can be used for refined completions as `last-prefix' gets
896 closer to a specific result.")
897 (last-prefix :type string
898 :protection :protected
899 :documentation "The last queried prefix.
900 This prefix can be used to cache intermediate completion offers.
901 making the action of homing in on a token faster.")
902 (last-completion :type (or null string)
903 :documentation "The last calculated completion.
904 This completion is calculated and saved for future use.")
905 (last-whitespace-completion :type (or null string)
906 :documentation "The last whitespace completion.
907 For partial completion, SPC will disabiguate over whitespace type
908 characters. This is the last calculated version.")
909 (current-exact-match :type list
910 :protection :protected
911 :documentation "The list of matched tags.
912 When tokens are matched, they are added to this list.")
913 )
914 "Root class for completion engines.
915 The baseclass provides basic functionality for interacting with
916 a completion displayor object, and tracking the current progress
917 of a completion."
918 :abstract t)
919
920 (defmethod semantic-collector-cleanup ((obj semantic-collector-abstract))
921 "Clean up any mess this collector may have."
922 nil)
923
924 (defmethod semantic-collector-next-action
925 ((obj semantic-collector-abstract) partial)
926 "What should we do next? OBJ can predict a next good action.
927 PARTIAL indicates if we are doing a partial completion."
928 (if (and (slot-boundp obj 'last-completion)
929 (string= (semantic-completion-text) (oref obj last-completion)))
930 (let* ((cem (semantic-collector-current-exact-match obj))
931 (cemlen (semanticdb-find-result-length cem))
932 (cac (semantic-collector-all-completions
933 obj (semantic-completion-text)))
934 (caclen (semanticdb-find-result-length cac)))
935 (cond ((and cem (= cemlen 1)
936 cac (> caclen 1)
937 (eq last-command this-command))
938 ;; Defer to the displayor...
939 nil)
940 ((and cem (= cemlen 1))
941 'done)
942 ((and (not cem) (not cac))
943 'empty)
944 ((and partial (semantic-collector-try-completion-whitespace
945 obj (semantic-completion-text)))
946 'complete-whitespace)))
947 'complete))
948
949 (defmethod semantic-collector-last-prefix= ((obj semantic-collector-abstract)
950 last-prefix)
951 "Return non-nil if OBJ's prefix matches PREFIX."
952 (and (slot-boundp obj 'last-prefix)
953 (string= (oref obj last-prefix) last-prefix)))
954
955 (defmethod semantic-collector-get-cache ((obj semantic-collector-abstract))
956 "Get the raw cache of tags for completion.
957 Calculate the cache if there isn't one."
958 (or (oref obj cache)
959 (semantic-collector-calculate-cache obj)))
960
961 (defmethod semantic-collector-calculate-completions-raw
962 ((obj semantic-collector-abstract) prefix completionlist)
963 "Calculate the completions for prefix from completionlist.
964 Output must be in semanticdb Find result format."
965 ;; Must output in semanticdb format
966 (let ((table (save-excursion
967 (set-buffer (oref obj buffer))
968 semanticdb-current-table))
969 (result (semantic-find-tags-for-completion
970 prefix
971 ;; To do this kind of search with a pre-built completion
972 ;; list, we need to strip it first.
973 (semanticdb-strip-find-results completionlist)))
974 )
975 (if result
976 (list (cons table result)))))
977
978 (defmethod semantic-collector-calculate-completions
979 ((obj semantic-collector-abstract) prefix partial)
980 "Calculate completions for prefix as setup for other queries."
981 (let* ((case-fold-search semantic-case-fold)
982 (same-prefix-p (semantic-collector-last-prefix= obj prefix))
983 (completionlist
984 (if (or same-prefix-p
985 (and (slot-boundp obj 'last-prefix)
986 (eq (compare-strings (oref obj last-prefix) 0 nil
987 prefix 0 (length prefix))
988 t)))
989 ;; New prefix is subset of old prefix
990 (oref obj last-all-completions)
991 (semantic-collector-get-cache obj)))
992 ;; Get the result
993 (answer (if same-prefix-p
994 completionlist
995 (semantic-collector-calculate-completions-raw
996 obj prefix completionlist))
997 )
998 (completion nil)
999 (complete-not-uniq nil)
1000 )
1001 ;;(semanticdb-find-result-test answer)
1002 (when (not same-prefix-p)
1003 ;; Save results if it is interesting and beneficial
1004 (oset obj last-prefix prefix)
1005 (oset obj last-all-completions answer))
1006 ;; Now calculate the completion.
1007 (setq completion (try-completion
1008 prefix
1009 (semanticdb-strip-find-results answer)))
1010 (oset obj last-whitespace-completion nil)
1011 (oset obj current-exact-match nil)
1012 ;; Only do this if a completion was found. Letting a nil in
1013 ;; could cause a full semanticdb search by accident.
1014 (when completion
1015 (oset obj last-completion
1016 (cond
1017 ;; Unique match in AC. Last completion is a match.
1018 ;; Also set the current-exact-match.
1019 ((eq completion t)
1020 (oset obj current-exact-match answer)
1021 prefix)
1022 ;; It may be complete (a symbol) but still not unique.
1023 ;; We can capture a match
1024 ((setq complete-not-uniq
1025 (semanticdb-find-tags-by-name
1026 prefix
1027 answer))
1028 (oset obj current-exact-match
1029 complete-not-uniq)
1030 prefix
1031 )
1032 ;; Non unique match, return the string that handles
1033 ;; completion
1034 (t (or completion prefix))
1035 )))
1036 ))
1037
1038 (defmethod semantic-collector-try-completion-whitespace
1039 ((obj semantic-collector-abstract) prefix)
1040 "For OBJ, do whatepsace completion based on PREFIX.
1041 This implies that if there are two completions, one matching
1042 the test \"preifx\\>\", and one not, the one matching the full
1043 word version of PREFIX will be chosen, and that text returned.
1044 This function requires that `semantic-collector-calculate-completions'
1045 has been run first."
1046 (let* ((ac (semantic-collector-all-completions obj prefix))
1047 (matchme (concat "^" prefix "\\>"))
1048 (compare (semanticdb-find-tags-by-name-regexp matchme ac))
1049 (numtag (semanticdb-find-result-length compare))
1050 )
1051 (if compare
1052 (let* ((idx 0)
1053 (cutlen (1+ (length prefix)))
1054 (twws (semanticdb-find-result-nth compare idx)))
1055 ;; Is our tag with whitespace a match that has whitespace
1056 ;; after it, or just an already complete symbol?
1057 (while (and (< idx numtag)
1058 (< (length (semantic-tag-name (car twws))) cutlen))
1059 (setq idx (1+ idx)
1060 twws (semanticdb-find-result-nth compare idx)))
1061 (when (and twws (car-safe twws))
1062 ;; If COMPARE has succeeded, then we should take the very
1063 ;; first match, and extend prefix by one character.
1064 (oset obj last-whitespace-completion
1065 (substring (semantic-tag-name (car twws))
1066 0 cutlen))))
1067 )))
1068
1069
1070 (defmethod semantic-collector-current-exact-match ((obj semantic-collector-abstract))
1071 "Return the active valid MATCH from the semantic collector.
1072 For now, just return the first element from our list of available
1073 matches. For semanticdb based results, make sure the file is loaded
1074 into a buffer."
1075 (when (slot-boundp obj 'current-exact-match)
1076 (oref obj current-exact-match)))
1077
1078 (defmethod semantic-collector-current-whitespace-completion ((obj semantic-collector-abstract))
1079 "Return the active whitespace completion value."
1080 (when (slot-boundp obj 'last-whitespace-completion)
1081 (oref obj last-whitespace-completion)))
1082
1083 (defmethod semantic-collector-get-match ((obj semantic-collector-abstract))
1084 "Return the active valid MATCH from the semantic collector.
1085 For now, just return the first element from our list of available
1086 matches. For semanticdb based results, make sure the file is loaded
1087 into a buffer."
1088 (when (slot-boundp obj 'current-exact-match)
1089 (semanticdb-find-result-nth-in-buffer (oref obj current-exact-match) 0)))
1090
1091 (defmethod semantic-collector-all-completions
1092 ((obj semantic-collector-abstract) prefix)
1093 "For OBJ, retrieve all completions matching PREFIX.
1094 The returned list consists of all the tags currently
1095 matching PREFIX."
1096 (when (slot-boundp obj 'last-all-completions)
1097 (oref obj last-all-completions)))
1098
1099 (defmethod semantic-collector-try-completion
1100 ((obj semantic-collector-abstract) prefix)
1101 "For OBJ, attempt to match PREFIX.
1102 See `try-completion' for details on how this works.
1103 Return nil for no match.
1104 Return a string for a partial match.
1105 For a unique match of PREFIX, return the list of all tags
1106 with that name."
1107 (if (slot-boundp obj 'last-completion)
1108 (oref obj last-completion)))
1109
1110 (defmethod semantic-collector-calculate-cache
1111 ((obj semantic-collector-abstract))
1112 "Calculate the completion cache for OBJ."
1113 nil
1114 )
1115
1116 (defmethod semantic-collector-flush ((this semantic-collector-abstract))
1117 "Flush THIS collector object, clearing any caches and prefix."
1118 (oset this cache nil)
1119 (slot-makeunbound this 'last-prefix)
1120 (slot-makeunbound this 'last-completion)
1121 (slot-makeunbound this 'last-all-completions)
1122 (slot-makeunbound this 'current-exact-match)
1123 )
1124
1125 ;;; PER BUFFER
1126 ;;
1127 (defclass semantic-collector-buffer-abstract (semantic-collector-abstract)
1128 ()
1129 "Root class for per-buffer completion engines.
1130 These collectors track themselves on a per-buffer basis."
1131 :abstract t)
1132
1133 (defmethod constructor :STATIC ((this semantic-collector-buffer-abstract)
1134 newname &rest fields)
1135 "Reuse previously created objects of this type in buffer."
1136 (let ((old nil)
1137 (bl semantic-collector-per-buffer-list))
1138 (while (and bl (null old))
1139 (if (eq (object-class (car bl)) this)
1140 (setq old (car bl))))
1141 (unless old
1142 (let ((new (call-next-method)))
1143 (add-to-list 'semantic-collector-per-buffer-list new)
1144 (setq old new)))
1145 (slot-makeunbound old 'last-completion)
1146 (slot-makeunbound old 'last-prefix)
1147 (slot-makeunbound old 'current-exact-match)
1148 old))
1149
1150 ;; Buffer specific collectors should flush themselves
1151 (defun semantic-collector-buffer-flush (newcache)
1152 "Flush all buffer collector objects.
1153 NEWCACHE is the new tag table, but we ignore it."
1154 (condition-case nil
1155 (let ((l semantic-collector-per-buffer-list))
1156 (while l
1157 (if (car l) (semantic-collector-flush (car l)))
1158 (setq l (cdr l))))
1159 (error nil)))
1160
1161 (add-hook 'semantic-after-toplevel-cache-change-hook
1162 'semantic-collector-buffer-flush)
1163
1164 ;;; DEEP BUFFER SPECIFIC COMPLETION
1165 ;;
1166 (defclass semantic-collector-buffer-deep
1167 (semantic-collector-buffer-abstract)
1168 ()
1169 "Completion engine for tags in the current buffer.
1170 When searching for a tag, uses semantic deep searche functions.
1171 Basics search only in the current buffer.")
1172
1173 (defmethod semantic-collector-calculate-cache
1174 ((obj semantic-collector-buffer-deep))
1175 "Calculate the completion cache for OBJ.
1176 Uses `semantic-flatten-tags-table'"
1177 (oset obj cache
1178 ;; Must create it in SEMANTICDB find format.
1179 ;; ( ( DBTABLE TAG TAG ... ) ... )
1180 (list
1181 (cons semanticdb-current-table
1182 (semantic-flatten-tags-table (oref obj buffer))))))
1183
1184 ;;; PROJECT SPECIFIC COMPLETION
1185 ;;
1186 (defclass semantic-collector-project-abstract (semantic-collector-abstract)
1187 ((path :initarg :path
1188 :initform nil
1189 :documentation "List of database tables to search.
1190 At creation time, it can be anything accepted by
1191 `semanticdb-find-translate-path' as a PATH argument.")
1192 )
1193 "Root class for project wide completion engines.
1194 Uses semanticdb for searching all tags in the current project."
1195 :abstract t)
1196
1197 ;;; Project Search
1198 (defclass semantic-collector-project (semantic-collector-project-abstract)
1199 ()
1200 "Completion engine for tags in a project.")
1201
1202
1203 (defmethod semantic-collector-calculate-completions-raw
1204 ((obj semantic-collector-project) prefix completionlist)
1205 "Calculate the completions for prefix from completionlist."
1206 (semanticdb-find-tags-for-completion prefix (oref obj path)))
1207
1208 ;;; Brutish Project search
1209 (defclass semantic-collector-project-brutish (semantic-collector-project-abstract)
1210 ()
1211 "Completion engine for tags in a project.")
1212
1213 (declare-function semanticdb-brute-deep-find-tags-for-completion
1214 "semantic/db-find")
1215
1216 (defmethod semantic-collector-calculate-completions-raw
1217 ((obj semantic-collector-project-brutish) prefix completionlist)
1218 "Calculate the completions for prefix from completionlist."
1219 (require 'semantic/db-find)
1220 (semanticdb-brute-deep-find-tags-for-completion prefix (oref obj path)))
1221
1222 (defclass semantic-collector-analyze-completions (semantic-collector-abstract)
1223 ((context :initarg :context
1224 :type semantic-analyze-context
1225 :documentation "An analysis context.
1226 Specifies some context location from whence completion lists will be drawn."
1227 )
1228 (first-pass-completions :type list
1229 :documentation "List of valid completion tags.
1230 This list of tags is generated when completion starts. All searches
1231 derive from this list.")
1232 )
1233 "Completion engine that uses the context analyzer to provide options.
1234 The only options available for completion are those which can be logically
1235 inserted into the current context.")
1236
1237 (defmethod semantic-collector-calculate-completions-raw
1238 ((obj semantic-collector-analyze-completions) prefix completionlist)
1239 "calculate the completions for prefix from completionlist."
1240 ;; if there are no completions yet, calculate them.
1241 (if (not (slot-boundp obj 'first-pass-completions))
1242 (oset obj first-pass-completions
1243 (semantic-analyze-possible-completions (oref obj context))))
1244 ;; search our cached completion list. make it look like a semanticdb
1245 ;; results type.
1246 (list (cons (save-excursion
1247 (set-buffer (oref (oref obj context) buffer))
1248 semanticdb-current-table)
1249 (semantic-find-tags-for-completion
1250 prefix
1251 (oref obj first-pass-completions)))))
1252
1253 \f
1254 ;;; ------------------------------------------------------------
1255 ;;; Tag List Display Engines
1256 ;;
1257 ;; A typical displayor accepts a pre-determined list of completions
1258 ;; generated by a collector. This format is in semanticdb search
1259 ;; form. This vaguely standard form is a bit challenging to navigate
1260 ;; because the tags do not contain buffer info, but the file assocated
1261 ;; with the tags preceed the tag in the list.
1262 ;;
1263 ;; Basic displayors don't care, and can strip the results.
1264 ;; Advanced highlighting displayors need to know when they need
1265 ;; to load a file so that the tag in question can be highlighted.
1266 ;;
1267 ;; Key interface methods to a displayor are:
1268 ;; * semantic-displayor-next-action
1269 ;; * semantic-displayor-set-completions
1270 ;; * semantic-displayor-current-focus
1271 ;; * semantic-displayor-show-request
1272 ;; * semantic-displayor-scroll-request
1273 ;; * semantic-displayor-focus-request
1274
1275 (defclass semantic-displayor-abstract ()
1276 ((table :type (or null semanticdb-find-result-with-nil)
1277 :initform nil
1278 :protection :protected
1279 :documentation "List of tags this displayor is showing.")
1280 (last-prefix :type string
1281 :protection :protected
1282 :documentation "Prefix associated with slot `table'")
1283 )
1284 "Abstract displayor baseclass.
1285 Manages the display of some number of tags.
1286 Provides the basics for a displayor, including interacting with
1287 a collector, and tracking tables of completion to display."
1288 :abstract t)
1289
1290 (defmethod semantic-displayor-cleanup ((obj semantic-displayor-abstract))
1291 "Clean up any mess this displayor may have."
1292 nil)
1293
1294 (defmethod semantic-displayor-next-action ((obj semantic-displayor-abstract))
1295 "The next action to take on the minibuffer related to display."
1296 (if (and (slot-boundp obj 'last-prefix)
1297 (string= (oref obj last-prefix) (semantic-completion-text))
1298 (eq last-command this-command))
1299 'scroll
1300 'display))
1301
1302 (defmethod semantic-displayor-set-completions ((obj semantic-displayor-abstract)
1303 table prefix)
1304 "Set the list of tags to be completed over to TABLE."
1305 (oset obj table table)
1306 (oset obj last-prefix prefix))
1307
1308 (defmethod semantic-displayor-show-request ((obj semantic-displayor-abstract))
1309 "A request to show the current tags table."
1310 (ding))
1311
1312 (defmethod semantic-displayor-focus-request ((obj semantic-displayor-abstract))
1313 "A request to for the displayor to focus on some tag option."
1314 (ding))
1315
1316 (defmethod semantic-displayor-scroll-request ((obj semantic-displayor-abstract))
1317 "A request to for the displayor to scroll the completion list (if needed)."
1318 (scroll-other-window))
1319
1320 (defmethod semantic-displayor-focus-previous ((obj semantic-displayor-abstract))
1321 "Set the current focus to the previous item."
1322 nil)
1323
1324 (defmethod semantic-displayor-focus-next ((obj semantic-displayor-abstract))
1325 "Set the current focus to the next item."
1326 nil)
1327
1328 (defmethod semantic-displayor-current-focus ((obj semantic-displayor-abstract))
1329 "Return a single tag currently in focus.
1330 This object type doesn't do focus, so will never have a focus object."
1331 nil)
1332
1333 ;; Traditional displayor
1334 (defcustom semantic-completion-displayor-format-tag-function
1335 #'semantic-format-tag-name
1336 "*A Tag format function to use when showing completions."
1337 :group 'semantic
1338 :type semantic-format-tag-custom-list)
1339
1340 (defclass semantic-displayor-traditional (semantic-displayor-abstract)
1341 ()
1342 "Display options in *Completions* buffer.
1343 Traditional display mechanism for a list of possible completions.
1344 Completions are showin in a new buffer and listed with the ability
1345 to click on the items to aid in completion.")
1346
1347 (defmethod semantic-displayor-show-request ((obj semantic-displayor-traditional))
1348 "A request to show the current tags table."
1349
1350 ;; NOTE TO SELF. Find the character to type next, and emphesize it.
1351
1352 (with-output-to-temp-buffer "*Completions*"
1353 (display-completion-list
1354 (mapcar semantic-completion-displayor-format-tag-function
1355 (semanticdb-strip-find-results (oref obj table))))
1356 )
1357 )
1358
1359 ;;; Abstract baseclass for any displayor which supports focus
1360 (defclass semantic-displayor-focus-abstract (semantic-displayor-abstract)
1361 ((focus :type number
1362 :protection :protected
1363 :documentation "A tag index from `table' which has focus.
1364 Multiple calls to the display function can choose to focus on a
1365 given tag, by highlighting its location.")
1366 (find-file-focus
1367 :allocation :class
1368 :initform nil
1369 :documentation
1370 "Non-nil if focusing requires a tag's buffer be in memory.")
1371 )
1372 "Abstract displayor supporting `focus'.
1373 A displayor which has the ability to focus in on one tag.
1374 Focusing is a way of differentiationg between multiple tags
1375 which have the same name."
1376 :abstract t)
1377
1378 (defmethod semantic-displayor-next-action ((obj semantic-displayor-focus-abstract))
1379 "The next action to take on the minibuffer related to display."
1380 (if (and (slot-boundp obj 'last-prefix)
1381 (string= (oref obj last-prefix) (semantic-completion-text))
1382 (eq last-command this-command))
1383 (if (and
1384 (slot-boundp obj 'focus)
1385 (slot-boundp obj 'table)
1386 (<= (semanticdb-find-result-length (oref obj table))
1387 (1+ (oref obj focus))))
1388 ;; We are at the end of the focus road.
1389 'displayend
1390 ;; Focus on some item.
1391 'focus)
1392 'display))
1393
1394 (defmethod semantic-displayor-set-completions ((obj semantic-displayor-focus-abstract)
1395 table prefix)
1396 "Set the list of tags to be completed over to TABLE."
1397 (call-next-method)
1398 (slot-makeunbound obj 'focus))
1399
1400 (defmethod semantic-displayor-focus-previous ((obj semantic-displayor-focus-abstract))
1401 "Set the current focus to the previous item.
1402 Not meaningful return value."
1403 (when (and (slot-boundp obj 'table) (oref obj table))
1404 (with-slots (table) obj
1405 (if (or (not (slot-boundp obj 'focus))
1406 (<= (oref obj focus) 0))
1407 (oset obj focus (1- (semanticdb-find-result-length table)))
1408 (oset obj focus (1- (oref obj focus)))
1409 )
1410 )))
1411
1412 (defmethod semantic-displayor-focus-next ((obj semantic-displayor-focus-abstract))
1413 "Set the current focus to the next item.
1414 Not meaningful return value."
1415 (when (and (slot-boundp obj 'table) (oref obj table))
1416 (with-slots (table) obj
1417 (if (not (slot-boundp obj 'focus))
1418 (oset obj focus 0)
1419 (oset obj focus (1+ (oref obj focus)))
1420 )
1421 (if (<= (semanticdb-find-result-length table) (oref obj focus))
1422 (oset obj focus 0))
1423 )))
1424
1425 (defmethod semantic-displayor-focus-tag ((obj semantic-displayor-focus-abstract))
1426 "Return the next tag OBJ should focus on."
1427 (when (and (slot-boundp obj 'table) (oref obj table))
1428 (with-slots (table) obj
1429 (semanticdb-find-result-nth table (oref obj focus)))))
1430
1431 (defmethod semantic-displayor-current-focus ((obj semantic-displayor-focus-abstract))
1432 "Return the tag currently in focus, or call parent method."
1433 (if (and (slot-boundp obj 'focus)
1434 (slot-boundp obj 'table)
1435 ;; Only return the current focus IFF the minibuffer reflects
1436 ;; the list this focus was derived from.
1437 (slot-boundp obj 'last-prefix)
1438 (string= (semantic-completion-text) (oref obj last-prefix))
1439 )
1440 ;; We need to focus
1441 (if (oref obj find-file-focus)
1442 (semanticdb-find-result-nth-in-buffer (oref obj table) (oref obj focus))
1443 ;; result-nth returns a cons with car being the tag, and cdr the
1444 ;; database.
1445 (car (semanticdb-find-result-nth (oref obj table) (oref obj focus))))
1446 ;; Do whatever
1447 (call-next-method)))
1448
1449 ;;; Simple displayor which performs traditional display completion,
1450 ;; and also focuses with highlighting.
1451 (defclass semantic-displayor-traditional-with-focus-highlight
1452 (semantic-displayor-focus-abstract semantic-displayor-traditional)
1453 ((find-file-focus :initform t))
1454 "Display completions in *Completions* buffer, with focus highlight.
1455 A traditional displayor which can focus on a tag by showing it.
1456 Same as `semantic-displayor-traditional', but with selection between
1457 multiple tags with the same name done by 'focusing' on the source
1458 location of the different tags to differentiate them.")
1459
1460 (defmethod semantic-displayor-focus-request
1461 ((obj semantic-displayor-traditional-with-focus-highlight))
1462 "Focus in on possible tag completions.
1463 Focus is performed by cycling through the tags and highlighting
1464 one in the source buffer."
1465 (let* ((tablelength (semanticdb-find-result-length (oref obj table)))
1466 (focus (semantic-displayor-focus-tag obj))
1467 ;; Raw tag info.
1468 (rtag (car focus))
1469 (rtable (cdr focus))
1470 ;; Normalize
1471 (nt (semanticdb-normalize-one-tag rtable rtag))
1472 (tag (cdr nt))
1473 (table (car nt))
1474 )
1475 ;; If we fail to normalize, resete.
1476 (when (not tag) (setq table rtable tag rtag))
1477 ;; Do the focus.
1478 (let ((buf (or (semantic-tag-buffer tag)
1479 (and table (semanticdb-get-buffer table)))))
1480 ;; If no buffer is provided, then we can make up a summary buffer.
1481 (when (not buf)
1482 (save-excursion
1483 (set-buffer (get-buffer-create "*Completion Focus*"))
1484 (erase-buffer)
1485 (insert "Focus on tag: \n")
1486 (insert (semantic-format-tag-summarize tag nil t) "\n\n")
1487 (when table
1488 (insert "From table: \n")
1489 (insert (object-name table) "\n\n"))
1490 (when buf
1491 (insert "In buffer: \n\n")
1492 (insert (format "%S" buf)))
1493 (setq buf (current-buffer))))
1494 ;; Show the tag in the buffer.
1495 (if (get-buffer-window buf)
1496 (select-window (get-buffer-window buf))
1497 (switch-to-buffer-other-window buf t)
1498 (select-window (get-buffer-window buf)))
1499 ;; Now do some positioning
1500 (unwind-protect
1501 (if (semantic-tag-with-position-p tag)
1502 ;; Full tag positional information available
1503 (progn
1504 (goto-char (semantic-tag-start tag))
1505 ;; This avoids a dangerous problem if we just loaded a tag
1506 ;; from a file, but the original position was not updated
1507 ;; in the TAG variable we are currently using.
1508 (semantic-momentary-highlight-tag (semantic-current-tag))
1509 ))
1510 (select-window (minibuffer-window)))
1511 ;; Calculate text difference between contents and the focus item.
1512 (let* ((mbc (semantic-completion-text))
1513 (ftn (semantic-tag-name tag))
1514 (diff (substring ftn (length mbc))))
1515 (semantic-completion-message
1516 (format "%s [%d of %d matches]" diff (1+ (oref obj focus)) tablelength)))
1517 )))
1518
1519 \f
1520 ;;; Tooltip completion lister
1521 ;;
1522 ;; Written and contributed by Masatake YAMATO <jet@gyve.org>
1523 ;;
1524 ;; Modified by Eric Ludlam for
1525 ;; * Safe compatibility for tooltip free systems.
1526 ;; * Don't use 'avoid package for tooltip positioning.
1527
1528 (defclass semantic-displayor-tooltip (semantic-displayor-traditional)
1529 ((max-tags :type integer
1530 :initarg :max-tags
1531 :initform 5
1532 :custom integer
1533 :documentation
1534 "Max number of tags displayed on tooltip at once.
1535 If `force-show' is 1, this value is ignored with typing tab or space twice continuously.
1536 if `force-show' is 0, this value is always ignored.")
1537 (force-show :type integer
1538 :initarg :force-show
1539 :initform 1
1540 :custom (choice (const
1541 :tag "Show when double typing"
1542 1)
1543 (const
1544 :tag "Show always"
1545 0)
1546 (const
1547 :tag "Show if the number of tags is less than `max-tags'."
1548 -1))
1549 :documentation
1550 "Control the behavior of the number of tags is greater than `max-tags'.
1551 -1 means tags are never shown.
1552 0 means the tags are always shown.
1553 1 means tags are shown if space or tab is typed twice continuously.")
1554 (typing-count :type integer
1555 :initform 0
1556 :documentation
1557 "Counter holding how many times the user types space or tab continuously before showing tags.")
1558 (shown :type boolean
1559 :initform nil
1560 :documentation
1561 "Flag representing whether tags is shown once or not.")
1562 )
1563 "Display completions options in a tooltip.
1564 Display mechanism using tooltip for a list of possible completions.")
1565
1566 (defmethod initialize-instance :AFTER ((obj semantic-displayor-tooltip) &rest args)
1567 "Make sure we have tooltips required."
1568 (condition-case nil
1569 (require 'tooltip)
1570 (error nil))
1571 )
1572
1573 (defmethod semantic-displayor-show-request ((obj semantic-displayor-tooltip))
1574 "A request to show the current tags table."
1575 (if (or (not (featurep 'tooltip)) (not tooltip-mode))
1576 ;; If we cannot use tooltips, then go to the normal mode with
1577 ;; a traditional completion buffer.
1578 (call-next-method)
1579 (let* ((tablelong (semanticdb-strip-find-results (oref obj table)))
1580 (table (semantic-unique-tag-table-by-name tablelong))
1581 (l (mapcar semantic-completion-displayor-format-tag-function table))
1582 (ll (length l))
1583 (typing-count (oref obj typing-count))
1584 (force-show (oref obj force-show))
1585 (matchtxt (semantic-completion-text))
1586 msg)
1587 (if (or (oref obj shown)
1588 (< ll (oref obj max-tags))
1589 (and (<= 0 force-show)
1590 (< (1- force-show) typing-count)))
1591 (progn
1592 (oset obj typing-count 0)
1593 (oset obj shown t)
1594 (if (eq 1 ll)
1595 ;; We Have only one possible match. There could be two cases.
1596 ;; 1) input text != single match.
1597 ;; --> Show it!
1598 ;; 2) input text == single match.
1599 ;; --> Complain about it, but still show the match.
1600 (if (string= matchtxt (semantic-tag-name (car table)))
1601 (setq msg (concat "[COMPLETE]\n" (car l)))
1602 (setq msg (car l)))
1603 ;; Create the long message.
1604 (setq msg (mapconcat 'identity l "\n"))
1605 ;; If there is nothing, say so!
1606 (if (eq 0 (length msg))
1607 (setq msg "[NO MATCH]")))
1608 (semantic-displayor-tooltip-show msg))
1609 ;; The typing count determines if the user REALLY REALLY
1610 ;; wanted to show that much stuff. Only increment
1611 ;; if the current command is a completion command.
1612 (if (and (stringp (this-command-keys))
1613 (string= (this-command-keys) "\C-i"))
1614 (oset obj typing-count (1+ typing-count)))
1615 ;; At this point, we know we have too many items.
1616 ;; Lets be brave, and truncate l
1617 (setcdr (nthcdr (oref obj max-tags) l) nil)
1618 (setq msg (mapconcat 'identity l "\n"))
1619 (cond
1620 ((= force-show -1)
1621 (semantic-displayor-tooltip-show (concat msg "\n...")))
1622 ((= force-show 1)
1623 (semantic-displayor-tooltip-show (concat msg "\n(TAB for more)")))
1624 )))))
1625
1626 ;;; Compatibility
1627 ;;
1628 (eval-and-compile
1629 (if (fboundp 'window-inside-edges)
1630 ;; Emacs devel.
1631 (defalias 'semantic-displayor-window-edges
1632 'window-inside-edges)
1633 ;; Emacs 21
1634 (defalias 'semantic-displayor-window-edges
1635 'window-edges)
1636 ))
1637
1638 (defun semantic-displayor-point-position ()
1639 "Return the location of POINT as positioned on the selected frame.
1640 Return a cons cell (X . Y)"
1641 (let* ((frame (selected-frame))
1642 (left (frame-parameter frame 'left))
1643 (top (frame-parameter frame 'top))
1644 (point-pix-pos (posn-x-y (posn-at-point)))
1645 (edges (window-inside-pixel-edges (selected-window))))
1646 (cons (+ (car point-pix-pos) (car edges) left)
1647 (+ (cdr point-pix-pos) (cadr edges) top))))
1648
1649
1650 (defun semantic-displayor-tooltip-show (text)
1651 "Display a tooltip with TEXT near cursor."
1652 (let ((point-pix-pos (semantic-displayor-point-position))
1653 (tooltip-frame-parameters
1654 (append tooltip-frame-parameters nil)))
1655 (push
1656 (cons 'left (+ (car point-pix-pos) (frame-char-width)))
1657 tooltip-frame-parameters)
1658 (push
1659 (cons 'top (+ (cdr point-pix-pos) (frame-char-height)))
1660 tooltip-frame-parameters)
1661 (tooltip-show text)))
1662
1663 (defmethod semantic-displayor-scroll-request ((obj semantic-displayor-tooltip))
1664 "A request to for the displayor to scroll the completion list (if needed)."
1665 ;; Do scrolling in the tooltip.
1666 (oset obj max-tags 30)
1667 (semantic-displayor-show-request obj)
1668 )
1669
1670 ;; End code contributed by Masatake YAMATO <jet@gyve.org>
1671
1672 \f
1673 ;;; Ghost Text displayor
1674 ;;
1675 (defclass semantic-displayor-ghost (semantic-displayor-focus-abstract)
1676
1677 ((ghostoverlay :type overlay
1678 :documentation
1679 "The overlay the ghost text is displayed in.")
1680 (first-show :initform t
1681 :documentation
1682 "Non nil if we have not seen our first show request.")
1683 )
1684 "Cycle completions inline with ghost text.
1685 Completion displayor using ghost chars after point for focus options.
1686 Whichever completion is currently in focus will be displayed as ghost
1687 text using overlay options.")
1688
1689 (defmethod semantic-displayor-next-action ((obj semantic-displayor-ghost))
1690 "The next action to take on the inline completion related to display."
1691 (let ((ans (call-next-method))
1692 (table (when (slot-boundp obj 'table)
1693 (oref obj table))))
1694 (if (and (eq ans 'displayend)
1695 table
1696 (= (semanticdb-find-result-length table) 1)
1697 )
1698 nil
1699 ans)))
1700
1701 (defmethod semantic-displayor-cleanup ((obj semantic-displayor-ghost))
1702 "Clean up any mess this displayor may have."
1703 (when (slot-boundp obj 'ghostoverlay)
1704 (semantic-overlay-delete (oref obj ghostoverlay)))
1705 )
1706
1707 (defmethod semantic-displayor-set-completions ((obj semantic-displayor-ghost)
1708 table prefix)
1709 "Set the list of tags to be completed over to TABLE."
1710 (call-next-method)
1711
1712 (semantic-displayor-cleanup obj)
1713 )
1714
1715
1716 (defmethod semantic-displayor-show-request ((obj semantic-displayor-ghost))
1717 "A request to show the current tags table."
1718 ; (if (oref obj first-show)
1719 ; (progn
1720 ; (oset obj first-show nil)
1721 (semantic-displayor-focus-next obj)
1722 (semantic-displayor-focus-request obj)
1723 ; )
1724 ;; Only do the traditional thing if the first show request
1725 ;; has been seen. Use the first one to start doing the ghost
1726 ;; text display.
1727 ; (call-next-method)
1728 ; )
1729 )
1730
1731 (defmethod semantic-displayor-focus-request
1732 ((obj semantic-displayor-ghost))
1733 "Focus in on possible tag completions.
1734 Focus is performed by cycling through the tags and showing a possible
1735 completion text in ghost text."
1736 (let* ((tablelength (semanticdb-find-result-length (oref obj table)))
1737 (focus (semantic-displayor-focus-tag obj))
1738 (tag (car focus))
1739 )
1740 (if (not tag)
1741 (semantic-completion-message "No tags to focus on.")
1742 ;; Display the focus completion as ghost text after the current
1743 ;; inline text.
1744 (when (or (not (slot-boundp obj 'ghostoverlay))
1745 (not (semantic-overlay-live-p (oref obj ghostoverlay))))
1746 (oset obj ghostoverlay
1747 (semantic-make-overlay (point) (1+ (point)) (current-buffer) t)))
1748
1749 (let* ((lp (semantic-completion-text))
1750 (os (substring (semantic-tag-name tag) (length lp)))
1751 (ol (oref obj ghostoverlay))
1752 )
1753
1754 (put-text-property 0 (length os) 'face 'region os)
1755
1756 (semantic-overlay-put
1757 ol 'display (concat os (buffer-substring (point) (1+ (point)))))
1758 )
1759 ;; Calculate text difference between contents and the focus item.
1760 (let* ((mbc (semantic-completion-text))
1761 (ftn (concat (semantic-tag-name tag)))
1762 )
1763 (put-text-property (length mbc) (length ftn) 'face
1764 'bold ftn)
1765 (semantic-completion-message
1766 (format "%s [%d of %d matches]" ftn (1+ (oref obj focus)) tablelength)))
1767 )))
1768
1769 \f
1770 ;;; ------------------------------------------------------------
1771 ;;; Specific queries
1772 ;;
1773 (defvar semantic-complete-inline-custom-type
1774 (append '(radio)
1775 (mapcar
1776 (lambda (class)
1777 (let* ((C (intern (car class)))
1778 (doc (documentation-property C 'variable-documentation))
1779 (doc1 (car (split-string doc "\n")))
1780 )
1781 (list 'const
1782 :tag doc1
1783 C)))
1784 (eieio-build-class-alist semantic-displayor-abstract t))
1785 )
1786 "Possible options for inlince completion displayors.
1787 Use this to enable custom editing.")
1788
1789 (defcustom semantic-complete-inline-analyzer-displayor-class
1790 'semantic-displayor-traditional
1791 "*Class for displayor to use with inline completion."
1792 :group 'semantic
1793 :type semantic-complete-inline-custom-type
1794 )
1795
1796 (defun semantic-complete-read-tag-buffer-deep (prompt &optional
1797 default-tag
1798 initial-input
1799 history)
1800 "Ask for a tag by name from the current buffer.
1801 Available tags are from the current buffer, at any level.
1802 Completion options are presented in a traditional way, with highlighting
1803 to resolve same-name collisions.
1804 PROMPT is a string to prompt with.
1805 DEFAULT-TAG is a semantic tag or string to use as the default value.
1806 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.
1807 HISTORY is a symbol representing a variable to store the history in."
1808 (semantic-complete-read-tag-engine
1809 (semantic-collector-buffer-deep prompt :buffer (current-buffer))
1810 (semantic-displayor-traditional-with-focus-highlight "simple")
1811 ;;(semantic-displayor-tooltip "simple")
1812 prompt
1813 default-tag
1814 initial-input
1815 history)
1816 )
1817
1818 (defun semantic-complete-read-tag-project (prompt &optional
1819 default-tag
1820 initial-input
1821 history)
1822 "Ask for a tag by name from the current project.
1823 Available tags are from the current project, at the top level.
1824 Completion options are presented in a traditional way, with highlighting
1825 to resolve same-name collisions.
1826 PROMPT is a string to prompt with.
1827 DEFAULT-TAG is a semantic tag or string to use as the default value.
1828 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.
1829 HISTORY is a symbol representing a variable to store the history in."
1830 (semantic-complete-read-tag-engine
1831 (semantic-collector-project-brutish prompt
1832 :buffer (current-buffer)
1833 :path (current-buffer)
1834 )
1835 (semantic-displayor-traditional-with-focus-highlight "simple")
1836 prompt
1837 default-tag
1838 initial-input
1839 history)
1840 )
1841
1842 (defun semantic-complete-inline-tag-project ()
1843 "Complete a symbol name by name from within the current project.
1844 This is similar to `semantic-complete-read-tag-project', except
1845 that the completion interaction is in the buffer where the context
1846 was calculated from.
1847 Customize `semantic-complete-inline-analyzer-displayor-class'
1848 to control how completion options are displayed.
1849 See `semantic-complete-inline-tag-engine' for details on how
1850 completion works."
1851 (let* ((collector (semantic-collector-project-brutish
1852 "inline"
1853 :buffer (current-buffer)
1854 :path (current-buffer)))
1855 (sbounds (semantic-ctxt-current-symbol-and-bounds))
1856 (syms (car sbounds))
1857 (start (car (nth 2 sbounds)))
1858 (end (cdr (nth 2 sbounds)))
1859 (rsym (reverse syms))
1860 (thissym (nth 1 sbounds))
1861 (nextsym (car-safe (cdr rsym)))
1862 (complst nil))
1863 (when (and thissym (or (not (string= thissym ""))
1864 nextsym))
1865 ;; Do a quick calcuation of completions.
1866 (semantic-collector-calculate-completions
1867 collector thissym nil)
1868 ;; Get the master list
1869 (setq complst (semanticdb-strip-find-results
1870 (semantic-collector-all-completions collector thissym)))
1871 ;; Shorten by name
1872 (setq complst (semantic-unique-tag-table-by-name complst))
1873 (if (or (and (= (length complst) 1)
1874 ;; Check to see if it is the same as what is there.
1875 ;; if so, we can offer to complete.
1876 (let ((compname (semantic-tag-name (car complst))))
1877 (not (string= compname thissym))))
1878 (> (length complst) 1))
1879 ;; There are several options. Do the completion.
1880 (semantic-complete-inline-tag-engine
1881 collector
1882 (funcall semantic-complete-inline-analyzer-displayor-class
1883 "inline displayor")
1884 ;;(semantic-displayor-tooltip "simple")
1885 (current-buffer)
1886 start end))
1887 )))
1888
1889 (defun semantic-complete-read-tag-analyzer (prompt &optional
1890 context
1891 history)
1892 "Ask for a tag by name based on the current context.
1893 The function `semantic-analyze-current-context' is used to
1894 calculate the context. `semantic-analyze-possible-completions' is used
1895 to generate the list of possible completions.
1896 PROMPT is the first part of the prompt. Additional prompt
1897 is added based on the contexts full prefix.
1898 CONTEXT is the semantic analyzer context to start with.
1899 HISTORY is a symbol representing a variable to stor the history in.
1900 usually a default-tag and initial-input are available for completion
1901 prompts. these are calculated from the CONTEXT variable passed in."
1902 (if (not context) (setq context (semantic-analyze-current-context (point))))
1903 (let* ((syms (semantic-ctxt-current-symbol (point)))
1904 (inp (car (reverse syms))))
1905 (setq syms (nreverse (cdr (nreverse syms))))
1906 (semantic-complete-read-tag-engine
1907 (semantic-collector-analyze-completions
1908 prompt
1909 :buffer (oref context buffer)
1910 :context context)
1911 (semantic-displayor-traditional-with-focus-highlight "simple")
1912 (save-excursion
1913 (set-buffer (oref context buffer))
1914 (goto-char (cdr (oref context bounds)))
1915 (concat prompt (mapconcat 'identity syms ".")
1916 (if syms "." "")
1917 ))
1918 nil
1919 inp
1920 history)))
1921
1922 (defun semantic-complete-inline-analyzer (context)
1923 "Complete a symbol name by name based on the current context.
1924 This is similar to `semantic-complete-read-tag-analyze', except
1925 that the completion interaction is in the buffer where the context
1926 was calculated from.
1927 CONTEXT is the semantic analyzer context to start with.
1928 Customize `semantic-complete-inline-analyzer-displayor-class'
1929 to control how completion options are displayed.
1930
1931 See `semantic-complete-inline-tag-engine' for details on how
1932 completion works."
1933 (if (not context) (setq context (semantic-analyze-current-context (point))))
1934 (if (not context) (error "Nothing to complete on here"))
1935 (let* ((collector (semantic-collector-analyze-completions
1936 "inline"
1937 :buffer (oref context buffer)
1938 :context context))
1939 (syms (semantic-ctxt-current-symbol (point)))
1940 (rsym (reverse syms))
1941 (thissym (car rsym))
1942 (nextsym (car-safe (cdr rsym)))
1943 (complst nil))
1944 (when (and thissym (or (not (string= thissym ""))
1945 nextsym))
1946 ;; Do a quick calcuation of completions.
1947 (semantic-collector-calculate-completions
1948 collector thissym nil)
1949 ;; Get the master list
1950 (setq complst (semanticdb-strip-find-results
1951 (semantic-collector-all-completions collector thissym)))
1952 ;; Shorten by name
1953 (setq complst (semantic-unique-tag-table-by-name complst))
1954 (if (or (and (= (length complst) 1)
1955 ;; Check to see if it is the same as what is there.
1956 ;; if so, we can offer to complete.
1957 (let ((compname (semantic-tag-name (car complst))))
1958 (not (string= compname thissym))))
1959 (> (length complst) 1))
1960 ;; There are several options. Do the completion.
1961 (semantic-complete-inline-tag-engine
1962 collector
1963 (funcall semantic-complete-inline-analyzer-displayor-class
1964 "inline displayor")
1965 ;;(semantic-displayor-tooltip "simple")
1966 (oref context buffer)
1967 (car (oref context bounds))
1968 (cdr (oref context bounds))
1969 ))
1970 )))
1971
1972 (defcustom semantic-complete-inline-analyzer-idle-displayor-class
1973 'semantic-displayor-ghost
1974 "*Class for displayor to use with inline completion at idle time."
1975 :group 'semantic
1976 :type semantic-complete-inline-custom-type
1977 )
1978
1979 (defun semantic-complete-inline-analyzer-idle (context)
1980 "Complete a symbol name by name based on the current context for idle time.
1981 CONTEXT is the semantic analyzer context to start with.
1982 This function is used from `semantic-idle-completions-mode'.
1983
1984 This is the same as `semantic-complete-inline-analyzer', except that
1985 it uses `semantic-complete-inline-analyzer-idle-displayor-class'
1986 to control how completions are displayed.
1987
1988 See `semantic-complete-inline-tag-engine' for details on how
1989 completion works."
1990 (let ((semantic-complete-inline-analyzer-displayor-class
1991 semantic-complete-inline-analyzer-idle-displayor-class))
1992 (semantic-complete-inline-analyzer context)
1993 ))
1994
1995 \f
1996 ;;; ------------------------------------------------------------
1997 ;;; Testing/Samples
1998 ;;
1999 (defun semantic-complete-test ()
2000 "Test completion mechanisms."
2001 (interactive)
2002 (message "%S"
2003 (semantic-format-tag-prototype
2004 (semantic-complete-read-tag-project "Symbol: ")
2005 )))
2006
2007 ;;;###autoload
2008 (defun semantic-complete-jump-local ()
2009 "Jump to a semantic symbol."
2010 (interactive)
2011 (let ((tag (semantic-complete-read-tag-buffer-deep "Symbol: ")))
2012 (when (semantic-tag-p tag)
2013 (push-mark)
2014 (goto-char (semantic-tag-start tag))
2015 (semantic-momentary-highlight-tag tag)
2016 (message "%S: %s "
2017 (semantic-tag-class tag)
2018 (semantic-tag-name tag)))))
2019
2020 ;;;###autoload
2021 (defun semantic-complete-jump ()
2022 "Jump to a semantic symbol."
2023 (interactive)
2024 (let* ((tag (semantic-complete-read-tag-project "Symbol: ")))
2025 (when (semantic-tag-p tag)
2026 (push-mark)
2027 (semantic-go-to-tag tag)
2028 (switch-to-buffer (current-buffer))
2029 (semantic-momentary-highlight-tag tag)
2030 (message "%S: %s "
2031 (semantic-tag-class tag)
2032 (semantic-tag-name tag)))))
2033
2034 ;;;###autoload
2035 (defun semantic-complete-analyze-and-replace ()
2036 "Perform prompt completion to do in buffer completion.
2037 `semantic-analyze-possible-completions' is used to determine the
2038 possible values.
2039 The minibuffer is used to perform the completion.
2040 The result is inserted as a replacement of the text that was there."
2041 (interactive)
2042 (let* ((c (semantic-analyze-current-context (point)))
2043 (tag (save-excursion (semantic-complete-read-tag-analyzer "" c))))
2044 ;; Take tag, and replace context bound with its name.
2045 (goto-char (car (oref c bounds)))
2046 (delete-region (point) (cdr (oref c bounds)))
2047 (insert (semantic-tag-name tag))
2048 (message "%S" (semantic-format-tag-summarize tag))))
2049
2050 ;;;###autoload
2051 (defun semantic-complete-analyze-inline ()
2052 "Perform prompt completion to do in buffer completion.
2053 `semantic-analyze-possible-completions' is used to determine the
2054 possible values.
2055 The function returns immediately, leaving the buffer in a mode that
2056 will perform the completion.
2057 Configure `semantic-complete-inline-analyzer-displayor-class' to change
2058 how completion options are displayed."
2059 (interactive)
2060 ;; Only do this if we are not already completing something.
2061 (if (not (semantic-completion-inline-active-p))
2062 (semantic-complete-inline-analyzer
2063 (semantic-analyze-current-context (point))))
2064 ;; Report a message if things didn't startup.
2065 (if (and (interactive-p)
2066 (not (semantic-completion-inline-active-p)))
2067 (message "Inline completion not needed.")
2068 ;; Since this is most likely bound to something, and not used
2069 ;; at idle time, throw in a TAB for good measure.
2070 (semantic-complete-inline-TAB)
2071 ))
2072
2073 ;;;###autoload
2074 (defun semantic-complete-analyze-inline-idle ()
2075 "Perform prompt completion to do in buffer completion.
2076 `semantic-analyze-possible-completions' is used to determine the
2077 possible values.
2078 The function returns immediately, leaving the buffer in a mode that
2079 will perform the completion.
2080 Configure `semantic-complete-inline-analyzer-idle-displayor-class'
2081 to change how completion options are displayed."
2082 (interactive)
2083 ;; Only do this if we are not already completing something.
2084 (if (not (semantic-completion-inline-active-p))
2085 (semantic-complete-inline-analyzer-idle
2086 (semantic-analyze-current-context (point))))
2087 ;; Report a message if things didn't startup.
2088 (if (and (interactive-p)
2089 (not (semantic-completion-inline-active-p)))
2090 (message "Inline completion not needed."))
2091 )
2092
2093 ;;;###autoload
2094 (defun semantic-complete-self-insert (arg)
2095 "Like `self-insert-command', but does completion afterwards.
2096 ARG is passed to `self-insert-command'. If ARG is nil,
2097 use `semantic-complete-analyze-inline' to complete."
2098 (interactive "p")
2099 ;; If we are already in a completion scenario, exit now, and then start over.
2100 (semantic-complete-inline-exit)
2101
2102 ;; Insert the key
2103 (self-insert-command arg)
2104
2105 ;; Prepare for doing completion, but exit quickly if there is keyboard
2106 ;; input.
2107 (when (and (not (semantic-exit-on-input 'csi
2108 (semantic-fetch-tags)
2109 (semantic-throw-on-input 'csi)
2110 nil))
2111 (= arg 1)
2112 (not (semantic-exit-on-input 'csi
2113 (semantic-analyze-current-context)
2114 (semantic-throw-on-input 'csi)
2115 nil)))
2116 (condition-case nil
2117 (semantic-complete-analyze-inline)
2118 ;; Ignore errors. Seems likely that we'll get some once in a while.
2119 (error nil))
2120 ))
2121
2122 ;; @TODO - I can't find where this fcn is used. Delete?
2123
2124 ;;;;###autoload
2125 ;(defun semantic-complete-inline-project ()
2126 ; "Perform inline completion for any symbol in the current project.
2127 ;`semantic-analyze-possible-completions' is used to determine the
2128 ;possible values.
2129 ;The function returns immediately, leaving the buffer in a mode that
2130 ;will perform the completion."
2131 ; (interactive)
2132 ; ;; Only do this if we are not already completing something.
2133 ; (if (not (semantic-completion-inline-active-p))
2134 ; (semantic-complete-inline-tag-project))
2135 ; ;; Report a message if things didn't startup.
2136 ; (if (and (interactive-p)
2137 ; (not (semantic-completion-inline-active-p)))
2138 ; (message "Inline completion not needed."))
2139 ; )
2140
2141 ;; End
2142 (provide 'semantic/complete)
2143
2144 ;; Local variables:
2145 ;; generated-autoload-file: "loaddefs.el"
2146 ;; generated-autoload-feature: semantic/loaddefs
2147 ;; generated-autoload-load-name: "semantic/complete"
2148 ;; End:
2149
2150 ;;; semantic/complete.el ends here