1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-11-23 07:19:15 +00:00

Implement a way to customize "default" values

* doc/lispref/minibuf.texi (Text from Minibuffer): Document them.

* lisp/minibuffer.el (format-prompt): New function (bug#12443).
(minibuffer-default-prompt-format): New variable.
This commit is contained in:
Lars Ingebrigtsen 2020-08-26 14:07:25 +02:00
parent a4d57add69
commit 0985c0e6c6
3 changed files with 64 additions and 0 deletions

View File

@ -411,6 +411,34 @@ following bindings, in addition to those of @code{minibuffer-local-map}:
@end table
@end defvar
@vindex minibuffer-default-prompt-format
@defun format-prompt prompt default &rest format-args
Format @var{prompt} with default value @var{default} according to the
@code{minibuffer-default-prompt-format} variable.
@code{minibuffer-default-prompt-format} is a format string (defaulting
to @samp{" (default %s)"} that says how the ``default'' bit in prompts
like @samp{"Local filename (default somefile): "} are to be formatted.
To allow the users to customize how this is displayed, code that
prompts the user for a value (and has a default) should look something
along the lines of this code snippet:
@lisp
(read-file-name
(format-prompt "Local filename" file)
nil file)
@end lisp
If @var{format-args} is @code{nil}, @var{prompt} is used as a literal
string. If @var{format-args} is non-@code{nil}, @var{prompt} is used
as a format control string, and @var{prompt} and @var{format-args} are
passed to @code{format} (@pxref{Formatting Strings}).
@code{minibuffer-default-prompt-format} can be @samp{""}, in which
case no default values are displayed.
@end defun
@node Object from Minibuffer
@section Reading Lisp Objects with the Minibuffer
@cindex minibuffer input, reading lisp objects

View File

@ -886,6 +886,16 @@ window after starting). This variable defaults to nil.
** Miscellaneous
+++
*** The user can now customize how \"default\" values are prompted for.
The new utility function 'format-prompt' has been added which uses the
new 'minibuffer-default-prompt-format' variable to format \"default\"
prompts. This means that prompts that look like "Enter a number
(default 10)" can be customized to look like, for instance, "Enter a
number [10]", or not have the default displayed at all, like "Enter a
number". (This requires that all callers are altered to user
'format-prompt', though.)
---
*** New 'diff-mode' font locking face 'diff-error'.
This face is used for error messages from diff.

View File

@ -3028,6 +3028,19 @@ the commands start with a \"-\" or a SPC."
:version "24.1"
:type 'boolean)
(defcustom minibuffer-default-prompt-format " (default %s)"
"Format string used to output \"default\" values.
When prompting for input, there will often be a default value,
leading to prompts like \"Number of articles (default 50): \".
The \"default\" part of that prompt is controlled by this
variable, and can be set to, for instance, \" [%s]\" if you want
a shorter displayed prompt, or \"\", if you don't want to display
the default at all.
This variable is used by the `format-prompt' function."
:version "28.1"
:type 'string)
(defun completion-pcm--pattern-trivial-p (pattern)
(and (stringp (car pattern))
;; It can be followed by `point' and "" and still be trivial.
@ -3845,6 +3858,19 @@ the minibuffer was activated, and execute the forms."
(with-minibuffer-selected-window
(scroll-other-window-down arg)))
(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."
(concat
(if (null format-args)
prompt
(apply #'format prompt format-args))
(format minibuffer-default-prompt-format default)
": "))
(provide 'minibuffer)
;;; minibuffer.el ends here