From 296b4eca9479bdd1302c4b2d910f3ddd8c955fb9 Mon Sep 17 00:00:00 2001 From: pommicket Date: Fri, 29 Jul 2022 14:56:57 -0400 Subject: indent-with-spaces --- buffer.c | 25 +++++++++++++++++++++---- command.c | 2 +- config.c | 1 + main.c | 4 ++-- ted.cfg | 1 + ted.h | 1 + 6 files changed, 27 insertions(+), 7 deletions(-) diff --git a/buffer.c b/buffer.c index a4eeab6..d73e68c 100644 --- a/buffer.c +++ b/buffer.c @@ -1756,6 +1756,17 @@ void buffer_insert_utf8_at_cursor(TextBuffer *buffer, char const *utf8) { } } +void buffer_insert_tab_at_cursor(TextBuffer *buffer) { + Settings const *settings = buffer_settings(buffer); + + if (settings->indent_with_spaces) { + for (int i = 0; i < settings->tab_width; ++i) + buffer_insert_char_at_cursor(buffer, ' '); + } else { + buffer_insert_char_at_cursor(buffer, '\t'); + } +} + // insert newline at cursor and auto-indent void buffer_newline(TextBuffer *buffer) { if (buffer->is_line_buffer) { @@ -2584,11 +2595,17 @@ void buffer_render(TextBuffer *buffer, Rect r) { void buffer_indent_lines(TextBuffer *buffer, u32 first_line, u32 last_line) { assert(first_line <= last_line); + Settings const *settings = buffer_settings(buffer); + buffer_start_edit_chain(buffer); - for (u32 i = first_line; i <= last_line; ++i) { - BufferPos pos = {.line = i, .index = 0}; - char32_t c = '\t'; - buffer_insert_text_at_pos(buffer, pos, str32(&c, 1)); + for (u32 l = first_line; l <= last_line; ++l) { + BufferPos pos = {.line = l, .index = 0}; + if (settings->indent_with_spaces) { + for (int i = 0; i < settings->tab_width; ++i) + buffer_insert_char_at_pos(buffer, pos, ' '); + } else { + buffer_insert_char_at_pos(buffer, pos, '\t'); + } } buffer_end_edit_chain(buffer); } diff --git a/command.c b/command.c index 3e10a93..fc161e2 100644 --- a/command.c +++ b/command.c @@ -126,7 +126,7 @@ void command_execute(Ted *ted, Command c, i64 argument) { if (buffer->selection) buffer_indent_selection(buffer); else - buffer_insert_char_at_cursor(buffer, '\t'); + buffer_insert_tab_at_cursor(buffer); } break; case CMD_BACKTAB: diff --git a/config.c b/config.c index 3cd64de..43036dc 100644 --- a/config.c +++ b/config.c @@ -239,6 +239,7 @@ void config_read(Ted *ted, char const *filename, int pass) { {"line-numbers", &nullset->line_numbers, true}, {"restore-session", &nullset->restore_session, false}, {"regenerate-tags-if-not-found", &nullset->regenerate_tags_if_not_found, true}, + {"indent-with-spaces", &nullset->indent_with_spaces, true}, }; OptionU8 const options_u8[] = { {"tab-width", &nullset->tab_width, 1, 100, true}, diff --git a/main.c b/main.c index 60bc230..96b4062 100644 --- a/main.c +++ b/main.c @@ -84,12 +84,12 @@ static void die(char const *fmt, ...) { va_start(args, fmt); vsnprintf(buf, sizeof buf - 1, fmt, args); va_end(args); - + // show a message box, and if that fails, print it if (SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", buf, NULL) < 0) { debug_println("%s\n", buf); } - + exit(EXIT_FAILURE); } diff --git a/ted.cfg b/ted.cfg index 6b30ff9..093b226 100644 --- a/ted.cfg +++ b/ted.cfg @@ -1,5 +1,6 @@ [core] tab-width = 4 +indent-with-spaces = off # cursor width in pixels cursor-width = 1 # time to blink cursor for (i.e. it will be on for cursor-blink-time-on seconds, then off for cursor-blink-time-off seconds) diff --git a/ted.h b/ted.h index 1ee8525..cfaeba9 100644 --- a/ted.h +++ b/ted.h @@ -143,6 +143,7 @@ typedef struct { bool auto_reload; bool restore_session; bool regenerate_tags_if_not_found; + bool indent_with_spaces; u8 tab_width; u8 cursor_width; u8 undo_save_time; -- cgit v1.2.3