0

Although the use of optional arguments to functions are easy to understand when running functions non-interactively. But things get complicated when declaring the function interactive, where you want to set the argument values during an interactive call.

When using the interactive clause one can use either Code Characters or more generally using a list.

I did not find good information about making interactive functions with optional arguments, with no examples to help users. And even when some examples are provided, they tend to be simplistic ones.

In summary:

  1. Should one use prefix variable in function if one intends to use the prefix argument?

  2. Should the prefix variable be optional?

  3. Should prefix variable always be the first argument?

  4. How would one call the function non-interactively?

3
  • 1
    What is the question? Commented Jul 8, 2022 at 17:21
  • One question per post, please. And the statements introducing the questions here are generally mistaken/incorrect, as well as not really related to the questions, AFAICS.
    – Drew
    Commented Jul 8, 2022 at 19:43
  • I do not like the idea of splitting a difficulty with using prefix argument into 21 separate questions.
    – Dilna
    Commented Jul 8, 2022 at 19:50

1 Answer 1

0

It’s actually not that hard to understand. When you call a lisp function, either interactively or not, the caller gives a list of arguments. Those are assigned, one by one, to the list of names given in the definition of the function to be called. That’s basically it.

But there are two edge cases: if the caller doesn’t specify enough arguments, then it checks to see if the called function has those arguments marked as optional. If they are optional, then they get nil as a value instead. If they are not optional, then it signals an error.

If the caller specifies too many arguments, then it signals a different error.

Making a function interactive doesn’t change any of that. All it does is tell the Emacs UI how to build the argument list before it actually calls the function. That argument list is then used to call the function in exactly the normal way.

Should one use prefix variable in function if one intends to use the prefix argument?

I think you are asking about the name of the argument? The name doesn’t matter. Assume we have this function:

(defun foo (bar)
  (interactive "P")
  (message "prefix=%s" bar))

This function foo takes one argument. The caller can pass anything that they want, but when called interactively Emacs will ensure that the first argument is the prefix argument, if any. Notice that we didn’t have to call this argument prefix, but we did have to give it some name. If we hadn’t specified a name, the argument list would have been empty and the user would have gotten an error when they called it.

Should the prefix variable be optional?

It won’t matter. If you ask for the prefix to be passed in, then Emacs will always do so. If the user has not entered a prefix, then Emacs will pass in nil.

Should prefix variable always be the first argument?

It doesn’t matter at all. The order of arguments is entirely up to you. Just make sure that arguments you specify in your interactive form and the names you specify in your argument list match up otherwise it will be confusing.

How would one call the function non-interactively?

The same way you would call any other type of function. Taking the example I gave above, you could call it like this: (foo 42). Just remember that the interactive form will not be examined and so you will have to specify all of the arguments yourself. The values you specify need not come from the same source, though.

Since all of this information is available in the Emacs manuals, I hope that you will do us the favor of reporting which parts of those manuals you read and how they failed to answer your questions. With this information we may be able to improve them. Without it, there can be no improvement.

Chapter 10 Evaluation of the Emacs Lisp manual in particular should cover most of what you need to know.

5
  • About "Should one use prefix variable in function if one intends to use the prefix argument?" I meant to say whether to introduce a function argument (as you did with bar) or avoid the argument and simply check with current-prefix-arg.
    – Dilna
    Commented Jul 8, 2022 at 21:27
  • Mentioning (foo 42), you then mean that I will be using the function as though the user did supply a prefix argument as would happen when using the function interactively. You are then basically using things designed primarily for interactivity by making the function believe that you are using the prefix argument.
    – Dilna
    Commented Jul 8, 2022 at 21:32
  • If either the reference or the tutorial could be improved, many people would value that, I would think.
    – Dilna
    Commented Jul 8, 2022 at 21:34
  • Suppose you write a command A that takes some action, and repeats it n times when given n as a prefix argument. Now I write a command B that also wants to do something with the prefix argument, and needs to call A as part of its work. If A takes the prefix arg as a real argument, then it is easy for B to call it with nil or whatever value is appropriate. If A examines current-prefix-arg instead, then B may not actually work correctly. The user might call B with a prefix of 5, only for 25 things happen. B would repeat 5 times, and each call to A it would also repeat.
    – db48x
    Commented Jul 9, 2022 at 5:03
  • There is a trick that I can use when writing B to get around that, but mentioning it would only add to your confusion, I think. Just do things the simple way by taking things as actual arguments whenever possible. That means staying away from the current-prefix-arg variable, for one thing.
    – db48x
    Commented Jul 9, 2022 at 5:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.