交互式“r”带有附加参数的 elisp defun ?
是否可以使用具有附加可选参数的代码“r”编写交互式 defun(以便它在选定区域内执行操作,但使用另一个参数)?我想要类似以下内容:
(defun my-function (start end &optional arg)
"Do something with selected region"
(interactive "r")
(if arg
(setq val arg)
(setq val 2))
(do things...))
查看
'r':点和标记,作为两个数字 参数,最小的在前。这是 唯一指定两个的代码字母 连续的争论而不是一个。 无 I/O。
我不确定“无 I/O”和“两个连续参数”是否意味着它需要 2 个且仅需要 2 个参数(即,仅限于该区域的起点和终点作为参数)。虽然它允许我使用附加参数来评估和运行 defun,但 Emacs 似乎忽略了它。
谢谢。
Is it possible to write an interactive defun with code "r" that has an additional optional argument (so that it does things within the selected region, but with another argument)? I would like something like the following:
(defun my-function (start end &optional arg)
"Do something with selected region"
(interactive "r")
(if arg
(setq val arg)
(setq val 2))
(do things...))
Looking at the documentation it says
'r': Point and the mark, as two numeric
arguments, smallest first. This is the
only code letter that specifies two
successive arguments rather than one.
No I/O.
I'm not sure if the 'No I/O' and 'two successive arguments' means that it takes 2 and only 2 arguments (i.e., limited to the region's start and end point as args). Although it allows me to evaluate and run the defun with an additional argument, Emacs appears to be ignoring it.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要使
交互式
要求多个参数,请用换行符分隔它们。例如,如果您希望将第三个参数绑定到前缀参数的值,请像这样定义您的函数:Mxdescribe-functioninteractive
为您提供更多信息。To make
interactive
ask for multiple parameters, separate them with a newline character. For instance, if you want your third parameter be bound to the value of the prefix argument, define your function like this:M-x describe-function interactive
gives you further information.可以通过两种方式调用函数:
(r 100 200 t)
。在您的情况下,您必须确保参数与交互规范匹配,在这种情况下它必须接受两个参数。第三个在交互调用时不会被使用(因此它将获得值
nil
)。NO I/O 意味着它不会提示用户输入(就像要求文件名时那样)。
如果您希望函数根据区域何时处于活动状态而采取不同的行为,您可以询问函数
(use-region-p)
。A function can be called in two ways:
(r 100 200 t)
.In your case, you have to make sure that the arguments match the interactive specification, in this case it must accept two arguments. The third will not be used when called interactively (so then it will get the value
nil
).NO I/O means that it will not prompt the user for input (like it does when it asks for a file name).
If you want your function to act differently depending in when the region is active, you could ask the function
(use-region-p)
.