2

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
Drew
  • 77,472
  • 10
  • 114
  • 243
a_subscriber
  • 4,062
  • 1
  • 18
  • 56
  • 1
    (to) tries to invoke to 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
  • And = is the wrong predicate - its args need to be numbers or markers. – Drew Aug 26 '19 at 16:02
  • Instead of testing whether is nil, just test , 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 '21 at 21:18

1 Answers1

4

In Lisp, the term/word that follows an open parenthesis (if the open parenthesis is not quoted) is interpreted to be a function, not a variable. In the context of this question, to is a variable. Because the word to follows an open parenthesis that is not quoted, Emacs thinks it was meant to be interpreted as a function instead of a variable. Try instead using (if to (insert .... -- which tests whether the variable to has been assigned a value.

When using the function =, Emacs expects that two integers will be compared. Inasmuch as nil is not a number, Emacs will throw an error: (wrong-type-argument number-or-marker-p nil). However, using = here appears to not be needed as explained in the preceding paragraph.

Another way to test whether a variable has been assigned a value is to use the function null:

null is a built-in function in `C source code'.

(null OBJECT)

Return t if OBJECT is nil, and return nil otherwise.

So we could use (if (not (null to)) ...

lawlist
  • 19,106
  • 5
  • 38
  • 120
  • (if to (insert (number-to-string (random 1000))) -> it's always ask me about number. But I want , if not input number, then use default value. – a_subscriber Aug 26 '19 at 15:54
  • How about using (if (not to) ... instead? – lawlist Aug 26 '19 at 15:55
  • I't always ask me about input number. When I pres RET it show message "Please, enter a number". But I want when press RET to use default value – a_subscriber Aug 26 '19 at 16:14
  • 1
    According to the documentation, the interactive code n: "A number, read with the minibuffer. If the input is not a number, the user has to try again. ‘n’ never uses the prefix argument. Prompt." https://www.gnu.org/software/emacs/manual/html_node/elisp/Interactive-Codes.html Perhaps you want to use read-number in the body instead of as an interactive code such as n and set the default value to something like -1 or zero and test for that default value ... The function read-number has an optional argument for a default value. See ... https://emacs.stackexchange.com/a/52323/2287 – lawlist Aug 26 '19 at 16:17