Subject: bug#47286: 28.0.50; [PATCH] Replace "(default %s)" with 'format-prompt'

From: Gabriel <[email-masked]>
Date: Sun, 21 Mar 2021 21:03:15 -0300

Dmitry Gutov <[email-masked]> writes:
> Also note that project--completing-read-strict includes as (string-equal default
> "") check added in 2be537e64.
>
> Perhaps format-prompt could use it as well.

Do you mean to add a new check to 'format-prompt' to consider an empty
DEFAULT as nil, e.g., to not show it on minibuffer prompt in case the
value of the DEFAULT argument is an empty string ?

Also, maybe another useful check for 'format-prompt' would remove the
": " suffix from the PROMPT argument, to avoid displaying something like
"prompt: : ", which could avoid regressions when migrating existing
code.

Example of the new 'format-prompt':

(defun format-prompt (prompt default &rest format-args)
  "Format PROMPT with DEFAULT according to `minibuffer-default-prompt-format'.
If FORMAT-ARGS is nil, PROMPT is used as a plain string.  If
FORMAT-ARGS is non-nil, PROMPT is used as a format control
string, and FORMAT-ARGS are the arguments to be substituted into
it.  See `format' for details.

If DEFAULT is a list, the first element is used as the default.
If not, the element is used as is.

If DEFAULT is nil or an empty string, no \"default value\" string
is included in the return value."
  (let* ((suffix ": ")
         (prompt (if (string-suffix-p suffix prompt)
                     (substring prompt 0 (* -1 (length suffix)))
                   prompt))
         (default (if (equal "" default)
                      nil
                    default))
         (formatted-prompt (if (null format-args)
                               prompt
                             (apply #'format prompt format-args)))
         (formatted-default (and default
                                 (format minibuffer-default-prompt-format
                                         (if (consp default)
                                             (car default)
                                           default)))))
    (concat formatted-prompt
            formatted-default
            suffix)))