8

Using

(interactive "sPROMPT: ")

one can set a prompt for, e.g., a string function.

Question: Is it possible to also supplement a default argument to the function? Say

(interactive "sPROMPT: default")

so that the users can supply "default" as the default argument to the function, unless they change it manually?

1 Answer 1

8
(defun hello (st)
  (interactive (list (read-string "Your name: " "toto")))
  (message "Hello Mr %s" st))

where "toto" is the initial-input.

3
  • 1
    Read the documentation of read-string, and note that the initial input ("toto" in the example above) is not necessarily the same thing as the default value (the optional fourth argument), meaning the value that is returned if the user deletes the initial input and returns the empty string. Commented Mar 16, 2021 at 21:00
  • 1
    @ Phil Hudson: I know this, but I imagine that the careful user of read-string will have the curiosity to read the documentation, which is neither long nor very complex to have a code as close to his desire.
    – gigiair
    Commented Mar 16, 2021 at 21:24
  • 2
    Fair enough, and on reflection I think yours is the better (because simpler) take. Your answer is exactly what was wanted, my comment is (probably) surplus and perhaps a bit pedantic. Still, it might come in useful to someone searching someday. Commented Mar 18, 2021 at 11:45

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.