Hi!
This isn't a bug, but I have attached a patch for a new feature that I hope
you'll consider for inclusion in Readline. I just recently switched to
using vi mode in Bash, and have noticed the Readline arguments in the
prompt (eg `(arg: 2)'). This functionality is also in emacs mode, but I
don't think I ever noticed it (probably because I don't know emacs well
enough). When the args are added to the prompt, it shifts the text, which
can be distracting. My patch adds a Readline variable to disable this
functionality. By default, Readline will show the args in the prompt, just
as it does now. But if a user sets the `show-arg-in-prompt' variable to
`off', the args won't show in the prompt.

I've tested with Bash and a bunch of the programs in the Readline examples
directory, and it looks to be working correctly as far as I can tell.
Though I wish I could say I systematically tracked all the memory usage to
prevent segfaults, that wasn't the case. It was more like a game of
whack-a-mole. So it might be a good idea for someone more familiar with the
code to double-check everything.

I updated some of the documentation, but not the `dvi', `pdf', or `ps'
files for `readline', or `rluserman'.

I can see someone else submitted a [similar patch](
https://savannah.gnu.org/support/?111068) that simply prevents printing of
args in the prompt when you are in vi mode. So there are at least 2 of us
that would like to disable this functionality in vi mode ;).

Thanks,
Justin
From 59706ed21f8ec5b4d0fbd523838522314c5d9d3e Mon Sep 17 00:00:00 2001
From: Justin Donnelly <justinrdonne...@gmail.com>
Date: Wed, 5 Feb 2025 19:59:13 -0500
Subject: [PATCH] Include show-arg-in-prompt variable

Some users may prefer not to see the numeric argument in the prompt. In
particular, it can be jarring when it shifts the rest of the text in the
prompt. Setting the `show-arg-in-prompt' variable to `off' will disable
this functionality. The default value is `on', to match the existing
behavior.
---
 bind.c             |  1 +
 doc/readline.html  |  7 +++++++
 doc/readline.info  |  5 +++++
 doc/rluser.texi    |  6 ++++++
 doc/rluserman.html |  6 ++++++
 doc/rluserman.info |  5 +++++
 misc.c             | 20 +++++++++++++-------
 readline.c         |  4 ++++
 rlprivate.h        |  1 +
 9 files changed, 48 insertions(+), 7 deletions(-)

diff --git a/bind.c b/bind.c
index 6aaa637..9cb07a1 100644
--- a/bind.c
+++ b/bind.c
@@ -1906,6 +1906,7 @@ static const struct {
   { "search-ignore-case",	&_rl_search_case_fold,		0 },
   { "show-all-if-ambiguous",	&_rl_complete_show_all,		0 },
   { "show-all-if-unmodified",	&_rl_complete_show_unmodified,	0 },
+  { "show-arg-in-prompt",	&_rl_show_arg_in_prompt,	0 },
   { "show-mode-in-prompt",	&_rl_show_mode_in_prompt,	0 },
   { "skip-completed-text",	&_rl_skip_completed_text,	0 },
 #if defined (VISIBLE_STATS)
diff --git a/doc/readline.html b/doc/readline.html
index 4182c89..819fae9 100644
--- a/doc/readline.html
+++ b/doc/readline.html
@@ -1013,6 +1013,12 @@ of ringing the bell.
 The default value is &lsquo;<samp class="samp">off</samp>&rsquo;.
 </p>
 </dd>
+<dt><a id="index-show_002darg_002din_002dprompt"></a><span><code class="code">show-arg-in-prompt</code><a class="copiable-link" href="#index-show_002darg_002din_002dprompt"> &para;</a></span></dt>
+<dd><p>If set to &lsquo;<samp class="samp">on</samp>&rsquo;, add a string to the beginning of the prompt
+indicating the number of arguments.
+The default value is &lsquo;<samp class="samp">on</samp>&rsquo;.
+</p>
+</dd>
 <dt><a id="index-show_002dmode_002din_002dprompt"></a><span><code class="code">show-mode-in-prompt</code><a class="copiable-link" href="#index-show_002dmode_002din_002dprompt"> &para;</a></span></dt>
 <dd><p>If set to &lsquo;<samp class="samp">on</samp>&rsquo;, add a string to the beginning of the prompt
 indicating the editing mode: emacs, vi command, or vi insertion.
@@ -6613,6 +6619,7 @@ Previous: <a href="#Concept-Index" accesskey="p" rel="prev">Concept Index</a>, U
 <tr><td></td><td class="printindex-index-entry"><a href="#index-set_002dmark-_0028C_002d_0040_0029"><code>set-mark (C-@)</code></a></td><td class="printindex-index-section"><a href="#Miscellaneous-Commands">Miscellaneous Commands</a></td></tr>
 <tr><td></td><td class="printindex-index-entry"><a href="#index-show_002dall_002dif_002dambiguous">show-all-if-ambiguous</a></td><td class="printindex-index-section"><a href="#Readline-Init-File-Syntax">Readline Init File Syntax</a></td></tr>
 <tr><td></td><td class="printindex-index-entry"><a href="#index-show_002dall_002dif_002dunmodified">show-all-if-unmodified</a></td><td class="printindex-index-section"><a href="#Readline-Init-File-Syntax">Readline Init File Syntax</a></td></tr>
+<tr><td></td><td class="printindex-index-entry"><a href="#index-show_002darg_002din_002dprompt">show-arg-in-prompt</a></td><td class="printindex-index-section"><a href="#Readline-Init-File-Syntax">Readline Init File Syntax</a></td></tr>
 <tr><td></td><td class="printindex-index-entry"><a href="#index-show_002dmode_002din_002dprompt">show-mode-in-prompt</a></td><td class="printindex-index-section"><a href="#Readline-Init-File-Syntax">Readline Init File Syntax</a></td></tr>
 <tr><td></td><td class="printindex-index-entry"><a href="#index-skip_002dcompleted_002dtext">skip-completed-text</a></td><td class="printindex-index-section"><a href="#Readline-Init-File-Syntax">Readline Init File Syntax</a></td></tr>
 <tr><td></td><td class="printindex-index-entry"><a href="#index-skip_002dcsi_002dsequence-_0028_0029"><code>skip-csi-sequence ()</code></a></td><td class="printindex-index-section"><a href="#Miscellaneous-Commands">Miscellaneous Commands</a></td></tr>
diff --git a/doc/readline.info b/doc/readline.info
index 3ad1ccb..695e9c0 100644
--- a/doc/readline.info
+++ b/doc/readline.info
@@ -713,6 +713,11 @@ Variable Settings
           be listed immediately instead of ringing the bell.  The
           default value is ‘off’.
 
+     ‘show-arg-in-prompt’
+          If set to ‘on’, add a string to the beginning of the prompt
+          indicating the number of arguments.  The default value is
+          ‘on’.
+
      ‘show-mode-in-prompt’
           If set to ‘on’, add a string to the beginning of the prompt
           indicating the editing mode: emacs, vi command, or vi
diff --git a/doc/rluser.texi b/doc/rluser.texi
index 27078b5..f81fa79 100644
--- a/doc/rluser.texi
+++ b/doc/rluser.texi
@@ -869,6 +869,12 @@ a common prefix) cause the matches to be listed immediately instead
 of ringing the bell.
 The default value is @samp{off}.
 
+@item show-arg-in-prompt
+@vindex show-arg-in-prompt
+If set to @samp{on}, add a string to the beginning of the prompt
+indicating the number of arguments.
+The default value is @samp{on}.
+
 @item show-mode-in-prompt
 @vindex show-mode-in-prompt
 If set to @samp{on}, add a string to the beginning of the prompt
diff --git a/doc/rluserman.html b/doc/rluserman.html
index f238c2b..0c851c9 100644
--- a/doc/rluserman.html
+++ b/doc/rluserman.html
@@ -969,6 +969,12 @@ of ringing the bell.
 The default value is &lsquo;<samp class="samp">off</samp>&rsquo;.
 </p>
 </dd>
+<dt><a id="index-show_002darg_002din_002dprompt"></a><span><code class="code">show-arg-in-prompt</code><a class="copiable-link" href="#index-show_002darg_002din_002dprompt"> &para;</a></span></dt>
+<dd><p>If set to &lsquo;<samp class="samp">on</samp>&rsquo;, add a string to the beginning of the prompt
+indicating the number of arguments.
+The default value is &lsquo;<samp class="samp">on</samp>&rsquo;.
+</p>
+</dd>
 <dt><a id="index-show_002dmode_002din_002dprompt"></a><span><code class="code">show-mode-in-prompt</code><a class="copiable-link" href="#index-show_002dmode_002din_002dprompt"> &para;</a></span></dt>
 <dd><p>If set to &lsquo;<samp class="samp">on</samp>&rsquo;, add a string to the beginning of the prompt
 indicating the editing mode: emacs, vi command, or vi insertion.
diff --git a/doc/rluserman.info b/doc/rluserman.info
index 5eaaeed..dc70814 100644
--- a/doc/rluserman.info
+++ b/doc/rluserman.info
@@ -711,6 +711,11 @@ Variable Settings
           be listed immediately instead of ringing the bell.  The
           default value is ‘off’.
 
+     ‘show-arg-in-prompt’
+          If set to ‘on’, add a string to the beginning of the prompt
+          indicating the number of arguments.  The default value is
+          ‘on’.
+
      ‘show-mode-in-prompt’
           If set to ‘on’, add a string to the beginning of the prompt
           indicating the editing mode: emacs, vi command, or vi
diff --git a/misc.c b/misc.c
index 49ed74b..6ec8d0f 100644
--- a/misc.c
+++ b/misc.c
@@ -92,7 +92,8 @@ _rl_arg_overflow (void)
 void
 _rl_arg_init (void)
 {
-  rl_save_prompt ();
+  if (_rl_show_arg_in_prompt)
+    rl_save_prompt ();
   _rl_argcxt = 0;
   RL_SETSTATE(RL_STATE_NUMERICARG);
 }
@@ -102,7 +103,8 @@ _rl_arg_getchar (void)
 {
   int c;
 
-  rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
+  if (_rl_show_arg_in_prompt)
+    rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
   RL_SETSTATE(RL_STATE_MOREINPUT);
   c = rl_read_key ();
   RL_UNSETSTATE(RL_STATE_MOREINPUT);
@@ -137,7 +139,8 @@ _rl_arg_dispatch (_rl_arg_cxt cxt, int c)
       else
 	{
 	  key = _rl_bracketed_read_key ();
-	  rl_restore_prompt ();
+	  if (_rl_show_arg_in_prompt)
+	    rl_restore_prompt ();
 	  rl_clear_message ();
 	  RL_UNSETSTATE(RL_STATE_NUMERICARG);
 	  if (key < 0)
@@ -168,7 +171,8 @@ _rl_arg_dispatch (_rl_arg_cxt cxt, int c)
       /* Make M-- command equivalent to M--1 command. */
       if ((_rl_argcxt & NUM_SAWMINUS) && rl_numeric_arg == 1 && rl_explicit_arg == 0)
 	rl_explicit_arg = 1;
-      rl_restore_prompt ();
+      if (_rl_show_arg_in_prompt)
+	rl_restore_prompt ();
       rl_clear_message ();
       RL_UNSETSTATE(RL_STATE_NUMERICARG);
 
@@ -231,7 +235,8 @@ rl_digit_argument (int ignore, int key)
   if (RL_ISSTATE (RL_STATE_CALLBACK))
     {
       _rl_arg_dispatch (_rl_argcxt, key);
-      rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
+      if (_rl_show_arg_in_prompt)
+        rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
       return 0;
     }
   else
@@ -266,7 +271,8 @@ _rl_arg_callback (_rl_arg_cxt cxt)
   if (_rl_argcxt & NUM_READONE)
     {
       _rl_argcxt &= ~NUM_READONE;
-      rl_restore_prompt ();
+      if (_rl_show_arg_in_prompt)
+	rl_restore_prompt ();
       rl_clear_message ();
       RL_UNSETSTATE(RL_STATE_NUMERICARG);
       rl_execute_next (c);
@@ -274,7 +280,7 @@ _rl_arg_callback (_rl_arg_cxt cxt)
     }
 
   r = _rl_arg_dispatch (cxt, c);
-  if (r > 0)
+  if (r > 0 && _rl_show_arg_in_prompt)
     rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
   return (r != 1);
 }
diff --git a/readline.c b/readline.c
index 03ab24a..42df059 100644
--- a/readline.c
+++ b/readline.c
@@ -316,6 +316,10 @@ int _rl_revert_all_at_newline = 0;
    characters corresponding to keyboard-generated signals. */
 int _rl_echo_control_chars = 1;
 
+/* Non-zero means that when there are arugments, prefix the displayed prompt
+   with `(arg: n)' where n is the number of arguments. */
+int _rl_show_arg_in_prompt = 1;
+
 /* Non-zero means to prefix the displayed prompt with a character indicating
    the editing mode: @ for emacs, : for vi-command, + for vi-insert. */
 int _rl_show_mode_in_prompt = 0;
diff --git a/rlprivate.h b/rlprivate.h
index ac1de28..ffafca8 100644
--- a/rlprivate.h
+++ b/rlprivate.h
@@ -614,6 +614,7 @@ extern int _rl_output_meta_chars;
 extern int _rl_bind_stty_chars;
 extern int _rl_revert_all_at_newline;
 extern int _rl_echo_control_chars;
+extern int _rl_show_arg_in_prompt;
 extern int _rl_show_mode_in_prompt;
 extern int _rl_enable_bracketed_paste;
 extern int _rl_enable_active_region;
-- 
2.45.2

Reply via email to