Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

emacs interactive function with optional numeric prefix

How do I specify a function 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

like image 397
Anycorn Avatar asked Feb 07 '10 00:02

Anycorn


1 Answers

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: ")))))
like image 193
Trey Jackson Avatar answered Nov 17 '22 02:11

Trey Jackson