help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ELisp Interactive Calls


From: Yuri Khan
Subject: Re: ELisp Interactive Calls
Date: Sat, 20 Dec 2014 03:38:21 +0700

On Sat, Dec 20, 2014 at 2:13 AM,  <mflynn@scu.edu> wrote:

> (defun my-test-function (arg1 arg2)
>   "DOC: Multiply two numbers."
>   (interactive "p p")
>   (message "The product of the args is: %d" (* arg1 arg2))
> )
>
> How do I call this?  If I type Ctrl-U 8 8 Esc-x my-test-function
>
> the call fails with a Wrong number of args message.   The second 8 I type 
> just shows up in the scratch buffer.

Have you read the documentation on “interactive”? (C-h f interactive)

“To get several arguments, concatenate the individual strings,
separating them by newline characters.”

(defun my-test-function (arg1 arg2)
  "DOC: Multiply two numbers."
  (interactive "p\np")
  (message "The product of the args is: %d" (* arg1 arg2)))

C-u 8 M-x my-test-function
=> The product of the args is: 64

It kind of works, but is kind of useless, because the universal prefix
gets fed into *both* arguments.

“n -- Number read using minibuffer.
N -- Numeric prefix arg, or if none, do like code `n'.
p -- Prefix arg converted to number.  Does not do I/O.”

Now this looks useful.

(defun my-test-function (arg1 arg2)
  "DOC: Multiply two numbers."
  (interactive "N\nn")
  (message "The product of the args is: %d" (* arg1 arg2)))

C-u 8 M-x my-test-function
7
=> The product of the args is: 56

M-x my-test-function
8
7
=> The product of the args is: 56

“Usually the argument of `interactive' is a string containing a code
letter followed optionally by a prompt.”

Let’s use that to give useful prompts.

(defun my-test-function (arg1 arg2)
  "DOC: Multiply two numbers."
  (interactive "NMultiply what: \nnMultiply by: ")
  (message "The product of the args is: %d" (* arg1 arg2)))

C-u 8 M-x my-test-function
Multiply by: 7
=> The product of the args is: 56

M-x my-test-function
Multiply what: 8
Multiply by: 7
=> The product of the args is: 56



reply via email to

[Prev in Thread] Current Thread [Next in Thread]