]> code.delx.au - gnu-emacs/blob - lisp/cedet/semantic/complete.el
Convert consecutive FSF copyright years to ranges.
[gnu-emacs] / lisp / cedet / semantic / complete.el
1 ;;; semantic/complete.el --- Routines for performing tag completion
2
3 ;; Copyright (C) 2003-2005, 2007-2011
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 distinguish, 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 ;; fulfil 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 (eval-when-compile (require 'cl))
111 (require 'semantic)
112 (require 'eieio-opt)
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 ;;; Code:
123
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.
140 For a minibuffer prompt, this is the minibuffer text.
141 For inline completion, this is the text wrapped in the inline completion
142 overlay."
143 (if semantic-complete-inline-overlay
144 (semantic-complete-inline-text)
145 (minibuffer-contents)))
146
147 (defun semantic-completion-delete-text ()
148 "Delete the text that is actively being completed.
149 Presumably if you call this you will insert something new there."
150 (if semantic-complete-inline-overlay
151 (semantic-complete-inline-delete-text)
152 (delete-minibuffer-contents)))
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.
165 Value 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.
170 Value 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.
188 Keeps 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.
195 Argument COLLECTOR is an object which can be used to calculate
196 a list of possible hits. See `semantic-completion-collector-engine'
197 for details on COLLECTOR.
198 Argument DISPLAYOR is an object used to display a list of possible
199 completions for a given prefix. See`semantic-completion-display-engine'
200 for details on DISPLAYOR.
201 PROMPT is a string to prompt with.
202 DEFAULT-TAG is a semantic tag or string to use as the default value.
203 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.
204 HISTORY 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))
227 " (default " default-as-string ")"
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
313 ;; semantic-displayor-focus-abstract-child-p is part of the
314 ;; semantic-displayor-focus-abstract class, defined later in this
315 ;; file.
316 (declare-function semantic-displayor-focus-abstract-child-p "semantic/complete"
317 t t)
318
319 (defun semantic-complete-current-match ()
320 "Calculate a match from the current completion environment.
321 Save this in our completion variable. Make sure that variable
322 is cleared if any other keypress is made.
323 Return 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 ;;
397 ;; Keys are bound to perform completion using our mechanisms.
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.
424 ORIGINAL is the text in the minibuffer.
425 NEW is the new text to insert into the minibuffer.
426 Within the difference bounds of ORIGINAL and NEW, shorten NEW
427 to 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.
443 If 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.
491 If PARTIAL, do partial completion stopping at spaces.
492 if 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
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))
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)
561 "Keymap used while performing Semantic inline completion.")
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.
569 The 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.
574 Similar 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.
657 When installed as a `pre-command-hook' the special keymap
658 `semantic-complete-inline-map' is queried to replace commands normally run.
659 Commands which edit what is in the region of interest operate normally.
660 Commands which would take us out of the region of interest, or our
661 quit 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.
671 If completion mode is active, check to see if we are within
672 the bounds of `semantic-complete-inline-overlay', or within
673 a 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)))
689 (< (point) s)
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.
720 DO 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.
741 Argument COLLECTOR is an object which can be used to calculate
742 a list of possible hits. See `semantic-completion-collector-engine'
743 for details on COLLECTOR.
744 Argument DISPLAYOR is an object used to display a list of possible
745 completions for a given prefix. See`semantic-completion-display-engine'
746 for details on DISPLAYOR.
747 BUFFER is the buffer in which completion will take place.
748 START is a location for the start of the full symbol.
749 If the symbol being completed is \"foo.ba\", then START
750 is on the \"f\" character.
751 END 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.
810 PARTIAL is non-nil if we are doing partial completion.
811 First, the collector can determine if we should perform a completion or not.
812 If there is nothing to complete, then the displayor determines if we are
813 to show a completion list, scroll, or perhaps do a focus (if it is capable.)
814 Expected 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.
873 Some collectors use a given buffer as a starting place while looking up
874 tags.")
875 (cache :initform nil
876 :type (or null semanticdb-find-result-with-nil)
877 :documentation "Cache of tags.
878 These tags are re-used during a completion session.
879 Sometimes 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'.
883 This result can be used for refined completions as `last-prefix' gets
884 closer to a specific result.")
885 (last-prefix :type string
886 :protection :protected
887 :documentation "The last queried prefix.
888 This prefix can be used to cache intermediate completion offers.
889 making the action of homing in on a token faster.")
890 (last-completion :type (or null string)
891 :documentation "The last calculated completion.
892 This completion is calculated and saved for future use.")
893 (last-whitespace-completion :type (or null string)
894 :documentation "The last whitespace completion.
895 For partial completion, SPC will disabiguate over whitespace type
896 characters. This is the last calculated version.")
897 (current-exact-match :type list
898 :protection :protected
899 :documentation "The list of matched tags.
900 When tokens are matched, they are added to this list.")
901 )
902 "Root class for completion engines.
903 The baseclass provides basic functionality for interacting with
904 a completion displayor object, and tracking the current progress
905 of a completion."
906 :abstract t)
907
908 (defmethod semantic-collector-cleanup ((obj semantic-collector-abstract))
909 "Clean up any mess this collector may have."
910 nil)
911
912 (defmethod semantic-collector-next-action
913 ((obj semantic-collector-abstract) partial)
914 "What should we do next? OBJ can predict a next good action.
915 PARTIAL indicates if we are doing a partial completion."
916 (if (and (slot-boundp obj 'last-completion)
917 (string= (semantic-completion-text) (oref obj last-completion)))
918 (let* ((cem (semantic-collector-current-exact-match obj))
919 (cemlen (semanticdb-find-result-length cem))
920 (cac (semantic-collector-all-completions
921 obj (semantic-completion-text)))
922 (caclen (semanticdb-find-result-length cac)))
923 (cond ((and cem (= cemlen 1)
924 cac (> caclen 1)
925 (eq last-command this-command))
926 ;; Defer to the displayor...
927 nil)
928 ((and cem (= cemlen 1))
929 'done)
930 ((and (not cem) (not cac))
931 'empty)
932 ((and partial (semantic-collector-try-completion-whitespace
933 obj (semantic-completion-text)))
934 'complete-whitespace)))
935 'complete))
936
937 (defmethod semantic-collector-last-prefix= ((obj semantic-collector-abstract)
938 last-prefix)
939 "Return non-nil if OBJ's prefix matches PREFIX."
940 (and (slot-boundp obj 'last-prefix)
941 (string= (oref obj last-prefix) last-prefix)))
942
943 (defmethod semantic-collector-get-cache ((obj semantic-collector-abstract))
944 "Get the raw cache of tags for completion.
945 Calculate the cache if there isn't one."
946 (or (oref obj cache)
947 (semantic-collector-calculate-cache obj)))
948
949 (defmethod semantic-collector-calculate-completions-raw
950 ((obj semantic-collector-abstract) prefix completionlist)
951 "Calculate the completions for prefix from completionlist.
952 Output must be in semanticdb Find result format."
953 ;; Must output in semanticdb format
954 (let ((table (with-current-buffer (oref obj buffer)
955 semanticdb-current-table))
956 (result (semantic-find-tags-for-completion
957 prefix
958 ;; To do this kind of search with a pre-built completion
959 ;; list, we need to strip it first.
960 (semanticdb-strip-find-results completionlist)))
961 )
962 (if result
963 (list (cons table result)))))
964
965 (defmethod semantic-collector-calculate-completions
966 ((obj semantic-collector-abstract) prefix partial)
967 "Calculate completions for prefix as setup for other queries."
968 (let* ((case-fold-search semantic-case-fold)
969 (same-prefix-p (semantic-collector-last-prefix= obj prefix))
970 (completionlist
971 (if (or same-prefix-p
972 (and (slot-boundp obj 'last-prefix)
973 (eq (compare-strings (oref obj last-prefix) 0 nil
974 prefix 0 (length prefix))
975 t)))
976 ;; New prefix is subset of old prefix
977 (oref obj last-all-completions)
978 (semantic-collector-get-cache obj)))
979 ;; Get the result
980 (answer (if same-prefix-p
981 completionlist
982 (semantic-collector-calculate-completions-raw
983 obj prefix completionlist))
984 )
985 (completion nil)
986 (complete-not-uniq nil)
987 )
988 ;;(semanticdb-find-result-test answer)
989 (when (not same-prefix-p)
990 ;; Save results if it is interesting and beneficial
991 (oset obj last-prefix prefix)
992 (oset obj last-all-completions answer))
993 ;; Now calculate the completion.
994 (setq completion (try-completion
995 prefix
996 (semanticdb-strip-find-results answer)))
997 (oset obj last-whitespace-completion nil)
998 (oset obj current-exact-match nil)
999 ;; Only do this if a completion was found. Letting a nil in
1000 ;; could cause a full semanticdb search by accident.
1001 (when completion
1002 (oset obj last-completion
1003 (cond
1004 ;; Unique match in AC. Last completion is a match.
1005 ;; Also set the current-exact-match.
1006 ((eq completion t)
1007 (oset obj current-exact-match answer)
1008 prefix)
1009 ;; It may be complete (a symbol) but still not unique.
1010 ;; We can capture a match
1011 ((setq complete-not-uniq
1012 (semanticdb-find-tags-by-name
1013 prefix
1014 answer))
1015 (oset obj current-exact-match
1016 complete-not-uniq)
1017 prefix
1018 )
1019 ;; Non unique match, return the string that handles
1020 ;; completion
1021 (t (or completion prefix))
1022 )))
1023 ))
1024
1025 (defmethod semantic-collector-try-completion-whitespace
1026 ((obj semantic-collector-abstract) prefix)
1027 "For OBJ, do whatepsace completion based on PREFIX.
1028 This implies that if there are two completions, one matching
1029 the test \"preifx\\>\", and one not, the one matching the full
1030 word version of PREFIX will be chosen, and that text returned.
1031 This function requires that `semantic-collector-calculate-completions'
1032 has been run first."
1033 (let* ((ac (semantic-collector-all-completions obj prefix))
1034 (matchme (concat "^" prefix "\\>"))
1035 (compare (semanticdb-find-tags-by-name-regexp matchme ac))
1036 (numtag (semanticdb-find-result-length compare))
1037 )
1038 (if compare
1039 (let* ((idx 0)
1040 (cutlen (1+ (length prefix)))
1041 (twws (semanticdb-find-result-nth compare idx)))
1042 ;; Is our tag with whitespace a match that has whitespace
1043 ;; after it, or just an already complete symbol?
1044 (while (and (< idx numtag)
1045 (< (length (semantic-tag-name (car twws))) cutlen))
1046 (setq idx (1+ idx)
1047 twws (semanticdb-find-result-nth compare idx)))
1048 (when (and twws (car-safe twws))
1049 ;; If COMPARE has succeeded, then we should take the very
1050 ;; first match, and extend prefix by one character.
1051 (oset obj last-whitespace-completion
1052 (substring (semantic-tag-name (car twws))
1053 0 cutlen))))
1054 )))
1055
1056
1057 (defmethod semantic-collector-current-exact-match ((obj semantic-collector-abstract))
1058 "Return the active valid MATCH from the semantic collector.
1059 For now, just return the first element from our list of available
1060 matches. For semanticdb based results, make sure the file is loaded
1061 into a buffer."
1062 (when (slot-boundp obj 'current-exact-match)
1063 (oref obj current-exact-match)))
1064
1065 (defmethod semantic-collector-current-whitespace-completion ((obj semantic-collector-abstract))
1066 "Return the active whitespace completion value."
1067 (when (slot-boundp obj 'last-whitespace-completion)
1068 (oref obj last-whitespace-completion)))
1069
1070 (defmethod semantic-collector-get-match ((obj semantic-collector-abstract))
1071 "Return the active valid MATCH from the semantic collector.
1072 For now, just return the first element from our list of available
1073 matches. For semanticdb based results, make sure the file is loaded
1074 into a buffer."
1075 (when (slot-boundp obj 'current-exact-match)
1076 (semanticdb-find-result-nth-in-buffer (oref obj current-exact-match) 0)))
1077
1078 (defmethod semantic-collector-all-completions
1079 ((obj semantic-collector-abstract) prefix)
1080 "For OBJ, retrieve all completions matching PREFIX.
1081 The returned list consists of all the tags currently
1082 matching PREFIX."
1083 (when (slot-boundp obj 'last-all-completions)
1084 (oref obj last-all-completions)))
1085
1086 (defmethod semantic-collector-try-completion
1087 ((obj semantic-collector-abstract) prefix)
1088 "For OBJ, attempt to match PREFIX.
1089 See `try-completion' for details on how this works.
1090 Return nil for no match.
1091 Return a string for a partial match.
1092 For a unique match of PREFIX, return the list of all tags
1093 with that name."
1094 (if (slot-boundp obj 'last-completion)
1095 (oref obj last-completion)))
1096
1097 (defmethod semantic-collector-calculate-cache
1098 ((obj semantic-collector-abstract))
1099 "Calculate the completion cache for OBJ."
1100 nil
1101 )
1102
1103 (defmethod semantic-collector-flush ((this semantic-collector-abstract))
1104 "Flush THIS collector object, clearing any caches and prefix."
1105 (oset this cache nil)
1106 (slot-makeunbound this 'last-prefix)
1107 (slot-makeunbound this 'last-completion)
1108 (slot-makeunbound this 'last-all-completions)
1109 (slot-makeunbound this 'current-exact-match)
1110 )
1111
1112 ;;; PER BUFFER
1113 ;;
1114 (defclass semantic-collector-buffer-abstract (semantic-collector-abstract)
1115 ()
1116 "Root class for per-buffer completion engines.
1117 These collectors track themselves on a per-buffer basis."
1118 :abstract t)
1119
1120 (defmethod constructor :STATIC ((this semantic-collector-buffer-abstract)
1121 newname &rest fields)
1122 "Reuse previously created objects of this type in buffer."
1123 (let ((old nil)
1124 (bl semantic-collector-per-buffer-list))
1125 (while (and bl (null old))
1126 (if (eq (object-class (car bl)) this)
1127 (setq old (car bl))))
1128 (unless old
1129 (let ((new (call-next-method)))
1130 (add-to-list 'semantic-collector-per-buffer-list new)
1131 (setq old new)))
1132 (slot-makeunbound old 'last-completion)
1133 (slot-makeunbound old 'last-prefix)
1134 (slot-makeunbound old 'current-exact-match)
1135 old))
1136
1137 ;; Buffer specific collectors should flush themselves
1138 (defun semantic-collector-buffer-flush (newcache)
1139 "Flush all buffer collector objects.
1140 NEWCACHE is the new tag table, but we ignore it."
1141 (condition-case nil
1142 (let ((l semantic-collector-per-buffer-list))
1143 (while l
1144 (if (car l) (semantic-collector-flush (car l)))
1145 (setq l (cdr l))))
1146 (error nil)))
1147
1148 (add-hook 'semantic-after-toplevel-cache-change-hook
1149 'semantic-collector-buffer-flush)
1150
1151 ;;; DEEP BUFFER SPECIFIC COMPLETION
1152 ;;
1153 (defclass semantic-collector-buffer-deep
1154 (semantic-collector-buffer-abstract)
1155 ()
1156 "Completion engine for tags in the current buffer.
1157 When searching for a tag, uses semantic deep searche functions.
1158 Basics search only in the current buffer.")
1159
1160 (defmethod semantic-collector-calculate-cache
1161 ((obj semantic-collector-buffer-deep))
1162 "Calculate the completion cache for OBJ.
1163 Uses `semantic-flatten-tags-table'"
1164 (oset obj cache
1165 ;; Must create it in SEMANTICDB find format.
1166 ;; ( ( DBTABLE TAG TAG ... ) ... )
1167 (list
1168 (cons semanticdb-current-table
1169 (semantic-flatten-tags-table (oref obj buffer))))))
1170
1171 ;;; PROJECT SPECIFIC COMPLETION
1172 ;;
1173 (defclass semantic-collector-project-abstract (semantic-collector-abstract)
1174 ((path :initarg :path
1175 :initform nil
1176 :documentation "List of database tables to search.
1177 At creation time, it can be anything accepted by
1178 `semanticdb-find-translate-path' as a PATH argument.")
1179 )
1180 "Root class for project wide completion engines.
1181 Uses semanticdb for searching all tags in the current project."
1182 :abstract t)
1183
1184 ;;; Project Search
1185 (defclass semantic-collector-project (semantic-collector-project-abstract)
1186 ()
1187 "Completion engine for tags in a project.")
1188
1189
1190 (defmethod semantic-collector-calculate-completions-raw
1191 ((obj semantic-collector-project) prefix completionlist)
1192 "Calculate the completions for prefix from completionlist."
1193 (semanticdb-find-tags-for-completion prefix (oref obj path)))
1194
1195 ;;; Brutish Project search
1196 (defclass semantic-collector-project-brutish (semantic-collector-project-abstract)
1197 ()
1198 "Completion engine for tags in a project.")
1199
1200 (declare-function semanticdb-brute-deep-find-tags-for-completion
1201 "semantic/db-find")
1202
1203 (defmethod semantic-collector-calculate-completions-raw
1204 ((obj semantic-collector-project-brutish) prefix completionlist)
1205 "Calculate the completions for prefix from completionlist."
1206 (require 'semantic/db-find)
1207 (semanticdb-brute-deep-find-tags-for-completion prefix (oref obj path)))
1208
1209 ;;; Current Datatype member search.
1210 (defclass semantic-collector-local-members (semantic-collector-project-abstract)
1211 ((scope :initform nil
1212 :type (or null semantic-scope-cache)
1213 :documentation
1214 "The scope the local members are being completed from."))
1215 "Completion engine for tags in a project.")
1216
1217 (defmethod semantic-collector-calculate-completions-raw
1218 ((obj semantic-collector-local-members) prefix completionlist)
1219 "Calculate the completions for prefix from completionlist."
1220 (let* ((scope (or (oref obj scope)
1221 (oset obj scope (semantic-calculate-scope))))
1222 (localstuff (oref scope scope)))
1223 (list
1224 (cons
1225 (oref scope :table)
1226 (semantic-find-tags-for-completion prefix localstuff)))))
1227 ;(semanticdb-brute-deep-find-tags-for-completion prefix (oref obj path))))
1228
1229 ;;; Smart completion collector
1230 (defclass semantic-collector-analyze-completions (semantic-collector-abstract)
1231 ((context :initarg :context
1232 :type semantic-analyze-context
1233 :documentation "An analysis context.
1234 Specifies some context location from whence completion lists will be drawn."
1235 )
1236 (first-pass-completions :type list
1237 :documentation "List of valid completion tags.
1238 This list of tags is generated when completion starts. All searches
1239 derive from this list.")
1240 )
1241 "Completion engine that uses the context analyzer to provide options.
1242 The only options available for completion are those which can be logically
1243 inserted into the current context.")
1244
1245 (defmethod semantic-collector-calculate-completions-raw
1246 ((obj semantic-collector-analyze-completions) prefix completionlist)
1247 "calculate the completions for prefix from completionlist."
1248 ;; if there are no completions yet, calculate them.
1249 (if (not (slot-boundp obj 'first-pass-completions))
1250 (oset obj first-pass-completions
1251 (semantic-analyze-possible-completions (oref obj context))))
1252 ;; search our cached completion list. make it look like a semanticdb
1253 ;; results type.
1254 (list (cons (with-current-buffer (oref (oref obj context) buffer)
1255 semanticdb-current-table)
1256 (semantic-find-tags-for-completion
1257 prefix
1258 (oref obj first-pass-completions)))))
1259
1260 \f
1261 ;;; ------------------------------------------------------------
1262 ;;; Tag List Display Engines
1263 ;;
1264 ;; A typical displayor accepts a pre-determined list of completions
1265 ;; generated by a collector. This format is in semanticdb search
1266 ;; form. This vaguely standard form is a bit challenging to navigate
1267 ;; because the tags do not contain buffer info, but the file associated
1268 ;; with the tags preceed the tag in the list.
1269 ;;
1270 ;; Basic displayors don't care, and can strip the results.
1271 ;; Advanced highlighting displayors need to know when they need
1272 ;; to load a file so that the tag in question can be highlighted.
1273 ;;
1274 ;; Key interface methods to a displayor are:
1275 ;; * semantic-displayor-next-action
1276 ;; * semantic-displayor-set-completions
1277 ;; * semantic-displayor-current-focus
1278 ;; * semantic-displayor-show-request
1279 ;; * semantic-displayor-scroll-request
1280 ;; * semantic-displayor-focus-request
1281
1282 (defclass semantic-displayor-abstract ()
1283 ((table :type (or null semanticdb-find-result-with-nil)
1284 :initform nil
1285 :protection :protected
1286 :documentation "List of tags this displayor is showing.")
1287 (last-prefix :type string
1288 :protection :protected
1289 :documentation "Prefix associated with slot `table'")
1290 )
1291 "Abstract displayor baseclass.
1292 Manages the display of some number of tags.
1293 Provides the basics for a displayor, including interacting with
1294 a collector, and tracking tables of completion to display."
1295 :abstract t)
1296
1297 (defmethod semantic-displayor-cleanup ((obj semantic-displayor-abstract))
1298 "Clean up any mess this displayor may have."
1299 nil)
1300
1301 (defmethod semantic-displayor-next-action ((obj semantic-displayor-abstract))
1302 "The next action to take on the minibuffer related to display."
1303 (if (and (slot-boundp obj 'last-prefix)
1304 (string= (oref obj last-prefix) (semantic-completion-text))
1305 (eq last-command this-command))
1306 'scroll
1307 'display))
1308
1309 (defmethod semantic-displayor-set-completions ((obj semantic-displayor-abstract)
1310 table prefix)
1311 "Set the list of tags to be completed over to TABLE."
1312 (oset obj table table)
1313 (oset obj last-prefix prefix))
1314
1315 (defmethod semantic-displayor-show-request ((obj semantic-displayor-abstract))
1316 "A request to show the current tags table."
1317 (ding))
1318
1319 (defmethod semantic-displayor-focus-request ((obj semantic-displayor-abstract))
1320 "A request to for the displayor to focus on some tag option."
1321 (ding))
1322
1323 (defmethod semantic-displayor-scroll-request ((obj semantic-displayor-abstract))
1324 "A request to for the displayor to scroll the completion list (if needed)."
1325 (scroll-other-window))
1326
1327 (defmethod semantic-displayor-focus-previous ((obj semantic-displayor-abstract))
1328 "Set the current focus to the previous item."
1329 nil)
1330
1331 (defmethod semantic-displayor-focus-next ((obj semantic-displayor-abstract))
1332 "Set the current focus to the next item."
1333 nil)
1334
1335 (defmethod semantic-displayor-current-focus ((obj semantic-displayor-abstract))
1336 "Return a single tag currently in focus.
1337 This object type doesn't do focus, so will never have a focus object."
1338 nil)
1339
1340 ;; Traditional displayor
1341 (defcustom semantic-completion-displayor-format-tag-function
1342 #'semantic-format-tag-name
1343 "*A Tag format function to use when showing completions."
1344 :group 'semantic
1345 :type semantic-format-tag-custom-list)
1346
1347 (defclass semantic-displayor-traditional (semantic-displayor-abstract)
1348 ()
1349 "Display options in *Completions* buffer.
1350 Traditional display mechanism for a list of possible completions.
1351 Completions are showin in a new buffer and listed with the ability
1352 to click on the items to aid in completion.")
1353
1354 (defmethod semantic-displayor-show-request ((obj semantic-displayor-traditional))
1355 "A request to show the current tags table."
1356
1357 ;; NOTE TO SELF. Find the character to type next, and emphesize it.
1358
1359 (with-output-to-temp-buffer "*Completions*"
1360 (display-completion-list
1361 (mapcar semantic-completion-displayor-format-tag-function
1362 (semanticdb-strip-find-results (oref obj table))))
1363 )
1364 )
1365
1366 ;;; Abstract baseclass for any displayor which supports focus
1367 (defclass semantic-displayor-focus-abstract (semantic-displayor-abstract)
1368 ((focus :type number
1369 :protection :protected
1370 :documentation "A tag index from `table' which has focus.
1371 Multiple calls to the display function can choose to focus on a
1372 given tag, by highlighting its location.")
1373 (find-file-focus
1374 :allocation :class
1375 :initform nil
1376 :documentation
1377 "Non-nil if focusing requires a tag's buffer be in memory.")
1378 )
1379 "Abstract displayor supporting `focus'.
1380 A displayor which has the ability to focus in on one tag.
1381 Focusing is a way of differentiationg between multiple tags
1382 which have the same name."
1383 :abstract t)
1384
1385 (defmethod semantic-displayor-next-action ((obj semantic-displayor-focus-abstract))
1386 "The next action to take on the minibuffer related to display."
1387 (if (and (slot-boundp obj 'last-prefix)
1388 (string= (oref obj last-prefix) (semantic-completion-text))
1389 (eq last-command this-command))
1390 (if (and
1391 (slot-boundp obj 'focus)
1392 (slot-boundp obj 'table)
1393 (<= (semanticdb-find-result-length (oref obj table))
1394 (1+ (oref obj focus))))
1395 ;; We are at the end of the focus road.
1396 'displayend
1397 ;; Focus on some item.
1398 'focus)
1399 'display))
1400
1401 (defmethod semantic-displayor-set-completions ((obj semantic-displayor-focus-abstract)
1402 table prefix)
1403 "Set the list of tags to be completed over to TABLE."
1404 (call-next-method)
1405 (slot-makeunbound obj 'focus))
1406
1407 (defmethod semantic-displayor-focus-previous ((obj semantic-displayor-focus-abstract))
1408 "Set the current focus to the previous item.
1409 Not meaningful return value."
1410 (when (and (slot-boundp obj 'table) (oref obj table))
1411 (with-slots (table) obj
1412 (if (or (not (slot-boundp obj 'focus))
1413 (<= (oref obj focus) 0))
1414 (oset obj focus (1- (semanticdb-find-result-length table)))
1415 (oset obj focus (1- (oref obj focus)))
1416 )
1417 )))
1418
1419 (defmethod semantic-displayor-focus-next ((obj semantic-displayor-focus-abstract))
1420 "Set the current focus to the next item.
1421 Not meaningful return value."
1422 (when (and (slot-boundp obj 'table) (oref obj table))
1423 (with-slots (table) obj
1424 (if (not (slot-boundp obj 'focus))
1425 (oset obj focus 0)
1426 (oset obj focus (1+ (oref obj focus)))
1427 )
1428 (if (<= (semanticdb-find-result-length table) (oref obj focus))
1429 (oset obj focus 0))
1430 )))
1431
1432 (defmethod semantic-displayor-focus-tag ((obj semantic-displayor-focus-abstract))
1433 "Return the next tag OBJ should focus on."
1434 (when (and (slot-boundp obj 'table) (oref obj table))
1435 (with-slots (table) obj
1436 (semanticdb-find-result-nth table (oref obj focus)))))
1437
1438 (defmethod semantic-displayor-current-focus ((obj semantic-displayor-focus-abstract))
1439 "Return the tag currently in focus, or call parent method."
1440 (if (and (slot-boundp obj 'focus)
1441 (slot-boundp obj 'table)
1442 ;; Only return the current focus IFF the minibuffer reflects
1443 ;; the list this focus was derived from.
1444 (slot-boundp obj 'last-prefix)
1445 (string= (semantic-completion-text) (oref obj last-prefix))
1446 )
1447 ;; We need to focus
1448 (if (oref obj find-file-focus)
1449 (semanticdb-find-result-nth-in-buffer (oref obj table) (oref obj focus))
1450 ;; result-nth returns a cons with car being the tag, and cdr the
1451 ;; database.
1452 (car (semanticdb-find-result-nth (oref obj table) (oref obj focus))))
1453 ;; Do whatever
1454 (call-next-method)))
1455
1456 ;;; Simple displayor which performs traditional display completion,
1457 ;; and also focuses with highlighting.
1458 (defclass semantic-displayor-traditional-with-focus-highlight
1459 (semantic-displayor-focus-abstract semantic-displayor-traditional)
1460 ((find-file-focus :initform t))
1461 "Display completions in *Completions* buffer, with focus highlight.
1462 A traditional displayor which can focus on a tag by showing it.
1463 Same as `semantic-displayor-traditional', but with selection between
1464 multiple tags with the same name done by 'focusing' on the source
1465 location of the different tags to differentiate them.")
1466
1467 (defmethod semantic-displayor-focus-request
1468 ((obj semantic-displayor-traditional-with-focus-highlight))
1469 "Focus in on possible tag completions.
1470 Focus is performed by cycling through the tags and highlighting
1471 one in the source buffer."
1472 (let* ((tablelength (semanticdb-find-result-length (oref obj table)))
1473 (focus (semantic-displayor-focus-tag obj))
1474 ;; Raw tag info.
1475 (rtag (car focus))
1476 (rtable (cdr focus))
1477 ;; Normalize
1478 (nt (semanticdb-normalize-one-tag rtable rtag))
1479 (tag (cdr nt))
1480 (table (car nt))
1481 )
1482 ;; If we fail to normalize, resete.
1483 (when (not tag) (setq table rtable tag rtag))
1484 ;; Do the focus.
1485 (let ((buf (or (semantic-tag-buffer tag)
1486 (and table (semanticdb-get-buffer table)))))
1487 ;; If no buffer is provided, then we can make up a summary buffer.
1488 (when (not buf)
1489 (with-current-buffer (get-buffer-create "*Completion Focus*")
1490 (erase-buffer)
1491 (insert "Focus on tag: \n")
1492 (insert (semantic-format-tag-summarize tag nil t) "\n\n")
1493 (when table
1494 (insert "From table: \n")
1495 (insert (object-name table) "\n\n"))
1496 (when buf
1497 (insert "In buffer: \n\n")
1498 (insert (format "%S" buf)))
1499 (setq buf (current-buffer))))
1500 ;; Show the tag in the buffer.
1501 (if (get-buffer-window buf)
1502 (select-window (get-buffer-window buf))
1503 (switch-to-buffer-other-window buf t)
1504 (select-window (get-buffer-window buf)))
1505 ;; Now do some positioning
1506 (unwind-protect
1507 (if (semantic-tag-with-position-p tag)
1508 ;; Full tag positional information available
1509 (progn
1510 (goto-char (semantic-tag-start tag))
1511 ;; This avoids a dangerous problem if we just loaded a tag
1512 ;; from a file, but the original position was not updated
1513 ;; in the TAG variable we are currently using.
1514 (semantic-momentary-highlight-tag (semantic-current-tag))
1515 ))
1516 (select-window (minibuffer-window)))
1517 ;; Calculate text difference between contents and the focus item.
1518 (let* ((mbc (semantic-completion-text))
1519 (ftn (semantic-tag-name tag))
1520 (diff (substring ftn (length mbc))))
1521 (semantic-completion-message
1522 (format "%s [%d of %d matches]" diff (1+ (oref obj focus)) tablelength)))
1523 )))
1524
1525 \f
1526 ;;; Tooltip completion lister
1527 ;;
1528 ;; Written and contributed by Masatake YAMATO <jet@gyve.org>
1529 ;;
1530 ;; Modified by Eric Ludlam for
1531 ;; * Safe compatibility for tooltip free systems.
1532 ;; * Don't use 'avoid package for tooltip positioning.
1533
1534 (defclass semantic-displayor-tooltip (semantic-displayor-traditional)
1535 ((max-tags :type integer
1536 :initarg :max-tags
1537 :initform 5
1538 :custom integer
1539 :documentation
1540 "Max number of tags displayed on tooltip at once.
1541 If `force-show' is 1, this value is ignored with typing tab or space twice continuously.
1542 if `force-show' is 0, this value is always ignored.")
1543 (force-show :type integer
1544 :initarg :force-show
1545 :initform 1
1546 :custom (choice (const
1547 :tag "Show when double typing"
1548 1)
1549 (const
1550 :tag "Show always"
1551 0)
1552 (const
1553 :tag "Show if the number of tags is less than `max-tags'."
1554 -1))
1555 :documentation
1556 "Control the behavior of the number of tags is greater than `max-tags'.
1557 -1 means tags are never shown.
1558 0 means the tags are always shown.
1559 1 means tags are shown if space or tab is typed twice continuously.")
1560 (typing-count :type integer
1561 :initform 0
1562 :documentation
1563 "Counter holding how many times the user types space or tab continuously before showing tags.")
1564 (shown :type boolean
1565 :initform nil
1566 :documentation
1567 "Flag representing whether tags is shown once or not.")
1568 )
1569 "Display completions options in a tooltip.
1570 Display mechanism using tooltip for a list of possible completions.")
1571
1572 (defmethod initialize-instance :AFTER ((obj semantic-displayor-tooltip) &rest args)
1573 "Make sure we have tooltips required."
1574 (condition-case nil
1575 (require 'tooltip)
1576 (error nil))
1577 )
1578
1579 (defmethod semantic-displayor-show-request ((obj semantic-displayor-tooltip))
1580 "A request to show the current tags table."
1581 (if (or (not (featurep 'tooltip)) (not tooltip-mode))
1582 ;; If we cannot use tooltips, then go to the normal mode with
1583 ;; a traditional completion buffer.
1584 (call-next-method)
1585 (let* ((tablelong (semanticdb-strip-find-results (oref obj table)))
1586 (table (semantic-unique-tag-table-by-name tablelong))
1587 (l (mapcar semantic-completion-displayor-format-tag-function table))
1588 (ll (length l))
1589 (typing-count (oref obj typing-count))
1590 (force-show (oref obj force-show))
1591 (matchtxt (semantic-completion-text))
1592 msg)
1593 (if (or (oref obj shown)
1594 (< ll (oref obj max-tags))
1595 (and (<= 0 force-show)
1596 (< (1- force-show) typing-count)))
1597 (progn
1598 (oset obj typing-count 0)
1599 (oset obj shown t)
1600 (if (eq 1 ll)
1601 ;; We Have only one possible match. There could be two cases.
1602 ;; 1) input text != single match.
1603 ;; --> Show it!
1604 ;; 2) input text == single match.
1605 ;; --> Complain about it, but still show the match.
1606 (if (string= matchtxt (semantic-tag-name (car table)))
1607 (setq msg (concat "[COMPLETE]\n" (car l)))
1608 (setq msg (car l)))
1609 ;; Create the long message.
1610 (setq msg (mapconcat 'identity l "\n"))
1611 ;; If there is nothing, say so!
1612 (if (eq 0 (length msg))
1613 (setq msg "[NO MATCH]")))
1614 (semantic-displayor-tooltip-show msg))
1615 ;; The typing count determines if the user REALLY REALLY
1616 ;; wanted to show that much stuff. Only increment
1617 ;; if the current command is a completion command.
1618 (if (and (stringp (this-command-keys))
1619 (string= (this-command-keys) "\C-i"))
1620 (oset obj typing-count (1+ typing-count)))
1621 ;; At this point, we know we have too many items.
1622 ;; Lets be brave, and truncate l
1623 (setcdr (nthcdr (oref obj max-tags) l) nil)
1624 (setq msg (mapconcat 'identity l "\n"))
1625 (cond
1626 ((= force-show -1)
1627 (semantic-displayor-tooltip-show (concat msg "\n...")))
1628 ((= force-show 1)
1629 (semantic-displayor-tooltip-show (concat msg "\n(TAB for more)")))
1630 )))))
1631
1632 ;;; Compatibility
1633 ;;
1634 (eval-and-compile
1635 (if (fboundp 'window-inside-edges)
1636 ;; Emacs devel.
1637 (defalias 'semantic-displayor-window-edges
1638 'window-inside-edges)
1639 ;; Emacs 21
1640 (defalias 'semantic-displayor-window-edges
1641 'window-edges)
1642 ))
1643
1644 (defun semantic-displayor-point-position ()
1645 "Return the location of POINT as positioned on the selected frame.
1646 Return a cons cell (X . Y)"
1647 (let* ((frame (selected-frame))
1648 (left (frame-parameter frame 'left))
1649 (top (frame-parameter frame 'top))
1650 (point-pix-pos (posn-x-y (posn-at-point)))
1651 (edges (window-inside-pixel-edges (selected-window))))
1652 (cons (+ (car point-pix-pos) (car edges) left)
1653 (+ (cdr point-pix-pos) (cadr edges) top))))
1654
1655
1656 (defun semantic-displayor-tooltip-show (text)
1657 "Display a tooltip with TEXT near cursor."
1658 (let ((point-pix-pos (semantic-displayor-point-position))
1659 (tooltip-frame-parameters
1660 (append tooltip-frame-parameters nil)))
1661 (push
1662 (cons 'left (+ (car point-pix-pos) (frame-char-width)))
1663 tooltip-frame-parameters)
1664 (push
1665 (cons 'top (+ (cdr point-pix-pos) (frame-char-height)))
1666 tooltip-frame-parameters)
1667 (tooltip-show text)))
1668
1669 (defmethod semantic-displayor-scroll-request ((obj semantic-displayor-tooltip))
1670 "A request to for the displayor to scroll the completion list (if needed)."
1671 ;; Do scrolling in the tooltip.
1672 (oset obj max-tags 30)
1673 (semantic-displayor-show-request obj)
1674 )
1675
1676 ;; End code contributed by Masatake YAMATO <jet@gyve.org>
1677
1678 \f
1679 ;;; Ghost Text displayor
1680 ;;
1681 (defclass semantic-displayor-ghost (semantic-displayor-focus-abstract)
1682
1683 ((ghostoverlay :type overlay
1684 :documentation
1685 "The overlay the ghost text is displayed in.")
1686 (first-show :initform t
1687 :documentation
1688 "Non nil if we have not seen our first show request.")
1689 )
1690 "Cycle completions inline with ghost text.
1691 Completion displayor using ghost chars after point for focus options.
1692 Whichever completion is currently in focus will be displayed as ghost
1693 text using overlay options.")
1694
1695 (defmethod semantic-displayor-next-action ((obj semantic-displayor-ghost))
1696 "The next action to take on the inline completion related to display."
1697 (let ((ans (call-next-method))
1698 (table (when (slot-boundp obj 'table)
1699 (oref obj table))))
1700 (if (and (eq ans 'displayend)
1701 table
1702 (= (semanticdb-find-result-length table) 1)
1703 )
1704 nil
1705 ans)))
1706
1707 (defmethod semantic-displayor-cleanup ((obj semantic-displayor-ghost))
1708 "Clean up any mess this displayor may have."
1709 (when (slot-boundp obj 'ghostoverlay)
1710 (semantic-overlay-delete (oref obj ghostoverlay)))
1711 )
1712
1713 (defmethod semantic-displayor-set-completions ((obj semantic-displayor-ghost)
1714 table prefix)
1715 "Set the list of tags to be completed over to TABLE."
1716 (call-next-method)
1717
1718 (semantic-displayor-cleanup obj)
1719 )
1720
1721
1722 (defmethod semantic-displayor-show-request ((obj semantic-displayor-ghost))
1723 "A request to show the current tags table."
1724 ; (if (oref obj first-show)
1725 ; (progn
1726 ; (oset obj first-show nil)
1727 (semantic-displayor-focus-next obj)
1728 (semantic-displayor-focus-request obj)
1729 ; )
1730 ;; Only do the traditional thing if the first show request
1731 ;; has been seen. Use the first one to start doing the ghost
1732 ;; text display.
1733 ; (call-next-method)
1734 ; )
1735 )
1736
1737 (defmethod semantic-displayor-focus-request
1738 ((obj semantic-displayor-ghost))
1739 "Focus in on possible tag completions.
1740 Focus is performed by cycling through the tags and showing a possible
1741 completion text in ghost text."
1742 (let* ((tablelength (semanticdb-find-result-length (oref obj table)))
1743 (focus (semantic-displayor-focus-tag obj))
1744 (tag (car focus))
1745 )
1746 (if (not tag)
1747 (semantic-completion-message "No tags to focus on.")
1748 ;; Display the focus completion as ghost text after the current
1749 ;; inline text.
1750 (when (or (not (slot-boundp obj 'ghostoverlay))
1751 (not (semantic-overlay-live-p (oref obj ghostoverlay))))
1752 (oset obj ghostoverlay
1753 (semantic-make-overlay (point) (1+ (point)) (current-buffer) t)))
1754
1755 (let* ((lp (semantic-completion-text))
1756 (os (substring (semantic-tag-name tag) (length lp)))
1757 (ol (oref obj ghostoverlay))
1758 )
1759
1760 (put-text-property 0 (length os) 'face 'region os)
1761
1762 (semantic-overlay-put
1763 ol 'display (concat os (buffer-substring (point) (1+ (point)))))
1764 )
1765 ;; Calculate text difference between contents and the focus item.
1766 (let* ((mbc (semantic-completion-text))
1767 (ftn (concat (semantic-tag-name tag)))
1768 )
1769 (put-text-property (length mbc) (length ftn) 'face
1770 'bold ftn)
1771 (semantic-completion-message
1772 (format "%s [%d of %d matches]" ftn (1+ (oref obj focus)) tablelength)))
1773 )))
1774
1775 \f
1776 ;;; ------------------------------------------------------------
1777 ;;; Specific queries
1778 ;;
1779 (defvar semantic-complete-inline-custom-type
1780 (append '(radio)
1781 (mapcar
1782 (lambda (class)
1783 (let* ((C (intern (car class)))
1784 (doc (documentation-property C 'variable-documentation))
1785 (doc1 (car (split-string doc "\n")))
1786 )
1787 (list 'const
1788 :tag doc1
1789 C)))
1790 (eieio-build-class-alist semantic-displayor-abstract t))
1791 )
1792 "Possible options for inline completion displayors.
1793 Use this to enable custom editing.")
1794
1795 (defcustom semantic-complete-inline-analyzer-displayor-class
1796 'semantic-displayor-traditional
1797 "*Class for displayor to use with inline completion."
1798 :group 'semantic
1799 :type semantic-complete-inline-custom-type
1800 )
1801
1802 (defun semantic-complete-read-tag-buffer-deep (prompt &optional
1803 default-tag
1804 initial-input
1805 history)
1806 "Ask for a tag by name from the current buffer.
1807 Available tags are from the current buffer, at any level.
1808 Completion options are presented in a traditional way, with highlighting
1809 to resolve same-name collisions.
1810 PROMPT is a string to prompt with.
1811 DEFAULT-TAG is a semantic tag or string to use as the default value.
1812 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.
1813 HISTORY is a symbol representing a variable to store the history in."
1814 (semantic-complete-read-tag-engine
1815 (semantic-collector-buffer-deep prompt :buffer (current-buffer))
1816 (semantic-displayor-traditional-with-focus-highlight "simple")
1817 ;;(semantic-displayor-tooltip "simple")
1818 prompt
1819 default-tag
1820 initial-input
1821 history)
1822 )
1823
1824 (defun semantic-complete-read-tag-local-members (prompt &optional
1825 default-tag
1826 initial-input
1827 history)
1828 "Ask for a tag by name from the local type members.
1829 Available tags are from the the current scope.
1830 Completion options are presented in a traditional way, with highlighting
1831 to resolve same-name collisions.
1832 PROMPT is a string to prompt with.
1833 DEFAULT-TAG is a semantic tag or string to use as the default value.
1834 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.
1835 HISTORY is a symbol representing a variable to store the history in."
1836 (semantic-complete-read-tag-engine
1837 (semantic-collector-local-members prompt :buffer (current-buffer))
1838 (semantic-displayor-traditional-with-focus-highlight "simple")
1839 ;;(semantic-displayor-tooltip "simple")
1840 prompt
1841 default-tag
1842 initial-input
1843 history)
1844 )
1845
1846 (defun semantic-complete-read-tag-project (prompt &optional
1847 default-tag
1848 initial-input
1849 history)
1850 "Ask for a tag by name from the current project.
1851 Available tags are from the current project, at the top level.
1852 Completion options are presented in a traditional way, with highlighting
1853 to resolve same-name collisions.
1854 PROMPT is a string to prompt with.
1855 DEFAULT-TAG is a semantic tag or string to use as the default value.
1856 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.
1857 HISTORY is a symbol representing a variable to store the history in."
1858 (semantic-complete-read-tag-engine
1859 (semantic-collector-project-brutish prompt
1860 :buffer (current-buffer)
1861 :path (current-buffer)
1862 )
1863 (semantic-displayor-traditional-with-focus-highlight "simple")
1864 prompt
1865 default-tag
1866 initial-input
1867 history)
1868 )
1869
1870 (defun semantic-complete-inline-tag-project ()
1871 "Complete a symbol name by name from within the current project.
1872 This is similar to `semantic-complete-read-tag-project', except
1873 that the completion interaction is in the buffer where the context
1874 was calculated from.
1875 Customize `semantic-complete-inline-analyzer-displayor-class'
1876 to control how completion options are displayed.
1877 See `semantic-complete-inline-tag-engine' for details on how
1878 completion works."
1879 (let* ((collector (semantic-collector-project-brutish
1880 "inline"
1881 :buffer (current-buffer)
1882 :path (current-buffer)))
1883 (sbounds (semantic-ctxt-current-symbol-and-bounds))
1884 (syms (car sbounds))
1885 (start (car (nth 2 sbounds)))
1886 (end (cdr (nth 2 sbounds)))
1887 (rsym (reverse syms))
1888 (thissym (nth 1 sbounds))
1889 (nextsym (car-safe (cdr rsym)))
1890 (complst nil))
1891 (when (and thissym (or (not (string= thissym ""))
1892 nextsym))
1893 ;; Do a quick calcuation of completions.
1894 (semantic-collector-calculate-completions
1895 collector thissym nil)
1896 ;; Get the master list
1897 (setq complst (semanticdb-strip-find-results
1898 (semantic-collector-all-completions collector thissym)))
1899 ;; Shorten by name
1900 (setq complst (semantic-unique-tag-table-by-name complst))
1901 (if (or (and (= (length complst) 1)
1902 ;; Check to see if it is the same as what is there.
1903 ;; if so, we can offer to complete.
1904 (let ((compname (semantic-tag-name (car complst))))
1905 (not (string= compname thissym))))
1906 (> (length complst) 1))
1907 ;; There are several options. Do the completion.
1908 (semantic-complete-inline-tag-engine
1909 collector
1910 (funcall semantic-complete-inline-analyzer-displayor-class
1911 "inline displayor")
1912 ;;(semantic-displayor-tooltip "simple")
1913 (current-buffer)
1914 start end))
1915 )))
1916
1917 (defun semantic-complete-read-tag-analyzer (prompt &optional
1918 context
1919 history)
1920 "Ask for a tag by name based on the current context.
1921 The function `semantic-analyze-current-context' is used to
1922 calculate the context. `semantic-analyze-possible-completions' is used
1923 to generate the list of possible completions.
1924 PROMPT is the first part of the prompt. Additional prompt
1925 is added based on the contexts full prefix.
1926 CONTEXT is the semantic analyzer context to start with.
1927 HISTORY is a symbol representing a variable to store the history in.
1928 usually a default-tag and initial-input are available for completion
1929 prompts. these are calculated from the CONTEXT variable passed in."
1930 (if (not context) (setq context (semantic-analyze-current-context (point))))
1931 (let* ((syms (semantic-ctxt-current-symbol (point)))
1932 (inp (car (reverse syms))))
1933 (setq syms (nreverse (cdr (nreverse syms))))
1934 (semantic-complete-read-tag-engine
1935 (semantic-collector-analyze-completions
1936 prompt
1937 :buffer (oref context buffer)
1938 :context context)
1939 (semantic-displayor-traditional-with-focus-highlight "simple")
1940 (with-current-buffer (oref context buffer)
1941 (goto-char (cdr (oref context bounds)))
1942 (concat prompt (mapconcat 'identity syms ".")
1943 (if syms "." "")
1944 ))
1945 nil
1946 inp
1947 history)))
1948
1949 (defun semantic-complete-inline-analyzer (context)
1950 "Complete a symbol name by name based on the current context.
1951 This is similar to `semantic-complete-read-tag-analyze', except
1952 that the completion interaction is in the buffer where the context
1953 was calculated from.
1954 CONTEXT is the semantic analyzer context to start with.
1955 Customize `semantic-complete-inline-analyzer-displayor-class'
1956 to control how completion options are displayed.
1957
1958 See `semantic-complete-inline-tag-engine' for details on how
1959 completion works."
1960 (if (not context) (setq context (semantic-analyze-current-context (point))))
1961 (if (not context) (error "Nothing to complete on here"))
1962 (let* ((collector (semantic-collector-analyze-completions
1963 "inline"
1964 :buffer (oref context buffer)
1965 :context context))
1966 (syms (semantic-ctxt-current-symbol (point)))
1967 (rsym (reverse syms))
1968 (thissym (car rsym))
1969 (nextsym (car-safe (cdr rsym)))
1970 (complst nil))
1971 (when (and thissym (or (not (string= thissym ""))
1972 nextsym))
1973 ;; Do a quick calcuation of completions.
1974 (semantic-collector-calculate-completions
1975 collector thissym nil)
1976 ;; Get the master list
1977 (setq complst (semanticdb-strip-find-results
1978 (semantic-collector-all-completions collector thissym)))
1979 ;; Shorten by name
1980 (setq complst (semantic-unique-tag-table-by-name complst))
1981 (if (or (and (= (length complst) 1)
1982 ;; Check to see if it is the same as what is there.
1983 ;; if so, we can offer to complete.
1984 (let ((compname (semantic-tag-name (car complst))))
1985 (not (string= compname thissym))))
1986 (> (length complst) 1))
1987 ;; There are several options. Do the completion.
1988 (semantic-complete-inline-tag-engine
1989 collector
1990 (funcall semantic-complete-inline-analyzer-displayor-class
1991 "inline displayor")
1992 ;;(semantic-displayor-tooltip "simple")
1993 (oref context buffer)
1994 (car (oref context bounds))
1995 (cdr (oref context bounds))
1996 ))
1997 )))
1998
1999 (defcustom semantic-complete-inline-analyzer-idle-displayor-class
2000 'semantic-displayor-ghost
2001 "*Class for displayor to use with inline completion at idle time."
2002 :group 'semantic
2003 :type semantic-complete-inline-custom-type
2004 )
2005
2006 (defun semantic-complete-inline-analyzer-idle (context)
2007 "Complete a symbol name by name based on the current context for idle time.
2008 CONTEXT is the semantic analyzer context to start with.
2009 This function is used from `semantic-idle-completions-mode'.
2010
2011 This is the same as `semantic-complete-inline-analyzer', except that
2012 it uses `semantic-complete-inline-analyzer-idle-displayor-class'
2013 to control how completions are displayed.
2014
2015 See `semantic-complete-inline-tag-engine' for details on how
2016 completion works."
2017 (let ((semantic-complete-inline-analyzer-displayor-class
2018 semantic-complete-inline-analyzer-idle-displayor-class))
2019 (semantic-complete-inline-analyzer context)
2020 ))
2021
2022 \f
2023 ;;;###autoload
2024 (defun semantic-complete-jump-local ()
2025 "Jump to a local semantic symbol."
2026 (interactive)
2027 (let ((tag (semantic-complete-read-tag-buffer-deep "Jump to symbol: ")))
2028 (when (semantic-tag-p tag)
2029 (push-mark)
2030 (goto-char (semantic-tag-start tag))
2031 (semantic-momentary-highlight-tag tag)
2032 (message "%S: %s "
2033 (semantic-tag-class tag)
2034 (semantic-tag-name tag)))))
2035
2036 ;;;###autoload
2037 (defun semantic-complete-jump ()
2038 "Jump to a semantic symbol."
2039 (interactive)
2040 (let* ((tag (semantic-complete-read-tag-project "Jump to symbol: ")))
2041 (when (semantic-tag-p tag)
2042 (push-mark)
2043 (semantic-go-to-tag tag)
2044 (switch-to-buffer (current-buffer))
2045 (semantic-momentary-highlight-tag tag)
2046 (message "%S: %s "
2047 (semantic-tag-class tag)
2048 (semantic-tag-name tag)))))
2049
2050 ;;;###autoload
2051 (defun semantic-complete-jump-local-members ()
2052 "Jump to a semantic symbol."
2053 (interactive)
2054 (let* ((tag (semantic-complete-read-tag-local-members "Jump to symbol: ")))
2055 (when (semantic-tag-p tag)
2056 (let ((start (condition-case nil (semantic-tag-start tag)
2057 (error nil))))
2058 (unless start
2059 (error "Tag %s has no location" (semantic-format-tag-prototype tag)))
2060 (push-mark)
2061 (goto-char start)
2062 (semantic-momentary-highlight-tag tag)
2063 (message "%S: %s "
2064 (semantic-tag-class tag)
2065 (semantic-tag-name tag))))))
2066
2067 ;;;###autoload
2068 (defun semantic-complete-analyze-and-replace ()
2069 "Perform prompt completion to do in buffer completion.
2070 `semantic-analyze-possible-completions' is used to determine the
2071 possible values.
2072 The minibuffer is used to perform the completion.
2073 The result is inserted as a replacement of the text that was there."
2074 (interactive)
2075 (let* ((c (semantic-analyze-current-context (point)))
2076 (tag (save-excursion (semantic-complete-read-tag-analyzer "" c))))
2077 ;; Take tag, and replace context bound with its name.
2078 (goto-char (car (oref c bounds)))
2079 (delete-region (point) (cdr (oref c bounds)))
2080 (insert (semantic-tag-name tag))
2081 (message "%S" (semantic-format-tag-summarize tag))))
2082
2083 ;;;###autoload
2084 (defun semantic-complete-analyze-inline ()
2085 "Perform prompt completion to do in buffer completion.
2086 `semantic-analyze-possible-completions' is used to determine the
2087 possible values.
2088 The function returns immediately, leaving the buffer in a mode that
2089 will perform the completion.
2090 Configure `semantic-complete-inline-analyzer-displayor-class' to change
2091 how completion options are displayed."
2092 (interactive)
2093 ;; Only do this if we are not already completing something.
2094 (if (not (semantic-completion-inline-active-p))
2095 (semantic-complete-inline-analyzer
2096 (semantic-analyze-current-context (point))))
2097 ;; Report a message if things didn't startup.
2098 (if (and (called-interactively-p 'any)
2099 (not (semantic-completion-inline-active-p)))
2100 (message "Inline completion not needed.")
2101 ;; Since this is most likely bound to something, and not used
2102 ;; at idle time, throw in a TAB for good measure.
2103 (semantic-complete-inline-TAB)))
2104
2105 ;;;###autoload
2106 (defun semantic-complete-analyze-inline-idle ()
2107 "Perform prompt completion to do in buffer completion.
2108 `semantic-analyze-possible-completions' is used to determine the
2109 possible values.
2110 The function returns immediately, leaving the buffer in a mode that
2111 will perform the completion.
2112 Configure `semantic-complete-inline-analyzer-idle-displayor-class'
2113 to change how completion options are displayed."
2114 (interactive)
2115 ;; Only do this if we are not already completing something.
2116 (if (not (semantic-completion-inline-active-p))
2117 (semantic-complete-inline-analyzer-idle
2118 (semantic-analyze-current-context (point))))
2119 ;; Report a message if things didn't startup.
2120 (if (and (called-interactively-p 'interactive)
2121 (not (semantic-completion-inline-active-p)))
2122 (message "Inline completion not needed.")))
2123
2124 ;;;###autoload
2125 (defun semantic-complete-self-insert (arg)
2126 "Like `self-insert-command', but does completion afterwards.
2127 ARG is passed to `self-insert-command'. If ARG is nil,
2128 use `semantic-complete-analyze-inline' to complete."
2129 (interactive "p")
2130 ;; If we are already in a completion scenario, exit now, and then start over.
2131 (semantic-complete-inline-exit)
2132
2133 ;; Insert the key
2134 (self-insert-command arg)
2135
2136 ;; Prepare for doing completion, but exit quickly if there is keyboard
2137 ;; input.
2138 (when (save-window-excursion
2139 (save-excursion
2140 (and (not (semantic-exit-on-input 'csi
2141 (semantic-fetch-tags)
2142 (semantic-throw-on-input 'csi)
2143 nil))
2144 (= arg 1)
2145 (not (semantic-exit-on-input 'csi
2146 (semantic-analyze-current-context)
2147 (semantic-throw-on-input 'csi)
2148 nil)))))
2149 (condition-case nil
2150 (semantic-complete-analyze-inline)
2151 ;; Ignore errors. Seems likely that we'll get some once in a while.
2152 (error nil))
2153 ))
2154
2155 (provide 'semantic/complete)
2156
2157 ;; Local variables:
2158 ;; generated-autoload-file: "loaddefs.el"
2159 ;; generated-autoload-load-name: "semantic/complete"
2160 ;; End:
2161
2162 ;;; semantic/complete.el ends here