带有可选数字前缀的emacs交互式功能

Any*_*orn 8 lisp emacs prefix optional

如何指定具有可选数字前缀的函数,如果没有,它会提示输入数字?基本上goto-line表现如何?

(defun my-function(&optional  n)
  ; I have tried
  (interactive "N") ; reads string, no prompt
  (interactive "p") ; defaults to one
  (interactive (if (not n) (read-number "N: "))) ; runtime error
Run Code Online (Sandbox Code Playgroud)

那我怎么做工作?谢谢

Tre*_*son 9

看看如何'goto-line定义(M-x find-function goto-line RET).

(defun my-function (n)
  "Example function taking a prefix arg, or reading a number if no prefix arg"
  (interactive
   (if (and current-prefix-arg (not (consp current-prefix-arg)))
       (list (prefix-numeric-value current-prefix-arg))
     (list (read-number "N: ")))))
Run Code Online (Sandbox Code Playgroud)