From: Lars Ingebrigtsen Date: Wed, 26 Aug 2020 12:07:25 +0000 (+0200) Subject: Implement a way to customize "default" values X-Git-Tag: emacs-28.0.90~6383 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=0985c0e6c6f1da712d3a48fa88af2861bf218d60;p=emacs.git 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. --- diff --git a/doc/lispref/minibuf.texi b/doc/lispref/minibuf.texi index 2488fb37529..8b4240c5d80 100644 --- a/doc/lispref/minibuf.texi +++ b/doc/lispref/minibuf.texi @@ -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 diff --git a/etc/NEWS b/etc/NEWS index 5cb6fcb9345..7be7aced983 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -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. diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el index 1f2dcc47559..47f28d0010c 100644 --- a/lisp/minibuffer.el +++ b/lisp/minibuffer.el @@ -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