From 76cd1aa9c674db76e2a95db5a95aa79c5a2ce4ae Mon Sep 17 00:00:00 2001 From: pommicket Date: Sun, 15 Oct 2023 14:36:12 -0400 Subject: remove-trailing-whitespace option --- buffer.c | 20 +++++++++++++++++++- config.c | 1 + ted-internal.h | 1 + ted.cfg | 6 ++++-- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/buffer.c b/buffer.c index c006096..35330ec 100644 --- a/buffer.c +++ b/buffer.c @@ -3139,6 +3139,7 @@ static bool buffer_write_to_file(TextBuffer *buffer, const char *path) { buffer_error(buffer, "Couldn't open file %s for writing: %s.", path, strerror(errno)); return false; } + buffer_start_edit_chain(buffer); if (settings->auto_add_newline) { Line *last_line = &buffer->lines[buffer->nlines - 1]; if (last_line->len) { @@ -3148,7 +3149,24 @@ static bool buffer_write_to_file(TextBuffer *buffer, const char *path) { buffer_insert_text_at_pos(buffer, buffer_pos_end_of_file(buffer), s); } } - + if (settings->remove_trailing_whitespace) { + // remove trailing whitespace + for (u32 l = 0; l < buffer->nlines; l++) { + Line *line = &buffer->lines[l]; + u32 i = line->len; + while (i > 0 && is32_space(line->str[i - 1])) { + i -= 1; + } + if (i < line->len) { + BufferPos pos = { + .line = l, + .index = i, + }; + buffer_delete_chars_at_pos(buffer, pos, line->len - i); + } + } + } + buffer_end_edit_chain(buffer); bool success = true; for (u32 i = 0; i < buffer->nlines; ++i) { diff --git a/config.c b/config.c index 0098095..3e9aa2a 100644 --- a/config.c +++ b/config.c @@ -100,6 +100,7 @@ static const Settings settings_zero = {0}; static const SettingBool settings_bool[] = { {"auto-indent", &settings_zero.auto_indent, true}, {"auto-add-newline", &settings_zero.auto_add_newline, true}, + {"remove-trailing-whitespace", &settings_zero.remove_trailing_whitespace, true}, {"auto-reload", &settings_zero.auto_reload, true}, {"auto-reload-config", &settings_zero.auto_reload_config, false}, {"syntax-highlighting", &settings_zero.syntax_highlighting, true}, diff --git a/ted-internal.h b/ted-internal.h index 2a6eb27..5594adc 100644 --- a/ted-internal.h +++ b/ted-internal.h @@ -115,6 +115,7 @@ struct Settings { u16 lsp_port; bool auto_indent; bool auto_add_newline; + bool remove_trailing_whitespace; bool syntax_highlighting; bool line_numbers; bool auto_reload; diff --git a/ted.cfg b/ted.cfg index 8fd7be3..8ad5362 100644 --- a/ted.cfg +++ b/ted.cfg @@ -31,10 +31,12 @@ max-menu-width = 600 padding = 6 error-display-time = 10 auto-indent = on +line-numbers = on +syntax-highlighting = on # automatically add a newline at the end of the file on save auto-add-newline = on -syntax-highlighting = on -line-numbers = on +# automatically remove trailing whitespace at end of line on save +remove-trailing-whitespace = off # if set to "on", when a file is changed by another program, it will be reloaded by ted without asking you. auto-reload = on # automatically reload config when saved -- cgit v1.2.3