我需要提示用户输入命令,使用 read-command
,然后根据用户是否在迷你缓冲区上输入空字符串来执行操作。
大多数时候,read-command
返回有效命令。但是,当用户输入空白字符串(只需在迷你缓冲区中按 Enter 键)时,会返回一个奇怪的对象,它既不是 nil
,也不是字符串,也不是命令(根据 stringp
和 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
我怎样才能:
- 访问该对象的读取语法?
- 确定对象的类型,无需与定义的每个类型谓词进行匹配 here
- 测试该对象是否相等?
- 为什么是
read-command
行为不端?规范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]
我预计nil
,因为这就是我传递的 DEFAULT-VALUE
,但显然read-command
不在乎。
我尝试加载 minibuf.c
的源代码看看发生了什么,但我也无法让它发挥作用。我已经下载了emacs-23.3b.tar.gz
来自here并让 emacs 在那里寻找源代码,但找不到它。 minibuf.c
也不行。似乎存在在那里。非常令人沮丧的事情,我将不胜感激任何指点。
最佳答案
Elisp 手册是您的 friend 。 C-h i
,选择 Elisp。然后i
并输入read-command
。作为描述的一部分,您会看到以下内容:
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.
阅读完整说明。但仅此一点就可以帮助您理解。返回的值是一个名称为空的符号。
关于emacs - (读取命令...)在空白输入时返回奇怪的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19106759/