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

## Re: Making a function than can only be used interactively

 From: Yuri Khan Subject: Re: Making a function than can only be used interactively Date: Fri, 8 Jul 2022 13:08:43 +0700

```On Fri, 8 Jul 2022 at 05:14, carlmarcos--- via Users list for the GNU
Emacs text editor <help-gnu-emacs@gnu.org> wrote:

> I want to know a few specific things.  If I want to use the prefix argument,
> I should include
> a variable in the argument list, right?  Let us call the variable "prefix".
> Now, should the prefix
>  be  mandatory or optional?  Should it always be the first argument?
>
> (defun funname (prefix arg-a arg-b)
>   "docstring"
>   (interactive "P\ns Name:\n s City")
>   (message "executed funname"))

It does not matter for interactive use. Your (interactive) spec, if it
were written correctly[see below], would describe three arguments, and
Emacs would therefore pass three arguments to your function.

‘&optional’ comes into play if you use this function
non-interactively, from Lisp:

(defun funname (prefix arg-a arg-b)
nil)
(funname 0 1 2)
⇒ nil
(funname)
⇒ (wrong-number-of-arguments (lambda (prefix arg-a arg-b) nil) 0)

(defun funname (&optional prefix arg-a arg-b)
nil)
(funname 0 1 2)
⇒ nil
(funname)
⇒ nil

Now about (interactive) spec syntax: you have three spaces there. The
one immediately after the ‘\n’ breaks things — the first character of
an argument specification specifies the way it is produced, and space
is not a valid code letter. The other two spaces (after ‘s’) become
part of the prompt so the prompt is displayed not at the window edge
but one character to the right. Your spec should be:

(interactive "P\nsName:\nsCity")

```