I need to prompt the user for a command, using read-command
, then take an action based on whether or not the user entered the empty string on the minibuffer.
Most of the time, read-command
returns a valid command. But when the user enters a blank string (by just hitting enter in the minibuffer), a strange object is returned, which is neither nil
, nor a string, nor a command (according to stringp
and commandp
).
ELISP> (setq strange-object (read-command "Enter some command" nil) )
ELISP> (equal nil strange-object)
nil
ELISP> (if strange-object "YES" "NO")
"YES"
ELISP> (mapcar (lambda (fun) (funcall fun strange-object)) (list 'stringp 'commandp 'char-or-string-p 'functionp 'integerp 'listp) )
(nil nil nil nil nil nil)
ELISP> (prin1-to-string strange-object)
""
ELISP> (equal "" strange-object)
nil
How can I:
read-command
misbehaving? Specification of read-command
:read-command is a built-in function in `minibuf.c'.
(read-command PROMPT &optional DEFAULT-VALUE)
Read the name of a command and return as a symbol. Prompt with PROMPT. By default, return DEFAULT-VALUE or its first element if it is a list.
[back]
I expected nil
, since that is what I passed as DEFAULT-VALUE
, but apparently read-command
doesn't care.
I've tried loading the source of minibuf.c
to see what is going on, but I haven't been able to get that to work either. I've downloaded emacs-23.3b.tar.gz
from here and had emacs look for the source there, but it wasn't able to find it. Neither does a minibuf.c
seem to exist there. Very frustrating stuff, I will appreciate any pointers.
The Elisp manual is your friend. C-h i
, choose Elisp. Then i
and enter read-command
. As part of the description you see this:
The argument DEFAULT specifies what to return if the user enters
null input. It can be a symbol, a string or a list of strings.
If it is a string, `read-command' interns it before returning it.
If it is a list, `read-command' interns the first element of this
list. If DEFAULT is `nil', that means no default has been
specified; then if the user enters null input, the return value is
`(intern "")', that is, a symbol whose name is an empty string.
Read the full description. But this alone should help you understand. The value returned is a symbol that has an empty name.