home *** CD-ROM | disk | FTP | other *** search
- ;;;; prompt.jl -- Prompt in a buffer with completion
- ;;; Copyright (C) 1993, 1994 John Harper <jsh@ukc.ac.uk>
-
- ;;; This file is part of Jade.
-
- ;;; Jade is free software; you can redistribute it and/or modify it
- ;;; under the terms of the GNU General Public License as published by
- ;;; the Free Software Foundation; either version 2, or (at your option)
- ;;; any later version.
-
- ;;; Jade is distributed in the hope that it will be useful, but
- ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
- ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ;;; GNU General Public License for more details.
-
- ;;; You should have received a copy of the GNU General Public License
- ;;; along with Jade; see the file COPYING. If not, write to
- ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
-
- (setq
- prompt-keymap (make-keylist)
- prompt-esc-keymap (make-keylist)
- ;; stack of buffers which can be used for prompts
- prompt-buffer-list ())
-
- (bind-keys prompt-keymap
- "esc" '(setq next-keymap-path '(prompt-esc-keymap esc-keymap))
- "tab" 'prompt-complete-word
- "return" 'prompt-enter-line
- "ctrl-g" '(throw 'prompt nil))
- (bind-keys prompt-esc-keymap
- "?" 'prompt-print-word-completions)
-
- (defun prompt2 (prompt-comp-func &optional prompt-title prompt-start word-regexps)
- "(prompt2 COMP-FUNC [TITLE] [START] [WORD-REGEXPS])
- Prompts for a string using completion. COMP-FUNC is a function which takes
- one argument, the string which should be completed. It should return a list
- of all matches. TITLE is the optional title to print in the buffer, START the
- original contents of the buffer. WORD-REGEXPS is a vector of two regexps
- corresponding to the values of `word-regexp' and `word-not-regexp' for the
- completion mechanism.
- The string entered is returned, or nil if the prompt is cancelled (by Ctrl-g)."
- (let*
- (prompt-buffer
- prompt-line-pos
- prompt-completions-pos
- result)
- (if prompt-buffer-list
- (setq
- prompt-buffer (car prompt-buffer-list)
- prompt-buffer-list (cdr prompt-buffer-list))
- (setq prompt-buffer (make-buffer "*prompt*")))
- (setq buffer-list (cons prompt-buffer buffer-list))
- (set-buffer-special prompt-buffer t)
- (with-buffer prompt-buffer
- (if word-regexps
- (setq
- word-regexp (aref word-regexps 0)
- word-not-regexp (aref word-regexps 1))
- (setq
- word-regexp "."
- word-not-regexp "^|$"))
- (if (stringp prompt-title)
- (insert prompt-title)
- (insert "Enter string:"))
- (split-line)
- (if (stringp prompt-start)
- (format (current-buffer) "\n%s\n\n" prompt-start)
- (insert "\n\n\n"))
- (insert "::Completions::\n")
- (setq prompt-completions-pos (cursor-pos))
- (goto (line-end (prev-line 3)))
- (setq keymap-path '(prompt-keymap global-keymap))
- (setq prompt-line-pos (catch 'prompt (recursive-edit)))
- (setq buffer-list (delq prompt-buffer buffer-list))
- (when (posp prompt-line-pos)
- (setq result (copy-area (line-start prompt-line-pos) prompt-line-pos))))
- (clear-buffer prompt-buffer)
- (setq prompt-buffer-list (cons prompt-buffer prompt-buffer-list))
- result))
-
- (defun prompt-enter-line ()
- (throw 'prompt (if (> (cursor-pos) prompt-completions-pos)
- (line-end)
- (cursor-pos))))
-
- (defun prompt-complete-word ()
- (let*
- (word
- word-pos
- comp-list
- num-found)
- (setq
- word-pos (unless (word-start (left-char)) (line-start))
- word (copy-area word-pos (cursor-pos))
- comp-list (funcall prompt-comp-func word)
- num-found (length comp-list))
- (cond
- ((= num-found 0)
- (beep)
- (delete-area prompt-completions-pos (file-end))
- (title "No completions."))
- ((= num-found 1)
- (goto (replace-string word (car comp-list) word-pos))
- (delete-area prompt-completions-pos (file-end))
- (title "Unique completion."))
- (t
- (beep)
- (prompt-print-completions comp-list)
- (goto (replace-string word (make-completion-string word comp-list) word-pos))
- (format t "%d completions." num-found)))))
-
- (defun prompt-print-completions (comp-list &optional insert-pos)
- (let*
- ((ipos (dup-pos prompt-completions-pos)))
- (delete-area ipos (file-end))
- (insert "\n" ipos)
- (while (consp comp-list)
- (format (cons (current-buffer) ipos) "%s\n" (car comp-list))
- (setq comp-list (cdr comp-list)))))
-
- (defun prompt-print-word-completions ()
- (prompt-print-completions
- (funcall prompt-comp-func
- (copy-area (unless (word-start (left-char))
- (line-start)) (cursor-pos)))))
-
- (defun prompt-complete-symbol (word)
- (mapcar 'symbol-name (apropos (concat ?^ word))))
- (defun prompt-complete-function (word)
- (mapcar 'symbol-name (apropos (concat ?^ word) 'fboundp)))
- (defun prompt-complete-variable (word)
- (mapcar 'symbol-name (apropos (concat ?^ word) 'boundp)))
- (defun prompt-complete-buffer (word)
- (delete-if-not #'(lambda (b) (string-head-eq b word))
- (mapcar 'buffer-name buffer-list)))
-
- (defun prompt-complete-filename (word)
- (let*
- ((path (path-name word))
- (file (base-name word))
- (files (directory-files path)))
- (mapcar
- #'(lambda (x &aux y)
- (when (file-directory-p (setq y (concat path x)))
- (setq y (concat y ?/)))
- y)
- (delete-if-not #'(lambda (f) (string-head-eq f file)) files))))
-
- (defun prompt-for-file (&optional prompt start-name)
- (unless (stringp prompt)
- (setq prompt "Enter filename:"))
- (prompt2 'prompt-complete-filename prompt start-name))
-
- (defun prompt-for-buffer (&optional prompt start)
- (when (bufferp start)
- (setq start (buffer-name start)))
- (unless (stringp prompt)
- (setq prompt "Enter buffer name:"))
- (prompt2 'prompt-complete-buffer prompt start))
-
- (defun prompt-for-symbol (&optional prompt start)
- (when (and (symbolp start) (not (null start)))
- (setq start (symbol-name start)))
- (unless (stringp prompt)
- (setq prompt "Enter name of symbol:"))
- (intern (prompt2 'prompt-complete-symbol prompt start symbol-word-regexps)))
-
- (defun lisp-prompt (prompt &optional start)
- (prompt2 'prompt-complete-symbol prompt start symbol-word-regexps))
-
- (defun prompt-for-function (&optional prompt start)
- (when (and (symbolp start) (not (null start)))
- (setq start (symbol-name start)))
- (unless (stringp prompt)
- (setq prompt "Enter name of function:"))
- (intern (prompt2 'prompt-complete-function prompt start symbol-word-regexps)))
-
- (defun prompt-for-variable (&optional prompt start)
- (when (and (symbolp start) (not (null start)))
- (setq start (symbol-name start)))
- (unless (stringp prompt)
- (setq prompt "Enter name of variable:"))
- (intern (prompt2 'prompt-complete-variable prompt start symbol-word-regexps)))
-
- (defun prompt-complete-from-list (word)
- (let
- ((src prompt-list)
- (dst ()))
- (while src
- (when (string-head-eq (car src) word)
- (setq dst (cons (car src) dst)))
- (setq src (cdr src)))
- dst))
-
- (defun prompt-from-list (prompt-list prompt &optional start)
- (prompt2 'prompt-complete-from-list prompt start))
-