[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Tue, 11 Aug 2009 03:01:57 +0000 (UTC)
This isn't a patch to completing-read, instead it is a new function. I think
of it more as a facade with bells and whistles. In summary, it makes prompting
for user input easy requiring terse, in the context of a function invocation,
code for this purpose.
(defun read-completing-choice (prompt choices &optional return-as-string
history default allow-empty-p
"Read from the user a choice.
PROMPT is a string to prompt with; normally it ends in a colon and a space.
CHOICES the list of things to auto-complete and allow the user to choose
from. Each element is analyzed independently If each element is not a
string, it is written with `prin1-to-string'.
RETURN-AS-STRING is non-nil, return the symbol as a string
If REQUIRE-MATCH is non-nil, the user is not allowed to exit unless
the input is (or completes to) an element of TABLE or is null.
If it is also not t, Return does not exit if it does non-null completion.
If INITIAL-CONTENTS is non-nil, insert it in the minibuffer initially.
If it is (STRING . POSITION), the initial input
is STRING, but point is placed POSITION characters into the string.
HISTORY, if non-nil, specifies a history list
and optionally the initial position in the list.
It can be a symbol, which is the history list variable to use,
or it can be a cons cell (HISTVAR . HISTPOS).
In that case, HISTVAR is the history list variable to use,
and HISTPOS is the initial position (the position in the list
which INITIAL-CONTENTS corresponds to).
If HISTORY is `t', no history will be recorded.
Positions are counted starting from 1 at the beginning of the list.
DEFAULT, if non-nil, will be returned when the user enters an empty
ALLOW-EMPTY-P, if non-nil, allow no data (empty string) to be returned. In
this case, nil is returned, otherwise, an error is raised.
NO-INITIAL-CONTENTS-ON-SINGLETON-P, if non-nil, don't populate with initialial
contents when there is only one choice to pick from.
ADD-PROMPT-DEFAULT-P, if non-nil, munge the prompt using the default notation
\(i.e. `<Prompt> (default CHOICE)')."
(let* ((choice-alist-p (listp (car choices)))
(choice-options (if choice-alist-p (mapcar #'car choices) choices))
(sym-list (mapcar #'(lambda (arg)
(t (prin1-to-string arg))
(initial (if initial-contents
(if (symbolp initial-contents)
(def (if default
(symbol default (symbol-name default))
(when (not no-initial-contents-on-singleton-p)
(if (and (null initial) (= 1 (length sym-list)))
(setq initial (car (car sym-list))))
(if (and (null initial)
;; cases where a default is given and the user can't then just
;; press return; instead, the user has to clear the minibuffer
;; contents first
(setq tc (try-completion "" sym-list)))
(setq initial tc))))
(if (and add-prompt-default-p def)
(concat prompt (format " (default %s): " def))))
(setq res-str (completing-read prompt sym-list nil
(if (or allow-empty-p (> (length res-str) 0))
"Input required or type `\\[keyboard-quit]' to quit"))
(when (> (length res-str) 0)
(let ((choices (if (symbolp (caar choices))
(mapcar #'(lambda (arg)
(cons (symbol-name (car arg))
(setq res-str (cdr (assoc res-str choices))))
- completing-read enhancement,
Paul Landes <=