From e108d2ab1b56de8a7e7cef9db91885d47768be09 Mon Sep 17 00:00:00 2001
From: mondaugen <nicholas.esterer@gmail.com>
Date: Sat, 7 Oct 2017 14:22:34 -0400
Subject: [PATCH] numbered-completion-display

Display numbers next to possible completions when printing.
This is so the user can select a completion using arguments to the
menu-complete function.
---
 lib/readline/bind.c          |  1 +
 lib/readline/complete.c      | 26 ++++++++++++++++++++++++--
 lib/readline/doc/rluser.texi |  6 ++++++
 lib/readline/rlprivate.h     |  1 +
 4 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/lib/readline/bind.c b/lib/readline/bind.c
index fef538b1..f7baa7f3 100644
--- a/lib/readline/bind.c
+++ b/lib/readline/bind.c
@@ -1516,6 +1516,7 @@ static const struct {
   { "match-hidden-files",	&_rl_match_hidden_files,	0 },
   { "menu-complete-display-prefix", &_rl_menu_complete_prefix_first, 0 },
   { "meta-flag",		&_rl_meta_flag,			0 },
+  { "numbered-completion-display", &_rl_numbered_completion_display, 0 },
   { "output-meta",		&_rl_output_meta_chars,		0 },
   { "page-completions",		&_rl_page_completions,		0 },
   { "prefer-visible-bell",	&_rl_prefer_visible_bell,	V_SPECIAL },
diff --git a/lib/readline/complete.c b/lib/readline/complete.c
index adce0d69..aad125dc 100644
--- a/lib/readline/complete.c
+++ b/lib/readline/complete.c
@@ -173,6 +173,10 @@ int _rl_complete_mark_symlink_dirs = 0;
    like `ls -x'. */
 int _rl_print_completions_horizontally;
 
+/* If non-zero, completions are preceded with a number which can be used in
+ * conjunction with (arg)menu-complete to choose the (arg)th completion. */
+int _rl_numbered_completion_display;
+
 /* Non-zero means that case is not significant in filename completion. */
 #if (defined (__MSDOS__) && !defined (__DJGPP__)) || (defined (_WIN32) && !defined (__CYGWIN__))
 int _rl_completion_case_fold = 1;
@@ -1569,6 +1573,14 @@ rl_display_match_list (char **matches, int len, int max)
   /* How many items of MAX length can we fit in the screen window? */
   cols = complete_get_screenwidth ();
   max += 2;
+  int lend = 1;
+  int compi = 1;
+  if (_rl_numbered_completion_display)
+  {
+      int lent = len;
+      while ( lent /= 10 ) { lend++; }
+      max += lend + 1;
+  }
   limit = cols / max;
   if (limit != 1 && (limit * max == cols))
     limit--;
@@ -1608,7 +1620,12 @@ rl_display_match_list (char **matches, int len, int max)
 	      else
 		{
 		  temp = printable_part (matches[l]);
-		  printed_len = print_filename (temp, matches[l], sind);
+          if (_rl_numbered_completion_display) {
+              printed_len = fprintf(rl_outstream,"%*d ",lend,l);
+              printed_len += print_filename (temp, matches[l], sind);
+          } else {
+              printed_len = print_filename (temp, matches[l], sind);
+          }
 
 		  if (j + 1 < limit)
 		    {
@@ -1643,7 +1660,12 @@ rl_display_match_list (char **matches, int len, int max)
       for (i = 1; matches[i]; i++)
 	{
 	  temp = printable_part (matches[i]);
-	  printed_len = print_filename (temp, matches[i], sind);
+      if (_rl_numbered_completion_display) {
+          printed_len = fprintf(rl_outstream,"%*d ",lend,compi++);
+		  printed_len += print_filename (temp, matches[l], sind);
+      } else {
+		  printed_len = print_filename (temp, matches[l], sind);
+      }
 	  /* Have we reached the end of this line? */
 #if defined (SIGWINCH)
 	  if (RL_SIG_RECEIVED () && RL_SIGWINCH_RECEIVED() == 0)
diff --git a/lib/readline/doc/rluser.texi b/lib/readline/doc/rluser.texi
index 95de21b3..61b65fe1 100644
--- a/lib/readline/doc/rluser.texi
+++ b/lib/readline/doc/rluser.texi
@@ -667,6 +667,12 @@ If set to @samp{on}, menu completion displays the common prefix of the
 list of possible completions (which may be empty) before cycling through
 the list.  The default is @samp{off}.
 
+@item numbered-completion-display
+@vindex numbered-completion-display
+If set to @samp{on}, when printing all completions, they are numbered beginning
+at 1. Using one these numbers as a argument to @code{menu-complete} allows the
+user to insert the completion corresponding to the number.
+
 @item output-meta
 @vindex output-meta
 If set to @samp{on}, Readline will display characters with the
diff --git a/lib/readline/rlprivate.h b/lib/readline/rlprivate.h
index b11a6bf8..d0cd78ca 100644
--- a/lib/readline/rlprivate.h
+++ b/lib/readline/rlprivate.h
@@ -459,6 +459,7 @@ extern int _rl_match_hidden_files;
 extern int _rl_page_completions;
 extern int _rl_skip_completed_text;
 extern int _rl_menu_complete_prefix_first;
+extern int _rl_numbered_completion_display;
 
 /* display.c */
 extern int _rl_vis_botlin;
-- 
2.12.2

