如何定义一个在传递参数时重复自身的函数

unu*_*tbu 1 emacs elisp

是否有一种简单的方法来定义一个在传递参数时重复的函数?

例如,我已经定义了以下函数

(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-signswap-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

hua*_*uan 5

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