交互式“r”带有附加参数的 elisp defun ?

发布于 2024-10-31 07:40:32 字数 513 浏览 4 评论 0原文

是否可以使用具有附加可选参数的代码“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千里故人稀 2024-11-07 07:40:32

要使交互式要求多个参数,请用换行符分隔它们。例如,如果您希望将第三个参数绑定到前缀参数的值,请像这样定义您的函数:

(defun my-function (start end &optional arg)
  "Do something with selected region"
  (interactive "r\np")
  (if arg
      (setq val arg)
    (setq val 2))
  (do things...))

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:

(defun my-function (start end &optional arg)
  "Do something with selected region"
  (interactive "r\np")
  (if arg
      (setq val arg)
    (setq val 2))
  (do things...))

M-x describe-function interactive gives you further information.

给我一枪 2024-11-07 07:40:32

可以通过两种方式调用函数:

  • 交互:这是当用户调用命令时发生的情况,例如当它绑定到某个键时。
  • 来自 lisp:当该函数被另一个 lisp 函数调用时。例如(r 100 200 t)

在您的情况下,您必须确保参数与交互规范匹配,在这种情况下它必须接受两个参数。第三个在交互调用时不会被使用(因此它将获得值nil)。

NO I/O 意味着它不会提示用户输入(就像要求文件名时那样)。

如果您希望函数根据区域何时处于活动状态而采取不同的行为,您可以询问函数(use-region-p)

A function can be called in two ways:

  • Interactively: This is what happens when a user calls the command, e.g. when it has been bound to a key.
  • From lisp: When the function is called from another lisp function. e.g. (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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文