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