all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Argument-list in function with variable number of arguments...
@ 2005-05-20 14:17 luca.spinacci
  2005-05-20 15:15 ` Kevin Rodgers
  0 siblings, 1 reply; 3+ messages in thread
From: luca.spinacci @ 2005-05-20 14:17 UTC (permalink / raw)



Is there a way to have a variable number of arguments in
a function argument-list?
I know how to write a function in which a fixed number of arguments
is expected for ex.

( defun my-function ( first second )
   (interactive "sFirst : \nsSecond : ")
   (insert ""first"\n"
                ""second"\n")
)

So I'm asked for "first" and for "second"...and my-function
inserts the two of them in a buffer.
I would like to have more arguments (let's say n) and being
asked for them interactively, for instance:
How many arguments? : 5 <Ret>
First : first_argument <Ret>
Second : second_argument <Ret>
Third : third_argument <Ret>
...and so on without knowing their number in advance.

Is it possible?
Thank you very much,
Luca.

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

* Re: Argument-list in function with variable number of arguments...
       [not found] <mailman.1029.1116599428.25862.help-gnu-emacs@gnu.org>
@ 2005-05-20 15:00 ` Pascal Bourguignon
  0 siblings, 0 replies; 3+ messages in thread
From: Pascal Bourguignon @ 2005-05-20 15:00 UTC (permalink / raw)


luca.spinacci@seleniacomms.com writes:

> Is there a way to have a variable number of arguments in
> a function argument-list?
> I know how to write a function in which a fixed number of arguments
> is expected for ex.
>
> ( defun my-function ( first second )
>    (interactive "sFirst : \nsSecond : ")
>    (insert ""first"\n"
>                 ""second"\n")
> )
>
> So I'm asked for "first" and for "second"...and my-function
> inserts the two of them in a buffer.
> I would like to have more arguments (let's say n) and being
> asked for them interactively, for instance:
> How many arguments? : 5 <Ret>
> First : first_argument <Ret>
> Second : second_argument <Ret>
> Third : third_argument <Ret>
> ...and so on without knowing their number in advance.
>
> Is it possible?
> Thank you very much,
> Luca.

Yes.  C-h f interactive RET

"If the argument is not a string, it is evaluated to get a list of
 arguments to pass to the function."

So:

(require 'cl)
(defun my-command (&rest arguments)
   (interactive (loop while (y-or-n-p "Another argument? ")
                         collect (read-from-minibuffer "Next argument: ")))
   (insert (format "%S" arguments)))


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Kitty like plastic.
Confuses for litter box.
Don't leave tarp around.

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

* Re: Argument-list in function with variable number of arguments...
  2005-05-20 14:17 Argument-list in function with variable number of arguments luca.spinacci
@ 2005-05-20 15:15 ` Kevin Rodgers
  0 siblings, 0 replies; 3+ messages in thread
From: Kevin Rodgers @ 2005-05-20 15:15 UTC (permalink / raw)


luca.spinacci@seleniacomms.com wrote:
 > Is there a way to have a variable number of arguments in
 > a function argument-list?
 > I know how to write a function in which a fixed number of arguments
 > is expected for ex.
 >
 > ( defun my-function ( first second )
 >    (interactive "sFirst : \nsSecond : ")
 >    (insert ""first"\n"
 >                 ""second"\n")
 > )

See the Lambda Expressions node of the Emacs Lisp manual.

 > So I'm asked for "first" and for "second"...and my-function
 > inserts the two of them in a buffer.
 > I would like to have more arguments (let's say n) and being
 > asked for them interactively, for instance:
 > How many arguments? : 5 <Ret>
 > First : first_argument <Ret>
 > Second : second_argument <Ret>
 > Third : third_argument <Ret>
 > ...and so on without knowing their number in advance.

If you don't know how many there are, you can't name them individually.

(defun your-function (&rest argument-list)
   (interactive
    (let* ((n (read (read-from-minibuffer "How many arguments? ")))
           (i 0)
           (args '()))
      (while (< i n)
        (setq args
              (cons (read-from-minibuffer (format "Argument %d: "
                                                  (setq i (1+ i))))
                    args)))
      args))
   ...)

-- 
Kevin Rodgers

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

end of thread, other threads:[~2005-05-20 15:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-05-20 14:17 Argument-list in function with variable number of arguments luca.spinacci
2005-05-20 15:15 ` Kevin Rodgers
     [not found] <mailman.1029.1116599428.25862.help-gnu-emacs@gnu.org>
2005-05-20 15:00 ` Pascal Bourguignon

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.