Blame SOURCES/bash-4.4-bracketed-paste.patch

a9435e
diff --git a/lib/readline/bind.c b/lib/readline/bind.c
a9435e
--- a/lib/readline/bind.c
a9435e
+++ b/lib/readline/bind.c
a9435e
@@ -1428,6 +1428,7 @@ static const struct {
a9435e
   { "convert-meta",		&_rl_convert_meta_chars_to_ascii, 0 },
a9435e
   { "disable-completion",	&rl_inhibit_completion,		0 },
a9435e
   { "echo-control-characters",	&_rl_echo_control_chars,	0 },
a9435e
+  { "enable-bracketed-paste",  &_rl_enable_bracketed_paste,    0 },
a9435e
   { "enable-keypad",		&_rl_enable_keypad,		0 },
a9435e
   { "enable-meta-key",		&_rl_enable_meta,		0 },
a9435e
   { "expand-tilde",		&rl_complete_with_tilde_expansion, 0 },
a9435e
diff --git a/lib/readline/funmap.c b/lib/readline/funmap.c
a9435e
--- a/lib/readline/funmap.c
a9435e
+++ b/lib/readline/funmap.c
a9435e
@@ -68,6 +68,7 @@ static const FUNMAP default_funmap[] = {
a9435e
   { "backward-word", rl_backward_word },
a9435e
   { "beginning-of-history", rl_beginning_of_history },
a9435e
   { "beginning-of-line", rl_beg_of_line },
a9435e
+  { "bracketed-paste-begin", rl_bracketed_paste_begin },
a9435e
   { "call-last-kbd-macro", rl_call_last_kbd_macro },
a9435e
   { "capitalize-word", rl_capitalize_word },
a9435e
   { "character-search", rl_char_search },
a9435e
diff --git a/lib/readline/kill.c b/lib/readline/kill.c
a9435e
--- a/lib/readline/kill.c
a9435e
+++ b/lib/readline/kill.c
a9435e
@@ -656,6 +656,55 @@ rl_yank_last_arg (count, key)
a9435e
   return retval;
a9435e
 }
a9435e
a9435e
+/* Having read the special escape sequence denoting the beginning of a
a9435e
+   `bracketed paste' sequence, read the rest of the pasted input until the
a9435e
+   closing sequence and insert the pasted text as a single unit without
a9435e
+   interpretation. */
a9435e
+int
a9435e
+rl_bracketed_paste_begin (int count, int key)
a9435e
+{
a9435e
+  int retval, c;
a9435e
+  size_t len, cap;
a9435e
+  char *buf;
a9435e
+
a9435e
+  retval = 1;
a9435e
+  len = 0;
a9435e
+  buf = xmalloc (cap = 64);
a9435e
+
a9435e
+  RL_SETSTATE (RL_STATE_MOREINPUT);
a9435e
+  while ((c = rl_read_key ()) >= 0)
a9435e
+    {
a9435e
+      if (RL_ISSTATE (RL_STATE_MACRODEF))
a9435e
+	_rl_add_macro_char (c);
a9435e
+
a9435e
+      if (c == '\r')		/* XXX */
a9435e
+	c = '\n';
a9435e
+
a9435e
+      if (len == cap)
a9435e
+	buf = xrealloc (buf, cap *= 2);
a9435e
+
a9435e
+      buf[len++] = c;
a9435e
+      if (len >= BRACK_PASTE_SLEN && c == BRACK_PASTE_LAST &&
a9435e
+	  STREQN (buf + len - BRACK_PASTE_SLEN, BRACK_PASTE_SUFF, BRACK_PASTE_SLEN))
a9435e
+	{
a9435e
+	  len -= BRACK_PASTE_SLEN;
a9435e
+	  break;
a9435e
+	}
a9435e
+    }
a9435e
+  RL_UNSETSTATE (RL_STATE_MOREINPUT);
a9435e
+
a9435e
+  if (c >= 0)
a9435e
+    {
a9435e
+      if (len == cap)
a9435e
+	buf = xrealloc (buf, cap + 1);
a9435e
+      buf[len] = '\0';
a9435e
+      retval = rl_insert_text (buf);
a9435e
+    }
a9435e
+
a9435e
+  xfree (buf);
a9435e
+  return (retval);
a9435e
+}
a9435e
+
a9435e
 /* A special paste command for users of Cygnus's cygwin32. */
a9435e
 #if defined (__CYGWIN__)
a9435e
 #include <windows.h>
a9435e
diff --git a/lib/readline/readline.c b/lib/readline/readline.c
a9435e
--- a/lib/readline/readline.c
a9435e
+++ b/lib/readline/readline.c
a9435e
@@ -95,6 +95,8 @@ static void readline_initialize_everything PARAMS((void));
a9435e
 static void bind_arrow_keys_internal PARAMS((Keymap));
a9435e
 static void bind_arrow_keys PARAMS((void));
a9435e
a9435e
+static void bind_bracketed_paste_prefix PARAMS((void));
a9435e
+
a9435e
 static void readline_default_bindings PARAMS((void));
a9435e
 static void reset_default_bindings PARAMS((void));
a9435e
a9435e
@@ -285,6 +287,11 @@ int _rl_revert_all_at_newline = 0;
a9435e
    characters corresponding to keyboard-generated signals. */
a9435e
 int _rl_echo_control_chars = 1;
a9435e
a9435e
+/* Non-zero means to attempt to put the terminal in `bracketed paste mode',
a9435e
+   where it will prefix pasted text with an escape sequence and send
a9435e
+   another to mark the end of the paste. */
a9435e
+int _rl_enable_bracketed_paste = 0;
a9435e
+
a9435e
 /* **************************************************************** */
a9435e
 /*								    */
a9435e
 /*			Top Level Functions			    */
a9435e
@@ -1143,6 +1150,10 @@ readline_initialize_everything ()
a9435e
   /* Try to bind a common arrow key prefix, if not already bound. */
a9435e
   bind_arrow_keys ();
a9435e
a9435e
+  /* Bind the bracketed paste prefix assuming that the user will enable
a9435e
+     it on terminals that support it. */
a9435e
+  bind_bracketed_paste_prefix ();
a9435e
+
a9435e
   /* Enable the meta key, if this terminal has one. */
a9435e
   if (_rl_enable_meta)
a9435e
     _rl_enable_meta_key ();
a9435e
@@ -1234,6 +1245,22 @@ bind_arrow_keys ()
a9435e
 #endif
a9435e
 }
a9435e
a9435e
+static void
a9435e
+bind_bracketed_paste_prefix (void)
a9435e
+{
a9435e
+  Keymap xkeymap;
a9435e
+
a9435e
+  xkeymap = _rl_keymap;
a9435e
+
a9435e
+  _rl_keymap = emacs_standard_keymap;
a9435e
+  rl_bind_keyseq_if_unbound (BRACK_PASTE_PREF, rl_bracketed_paste_begin);
a9435e
+
a9435e
+  _rl_keymap = vi_insertion_keymap;
a9435e
+  rl_bind_keyseq_if_unbound (BRACK_PASTE_PREF, rl_bracketed_paste_begin);
a9435e
+
a9435e
+  _rl_keymap = xkeymap;
a9435e
+}
a9435e
+
a9435e
 /* **************************************************************** */
a9435e
 /*								    */
a9435e
 /*		Saving and Restoring Readline's state		    */
a9435e
diff --git a/lib/readline/readline.h b/lib/readline/readline.h
a9435e
--- a/lib/readline/readline.h
a9435e
+++ b/lib/readline/readline.h
a9435e
@@ -172,6 +172,7 @@ extern int rl_yank PARAMS((int, int));
a9435e
 extern int rl_yank_pop PARAMS((int, int));
a9435e
 extern int rl_yank_nth_arg PARAMS((int, int));
a9435e
 extern int rl_yank_last_arg PARAMS((int, int));
a9435e
+extern int rl_bracketed_paste_begin PARAMS((int, int));
a9435e
 /* Not available unless __CYGWIN__ is defined. */
a9435e
 #ifdef __CYGWIN__
a9435e
 extern int rl_paste_from_clipboard PARAMS((int, int));
a9435e
diff --git a/lib/readline/rlprivate.h b/lib/readline/rlprivate.h
a9435e
--- a/lib/readline/rlprivate.h
a9435e
+++ b/lib/readline/rlprivate.h
a9435e
@@ -195,6 +195,14 @@ extern int rl_blink_matching_paren;
a9435e
a9435e
 /* kill.c */
a9435e
 extern int rl_set_retained_kills PARAMS((int));
a9435e
+#define BRACK_PASTE_PREF	"\033[200~"
a9435e
+#define BRACK_PASTE_SUFF	"\033[201~"
a9435e
+
a9435e
+#define BRACK_PASTE_LAST	'~'
a9435e
+#define BRACK_PASTE_SLEN	6
a9435e
+
a9435e
+#define BRACK_PASTE_INIT	"\033[?2004h"
a9435e
+#define BRACK_PASTE_FINI	"\033[?2004l\r"
a9435e
a9435e
 /* terminal.c */
a9435e
 extern void _rl_set_screen_size PARAMS((int, int));
a9435e
@@ -452,6 +460,7 @@ extern int _rl_output_meta_chars;
a9435e
 extern int _rl_bind_stty_chars;
a9435e
 extern int _rl_revert_all_at_newline;
a9435e
 extern int _rl_echo_control_chars;
a9435e
+extern int _rl_enable_bracketed_paste;
a9435e
 extern char *_rl_comment_begin;
a9435e
 extern unsigned char _rl_parsing_conditionalized_out;
a9435e
 extern Keymap _rl_keymap;
a9435e
diff --git a/lib/readline/rltty.c b/lib/readline/rltty.c
a9435e
--- a/lib/readline/rltty.c
a9435e
+++ b/lib/readline/rltty.c
a9435e
@@ -60,6 +60,12 @@ static void set_winsize PARAMS((int));
a9435e
 /*								    */
a9435e
 /* **************************************************************** */
a9435e
a9435e
+/* Non-zero means that the terminal is in a prepped state.  There are several
a9435e
+   flags that are OR'd in to denote whether or not we have sent various
a9435e
+   init strings to the terminal. */
a9435e
+#define TPX_PREPPED	0x01
a9435e
+#define TPX_BRACKPASTE	0x02
a9435e
+
a9435e
 /* Non-zero means that the terminal is in a prepped state. */
a9435e
 static int terminal_prepped;
a9435e
a9435e
@@ -595,7 +601,7 @@ void
a9435e
 rl_prep_terminal (meta_flag)
a9435e
      int meta_flag;
a9435e
 {
a9435e
-  int tty;
a9435e
+  int tty, nprep;
a9435e
   TIOTYPE tio;
a9435e
a9435e
   if (terminal_prepped)
a9435e
@@ -659,8 +665,16 @@ rl_prep_terminal (meta_flag)
a9435e
   if (_rl_enable_keypad)
a9435e
     _rl_control_keypad (1);
a9435e
a9435e
+  nprep = TPX_PREPPED;
a9435e
+
a9435e
+  if (_rl_enable_bracketed_paste)
a9435e
+  {
a9435e
+        fprintf (rl_outstream, BRACK_PASTE_INIT);
a9435e
+        nprep |= TPX_BRACKPASTE;
a9435e
+  }
a9435e
+
a9435e
   fflush (rl_outstream);
a9435e
-  terminal_prepped = 1;
a9435e
+  terminal_prepped = nprep;
a9435e
   RL_SETSTATE(RL_STATE_TERMPREPPED);
a9435e
a9435e
   _rl_release_sigint ();
a9435e
@@ -680,6 +694,9 @@ rl_deprep_terminal ()
a9435e
a9435e
   tty = rl_instream ? fileno (rl_instream) : fileno (stdout);
a9435e
a9435e
+  if (terminal_prepped & TPX_BRACKPASTE)
a9435e
+    fprintf (rl_outstream, BRACK_PASTE_FINI);
a9435e
+
a9435e
   if (_rl_enable_keypad)
a9435e
     _rl_control_keypad (0);