你如何编写一个elisp函数,它应该绑定到按键,默认情况下没有提示,但是在Ctrl-u之前提示用户输入参数.类似的东西(这是错误的语法,但我希望你能得到这个想法)?
(defun my-message (&optional (print-message "foo"))
(interactive "P")
(message print-message))
(global-set-key "\C-c\C-m" 'my-message)
Run Code Online (Sandbox Code Playgroud)
Lin*_*cer 21
以下使用的功能interactive允许您使用代码而不是字符串.此代码仅在交互式调用函数时执行(与之前的答案相比,这是一个不同的答案).代码应评估为列表,其中元素映射到参数.
(defun my-test (&optional arg)
(interactive (list (if current-prefix-arg
(read-from-minibuffer "MyPrompt: ")
nil)))
(if arg
(message arg)
(message "NO ARG")))
Run Code Online (Sandbox Code Playgroud)
通过使用此方法,可以从代码中调用此函数(my-test),(my-test "X")无论是否提示用户输入.对于大多数情况,您希望设计函数,以便它们仅在交互式调用时提示输入.
按照与您的示例相同的实现方式,您可以执行以下操作:
(defun my-message (&optional arg)
(interactive "P")
(let ((msg "foo"))
(when arg
(setq msg (read-from-minibuffer "Message: ")))
(message msg)))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3490 次 |
| 最近记录: |