emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Unexpected behavior of format-number with format-prompt


From: Joseph Turner
Subject: Unexpected behavior of format-number with format-prompt
Date: Wed, 06 Nov 2024 12:06:02 -0800

When used with `read-string' (and other read-* functions),
`format-prompt' DTRT:

(let ((default "foo"))
  (read-string (format-prompt "Read" default) default))

The prompt looks like: "Read (default foo): "

However, `read-number' adds its own default argument:

(let ((default 1))
  (read-number (format-prompt "Read" default) default))

Prompt looks like: "Read (default 1) (default 1): "

This patch makes the behavior of `read-number' consistent with other
read-* functions:

>From f42643852249a07acf9181aca37af351ff52bb5b Mon Sep 17 00:00:00 2001
From: Joseph Turner <joseph@breatheoutbreathe.in>
Date: Wed, 6 Nov 2024 11:54:31 -0800
Subject: [PATCH] Don't insert DEFAULT value into prompt

* lisp/subr.el (read-number): Don't insert default value into prompt.
---
 lisp/subr.el | 8 --------
 1 file changed, 8 deletions(-)

diff --git a/lisp/subr.el b/lisp/subr.el
index e630087b68f..aa37066609c 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -3424,20 +3424,12 @@ read-number-history
 (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
-- 
2.46.0

If this patch is acceptable, I'll update NEWS and submit patches to make
`read-number' calls in core to use `format-prompt'.

I understand that such a breaking change may not be acceptable since it
affects external packages as well.  In that case, this patch documents
the collision between `read-number' and `format-prompt':

>From 0f3c690650e30201c725186bff3b91ef8fa4ee19 Mon Sep 17 00:00:00 2001
From: Joseph Turner <joseph@breatheoutbreathe.in>
Date: Wed, 6 Nov 2024 11:53:42 -0800
Subject: [PATCH] Document behavior of format-prompt with read-number

* doc/lispref/minibuf.texi (Text from Minibuffer): Update info manual.
---
 doc/lispref/minibuf.texi | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/doc/lispref/minibuf.texi b/doc/lispref/minibuf.texi
index c5b9176d628..8bb53f6d831 100644
--- a/doc/lispref/minibuf.texi
+++ b/doc/lispref/minibuf.texi
@@ -491,6 +491,10 @@ Text from Minibuffer
 
 If @var{default} is @code{nil}, there is no default value, and
 therefore no ``default value'' string is included in the result value.
+For historical reasons, @code{read-number} always adds its
+@code{default} argument to its prompt, so the @var{default} argument of
+@code{format-prompt} should be @code{nil} when its return value will be
+passed to @code{read-number}.
 If @var{default} is a non-@code{nil} list, the first element of the
 list is used in the prompt.
 
-- 
2.46.0

Thanks!

Joseph

reply via email to

[Prev in Thread] Current Thread [Next in Thread]