开发者

emacs interactive function with optional numeric prefix

开发者 https://www.devze.com 2022-12-19 22:22 出处:网络
How do I specify a fun开发者_C百科ction which has optional numeric prefix, if not, it prompts for a number?basically how goto-line behaves?

How do I specify a fun开发者_C百科ction which has optional numeric prefix, if not, it prompts for a number? basically how goto-line behaves?

(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

so how do I make work? thanks


Take a look at how 'goto-line is defined (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: ")))))
0

精彩评论

暂无评论...
验证码 换一张
取 消