diff options
-rw-r--r-- | buffer.c | 20 | ||||
-rw-r--r-- | config.c | 1 | ||||
-rw-r--r-- | ted-internal.h | 1 | ||||
-rw-r--r-- | ted.cfg | 6 |
4 files changed, 25 insertions, 3 deletions
@@ -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) { @@ -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; @@ -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 |