From eab874eb06de11763683f312e6c1557319788cee Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 28 Mar 2021 21:03:53 +0200 Subject: selection: expose find_word_boundary_{left,right}() --- selection.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'selection.c') diff --git a/selection.c b/selection.c index 5bb386ab..7b0d93d7 100644 --- a/selection.c +++ b/selection.c @@ -227,9 +227,9 @@ selection_to_text(const struct terminal *term) return extract_finish(ctx, &text, NULL) ? text : NULL; } -static void -find_word_boundary_left(struct terminal *term, struct coord *pos, - bool spaces_only) +void +selection_find_word_boundary_left(struct terminal *term, struct coord *pos, + bool spaces_only) { const struct row *r = grid_row_in_view(term->grid, pos->row); wchar_t c = r->cells[pos->col].wc; @@ -299,9 +299,9 @@ find_word_boundary_left(struct terminal *term, struct coord *pos, } } -static void -find_word_boundary_right(struct terminal *term, struct coord *pos, - bool spaces_only) +void +selection_find_word_boundary_right(struct terminal *term, struct coord *pos, + bool spaces_only) { const struct row *r = grid_row_in_view(term->grid, pos->row); wchar_t c = r->cells[pos->col].wc; @@ -403,8 +403,8 @@ selection_start(struct terminal *term, int col, int row, case SELECTION_WORD_WISE: { struct coord start = {col, row}, end = {col, row}; - find_word_boundary_left(term, &start, spaces_only); - find_word_boundary_right(term, &end, spaces_only); + selection_find_word_boundary_left(term, &start, spaces_only); + selection_find_word_boundary_right(term, &end, spaces_only); term->selection.start = (struct coord){ start.col, term->grid->view + start.row}; @@ -694,14 +694,16 @@ selection_update(struct terminal *term, int col, int row) switch (term->selection.direction) { case SELECTION_LEFT: { struct coord end = {col, row}; - find_word_boundary_left(term, &end, term->selection.spaces_only); + selection_find_word_boundary_left( + term, &end, term->selection.spaces_only); new_end = (struct coord){end.col, term->grid->view + end.row}; break; } case SELECTION_RIGHT: { struct coord end = {col, row}; - find_word_boundary_right(term, &end, term->selection.spaces_only); + selection_find_word_boundary_right( + term, &end, term->selection.spaces_only); new_end = (struct coord){end.col, term->grid->view + end.row}; break; } @@ -840,8 +842,10 @@ selection_extend_normal(struct terminal *term, int col, int row, struct coord pivot_start = {new_start.col, new_start.row - term->grid->view}; struct coord pivot_end = pivot_start; - find_word_boundary_left(term, &pivot_start, term->selection.spaces_only); - find_word_boundary_right(term, &pivot_end, term->selection.spaces_only); + selection_find_word_boundary_left( + term, &pivot_start, term->selection.spaces_only); + selection_find_word_boundary_right( + term, &pivot_end, term->selection.spaces_only); term->selection.pivot.start = (struct coord){pivot_start.col, term->grid->view + pivot_start.row}; -- cgit v1.3 From 1bc9fd5fe14a60db861fb317f731a5e27e857bf3 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 28 Mar 2021 21:04:48 +0200 Subject: extract: add extract_finish_wide(), and optionally skip stripping trailing empty cells extract_finish() returns the extracted text in UTF-8, while extract_finish_wide() returns the extracted text in Unicode. This patch also adds a new argument to extract_finish{,_wide}, that when set to true, skips stripping trailing empty cells. --- extract.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++---------- extract.h | 7 ++++++- selection.c | 2 +- terminal.c | 2 +- 4 files changed, 55 insertions(+), 13 deletions(-) (limited to 'selection.c') diff --git a/extract.c b/extract.c index ac5f3897..dd72ea92 100644 --- a/extract.c +++ b/extract.c @@ -51,10 +51,9 @@ ensure_size(struct extraction_context *ctx, size_t additional_chars) } bool -extract_finish(struct extraction_context *ctx, char **text, size_t *len) +extract_finish_wide(struct extraction_context *ctx, bool strip_trailing_empty, + wchar_t **text, size_t *len) { - bool ret = false; - if (text == NULL) return false; @@ -63,12 +62,24 @@ extract_finish(struct extraction_context *ctx, char **text, size_t *len) *len = 0; if (ctx->failed) - goto out; + goto err; + + if (!strip_trailing_empty) { + /* Insert pending newlines, and replace empty cells with spaces */ + if (!ensure_size(ctx, ctx->newline_count + ctx->empty_count)) + goto err; + + for (size_t i = 0; i < ctx->newline_count; i++) + ctx->buf[ctx->idx++] = L'\n'; + + for (size_t i = 0; i < ctx->empty_count; i++) + ctx->buf[ctx->idx++] = L' '; + } if (ctx->idx == 0) { /* Selection of empty cells only */ if (!ensure_size(ctx, 1)) - goto out; + goto err; ctx->buf[ctx->idx++] = L'\0'; } else { xassert(ctx->idx > 0); @@ -77,12 +88,39 @@ extract_finish(struct extraction_context *ctx, char **text, size_t *len) ctx->buf[ctx->idx - 1] = L'\0'; else { if (!ensure_size(ctx, 1)) - goto out; + goto err; ctx->buf[ctx->idx++] = L'\0'; } } - size_t _len = wcstombs(NULL, ctx->buf, 0); + *text = ctx->buf; + if (len != NULL) + *len = ctx->idx - 1; + free(ctx); + return true; + +err: + free(ctx->buf); + free(ctx); + return false; +} + +bool +extract_finish(struct extraction_context *ctx, bool strip_trailing_empty, + char **text, size_t *len) +{ + if (text == NULL) + return false; + if (len != NULL) + *len = 0; + + wchar_t *wtext; + if (!extract_finish_wide(ctx, strip_trailing_empty, &wtext, NULL)) + return false; + + bool ret = false; + + size_t _len = wcstombs(NULL, wtext, 0); if (_len == (size_t)-1) { LOG_ERRNO("failed to convert selection to UTF-8"); goto out; @@ -94,7 +132,7 @@ extract_finish(struct extraction_context *ctx, char **text, size_t *len) goto out; } - wcstombs(*text, ctx->buf, _len + 1); + wcstombs(*text, wtext, _len + 1); if (len != NULL) *len = _len; @@ -102,8 +140,7 @@ extract_finish(struct extraction_context *ctx, char **text, size_t *len) ret = true; out: - free(ctx->buf); - free(ctx); + free(wtext); return ret; } diff --git a/extract.h b/extract.h index 0b3693d2..879461cf 100644 --- a/extract.h +++ b/extract.h @@ -2,6 +2,7 @@ #include #include +#include #include "terminal.h" @@ -14,4 +15,8 @@ bool extract_one( int col, void *context); bool extract_finish( - struct extraction_context *context, char **text, size_t *len); + struct extraction_context *context, bool strip_trailing_empty, + char **text, size_t *len); +bool extract_finish_wide( + struct extraction_context *context, bool strip_trailing_empty, + wchar_t **text, size_t *len); diff --git a/selection.c b/selection.c index 7b0d93d7..7316dfc3 100644 --- a/selection.c +++ b/selection.c @@ -224,7 +224,7 @@ selection_to_text(const struct terminal *term) &extract_one_const_wrapper, ctx); char *text; - return extract_finish(ctx, &text, NULL) ? text : NULL; + return extract_finish(ctx, true, &text, NULL) ? text : NULL; } void diff --git a/terminal.c b/terminal.c index 4a39b659..c2bcf86f 100644 --- a/terminal.c +++ b/terminal.c @@ -2917,7 +2917,7 @@ rows_to_text(const struct terminal *term, int start, int end, } out: - return extract_finish(ctx, text, len); + return extract_finish(ctx, true, text, len); } bool -- cgit v1.3 From a6d9f01c0d1d914362b73eacd89e1e09877632cb Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 30 Mar 2021 14:40:21 +0200 Subject: extract: move ‘strip_trailing_empty’ parameter from extra_finish() to extract_begin() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extract.c | 28 +++++++++++++++++++++------- extract.h | 9 ++++----- pgo/pgo.c | 2 +- search.c | 4 ++-- selection.c | 4 ++-- terminal.c | 4 ++-- 6 files changed, 32 insertions(+), 19 deletions(-) (limited to 'selection.c') diff --git a/extract.c b/extract.c index dd72ea92..b9fd3331 100644 --- a/extract.c +++ b/extract.c @@ -11,6 +11,7 @@ struct extraction_context { size_t idx; size_t empty_count; size_t newline_count; + bool strip_trailing_empty; bool failed; const struct row *last_row; const struct cell *last_cell; @@ -18,7 +19,7 @@ struct extraction_context { }; struct extraction_context * -extract_begin(enum selection_kind kind) +extract_begin(enum selection_kind kind, bool strip_trailing_empty) { struct extraction_context *ctx = malloc(sizeof(*ctx)); if (unlikely(ctx == NULL)) { @@ -28,6 +29,7 @@ extract_begin(enum selection_kind kind) *ctx = (struct extraction_context){ .selection_kind = kind, + .strip_trailing_empty = strip_trailing_empty, }; return ctx; } @@ -51,8 +53,7 @@ ensure_size(struct extraction_context *ctx, size_t additional_chars) } bool -extract_finish_wide(struct extraction_context *ctx, bool strip_trailing_empty, - wchar_t **text, size_t *len) +extract_finish_wide(struct extraction_context *ctx, wchar_t **text, size_t *len) { if (text == NULL) return false; @@ -64,7 +65,7 @@ extract_finish_wide(struct extraction_context *ctx, bool strip_trailing_empty, if (ctx->failed) goto err; - if (!strip_trailing_empty) { + if (!ctx->strip_trailing_empty) { /* Insert pending newlines, and replace empty cells with spaces */ if (!ensure_size(ctx, ctx->newline_count + ctx->empty_count)) goto err; @@ -106,8 +107,7 @@ err: } bool -extract_finish(struct extraction_context *ctx, bool strip_trailing_empty, - char **text, size_t *len) +extract_finish(struct extraction_context *ctx, char **text, size_t *len) { if (text == NULL) return false; @@ -115,7 +115,7 @@ extract_finish(struct extraction_context *ctx, bool strip_trailing_empty, *len = 0; wchar_t *wtext; - if (!extract_finish_wide(ctx, strip_trailing_empty, &wtext, NULL)) + if (!extract_finish_wide(ctx, &wtext, NULL)) return false; bool ret = false; @@ -167,6 +167,13 @@ extract_one(const struct terminal *term, const struct row *row, /* Don't emit newline just yet - only if there are * non-empty cells following it */ ctx->newline_count++; + + if (!ctx->strip_trailing_empty) { + if (!ensure_size(ctx, ctx->empty_count)) + goto err; + for (size_t i = 0; i < ctx->empty_count; i++) + ctx->buf[ctx->idx++] = L' '; + } ctx->empty_count = 0; } } else { @@ -175,6 +182,13 @@ extract_one(const struct terminal *term, const struct row *row, goto err; ctx->buf[ctx->idx++] = L'\n'; + + if (!ctx->strip_trailing_empty) { + if (!ensure_size(ctx, ctx->empty_count)) + goto err; + for (size_t i = 0; i < ctx->empty_count; i++) + ctx->buf[ctx->idx++] = L' '; + } ctx->empty_count = 0; } } diff --git a/extract.h b/extract.h index 879461cf..dc802bc5 100644 --- a/extract.h +++ b/extract.h @@ -8,15 +8,14 @@ struct extraction_context; -struct extraction_context *extract_begin(enum selection_kind kind); +struct extraction_context *extract_begin( + enum selection_kind kind, bool strip_trailing_empty); bool extract_one( const struct terminal *term, const struct row *row, const struct cell *cell, int col, void *context); bool extract_finish( - struct extraction_context *context, bool strip_trailing_empty, - char **text, size_t *len); + struct extraction_context *context, char **text, size_t *len); bool extract_finish_wide( - struct extraction_context *context, bool strip_trailing_empty, - wchar_t **text, size_t *len); + struct extraction_context *context, wchar_t **text, size_t *len); diff --git a/pgo/pgo.c b/pgo/pgo.c index c1620b04..34af7115 100644 --- a/pgo/pgo.c +++ b/pgo/pgo.c @@ -106,7 +106,7 @@ render_worker_thread(void *_ctx) } struct extraction_context * -extract_begin(enum selection_kind kind) +extract_begin(enum selection_kind kind, bool strip_trailing_empty) { return NULL; } diff --git a/search.c b/search.c index cce205d4..e7d03fe1 100644 --- a/search.c +++ b/search.c @@ -473,7 +473,7 @@ search_match_to_end_of_word(struct terminal *term, bool spaces_only) struct coord pos = old_end; row = term->grid->rows[pos.row]; - struct extraction_context *ctx = extract_begin(SELECTION_NONE); + struct extraction_context *ctx = extract_begin(SELECTION_NONE, false); if (ctx == NULL) return; @@ -487,7 +487,7 @@ search_match_to_end_of_word(struct terminal *term, bool spaces_only) wchar_t *new_text; size_t new_len; - if (!extract_finish_wide(ctx, false, &new_text, &new_len)) + if (!extract_finish_wide(ctx, &new_text, &new_len)) return; if (!search_ensure_size(term, term->search.len + new_len)) diff --git a/selection.c b/selection.c index 7316dfc3..f684c2fa 100644 --- a/selection.c +++ b/selection.c @@ -215,7 +215,7 @@ selection_to_text(const struct terminal *term) if (term->selection.end.row == -1) return NULL; - struct extraction_context *ctx = extract_begin(term->selection.kind); + struct extraction_context *ctx = extract_begin(term->selection.kind, true); if (ctx == NULL) return NULL; @@ -224,7 +224,7 @@ selection_to_text(const struct terminal *term) &extract_one_const_wrapper, ctx); char *text; - return extract_finish(ctx, true, &text, NULL) ? text : NULL; + return extract_finish(ctx, &text, NULL) ? text : NULL; } void diff --git a/terminal.c b/terminal.c index c2bcf86f..8264274a 100644 --- a/terminal.c +++ b/terminal.c @@ -2900,7 +2900,7 @@ static bool rows_to_text(const struct terminal *term, int start, int end, char **text, size_t *len) { - struct extraction_context *ctx = extract_begin(SELECTION_NONE); + struct extraction_context *ctx = extract_begin(SELECTION_NONE, true); if (ctx == NULL) return false; @@ -2917,7 +2917,7 @@ rows_to_text(const struct terminal *term, int start, int end, } out: - return extract_finish(ctx, true, text, len); + return extract_finish(ctx, text, len); } bool -- cgit v1.3