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