8

如何指定具有可选数字前缀的函数,如果没有,它会提示输入数字?基本上 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

那么我该如何工作呢?谢谢

4

1 回答 1

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: ")))))
于 2010-02-07T00:20:20.777 回答