I am having difficulty seeing how to properly use function arguments with the interactive command.
Suppose I have a function with three arguments myfunc (arg1 &optional arg2 arg3)
, the last two being optional. In what circumstances does one need the interactive part to fill the values for arg1
, arg2
and arg3
?
In the case of the prefix argument, does one need to include the prefix argument as a function argument?
It looks to me that besides make a function into interactive command, the interactive expression provides a mechanism for passing arguments to the function when called interactively.
(defun change-theme (theme)
(interactive "a Theme Name: ")
...)
(defun history (&optional prefix)
(interactive "P"))
But then one can use things like completing-read
and read-from-minibuffer
that can be saved to a variable. This gives the same utility as interactive
does for the user to enter some input.
(defun cursor-setting ()
(interactive)
(let* ( (cseq '("bar" "hbar" "hollow" "box"))
(csel (completing-read "Cursor: " cseq nil t "box")) ))
But then I can also do
(interactive
(let ((string (read-string "Foo: " nil 'my-history)))
(list (region-beginning) (region-end) string)))
where read-string
is called inside the interactive command.
One can also use a list as here
(interactive
(list
(read-string "Remote host: ")
(read-string "Remote filename: ")))
I am finding the interactive command as one of the most complicated to use.
The following is an important example. I would think that although the example works, the implementation is quite bad. Because whilst one can use it interactively, it is not advisable to use it in a non-interactive way because of the call to read-from-minibuffer
, even though the function would still work.
I wonder whether it is a requirement for all interactive functions to always allow a non-interactive call.
(defun workbench (&optional prefix)
"Make new temporary buffer manually using user input."
(interactive "P")
(cond
;; -------------------------------------------------
((equal current-prefix-arg nil) ; no C-u
(message "no C-u")
(setq thote 1))
;; -------------------------------------------------
((equal current-prefix-arg '(4)) ; C-u
(message "no C-u")
(arktika-automated-workbench))
;; -------------------------------------------------
((equal current-prefix-arg 1) ; C-u 1
(message "C-u 1")
(let* ( (name (read-from-minibuffer " Name: ")) )
(arktika-automated-workbench name))))
(interactive "P")
, would I need to provide an function argument for the prefix? What would be the difference whether the argument is optional or not?