unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Stephen Berman <stephen.berman@gmx.net>
To: Eli Zaretskii <eliz@gnu.org>
Cc: Joseph Turner <joseph@breatheoutbreathe.in>,  emacs-devel@gnu.org
Subject: Re: Unexpected behavior of format-number with format-prompt
Date: Thu, 07 Nov 2024 14:50:27 +0100	[thread overview]
Message-ID: <87jzdfp2zg.fsf@gmx.net> (raw)
In-Reply-To: <86h68jqyut.fsf@gnu.org> (Eli Zaretskii's message of "Thu, 07 Nov 2024 09:36:42 +0200")

[-- Attachment #1: Type: text/plain, Size: 1450 bytes --]

On Thu, 07 Nov 2024 09:36:42 +0200 Eli Zaretskii <eliz@gnu.org> wrote:

>> From: Joseph Turner <joseph@breatheoutbreathe.in>
>> 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:
>
> Thanks.
>
> First, this should have been sent to our issue tracker, via
> report-emacs-bug or submit-emacs-patch.
>
> More to the point: we cannot possibly change the behavior of
> read-number in such a backward-incompatible way.  Especially since
> this behavior is old, and explicitly called out in the doc string.  It
> is perhaps unfortunate that read-number behaves differently in this
> manner, but I'm afraid we will have to live with this.

Maybe something like the attached patch is acceptable?  With it,
evaluating each of the following prompts with "Enter (default 42): "

(read-number "Enter: " 42)
(read-number (format-prompt "Enter" 42))
(read-number (format-prompt "Enter" 42) 42)

Steve Berman

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: read-number patch --]
[-- Type: text/x-patch, Size: 1104 bytes --]

diff --git a/lisp/subr.el b/lisp/subr.el
index e630087b68f..8dcaef08b65 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -3432,12 +3432,18 @@ read-number
   (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))))
+      ;; If PROMPT uses `format-prompt', don't duplicate DEFAULT in
+      ;; the prompt string.
+      (if (string-match (regexp-quote
+			 (format minibuffer-default-prompt-format default1))
+			prompt)
+	  (setq default1 nil)
+        (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

  parent reply	other threads:[~2024-11-07 13:50 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-06 20:06 Unexpected behavior of format-number with format-prompt Joseph Turner
2024-11-07  7:36 ` Eli Zaretskii
2024-11-07  7:44   ` Joseph Turner
2024-11-07  7:57     ` Eli Zaretskii
2024-11-09 19:18       ` Joseph Turner
2024-11-14  8:29         ` Eli Zaretskii
2024-11-07 13:50   ` Stephen Berman [this message]
2024-11-07 14:47     ` Eli Zaretskii
2024-11-07 15:57       ` Stephen Berman
2024-11-09 19:02         ` Joseph Turner
2024-11-09 21:51           ` Stephen Berman
2024-11-10  3:55             ` Joseph Turner
2024-11-10 10:37               ` Stephen Berman
2024-11-10  5:46             ` Eli Zaretskii
2024-11-10 10:40               ` Stephen Berman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87jzdfp2zg.fsf@gmx.net \
    --to=stephen.berman@gmx.net \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=joseph@breatheoutbreathe.in \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).