From 66fde3d8bb7703c46f8a93ce105b9d48e2032acc Mon Sep 17 00:00:00 2001
From: Christy O'Reilly <christyor73@gmail.com>
Date: Wed, 27 Jul 2011 21:33:20 +0100
Subject: [PATCH 1/3] Add chaining of !! history expansion

---
 lib/readline/histexpand.c |   13 +++++++++----
 1 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/lib/readline/histexpand.c b/lib/readline/histexpand.c
index bf5ac0e..abdfd7d 100644
--- a/lib/readline/histexpand.c
+++ b/lib/readline/histexpand.c
@@ -141,7 +141,8 @@ get_history_event (string, caller_index, delimiting_quote)
 
   /* The event can be specified in a number of ways.
 
-     !!   the previous command
+     !!   the previous command (may be chained, i.e.
+          !!! is the previous but one command)
      !n   command line N
      !-n  current command-line minus N
      !str the most recent command starting with STR
@@ -164,11 +165,15 @@ get_history_event (string, caller_index, delimiting_quote)
 #define RETURN_ENTRY(e, w) \
 	return ((e = history_get (w)) ? e->line : (char *)NULL)
 
-  /* Handle !! case. */
+  /* Handle !!... case. */
   if (string[i] == history_expansion_char)
     {
-      i++;
-      which = history_base + (history_length - 1);
+      which = history_base + history_length;
+      while(string[i] == history_expansion_char)
+        {
+          i++;
+          which--;
+        }
       *caller_index = i;
       RETURN_ENTRY (entry, which);
     }
-- 
1.7.4.1


From 71438d4b1f6a6d9ef3298394518bfdccaf986e56 Mon Sep 17 00:00:00 2001
From: Christy O'Reilly <christyor73@gmail.com>
Date: Wed, 27 Jul 2011 21:56:39 +0100
Subject: [PATCH 2/3] Add tests for !! history expansion chaining

---
 tests/histexp.right |    7 +++++++
 tests/histexp.tests |    7 +++++++
 2 files changed, 14 insertions(+), 0 deletions(-)

diff --git a/tests/histexp.right b/tests/histexp.right
index f1c9e9d..f441410 100644
--- a/tests/histexp.right
+++ b/tests/histexp.right
@@ -127,3 +127,10 @@ echo '!!' \!\!
 ok 1
 ok 2
 ok 3
+a
+b
+c
+echo a
+a
+echo c
+c
diff --git a/tests/histexp.tests b/tests/histexp.tests
index 721208c..81f6b97 100644
--- a/tests/histexp.tests
+++ b/tests/histexp.tests
@@ -122,3 +122,10 @@ echo ${!var2}
 # Bash-2.01[.1] fails this test -- it attempts history expansion after the
 # history_expansion_char
 echo ok 3 # !1200
+
+# Test chaining of !! history expansions
+echo a
+echo b
+echo c
+!!!!
+!!!
-- 
1.7.4.1


From 572655962d8d9014b4787dfb35ee9923d53bdc98 Mon Sep 17 00:00:00 2001
From: Christy O'Reilly <christyor73@gmail.com>
Date: Wed, 27 Jul 2011 22:08:15 +0100
Subject: [PATCH 3/3] Update documentation

---
 doc/bash.1 |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)

diff --git a/doc/bash.1 b/doc/bash.1
index 4308214..e32b598 100644
--- a/doc/bash.1
+++ b/doc/bash.1
@@ -6055,6 +6055,9 @@ Refer to the current command line minus
 .TP
 .B !!
 Refer to the previous command.  This is a synonym for `!\-1'.
+This may be chained, i.e. 
+.B !!!
+refers to the previous but one command.
 .TP
 .B !\fIstring\fR
 Refer to the most recent command starting with 
-- 
1.7.4.1

