2010-02-07 20 views
8

如何指定一個具有可選數字前綴的函數,如果沒有,它會提示輸入數字?基本上goto-line的行爲如何?帶可選數字前綴的emacs交互功能

(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 

那麼我該如何工作? 謝謝

+1

FWIW,如果您想提示輸入「N」,只需在N之後加上提示文字; '(交互式「NType:」)'。 – jrockway 2010-02-07 01:09:14

回答

9

看看如何定義'goto-lineM-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: "))))) 
+2

+1。使用源碼,盧克! – Bahbar 2010-02-07 09:53:38