0

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 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 at 15:56
  • And = is the wrong predicate - its args need to be numbers or markers. – Drew Aug 26 at 16:02
0

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)) ...

  • (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 at 15:54
  • How about using (if (not to) ... instead? – lawlist Aug 26 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 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." gnu.org/software/emacs/manual/html_node/elisp/… 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 ... emacs.stackexchange.com/a/52323/2287 – lawlist Aug 26 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.