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

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