From 7a762c65aff175e37bf5b46381b7caabfabfd201 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Tue, 8 Sep 2015 00:40:25 +0200 Subject: [PATCH 1/8] Implement the bracketed paste mode As an alternative method of paste detection, more reliable but might not be supported by all the VTs. --- src/fe-text/gui-readline.c | 42 +++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/fe-text/gui-readline.c b/src/fe-text/gui-readline.c index edc4c55d..c7535a0b 100644 --- a/src/fe-text/gui-readline.c +++ b/src/fe-text/gui-readline.c @@ -37,6 +37,7 @@ #include "gui-windows.h" #include "utf8.h" +#include #include typedef void (*ENTRY_REDIRECT_KEY_FUNC) (int key, void *data, SERVER_REC *server, WI_ITEM_REC *item); @@ -65,6 +66,13 @@ static char *paste_old_prompt; static int paste_prompt, paste_line_count; static int paste_join_multiline; static int paste_timeout_id; +static int paste_bracketed_mode; + +/* Terminal sequences that surround the input when the terminal has the + * bracketed paste mode active. Fror more details see + * https://cirw.in/blog/bracketed-paste */ +static const unichar bp_start[] = { 0x1b, '[', '2', '0', '0', '~' }; +static const unichar bp_end[] = { 0x1b, '[', '2', '0', '1', '~' }; static void sig_input(void); @@ -647,12 +655,43 @@ static void sig_input(void) unichar key; term_gets(buffer, &line_count); key = g_array_index(buffer, unichar, 0); + /* Either Ctrl-k or Ctrl-c is pressed */ if (key == 11 || key == 3) paste_flush(key == 11); g_array_free(buffer, TRUE); } else { term_gets(paste_buffer, &paste_line_count); - if (paste_detect_time > 0 && paste_buffer->len >= 3) { + + /* use the bracketed paste mode to detect when the user has + * pasted some text into the field. */ + if (paste_buffer->len > 12) { + /* try to find the start/end sequence */ + int seq_start = memmem(paste_buffer->data, + paste_buffer->len * g_array_get_element_size(paste_buffer), + bp_start, sizeof(bp_start)) != NULL, + seq_end = memmem(paste_buffer->data, + paste_buffer->len * g_array_get_element_size(paste_buffer), + bp_end, sizeof(bp_end)) != NULL; + + g_warning("found sequences : start %d end %d", seq_start, seq_end); + + if (seq_start) { + paste_bracketed_mode = TRUE; + /* remove the leading sequence chars */ + memmove(paste_buffer->data, paste_buffer->data + sizeof(bp_start), + paste_buffer->len * g_array_get_element_size(paste_buffer) - sizeof(bp_start)); + g_array_set_size(paste_buffer, paste_buffer->len - 6); + } + + if (seq_end) { + paste_bracketed_mode = FALSE; + /* remove the trailing sequence chars */ + g_array_set_size(paste_buffer, paste_buffer->len - 6); + /* decide what to do with the buffer */ + paste_timeout(NULL); + } + } + else if (paste_detect_time > 0 && paste_buffer->len >= 3) { if (paste_timeout_id != -1) g_source_remove(paste_timeout_id); paste_timeout_id = g_timeout_add(paste_detect_time, paste_timeout, NULL); @@ -945,6 +984,7 @@ void gui_readline_init(void) paste_buffer = g_array_new(FALSE, FALSE, sizeof(unichar)); paste_old_prompt = NULL; paste_timeout_id = -1; + paste_bracketed_mode = FALSE; g_get_current_time(&last_keypress); input_listen_init(STDIN_FILENO); From 507af4d337218eac0529656357acc3f650a74aa7 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Tue, 8 Sep 2015 00:48:13 +0200 Subject: [PATCH 2/8] Toggles --- src/fe-text/gui-readline.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/fe-text/gui-readline.c b/src/fe-text/gui-readline.c index c7535a0b..b7e503cc 100644 --- a/src/fe-text/gui-readline.c +++ b/src/fe-text/gui-readline.c @@ -66,6 +66,7 @@ static char *paste_old_prompt; static int paste_prompt, paste_line_count; static int paste_join_multiline; static int paste_timeout_id; +static int paste_use_bracketed_mode; static int paste_bracketed_mode; /* Terminal sequences that surround the input when the terminal has the @@ -662,11 +663,11 @@ static void sig_input(void) } else { term_gets(paste_buffer, &paste_line_count); - /* use the bracketed paste mode to detect when the user has - * pasted some text into the field. */ - if (paste_buffer->len > 12) { + /* use the bracketed paste mode to detect when the user pastes + * some text into the entry */ + if (paste_use_bracketed_mode != FALSE && paste_buffer->len > 12) { /* try to find the start/end sequence */ - int seq_start = memmem(paste_buffer->data, + int seq_start = memmem(paste_buffer->data, paste_buffer->len * g_array_get_element_size(paste_buffer), bp_start, sizeof(bp_start)) != NULL, seq_end = memmem(paste_buffer->data, @@ -678,7 +679,7 @@ static void sig_input(void) if (seq_start) { paste_bracketed_mode = TRUE; /* remove the leading sequence chars */ - memmove(paste_buffer->data, paste_buffer->data + sizeof(bp_start), + memmove(paste_buffer->data, paste_buffer->data + sizeof(bp_start), paste_buffer->len * g_array_get_element_size(paste_buffer) - sizeof(bp_start)); g_array_set_size(paste_buffer, paste_buffer->len - 6); } @@ -969,6 +970,7 @@ static void setup_changed(void) paste_verify_line_count = settings_get_int("paste_verify_line_count"); paste_join_multiline = settings_get_bool("paste_join_multiline"); + paste_use_bracketed_mode = settings_get_bool("paste_use_bracketed_mode"); } void gui_readline_init(void) @@ -990,6 +992,7 @@ void gui_readline_init(void) settings_add_str("history", "scroll_page_count", "/2"); settings_add_time("misc", "paste_detect_time", "5msecs"); + settings_add_bool("misc", "paste_use_bracketed_mode", FALSE); /* NOTE: function keys can generate at least 5 characters long keycodes. this must be larger to allow them to work. */ settings_add_int("misc", "paste_verify_line_count", 5); From d56b8b7c60e6f8c645b17f2a18a9bb215f59b6de Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Tue, 8 Sep 2015 00:55:34 +0200 Subject: [PATCH 3/8] Replace some hairy logic with g_array_remove_range In the hope it'll do the same under the hood. --- src/fe-text/gui-readline.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/fe-text/gui-readline.c b/src/fe-text/gui-readline.c index b7e503cc..03553657 100644 --- a/src/fe-text/gui-readline.c +++ b/src/fe-text/gui-readline.c @@ -679,9 +679,7 @@ static void sig_input(void) if (seq_start) { paste_bracketed_mode = TRUE; /* remove the leading sequence chars */ - memmove(paste_buffer->data, paste_buffer->data + sizeof(bp_start), - paste_buffer->len * g_array_get_element_size(paste_buffer) - sizeof(bp_start)); - g_array_set_size(paste_buffer, paste_buffer->len - 6); + g_array_remove_range(paste_buffer, 0, 6); } if (seq_end) { From 9f87345cc583b955b4745f99dbc2c81be55cbd00 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Wed, 9 Sep 2015 22:35:11 +0200 Subject: [PATCH 4/8] Enable the bracketed paste mode on demand --- src/fe-text/gui-readline.c | 3 +++ src/fe-text/term-terminfo.c | 8 ++++++++ src/fe-text/term.h | 2 ++ 3 files changed, 13 insertions(+) diff --git a/src/fe-text/gui-readline.c b/src/fe-text/gui-readline.c index 03553657..32d07bfa 100644 --- a/src/fe-text/gui-readline.c +++ b/src/fe-text/gui-readline.c @@ -969,6 +969,9 @@ static void setup_changed(void) paste_verify_line_count = settings_get_int("paste_verify_line_count"); paste_join_multiline = settings_get_bool("paste_join_multiline"); paste_use_bracketed_mode = settings_get_bool("paste_use_bracketed_mode"); + + /* Enable the bracketed paste mode on demand */ + term_set_bracketed_paste_mode(paste_use_bracketed_mode); } void gui_readline_init(void) diff --git a/src/fe-text/term-terminfo.c b/src/fe-text/term-terminfo.c index ded79c28..9376bda8 100644 --- a/src/fe-text/term-terminfo.c +++ b/src/fe-text/term-terminfo.c @@ -689,3 +689,11 @@ void term_gets(GArray *buffer, int *line_count) } } } + +void term_set_bracketed_paste_mode(int enable) +{ + if (enable) + tputs("\e[?2004h", 0, term_putchar); + else + tputs("\e[?2004l", 0, term_putchar); +} diff --git a/src/fe-text/term.h b/src/fe-text/term.h index cdcc787a..692ce9c5 100644 --- a/src/fe-text/term.h +++ b/src/fe-text/term.h @@ -94,6 +94,8 @@ void term_refresh(TERM_WINDOW *window); void term_stop(void); +void term_set_bracketed_paste_mode(int enable); + /* keyboard input handling */ void term_set_input_type(int type); void term_gets(GArray *buffer, int *line_count); From ed1650f01343b16fb341a46a72c3f8c08f600b20 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Wed, 9 Sep 2015 22:41:17 +0200 Subject: [PATCH 5/8] Get rid of the non-portable memmem The sequences we're after are found at the beginning or at the end of the buffer, there's no need to scan the whole thing. --- src/fe-text/gui-readline.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/fe-text/gui-readline.c b/src/fe-text/gui-readline.c index 32d07bfa..37487b5c 100644 --- a/src/fe-text/gui-readline.c +++ b/src/fe-text/gui-readline.c @@ -666,15 +666,11 @@ static void sig_input(void) /* use the bracketed paste mode to detect when the user pastes * some text into the entry */ if (paste_use_bracketed_mode != FALSE && paste_buffer->len > 12) { - /* try to find the start/end sequence */ - int seq_start = memmem(paste_buffer->data, - paste_buffer->len * g_array_get_element_size(paste_buffer), - bp_start, sizeof(bp_start)) != NULL, - seq_end = memmem(paste_buffer->data, - paste_buffer->len * g_array_get_element_size(paste_buffer), - bp_end, sizeof(bp_end)) != NULL; - - g_warning("found sequences : start %d end %d", seq_start, seq_end); + /* try to find the start/end sequence, we know that we + * either find those at the start/end of the buffer or + * we don't find those at all. */ + int seq_start = !memcmp(paste_buffer->data, bp_start, sizeof(bp_start)), + seq_end = !memcmp(paste_buffer->data + paste_buffer->len * g_array_get_element_size(paste_buffer) - sizeof(bp_end), bp_end, sizeof(bp_end)); if (seq_start) { paste_bracketed_mode = TRUE; From ce84ed5d9917eff07a3377f2894acbc4ae6f20b5 Mon Sep 17 00:00:00 2001 From: dequis Date: Thu, 17 Sep 2015 00:52:55 -0300 Subject: [PATCH 6/8] Improve bracketed paste start/end detection - Use a keybinding to detect the start of a bracketed paste - Iterate over the paste buffer looking for the end marker --- src/fe-text/gui-readline.c | 50 +++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/src/fe-text/gui-readline.c b/src/fe-text/gui-readline.c index 37487b5c..c2e7ff4e 100644 --- a/src/fe-text/gui-readline.c +++ b/src/fe-text/gui-readline.c @@ -665,37 +665,44 @@ static void sig_input(void) /* use the bracketed paste mode to detect when the user pastes * some text into the entry */ - if (paste_use_bracketed_mode != FALSE && paste_buffer->len > 12) { - /* try to find the start/end sequence, we know that we - * either find those at the start/end of the buffer or - * we don't find those at all. */ - int seq_start = !memcmp(paste_buffer->data, bp_start, sizeof(bp_start)), - seq_end = !memcmp(paste_buffer->data + paste_buffer->len * g_array_get_element_size(paste_buffer) - sizeof(bp_end), bp_end, sizeof(bp_end)); + if (paste_bracketed_mode) { + int i; + int len = paste_buffer->len - G_N_ELEMENTS(bp_end); + unichar *ptr = (unichar *) paste_buffer->data; - if (seq_start) { - paste_bracketed_mode = TRUE; - /* remove the leading sequence chars */ - g_array_remove_range(paste_buffer, 0, 6); + if (len <= 0) { + return; } - if (seq_end) { - paste_bracketed_mode = FALSE; - /* remove the trailing sequence chars */ - g_array_set_size(paste_buffer, paste_buffer->len - 6); - /* decide what to do with the buffer */ - paste_timeout(NULL); + for (i = 0; i <= len; i++, ptr++) { + if (ptr[0] == bp_end[0] && !memcmp(ptr, bp_end, sizeof(bp_end))) { + /* remove the trailing sequence chars */ + g_array_set_size(paste_buffer, i); + + /* decide what to do with the buffer */ + paste_timeout(NULL); + + paste_bracketed_mode = FALSE; + break; + } } } else if (paste_detect_time > 0 && paste_buffer->len >= 3) { if (paste_timeout_id != -1) g_source_remove(paste_timeout_id); paste_timeout_id = g_timeout_add(paste_detect_time, paste_timeout, NULL); - } else { + } else if (!paste_bracketed_mode) { int i; for (i = 0; i < paste_buffer->len; i++) { unichar key = g_array_index(paste_buffer, unichar, i); signal_emit("gui key pressed", 1, GINT_TO_POINTER(key)); + + if (paste_bracketed_mode) { + /* just enabled by the signal, remove what was processed so far */ + g_array_remove_range(paste_buffer, 0, i + 1); + return; + } } g_array_set_size(paste_buffer, 0); paste_line_count = 0; @@ -703,6 +710,11 @@ static void sig_input(void) } } +static void key_paste_start(void) +{ + paste_bracketed_mode = TRUE; +} + time_t get_idle_time(void) { return last_keypress.tv_sec; @@ -1060,6 +1072,8 @@ void gui_readline_init(void) key_bind("key", NULL, "meta2-5F", "cend", (SIGNAL_FUNC) key_combo); key_bind("key", NULL, "meta2-1;5F", "cend", (SIGNAL_FUNC) key_combo); + key_bind("paste_start", "Bracketed paste start", "meta2-200~", "paste_start", (SIGNAL_FUNC) key_paste_start); + /* cursor movement */ key_bind("backward_character", "Move the cursor a character backward", "left", NULL, (SIGNAL_FUNC) key_backward_character); key_bind("forward_character", "Move the cursor a character forward", "right", NULL, (SIGNAL_FUNC) key_forward_character); @@ -1155,6 +1169,8 @@ void gui_readline_deinit(void) key_configure_freeze(); + key_unbind("paste_start", (SIGNAL_FUNC) key_paste_start); + key_unbind("backward_character", (SIGNAL_FUNC) key_backward_character); key_unbind("forward_character", (SIGNAL_FUNC) key_forward_character); key_unbind("backward_word", (SIGNAL_FUNC) key_backward_word); From cc1215513d2fba016cc7ccec1810295223a2b78d Mon Sep 17 00:00:00 2001 From: dequis Date: Thu, 17 Sep 2015 00:54:13 -0300 Subject: [PATCH 7/8] Save the part of the paste buffer after the bp_end marker for later Also move relevant code to a paste_bracketed_end() function --- src/fe-text/gui-readline.c | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/src/fe-text/gui-readline.c b/src/fe-text/gui-readline.c index c2e7ff4e..f1e8b5a0 100644 --- a/src/fe-text/gui-readline.c +++ b/src/fe-text/gui-readline.c @@ -61,6 +61,7 @@ static int paste_detect_time, paste_verify_line_count; static char *paste_entry; static int paste_entry_pos; static GArray *paste_buffer; +static GArray *paste_buffer_rest; static char *paste_old_prompt; static int paste_prompt, paste_line_count; @@ -331,6 +332,12 @@ static void paste_flush(int send) paste_send(); g_array_set_size(paste_buffer, 0); + /* re-add anything that may have been after the bracketed paste end */ + if (paste_buffer_rest->len) { + g_array_append_vals(paste_buffer, paste_buffer_rest->data, paste_buffer_rest->len); + g_array_set_size(paste_buffer_rest, 0); + } + gui_entry_set_prompt(active_entry, paste_old_prompt == NULL ? "" : paste_old_prompt); g_free(paste_old_prompt); paste_old_prompt = NULL; @@ -643,6 +650,26 @@ static gboolean paste_timeout(gpointer data) return FALSE; } +static void paste_bracketed_end(int i, gboolean rest) +{ + /* if there's stuff after the end bracket, save it for later */ + if (rest) { + unichar *start = ((unichar *) paste_buffer->data) + i + G_N_ELEMENTS(bp_end); + int len = paste_buffer->len - G_N_ELEMENTS(bp_end); + + g_array_set_size(paste_buffer_rest, 0); + g_array_append_vals(paste_buffer_rest, start, len); + } + + /* remove the rest, including the trailing sequence chars */ + g_array_set_size(paste_buffer, i); + + /* decide what to do with the buffer */ + paste_timeout(NULL); + + paste_bracketed_mode = FALSE; +} + static void sig_input(void) { if (!active_entry) { @@ -676,13 +703,7 @@ static void sig_input(void) for (i = 0; i <= len; i++, ptr++) { if (ptr[0] == bp_end[0] && !memcmp(ptr, bp_end, sizeof(bp_end))) { - /* remove the trailing sequence chars */ - g_array_set_size(paste_buffer, i); - - /* decide what to do with the buffer */ - paste_timeout(NULL); - - paste_bracketed_mode = FALSE; + paste_bracketed_end(i, i != len); break; } } @@ -993,6 +1014,7 @@ void gui_readline_init(void) paste_entry = NULL; paste_entry_pos = 0; paste_buffer = g_array_new(FALSE, FALSE, sizeof(unichar)); + paste_buffer_rest = g_array_new(FALSE, FALSE, sizeof(unichar)); paste_old_prompt = NULL; paste_timeout_id = -1; paste_bracketed_mode = FALSE; @@ -1228,6 +1250,7 @@ void gui_readline_deinit(void) key_unbind("stop_irc", (SIGNAL_FUNC) key_sig_stop); keyboard_destroy(keyboard); g_array_free(paste_buffer, TRUE); + g_array_free(paste_buffer_rest, TRUE); key_configure_thaw(); From 0fc6e878e0735368842714e687e43f65463df046 Mon Sep 17 00:00:00 2001 From: dequis Date: Thu, 17 Sep 2015 00:55:31 -0300 Subject: [PATCH 8/8] Fix FS#905, mangled text when pasted line length exceeds 400 http://bugs.irssi.org/index.php?do=details&task_id=905 Not using the patch from that ticket, the issue turned out to be that (dest - last_lf_pos) returned number of unichr, not bytes, so that's 4 times less than what the size parameter of memmove() should be. --- src/fe-text/gui-readline.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fe-text/gui-readline.c b/src/fe-text/gui-readline.c index f1e8b5a0..61cdad1a 100644 --- a/src/fe-text/gui-readline.c +++ b/src/fe-text/gui-readline.c @@ -247,7 +247,7 @@ static void paste_buffer_join_lines(GArray *buf) last_lf = FALSE; if (++line_len >= 400 && last_lf_pos != NULL) { memmove(last_lf_pos+1, last_lf_pos, - dest - last_lf_pos); + (dest - last_lf_pos) * sizeof(unichar)); *last_lf_pos = '\n'; last_lf_pos = NULL; line_len = 0; dest++;