diff --git a/CHANGELOG.md b/CHANGELOG.md index 645b6eeb..0e55316b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,8 @@ * Blinking text now uses the foreground color, but dimmed down in its off state, instead of the background color. +* Trailing spaces are trimmed when copying a block selection + (https://codeberg.org/dnkl/foot/issues/181). ### Deprecated diff --git a/extract.c b/extract.c index 7b5da229..59289755 100644 --- a/extract.c +++ b/extract.c @@ -15,10 +15,11 @@ struct extraction_context { const struct row *last_row; const struct cell *last_cell; enum selection_kind selection_kind; + bool trim_trailing_spaces; }; struct extraction_context * -extract_begin(enum selection_kind kind) +extract_begin(enum selection_kind kind, bool trim_trailing_spaces) { 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, + .trim_trailing_spaces = trim_trailing_spaces, }; return ctx; } @@ -143,7 +145,7 @@ extract_one(const struct terminal *term, const struct row *row, } } - if (cell->wc == 0) { + if (cell->wc == 0 || (ctx->trim_trailing_spaces && cell->wc == L' ')) { ctx->empty_count++; ctx->last_row = row; ctx->last_cell = cell; diff --git a/extract.h b/extract.h index 0b3693d2..3926b8de 100644 --- a/extract.h +++ b/extract.h @@ -7,7 +7,8 @@ struct extraction_context; -struct extraction_context *extract_begin(enum selection_kind kind); +struct extraction_context *extract_begin( + enum selection_kind kind, bool trim_trailing_spaces); bool extract_one( const struct terminal *term, const struct row *row, const struct cell *cell, diff --git a/selection.c b/selection.c index 5b3f06ea..558689e6 100644 --- a/selection.c +++ b/selection.c @@ -224,7 +224,10 @@ selection_to_text(const struct terminal *term) if (term->selection.end.row == -1) return NULL; - struct extraction_context *ctx = extract_begin(term->selection.kind); + const enum selection_kind kind = term->selection.kind; + const bool trim_trailing_spaces = kind == SELECTION_BLOCK; + + struct extraction_context *ctx = extract_begin(kind, trim_trailing_spaces); if (ctx == NULL) return NULL; diff --git a/terminal.c b/terminal.c index b9e4528a..e9a18138 100644 --- a/terminal.c +++ b/terminal.c @@ -2651,7 +2651,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, false); if (ctx == NULL) return false;