简体   繁体   English

交互式“r”elisp与另外的args一起定义?

[英]Interactive “r” elisp defun with additional args?

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)? 是否有可能使用代码“r”编写一个具有附加可选参数的交互式defun(以便它在所选区域内执行某些操作,但使用另一个参数)? 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. 'r':点和标记,作为两个数字参数,最小的第一个。 This is the only code letter that specifies two successive arguments rather than one. 这是唯一一个指定两个连续参数而不是一个参数的代码字母。 No I/O. 没有I / O.

I'm not sure if the 'No I/O' and 'two successive arguments' means that it takes 2 and only 2 arguments (ie, limited to the region's start and end point as args). 我不确定'No I / O'和'两个连续的参数'是否意味着它需要2个且只有2个参数(即,仅限于区域的起点和终点作为args)。 Although it allows me to evaluate and run the defun with an additional argument, Emacs appears to be ignoring it. 虽然它允许我用额外的参数来评估和运行defun,但Emacs似乎忽略了它。

Thank you. 谢谢。

To make interactive ask for multiple parameters, separate them with a newline character. 要使interactive请求多个参数,请使用换行符分隔它们。 For instance, if you want your third parameter be bound to the value of the prefix argument, define your function like this: 例如,如果您希望将第三个参数绑定到prefix参数的值,请按以下方式定义您的函数:

(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...))

Mx describe-function interactive gives you further information. Mx describe-function interactive为您提供更多信息。

A function can be called in two ways: 可以通过两种方式调用函数:

  • Interactively: This is what happens when a user calls the command, eg when it has been bound to a key. 交互式:这是当用户调用命令时发生的事情,例如当它被绑定到一个键时。
  • From lisp: When the function is called from another lisp function. 来自lisp:从另一个lisp函数调用该函数时。 eg (r 100 200 t) . 例如(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 ). 交互式调用时不会使用第三个(因此它将获得值nil )。

NO I/O means that it will not prompt the user for input (like it does when it asks for a file name). NO I / O意味着它不会提示用户输入(就像它要求输入文件名时那样)。

If you want your function to act differently depending in when the region is active, you could ask the function (use-region-p) . 如果您希望函数在区域处于活动状态时采取不同的行为,则可以询问函数(use-region-p)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM