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