diff options
author | pommicket <pommicket@gmail.com> | 2022-07-29 15:46:13 -0400 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2022-07-29 15:46:13 -0400 |
commit | efce2c731927adc4aaf26d6cc6c165bbced39eb5 (patch) | |
tree | 39c4eca8347f132c413a7405c2c3ae35d0fb0903 | |
parent | 296b4eca9479bdd1302c4b2d910f3ddd8c955fb9 (diff) |
auto reload config
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | buffer.c | 7 | ||||
-rw-r--r-- | config.c | 11 | ||||
-rw-r--r-- | main.c | 70 | ||||
-rw-r--r-- | ted.c | 35 | ||||
-rw-r--r-- | ted.cfg | 6 | ||||
-rw-r--r-- | ted.h | 4 |
7 files changed, 78 insertions, 57 deletions
@@ -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), @@ -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)); @@ -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) { @@ -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) @@ -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); +} @@ -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. @@ -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); |