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?

George
  • 879
  • 5
  • 17

1 Answers1

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

where "toto" is the initial-input.

TerryTsao
  • 1,176
  • 4
  • 18
gigiair
  • 2,124
  • 1
  • 8
  • 14
  • 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. – Phil Hudson Mar 16 '21 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 Mar 16 '21 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. – Phil Hudson Mar 18 '21 at 11:45