0

I have a function argument and want to set an empty string for name and mode. What can I do?

(defun bench (&optional name mode prefix)
  "DOCSTRING"

  (interactive
   (cond

    ((equal current-prefix-arg nil)
     (list

      current-prefix-arg)))))
4
  • 1
    Your question doesn’t make any sense. What are you really trying to accomplish?
    – db48x
    Jul 9, 2022 at 5:30
  • Want to fill name and mode with empty or default strings.
    – Dilna
    Jul 9, 2022 at 5:48
  • cl-defun allows you to set defaults for optional arguments. Do C-h f cl-defun to learn more. Jul 9, 2022 at 7:13
  • Can I do (list "" "" current-perfix-arg)?
    – Dilna
    Jul 9, 2022 at 7:30

1 Answer 1

1

I don’t really know what you want, but based on your comment you want the name and mode variables to be strings if the caller passes in nil or omits them. While cl-defun is an option, I wouldn’t do anything fancy at all.

Instead, when you go to use name or mode in the body of your function, just write (or name "") and (or mode "") instead. The or function looks at each argument in turn and gives you the first one that is non–nil. Like this:

(defun bench (&optional name mode prefix)
  "DOCSTRING"
  (message "name=%s mode=%s" (or name "") (or mode ""))

If you find yourself in a situation where you need to type these expressions out more than once, just use let to rebind them:

(defun bench (&optional name mode prefix)
  "DOCSTRING"
  (let ((name (or name ""))
        (mode (or mode "")))
    …))

I wouldn’t try to do anything tricky with the interactive declaration. Keep it as simple as possible.

5
  • In my function I was only doing (if name and (if mode or (if (string= "" name).
    – Dilna
    Jul 9, 2022 at 13:56
  • But I still have to set three values for the list that fills the three function arguments. Would I use nil or "" when using the function interactively.
    – Dilna
    Jul 9, 2022 at 13:59
  • No, you don’t have to. But if you want to use that type of interactive form, you can use whatever values you want in the list.
    – db48x
    Jul 9, 2022 at 15:19
  • If I want to use that type of interactive form, then I have to make the three valued list, right? If I have optional arguments, can I just fill in just some of them in the interactive list setup?
    – Dilna
    Jul 9, 2022 at 17:19
  • 1
    With this type of interactive form, you must supply a list of values to be used as arguments. They will be applied to the arguments in precisely the same manner as any other list of arguments. This means that the values are applied to the arguments in order, and you do not have to specify a value for the optional arguments. There is really nothing complex here. Did you try something that failed? If so, you really should include it, and the error you got, in the question. Otherwise people will think you haven’t even bothered to try it.
    – db48x
    Jul 9, 2022 at 18:32

Your Answer

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

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