Hi all,
I have created a simple patch to have history search with up-down arrow, to search anywhere within history string (unlike at begining in history-search-forward/backward). Please see if it is useful.

Bash version : 4.2

Here is my .inputrc

"\e[A": history-search-backward-new
"\e[B": history-search-forward-new


Niraj
diff -ur ../bash_old/bash-4.2//lib/readline/funmap.c ./lib/readline/funmap.c
--- ../bash_old/bash-4.2//lib/readline/funmap.c 2010-05-31 04:02:37.000000000 
+0530
+++ ./lib/readline/funmap.c     2011-05-05 14:17:40.312594508 +0530
@@ -98,6 +98,8 @@
   { "forward-word", rl_forward_word },
   { "history-search-backward", rl_history_search_backward },
   { "history-search-forward", rl_history_search_forward },
+  { "history-search-backward-new", rl_history_search_backward_new },
+  { "history-search-forward-new", rl_history_search_forward_new },
   { "insert-comment", rl_insert_comment },
   { "insert-completions", rl_insert_completions },
   { "kill-whole-line", rl_kill_full_line },
diff -ur ../bash_old/bash-4.2//lib/readline/readline.h ./lib/readline/readline.h
--- ../bash_old/bash-4.2//lib/readline/readline.h       2011-01-17 
02:03:09.000000000 +0530
+++ ./lib/readline/readline.h   2011-05-05 14:19:41.162594470 +0530
@@ -204,6 +204,8 @@
 /* Bindable commands for incremental and non-incremental history searching. */
 extern int rl_history_search_forward PARAMS((int, int));
 extern int rl_history_search_backward PARAMS((int, int));
+extern int rl_history_search_forward_new PARAMS((int, int));
+extern int rl_history_search_backward_new PARAMS((int, int));
 extern int rl_noninc_forward_search PARAMS((int, int));
 extern int rl_noninc_reverse_search PARAMS((int, int));
 extern int rl_noninc_forward_search_again PARAMS((int, int));
diff -ur ../bash_old/bash-4.2//lib/readline/search.c ./lib/readline/search.c
--- ../bash_old/bash-4.2//lib/readline/search.c 2010-07-26 02:59:05.000000000 
+0530
+++ ./lib/readline/search.c     2011-05-13 10:07:52.786726413 +0530
@@ -488,10 +488,11 @@
       /* If you don't want the saved history line (last match) to show up
          in the line buffer after the search fails, change the #if 0 to
          #if 1 */
-#if 0
+#if 1
       if (rl_point > rl_history_search_len)
         {
           rl_point = rl_end = rl_history_search_len;
+          strncpy(rl_line_buffer,history_search_string,rl_end);
           rl_line_buffer[rl_end] = '\0';
           rl_mark = 0;
         }
@@ -504,8 +505,10 @@
 
   /* Copy the line we found into the current line buffer. */
   make_history_line_current (temp);
-
-  rl_point = rl_history_search_len;
+  rl_point=(int)(strstr(rl_line_buffer,history_search_string)+
+                       strlen(history_search_string)-(char *)rl_line_buffer);
+  //rl_point = rl_history_search_len;
+  
   rl_mark = rl_end;
 
   return 0;
@@ -531,6 +534,26 @@
   _rl_free_saved_history_line ();
 }
 
+static void
+rl_history_search_reinit_new ()
+{
+  rl_history_search_pos = where_history ();
+  rl_history_search_len = rl_point;
+  prev_line_found = (char *)NULL;
+  if (rl_point)
+    {
+      if (rl_history_search_len >= history_string_size - 2)
+       {
+         history_string_size = rl_history_search_len + 2;
+         history_search_string = (char *)xrealloc (history_search_string, 
history_string_size);
+       }
+      //history_search_string[0] = '^';
+      strncpy (history_search_string , rl_line_buffer, rl_point);
+      history_search_string[rl_point ] = '\0';
+    }
+  _rl_free_saved_history_line ();
+}
+
 /* Search forward in the history for the string of characters
    from the start of the line to rl_point.  This is a non-incremental
    search. */
@@ -566,5 +589,41 @@
 
   if (rl_history_search_len == 0)
     return (rl_get_previous_history (count, ignore));
+  return (rl_history_search_internal (abs (count), (count > 0) ? -1 : 1));
+}
+
+
+int
+rl_history_search_forward_new (count, ignore)
+     int count, ignore;
+{
+  if (count == 0)
+    return (0);
+
+  if (rl_last_func != rl_history_search_forward_new &&
+      rl_last_func != rl_history_search_backward_new)
+    rl_history_search_reinit_new ();
+
+  if (rl_history_search_len == 0)
+    return (rl_get_next_history (count, ignore));
+  return (rl_history_search_internal (abs (count), (count > 0) ? 1 : -1));
+}
+
+/* Search backward through the history for the string of characters
+   from the start of the line to rl_point.  This is a non-incremental
+   search. */
+int
+rl_history_search_backward_new (count, ignore)
+     int count, ignore;
+{
+  if (count == 0)
+    return (0);
+
+  if (rl_last_func != rl_history_search_forward_new &&
+      rl_last_func != rl_history_search_backward_new)
+    rl_history_search_reinit_new ();
+
+  if (rl_history_search_len == 0)
+    return (rl_get_previous_history (count, ignore));
   return (rl_history_search_internal (abs (count), (count > 0) ? -1 : 1));
 }

Reply via email to