是否有一种简单的方法来定义一个在传递参数时重复的函数?
例如,我已经定义了以下函数
(defun swap-sign ()
(interactive)
(search-forward-regexp "[+-]")
(if (equal (match-string 0) "-")
(replace-match "+")
(replace-match "-"))
)
Run Code Online (Sandbox Code Playgroud)
我想C-u swap-sign打swap-sign四次电话.
我试过了
(defun swap-sign (&optional num)
(interactive)
(let ((counter 0)
(num (if num (string-to-number num) 0)))
(while (<= counter num)
(search-forward-regexp "[+-]")
(if (equal (match-string 0) "-")
(replace-match "+")
(replace-match "-"))
(setq counter (1+ counter)))))
Run Code Online (Sandbox Code Playgroud)
但C-u swap-sign仍然只运行交换符号(或者更准确地说,是while循环的主体)一次.我猜这是因为if num不是测试if num是否为空字符串的正确方法.
我是在正确的轨道上,还是有更好/更容易的扩展方式swap-sign?
(defun swap-sign (arg)
(interactive "p")
(dotimes (i arg)
(search-forward-regexp "[+-]")
(if (equal (match-string 0) "-")
(replace-match "+")
(replace-match "-"))))
Run Code Online (Sandbox Code Playgroud)
有关interactive详细信息,请参阅特殊表单的文档:
C-h finteractiveRET.
| 归档时间: |
|
| 查看次数: |
213 次 |
| 最近记录: |