From efce2c731927adc4aaf26d6cc6c165bbced39eb5 Mon Sep 17 00:00:00 2001 From: pommicket Date: Fri, 29 Jul 2022 15:46:13 -0400 Subject: auto reload config --- README.md | 2 +- buffer.c | 7 ++++++- config.c | 11 ++++++++-- main.c | 70 ++++++++++++++++++--------------------------------------------- ted.c | 35 ++++++++++++++++++++++++++++++++ ted.cfg | 6 +++--- ted.h | 4 ++++ 7 files changed, 78 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index ff9d651..45feb24 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ to open the command palette, and select "open-config". There are several section Comments begin with `#`, and all other lines are of the form `key = value`. -You need to restart ted when you make a change to ted.cfg. +By default ted's settings will automatically update when you save the config file. The `core` section's settings should be pretty familiar (font size, etc.) or should have comments on the previous line explaining what they do. Keyboard shortcuts are of the form `key combo = action`, where `action` is an argument (number or string), diff --git a/buffer.c b/buffer.c index d73e68c..65a90ab 100644 --- a/buffer.c +++ b/buffer.c @@ -2178,8 +2178,13 @@ bool buffer_save(TextBuffer *buffer) { } buffer->last_write_time = time_last_modified(buffer->filename); bool success = !buffer_haserr(buffer); - if (success) + if (success) { buffer->undo_history_write_pos = arr_len(buffer->undo_history); + char const *name = buffer->filename ? path_filename(buffer->filename) : TED_UNTITLED; + if (streq(name, "ted.cfg") && buffer_settings(buffer)->auto_reload_config) { + ted_load_configs(buffer->ted); + } + } return success; } else { buffer_seterr(buffer, "Couldn't open file %s for writing: %s.", buffer->filename, strerror(errno)); diff --git a/config.c b/config.c index 43036dc..5a52151 100644 --- a/config.c +++ b/config.c @@ -235,6 +235,7 @@ void config_read(Ted *ted, char const *filename, int pass) { {"auto-indent", &nullset->auto_indent, true}, {"auto-add-newline", &nullset->auto_add_newline, true}, {"auto-reload", &nullset->auto_reload, true}, + {"auto-reload-config", &nullset->auto_reload_config, false}, {"syntax-highlighting", &nullset->syntax_highlighting, true}, {"line-numbers", &nullset->line_numbers, true}, {"restore-session", &nullset->restore_session, false}, @@ -521,7 +522,7 @@ void config_read(Ted *ted, char const *filename, int pass) { boolean = false; } - // go through all options + // go through all options bool recognized = false; for (size_t i = 0; i < arr_count(all_options) && !recognized; ++i) { OptionAny const *any = &all_options[i]; @@ -574,6 +575,12 @@ void config_read(Ted *ted, char const *filename, int pass) { } } } + + // this is probably a bad idea: + //if (!recognized) + // config_err(cfg, "Unrecognized option: %s", key); + // because if we ever remove an option in the future + // everyone will get errors } break; } } @@ -594,7 +601,7 @@ void config_read(Ted *ted, char const *filename, int pass) { fclose(fp); } -static void config_free(Ted *ted) { +void config_free(Ted *ted) { for (u16 i = 0; i < LANG_COUNT; ++i) { free(ted->settings_by_language[0].language_extensions[i]); for (u16 l = 0; l < LANG_COUNT; ++l) { diff --git a/main.c b/main.c index 96b4062..448da87 100644 --- a/main.c +++ b/main.c @@ -45,6 +45,22 @@ no_warn_end #error "Unrecognized operating system." #endif +static void die(char const *fmt, ...) { + char buf[256] = {0}; + + va_list args; + 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); +} + #include "io.c" #include "text.h" @@ -77,22 +93,6 @@ bool tag_goto(Ted *ted, char const *tag); #define PROFILE_TIME(var) #endif -static void die(char const *fmt, ...) { - char buf[256] = {0}; - - va_list args; - 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); -} - static Rect error_box_rect(Ted *ted) { Font *font = ted->font; Settings const *settings = ted->settings; @@ -398,40 +398,10 @@ int main(int argc, char **argv) { PROFILE_TIME(misc_end) PROFILE_TIME(configs_start) - { - // copy global config to local config - char local_config_filename[TED_PATH_MAX]; - strbuf_printf(local_config_filename, "%s" PATH_SEPARATOR_STR TED_CFG, ted->local_data_dir); - char global_config_filename[TED_PATH_MAX]; - strbuf_printf(global_config_filename, "%s" PATH_SEPARATOR_STR TED_CFG, ted->global_data_dir); - if (!fs_file_exists(local_config_filename)) { - if (fs_file_exists(global_config_filename)) { - if (!copy_file(global_config_filename, local_config_filename)) { - die("Couldn't copy config %s to %s.", global_config_filename, local_config_filename); - } - } else { - die("ted's backup config file, %s, does not exist. Try reinstalling ted?", global_config_filename); - } - } - - // read global settings - config_read(ted, global_config_filename, 0); - config_read(ted, local_config_filename, 0); - if (ted->search_cwd) { - // read config in cwd - config_read(ted, TED_CFG, 0); - } - // copy global settings to language-specific settings - for (int l = 1; l < LANG_COUNT; ++l) - ted->settings_by_language[l] = ted->settings_by_language[0]; - // read language-specific settings - config_read(ted, global_config_filename, 1); - config_read(ted, local_config_filename, 1); - if (ted->search_cwd) config_read(ted, TED_CFG, 1); - if (ted_haserr(ted)) { - strcpy(config_err, ted->error); - ted_clearerr(ted); // clear the error so later things (e.g. loading font) don't detect an error - } + ted_load_configs(ted); + if (ted_haserr(ted)) { + strcpy(config_err, ted->error); + ted_clearerr(ted); // clear the error so later things (e.g. loading font) don't detect an error } PROFILE_TIME(configs_end) diff --git a/ted.c b/ted.c index e861736..9bff01a 100644 --- a/ted.c +++ b/ted.c @@ -352,3 +352,38 @@ static void ted_reload_all(Ted *ted) { menu_close(ted); } } + +// load/reload configs +void ted_load_configs(Ted *ted) { + config_free(ted); + + // copy global config to local config + char local_config_filename[TED_PATH_MAX]; + strbuf_printf(local_config_filename, "%s" PATH_SEPARATOR_STR TED_CFG, ted->local_data_dir); + char global_config_filename[TED_PATH_MAX]; + strbuf_printf(global_config_filename, "%s" PATH_SEPARATOR_STR TED_CFG, ted->global_data_dir); + if (!fs_file_exists(local_config_filename)) { + if (fs_file_exists(global_config_filename)) { + if (!copy_file(global_config_filename, local_config_filename)) { + die("Couldn't copy config %s to %s.", global_config_filename, local_config_filename); + } + } else { + die("ted's backup config file, %s, does not exist. Try reinstalling ted?", global_config_filename); + } + } + + // read global settings + config_read(ted, global_config_filename, 0); + config_read(ted, local_config_filename, 0); + if (ted->search_cwd) { + // read config in cwd + config_read(ted, TED_CFG, 0); + } + // copy global settings to language-specific settings + for (int l = 1; l < LANG_COUNT; ++l) + ted->settings_by_language[l] = ted->settings_by_language[0]; + // read language-specific settings + config_read(ted, global_config_filename, 1); + config_read(ted, local_config_filename, 1); + if (ted->search_cwd) config_read(ted, TED_CFG, 1); +} diff --git a/ted.cfg b/ted.cfg index 093b226..b4ede62 100644 --- a/ted.cfg +++ b/ted.cfg @@ -22,11 +22,11 @@ auto-indent = on auto-add-newline = on syntax-highlighting = on line-numbers = on -# If set to "on", when a file is changed by another program, it will be reloaded by ted without asking you. +# if set to "on", when a file is changed by another program, it will be reloaded by ted without asking you. auto-reload = off +# automatically reload config when saved +auto-reload-config = on build-default-command = make -# file name for ctags output -tags-filename = tags # restore previously opened files when ted is launched? restore-session = on # search depth for files to generate tags for. diff --git a/ted.h b/ted.h index cfaeba9..94e034c 100644 --- a/ted.h +++ b/ted.h @@ -141,6 +141,7 @@ typedef struct { bool syntax_highlighting; bool line_numbers; bool auto_reload; + bool auto_reload_config; bool restore_session; bool regenerate_tags_if_not_found; bool indent_with_spaces; @@ -403,4 +404,7 @@ void command_execute(Ted *ted, Command c, i64 argument); void ted_switch_to_buffer(Ted *ted, TextBuffer *buffer); // the settings of the active buffer, or the default settings if there is no active buffer Settings *ted_active_settings(Ted *ted); +void ted_load_configs(Ted *ted); static TextBuffer *find_search_buffer(Ted *ted); +void config_read(Ted *ted, const char *filename, int pass); +void config_free(Ted *ted); -- cgit v1.2.3