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