I have a command with an optional argument whose value is a number. I need:
If no input argument then use default value (1000) else use input arg.
(random t)
(defun insert-random-number-at-point(&optional to)
"Insert at point a random number (default from 0 to 1000)."
(interactive "nInsert at point a random number (default from 0 to 1000): ")
(if (= (to) nil)
(insert (number-to-string (random 1000))) ;; then use default value
(insert (number-to-string to)))) ;; else
Start :
Insert at point a random number (default from 0 to 1000): 15
Error:
Symbol’s function definition is void: to
(to)
tries to invoketo
as a function. Lose the parentheses, to use its value as a variable.(if (= to nil)...)
is simpler as(if to ...)
. – Drew Aug 26 '19 at 15:56=
is the wrong predicate - its args need to be numbers or markers. – Drew Aug 26 '19 at 16:02nil
, just test <something>, that is, test whether it's non-nil
. And instead of using conditionals, just(setq to (or to (random 1000)))
or(unless to (setq to (random 1000)))
. – Drew Jan 17 at 21:18