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 distiguish, 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 ;; fullfill 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
113 (require 'semantic/analyze)
114 (require 'semantic/ctxt)
115 (require 'semantic/decorate)
116 (require 'semantic/format)
119 ;; For the semantic-find-tags-for-completion macro.
120 (require 'semantic/find))
124 ;; Tooltip not available in older emacsen.
133 (if (fboundp 'minibuffer-contents)
134 (eval-and-compile (defalias 'semantic-minibuffer-contents 'minibuffer-contents))
135 (eval-and-compile (defalias 'semantic-minibuffer-contents 'buffer-string)))
136 (if (fboundp 'delete-minibuffer-contents)
137 (eval-and-compile (defalias 'semantic-delete-minibuffer-contents 'delete-minibuffer-contents))
138 (eval-and-compile (defalias 'semantic-delete-minibuffer-contents 'erase-buffer)))
140 (defvar semantic-complete-inline-overlay nil
141 "The overlay currently active while completing inline.")
143 (defun semantic-completion-inline-active-p ()
144 "Non-nil if inline completion is active."
145 (when (and semantic-complete-inline-overlay
146 (not (semantic-overlay-live-p semantic-complete-inline-overlay)))
147 (semantic-overlay-delete semantic-complete-inline-overlay)
148 (setq semantic-complete-inline-overlay nil))
149 semantic-complete-inline-overlay)
151 ;;; ------------------------------------------------------------
152 ;;; MINIBUFFER or INLINE utils
154 (defun semantic-completion-text ()
155 "Return the text that is currently in the completion buffer.
156 For a minibuffer prompt, this is the minibuffer text.
157 For inline completion, this is the text wrapped in the inline completion
159 (if semantic-complete-inline-overlay
160 (semantic-complete-inline-text)
161 (semantic-minibuffer-contents)))
163 (defun semantic-completion-delete-text ()
164 "Delete the text that is actively being completed.
165 Presumably if you call this you will insert something new there."
166 (if semantic-complete-inline-overlay
167 (semantic-complete-inline-delete-text)
168 (semantic-delete-minibuffer-contents)))
170 (defun semantic-completion-message (fmt &rest args)
171 "Display the string FMT formatted with ARGS at the end of the minibuffer."
172 (if semantic-complete-inline-overlay
173 (apply 'message fmt args)
174 (message (concat (buffer-string) (apply 'format fmt args)))))
176 ;;; ------------------------------------------------------------
177 ;;; MINIBUFFER: Option Selection harnesses
179 (defvar semantic-completion-collector-engine nil
180 "The tag collector for the current completion operation.
181 Value should be an object of a subclass of
182 `semantic-completion-engine-abstract'.")
184 (defvar semantic-completion-display-engine nil
185 "The tag display engine for the current completion operation.
186 Value should be a ... what?")
188 (defvar semantic-complete-key-map
189 (let ((km (make-sparse-keymap)))
190 (define-key km " " 'semantic-complete-complete-space)
191 (define-key km "\t" 'semantic-complete-complete-tab)
192 (define-key km "\C-m" 'semantic-complete-done)
193 (define-key km "\C-g" 'abort-recursive-edit)
194 (define-key km "\M-n" 'next-history-element)
195 (define-key km "\M-p" 'previous-history-element)
196 (define-key km "\C-n" 'next-history-element)
197 (define-key km "\C-p" 'previous-history-element)
198 ;; Add history navigation
200 "Keymap used while completing across a list of tags.")
202 (defvar semantic-completion-default-history nil
203 "Default history variable for any unhistoried prompt.
204 Keeps STRINGS only in the history.")
207 (defun semantic-complete-read-tag-engine (collector displayor prompt
208 default-tag initial-input
210 "Read a semantic tag, and return a tag for the selection.
211 Argument COLLECTOR is an object which can be used to to calculate
212 a list of possible hits. See `semantic-completion-collector-engine'
213 for details on COLLECTOR.
214 Argumeng DISPLAYOR is an object used to display a list of possible
215 completions for a given prefix. See`semantic-completion-display-engine'
216 for details on DISPLAYOR.
217 PROMPT is a string to prompt with.
218 DEFAULT-TAG is a semantic tag or string to use as the default value.
219 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.
220 HISTORY is a symbol representing a variable to story the history in."
221 (let* ((semantic-completion-collector-engine collector)
222 (semantic-completion-display-engine displayor)
223 (semantic-complete-active-default nil)
224 (semantic-complete-current-matched-tag nil)
225 (default-as-tag (semantic-complete-default-to-tag default-tag))
226 (default-as-string (when (semantic-tag-p default-as-tag)
227 (semantic-tag-name default-as-tag)))
230 (when default-as-string
231 ;; Add this to the prompt.
233 ;; I really want to add a lookup of the symbol in those
234 ;; tags available to the collector and only add it if it
235 ;; is available as a possibility, but I'm too lazy right
239 ;; @todo - move from () to into the editable area
240 (if (string-match ":" prompt)
242 (substring prompt 0 (match-beginning 0))
243 " (" default-as-string ")"
244 (substring prompt (match-beginning 0))))
245 (setq prompt (concat prompt " (" default-as-string "): "))))
247 ;; Perform the Completion
250 (read-from-minibuffer prompt
252 semantic-complete-key-map
255 'semantic-completion-default-history)
257 (semantic-collector-cleanup semantic-completion-collector-engine)
258 (semantic-displayor-cleanup semantic-completion-display-engine)
261 ;; Extract the tag from the completion machinery.
263 semantic-complete-current-matched-tag
267 ;;; Util for basic completion prompts
270 (defvar semantic-complete-active-default nil
271 "The current default tag calculated for this prompt.")
273 (defun semantic-complete-default-to-tag (default)
274 "Convert a calculated or passed in DEFAULT into a tag."
275 (if (semantic-tag-p default)
276 ;; Just return what was passed in.
277 (setq semantic-complete-active-default default)
278 ;; If none was passed in, guess.
280 (setq default (semantic-ctxt-current-thing)))
284 ;; Turn default into something useful.
287 ;; Semantic-ctxt-current-symbol will return a list of
288 ;; strings. Technically, we should use the analyzer to
289 ;; fully extract what we need, but for now, just grab the
291 ((and (listp default) (stringp (car default)))
296 (symbol-name default))
298 (signal 'wrong-type-argument
299 (list default 'semantic-tag-p)))))
301 ;; Now that we have that symbol string, look it up using the active
302 ;; collector. If we get a match, use it.
304 (semantic-collector-calculate-completions
305 semantic-completion-collector-engine
307 ;; Do we have the perfect match???
308 (let ((ml (semantic-collector-current-exact-match
309 semantic-completion-collector-engine)))
311 ;; We don't care about uniqueness. Just guess for convenience
312 (setq tag (semanticdb-find-result-nth-in-buffer ml 0))))
314 (setq semantic-complete-active-default tag)
315 ;; Return it.. .whatever it may be
319 ;;; Prompt Return Value
321 ;; Getting a return value out of this completion prompt is a bit
322 ;; challenging. The read command returns the string typed in.
323 ;; We need to convert this into a valid tag. We can exit the minibuffer
324 ;; for different reasons. If we purposely exit, we must make sure
325 ;; the focused tag is calculated... preferably once.
326 (defvar semantic-complete-current-matched-tag nil
327 "Variable used to pass the tags being matched to the prompt.")
329 ;; semantic-displayor-focus-abstract-child-p is part of the
330 ;; semantic-displayor-focus-abstract class, defined later in this
332 (declare-function semantic-displayor-focus-abstract-child-p "semantic/complete")
334 (defun semantic-complete-current-match ()
335 "Calculate a match from the current completion environment.
336 Save this in our completion variable. Make sure that variable
337 is cleared if any other keypress is made.
339 tag - a single tag that has been matched.
340 string - a message to show in the minibuffer."
341 ;; Query the environment for an active completion.
342 (let ((collector semantic-completion-collector-engine)
343 (displayor semantic-completion-display-engine)
344 (contents (semantic-completion-text))
347 (if (string= contents "")
348 ;; The user wants the defaults!
349 (setq answer semantic-complete-active-default)
350 ;; This forces a full calculation of completion on CR.
352 (semantic-collector-calculate-completions collector contents nil))
353 (semantic-complete-try-completion)
355 ;; Input match displayor focus entry
356 ((setq answer (semantic-displayor-current-focus displayor))
357 ;; We have answer, continue
359 ;; One match from the collector
360 ((setq matchlist (semantic-collector-current-exact-match collector))
361 (if (= (semanticdb-find-result-length matchlist) 1)
362 (setq answer (semanticdb-find-result-nth-in-buffer matchlist 0))
363 (if (semantic-displayor-focus-abstract-child-p displayor)
364 ;; For focusing displayors, we can claim this is
365 ;; not unique. Multiple focuses can choose the correct
367 (setq answer "Not Unique")
368 ;; If we don't have a focusing displayor, we need to do something
369 ;; graceful. First, see if all the matches have the same name.
371 (firstname (semantic-tag-name
373 (semanticdb-find-result-nth matchlist 0)))
376 (max (semanticdb-find-result-length matchlist)))
377 (while (and allsame (< cnt max))
382 (semanticdb-find-result-nth matchlist cnt)))))
386 ;; Now we know if they are all the same. If they are, just
387 ;; accept the first, otherwise complain.
389 (setq answer (semanticdb-find-result-nth-in-buffer
391 (setq answer "Not Unique"))
395 (setq answer "No Match")))
397 ;; Set it into our completion target.
398 (when (semantic-tag-p answer)
399 (setq semantic-complete-current-matched-tag answer)
400 ;; Make sure it is up to date by clearing it if the user dares
401 ;; to touch the keyboard.
402 (add-hook 'pre-command-hook
403 (lambda () (setq semantic-complete-current-matched-tag nil)))
412 ;; Keys are bound to to perform completion using our mechanisms.
413 ;; Do that work here.
414 (defun semantic-complete-done ()
415 "Accept the current input."
417 (let ((ans (semantic-complete-current-match)))
419 (semantic-completion-message (concat " [" ans "]"))
423 (defun semantic-complete-complete-space ()
424 "Complete the partial input in the minibuffer."
426 (semantic-complete-do-completion t))
428 (defun semantic-complete-complete-tab ()
429 "Complete the partial input in the minibuffer as far as possible."
431 (semantic-complete-do-completion))
433 ;;; Completion Functions
435 ;; Thees routines are functional entry points to performing completion.
437 (defun semantic-complete-hack-word-boundaries (original new)
438 "Return a string to use for completion.
439 ORIGINAL is the text in the minibuffer.
440 NEW is the new text to insert into the minibuffer.
441 Within the difference bounds of ORIGINAL and NEW, shorten NEW
442 to the nearest word boundary, and return that."
444 (let* ((diff (substring new (length original)))
445 (end (string-match "\\>" diff))
446 (start (string-match "\\<" diff)))
448 ((and start (> start 0))
449 ;; If start is greater than 0, include only the new
451 (concat original (substring diff 0 start)))
453 (concat original (substring diff 0 end)))
456 (defun semantic-complete-try-completion (&optional partial)
457 "Try a completion for the current minibuffer.
458 If PARTIAL, do partial completion stopping at spaces."
459 (let ((comp (semantic-collector-try-completion
460 semantic-completion-collector-engine
461 (semantic-completion-text))))
464 (semantic-completion-message " [No Match]")
468 (if (string= (semantic-completion-text) comp)
470 ;; Minibuffer isn't changing AND the text is not unique.
471 ;; Test for partial completion over a word separator character.
472 ;; If there is one available, use that so that SPC can
473 ;; act like a SPC insert key.
474 (let ((newcomp (semantic-collector-current-whitespace-completion
475 semantic-completion-collector-engine)))
477 (semantic-completion-delete-text)
481 (let ((orig (semantic-completion-text)))
482 ;; For partial completion, we stop and step over
483 ;; word boundaries. Use this nifty function to do
484 ;; that calculation for us.
486 (semantic-complete-hack-word-boundaries orig comp))))
487 ;; Do the replacement.
488 (semantic-completion-delete-text)
491 ((and (listp comp) (semantic-tag-p (car comp)))
492 (unless (string= (semantic-completion-text)
493 (semantic-tag-name (car comp)))
494 ;; A fully unique completion was available.
495 (semantic-completion-delete-text)
496 (insert (semantic-tag-name (car comp))))
497 ;; The match is complete
498 (if (= (length comp) 1)
499 (semantic-completion-message " [Complete]")
500 (semantic-completion-message " [Complete, but not unique]"))
504 (defun semantic-complete-do-completion (&optional partial inline)
505 "Do a completion for the current minibuffer.
506 If PARTIAL, do partial completion stopping at spaces.
507 if INLINE, then completion is happening inline in a buffer."
508 (let* ((collector semantic-completion-collector-engine)
509 (displayor semantic-completion-display-engine)
510 (contents (semantic-completion-text))
514 (semantic-collector-calculate-completions collector contents partial))
515 (let* ((na (semantic-complete-next-action partial)))
517 ;; We're all done, but only from a very specific
518 ;; area of completion.
520 (semantic-completion-message " [Complete]")
522 ;; Perform completion
523 ((or (eq na 'complete)
524 (eq na 'complete-whitespace))
525 (semantic-complete-try-completion partial)
526 (setq ans 'complete))
527 ;; We need to display the completions.
528 ;; Set the completions into the display engine
529 ((or (eq na 'display) (eq na 'displayend))
530 (semantic-displayor-set-completions
533 (and (not (eq na 'displayend))
534 (semantic-collector-current-exact-match collector))
535 (semantic-collector-all-completions collector contents))
537 ;; Ask the displayor to display them.
538 (semantic-displayor-show-request displayor))
540 (semantic-displayor-scroll-request displayor)
543 (semantic-displayor-focus-next displayor)
544 (semantic-displayor-focus-request displayor)
547 (semantic-completion-message " [No Match]"))
552 ;;; ------------------------------------------------------------
553 ;;; INLINE: tag completion harness
555 ;; Unlike the minibuffer, there is no mode nor other traditional
556 ;; means of reading user commands in completion mode. Instead
557 ;; we use a pre-command-hook to inset in our commands, and to
558 ;; push ourselves out of this mode on alternate keypresses.
559 (defvar semantic-complete-inline-map
560 (let ((km (make-sparse-keymap)))
561 (define-key km "\C-i" 'semantic-complete-inline-TAB)
562 (define-key km "\M-p" 'semantic-complete-inline-up)
563 (define-key km "\M-n" 'semantic-complete-inline-down)
564 (define-key km "\C-m" 'semantic-complete-inline-done)
565 (define-key km "\C-\M-c" 'semantic-complete-inline-exit)
566 (define-key km "\C-g" 'semantic-complete-inline-quit)
568 (lambda () (interactive)
569 (describe-variable 'semantic-complete-inline-map)))
571 "Keymap used while performing Semantic inline completion.
572 \\{semantic-complete-inline-map}")
574 (defface semantic-complete-inline-face
575 '((((class color) (background dark))
576 (:underline "yellow"))
577 (((class color) (background light))
578 (:underline "brown")))
579 "*Face used to show the region being completed inline.
580 The face is used in `semantic-complete-inline-tag-engine'."
581 :group 'semantic-faces)
583 (defun semantic-complete-inline-text ()
584 "Return the text that is being completed inline.
585 Similar to `minibuffer-contents' when completing in the minibuffer."
586 (let ((s (semantic-overlay-start semantic-complete-inline-overlay))
587 (e (semantic-overlay-end semantic-complete-inline-overlay)))
590 (buffer-substring-no-properties s e ))))
592 (defun semantic-complete-inline-delete-text ()
593 "Delete the text currently being completed in the current buffer."
595 (semantic-overlay-start semantic-complete-inline-overlay)
596 (semantic-overlay-end semantic-complete-inline-overlay)))
598 (defun semantic-complete-inline-done ()
599 "This completion thing is DONE, OR, insert a newline."
601 (let* ((displayor semantic-completion-display-engine)
602 (tag (semantic-displayor-current-focus displayor)))
604 (let ((txt (semantic-completion-text)))
605 (insert (substring (semantic-tag-name tag)
607 (semantic-complete-inline-exit))
609 ;; Get whatever binding RET usually has.
612 (lookup-key (current-active-maps) (this-command-keys))
614 ;; I don't know why, but for some reason the above
615 ;; throws an error sometimes.
616 (lookup-key (current-global-map) (this-command-keys))
622 (defun semantic-complete-inline-quit ()
623 "Quit an inline edit."
625 (semantic-complete-inline-exit)
628 (defun semantic-complete-inline-exit ()
629 "Exit inline completion mode."
631 ;; Remove this hook FIRST!
632 (remove-hook 'pre-command-hook 'semantic-complete-pre-command-hook)
636 (when semantic-completion-collector-engine
637 (semantic-collector-cleanup semantic-completion-collector-engine))
638 (when semantic-completion-display-engine
639 (semantic-displayor-cleanup semantic-completion-display-engine))
641 (when semantic-complete-inline-overlay
642 (let ((wc (semantic-overlay-get semantic-complete-inline-overlay
643 'window-config-start))
644 (buf (semantic-overlay-buffer semantic-complete-inline-overlay))
646 (semantic-overlay-delete semantic-complete-inline-overlay)
647 (setq semantic-complete-inline-overlay nil)
648 ;; DONT restore the window configuration if we just
650 (when (eq buf (current-buffer))
651 (set-window-configuration wc))
654 (setq semantic-completion-collector-engine nil
655 semantic-completion-display-engine nil))
658 ;; Remove this hook LAST!!!
659 ;; This will force us back through this function if there was
660 ;; some sort of error above.
661 (remove-hook 'post-command-hook 'semantic-complete-post-command-hook)
663 ;;(message "Exiting inline completion.")
666 (defun semantic-complete-pre-command-hook ()
667 "Used to redefine what commands are being run while completing.
668 When installed as a `pre-command-hook' the special keymap
669 `semantic-complete-inline-map' is queried to replace commands normally run.
670 Commands which edit what is in the region of interest operate normally.
671 Commands which would take us out of the region of interest, or our
672 quit hook, will exit this completion mode."
673 (let ((fcn (lookup-key semantic-complete-inline-map
674 (this-command-keys) nil)))
675 (cond ((commandp fcn)
676 (setq this-command fcn))
680 (defun semantic-complete-post-command-hook ()
681 "Used to determine if we need to exit inline completion mode.
682 If completion mode is active, check to see if we are within
683 the bounds of `semantic-complete-inline-overlay', or within
684 a reasonable distance."
686 ;; Exit if something bad happened.
687 (if (not semantic-complete-inline-overlay)
689 ;;(message "Inline Hook installed, but overlay deleted.")
690 (semantic-complete-inline-exit))
691 ;; Exit if commands caused us to exit the area of interest
692 (let ((s (semantic-overlay-start semantic-complete-inline-overlay))
693 (e (semantic-overlay-end semantic-complete-inline-overlay))
694 (b (semantic-overlay-buffer semantic-complete-inline-overlay))
698 ;; EXIT when we are no longer in a good place.
699 ((or (not (eq b (current-buffer)))
702 ;;(message "Exit: %S %S %S" s e (point))
703 (semantic-complete-inline-exit)
705 ;; Exit if the user typed in a character that is not part
706 ;; of the symbol being completed.
707 ((and (setq txt (semantic-completion-text))
708 (not (string= txt ""))
712 (not (looking-at "\\(\\w\\|\\s_\\)")))))
713 ;;(message "Non symbol character.")
714 (semantic-complete-inline-exit))
715 ((lookup-key semantic-complete-inline-map
716 (this-command-keys) nil)
717 ;; If the last command was one of our completion commands,
722 ;; Else, show completions now
723 (semantic-complete-inline-force-display)
726 ;; If something goes terribly wrong, clean up after ourselves.
727 (error (semantic-complete-inline-exit))))
729 (defun semantic-complete-inline-force-display ()
730 "Force the display of whatever the current completions are.
731 DO NOT CALL THIS IF THE INLINE COMPLETION ENGINE IS NOT ACTIVE."
734 (let ((collector semantic-completion-collector-engine)
735 (displayor semantic-completion-display-engine)
736 (contents (semantic-completion-text)))
738 (semantic-collector-calculate-completions
739 collector contents nil)
740 (semantic-displayor-set-completions
742 (semantic-collector-all-completions collector contents)
744 ;; Ask the displayor to display them.
745 (semantic-displayor-show-request displayor))
747 (error (message "Bug Showing Completions: %S" e))))
749 (defun semantic-complete-inline-tag-engine
750 (collector displayor buffer start end)
751 "Perform completion based on semantic tags in a buffer.
752 Argument COLLECTOR is an object which can be used to to calculate
753 a list of possible hits. See `semantic-completion-collector-engine'
754 for details on COLLECTOR.
755 Argumeng DISPLAYOR is an object used to display a list of possible
756 completions for a given prefix. See`semantic-completion-display-engine'
757 for details on DISPLAYOR.
758 BUFFER is the buffer in which completion will take place.
759 START is a location for the start of the full symbol.
760 If the symbol being completed is \"foo.ba\", then START
761 is on the \"f\" character.
762 END is at the end of the current symbol being completed."
763 ;; Set us up for doing completion
764 (setq semantic-completion-collector-engine collector
765 semantic-completion-display-engine displayor)
767 (setq semantic-complete-inline-overlay
768 (semantic-make-overlay start end buffer nil t))
769 (semantic-overlay-put semantic-complete-inline-overlay
771 'semantic-complete-inline-face)
772 (semantic-overlay-put semantic-complete-inline-overlay
774 (current-window-configuration))
775 ;; Install our command hooks
776 (add-hook 'pre-command-hook 'semantic-complete-pre-command-hook)
777 (add-hook 'post-command-hook 'semantic-complete-post-command-hook)
779 (semantic-complete-inline-force-display)
782 ;;; Inline Completion Keymap Functions
784 (defun semantic-complete-inline-TAB ()
785 "Perform inline completion."
787 (let ((cmpl (semantic-complete-do-completion nil t)))
790 (semantic-complete-inline-force-display))
792 (semantic-complete-inline-done))
796 (defun semantic-complete-inline-down()
797 "Focus forwards through the displayor."
799 (let ((displayor semantic-completion-display-engine))
800 (semantic-displayor-focus-next displayor)
801 (semantic-displayor-focus-request displayor)
804 (defun semantic-complete-inline-up ()
805 "Focus backwards through the displayor."
807 (let ((displayor semantic-completion-display-engine))
808 (semantic-displayor-focus-previous displayor)
809 (semantic-displayor-focus-request displayor)
813 ;;; ------------------------------------------------------------
814 ;;; Interactions between collection and displaying
816 ;; Functional routines used to help collectors communicate with
817 ;; the current displayor, or for the previous section.
819 (defun semantic-complete-next-action (partial)
820 "Determine what the next completion action should be.
821 PARTIAL is non-nil if we are doing partial completion.
822 First, the collector can determine if we should perform a completion or not.
823 If there is nothing to complete, then the displayor determines if we are
824 to show a completion list, scroll, or perhaps do a focus (if it is capable.)
825 Expected return values are:
826 done -> We have a singular match
827 empty -> There are no matches to the current text
828 complete -> Perform a completion action
829 complete-whitespace -> Complete next whitespace type character.
830 display -> Show the list of completions
831 scroll -> The completions have been shown, and the user keeps hitting
832 the complete button. If possible, scroll the completions
833 focus -> The displayor knows how to shift focus among possible completions.
835 displayend -> Whatever options the displayor had for repeating options, there
836 are none left. Try something new."
837 (let ((ans1 (semantic-collector-next-action
838 semantic-completion-collector-engine
840 (ans2 (semantic-displayor-next-action
841 semantic-completion-display-engine))
844 ;; No collector answer, use displayor answer.
847 ;; Displayor selection of 'scroll, 'display, or 'focus trumps
849 ((and (eq ans1 'done) ans2)
851 ;; Use ans1 when we have it.
857 ;;; ------------------------------------------------------------
858 ;;; Collection Engines
860 ;; Collection engines can scan tags from the current environment and
861 ;; provide lists of possible completions.
863 ;; General features of the abstract collector:
864 ;; * Cache completion lists between uses
865 ;; * Cache itself per buffer. Handle reparse hooks
867 ;; Key Interface Functions to implement:
868 ;; * semantic-collector-next-action
869 ;; * semantic-collector-calculate-completions
870 ;; * semantic-collector-try-completion
871 ;; * semantic-collector-all-completions
873 (defvar semantic-collector-per-buffer-list nil
874 "List of collectors active in this buffer.")
875 (make-variable-buffer-local 'semantic-collector-per-buffer-list)
877 (defvar semantic-collector-list nil
878 "List of global collectors active this session.")
880 (defclass semantic-collector-abstract ()
881 ((buffer :initarg :buffer
883 :documentation "Originating buffer for this collector.
884 Some collectors use a given buffer as a starting place while looking up
887 :type (or null semanticdb-find-result-with-nil)
888 :documentation "Cache of tags.
889 These tags are re-used during a completion session.
890 Sometimes these tags are cached between completion sessions.")
891 (last-all-completions :initarg nil
892 :type semanticdb-find-result-with-nil
893 :documentation "Last result of `all-completions'.
894 This result can be used for refined completions as `last-prefix' gets
895 closer to a specific result.")
896 (last-prefix :type string
897 :protection :protected
898 :documentation "The last queried prefix.
899 This prefix can be used to cache intermediate completion offers.
900 making the action of homing in on a token faster.")
901 (last-completion :type (or null string)
902 :documentation "The last calculated completion.
903 This completion is calculated and saved for future use.")
904 (last-whitespace-completion :type (or null string)
905 :documentation "The last whitespace completion.
906 For partial completion, SPC will disabiguate over whitespace type
907 characters. This is the last calculated version.")
908 (current-exact-match :type list
909 :protection :protected
910 :documentation "The list of matched tags.
911 When tokens are matched, they are added to this list.")
913 "Root class for completion engines.
914 The baseclass provides basic functionality for interacting with
915 a completion displayor object, and tracking the current progress
919 (defmethod semantic-collector-cleanup ((obj semantic-collector-abstract))
920 "Clean up any mess this collector may have."
923 (defmethod semantic-collector-next-action
924 ((obj semantic-collector-abstract) partial)
925 "What should we do next? OBJ can predict a next good action.
926 PARTIAL indicates if we are doing a partial completion."
927 (if (and (slot-boundp obj 'last-completion)
928 (string= (semantic-completion-text) (oref obj last-completion)))
929 (let* ((cem (semantic-collector-current-exact-match obj))
930 (cemlen (semanticdb-find-result-length cem))
931 (cac (semantic-collector-all-completions
932 obj (semantic-completion-text)))
933 (caclen (semanticdb-find-result-length cac)))
934 (cond ((and cem (= cemlen 1)
936 (eq last-command this-command))
937 ;; Defer to the displayor...
939 ((and cem (= cemlen 1))
941 ((and (not cem) (not cac))
943 ((and partial (semantic-collector-try-completion-whitespace
944 obj (semantic-completion-text)))
945 'complete-whitespace)))
948 (defmethod semantic-collector-last-prefix= ((obj semantic-collector-abstract)
950 "Return non-nil if OBJ's prefix matches PREFIX."
951 (and (slot-boundp obj 'last-prefix)
952 (string= (oref obj last-prefix) last-prefix)))
954 (defmethod semantic-collector-get-cache ((obj semantic-collector-abstract))
955 "Get the raw cache of tags for completion.
956 Calculate the cache if there isn't one."
958 (semantic-collector-calculate-cache obj)))
960 (defmethod semantic-collector-calculate-completions-raw
961 ((obj semantic-collector-abstract) prefix completionlist)
962 "Calculate the completions for prefix from completionlist.
963 Output must be in semanticdb Find result format."
964 ;; Must output in semanticdb format
965 (let ((table (save-excursion
966 (set-buffer (oref obj buffer))
967 semanticdb-current-table))
968 (result (semantic-find-tags-for-completion
970 ;; To do this kind of search with a pre-built completion
971 ;; list, we need to strip it first.
972 (semanticdb-strip-find-results completionlist)))
975 (list (cons table result)))))
977 (defmethod semantic-collector-calculate-completions
978 ((obj semantic-collector-abstract) prefix partial)
979 "Calculate completions for prefix as setup for other queries."
980 (let* ((case-fold-search semantic-case-fold)
981 (same-prefix-p (semantic-collector-last-prefix= obj prefix))
983 (if (or same-prefix-p
984 (and (slot-boundp obj 'last-prefix)
985 (eq (compare-strings (oref obj last-prefix) 0 nil
986 prefix 0 (length prefix))
988 ;; New prefix is subset of old prefix
989 (oref obj last-all-completions)
990 (semantic-collector-get-cache obj)))
992 (answer (if same-prefix-p
994 (semantic-collector-calculate-completions-raw
995 obj prefix completionlist))
998 (complete-not-uniq nil)
1000 ;;(semanticdb-find-result-test answer)
1001 (when (not same-prefix-p)
1002 ;; Save results if it is interesting and beneficial
1003 (oset obj last-prefix prefix)
1004 (oset obj last-all-completions answer))
1005 ;; Now calculate the completion.
1006 (setq completion (try-completion
1008 (semanticdb-strip-find-results answer)))
1009 (oset obj last-whitespace-completion nil)
1010 (oset obj current-exact-match nil)
1011 ;; Only do this if a completion was found. Letting a nil in
1012 ;; could cause a full semanticdb search by accident.
1014 (oset obj last-completion
1016 ;; Unique match in AC. Last completion is a match.
1017 ;; Also set the current-exact-match.
1019 (oset obj current-exact-match answer)
1021 ;; It may be complete (a symbol) but still not unique.
1022 ;; We can capture a match
1023 ((setq complete-not-uniq
1024 (semanticdb-find-tags-by-name
1027 (oset obj current-exact-match
1031 ;; Non unique match, return the string that handles
1033 (t (or completion prefix))
1037 (defmethod semantic-collector-try-completion-whitespace
1038 ((obj semantic-collector-abstract) prefix)
1039 "For OBJ, do whatepsace completion based on PREFIX.
1040 This implies that if there are two completions, one matching
1041 the test \"preifx\\>\", and one not, the one matching the full
1042 word version of PREFIX will be chosen, and that text returned.
1043 This function requires that `semantic-collector-calculate-completions'
1044 has been run first."
1045 (let* ((ac (semantic-collector-all-completions obj prefix))
1046 (matchme (concat "^" prefix "\\>"))
1047 (compare (semanticdb-find-tags-by-name-regexp matchme ac))
1048 (numtag (semanticdb-find-result-length compare))
1052 (cutlen (1+ (length prefix)))
1053 (twws (semanticdb-find-result-nth compare idx)))
1054 ;; Is our tag with whitespace a match that has whitespace
1055 ;; after it, or just an already complete symbol?
1056 (while (and (< idx numtag)
1057 (< (length (semantic-tag-name (car twws))) cutlen))
1059 twws (semanticdb-find-result-nth compare idx)))
1060 (when (and twws (car-safe twws))
1061 ;; If COMPARE has succeeded, then we should take the very
1062 ;; first match, and extend prefix by one character.
1063 (oset obj last-whitespace-completion
1064 (substring (semantic-tag-name (car twws))
1069 (defmethod semantic-collector-current-exact-match ((obj semantic-collector-abstract))
1070 "Return the active valid MATCH from the semantic collector.
1071 For now, just return the first element from our list of available
1072 matches. For semanticdb based results, make sure the file is loaded
1074 (when (slot-boundp obj 'current-exact-match)
1075 (oref obj current-exact-match)))
1077 (defmethod semantic-collector-current-whitespace-completion ((obj semantic-collector-abstract))
1078 "Return the active whitespace completion value."
1079 (when (slot-boundp obj 'last-whitespace-completion)
1080 (oref obj last-whitespace-completion)))
1082 (defmethod semantic-collector-get-match ((obj semantic-collector-abstract))
1083 "Return the active valid MATCH from the semantic collector.
1084 For now, just return the first element from our list of available
1085 matches. For semanticdb based results, make sure the file is loaded
1087 (when (slot-boundp obj 'current-exact-match)
1088 (semanticdb-find-result-nth-in-buffer (oref obj current-exact-match) 0)))
1090 (defmethod semantic-collector-all-completions
1091 ((obj semantic-collector-abstract) prefix)
1092 "For OBJ, retrieve all completions matching PREFIX.
1093 The returned list consists of all the tags currently
1095 (when (slot-boundp obj 'last-all-completions)
1096 (oref obj last-all-completions)))
1098 (defmethod semantic-collector-try-completion
1099 ((obj semantic-collector-abstract) prefix)
1100 "For OBJ, attempt to match PREFIX.
1101 See `try-completion' for details on how this works.
1102 Return nil for no match.
1103 Return a string for a partial match.
1104 For a unique match of PREFIX, return the list of all tags
1106 (if (slot-boundp obj 'last-completion)
1107 (oref obj last-completion)))
1109 (defmethod semantic-collector-calculate-cache
1110 ((obj semantic-collector-abstract))
1111 "Calculate the completion cache for OBJ."
1115 (defmethod semantic-collector-flush ((this semantic-collector-abstract))
1116 "Flush THIS collector object, clearing any caches and prefix."
1117 (oset this cache nil)
1118 (slot-makeunbound this 'last-prefix)
1119 (slot-makeunbound this 'last-completion)
1120 (slot-makeunbound this 'last-all-completions)
1121 (slot-makeunbound this 'current-exact-match)
1126 (defclass semantic-collector-buffer-abstract (semantic-collector-abstract)
1128 "Root class for per-buffer completion engines.
1129 These collectors track themselves on a per-buffer basis."
1132 (defmethod constructor :STATIC ((this semantic-collector-buffer-abstract)
1133 newname &rest fields)
1134 "Reuse previously created objects of this type in buffer."
1136 (bl semantic-collector-per-buffer-list))
1137 (while (and bl (null old))
1138 (if (eq (object-class (car bl)) this)
1139 (setq old (car bl))))
1141 (let ((new (call-next-method)))
1142 (add-to-list 'semantic-collector-per-buffer-list new)
1144 (slot-makeunbound old 'last-completion)
1145 (slot-makeunbound old 'last-prefix)
1146 (slot-makeunbound old 'current-exact-match)
1149 ;; Buffer specific collectors should flush themselves
1150 (defun semantic-collector-buffer-flush (newcache)
1151 "Flush all buffer collector objects.
1152 NEWCACHE is the new tag table, but we ignore it."
1154 (let ((l semantic-collector-per-buffer-list))
1156 (if (car l) (semantic-collector-flush (car l)))
1160 (add-hook 'semantic-after-toplevel-cache-change-hook
1161 'semantic-collector-buffer-flush)
1163 ;;; DEEP BUFFER SPECIFIC COMPLETION
1165 (defclass semantic-collector-buffer-deep
1166 (semantic-collector-buffer-abstract)
1168 "Completion engine for tags in the current buffer.
1169 When searching for a tag, uses semantic deep searche functions.
1170 Basics search only in the current buffer.")
1172 (defmethod semantic-collector-calculate-cache
1173 ((obj semantic-collector-buffer-deep))
1174 "Calculate the completion cache for OBJ.
1175 Uses `semantic-flatten-tags-table'"
1177 ;; Must create it in SEMANTICDB find format.
1178 ;; ( ( DBTABLE TAG TAG ... ) ... )
1180 (cons semanticdb-current-table
1181 (semantic-flatten-tags-table (oref obj buffer))))))
1183 ;;; PROJECT SPECIFIC COMPLETION
1185 (defclass semantic-collector-project-abstract (semantic-collector-abstract)
1186 ((path :initarg :path
1188 :documentation "List of database tables to search.
1189 At creation time, it can be anything accepted by
1190 `semanticdb-find-translate-path' as a PATH argument.")
1192 "Root class for project wide completion engines.
1193 Uses semanticdb for searching all tags in the current project."
1197 (defclass semantic-collector-project (semantic-collector-project-abstract)
1199 "Completion engine for tags in a project.")
1202 (defmethod semantic-collector-calculate-completions-raw
1203 ((obj semantic-collector-project) prefix completionlist)
1204 "Calculate the completions for prefix from completionlist."
1205 (semanticdb-find-tags-for-completion prefix (oref obj path)))
1207 ;;; Brutish Project search
1208 (defclass semantic-collector-project-brutish (semantic-collector-project-abstract)
1210 "Completion engine for tags in a project.")
1212 (declare-function semanticdb-brute-deep-find-tags-for-completion
1215 (defmethod semantic-collector-calculate-completions-raw
1216 ((obj semantic-collector-project-brutish) prefix completionlist)
1217 "Calculate the completions for prefix from completionlist."
1218 (require 'semantic/db-find)
1219 (semanticdb-brute-deep-find-tags-for-completion prefix (oref obj path)))
1221 (defclass semantic-collector-analyze-completions (semantic-collector-abstract)
1222 ((context :initarg :context
1223 :type semantic-analyze-context
1224 :documentation "An analysis context.
1225 Specifies some context location from whence completion lists will be drawn."
1227 (first-pass-completions :type list
1228 :documentation "List of valid completion tags.
1229 This list of tags is generated when completion starts. All searches
1230 derive from this list.")
1232 "Completion engine that uses the context analyzer to provide options.
1233 The only options available for completion are those which can be logically
1234 inserted into the current context.")
1236 (defmethod semantic-collector-calculate-completions-raw
1237 ((obj semantic-collector-analyze-completions) prefix completionlist)
1238 "calculate the completions for prefix from completionlist."
1239 ;; if there are no completions yet, calculate them.
1240 (if (not (slot-boundp obj 'first-pass-completions))
1241 (oset obj first-pass-completions
1242 (semantic-analyze-possible-completions (oref obj context))))
1243 ;; search our cached completion list. make it look like a semanticdb
1245 (list (cons (save-excursion
1246 (set-buffer (oref (oref obj context) buffer))
1247 semanticdb-current-table)
1248 (semantic-find-tags-for-completion
1250 (oref obj first-pass-completions)))))
1253 ;;; ------------------------------------------------------------
1254 ;;; Tag List Display Engines
1256 ;; A typical displayor accepts a pre-determined list of completions
1257 ;; generated by a collector. This format is in semanticdb search
1258 ;; form. This vaguely standard form is a bit challenging to navigate
1259 ;; because the tags do not contain buffer info, but the file assocated
1260 ;; with the tags preceed the tag in the list.
1262 ;; Basic displayors don't care, and can strip the results.
1263 ;; Advanced highlighting displayors need to know when they need
1264 ;; to load a file so that the tag in question can be highlighted.
1266 ;; Key interface methods to a displayor are:
1267 ;; * semantic-displayor-next-action
1268 ;; * semantic-displayor-set-completions
1269 ;; * semantic-displayor-current-focus
1270 ;; * semantic-displayor-show-request
1271 ;; * semantic-displayor-scroll-request
1272 ;; * semantic-displayor-focus-request
1274 (defclass semantic-displayor-abstract ()
1275 ((table :type (or null semanticdb-find-result-with-nil)
1277 :protection :protected
1278 :documentation "List of tags this displayor is showing.")
1279 (last-prefix :type string
1280 :protection :protected
1281 :documentation "Prefix associated with slot `table'")
1283 "Abstract displayor baseclass.
1284 Manages the display of some number of tags.
1285 Provides the basics for a displayor, including interacting with
1286 a collector, and tracking tables of completion to display."
1289 (defmethod semantic-displayor-cleanup ((obj semantic-displayor-abstract))
1290 "Clean up any mess this displayor may have."
1293 (defmethod semantic-displayor-next-action ((obj semantic-displayor-abstract))
1294 "The next action to take on the minibuffer related to display."
1295 (if (and (slot-boundp obj 'last-prefix)
1296 (string= (oref obj last-prefix) (semantic-completion-text))
1297 (eq last-command this-command))
1301 (defmethod semantic-displayor-set-completions ((obj semantic-displayor-abstract)
1303 "Set the list of tags to be completed over to TABLE."
1304 (oset obj table table)
1305 (oset obj last-prefix prefix))
1307 (defmethod semantic-displayor-show-request ((obj semantic-displayor-abstract))
1308 "A request to show the current tags table."
1311 (defmethod semantic-displayor-focus-request ((obj semantic-displayor-abstract))
1312 "A request to for the displayor to focus on some tag option."
1315 (defmethod semantic-displayor-scroll-request ((obj semantic-displayor-abstract))
1316 "A request to for the displayor to scroll the completion list (if needed)."
1317 (scroll-other-window))
1319 (defmethod semantic-displayor-focus-previous ((obj semantic-displayor-abstract))
1320 "Set the current focus to the previous item."
1323 (defmethod semantic-displayor-focus-next ((obj semantic-displayor-abstract))
1324 "Set the current focus to the next item."
1327 (defmethod semantic-displayor-current-focus ((obj semantic-displayor-abstract))
1328 "Return a single tag currently in focus.
1329 This object type doesn't do focus, so will never have a focus object."
1332 ;; Traditional displayor
1333 (defcustom semantic-completion-displayor-format-tag-function
1334 #'semantic-format-tag-name
1335 "*A Tag format function to use when showing completions."
1337 :type semantic-format-tag-custom-list)
1339 (defclass semantic-displayor-traditional (semantic-displayor-abstract)
1341 "Display options in *Completions* buffer.
1342 Traditional display mechanism for a list of possible completions.
1343 Completions are showin in a new buffer and listed with the ability
1344 to click on the items to aid in completion.")
1346 (defmethod semantic-displayor-show-request ((obj semantic-displayor-traditional))
1347 "A request to show the current tags table."
1349 ;; NOTE TO SELF. Find the character to type next, and emphesize it.
1351 (with-output-to-temp-buffer "*Completions*"
1352 (display-completion-list
1353 (mapcar semantic-completion-displayor-format-tag-function
1354 (semanticdb-strip-find-results (oref obj table))))
1358 ;;; Abstract baseclass for any displayor which supports focus
1359 (defclass semantic-displayor-focus-abstract (semantic-displayor-abstract)
1360 ((focus :type number
1361 :protection :protected
1362 :documentation "A tag index from `table' which has focus.
1363 Multiple calls to the display function can choose to focus on a
1364 given tag, by highlighting its location.")
1369 "Non-nil if focusing requires a tag's buffer be in memory.")
1371 "Abstract displayor supporting `focus'.
1372 A displayor which has the ability to focus in on one tag.
1373 Focusing is a way of differentiationg between multiple tags
1374 which have the same name."
1377 (defmethod semantic-displayor-next-action ((obj semantic-displayor-focus-abstract))
1378 "The next action to take on the minibuffer related to display."
1379 (if (and (slot-boundp obj 'last-prefix)
1380 (string= (oref obj last-prefix) (semantic-completion-text))
1381 (eq last-command this-command))
1383 (slot-boundp obj 'focus)
1384 (slot-boundp obj 'table)
1385 (<= (semanticdb-find-result-length (oref obj table))
1386 (1+ (oref obj focus))))
1387 ;; We are at the end of the focus road.
1389 ;; Focus on some item.
1393 (defmethod semantic-displayor-set-completions ((obj semantic-displayor-focus-abstract)
1395 "Set the list of tags to be completed over to TABLE."
1397 (slot-makeunbound obj 'focus))
1399 (defmethod semantic-displayor-focus-previous ((obj semantic-displayor-focus-abstract))
1400 "Set the current focus to the previous item.
1401 Not meaningful return value."
1402 (when (and (slot-boundp obj 'table) (oref obj table))
1403 (with-slots (table) obj
1404 (if (or (not (slot-boundp obj 'focus))
1405 (<= (oref obj focus) 0))
1406 (oset obj focus (1- (semanticdb-find-result-length table)))
1407 (oset obj focus (1- (oref obj focus)))
1411 (defmethod semantic-displayor-focus-next ((obj semantic-displayor-focus-abstract))
1412 "Set the current focus to the next item.
1413 Not meaningful return value."
1414 (when (and (slot-boundp obj 'table) (oref obj table))
1415 (with-slots (table) obj
1416 (if (not (slot-boundp obj 'focus))
1418 (oset obj focus (1+ (oref obj focus)))
1420 (if (<= (semanticdb-find-result-length table) (oref obj focus))
1424 (defmethod semantic-displayor-focus-tag ((obj semantic-displayor-focus-abstract))
1425 "Return the next tag OBJ should focus on."
1426 (when (and (slot-boundp obj 'table) (oref obj table))
1427 (with-slots (table) obj
1428 (semanticdb-find-result-nth table (oref obj focus)))))
1430 (defmethod semantic-displayor-current-focus ((obj semantic-displayor-focus-abstract))
1431 "Return the tag currently in focus, or call parent method."
1432 (if (and (slot-boundp obj 'focus)
1433 (slot-boundp obj 'table)
1434 ;; Only return the current focus IFF the minibuffer reflects
1435 ;; the list this focus was derived from.
1436 (slot-boundp obj 'last-prefix)
1437 (string= (semantic-completion-text) (oref obj last-prefix))
1440 (if (oref obj find-file-focus)
1441 (semanticdb-find-result-nth-in-buffer (oref obj table) (oref obj focus))
1442 ;; result-nth returns a cons with car being the tag, and cdr the
1444 (car (semanticdb-find-result-nth (oref obj table) (oref obj focus))))
1446 (call-next-method)))
1448 ;;; Simple displayor which performs traditional display completion,
1449 ;; and also focuses with highlighting.
1450 (defclass semantic-displayor-traditional-with-focus-highlight
1451 (semantic-displayor-focus-abstract semantic-displayor-traditional)
1452 ((find-file-focus :initform t))
1453 "Display completions in *Completions* buffer, with focus highlight.
1454 A traditional displayor which can focus on a tag by showing it.
1455 Same as `semantic-displayor-traditional', but with selection between
1456 multiple tags with the same name done by 'focusing' on the source
1457 location of the different tags to differentiate them.")
1459 (defmethod semantic-displayor-focus-request
1460 ((obj semantic-displayor-traditional-with-focus-highlight))
1461 "Focus in on possible tag completions.
1462 Focus is performed by cycling through the tags and highlighting
1463 one in the source buffer."
1464 (let* ((tablelength (semanticdb-find-result-length (oref obj table)))
1465 (focus (semantic-displayor-focus-tag obj))
1468 (rtable (cdr focus))
1470 (nt (semanticdb-normalize-one-tag rtable rtag))
1474 ;; If we fail to normalize, resete.
1475 (when (not tag) (setq table rtable tag rtag))
1477 (let ((buf (or (semantic-tag-buffer tag)
1478 (and table (semanticdb-get-buffer table)))))
1479 ;; If no buffer is provided, then we can make up a summary buffer.
1482 (set-buffer (get-buffer-create "*Completion Focus*"))
1484 (insert "Focus on tag: \n")
1485 (insert (semantic-format-tag-summarize tag nil t) "\n\n")
1487 (insert "From table: \n")
1488 (insert (object-name table) "\n\n"))
1490 (insert "In buffer: \n\n")
1491 (insert (format "%S" buf)))
1492 (setq buf (current-buffer))))
1493 ;; Show the tag in the buffer.
1494 (if (get-buffer-window buf)
1495 (select-window (get-buffer-window buf))
1496 (switch-to-buffer-other-window buf t)
1497 (select-window (get-buffer-window buf)))
1498 ;; Now do some positioning
1500 (if (semantic-tag-with-position-p tag)
1501 ;; Full tag positional information available
1503 (goto-char (semantic-tag-start tag))
1504 ;; This avoids a dangerous problem if we just loaded a tag
1505 ;; from a file, but the original position was not updated
1506 ;; in the TAG variable we are currently using.
1507 (semantic-momentary-highlight-tag (semantic-current-tag))
1509 (select-window (minibuffer-window)))
1510 ;; Calculate text difference between contents and the focus item.
1511 (let* ((mbc (semantic-completion-text))
1512 (ftn (semantic-tag-name tag))
1513 (diff (substring ftn (length mbc))))
1514 (semantic-completion-message
1515 (format "%s [%d of %d matches]" diff (1+ (oref obj focus)) tablelength)))
1519 ;;; Tooltip completion lister
1521 ;; Written and contributed by Masatake YAMATO <jet@gyve.org>
1523 ;; Modified by Eric Ludlam for
1524 ;; * Safe compatibility for tooltip free systems.
1525 ;; * Don't use 'avoid package for tooltip positioning.
1527 (defclass semantic-displayor-tooltip (semantic-displayor-traditional)
1528 ((max-tags :type integer
1533 "Max number of tags displayed on tooltip at once.
1534 If `force-show' is 1, this value is ignored with typing tab or space twice continuously.
1535 if `force-show' is 0, this value is always ignored.")
1536 (force-show :type integer
1537 :initarg :force-show
1539 :custom (choice (const
1540 :tag "Show when double typing"
1546 :tag "Show if the number of tags is less than `max-tags'."
1549 "Control the behavior of the number of tags is greater than `max-tags'.
1550 -1 means tags are never shown.
1551 0 means the tags are always shown.
1552 1 means tags are shown if space or tab is typed twice continuously.")
1553 (typing-count :type integer
1556 "Counter holding how many times the user types space or tab continuously before showing tags.")
1557 (shown :type boolean
1560 "Flag representing whether tags is shown once or not.")
1562 "Display completions options in a tooltip.
1563 Display mechanism using tooltip for a list of possible completions.")
1565 (defmethod initialize-instance :AFTER ((obj semantic-displayor-tooltip) &rest args)
1566 "Make sure we have tooltips required."
1572 (defmethod semantic-displayor-show-request ((obj semantic-displayor-tooltip))
1573 "A request to show the current tags table."
1574 (if (or (not (featurep 'tooltip)) (not tooltip-mode))
1575 ;; If we cannot use tooltips, then go to the normal mode with
1576 ;; a traditional completion buffer.
1578 (let* ((tablelong (semanticdb-strip-find-results (oref obj table)))
1579 (table (semantic-unique-tag-table-by-name tablelong))
1580 (l (mapcar semantic-completion-displayor-format-tag-function table))
1582 (typing-count (oref obj typing-count))
1583 (force-show (oref obj force-show))
1584 (matchtxt (semantic-completion-text))
1586 (if (or (oref obj shown)
1587 (< ll (oref obj max-tags))
1588 (and (<= 0 force-show)
1589 (< (1- force-show) typing-count)))
1591 (oset obj typing-count 0)
1594 ;; We Have only one possible match. There could be two cases.
1595 ;; 1) input text != single match.
1597 ;; 2) input text == single match.
1598 ;; --> Complain about it, but still show the match.
1599 (if (string= matchtxt (semantic-tag-name (car table)))
1600 (setq msg (concat "[COMPLETE]\n" (car l)))
1602 ;; Create the long message.
1603 (setq msg (mapconcat 'identity l "\n"))
1604 ;; If there is nothing, say so!
1605 (if (eq 0 (length msg))
1606 (setq msg "[NO MATCH]")))
1607 (semantic-displayor-tooltip-show msg))
1608 ;; The typing count determines if the user REALLY REALLY
1609 ;; wanted to show that much stuff. Only increment
1610 ;; if the current command is a completion command.
1611 (if (and (stringp (this-command-keys))
1612 (string= (this-command-keys) "\C-i"))
1613 (oset obj typing-count (1+ typing-count)))
1614 ;; At this point, we know we have too many items.
1615 ;; Lets be brave, and truncate l
1616 (setcdr (nthcdr (oref obj max-tags) l) nil)
1617 (setq msg (mapconcat 'identity l "\n"))
1620 (semantic-displayor-tooltip-show (concat msg "\n...")))
1622 (semantic-displayor-tooltip-show (concat msg "\n(TAB for more)")))
1628 (if (fboundp 'window-inside-edges)
1630 (defalias 'semantic-displayor-window-edges
1631 'window-inside-edges)
1633 (defalias 'semantic-displayor-window-edges
1637 (defun semantic-displayor-point-position ()
1638 "Return the location of POINT as positioned on the selected frame.
1639 Return a cons cell (X . Y)"
1640 (let* ((frame (selected-frame))
1641 (left (frame-parameter frame 'left))
1642 (top (frame-parameter frame 'top))
1643 (point-pix-pos (posn-x-y (posn-at-point)))
1644 (edges (window-inside-pixel-edges (selected-window))))
1645 (cons (+ (car point-pix-pos) (car edges) left)
1646 (+ (cdr point-pix-pos) (cadr edges) top))))
1649 (defun semantic-displayor-tooltip-show (text)
1650 "Display a tooltip with TEXT near cursor."
1651 (let ((point-pix-pos (semantic-displayor-point-position))
1652 (tooltip-frame-parameters
1653 (append tooltip-frame-parameters nil)))
1655 (cons 'left (+ (car point-pix-pos) (frame-char-width)))
1656 tooltip-frame-parameters)
1658 (cons 'top (+ (cdr point-pix-pos) (frame-char-height)))
1659 tooltip-frame-parameters)
1660 (tooltip-show text)))
1662 (defmethod semantic-displayor-scroll-request ((obj semantic-displayor-tooltip))
1663 "A request to for the displayor to scroll the completion list (if needed)."
1664 ;; Do scrolling in the tooltip.
1665 (oset obj max-tags 30)
1666 (semantic-displayor-show-request obj)
1669 ;; End code contributed by Masatake YAMATO <jet@gyve.org>
1672 ;;; Ghost Text displayor
1674 (defclass semantic-displayor-ghost (semantic-displayor-focus-abstract)
1676 ((ghostoverlay :type overlay
1678 "The overlay the ghost text is displayed in.")
1679 (first-show :initform t
1681 "Non nil if we have not seen our first show request.")
1683 "Cycle completions inline with ghost text.
1684 Completion displayor using ghost chars after point for focus options.
1685 Whichever completion is currently in focus will be displayed as ghost
1686 text using overlay options.")
1688 (defmethod semantic-displayor-next-action ((obj semantic-displayor-ghost))
1689 "The next action to take on the inline completion related to display."
1690 (let ((ans (call-next-method))
1691 (table (when (slot-boundp obj 'table)
1693 (if (and (eq ans 'displayend)
1695 (= (semanticdb-find-result-length table) 1)
1700 (defmethod semantic-displayor-cleanup ((obj semantic-displayor-ghost))
1701 "Clean up any mess this displayor may have."
1702 (when (slot-boundp obj 'ghostoverlay)
1703 (semantic-overlay-delete (oref obj ghostoverlay)))
1706 (defmethod semantic-displayor-set-completions ((obj semantic-displayor-ghost)
1708 "Set the list of tags to be completed over to TABLE."
1711 (semantic-displayor-cleanup obj)
1715 (defmethod semantic-displayor-show-request ((obj semantic-displayor-ghost))
1716 "A request to show the current tags table."
1717 ; (if (oref obj first-show)
1719 ; (oset obj first-show nil)
1720 (semantic-displayor-focus-next obj)
1721 (semantic-displayor-focus-request obj)
1723 ;; Only do the traditional thing if the first show request
1724 ;; has been seen. Use the first one to start doing the ghost
1726 ; (call-next-method)
1730 (defmethod semantic-displayor-focus-request
1731 ((obj semantic-displayor-ghost))
1732 "Focus in on possible tag completions.
1733 Focus is performed by cycling through the tags and showing a possible
1734 completion text in ghost text."
1735 (let* ((tablelength (semanticdb-find-result-length (oref obj table)))
1736 (focus (semantic-displayor-focus-tag obj))
1740 (semantic-completion-message "No tags to focus on.")
1741 ;; Display the focus completion as ghost text after the current
1743 (when (or (not (slot-boundp obj 'ghostoverlay))
1744 (not (semantic-overlay-live-p (oref obj ghostoverlay))))
1745 (oset obj ghostoverlay
1746 (semantic-make-overlay (point) (1+ (point)) (current-buffer) t)))
1748 (let* ((lp (semantic-completion-text))
1749 (os (substring (semantic-tag-name tag) (length lp)))
1750 (ol (oref obj ghostoverlay))
1753 (put-text-property 0 (length os) 'face 'region os)
1755 (semantic-overlay-put
1756 ol 'display (concat os (buffer-substring (point) (1+ (point)))))
1758 ;; Calculate text difference between contents and the focus item.
1759 (let* ((mbc (semantic-completion-text))
1760 (ftn (concat (semantic-tag-name tag)))
1762 (put-text-property (length mbc) (length ftn) 'face
1764 (semantic-completion-message
1765 (format "%s [%d of %d matches]" ftn (1+ (oref obj focus)) tablelength)))
1769 ;;; ------------------------------------------------------------
1770 ;;; Specific queries
1772 (defvar semantic-complete-inline-custom-type
1776 (let* ((C (intern (car class)))
1777 (doc (documentation-property C 'variable-documentation))
1778 (doc1 (car (split-string doc "\n")))
1783 (eieio-build-class-alist semantic-displayor-abstract t))
1785 "Possible options for inlince completion displayors.
1786 Use this to enable custom editing.")
1788 (defcustom semantic-complete-inline-analyzer-displayor-class
1789 'semantic-displayor-traditional
1790 "*Class for displayor to use with inline completion."
1792 :type semantic-complete-inline-custom-type
1795 (defun semantic-complete-read-tag-buffer-deep (prompt &optional
1799 "Ask for a tag by name from the current buffer.
1800 Available tags are from the current buffer, at any level.
1801 Completion options are presented in a traditional way, with highlighting
1802 to resolve same-name collisions.
1803 PROMPT is a string to prompt with.
1804 DEFAULT-TAG is a semantic tag or string to use as the default value.
1805 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.
1806 HISTORY is a symbol representing a variable to store the history in."
1807 (semantic-complete-read-tag-engine
1808 (semantic-collector-buffer-deep prompt :buffer (current-buffer))
1809 (semantic-displayor-traditional-with-focus-highlight "simple")
1810 ;;(semantic-displayor-tooltip "simple")
1817 (defun semantic-complete-read-tag-project (prompt &optional
1821 "Ask for a tag by name from the current project.
1822 Available tags are from the current project, at the top level.
1823 Completion options are presented in a traditional way, with highlighting
1824 to resolve same-name collisions.
1825 PROMPT is a string to prompt with.
1826 DEFAULT-TAG is a semantic tag or string to use as the default value.
1827 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.
1828 HISTORY is a symbol representing a variable to store the history in."
1829 (semantic-complete-read-tag-engine
1830 (semantic-collector-project-brutish prompt
1831 :buffer (current-buffer)
1832 :path (current-buffer)
1834 (semantic-displayor-traditional-with-focus-highlight "simple")
1841 (defun semantic-complete-inline-tag-project ()
1842 "Complete a symbol name by name from within the current project.
1843 This is similar to `semantic-complete-read-tag-project', except
1844 that the completion interaction is in the buffer where the context
1845 was calculated from.
1846 Customize `semantic-complete-inline-analyzer-displayor-class'
1847 to control how completion options are displayed.
1848 See `semantic-complete-inline-tag-engine' for details on how
1850 (let* ((collector (semantic-collector-project-brutish
1852 :buffer (current-buffer)
1853 :path (current-buffer)))
1854 (sbounds (semantic-ctxt-current-symbol-and-bounds))
1855 (syms (car sbounds))
1856 (start (car (nth 2 sbounds)))
1857 (end (cdr (nth 2 sbounds)))
1858 (rsym (reverse syms))
1859 (thissym (nth 1 sbounds))
1860 (nextsym (car-safe (cdr rsym)))
1862 (when (and thissym (or (not (string= thissym ""))
1864 ;; Do a quick calcuation of completions.
1865 (semantic-collector-calculate-completions
1866 collector thissym nil)
1867 ;; Get the master list
1868 (setq complst (semanticdb-strip-find-results
1869 (semantic-collector-all-completions collector thissym)))
1871 (setq complst (semantic-unique-tag-table-by-name complst))
1872 (if (or (and (= (length complst) 1)
1873 ;; Check to see if it is the same as what is there.
1874 ;; if so, we can offer to complete.
1875 (let ((compname (semantic-tag-name (car complst))))
1876 (not (string= compname thissym))))
1877 (> (length complst) 1))
1878 ;; There are several options. Do the completion.
1879 (semantic-complete-inline-tag-engine
1881 (funcall semantic-complete-inline-analyzer-displayor-class
1883 ;;(semantic-displayor-tooltip "simple")
1888 (defun semantic-complete-read-tag-analyzer (prompt &optional
1891 "Ask for a tag by name based on the current context.
1892 The function `semantic-analyze-current-context' is used to
1893 calculate the context. `semantic-analyze-possible-completions' is used
1894 to generate the list of possible completions.
1895 PROMPT is the first part of the prompt. Additional prompt
1896 is added based on the contexts full prefix.
1897 CONTEXT is the semantic analyzer context to start with.
1898 HISTORY is a symbol representing a variable to stor the history in.
1899 usually a default-tag and initial-input are available for completion
1900 prompts. these are calculated from the CONTEXT variable passed in."
1901 (if (not context) (setq context (semantic-analyze-current-context (point))))
1902 (let* ((syms (semantic-ctxt-current-symbol (point)))
1903 (inp (car (reverse syms))))
1904 (setq syms (nreverse (cdr (nreverse syms))))
1905 (semantic-complete-read-tag-engine
1906 (semantic-collector-analyze-completions
1908 :buffer (oref context buffer)
1910 (semantic-displayor-traditional-with-focus-highlight "simple")
1912 (set-buffer (oref context buffer))
1913 (goto-char (cdr (oref context bounds)))
1914 (concat prompt (mapconcat 'identity syms ".")
1921 (defun semantic-complete-inline-analyzer (context)
1922 "Complete a symbol name by name based on the current context.
1923 This is similar to `semantic-complete-read-tag-analyze', except
1924 that the completion interaction is in the buffer where the context
1925 was calculated from.
1926 CONTEXT is the semantic analyzer context to start with.
1927 Customize `semantic-complete-inline-analyzer-displayor-class'
1928 to control how completion options are displayed.
1930 See `semantic-complete-inline-tag-engine' for details on how
1932 (if (not context) (setq context (semantic-analyze-current-context (point))))
1933 (if (not context) (error "Nothing to complete on here"))
1934 (let* ((collector (semantic-collector-analyze-completions
1936 :buffer (oref context buffer)
1938 (syms (semantic-ctxt-current-symbol (point)))
1939 (rsym (reverse syms))
1940 (thissym (car rsym))
1941 (nextsym (car-safe (cdr rsym)))
1943 (when (and thissym (or (not (string= thissym ""))
1945 ;; Do a quick calcuation of completions.
1946 (semantic-collector-calculate-completions
1947 collector thissym nil)
1948 ;; Get the master list
1949 (setq complst (semanticdb-strip-find-results
1950 (semantic-collector-all-completions collector thissym)))
1952 (setq complst (semantic-unique-tag-table-by-name complst))
1953 (if (or (and (= (length complst) 1)
1954 ;; Check to see if it is the same as what is there.
1955 ;; if so, we can offer to complete.
1956 (let ((compname (semantic-tag-name (car complst))))
1957 (not (string= compname thissym))))
1958 (> (length complst) 1))
1959 ;; There are several options. Do the completion.
1960 (semantic-complete-inline-tag-engine
1962 (funcall semantic-complete-inline-analyzer-displayor-class
1964 ;;(semantic-displayor-tooltip "simple")
1965 (oref context buffer)
1966 (car (oref context bounds))
1967 (cdr (oref context bounds))
1971 (defcustom semantic-complete-inline-analyzer-idle-displayor-class
1972 'semantic-displayor-ghost
1973 "*Class for displayor to use with inline completion at idle time."
1975 :type semantic-complete-inline-custom-type
1978 (defun semantic-complete-inline-analyzer-idle (context)
1979 "Complete a symbol name by name based on the current context for idle time.
1980 CONTEXT is the semantic analyzer context to start with.
1981 This function is used from `semantic-idle-completions-mode'.
1983 This is the same as `semantic-complete-inline-analyzer', except that
1984 it uses `semantic-complete-inline-analyzer-idle-displayor-class'
1985 to control how completions are displayed.
1987 See `semantic-complete-inline-tag-engine' for details on how
1989 (let ((semantic-complete-inline-analyzer-displayor-class
1990 semantic-complete-inline-analyzer-idle-displayor-class))
1991 (semantic-complete-inline-analyzer context)
1995 ;;; ------------------------------------------------------------
1998 (defun semantic-complete-test ()
1999 "Test completion mechanisms."
2002 (semantic-format-tag-prototype
2003 (semantic-complete-read-tag-project "Symbol: ")
2007 (defun semantic-complete-jump-local ()
2008 "Jump to a semantic symbol."
2010 (let ((tag (semantic-complete-read-tag-buffer-deep "Symbol: ")))
2011 (when (semantic-tag-p tag)
2013 (goto-char (semantic-tag-start tag))
2014 (semantic-momentary-highlight-tag tag)
2016 (semantic-tag-class tag)
2017 (semantic-tag-name tag)))))
2020 (defun semantic-complete-jump ()
2021 "Jump to a semantic symbol."
2023 (let* ((tag (semantic-complete-read-tag-project "Symbol: ")))
2024 (when (semantic-tag-p tag)
2026 (semantic-go-to-tag tag)
2027 (switch-to-buffer (current-buffer))
2028 (semantic-momentary-highlight-tag tag)
2030 (semantic-tag-class tag)
2031 (semantic-tag-name tag)))))
2034 (defun semantic-complete-analyze-and-replace ()
2035 "Perform prompt completion to do in buffer completion.
2036 `semantic-analyze-possible-completions' is used to determine the
2038 The minibuffer is used to perform the completion.
2039 The result is inserted as a replacement of the text that was there."
2041 (let* ((c (semantic-analyze-current-context (point)))
2042 (tag (save-excursion (semantic-complete-read-tag-analyzer "" c))))
2043 ;; Take tag, and replace context bound with its name.
2044 (goto-char (car (oref c bounds)))
2045 (delete-region (point) (cdr (oref c bounds)))
2046 (insert (semantic-tag-name tag))
2047 (message "%S" (semantic-format-tag-summarize tag))))
2050 (defun semantic-complete-analyze-inline ()
2051 "Perform prompt completion to do in buffer completion.
2052 `semantic-analyze-possible-completions' is used to determine the
2054 The function returns immediately, leaving the buffer in a mode that
2055 will perform the completion.
2056 Configure `semantic-complete-inline-analyzer-displayor-class' to change
2057 how completion options are displayed."
2059 ;; Only do this if we are not already completing something.
2060 (if (not (semantic-completion-inline-active-p))
2061 (semantic-complete-inline-analyzer
2062 (semantic-analyze-current-context (point))))
2063 ;; Report a message if things didn't startup.
2064 (if (and (interactive-p)
2065 (not (semantic-completion-inline-active-p)))
2066 (message "Inline completion not needed.")
2067 ;; Since this is most likely bound to something, and not used
2068 ;; at idle time, throw in a TAB for good measure.
2069 (semantic-complete-inline-TAB)
2073 (defun semantic-complete-analyze-inline-idle ()
2074 "Perform prompt completion to do in buffer completion.
2075 `semantic-analyze-possible-completions' is used to determine the
2077 The function returns immediately, leaving the buffer in a mode that
2078 will perform the completion.
2079 Configure `semantic-complete-inline-analyzer-idle-displayor-class'
2080 to change how completion options are displayed."
2082 ;; Only do this if we are not already completing something.
2083 (if (not (semantic-completion-inline-active-p))
2084 (semantic-complete-inline-analyzer-idle
2085 (semantic-analyze-current-context (point))))
2086 ;; Report a message if things didn't startup.
2087 (if (and (interactive-p)
2088 (not (semantic-completion-inline-active-p)))
2089 (message "Inline completion not needed."))
2093 (defun semantic-complete-self-insert (arg)
2094 "Like `self-insert-command', but does completion afterwards.
2095 ARG is passed to `self-insert-command'. If ARG is nil,
2096 use `semantic-complete-analyze-inline' to complete."
2098 ;; If we are already in a completion scenario, exit now, and then start over.
2099 (semantic-complete-inline-exit)
2102 (self-insert-command arg)
2104 ;; Prepare for doing completion, but exit quickly if there is keyboard
2106 (when (and (not (semantic-exit-on-input 'csi
2107 (semantic-fetch-tags)
2108 (semantic-throw-on-input 'csi)
2111 (not (semantic-exit-on-input 'csi
2112 (semantic-analyze-current-context)
2113 (semantic-throw-on-input 'csi)
2116 (semantic-complete-analyze-inline)
2117 ;; Ignore errors. Seems likely that we'll get some once in a while.
2121 ;; @TODO - I can't find where this fcn is used. Delete?
2124 ;(defun semantic-complete-inline-project ()
2125 ; "Perform inline completion for any symbol in the current project.
2126 ;`semantic-analyze-possible-completions' is used to determine the
2128 ;The function returns immediately, leaving the buffer in a mode that
2129 ;will perform the completion."
2131 ; ;; Only do this if we are not already completing something.
2132 ; (if (not (semantic-completion-inline-active-p))
2133 ; (semantic-complete-inline-tag-project))
2134 ; ;; Report a message if things didn't startup.
2135 ; (if (and (interactive-p)
2136 ; (not (semantic-completion-inline-active-p)))
2137 ; (message "Inline completion not needed."))
2141 (provide 'semantic/complete)
2144 ;; generated-autoload-file: "loaddefs.el"
2145 ;; generated-autoload-feature: semantic/loaddefs
2146 ;; generated-autoload-load-name: "semantic/complete"
2149 ;;; semantic/complete.el ends here