在看 Emacs 函数的时候经常看到 interactive,它的用处就是将函数变为可调用的,即可
以通过 M-x 进行调用。只是刚开始了解它的时候对它的参数不太理解,比如文档中出现
了一个示例中有 (interactive "P\nbbuffer: ") ,感觉参数跟乱码一样。不过最近突然
对这个参数理解了,这里记录一下。
参数为 string
这个参数用来限制参数类型及给出提示信息,参数格式为一个 code letter 加上提示信
息,多个参数用 \n 分隔(感觉就是输入完每个参数用回车确认)。可用的 code
letter 有:
a -- Function name: symbol with a function definition.
b -- Name of existing buffer.
B -- Name of buffer, possibly nonexistent.
c -- Character (no input method is used).
C -- Command name: symbol with interactive function definition.
d -- Value of point as number.  Does not do I/O.
D -- Directory name.
e -- Parameterized event (i.e., one that's a list) that invoked this command.
     If used more than once, the Nth `e' returns the Nth parameterized event.
     This skips events that are integers or symbols.
f -- Existing file name.
F -- Possibly nonexistent file name.
G -- Possibly nonexistent file name, defaulting to just directory name.
i -- Ignored, i.e. always nil.  Does not do I/O.
k -- Key sequence (downcase the last event if needed to get a definition).
K -- Key sequence to be redefined (do not downcase the last event).
m -- Value of mark as number.  Does not do I/O.
M -- Any string.  Inherits the current input method.
n -- Number read using minibuffer.
N -- Numeric prefix arg, or if none, do like code `n'.
p -- Prefix arg converted to number.  Does not do I/O.
P -- Prefix arg in raw form.  Does not do I/O.
r -- Region: point and mark as 2 numeric args, smallest first.  Does no I/O.
s -- Any string.  Does not inherit the current input method.
S -- Any symbol.
U -- Mouse up event discarded by a previous k or K argument.
v -- Variable name: symbol that is `custom-variable-p'.
x -- Lisp expression read but not evaluated.
X -- Lisp expression read and evaluated.
z -- Coding system.
Z -- Coding system, nil if no prefix arg.
这样再去理解 (interactive "P\nbbuffer: ") 的参数就很简单了。
首先,参数可以分解为 P \n bbuffer: ,表示有两个参数,分别为
P, 表示由C-u提供,并转换成数字。无提示信息;b, 表示参数为一个 buffer 的名字。提示信息为buffer:.
参数为 s-expression
当参数是 s-expression 时,该表达式会在函数被调用时求值,产生真实的参数。Emacs 也
提供了很多函数来实现参数的读取,如 read-buffer read-string 等。以下示例是以
不同方式给定的参数,但效果相同:
1  | (defun bar (arg)  |