unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* ELisp Interactive Calls
@ 2014-12-19 20:13 mflynn
  2014-12-19 20:38 ` Yuri Khan
  2014-12-19 21:22 ` mflynn
  0 siblings, 2 replies; 3+ messages in thread
From: mflynn @ 2014-12-19 20:13 UTC (permalink / raw)
  To: help-gnu-emacs

After all these years I'm trying to teach myself how to write Emacs Lisp programs.

To call this function:

(defun my-test-function (arg1)
  "DOC: Multiply two numbers."
  (interactive "p")
  (message "The square of the arg is: %d" (* arg1 arg1))
)


I type:
Ctrl-U 8 Esc-x my-test-function

and 64 is printed, as I would expect.

But I'm not sure how to supply two args interactively.

(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.

Thanks




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: ELisp Interactive Calls
  2014-12-19 20:13 ELisp Interactive Calls mflynn
@ 2014-12-19 20:38 ` Yuri Khan
  2014-12-19 21:22 ` mflynn
  1 sibling, 0 replies; 3+ messages in thread
From: Yuri Khan @ 2014-12-19 20:38 UTC (permalink / raw)
  To: mflynn; +Cc: help-gnu-emacs@gnu.org

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



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: ELisp Interactive Calls
  2014-12-19 20:13 ELisp Interactive Calls mflynn
  2014-12-19 20:38 ` Yuri Khan
@ 2014-12-19 21:22 ` mflynn
  1 sibling, 0 replies; 3+ messages in thread
From: mflynn @ 2014-12-19 21:22 UTC (permalink / raw)
  To: help-gnu-emacs

On Friday, December 19, 2014 12:13:36 PM UTC-8, mfl...@scu.edu wrote:
> After all these years I'm trying to teach myself how to write Emacs Lisp programs.
> 
> To call this function:
> 
> (defun my-test-function (arg1)
>   "DOC: Multiply two numbers."
>   (interactive "p")
>   (message "The square of the arg is: %d" (* arg1 arg1))
> )
> 
> 
> I type:
> Ctrl-U 8 Esc-x my-test-function
> 
> and 64 is printed, as I would expect.
> 
> But I'm not sure how to supply two args interactively.
> 
> (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.
> 
> Thanks

This is great.   Thank you.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-12-19 21:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-12-19 20:13 ELisp Interactive Calls mflynn
2014-12-19 20:38 ` Yuri Khan
2014-12-19 21:22 ` mflynn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).