【问题标题】:Elisp: Asking yes-or-no in interactive commandsElisp:在交互式命令中询问是或否
【发布时间】:2010-12-21 07:27:20
【问题描述】:

我是 Emacs 新手,正在尝试编写一些 Emacs Lisp 函数。

我想写一个函数,它接受两个参数并且可以处理交互。但是,其中一个参数是布尔值——如果我可以使用 (y-or-no-p),那就完美了,但 (interactive) 似乎没有字符代码。

有什么想法吗?

更新:我使用的是 GNU Emacs 23。

另外,这是我的函数到目前为止的样子:

(defun foo (str bool)
  (interactive "sSome text: \nsDo the thing?")
  (some-func str)
  (if bool (some-other-func str)))

【问题讨论】:

  • 当一个函数接受一个布尔参数时,在 Emacs 中通常使用前缀参数((interactive "sSome text:\nP"),没有额外的提示)。这提供了更好的用户体验(在最常见的情况下少了一个提示,更符合基本的 Emacs 命令)。在命令之前按C-uM-1 任何其他前缀参数组合以传递t,不要这样做以传递nil。安排nil 为普通情况。
  • 有趣。谢谢,吉尔斯! (你应该用它和它的代码来回答,这样我就可以接受它作为答案。)

标签: emacs lisp elisp


【解决方案1】:

啊,找到了。

(defun foo (str bool)
  (interactive
    (list (read-string "Some text: ")
          (y-or-n-p "Do the thing? ")))
  (some-func str)
  (if bool (some-other-func str)))

【讨论】:

    【解决方案2】:

    不太确定您在问什么,但我找不到名为 y-or-no-p 的函数。你的意思是yes-or-no-p吗?

    这似乎符合我的预期。

    【讨论】:

    • y-or-no-pyes-or-no-p 的更短版本,至少在 Emacs 23 中是这样。
    • y-or-n-pn,不是no)要求用户输入yn,而yes-or-no-p需要完整的单词(yesno ) 后按 Enter。
    • 显然是一个错字。问的问题是关于y-or-no-p
    猜你喜欢
    • 2014-02-13
    • 2021-12-12
    • 2014-05-08
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 2010-09-27
    相关资源
    最近更新 更多