1 ;;; semantic/complete.el --- Routines for performing tag completion
3 ;; Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009
4 ;; Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
26 ;; Completion of tags by name using tables of semantic generated tags.
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.
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".
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.
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.
49 ;; There are several parts of any completion engine. They are:
51 ;; A. Collection of possible hits
52 ;; B. Typing or selecting an option
53 ;; C. Displaying possible unique completions
54 ;; D. Using the result
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.
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.
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.
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.
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.
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.
92 ;; The format of the tag lists used to perform the completions are in
93 ;; semanticdb "find" format, like this:
95 ;; ( ( DBTABLE1 TAG1 TAG2 ...)
96 ;; ( DBTABLE2 TAG1 TAG2 ...)
99 ;; INLINE vs MINIBUFFER
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.
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
112 (require 'semantic
/analyze
)
113 (require 'semantic
/ctxt
)
114 (require 'semantic
/decorate
)
115 (require 'semantic
/format
)
118 ;; For the semantic-find-tags-for-completion macro.
119 (require 'semantic
/find
))
123 (defvar semantic-complete-inline-overlay nil
124 "The overlay currently active while completing inline.")
126 (defun semantic-completion-inline-active-p ()
127 "Non-nil if inline completion is active."
128 (when (and semantic-complete-inline-overlay
129 (not (semantic-overlay-live-p semantic-complete-inline-overlay
)))
130 (semantic-overlay-delete semantic-complete-inline-overlay
)
131 (setq semantic-complete-inline-overlay nil
))
132 semantic-complete-inline-overlay
)
134 ;;; ------------------------------------------------------------
135 ;;; MINIBUFFER or INLINE utils
137 (defun semantic-completion-text ()
138 "Return the text that is currently in the completion buffer.
139 For a minibuffer prompt, this is the minibuffer text.
140 For inline completion, this is the text wrapped in the inline completion
142 (if semantic-complete-inline-overlay
143 (semantic-complete-inline-text)
144 (minibuffer-contents)))
146 (defun semantic-completion-delete-text ()
147 "Delete the text that is actively being completed.
148 Presumably if you call this you will insert something new there."
149 (if semantic-complete-inline-overlay
150 (semantic-complete-inline-delete-text)
151 (delete-minibuffer-contents)))
153 (defun semantic-completion-message (fmt &rest args
)
154 "Display the string FMT formatted with ARGS at the end of the minibuffer."
155 (if semantic-complete-inline-overlay
156 (apply 'message fmt args
)
157 (message (concat (buffer-string) (apply 'format fmt args
)))))
159 ;;; ------------------------------------------------------------
160 ;;; MINIBUFFER: Option Selection harnesses
162 (defvar semantic-completion-collector-engine nil
163 "The tag collector for the current completion operation.
164 Value should be an object of a subclass of
165 `semantic-completion-engine-abstract'.")
167 (defvar semantic-completion-display-engine nil
168 "The tag display engine for the current completion operation.
169 Value should be a ... what?")
171 (defvar semantic-complete-key-map
172 (let ((km (make-sparse-keymap)))
173 (define-key km
" " 'semantic-complete-complete-space
)
174 (define-key km
"\t" 'semantic-complete-complete-tab
)
175 (define-key km
"\C-m" 'semantic-complete-done
)
176 (define-key km
"\C-g" 'abort-recursive-edit
)
177 (define-key km
"\M-n" 'next-history-element
)
178 (define-key km
"\M-p" 'previous-history-element
)
179 (define-key km
"\C-n" 'next-history-element
)
180 (define-key km
"\C-p" 'previous-history-element
)
181 ;; Add history navigation
183 "Keymap used while completing across a list of tags.")
185 (defvar semantic-completion-default-history nil
186 "Default history variable for any unhistoried prompt.
187 Keeps STRINGS only in the history.")
190 (defun semantic-complete-read-tag-engine (collector displayor prompt
191 default-tag initial-input
193 "Read a semantic tag, and return a tag for the selection.
194 Argument COLLECTOR is an object which can be used to to calculate
195 a list of possible hits. See `semantic-completion-collector-engine'
196 for details on COLLECTOR.
197 Argument DISPLAYOR is an object used to display a list of possible
198 completions for a given prefix. See`semantic-completion-display-engine'
199 for details on DISPLAYOR.
200 PROMPT is a string to prompt with.
201 DEFAULT-TAG is a semantic tag or string to use as the default value.
202 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.
203 HISTORY is a symbol representing a variable to story the history in."
204 (let* ((semantic-completion-collector-engine collector
)
205 (semantic-completion-display-engine displayor
)
206 (semantic-complete-active-default nil
)
207 (semantic-complete-current-matched-tag nil
)
208 (default-as-tag (semantic-complete-default-to-tag default-tag
))
209 (default-as-string (when (semantic-tag-p default-as-tag
)
210 (semantic-tag-name default-as-tag
)))
213 (when default-as-string
214 ;; Add this to the prompt.
216 ;; I really want to add a lookup of the symbol in those
217 ;; tags available to the collector and only add it if it
218 ;; is available as a possibility, but I'm too lazy right
222 ;; @todo - move from () to into the editable area
223 (if (string-match ":" prompt
)
225 (substring prompt
0 (match-beginning 0))
226 " (" default-as-string
")"
227 (substring prompt
(match-beginning 0))))
228 (setq prompt
(concat prompt
" (" default-as-string
"): "))))
230 ;; Perform the Completion
233 (read-from-minibuffer prompt
235 semantic-complete-key-map
238 'semantic-completion-default-history
)
240 (semantic-collector-cleanup semantic-completion-collector-engine
)
241 (semantic-displayor-cleanup semantic-completion-display-engine
)
244 ;; Extract the tag from the completion machinery.
246 semantic-complete-current-matched-tag
250 ;;; Util for basic completion prompts
253 (defvar semantic-complete-active-default nil
254 "The current default tag calculated for this prompt.")
256 (defun semantic-complete-default-to-tag (default)
257 "Convert a calculated or passed in DEFAULT into a tag."
258 (if (semantic-tag-p default
)
259 ;; Just return what was passed in.
260 (setq semantic-complete-active-default default
)
261 ;; If none was passed in, guess.
263 (setq default
(semantic-ctxt-current-thing)))
267 ;; Turn default into something useful.
270 ;; Semantic-ctxt-current-symbol will return a list of
271 ;; strings. Technically, we should use the analyzer to
272 ;; fully extract what we need, but for now, just grab the
274 ((and (listp default
) (stringp (car default
)))
279 (symbol-name default
))
281 (signal 'wrong-type-argument
282 (list default
'semantic-tag-p
)))))
284 ;; Now that we have that symbol string, look it up using the active
285 ;; collector. If we get a match, use it.
287 (semantic-collector-calculate-completions
288 semantic-completion-collector-engine
290 ;; Do we have the perfect match???
291 (let ((ml (semantic-collector-current-exact-match
292 semantic-completion-collector-engine
)))
294 ;; We don't care about uniqueness. Just guess for convenience
295 (setq tag
(semanticdb-find-result-nth-in-buffer ml
0))))
297 (setq semantic-complete-active-default tag
)
298 ;; Return it.. .whatever it may be
302 ;;; Prompt Return Value
304 ;; Getting a return value out of this completion prompt is a bit
305 ;; challenging. The read command returns the string typed in.
306 ;; We need to convert this into a valid tag. We can exit the minibuffer
307 ;; for different reasons. If we purposely exit, we must make sure
308 ;; the focused tag is calculated... preferably once.
309 (defvar semantic-complete-current-matched-tag nil
310 "Variable used to pass the tags being matched to the prompt.")
312 ;; semantic-displayor-focus-abstract-child-p is part of the
313 ;; semantic-displayor-focus-abstract class, defined later in this
315 (declare-function semantic-displayor-focus-abstract-child-p
"semantic/complete"
318 (defun semantic-complete-current-match ()
319 "Calculate a match from the current completion environment.
320 Save this in our completion variable. Make sure that variable
321 is cleared if any other keypress is made.
323 tag - a single tag that has been matched.
324 string - a message to show in the minibuffer."
325 ;; Query the environment for an active completion.
326 (let ((collector semantic-completion-collector-engine
)
327 (displayor semantic-completion-display-engine
)
328 (contents (semantic-completion-text))
331 (if (string= contents
"")
332 ;; The user wants the defaults!
333 (setq answer semantic-complete-active-default
)
334 ;; This forces a full calculation of completion on CR.
336 (semantic-collector-calculate-completions collector contents nil
))
337 (semantic-complete-try-completion)
339 ;; Input match displayor focus entry
340 ((setq answer
(semantic-displayor-current-focus displayor
))
341 ;; We have answer, continue
343 ;; One match from the collector
344 ((setq matchlist
(semantic-collector-current-exact-match collector
))
345 (if (= (semanticdb-find-result-length matchlist
) 1)
346 (setq answer
(semanticdb-find-result-nth-in-buffer matchlist
0))
347 (if (semantic-displayor-focus-abstract-child-p displayor
)
348 ;; For focusing displayors, we can claim this is
349 ;; not unique. Multiple focuses can choose the correct
351 (setq answer
"Not Unique")
352 ;; If we don't have a focusing displayor, we need to do something
353 ;; graceful. First, see if all the matches have the same name.
355 (firstname (semantic-tag-name
357 (semanticdb-find-result-nth matchlist
0)))
360 (max (semanticdb-find-result-length matchlist
)))
361 (while (and allsame
(< cnt max
))
366 (semanticdb-find-result-nth matchlist cnt
)))))
370 ;; Now we know if they are all the same. If they are, just
371 ;; accept the first, otherwise complain.
373 (setq answer
(semanticdb-find-result-nth-in-buffer
375 (setq answer
"Not Unique"))
379 (setq answer
"No Match")))
381 ;; Set it into our completion target.
382 (when (semantic-tag-p answer
)
383 (setq semantic-complete-current-matched-tag answer
)
384 ;; Make sure it is up to date by clearing it if the user dares
385 ;; to touch the keyboard.
386 (add-hook 'pre-command-hook
387 (lambda () (setq semantic-complete-current-matched-tag nil
)))
396 ;; Keys are bound to to perform completion using our mechanisms.
397 ;; Do that work here.
398 (defun semantic-complete-done ()
399 "Accept the current input."
401 (let ((ans (semantic-complete-current-match)))
403 (semantic-completion-message (concat " [" ans
"]"))
407 (defun semantic-complete-complete-space ()
408 "Complete the partial input in the minibuffer."
410 (semantic-complete-do-completion t
))
412 (defun semantic-complete-complete-tab ()
413 "Complete the partial input in the minibuffer as far as possible."
415 (semantic-complete-do-completion))
417 ;;; Completion Functions
419 ;; Thees routines are functional entry points to performing completion.
421 (defun semantic-complete-hack-word-boundaries (original new
)
422 "Return a string to use for completion.
423 ORIGINAL is the text in the minibuffer.
424 NEW is the new text to insert into the minibuffer.
425 Within the difference bounds of ORIGINAL and NEW, shorten NEW
426 to the nearest word boundary, and return that."
428 (let* ((diff (substring new
(length original
)))
429 (end (string-match "\\>" diff
))
430 (start (string-match "\\<" diff
)))
432 ((and start
(> start
0))
433 ;; If start is greater than 0, include only the new
435 (concat original
(substring diff
0 start
)))
437 (concat original
(substring diff
0 end
)))
440 (defun semantic-complete-try-completion (&optional partial
)
441 "Try a completion for the current minibuffer.
442 If PARTIAL, do partial completion stopping at spaces."
443 (let ((comp (semantic-collector-try-completion
444 semantic-completion-collector-engine
445 (semantic-completion-text))))
448 (semantic-completion-message " [No Match]")
452 (if (string= (semantic-completion-text) comp
)
454 ;; Minibuffer isn't changing AND the text is not unique.
455 ;; Test for partial completion over a word separator character.
456 ;; If there is one available, use that so that SPC can
457 ;; act like a SPC insert key.
458 (let ((newcomp (semantic-collector-current-whitespace-completion
459 semantic-completion-collector-engine
)))
461 (semantic-completion-delete-text)
465 (let ((orig (semantic-completion-text)))
466 ;; For partial completion, we stop and step over
467 ;; word boundaries. Use this nifty function to do
468 ;; that calculation for us.
470 (semantic-complete-hack-word-boundaries orig comp
))))
471 ;; Do the replacement.
472 (semantic-completion-delete-text)
475 ((and (listp comp
) (semantic-tag-p (car comp
)))
476 (unless (string= (semantic-completion-text)
477 (semantic-tag-name (car comp
)))
478 ;; A fully unique completion was available.
479 (semantic-completion-delete-text)
480 (insert (semantic-tag-name (car comp
))))
481 ;; The match is complete
482 (if (= (length comp
) 1)
483 (semantic-completion-message " [Complete]")
484 (semantic-completion-message " [Complete, but not unique]"))
488 (defun semantic-complete-do-completion (&optional partial inline
)
489 "Do a completion for the current minibuffer.
490 If PARTIAL, do partial completion stopping at spaces.
491 if INLINE, then completion is happening inline in a buffer."
492 (let* ((collector semantic-completion-collector-engine
)
493 (displayor semantic-completion-display-engine
)
494 (contents (semantic-completion-text))
498 (semantic-collector-calculate-completions collector contents partial
))
499 (let* ((na (semantic-complete-next-action partial
)))
501 ;; We're all done, but only from a very specific
502 ;; area of completion.
504 (semantic-completion-message " [Complete]")
506 ;; Perform completion
507 ((or (eq na
'complete
)
508 (eq na
'complete-whitespace
))
509 (semantic-complete-try-completion partial
)
510 (setq ans
'complete
))
511 ;; We need to display the completions.
512 ;; Set the completions into the display engine
513 ((or (eq na
'display
) (eq na
'displayend
))
514 (semantic-displayor-set-completions
517 (and (not (eq na
'displayend
))
518 (semantic-collector-current-exact-match collector
))
519 (semantic-collector-all-completions collector contents
))
521 ;; Ask the displayor to display them.
522 (semantic-displayor-show-request displayor
))
524 (semantic-displayor-scroll-request displayor
)
527 (semantic-displayor-focus-next displayor
)
528 (semantic-displayor-focus-request displayor
)
531 (semantic-completion-message " [No Match]"))
536 ;;; ------------------------------------------------------------
537 ;;; INLINE: tag completion harness
539 ;; Unlike the minibuffer, there is no mode nor other traditional
540 ;; means of reading user commands in completion mode. Instead
541 ;; we use a pre-command-hook to inset in our commands, and to
542 ;; push ourselves out of this mode on alternate keypresses.
543 (defvar semantic-complete-inline-map
544 (let ((km (make-sparse-keymap)))
545 (define-key km
"\C-i" 'semantic-complete-inline-TAB
)
546 (define-key km
"\M-p" 'semantic-complete-inline-up
)
547 (define-key km
"\M-n" 'semantic-complete-inline-down
)
548 (define-key km
"\C-m" 'semantic-complete-inline-done
)
549 (define-key km
"\C-\M-c" 'semantic-complete-inline-exit
)
550 (define-key km
"\C-g" 'semantic-complete-inline-quit
)
552 (lambda () (interactive)
553 (describe-variable 'semantic-complete-inline-map
)))
555 "Keymap used while performing Semantic inline completion.
556 \\{semantic-complete-inline-map}")
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
)
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
)))
574 (buffer-substring-no-properties s e
))))
576 (defun semantic-complete-inline-delete-text ()
577 "Delete the text currently being completed in the current buffer."
579 (semantic-overlay-start semantic-complete-inline-overlay
)
580 (semantic-overlay-end semantic-complete-inline-overlay
)))
582 (defun semantic-complete-inline-done ()
583 "This completion thing is DONE, OR, insert a newline."
585 (let* ((displayor semantic-completion-display-engine
)
586 (tag (semantic-displayor-current-focus displayor
)))
588 (let ((txt (semantic-completion-text)))
589 (insert (substring (semantic-tag-name tag
)
591 (semantic-complete-inline-exit))
593 ;; Get whatever binding RET usually has.
596 (lookup-key (current-active-maps) (this-command-keys))
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))
606 (defun semantic-complete-inline-quit ()
607 "Quit an inline edit."
609 (semantic-complete-inline-exit)
612 (defun semantic-complete-inline-exit ()
613 "Exit inline completion mode."
615 ;; Remove this hook FIRST!
616 (remove-hook 'pre-command-hook
'semantic-complete-pre-command-hook
)
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
))
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
))
630 (semantic-overlay-delete semantic-complete-inline-overlay
)
631 (setq semantic-complete-inline-overlay nil
)
632 ;; DONT restore the window configuration if we just
634 (when (eq buf
(current-buffer))
635 (set-window-configuration wc
))
638 (setq semantic-completion-collector-engine nil
639 semantic-completion-display-engine nil
))
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
)
647 ;;(message "Exiting inline completion.")
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
))
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."
670 ;; Exit if something bad happened.
671 (if (not semantic-complete-inline-overlay
)
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
))
682 ;; EXIT when we are no longer in a good place.
683 ((or (not (eq b
(current-buffer)))
686 ;;(message "Exit: %S %S %S" s e (point))
687 (semantic-complete-inline-exit)
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
""))
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,
706 ;; Else, show completions now
707 (semantic-complete-inline-force-display)
710 ;; If something goes terribly wrong, clean up after ourselves.
711 (error (semantic-complete-inline-exit))))
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."
718 (let ((collector semantic-completion-collector-engine
)
719 (displayor semantic-completion-display-engine
)
720 (contents (semantic-completion-text)))
722 (semantic-collector-calculate-completions
723 collector contents nil
)
724 (semantic-displayor-set-completions
726 (semantic-collector-all-completions collector contents
)
728 ;; Ask the displayor to display them.
729 (semantic-displayor-show-request displayor
))
731 (error (message "Bug Showing Completions: %S" e
))))
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
)
751 (setq semantic-complete-inline-overlay
752 (semantic-make-overlay start end buffer nil t
))
753 (semantic-overlay-put semantic-complete-inline-overlay
755 'semantic-complete-inline-face
)
756 (semantic-overlay-put semantic-complete-inline-overlay
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
)
763 (semantic-complete-inline-force-display)
766 ;;; Inline Completion Keymap Functions
768 (defun semantic-complete-inline-TAB ()
769 "Perform inline completion."
771 (let ((cmpl (semantic-complete-do-completion nil t
)))
774 (semantic-complete-inline-force-display))
776 (semantic-complete-inline-done))
780 (defun semantic-complete-inline-down()
781 "Focus forwards through the displayor."
783 (let ((displayor semantic-completion-display-engine
))
784 (semantic-displayor-focus-next displayor
)
785 (semantic-displayor-focus-request displayor
)
788 (defun semantic-complete-inline-up ()
789 "Focus backwards through the displayor."
791 (let ((displayor semantic-completion-display-engine
))
792 (semantic-displayor-focus-previous displayor
)
793 (semantic-displayor-focus-request displayor
)
797 ;;; ------------------------------------------------------------
798 ;;; Interactions between collection and displaying
800 ;; Functional routines used to help collectors communicate with
801 ;; the current displayor, or for the previous section.
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.
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
824 (ans2 (semantic-displayor-next-action
825 semantic-completion-display-engine
))
828 ;; No collector answer, use displayor answer.
831 ;; Displayor selection of 'scroll, 'display, or 'focus trumps
833 ((and (eq ans1
'done
) ans2
)
835 ;; Use ans1 when we have it.
841 ;;; ------------------------------------------------------------
842 ;;; Collection Engines
844 ;; Collection engines can scan tags from the current environment and
845 ;; provide lists of possible completions.
847 ;; General features of the abstract collector:
848 ;; * Cache completion lists between uses
849 ;; * Cache itself per buffer. Handle reparse hooks
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
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
)
861 (defvar semantic-collector-list nil
862 "List of global collectors active this session.")
864 (defclass semantic-collector-abstract
()
865 ((buffer :initarg
:buffer
867 :documentation
"Originating buffer for this collector.
868 Some collectors use a given buffer as a starting place while looking up
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.")
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
903 (defmethod semantic-collector-cleanup ((obj semantic-collector-abstract
))
904 "Clean up any mess this collector may have."
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)
920 (eq last-command this-command
))
921 ;; Defer to the displayor...
923 ((and cem
(= cemlen
1))
925 ((and (not cem
) (not cac
))
927 ((and partial
(semantic-collector-try-completion-whitespace
928 obj
(semantic-completion-text)))
929 'complete-whitespace
)))
932 (defmethod semantic-collector-last-prefix= ((obj semantic-collector-abstract
)
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
)))
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."
942 (semantic-collector-calculate-cache obj
)))
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 (save-excursion
950 (set-buffer (oref obj buffer
))
951 semanticdb-current-table
))
952 (result (semantic-find-tags-for-completion
954 ;; To do this kind of search with a pre-built completion
955 ;; list, we need to strip it first.
956 (semanticdb-strip-find-results completionlist
)))
959 (list (cons table result
)))))
961 (defmethod semantic-collector-calculate-completions
962 ((obj semantic-collector-abstract
) prefix partial
)
963 "Calculate completions for prefix as setup for other queries."
964 (let* ((case-fold-search semantic-case-fold
)
965 (same-prefix-p (semantic-collector-last-prefix= obj prefix
))
967 (if (or same-prefix-p
968 (and (slot-boundp obj
'last-prefix
)
969 (eq (compare-strings (oref obj last-prefix
) 0 nil
970 prefix
0 (length prefix
))
972 ;; New prefix is subset of old prefix
973 (oref obj last-all-completions
)
974 (semantic-collector-get-cache obj
)))
976 (answer (if same-prefix-p
978 (semantic-collector-calculate-completions-raw
979 obj prefix completionlist
))
982 (complete-not-uniq nil
)
984 ;;(semanticdb-find-result-test answer)
985 (when (not same-prefix-p
)
986 ;; Save results if it is interesting and beneficial
987 (oset obj last-prefix prefix
)
988 (oset obj last-all-completions answer
))
989 ;; Now calculate the completion.
990 (setq completion
(try-completion
992 (semanticdb-strip-find-results answer
)))
993 (oset obj last-whitespace-completion nil
)
994 (oset obj current-exact-match nil
)
995 ;; Only do this if a completion was found. Letting a nil in
996 ;; could cause a full semanticdb search by accident.
998 (oset obj last-completion
1000 ;; Unique match in AC. Last completion is a match.
1001 ;; Also set the current-exact-match.
1003 (oset obj current-exact-match answer
)
1005 ;; It may be complete (a symbol) but still not unique.
1006 ;; We can capture a match
1007 ((setq complete-not-uniq
1008 (semanticdb-find-tags-by-name
1011 (oset obj current-exact-match
1015 ;; Non unique match, return the string that handles
1017 (t (or completion prefix
))
1021 (defmethod semantic-collector-try-completion-whitespace
1022 ((obj semantic-collector-abstract
) prefix
)
1023 "For OBJ, do whatepsace completion based on PREFIX.
1024 This implies that if there are two completions, one matching
1025 the test \"preifx\\>\", and one not, the one matching the full
1026 word version of PREFIX will be chosen, and that text returned.
1027 This function requires that `semantic-collector-calculate-completions'
1028 has been run first."
1029 (let* ((ac (semantic-collector-all-completions obj prefix
))
1030 (matchme (concat "^" prefix
"\\>"))
1031 (compare (semanticdb-find-tags-by-name-regexp matchme ac
))
1032 (numtag (semanticdb-find-result-length compare
))
1036 (cutlen (1+ (length prefix
)))
1037 (twws (semanticdb-find-result-nth compare idx
)))
1038 ;; Is our tag with whitespace a match that has whitespace
1039 ;; after it, or just an already complete symbol?
1040 (while (and (< idx numtag
)
1041 (< (length (semantic-tag-name (car twws
))) cutlen
))
1043 twws
(semanticdb-find-result-nth compare idx
)))
1044 (when (and twws
(car-safe twws
))
1045 ;; If COMPARE has succeeded, then we should take the very
1046 ;; first match, and extend prefix by one character.
1047 (oset obj last-whitespace-completion
1048 (substring (semantic-tag-name (car twws
))
1053 (defmethod semantic-collector-current-exact-match ((obj semantic-collector-abstract
))
1054 "Return the active valid MATCH from the semantic collector.
1055 For now, just return the first element from our list of available
1056 matches. For semanticdb based results, make sure the file is loaded
1058 (when (slot-boundp obj
'current-exact-match
)
1059 (oref obj current-exact-match
)))
1061 (defmethod semantic-collector-current-whitespace-completion ((obj semantic-collector-abstract
))
1062 "Return the active whitespace completion value."
1063 (when (slot-boundp obj
'last-whitespace-completion
)
1064 (oref obj last-whitespace-completion
)))
1066 (defmethod semantic-collector-get-match ((obj semantic-collector-abstract
))
1067 "Return the active valid MATCH from the semantic collector.
1068 For now, just return the first element from our list of available
1069 matches. For semanticdb based results, make sure the file is loaded
1071 (when (slot-boundp obj
'current-exact-match
)
1072 (semanticdb-find-result-nth-in-buffer (oref obj current-exact-match
) 0)))
1074 (defmethod semantic-collector-all-completions
1075 ((obj semantic-collector-abstract
) prefix
)
1076 "For OBJ, retrieve all completions matching PREFIX.
1077 The returned list consists of all the tags currently
1079 (when (slot-boundp obj
'last-all-completions
)
1080 (oref obj last-all-completions
)))
1082 (defmethod semantic-collector-try-completion
1083 ((obj semantic-collector-abstract
) prefix
)
1084 "For OBJ, attempt to match PREFIX.
1085 See `try-completion' for details on how this works.
1086 Return nil for no match.
1087 Return a string for a partial match.
1088 For a unique match of PREFIX, return the list of all tags
1090 (if (slot-boundp obj
'last-completion
)
1091 (oref obj last-completion
)))
1093 (defmethod semantic-collector-calculate-cache
1094 ((obj semantic-collector-abstract
))
1095 "Calculate the completion cache for OBJ."
1099 (defmethod semantic-collector-flush ((this semantic-collector-abstract
))
1100 "Flush THIS collector object, clearing any caches and prefix."
1101 (oset this cache nil
)
1102 (slot-makeunbound this
'last-prefix
)
1103 (slot-makeunbound this
'last-completion
)
1104 (slot-makeunbound this
'last-all-completions
)
1105 (slot-makeunbound this
'current-exact-match
)
1110 (defclass semantic-collector-buffer-abstract
(semantic-collector-abstract)
1112 "Root class for per-buffer completion engines.
1113 These collectors track themselves on a per-buffer basis."
1116 (defmethod constructor :STATIC
((this semantic-collector-buffer-abstract
)
1117 newname
&rest fields
)
1118 "Reuse previously created objects of this type in buffer."
1120 (bl semantic-collector-per-buffer-list
))
1121 (while (and bl
(null old
))
1122 (if (eq (object-class (car bl
)) this
)
1123 (setq old
(car bl
))))
1125 (let ((new (call-next-method)))
1126 (add-to-list 'semantic-collector-per-buffer-list new
)
1128 (slot-makeunbound old
'last-completion
)
1129 (slot-makeunbound old
'last-prefix
)
1130 (slot-makeunbound old
'current-exact-match
)
1133 ;; Buffer specific collectors should flush themselves
1134 (defun semantic-collector-buffer-flush (newcache)
1135 "Flush all buffer collector objects.
1136 NEWCACHE is the new tag table, but we ignore it."
1138 (let ((l semantic-collector-per-buffer-list
))
1140 (if (car l
) (semantic-collector-flush (car l
)))
1144 (add-hook 'semantic-after-toplevel-cache-change-hook
1145 'semantic-collector-buffer-flush
)
1147 ;;; DEEP BUFFER SPECIFIC COMPLETION
1149 (defclass semantic-collector-buffer-deep
1150 (semantic-collector-buffer-abstract)
1152 "Completion engine for tags in the current buffer.
1153 When searching for a tag, uses semantic deep searche functions.
1154 Basics search only in the current buffer.")
1156 (defmethod semantic-collector-calculate-cache
1157 ((obj semantic-collector-buffer-deep
))
1158 "Calculate the completion cache for OBJ.
1159 Uses `semantic-flatten-tags-table'"
1161 ;; Must create it in SEMANTICDB find format.
1162 ;; ( ( DBTABLE TAG TAG ... ) ... )
1164 (cons semanticdb-current-table
1165 (semantic-flatten-tags-table (oref obj buffer
))))))
1167 ;;; PROJECT SPECIFIC COMPLETION
1169 (defclass semantic-collector-project-abstract
(semantic-collector-abstract)
1170 ((path :initarg
:path
1172 :documentation
"List of database tables to search.
1173 At creation time, it can be anything accepted by
1174 `semanticdb-find-translate-path' as a PATH argument.")
1176 "Root class for project wide completion engines.
1177 Uses semanticdb for searching all tags in the current project."
1181 (defclass semantic-collector-project
(semantic-collector-project-abstract)
1183 "Completion engine for tags in a project.")
1186 (defmethod semantic-collector-calculate-completions-raw
1187 ((obj semantic-collector-project
) prefix completionlist
)
1188 "Calculate the completions for prefix from completionlist."
1189 (semanticdb-find-tags-for-completion prefix
(oref obj path
)))
1191 ;;; Brutish Project search
1192 (defclass semantic-collector-project-brutish
(semantic-collector-project-abstract)
1194 "Completion engine for tags in a project.")
1196 (declare-function semanticdb-brute-deep-find-tags-for-completion
1199 (defmethod semantic-collector-calculate-completions-raw
1200 ((obj semantic-collector-project-brutish
) prefix completionlist
)
1201 "Calculate the completions for prefix from completionlist."
1202 (require 'semantic
/db-find
)
1203 (semanticdb-brute-deep-find-tags-for-completion prefix
(oref obj path
)))
1205 (defclass semantic-collector-analyze-completions
(semantic-collector-abstract)
1206 ((context :initarg
:context
1207 :type semantic-analyze-context
1208 :documentation
"An analysis context.
1209 Specifies some context location from whence completion lists will be drawn."
1211 (first-pass-completions :type list
1212 :documentation
"List of valid completion tags.
1213 This list of tags is generated when completion starts. All searches
1214 derive from this list.")
1216 "Completion engine that uses the context analyzer to provide options.
1217 The only options available for completion are those which can be logically
1218 inserted into the current context.")
1220 (defmethod semantic-collector-calculate-completions-raw
1221 ((obj semantic-collector-analyze-completions
) prefix completionlist
)
1222 "calculate the completions for prefix from completionlist."
1223 ;; if there are no completions yet, calculate them.
1224 (if (not (slot-boundp obj
'first-pass-completions
))
1225 (oset obj first-pass-completions
1226 (semantic-analyze-possible-completions (oref obj context
))))
1227 ;; search our cached completion list. make it look like a semanticdb
1229 (list (cons (save-excursion
1230 (set-buffer (oref (oref obj context
) buffer
))
1231 semanticdb-current-table
)
1232 (semantic-find-tags-for-completion
1234 (oref obj first-pass-completions
)))))
1237 ;;; ------------------------------------------------------------
1238 ;;; Tag List Display Engines
1240 ;; A typical displayor accepts a pre-determined list of completions
1241 ;; generated by a collector. This format is in semanticdb search
1242 ;; form. This vaguely standard form is a bit challenging to navigate
1243 ;; because the tags do not contain buffer info, but the file assocated
1244 ;; with the tags preceed the tag in the list.
1246 ;; Basic displayors don't care, and can strip the results.
1247 ;; Advanced highlighting displayors need to know when they need
1248 ;; to load a file so that the tag in question can be highlighted.
1250 ;; Key interface methods to a displayor are:
1251 ;; * semantic-displayor-next-action
1252 ;; * semantic-displayor-set-completions
1253 ;; * semantic-displayor-current-focus
1254 ;; * semantic-displayor-show-request
1255 ;; * semantic-displayor-scroll-request
1256 ;; * semantic-displayor-focus-request
1258 (defclass semantic-displayor-abstract
()
1259 ((table :type
(or null semanticdb-find-result-with-nil
)
1261 :protection
:protected
1262 :documentation
"List of tags this displayor is showing.")
1263 (last-prefix :type string
1264 :protection
:protected
1265 :documentation
"Prefix associated with slot `table'")
1267 "Abstract displayor baseclass.
1268 Manages the display of some number of tags.
1269 Provides the basics for a displayor, including interacting with
1270 a collector, and tracking tables of completion to display."
1273 (defmethod semantic-displayor-cleanup ((obj semantic-displayor-abstract
))
1274 "Clean up any mess this displayor may have."
1277 (defmethod semantic-displayor-next-action ((obj semantic-displayor-abstract
))
1278 "The next action to take on the minibuffer related to display."
1279 (if (and (slot-boundp obj
'last-prefix
)
1280 (string= (oref obj last-prefix
) (semantic-completion-text))
1281 (eq last-command this-command
))
1285 (defmethod semantic-displayor-set-completions ((obj semantic-displayor-abstract
)
1287 "Set the list of tags to be completed over to TABLE."
1288 (oset obj table table
)
1289 (oset obj last-prefix prefix
))
1291 (defmethod semantic-displayor-show-request ((obj semantic-displayor-abstract
))
1292 "A request to show the current tags table."
1295 (defmethod semantic-displayor-focus-request ((obj semantic-displayor-abstract
))
1296 "A request to for the displayor to focus on some tag option."
1299 (defmethod semantic-displayor-scroll-request ((obj semantic-displayor-abstract
))
1300 "A request to for the displayor to scroll the completion list (if needed)."
1301 (scroll-other-window))
1303 (defmethod semantic-displayor-focus-previous ((obj semantic-displayor-abstract
))
1304 "Set the current focus to the previous item."
1307 (defmethod semantic-displayor-focus-next ((obj semantic-displayor-abstract
))
1308 "Set the current focus to the next item."
1311 (defmethod semantic-displayor-current-focus ((obj semantic-displayor-abstract
))
1312 "Return a single tag currently in focus.
1313 This object type doesn't do focus, so will never have a focus object."
1316 ;; Traditional displayor
1317 (defcustom semantic-completion-displayor-format-tag-function
1318 #'semantic-format-tag-name
1319 "*A Tag format function to use when showing completions."
1321 :type semantic-format-tag-custom-list
)
1323 (defclass semantic-displayor-traditional
(semantic-displayor-abstract)
1325 "Display options in *Completions* buffer.
1326 Traditional display mechanism for a list of possible completions.
1327 Completions are showin in a new buffer and listed with the ability
1328 to click on the items to aid in completion.")
1330 (defmethod semantic-displayor-show-request ((obj semantic-displayor-traditional
))
1331 "A request to show the current tags table."
1333 ;; NOTE TO SELF. Find the character to type next, and emphesize it.
1335 (with-output-to-temp-buffer "*Completions*"
1336 (display-completion-list
1337 (mapcar semantic-completion-displayor-format-tag-function
1338 (semanticdb-strip-find-results (oref obj table
))))
1342 ;;; Abstract baseclass for any displayor which supports focus
1343 (defclass semantic-displayor-focus-abstract
(semantic-displayor-abstract)
1344 ((focus :type number
1345 :protection
:protected
1346 :documentation
"A tag index from `table' which has focus.
1347 Multiple calls to the display function can choose to focus on a
1348 given tag, by highlighting its location.")
1353 "Non-nil if focusing requires a tag's buffer be in memory.")
1355 "Abstract displayor supporting `focus'.
1356 A displayor which has the ability to focus in on one tag.
1357 Focusing is a way of differentiationg between multiple tags
1358 which have the same name."
1361 (defmethod semantic-displayor-next-action ((obj semantic-displayor-focus-abstract
))
1362 "The next action to take on the minibuffer related to display."
1363 (if (and (slot-boundp obj
'last-prefix
)
1364 (string= (oref obj last-prefix
) (semantic-completion-text))
1365 (eq last-command this-command
))
1367 (slot-boundp obj
'focus
)
1368 (slot-boundp obj
'table
)
1369 (<= (semanticdb-find-result-length (oref obj table
))
1370 (1+ (oref obj focus
))))
1371 ;; We are at the end of the focus road.
1373 ;; Focus on some item.
1377 (defmethod semantic-displayor-set-completions ((obj semantic-displayor-focus-abstract
)
1379 "Set the list of tags to be completed over to TABLE."
1381 (slot-makeunbound obj
'focus
))
1383 (defmethod semantic-displayor-focus-previous ((obj semantic-displayor-focus-abstract
))
1384 "Set the current focus to the previous item.
1385 Not meaningful return value."
1386 (when (and (slot-boundp obj
'table
) (oref obj table
))
1387 (with-slots (table) obj
1388 (if (or (not (slot-boundp obj
'focus
))
1389 (<= (oref obj focus
) 0))
1390 (oset obj focus
(1- (semanticdb-find-result-length table
)))
1391 (oset obj focus
(1- (oref obj focus
)))
1395 (defmethod semantic-displayor-focus-next ((obj semantic-displayor-focus-abstract
))
1396 "Set the current focus to the next item.
1397 Not meaningful return value."
1398 (when (and (slot-boundp obj
'table
) (oref obj table
))
1399 (with-slots (table) obj
1400 (if (not (slot-boundp obj
'focus
))
1402 (oset obj focus
(1+ (oref obj focus
)))
1404 (if (<= (semanticdb-find-result-length table
) (oref obj focus
))
1408 (defmethod semantic-displayor-focus-tag ((obj semantic-displayor-focus-abstract
))
1409 "Return the next tag OBJ should focus on."
1410 (when (and (slot-boundp obj
'table
) (oref obj table
))
1411 (with-slots (table) obj
1412 (semanticdb-find-result-nth table
(oref obj focus
)))))
1414 (defmethod semantic-displayor-current-focus ((obj semantic-displayor-focus-abstract
))
1415 "Return the tag currently in focus, or call parent method."
1416 (if (and (slot-boundp obj
'focus
)
1417 (slot-boundp obj
'table
)
1418 ;; Only return the current focus IFF the minibuffer reflects
1419 ;; the list this focus was derived from.
1420 (slot-boundp obj
'last-prefix
)
1421 (string= (semantic-completion-text) (oref obj last-prefix
))
1424 (if (oref obj find-file-focus
)
1425 (semanticdb-find-result-nth-in-buffer (oref obj table
) (oref obj focus
))
1426 ;; result-nth returns a cons with car being the tag, and cdr the
1428 (car (semanticdb-find-result-nth (oref obj table
) (oref obj focus
))))
1430 (call-next-method)))
1432 ;;; Simple displayor which performs traditional display completion,
1433 ;; and also focuses with highlighting.
1434 (defclass semantic-displayor-traditional-with-focus-highlight
1435 (semantic-displayor-focus-abstract semantic-displayor-traditional
)
1436 ((find-file-focus :initform t
))
1437 "Display completions in *Completions* buffer, with focus highlight.
1438 A traditional displayor which can focus on a tag by showing it.
1439 Same as `semantic-displayor-traditional', but with selection between
1440 multiple tags with the same name done by 'focusing' on the source
1441 location of the different tags to differentiate them.")
1443 (defmethod semantic-displayor-focus-request
1444 ((obj semantic-displayor-traditional-with-focus-highlight
))
1445 "Focus in on possible tag completions.
1446 Focus is performed by cycling through the tags and highlighting
1447 one in the source buffer."
1448 (let* ((tablelength (semanticdb-find-result-length (oref obj table
)))
1449 (focus (semantic-displayor-focus-tag obj
))
1452 (rtable (cdr focus
))
1454 (nt (semanticdb-normalize-one-tag rtable rtag
))
1458 ;; If we fail to normalize, resete.
1459 (when (not tag
) (setq table rtable tag rtag
))
1461 (let ((buf (or (semantic-tag-buffer tag
)
1462 (and table
(semanticdb-get-buffer table
)))))
1463 ;; If no buffer is provided, then we can make up a summary buffer.
1466 (set-buffer (get-buffer-create "*Completion Focus*"))
1468 (insert "Focus on tag: \n")
1469 (insert (semantic-format-tag-summarize tag nil t
) "\n\n")
1471 (insert "From table: \n")
1472 (insert (object-name table
) "\n\n"))
1474 (insert "In buffer: \n\n")
1475 (insert (format "%S" buf
)))
1476 (setq buf
(current-buffer))))
1477 ;; Show the tag in the buffer.
1478 (if (get-buffer-window buf
)
1479 (select-window (get-buffer-window buf
))
1480 (switch-to-buffer-other-window buf t
)
1481 (select-window (get-buffer-window buf
)))
1482 ;; Now do some positioning
1484 (if (semantic-tag-with-position-p tag
)
1485 ;; Full tag positional information available
1487 (goto-char (semantic-tag-start tag
))
1488 ;; This avoids a dangerous problem if we just loaded a tag
1489 ;; from a file, but the original position was not updated
1490 ;; in the TAG variable we are currently using.
1491 (semantic-momentary-highlight-tag (semantic-current-tag))
1493 (select-window (minibuffer-window)))
1494 ;; Calculate text difference between contents and the focus item.
1495 (let* ((mbc (semantic-completion-text))
1496 (ftn (semantic-tag-name tag
))
1497 (diff (substring ftn
(length mbc
))))
1498 (semantic-completion-message
1499 (format "%s [%d of %d matches]" diff
(1+ (oref obj focus
)) tablelength
)))
1503 ;;; Tooltip completion lister
1505 ;; Written and contributed by Masatake YAMATO <jet@gyve.org>
1507 ;; Modified by Eric Ludlam for
1508 ;; * Safe compatibility for tooltip free systems.
1509 ;; * Don't use 'avoid package for tooltip positioning.
1511 (defclass semantic-displayor-tooltip
(semantic-displayor-traditional)
1512 ((max-tags :type integer
1517 "Max number of tags displayed on tooltip at once.
1518 If `force-show' is 1, this value is ignored with typing tab or space twice continuously.
1519 if `force-show' is 0, this value is always ignored.")
1520 (force-show :type integer
1521 :initarg
:force-show
1523 :custom
(choice (const
1524 :tag
"Show when double typing"
1530 :tag
"Show if the number of tags is less than `max-tags'."
1533 "Control the behavior of the number of tags is greater than `max-tags'.
1534 -1 means tags are never shown.
1535 0 means the tags are always shown.
1536 1 means tags are shown if space or tab is typed twice continuously.")
1537 (typing-count :type integer
1540 "Counter holding how many times the user types space or tab continuously before showing tags.")
1541 (shown :type boolean
1544 "Flag representing whether tags is shown once or not.")
1546 "Display completions options in a tooltip.
1547 Display mechanism using tooltip for a list of possible completions.")
1549 (defmethod initialize-instance :AFTER
((obj semantic-displayor-tooltip
) &rest args
)
1550 "Make sure we have tooltips required."
1556 (defmethod semantic-displayor-show-request ((obj semantic-displayor-tooltip
))
1557 "A request to show the current tags table."
1558 (if (or (not (featurep 'tooltip
)) (not tooltip-mode
))
1559 ;; If we cannot use tooltips, then go to the normal mode with
1560 ;; a traditional completion buffer.
1562 (let* ((tablelong (semanticdb-strip-find-results (oref obj table
)))
1563 (table (semantic-unique-tag-table-by-name tablelong
))
1564 (l (mapcar semantic-completion-displayor-format-tag-function table
))
1566 (typing-count (oref obj typing-count
))
1567 (force-show (oref obj force-show
))
1568 (matchtxt (semantic-completion-text))
1570 (if (or (oref obj shown
)
1571 (< ll
(oref obj max-tags
))
1572 (and (<= 0 force-show
)
1573 (< (1- force-show
) typing-count
)))
1575 (oset obj typing-count
0)
1578 ;; We Have only one possible match. There could be two cases.
1579 ;; 1) input text != single match.
1581 ;; 2) input text == single match.
1582 ;; --> Complain about it, but still show the match.
1583 (if (string= matchtxt
(semantic-tag-name (car table
)))
1584 (setq msg
(concat "[COMPLETE]\n" (car l
)))
1586 ;; Create the long message.
1587 (setq msg
(mapconcat 'identity l
"\n"))
1588 ;; If there is nothing, say so!
1589 (if (eq 0 (length msg
))
1590 (setq msg
"[NO MATCH]")))
1591 (semantic-displayor-tooltip-show msg
))
1592 ;; The typing count determines if the user REALLY REALLY
1593 ;; wanted to show that much stuff. Only increment
1594 ;; if the current command is a completion command.
1595 (if (and (stringp (this-command-keys))
1596 (string= (this-command-keys) "\C-i"))
1597 (oset obj typing-count
(1+ typing-count
)))
1598 ;; At this point, we know we have too many items.
1599 ;; Lets be brave, and truncate l
1600 (setcdr (nthcdr (oref obj max-tags
) l
) nil
)
1601 (setq msg
(mapconcat 'identity l
"\n"))
1604 (semantic-displayor-tooltip-show (concat msg
"\n...")))
1606 (semantic-displayor-tooltip-show (concat msg
"\n(TAB for more)")))
1612 (if (fboundp 'window-inside-edges
)
1614 (defalias 'semantic-displayor-window-edges
1615 'window-inside-edges
)
1617 (defalias 'semantic-displayor-window-edges
1621 (defun semantic-displayor-point-position ()
1622 "Return the location of POINT as positioned on the selected frame.
1623 Return a cons cell (X . Y)"
1624 (let* ((frame (selected-frame))
1625 (left (frame-parameter frame
'left
))
1626 (top (frame-parameter frame
'top
))
1627 (point-pix-pos (posn-x-y (posn-at-point)))
1628 (edges (window-inside-pixel-edges (selected-window))))
1629 (cons (+ (car point-pix-pos
) (car edges
) left
)
1630 (+ (cdr point-pix-pos
) (cadr edges
) top
))))
1633 (defun semantic-displayor-tooltip-show (text)
1634 "Display a tooltip with TEXT near cursor."
1635 (let ((point-pix-pos (semantic-displayor-point-position))
1636 (tooltip-frame-parameters
1637 (append tooltip-frame-parameters nil
)))
1639 (cons 'left
(+ (car point-pix-pos
) (frame-char-width)))
1640 tooltip-frame-parameters
)
1642 (cons 'top
(+ (cdr point-pix-pos
) (frame-char-height)))
1643 tooltip-frame-parameters
)
1644 (tooltip-show text
)))
1646 (defmethod semantic-displayor-scroll-request ((obj semantic-displayor-tooltip
))
1647 "A request to for the displayor to scroll the completion list (if needed)."
1648 ;; Do scrolling in the tooltip.
1649 (oset obj max-tags
30)
1650 (semantic-displayor-show-request obj
)
1653 ;; End code contributed by Masatake YAMATO <jet@gyve.org>
1656 ;;; Ghost Text displayor
1658 (defclass semantic-displayor-ghost
(semantic-displayor-focus-abstract)
1660 ((ghostoverlay :type overlay
1662 "The overlay the ghost text is displayed in.")
1663 (first-show :initform t
1665 "Non nil if we have not seen our first show request.")
1667 "Cycle completions inline with ghost text.
1668 Completion displayor using ghost chars after point for focus options.
1669 Whichever completion is currently in focus will be displayed as ghost
1670 text using overlay options.")
1672 (defmethod semantic-displayor-next-action ((obj semantic-displayor-ghost
))
1673 "The next action to take on the inline completion related to display."
1674 (let ((ans (call-next-method))
1675 (table (when (slot-boundp obj
'table
)
1677 (if (and (eq ans
'displayend
)
1679 (= (semanticdb-find-result-length table
) 1)
1684 (defmethod semantic-displayor-cleanup ((obj semantic-displayor-ghost
))
1685 "Clean up any mess this displayor may have."
1686 (when (slot-boundp obj
'ghostoverlay
)
1687 (semantic-overlay-delete (oref obj ghostoverlay
)))
1690 (defmethod semantic-displayor-set-completions ((obj semantic-displayor-ghost
)
1692 "Set the list of tags to be completed over to TABLE."
1695 (semantic-displayor-cleanup obj
)
1699 (defmethod semantic-displayor-show-request ((obj semantic-displayor-ghost
))
1700 "A request to show the current tags table."
1701 ; (if (oref obj first-show)
1703 ; (oset obj first-show nil)
1704 (semantic-displayor-focus-next obj
)
1705 (semantic-displayor-focus-request obj
)
1707 ;; Only do the traditional thing if the first show request
1708 ;; has been seen. Use the first one to start doing the ghost
1710 ; (call-next-method)
1714 (defmethod semantic-displayor-focus-request
1715 ((obj semantic-displayor-ghost
))
1716 "Focus in on possible tag completions.
1717 Focus is performed by cycling through the tags and showing a possible
1718 completion text in ghost text."
1719 (let* ((tablelength (semanticdb-find-result-length (oref obj table
)))
1720 (focus (semantic-displayor-focus-tag obj
))
1724 (semantic-completion-message "No tags to focus on.")
1725 ;; Display the focus completion as ghost text after the current
1727 (when (or (not (slot-boundp obj
'ghostoverlay
))
1728 (not (semantic-overlay-live-p (oref obj ghostoverlay
))))
1729 (oset obj ghostoverlay
1730 (semantic-make-overlay (point) (1+ (point)) (current-buffer) t
)))
1732 (let* ((lp (semantic-completion-text))
1733 (os (substring (semantic-tag-name tag
) (length lp
)))
1734 (ol (oref obj ghostoverlay
))
1737 (put-text-property 0 (length os
) 'face
'region os
)
1739 (semantic-overlay-put
1740 ol
'display
(concat os
(buffer-substring (point) (1+ (point)))))
1742 ;; Calculate text difference between contents and the focus item.
1743 (let* ((mbc (semantic-completion-text))
1744 (ftn (concat (semantic-tag-name tag
)))
1746 (put-text-property (length mbc
) (length ftn
) 'face
1748 (semantic-completion-message
1749 (format "%s [%d of %d matches]" ftn
(1+ (oref obj focus
)) tablelength
)))
1753 ;;; ------------------------------------------------------------
1754 ;;; Specific queries
1756 (defvar semantic-complete-inline-custom-type
1760 (let* ((C (intern (car class
)))
1761 (doc (documentation-property C
'variable-documentation
))
1762 (doc1 (car (split-string doc
"\n")))
1767 (eieio-build-class-alist semantic-displayor-abstract t
))
1769 "Possible options for inline completion displayors.
1770 Use this to enable custom editing.")
1772 (defcustom semantic-complete-inline-analyzer-displayor-class
1773 'semantic-displayor-traditional
1774 "*Class for displayor to use with inline completion."
1776 :type semantic-complete-inline-custom-type
1779 (defun semantic-complete-read-tag-buffer-deep (prompt &optional
1783 "Ask for a tag by name from the current buffer.
1784 Available tags are from the current buffer, at any level.
1785 Completion options are presented in a traditional way, with highlighting
1786 to resolve same-name collisions.
1787 PROMPT is a string to prompt with.
1788 DEFAULT-TAG is a semantic tag or string to use as the default value.
1789 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.
1790 HISTORY is a symbol representing a variable to store the history in."
1791 (semantic-complete-read-tag-engine
1792 (semantic-collector-buffer-deep prompt
:buffer
(current-buffer))
1793 (semantic-displayor-traditional-with-focus-highlight "simple")
1794 ;;(semantic-displayor-tooltip "simple")
1801 (defun semantic-complete-read-tag-project (prompt &optional
1805 "Ask for a tag by name from the current project.
1806 Available tags are from the current project, at the top level.
1807 Completion options are presented in a traditional way, with highlighting
1808 to resolve same-name collisions.
1809 PROMPT is a string to prompt with.
1810 DEFAULT-TAG is a semantic tag or string to use as the default value.
1811 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.
1812 HISTORY is a symbol representing a variable to store the history in."
1813 (semantic-complete-read-tag-engine
1814 (semantic-collector-project-brutish prompt
1815 :buffer
(current-buffer)
1816 :path
(current-buffer)
1818 (semantic-displayor-traditional-with-focus-highlight "simple")
1825 (defun semantic-complete-inline-tag-project ()
1826 "Complete a symbol name by name from within the current project.
1827 This is similar to `semantic-complete-read-tag-project', except
1828 that the completion interaction is in the buffer where the context
1829 was calculated from.
1830 Customize `semantic-complete-inline-analyzer-displayor-class'
1831 to control how completion options are displayed.
1832 See `semantic-complete-inline-tag-engine' for details on how
1834 (let* ((collector (semantic-collector-project-brutish
1836 :buffer
(current-buffer)
1837 :path
(current-buffer)))
1838 (sbounds (semantic-ctxt-current-symbol-and-bounds))
1839 (syms (car sbounds
))
1840 (start (car (nth 2 sbounds
)))
1841 (end (cdr (nth 2 sbounds
)))
1842 (rsym (reverse syms
))
1843 (thissym (nth 1 sbounds
))
1844 (nextsym (car-safe (cdr rsym
)))
1846 (when (and thissym
(or (not (string= thissym
""))
1848 ;; Do a quick calcuation of completions.
1849 (semantic-collector-calculate-completions
1850 collector thissym nil
)
1851 ;; Get the master list
1852 (setq complst
(semanticdb-strip-find-results
1853 (semantic-collector-all-completions collector thissym
)))
1855 (setq complst
(semantic-unique-tag-table-by-name complst
))
1856 (if (or (and (= (length complst
) 1)
1857 ;; Check to see if it is the same as what is there.
1858 ;; if so, we can offer to complete.
1859 (let ((compname (semantic-tag-name (car complst
))))
1860 (not (string= compname thissym
))))
1861 (> (length complst
) 1))
1862 ;; There are several options. Do the completion.
1863 (semantic-complete-inline-tag-engine
1865 (funcall semantic-complete-inline-analyzer-displayor-class
1867 ;;(semantic-displayor-tooltip "simple")
1872 (defun semantic-complete-read-tag-analyzer (prompt &optional
1875 "Ask for a tag by name based on the current context.
1876 The function `semantic-analyze-current-context' is used to
1877 calculate the context. `semantic-analyze-possible-completions' is used
1878 to generate the list of possible completions.
1879 PROMPT is the first part of the prompt. Additional prompt
1880 is added based on the contexts full prefix.
1881 CONTEXT is the semantic analyzer context to start with.
1882 HISTORY is a symbol representing a variable to store the history in.
1883 usually a default-tag and initial-input are available for completion
1884 prompts. these are calculated from the CONTEXT variable passed in."
1885 (if (not context
) (setq context
(semantic-analyze-current-context (point))))
1886 (let* ((syms (semantic-ctxt-current-symbol (point)))
1887 (inp (car (reverse syms
))))
1888 (setq syms
(nreverse (cdr (nreverse syms
))))
1889 (semantic-complete-read-tag-engine
1890 (semantic-collector-analyze-completions
1892 :buffer
(oref context buffer
)
1894 (semantic-displayor-traditional-with-focus-highlight "simple")
1896 (set-buffer (oref context buffer
))
1897 (goto-char (cdr (oref context bounds
)))
1898 (concat prompt
(mapconcat 'identity syms
".")
1905 (defun semantic-complete-inline-analyzer (context)
1906 "Complete a symbol name by name based on the current context.
1907 This is similar to `semantic-complete-read-tag-analyze', except
1908 that the completion interaction is in the buffer where the context
1909 was calculated from.
1910 CONTEXT is the semantic analyzer context to start with.
1911 Customize `semantic-complete-inline-analyzer-displayor-class'
1912 to control how completion options are displayed.
1914 See `semantic-complete-inline-tag-engine' for details on how
1916 (if (not context
) (setq context
(semantic-analyze-current-context (point))))
1917 (if (not context
) (error "Nothing to complete on here"))
1918 (let* ((collector (semantic-collector-analyze-completions
1920 :buffer
(oref context buffer
)
1922 (syms (semantic-ctxt-current-symbol (point)))
1923 (rsym (reverse syms
))
1924 (thissym (car rsym
))
1925 (nextsym (car-safe (cdr rsym
)))
1927 (when (and thissym
(or (not (string= thissym
""))
1929 ;; Do a quick calcuation of completions.
1930 (semantic-collector-calculate-completions
1931 collector thissym nil
)
1932 ;; Get the master list
1933 (setq complst
(semanticdb-strip-find-results
1934 (semantic-collector-all-completions collector thissym
)))
1936 (setq complst
(semantic-unique-tag-table-by-name complst
))
1937 (if (or (and (= (length complst
) 1)
1938 ;; Check to see if it is the same as what is there.
1939 ;; if so, we can offer to complete.
1940 (let ((compname (semantic-tag-name (car complst
))))
1941 (not (string= compname thissym
))))
1942 (> (length complst
) 1))
1943 ;; There are several options. Do the completion.
1944 (semantic-complete-inline-tag-engine
1946 (funcall semantic-complete-inline-analyzer-displayor-class
1948 ;;(semantic-displayor-tooltip "simple")
1949 (oref context buffer
)
1950 (car (oref context bounds
))
1951 (cdr (oref context bounds
))
1955 (defcustom semantic-complete-inline-analyzer-idle-displayor-class
1956 'semantic-displayor-ghost
1957 "*Class for displayor to use with inline completion at idle time."
1959 :type semantic-complete-inline-custom-type
1962 (defun semantic-complete-inline-analyzer-idle (context)
1963 "Complete a symbol name by name based on the current context for idle time.
1964 CONTEXT is the semantic analyzer context to start with.
1965 This function is used from `semantic-idle-completions-mode'.
1967 This is the same as `semantic-complete-inline-analyzer', except that
1968 it uses `semantic-complete-inline-analyzer-idle-displayor-class'
1969 to control how completions are displayed.
1971 See `semantic-complete-inline-tag-engine' for details on how
1973 (let ((semantic-complete-inline-analyzer-displayor-class
1974 semantic-complete-inline-analyzer-idle-displayor-class
))
1975 (semantic-complete-inline-analyzer context
)
1980 (defun semantic-complete-jump-local ()
1981 "Jump to a semantic symbol."
1983 (let ((tag (semantic-complete-read-tag-buffer-deep "Symbol: ")))
1984 (when (semantic-tag-p tag
)
1986 (goto-char (semantic-tag-start tag
))
1987 (semantic-momentary-highlight-tag tag
)
1989 (semantic-tag-class tag
)
1990 (semantic-tag-name tag
)))))
1993 (defun semantic-complete-jump ()
1994 "Jump to a semantic symbol."
1996 (let* ((tag (semantic-complete-read-tag-project "Symbol: ")))
1997 (when (semantic-tag-p tag
)
1999 (semantic-go-to-tag tag
)
2000 (switch-to-buffer (current-buffer))
2001 (semantic-momentary-highlight-tag tag
)
2003 (semantic-tag-class tag
)
2004 (semantic-tag-name tag
)))))
2007 (defun semantic-complete-analyze-and-replace ()
2008 "Perform prompt completion to do in buffer completion.
2009 `semantic-analyze-possible-completions' is used to determine the
2011 The minibuffer is used to perform the completion.
2012 The result is inserted as a replacement of the text that was there."
2014 (let* ((c (semantic-analyze-current-context (point)))
2015 (tag (save-excursion (semantic-complete-read-tag-analyzer "" c
))))
2016 ;; Take tag, and replace context bound with its name.
2017 (goto-char (car (oref c bounds
)))
2018 (delete-region (point) (cdr (oref c bounds
)))
2019 (insert (semantic-tag-name tag
))
2020 (message "%S" (semantic-format-tag-summarize tag
))))
2023 (defun semantic-complete-analyze-inline ()
2024 "Perform prompt completion to do in buffer completion.
2025 `semantic-analyze-possible-completions' is used to determine the
2027 The function returns immediately, leaving the buffer in a mode that
2028 will perform the completion.
2029 Configure `semantic-complete-inline-analyzer-displayor-class' to change
2030 how completion options are displayed."
2032 ;; Only do this if we are not already completing something.
2033 (if (not (semantic-completion-inline-active-p))
2034 (semantic-complete-inline-analyzer
2035 (semantic-analyze-current-context (point))))
2036 ;; Report a message if things didn't startup.
2037 (if (and (interactive-p)
2038 (not (semantic-completion-inline-active-p)))
2039 (message "Inline completion not needed.")
2040 ;; Since this is most likely bound to something, and not used
2041 ;; at idle time, throw in a TAB for good measure.
2042 (semantic-complete-inline-TAB)
2046 (defun semantic-complete-analyze-inline-idle ()
2047 "Perform prompt completion to do in buffer completion.
2048 `semantic-analyze-possible-completions' is used to determine the
2050 The function returns immediately, leaving the buffer in a mode that
2051 will perform the completion.
2052 Configure `semantic-complete-inline-analyzer-idle-displayor-class'
2053 to change how completion options are displayed."
2055 ;; Only do this if we are not already completing something.
2056 (if (not (semantic-completion-inline-active-p))
2057 (semantic-complete-inline-analyzer-idle
2058 (semantic-analyze-current-context (point))))
2059 ;; Report a message if things didn't startup.
2060 (if (and (interactive-p)
2061 (not (semantic-completion-inline-active-p)))
2062 (message "Inline completion not needed."))
2066 (defun semantic-complete-self-insert (arg)
2067 "Like `self-insert-command', but does completion afterwards.
2068 ARG is passed to `self-insert-command'. If ARG is nil,
2069 use `semantic-complete-analyze-inline' to complete."
2071 ;; If we are already in a completion scenario, exit now, and then start over.
2072 (semantic-complete-inline-exit)
2075 (self-insert-command arg
)
2077 ;; Prepare for doing completion, but exit quickly if there is keyboard
2079 (when (and (not (semantic-exit-on-input 'csi
2080 (semantic-fetch-tags)
2081 (semantic-throw-on-input 'csi
)
2084 (not (semantic-exit-on-input 'csi
2085 (semantic-analyze-current-context)
2086 (semantic-throw-on-input 'csi
)
2089 (semantic-complete-analyze-inline)
2090 ;; Ignore errors. Seems likely that we'll get some once in a while.
2094 (provide 'semantic
/complete
)
2097 ;; generated-autoload-file: "loaddefs.el"
2098 ;; generated-autoload-feature: semantic/loaddefs
2099 ;; generated-autoload-load-name: "semantic/complete"
2102 ;; arch-tag: a07c8f71-e53b-416e-9704-3a99ef101b09
2103 ;;; semantic/complete.el ends here