home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / emacs-19.28-src.tgz / tar.out / fsf / emacs / lisp / icomplete.el < prev    next >
Lisp/Scheme  |  1996-09-28  |  11KB  |  264 lines

  1. ;;; icomplete.el - minibuffer completion incremental feedback
  2. ;;; This package is in the public domain.
  3.  
  4. ;;; Author: Ken Manheimer <klm@nist.gov>
  5. ;;; Maintainer: Ken Manheimer <klm@nist.gov>
  6. ;;; Version: icomplete.el,v 3.3 1993/12/11 11:27:35 klm Exp klm
  7. ;;; Created: Mar 1993 klm@nist.gov - first release to usenet
  8. ;;; Keywords: help, abbrev
  9.  
  10. ;;; Commentary:
  11.  
  12. ;;; Loading this package implements a more finely-grained completion
  13. ;;; feedback scheme, indicating, within the minibuffer, the
  14. ;;; prospective minibuffer completion candidates, as you type.  See
  15. ;;; the documentation string for 'icomplete-prompt' for a specific
  16. ;;; description of icompletion.
  17.  
  18. ;;; It should run on most version of Emacs 19 (including Lucid emacs
  19. ;;; 19 - thanks to the efforts of Colin Rafferty (craffert@lehman.com)
  20. ;;; - thanks, Colin!)  This version of icomplete will *not* work on
  21. ;;; Emacs 18 versions - the elisp archives, at
  22. ;;; archive.cis.ohio-state.edu:/pub/gnu/emacs/elisp-archive, probably
  23. ;;; still has a version that works in GNU Emacs v18.
  24.  
  25. ;;; Thanks to Colin Rafferty for assistance reconciling for lemacs,
  26. ;;; and to Michael Cook, who implemented an incremental completion
  27. ;;; style in his 'iswitch' functions that served as the basis for
  28. ;;; icomplete.
  29.  
  30. ;;; Code:
  31.  
  32. ;;;_* (Allout outline root topic.  Please leave this in.)
  33.  
  34. ;;;_ + Provide
  35. (provide 'icomplete)
  36.  
  37. ;;;_ + User Customization variables
  38. ;;;_  = icomplete-inhibit
  39. (defvar icomplete-inhibit nil
  40.   "*Set this variable to t at any time to inhibit icomplete.")
  41.  
  42. ;;;_ + Internal Variables
  43. ;;;_  = icomplete-eoinput 1
  44. (defvar icomplete-eoinput 1
  45.   "Point where minibuffer input ends and completion info begins.")
  46. (make-variable-buffer-local 'icomplete-eoinput)
  47.  
  48. ;;;_ > icomplete-prime-session ()
  49. ;;;###autoload
  50. (defun icomplete-prime-session ()
  51.  
  52.   "Prep emacs v 19 for more finely-grained minibuffer completion-feedback.
  53.  
  54. You can inhibit icomplete after loading by setting icomplete-inhibit
  55. non-nil.  Set the var back to nil to re-enable icomplete."
  56.  
  57.   ;; For emacs v19.18 and later revs, the icomplete key function is
  58.   ;; installed in 'minibuffer-setup-hook'.  Global pre- and post-
  59.   ;; command-hook functions are used in v19.17 and earlier v19 revs."
  60.  
  61.   (let* ((v19-rev (and (string-match "^19\\.\\([0-9]+\\)" emacs-version)
  62.                (string-to-int (substring emacs-version
  63.                          (match-beginning 1)
  64.                          (match-end 1))))))
  65.  
  66.     (cond ((and v19-rev            ; emacs v 19, some rev,
  67.         (> v19-rev 17))
  68.        ;; Post v19rev17, has minibuffer-setup-hook, use it:
  69.        (add-hook 'minibuffer-setup-hook 'icomplete-prime-minibuffer))
  70.       (v19-rev
  71.        ;; v19rev17 and prior (including lucid): use global
  72.        ;; pre- and post-command-hooks, instead:
  73.        (add-hook 'pre-command-hook 'icomplete-pre-command-hook 'append)
  74.        (add-hook 'post-command-hook
  75.              'icomplete-post-command-hook 'append))
  76.       ((format "icomplete: non v19 emacs, %s - %s"
  77.            emacs-version "try elisp-archive icomplete")))))
  78.  
  79. ;;;_ > icomplete-prime-minibuffer ()
  80. (defun icomplete-prime-minibuffer ()
  81.   "Prep emacs, v 19.18 or later, for icomplete.
  82. \(In emacs v19.17 and earlier, and in lemacs, icomplete-prime-session
  83. is used, instead to establish global hooks.\)
  84.  
  85. Run via `minibuffer-setup-hook', adds icomplete pre- and post-command
  86. hooks at the start of each minibuffer."
  87.  
  88.   ;; Append the hooks to avoid as much as posssible interference from
  89.   ;; other hooks that foul up minibuffer quit.
  90.   (make-local-variable 'pre-command-hook)
  91.   (make-local-variable 'post-command-hook)
  92.   (setq pre-command-hook (copy-sequence pre-command-hook))
  93.   (setq post-command-hook (copy-sequence post-command-hook))
  94.   (add-hook 'pre-command-hook 'icomplete-pre-command-hook)
  95.   (add-hook 'post-command-hook 'icomplete-post-command-hook))
  96.  
  97. ;;;_ > icomplete-window-minibuffer-p ()
  98. (defmacro icomplete-window-minibuffer-p ()
  99.  
  100.   "Returns non-nil if current window is a minibuffer window.
  101.  
  102. Trivially equates to '(window-minibuffer-p (selected-window))', with
  103. the argument definitely provided for emacsen that require it, eg Lucid."
  104.  
  105.   '(window-minibuffer-p (selected-window)))
  106.  
  107. ;;;_ + Completion
  108.  
  109. ;;;_  - Completion feedback hooks
  110. ;;;_   > icomplete-pre-command-hook ()
  111. (defun icomplete-pre-command-hook ()
  112.   "Cleanup completions display before user's new command is dealt with."
  113.   (if (and (icomplete-window-minibuffer-p)
  114.        (not executing-macro)
  115.        (not (symbolp minibuffer-completion-table))
  116.        (not icomplete-inhibit))
  117.       (if (and (boundp 'icomplete-eoinput)
  118.            icomplete-eoinput)
  119.       (if (> icomplete-eoinput (point-max))
  120.           ;; Oops, got rug pulled out from under us - reinit:
  121.           (setq icomplete-eoinput (point-max))
  122.         (let ((buffer-undo-list buffer-undo-list )) ; prevent entry
  123.           (delete-region icomplete-eoinput (point-max))))
  124.     ;; Reestablish the local variable 'cause minibuffer-setup is weird:
  125.     (make-local-variable 'icomplete-eoinput)
  126.     (setq icomplete-eoinput 1))))
  127. ;;;_   > icomplete-post-command-hook ()
  128. (defun icomplete-post-command-hook ()
  129.   "Exhibit completions, leaving bookkeeping so pre- hook can tidy up."
  130.  
  131.   (if (and (icomplete-window-minibuffer-p)    ; ... in a minibuffer.
  132.        (not executing-macro)
  133.        (not icomplete-inhibit)    ; ... not specifically inhibited.
  134.        ;(sit-for 0)            ; ... redisplay and if there's input
  135.                     ; waiting, then don't icomplete
  136.                     ; (stigs suggestion) (too jumpy!)
  137.        ;; Inhibit for file-name and other custom-func completions:
  138.        (not (symbolp minibuffer-completion-table))
  139.        )
  140.       (let ((buffer-undo-list buffer-undo-list ))    ; prevent entry
  141.     (icomplete-exhibit))))
  142. ;;;_   > icomplete-window-setup-hook ()
  143. (defun icomplete-window-setup-hook ()
  144.   "Exhibit completions, leaving bookkeeping so pre- hook can tidy up."
  145.  
  146.   (if (and (icomplete-window-minibuffer-p)    ; ... in a minibuffer.
  147.        )
  148.       (message "ic ws doing")(sit-for 1)))
  149. ;;;_   > icomplete-exhibit ()
  150. (defun icomplete-exhibit ()
  151.   "Insert icomplete completions display."
  152.   (if (not (symbolp minibuffer-completion-table))
  153.       (let ((contents (buffer-substring (point-min)(point-max)))
  154.         (buffer-undo-list t))
  155.     (save-excursion
  156.       (goto-char (point-max))
  157.                                         ; Register the end of input, so we
  158.                                         ; know where the extra stuff
  159.                                         ; (match-status info) begins:
  160.       (if (not (boundp 'icomplete-eoinput))
  161.           ;; In case it got wiped out by major mode business:
  162.           (make-local-variable 'icomplete-eoinput))
  163.       (setq icomplete-eoinput (point))
  164.                                         ; Insert the match-status information:
  165.       (if (> (point-max) 1)
  166.           (insert-string
  167.         (icomplete-prompt contents
  168.                   minibuffer-completion-table
  169.                   minibuffer-completion-predicate
  170.                   (not
  171.                    minibuffer-completion-confirm))))))))
  172.  
  173. ;;;_  - Completion feedback producer
  174. ;;;_   > icomplete-prompt (name candidates predicate require-match)
  175. (defun icomplete-prompt (name candidates predicate require-match)
  176.   "Identify prospective candidates for minibuffer completion.
  177.  
  178. The display is updated with each minibuffer keystroke during
  179. minibuffer completion.
  180.  
  181. Prospective completion suffixes (if any) are displayed, bracketed by
  182. one of \(), \[], or \{} pairs.  The choice of brackets is as follows:
  183.  
  184.   \(...) - a single prospect is identified and matching is enforced,
  185.   \[...] - a single prospect is identified but matching is optional, or
  186.   \{...} - multiple prospects, separated by commas, are indicated, and
  187.           further input is required to distingish a single one.
  188.  
  189. The displays for disambiguous matches have \" [Matched]\" appended
  190. \(whether complete or not), or \" \[No matches]\", if no eligible
  191. matches exist."
  192.  
  193.   (let ((comps (all-completions name candidates predicate))
  194.                                         ; "-determined" - only one candidate
  195.         (open-bracket-determined (if require-match "(" "["))
  196.         (close-bracket-determined (if require-match ")" "]"))
  197.                                         ;"-prospects" - more than one candidate
  198.         (open-bracket-prospects "{")
  199.         (close-bracket-prospects "}")
  200.         )
  201.     (cond ((null comps) (format " %sNo matches%s"
  202.                                 open-bracket-determined
  203.                                 close-bracket-determined))
  204.           ((null (cdr comps))           ;one match
  205.            (concat (if (and (> (length (car comps))
  206.                                (length name)))
  207.                        (concat open-bracket-determined
  208.                                (substring (car comps) (length name))
  209.                                close-bracket-determined)
  210.                      "")
  211.                    " [Matched]"))
  212.           (t                            ;multiple matches
  213.            (let* ((most (try-completion name candidates predicate))
  214.                   (most-len (length most))
  215.                   most-is-exact
  216.                   (alternatives
  217.                    (apply
  218.                     'concat
  219.                     (cdr (apply 'append
  220.                                 (mapcar '(lambda (com)
  221.                                            (if (= (length com) most-len)
  222.                                                ;; Most is one exact match,
  223.                                                ;; note that and leave out
  224.                                                ;; for later indication:
  225.                                                (progn
  226.                                                  (setq most-is-exact t)
  227.                                                  ())
  228.                                              (list ","
  229.                                                    (substring com
  230.                                                               most-len))))
  231.                                         comps))))))
  232.              (concat (and (> most-len (length name))
  233.                           (concat open-bracket-determined
  234.                                   (substring most (length name))
  235.                                   close-bracket-determined))
  236.                      open-bracket-prospects
  237.                      (if most-is-exact
  238.                          (concat "," alternatives)
  239.                        alternatives)
  240.                      close-bracket-prospects))))))
  241.  
  242. ;;;_  - Initialization
  243. (icomplete-prime-session)
  244.  
  245.  
  246. ;;;_ + Local emacs vars.
  247. '(
  248. Local variables:
  249. eval: (save-excursion
  250.         (if (not (condition-case err (outline-mode t)
  251.                    (wrong-number-of-arguments nil)))
  252.             (progn
  253.               (message
  254.                "Allout outline-mode not loaded, not adjusting buffer exposure")
  255.               (sit-for 1))
  256.           (message "Adjusting '%s' visibility" (buffer-name))
  257.           (outline-lead-with-comment-string ";;;_")
  258.           (goto-char 0)
  259.           (outline-exposure -1 0)))
  260. End:)
  261.  
  262. ;;; icomplete.el ends here
  263.  
  264.