comparison lisp/cedet/semantic/util.el @ 104508:90ca5d588aa9

* cedet/semantic.el: Add autoloads for semantic/idle functions. * cedet/semantic/util.el (semantic--completion-cache): New var. (semantic-symbol-start, semantic-find-tag-for-completion) (semantic-complete-symbol): New functions, adapted from Senator. * bindings.el (complete-symbol): Try semantic-complete-symbol if no tag table is active. * cedet/semantic/idle.el (define-semantic-idle-service): Doc fix.
author Chong Yidong <cyd@stupidchicken.com>
date Mon, 21 Sep 2009 15:59:48 +0000
parents 8db96f200ac8
children a6a812dd2d88
comparison
equal deleted inserted replaced
104507:51e316109fba 104508:90ca5d588aa9
435 (setq o (cons (car over) o))) 435 (setq o (cons (car over) o)))
436 (setq over (cdr over))) 436 (setq over (cdr over)))
437 (message "Remaining overlays: %S" o))) 437 (message "Remaining overlays: %S" o)))
438 over) 438 over)
439 439
440 ;;; Interactive commands (from Senator).
441
442 ;; The Senator library from upstream CEDET is not included in the
443 ;; built-in version of Emacs. The plan is to fold it into the
444 ;; different parts of CEDET and Emacs, so that it works
445 ;; "transparently". Here are some interactive commands based on
446 ;; Senator.
447
448 (defvar semantic--completion-cache nil
449 "Internal variable used by `senator-complete-symbol'.")
450
451 (defsubst semantic-symbol-start (pos)
452 "Return the start of the symbol at buffer position POS."
453 (car (nth 2 (semantic-ctxt-current-symbol-and-bounds pos))))
454
455 (defun semantic-find-tag-for-completion (prefix)
456 "Find all tags with name starting with PREFIX.
457 This uses `semanticdb' when available."
458 (let (result ctxt)
459 (condition-case nil
460 (and (featurep 'semantic/analyze)
461 (setq ctxt (semantic-analyze-current-context))
462 (setq result (semantic-analyze-possible-completions ctxt)))
463 (error nil))
464 (or result
465 ;; If the analyzer fails, then go into boring completion.
466 (if (and (featurep 'semantic/db) (semanticdb-minor-mode-p))
467 (semanticdb-fast-strip-find-results
468 (semanticdb-deep-find-tags-for-completion prefix))
469 (semantic-deep-find-tags-for-completion prefix (current-buffer))))))
470
471 (defun semantic-complete-symbol (&optional predicate)
472 "Complete the symbol under point, using Semantic facilities.
473 When called from a program, optional arg PREDICATE is a predicate
474 determining which symbols are considered."
475 (interactive)
476 (let* ((start (car (nth 2 (semantic-ctxt-current-symbol-and-bounds
477 (point)))))
478 (pattern (regexp-quote (buffer-substring start (point))))
479 collection completion)
480 (when start
481 (if (and semantic--completion-cache
482 (eq (nth 0 semantic--completion-cache) (current-buffer))
483 (= (nth 1 semantic--completion-cache) start)
484 (save-excursion
485 (goto-char start)
486 (looking-at (nth 3 semantic--completion-cache))))
487 ;; Use cached value.
488 (setq collection (nthcdr 4 semantic--completion-cache))
489 ;; Perform new query.
490 (setq collection (semantic-find-tag-for-completion pattern))
491 (setq semantic--completion-cache
492 (append (list (current-buffer) start 0 pattern)
493 collection))))
494 (if (null collection)
495 (let ((str (if pattern (format " for \"%s\"" pattern) "")))
496 (if (window-minibuffer-p (selected-window))
497 (minibuffer-message (format " [No completions%s]" str))
498 (message "Can't find completion%s" str)))
499 (setq completion (try-completion pattern collection predicate))
500 (if (string= pattern completion)
501 (let ((list (all-completions pattern collection predicate)))
502 (setq list (sort list 'string<))
503 (if (> (length list) 1)
504 (with-output-to-temp-buffer "*Completions*"
505 (display-completion-list list pattern))
506 ;; Bury any out-of-date completions buffer.
507 (let ((win (get-buffer-window "*Completions*" 0)))
508 (if win (with-selected-window win (bury-buffer))))))
509 ;; Exact match
510 (delete-region start (point))
511 (insert completion)
512 ;; Bury any out-of-date completions buffer.
513 (let ((win (get-buffer-window "*Completions*" 0)))
514 (if win (with-selected-window win (bury-buffer))))))))
515
440 (provide 'semantic/util) 516 (provide 'semantic/util)
441 517
442 ;;; Minor modes 518 ;;; Minor modes
443 ;; 519 ;;
444 (require 'semantic/util-modes) 520 (require 'semantic/util-modes)