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