From 2a3bbb6674fe3b0e4ff996cae750a6e2880315ef Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Fri, 16 Apr 2021 15:22:05 -0400 Subject: regenerate tags if tag not found --- command.c | 2 +- config.c | 1 + main.c | 1 - tags.c | 38 ++++++++++++++++++++++++++++++-------- ted.cfg | 2 ++ ted.h | 1 + 6 files changed, 35 insertions(+), 10 deletions(-) diff --git a/command.c b/command.c index 7f54a68..e1c2ee3 100644 --- a/command.c +++ b/command.c @@ -364,7 +364,7 @@ void command_execute(Ted *ted, Command c, i64 argument) { } } break; case CMD_GENERATE_TAGS: - tags_generate(ted); + tags_generate(ted, true); break; case CMD_GOTO_DEFINITION: diff --git a/config.c b/config.c index c70072d..49a0499 100644 --- a/config.c +++ b/config.c @@ -183,6 +183,7 @@ void config_read(Ted *ted, char const *filename) { {"syntax-highlighting", &settings->syntax_highlighting}, {"line-numbers", &settings->line_numbers}, {"restore-session", &settings->restore_session}, + {"regenerate-tags-if-not-found", &settings->regenerate_tags_if_not_found}, }; OptionU8 const options_u8[] = { {"tab-width", &settings->tab_width, 1, 100}, diff --git a/main.c b/main.c index 9cd2a86..668065a 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,5 @@ // @TODO: // - terminate process not working on windows? -// - auto-regenerate tags (if tag not found) // - comment/uncomment selection #include "base.h" no_warn_start diff --git a/tags.c b/tags.c index 99d8a2c..399e3b1 100644 --- a/tags.c +++ b/tags.c @@ -35,7 +35,7 @@ static bool is_source_file(char const *filename) { } -static void tags_generate_at_dir(Ted *ted, const char *dir, int depth) { +static void tags_generate_at_dir(Ted *ted, bool run_in_build_window, const char *dir, int depth) { Settings const *settings = &ted->settings; if (depth >= settings->tags_max_depth) { return; @@ -65,7 +65,10 @@ static void tags_generate_at_dir(Ted *ted, const char *dir, int depth) { any_files = true; // make sure command doesn't get too long if (cmdlen + pathlen + 5 >= sizeof command) { - build_queue_command(ted, command); + if (run_in_build_window) + build_queue_command(ted, command); + else + system(command); strbuf_printf(command, "%s %s", cmd_prefix, path); } else { command[cmdlen++] = ' '; @@ -74,20 +77,23 @@ static void tags_generate_at_dir(Ted *ted, const char *dir, int depth) { } } break; case FS_DIRECTORY: - tags_generate_at_dir(ted, path, depth+1); + tags_generate_at_dir(ted, run_in_build_window, path, depth+1); break; default: break; } } } if (any_files) { - build_queue_command(ted, command); + if (run_in_build_window) + build_queue_command(ted, command); + else + system(command); } } } // generate/re-generate tags. -static void tags_generate(Ted *ted) { +static void tags_generate(Ted *ted, bool run_in_build_window) { char const *filename = tags_filename(ted, false); if (!filename) { strcpy(ted->tags_dir, ted->cwd); @@ -95,9 +101,9 @@ static void tags_generate(Ted *ted) { change_directory(ted->tags_dir); strcpy(ted->build_dir, ted->tags_dir); remove("tags"); // delete old tags file - build_queue_start(ted); - tags_generate_at_dir(ted, ted->tags_dir, 0); - build_queue_finish(ted); + if (run_in_build_window) build_queue_start(ted); + tags_generate_at_dir(ted, run_in_build_window, ted->tags_dir, 0); + if (run_in_build_window) build_queue_finish(ted); change_directory(ted->cwd); } @@ -190,6 +196,11 @@ size_t tags_beginning_with(Ted *ted, char const *prefix, char **out, size_t out_ // returns true if the tag exists. bool tag_goto(Ted *ted, char const *tag) { + bool already_regenerated_tags; + already_regenerated_tags = false; +top:; + Settings const *settings = &ted->settings; + char const *tags_name = tags_filename(ted, true); if (!tags_name) return false; FILE *file = fopen(tags_name, "rb"); @@ -201,6 +212,7 @@ bool tag_goto(Ted *ted, char const *tag) { size_t lo = 0; size_t hi = file_size; bool success = false; + while (lo < hi) { size_t mid = (lo + hi)/2; fseek(file, (long)mid, SEEK_SET); @@ -303,6 +315,7 @@ bool tag_goto(Ted *ted, char const *tag) { BufferPos pos = {line_idx, (u32)index}; buffer_cursor_move_to_pos(buffer, pos); buffer->center_cursor_next_frame = true; + success = true; break; } } @@ -321,6 +334,15 @@ bool tag_goto(Ted *ted, char const *tag) { } } } + if (!success) { + if (settings->regenerate_tags_if_not_found && !already_regenerated_tags) { + tags_generate(ted, false); + already_regenerated_tags = true; + goto top; + } else { + ted_seterr(ted, "No such tag: %s", tag); + } + } fclose(file); return success; } diff --git a/ted.cfg b/ted.cfg index 64a9230..96bc03b 100644 --- a/ted.cfg +++ b/ted.cfg @@ -30,6 +30,8 @@ tags-filename = tags restore-session = on # search depth for files to generate tags for. if set to 0, tag generation/regeneration will do nothing tags-max-depth = 2 +# regenerate tags if an identifier is not found (with Ctrl+click)? +regenerate-tags-if-not-found = yes [keyboard] # motion and selection diff --git a/ted.h b/ted.h index 90882cc..d0632cc 100644 --- a/ted.h +++ b/ted.h @@ -74,6 +74,7 @@ typedef struct { bool line_numbers; bool auto_reload; bool restore_session; + bool regenerate_tags_if_not_found; u8 tab_width; u8 cursor_width; u8 undo_save_time; -- cgit v1.2.3