Function: read-number

read-number is a byte-compiled function defined in subr.el.gz.

Signature

(read-number PROMPT &optional DEFAULT HIST)

Documentation

Read a numeric value in the minibuffer, prompting with PROMPT.

DEFAULT specifies a default value to return if the user just types RET. The value of DEFAULT is inserted into PROMPT. HIST specifies a history list variable. See read-from-minibuffer for details of the HIST argument. This function is used by the interactive code letter n.

Probably introduced at or before Emacs version 28.1.

Source Code

;; Defined in /usr/src/emacs/lisp/subr.el.gz
(defun read-number (prompt &optional default hist)
  "Read a numeric value in the minibuffer, prompting with PROMPT.
DEFAULT specifies a default value to return if the user just types RET.
The value of DEFAULT is inserted into PROMPT.
HIST specifies a history list variable.  See `read-from-minibuffer'
for details of the HIST argument.
This function is used by the `interactive' code letter `n'."
  (let ((n nil)
	(default1 (if (consp default) (car default) default)))
    (when default1
      (setq prompt
	    (if (string-match "\\(\\):[ \t]*\\'" prompt)
		(replace-match (format minibuffer-default-prompt-format default1) t t prompt 1)
	      (replace-regexp-in-string "[ \t]*\\'"
					(format minibuffer-default-prompt-format default1)
					prompt t t))))
    (while
	(progn
	  (let ((str (read-from-minibuffer
		      prompt nil nil nil (or hist 'read-number-history)
		      (when default
			(if (consp default)
			    (mapcar #'number-to-string (delq nil default))
			  (number-to-string default))))))
	    (condition-case nil
		(setq n (cond
			 ((zerop (length str)) default1)
			 ((stringp str) (read str))))
	      (error nil)))
	  (unless (numberp n)
	    (message "Please enter a number.")
	    (sit-for 1)
	    t)))
    n))