1
0
mirror of https://github.com/irssi/irssi.git synced 2024-12-04 14:46:39 -05:00

Merge pull request #971 from ailin-nemui/paste-join-lines

fix paste_join_multiline
This commit is contained in:
ailin-nemui 2018-11-19 16:07:00 +01:00 committed by GitHub
commit 257cb2f5f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 129 additions and 8 deletions

View File

@ -732,6 +732,7 @@ scripts/examples/Makefile
tests/Makefile tests/Makefile
tests/fe-common/Makefile tests/fe-common/Makefile
tests/fe-common/core/Makefile tests/fe-common/core/Makefile
tests/fe-text/Makefile
tests/irc/Makefile tests/irc/Makefile
tests/irc/core/Makefile tests/irc/core/Makefile
tests/irc/flood/Makefile tests/irc/flood/Makefile

View File

@ -160,6 +160,7 @@ static void window_next_page(void)
gui_window_scroll(active_win, get_scroll_count()); gui_window_scroll(active_win, get_scroll_count());
} }
#define isnewline(x) ((x) == '\n' || (x) == '\r')
static void paste_buffer_join_lines(GArray *buf) static void paste_buffer_join_lines(GArray *buf)
{ {
unsigned int i, count, indent, line_len; unsigned int i, count, indent, line_len;
@ -199,7 +200,7 @@ static void paste_buffer_join_lines(GArray *buf)
/* find the first beginning of indented line */ /* find the first beginning of indented line */
for (i = 1; i < buf->len; i++) { for (i = 1; i < buf->len; i++) {
if (arr[i-1] == '\n' && isblank(arr[i])) if (isnewline(arr[i-1]) && isblank(arr[i]))
break; break;
} }
if (i == buf->len) if (i == buf->len)
@ -226,7 +227,7 @@ static void paste_buffer_join_lines(GArray *buf)
count = 0; count = 0;
} }
} }
if (arr[i] == '\n') if (isnewline(arr[i]))
last_lf = TRUE; last_lf = TRUE;
} }
@ -236,11 +237,12 @@ static void paste_buffer_join_lines(GArray *buf)
for (i = 0; i < buf->len; i++) { for (i = 0; i < buf->len; i++) {
if (last_lf && isblank(arr[i])) { if (last_lf && isblank(arr[i])) {
/* whitespace, ignore */ /* whitespace, ignore */
} else if (arr[i] == '\n') { } else if (isnewline(arr[i])) {
if (!last_lf && i+1 != buf->len && if (!last_lf && i+1 != buf->len &&
isblank(arr[i+1])) { isblank(arr[i+1])) {
last_lf_pos = dest; last_lf_pos = dest;
*dest++ = ' '; if (i != 0 && !isblank(arr[i-1]))
*dest++ = ' ';
} else { } else {
*dest++ = '\n'; /* double-LF */ *dest++ = '\n'; /* double-LF */
line_len = 0; line_len = 0;
@ -287,7 +289,7 @@ static void paste_send(void)
/* first line has to be kludged kind of to get pasting in the /* first line has to be kludged kind of to get pasting in the
middle of line right.. */ middle of line right.. */
for (i = 0; i < paste_buffer->len; i++) { for (i = 0; i < paste_buffer->len; i++) {
if (arr[i] == '\r' || arr[i] == '\n') { if (isnewline(arr[i])) {
i++; i++;
break; break;
} }
@ -303,7 +305,7 @@ static void paste_send(void)
/* rest of the lines */ /* rest of the lines */
str = g_string_new(NULL); str = g_string_new(NULL);
for (; i < paste_buffer->len; i++) { for (; i < paste_buffer->len; i++) {
if (arr[i] == '\r' || arr[i] == '\n') { if (isnewline(arr[i])) {
paste_send_line(str->str); paste_send_line(str->str);
g_string_truncate(str, 0); g_string_truncate(str, 0);
} else if (active_entry->utf8) { } else if (active_entry->utf8) {
@ -769,7 +771,7 @@ static void paste_bracketed_end(int i, gboolean rest)
last_char = g_array_index(paste_buffer, unichar, i - 1); last_char = g_array_index(paste_buffer, unichar, i - 1);
if (paste_line_count > 0 && last_char != '\n' && last_char != '\r') { if (paste_line_count > 0 && !isnewline(last_char)) {
/* there are newlines, but there's also stuff after the newline /* there are newlines, but there's also stuff after the newline
* adjust line count to reflect this */ * adjust line count to reflect this */
paste_line_count++; paste_line_count++;

View File

@ -1 +1,5 @@
SUBDIRS = fe-common irc if BUILD_TEXTUI
TEXTUI=fe-text
endif
SUBDIRS = fe-common irc $(TEXTUI)

38
tests/fe-text/Makefile.am Normal file
View File

@ -0,0 +1,38 @@
include $(top_srcdir)/utils/glib-tap.mk
PACKAGE_STRING=fe-text
AM_CPPFLAGS = \
-I$(top_srcdir)/src \
-I$(top_srcdir)/src/core \
-I$(top_srcdir)/src/fe-common/core \
-I$(top_srcdir)/src/fe-text \
$(GLIB_CFLAGS)
test_programs = test-paste-join-multiline
test_paste_join_multiline_CPPFLAGS = \
$(AM_CPPFLAGS)
# test_paste_join_multiline_DEPENDENCIES =
test_paste_join_multiline_LDADD = \
../../src/fe-common/core/libfe_common_core.a \
../../src/core/libcore.a \
../../src/lib-config/libirssi_config.a \
@PROG_LIBS@ \
@TEXTUI_LIBS@
test_paste_join_multiline_SOURCES = \
../../src/fe-text/gui-entry.c \
../../src/fe-text/mainwindows.c \
../../src/fe-text/term-terminfo.c \
../../src/fe-text/terminfo-core.c \
../../src/fe-text/term.c \
../../src/fe-text/textbuffer-view.c \
../../src/fe-text/textbuffer.c \
../../src/fe-text/gui-windows.c \
../../src/fe-text/gui-printtext.c \
mock-irssi.c \
test-paste-join-multiline.c

View File

@ -0,0 +1,9 @@
void irssi_set_dirty(void)
{
}
void irssi_redraw(void)
{
}
int quitting = 0;

View File

@ -0,0 +1,67 @@
#include "common.h"
#include "gui-readline.c"
typedef struct {
char const *const description;
char const *const input;
char const *const result;
} paste_join_multiline_test_case;
static void test_paste_join_multiline(const paste_join_multiline_test_case *test);
paste_join_multiline_test_case const paste_join_multiline_fixture[] = {
{
.description = "Lines should be joined, separator NL",
.input = "<User> A1\n B22\n C33",
.result = "<User> A1 B22 C33",
},
{
.description = "Lines should be joined, separator LF",
.input = "<User> A1\r B22\r C33",
.result = "<User> A1 B22 C33",
},
{
.description = "Lines should be joined, white space should be skipped",
.input = "<User> A1 \n B22 \n C33 ",
.result = "<User> A1 B22 C33 ",
},
};
int main(int argc, char **argv)
{
int i;
g_test_init(&argc, &argv, NULL);
for (i = 0; i < G_N_ELEMENTS(paste_join_multiline_fixture); i++) {
char *name = g_strdup_printf("/test/paste_join_multiline/%d", i);
g_test_add_data_func(name, &paste_join_multiline_fixture[i], (GTestDataFunc)test_paste_join_multiline);
g_free(name);
}
#if GLIB_CHECK_VERSION(2,38,0)
g_test_set_nonfatal_assertions();
#endif
return g_test_run();
}
static void test_paste_join_multiline(const paste_join_multiline_test_case *test)
{
char *resultstr, *t1;
GArray *buffer = g_array_new(FALSE, FALSE, sizeof(unichar));
g_test_message("Testing: %s", test->description);
g_test_message("INPUT: \"%s\"", (t1 = g_strescape(test->input, NULL)));
g_free(t1);
buffer->data = (char *) g_utf8_to_ucs4_fast(test->input, -1, (glong *) &buffer->len);
paste_buffer_join_lines(buffer);
resultstr = g_ucs4_to_utf8((unichar *) buffer->data, buffer->len, NULL, NULL, NULL);
g_assert_cmpstr(resultstr, ==, test->result);
g_free(resultstr);
g_array_free(buffer, TRUE);
return;
}