Blame SOURCES/readline-7.0-bracketed-paste.patch

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