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