#include "app.h" #include "common.h" #include "completion.h" #include "path.h" #include #include #include #include #include void app_message(app_t *app, const char *msg) { if (!app || !msg) return; snprintf(app->message, sizeof(app->message), "%s", msg); app->dirty = 1; } static const char *app_clipboard_get(void *userdata) { app_t *app = userdata; if (!app || !app->window) return NULL; return glfwGetClipboardString(app->window); } static void app_clipboard_set(void *userdata, const char *text) { app_t *app = userdata; if (!app || !app->window || !text) return; glfwSetClipboardString(app->window, text); } static void app_reset_completion(app_t *app) { if (!app) return; app->completion_active = 0; app->completion_query[0] = '\0'; app->completion_index = 0; app->completion_count = 0; } static void app_set_minibuffer(app_t *app, const char *text) { if (!app || !text) return; snprintf(app->minibuffer, sizeof(app->minibuffer), "%s", text); app->minibuffer_len = strlen(app->minibuffer); app->dirty = 1; } static void app_reset_prompt_completion(app_t *app) { if (!app) return; app->prompt_completion_active = 0; app->prompt_completion_query[0] = '\0'; app->prompt_completion_index = 0; app->prompt_completion_count = 0; } static void app_set_prompt_input(app_t *app, const char *text) { if (!app || !text) return; snprintf(app->prompt_input, sizeof(app->prompt_input), "%s", text); app->prompt_input_len = strlen(app->prompt_input); app->dirty = 1; } typedef struct command_candidate { const char *name; int score; size_t order; } command_candidate_t; static int compare_command_candidates(const void *a, const void *b) { const command_candidate_t *ca = (const command_candidate_t *)a; const command_candidate_t *cb = (const command_candidate_t *)b; if (ca->score != cb->score) { return cb->score - ca->score; } if (ca->order < cb->order) return -1; if (ca->order > cb->order) return 1; return 0; } static command_candidate_t *collect_command_candidates(ecex_t *ed, const char *query, size_t *out_count) { if (out_count) *out_count = 0; if (!ed || !query || ed->command_count == 0) return NULL; command_candidate_t *items = malloc(ed->command_count * sizeof(*items)); if (!items) return NULL; size_t count = 0; for (size_t i = 0; i < ed->command_count; i++) { const char *name = ed->commands[i].name; int score = ecex_fuzzy_score(name, query); if (score >= 0) { items[count].name = name; items[count].score = score; items[count].order = i; count++; } } if (count == 0) { free(items); return NULL; } qsort(items, count, sizeof(*items), compare_command_candidates); if (out_count) *out_count = count; return items; } static const char *command_candidate_at(ecex_t *ed, const char *query, size_t index, size_t *out_count) { size_t count = 0; command_candidate_t *items = collect_command_candidates(ed, query, &count); if (!items || count == 0) return NULL; const char *name = items[index % count].name; if (out_count) { *out_count = count; } free(items); return name; } typedef struct path_candidate { char *path; int score; int is_dir; size_t order; } path_candidate_t; static void free_path_candidates(path_candidate_t *items, size_t count) { if (!items) return; for (size_t i = 0; i < count; i++) { free(items[i].path); } free(items); } static int compare_path_candidates(const void *a, const void *b) { const path_candidate_t *pa = (const path_candidate_t *)a; const path_candidate_t *pb = (const path_candidate_t *)b; if (pa->is_dir != pb->is_dir) { return pb->is_dir - pa->is_dir; } if (pa->score != pb->score) { return pb->score - pa->score; } int cmp = strcmp(pa->path, pb->path); if (cmp != 0) return cmp; if (pa->order < pb->order) return -1; if (pa->order > pb->order) return 1; return 0; } static int split_path_query(const char *query, char *display_dir, size_t display_dir_size, char *fs_dir, size_t fs_dir_size, char *needle, size_t needle_size) { if (!query || !display_dir || !fs_dir || !needle) return -1; const char *last_slash = strrchr(query, '/'); if (last_slash) { size_t dir_len = (size_t)(last_slash - query) + 1; if (dir_len >= display_dir_size) dir_len = display_dir_size - 1; memcpy(display_dir, query, dir_len); display_dir[dir_len] = '\0'; snprintf(needle, needle_size, "%s", last_slash + 1); } else { display_dir[0] = '\0'; snprintf(needle, needle_size, "%s", query); } if (display_dir[0] == '\0') { snprintf(fs_dir, fs_dir_size, "."); } else { char *expanded = ecex_path_expand_user(display_dir); if (!expanded) return -1; snprintf(fs_dir, fs_dir_size, "%s", expanded); free(expanded); } return 0; } static char *join_display_path(const char *display_dir, const char *name, int is_dir) { size_t dir_len = display_dir ? strlen(display_dir) : 0; size_t name_len = name ? strlen(name) : 0; size_t slash_len = is_dir ? 1 : 0; char *out = malloc(dir_len + name_len + slash_len + 1); if (!out) return NULL; if (dir_len) memcpy(out, display_dir, dir_len); if (name_len) memcpy(out + dir_len, name, name_len); if (is_dir) out[dir_len + name_len] = '/'; out[dir_len + name_len + slash_len] = '\0'; return out; } static path_candidate_t *collect_path_candidates(const char *query, size_t *out_count) { if (out_count) *out_count = 0; if (!query) return NULL; char display_dir[1024]; char fs_dir[1024]; char needle[512]; if (split_path_query(query, display_dir, sizeof(display_dir), fs_dir, sizeof(fs_dir), needle, sizeof(needle)) != 0) { return NULL; } DIR *dir = opendir(fs_dir); if (!dir) return NULL; size_t cap = 32; size_t count = 0; size_t order = 0; path_candidate_t *items = malloc(cap * sizeof(*items)); if (!items) { closedir(dir); return NULL; } struct dirent *entry = NULL; while ((entry = readdir(dir)) != NULL) { const char *name = entry->d_name; if (strcmp(name, ".") == 0) { continue; } if (name[0] == '.' && needle[0] != '.' && strcmp(name, "..") != 0) { continue; } int score = ecex_fuzzy_score(name, needle); if (score < 0) { order++; continue; } char fs_path[2048]; if (strcmp(fs_dir, ".") == 0) { snprintf(fs_path, sizeof(fs_path), "%s", name); } else { size_t len = strlen(fs_dir); snprintf(fs_path, sizeof(fs_path), "%s%s%s", fs_dir, (len > 0 && fs_dir[len - 1] == '/') ? "" : "/", name); } struct stat st; int is_dir = stat(fs_path, &st) == 0 && S_ISDIR(st.st_mode); if (count == cap) { size_t new_cap = cap * 2; path_candidate_t *new_items = realloc(items, new_cap * sizeof(*items)); if (!new_items) break; items = new_items; cap = new_cap; } items[count].path = join_display_path(display_dir, name, is_dir); if (!items[count].path) continue; items[count].score = score + (is_dir ? 20 : 0); items[count].is_dir = is_dir; items[count].order = order; count++; order++; } closedir(dir); if (count == 0) { free(items); return NULL; } qsort(items, count, sizeof(*items), compare_path_candidates); if (out_count) *out_count = count; return items; } typedef ecex_completion_item_t prompt_candidate_t; static void free_prompt_candidates(prompt_candidate_t *items, size_t count) { ecex_completion_items_free(items, count); } static int compare_prompt_candidates(const void *a, const void *b) { return ecex_completion_item_compare(a, b); } static int prompt_kind_uses_path_completion(ecex_prompt_request_t kind) { return kind == ECEX_PROMPT_FIND_FILE || kind == ECEX_PROMPT_WRITE_FILE || kind == ECEX_PROMPT_EVAL_FILE; } static int prompt_kind_uses_buffer_completion(ecex_prompt_request_t kind) { return kind == ECEX_PROMPT_SWITCH_BUFFER || kind == ECEX_PROMPT_KILL_BUFFER || kind == ECEX_PROMPT_FORCE_KILL_BUFFER; } static prompt_candidate_t *collect_buffer_candidates(ecex_t *ed, const char *query, size_t *out_count) { if (out_count) *out_count = 0; if (!ed || !query || ed->buffer_count == 0) return NULL; prompt_candidate_t *items = calloc(ed->buffer_count, sizeof(*items)); if (!items) return NULL; size_t count = 0; for (size_t i = 0; i < ed->buffer_count; i++) { buffer_t *buf = ed->buffers[i]; if (!buf || !buf->name) continue; int score = ecex_fuzzy_score(buf->name, query); if (score < 0) continue; items[count].value = malloc(strlen(buf->name) + 1); if (!items[count].value) continue; strcpy(items[count].value, buf->name); items[count].score = score; items[count].is_dir = 0; items[count].order = i; count++; } if (count == 0) { free(items); return NULL; } qsort(items, count, sizeof(*items), compare_prompt_candidates); if (out_count) *out_count = count; return items; } static prompt_candidate_t *collect_prompt_candidates(app_t *app, const char *query, size_t *out_count) { if (out_count) *out_count = 0; if (!app || !query) return NULL; if (prompt_kind_uses_path_completion(app->prompt_kind)) { size_t path_count = 0; path_candidate_t *paths = collect_path_candidates(query, &path_count); if (!paths || path_count == 0) { free_path_candidates(paths, path_count); return NULL; } prompt_candidate_t *items = calloc(path_count, sizeof(*items)); if (!items) { free_path_candidates(paths, path_count); return NULL; } size_t count = 0; for (size_t i = 0; i < path_count; i++) { items[count].value = malloc(strlen(paths[i].path) + 1); if (!items[count].value) continue; strcpy(items[count].value, paths[i].path); items[count].score = paths[i].score; items[count].is_dir = paths[i].is_dir; items[count].order = paths[i].order; count++; } free_path_candidates(paths, path_count); if (count == 0) { free(items); return NULL; } if (out_count) *out_count = count; return items; } if (prompt_kind_uses_buffer_completion(app->prompt_kind)) { return collect_buffer_candidates(app->ed, query, out_count); } return NULL; } static const char *prompt_candidate_at(app_t *app, const char *query, size_t index, size_t *out_count) { static char candidate[1024]; size_t count = 0; prompt_candidate_t *items = collect_prompt_candidates(app, query, &count); if (!items || count == 0) { free_prompt_candidates(items, count); return NULL; } snprintf(candidate, sizeof(candidate), "%s", items[index % count].value); if (out_count) *out_count = count; free_prompt_candidates(items, count); return candidate; } static int app_prompt_input_is_directory(app_t *app) { if (!app || !prompt_kind_uses_path_completion(app->prompt_kind)) return 0; if (app->prompt_input_len == 0) return 0; char *expanded = ecex_path_expand_user(app->prompt_input); if (!expanded) return 0; struct stat st; int is_dir = stat(expanded, &st) == 0 && S_ISDIR(st.st_mode); free(expanded); return is_dir; } static void app_update_prompt_completion_preview(app_t *app) { if (!app || !app->prompt_completion_active) return; size_t count = 0; prompt_candidate_t *items = collect_prompt_candidates(app, app->prompt_completion_query, &count); if (!items || count == 0) { free_prompt_candidates(items, count); app->prompt_completion_preview_start = 0; app->prompt_completion_preview_count = 0; return; } size_t max_preview = sizeof(app->prompt_completion_preview) / sizeof(app->prompt_completion_preview[0]); size_t start = 0; if (app->prompt_completion_index >= max_preview) { start = app->prompt_completion_index - max_preview + 1; } app->prompt_completion_preview_start = start; app->prompt_completion_preview_count = 0; for (size_t i = 0; i < max_preview && start + i < count; i++) { snprintf(app->prompt_completion_preview[i], sizeof(app->prompt_completion_preview[i]), "%s", items[start + i].value); app->prompt_completion_preview_count++; } free_prompt_candidates(items, count); } static void app_begin_prompt_completion(app_t *app, int direction) { if (!app) return; snprintf(app->prompt_completion_query, sizeof(app->prompt_completion_query), "%s", app->prompt_input); size_t count = 0; prompt_candidate_t *items = collect_prompt_candidates(app, app->prompt_completion_query, &count); if (!items || count == 0) { free_prompt_candidates(items, count); app_reset_prompt_completion(app); app_message(app, prompt_kind_uses_buffer_completion(app->prompt_kind) ? "No buffer completions" : "No file completions"); app->mode = APP_MODE_PROMPT; return; } app->prompt_completion_active = 1; app->prompt_completion_count = count; app->prompt_completion_index = direction < 0 ? count - 1 : 0; app_set_prompt_input(app, items[app->prompt_completion_index].value); free_prompt_candidates(items, count); app_update_prompt_completion_preview(app); } static void app_cycle_prompt_completion(app_t *app, int delta) { if (!app) return; if (!app->prompt_completion_active || app->prompt_completion_count == 0) { app_begin_prompt_completion(app, delta); return; } if (delta < 0) { app->prompt_completion_index = app->prompt_completion_index == 0 ? app->prompt_completion_count - 1 : app->prompt_completion_index - 1; } else { app->prompt_completion_index = (app->prompt_completion_index + 1) % app->prompt_completion_count; } size_t count = 0; const char *candidate = prompt_candidate_at(app, app->prompt_completion_query, app->prompt_completion_index, &count); if (!candidate || count == 0) { app_reset_prompt_completion(app); return; } app->prompt_completion_count = count; app_set_prompt_input(app, candidate); app_update_prompt_completion_preview(app); } static void app_complete_prompt(app_t *app) { if (!app) return; if (app->prompt_completion_active && app_prompt_input_is_directory(app)) { app_reset_prompt_completion(app); app_begin_prompt_completion(app, 1); return; } app_cycle_prompt_completion(app, 1); } static void app_begin_completion(app_t *app, int direction) { if (!app) return; snprintf(app->completion_query, sizeof(app->completion_query), "%s", app->minibuffer); size_t count = 0; command_candidate_t *items = collect_command_candidates(app->ed, app->completion_query, &count); if (!items || count == 0) { free(items); app_reset_completion(app); app_message(app, "No command completions"); app->mode = APP_MODE_MX; return; } app->completion_active = 1; app->completion_count = count; if (direction < 0) { app->completion_index = count - 1; } else { app->completion_index = 0; } app_set_minibuffer(app, items[app->completion_index].name); free(items); } static void app_cycle_completion(app_t *app, int delta) { if (!app) return; if (!app->completion_active || app->completion_count == 0) { app_begin_completion(app, delta); return; } if (delta < 0) { if (app->completion_index == 0) { app->completion_index = app->completion_count - 1; } else { app->completion_index--; } } else { app->completion_index = (app->completion_index + 1) % app->completion_count; } size_t count = 0; const char *candidate = command_candidate_at(app->ed, app->completion_query, app->completion_index, &count); if (!candidate || count == 0) { app_reset_completion(app); return; } app->completion_count = count; app_set_minibuffer(app, candidate); } static void app_enter_mx(app_t *app) { app->mode = APP_MODE_MX; app->minibuffer[0] = '\0'; app->minibuffer_len = 0; app->prefix[0] = '\0'; app->prefix_len = 0; app->message[0] = '\0'; app_reset_completion(app); app->dirty = 1; } static void app_cancel_mx(app_t *app) { app->mode = APP_MODE_EDIT; app->minibuffer[0] = '\0'; app->minibuffer_len = 0; app_reset_completion(app); app_message(app, "Quit"); } static void app_update_isearch(app_t *app) { if (!app || !app->ed) return; buffer_t *buf = ecex_current_buffer(app->ed); if (!buf || app->isearch_len == 0) { app->isearch_has_match = 0; app->dirty = 1; return; } size_t pos = 0; size_t start = app->isearch_backward ? (buf->point > 0 ? buf->point - 1 : buf->len) : buf->point; int ok = app->isearch_backward ? buffer_search_backward(buf, app->isearch_query, start, &pos) : buffer_search_forward(buf, app->isearch_query, start, &pos); if (ok == ECEX_OK) { app->isearch_match = pos; app->isearch_has_match = 1; buffer_set_point(buf, pos); buffer_set_mark(buf, pos + strlen(app->isearch_query)); } else { app->isearch_has_match = 0; } app->dirty = 1; } static void app_enter_isearch(app_t *app, int backward) { if (!app || !app->ed) return; buffer_t *buf = ecex_current_buffer(app->ed); app->mode = APP_MODE_ISEARCH; app->isearch_backward = backward ? 1 : 0; app->isearch_query[0] = '\0'; app->isearch_len = 0; app->isearch_origin = buf ? buf->point : 0; app->isearch_match = app->isearch_origin; app->isearch_has_match = 0; app->prefix[0] = '\0'; app->prefix_len = 0; app->message[0] = '\0'; app->dirty = 1; } static void app_cancel_isearch(app_t *app) { if (!app || !app->ed) return; buffer_t *buf = ecex_current_buffer(app->ed); if (buf) { buffer_set_point(buf, app->isearch_origin); buffer_clear_mark(buf); } app->mode = APP_MODE_EDIT; app_message(app, "Search cancelled"); } static void app_finish_isearch(app_t *app) { if (!app || !app->ed) return; buffer_t *buf = ecex_current_buffer(app->ed); if (app->isearch_len > 0) { snprintf(app->last_search_query, sizeof(app->last_search_query), "%s", app->isearch_query); } if (buf) buffer_clear_mark(buf); app->mode = APP_MODE_EDIT; if (app->isearch_len > 0) app_message(app, app->isearch_has_match ? "Search done" : "Search failed"); app->dirty = 1; } static void app_enter_prompt(app_t *app, ecex_prompt_request_t kind, const char *label) { if (!app) return; app->mode = APP_MODE_PROMPT; app->prompt_kind = kind; snprintf(app->prompt_label, sizeof(app->prompt_label), "%s", label ? label : ""); app->prompt_input[0] = '\0'; app->prompt_input_len = 0; app_reset_prompt_completion(app); app->message[0] = '\0'; app->dirty = 1; } static void app_reload_font_if_needed(app_t *app) { if (!app || !app->ed) return; const char *path = NULL; if (app->ed->theme.font_path && app->ed->theme.font_path[0]) { path = app->ed->theme.font_path; } else if (app->font_path[0]) { path = app->font_path; } else { path = font_choose_path(NULL); } if (!path || !path[0]) return; float size = app->ed->theme.font_size; int same_path = app->font_path[0] && strcmp(app->font_path, path) == 0; int same_size = app->font.size_px == size; if (same_path && same_size) return; font_t new_font; if (font_load(&new_font, path, size) != 0) { char msg[ECEX_MINIBUFFER_SIZE]; snprintf(msg, sizeof(msg), "Failed to reload font: %s", path); app_message(app, msg); return; } font_free(&app->font); app->font = new_font; snprintf(app->font_path, sizeof(app->font_path), "%s", path); app->dirty = 1; } void app_sync_editor_ui(app_t *app) { if (!app || !app->ed) return; const char *path = app->ed->theme.font_path && app->ed->theme.font_path[0] ? app->ed->theme.font_path : app->font_path; int font_path_mismatch = path && path[0] && (!app->font_path[0] || strcmp(app->font_path, path) != 0); int font_size_mismatch = app->font.size_px != app->ed->theme.font_size; if (app->font_revision_seen != app->ed->font_revision || font_path_mismatch || font_size_mismatch) { app_reload_font_if_needed(app); app->font_revision_seen = app->ed->font_revision; } if (app->ui_revision_seen != app->ed->ui_revision) { app->ui_revision_seen = app->ed->ui_revision; app->dirty = 1; } if (app->message_revision_seen != app->ed->message_revision) { app->message_revision_seen = app->ed->message_revision; snprintf(app->message, sizeof(app->message), "%s", app->ed->message); app->dirty = 1; } } static int app_handle_prompt_request(app_t *app) { if (!app || !app->ed) return 0; if (app->ed->prompt_request == ECEX_PROMPT_NONE) { return 0; } ecex_prompt_request_t request = app->ed->prompt_request; char label[sizeof(app->ed->prompt_message)]; snprintf(label, sizeof(label), "%s", app->ed->prompt_message); ecex_clear_prompt_request(app->ed); if (request == ECEX_PROMPT_ISEARCH_FORWARD) { app_enter_isearch(app, 0); } else if (request == ECEX_PROMPT_ISEARCH_BACKWARD) { app_enter_isearch(app, 1); } else { app_enter_prompt(app, request, label); } return 1; } static int app_execute_command(app_t *app, const char *command) { if (!app || !app->ed || !command) return -1; int result = ecex_execute_command(app->ed, command); if (result == 0) { app_sync_editor_ui(app); app_handle_prompt_request(app); } if (app->ed->should_quit && app->window) { glfwSetWindowShouldClose(app->window, GLFW_TRUE); glfwPostEmptyEvent(); } app->dirty = 1; return result; } static void app_cancel_prompt(app_t *app) { if (!app) return; app->mode = APP_MODE_EDIT; app->prompt_kind = ECEX_PROMPT_NONE; app->prompt_label[0] = '\0'; app->prompt_input[0] = '\0'; app->prompt_input_len = 0; app_reset_prompt_completion(app); app_message(app, "Prompt cancelled"); } static const char *app_default_prompt_input(app_t *app, ecex_prompt_request_t kind) { if (!app || !app->ed) return NULL; if (kind == ECEX_PROMPT_SWITCH_BUFFER) { buffer_t *other = ecex_other_buffer(app->ed); return other ? other->name : NULL; } if (kind == ECEX_PROMPT_KILL_BUFFER || kind == ECEX_PROMPT_FORCE_KILL_BUFFER) { buffer_t *current = ecex_current_buffer(app->ed); return current ? current->name : NULL; } return NULL; } static void app_submit_prompt(app_t *app) { if (!app || !app->ed) return; ecex_prompt_request_t kind = app->prompt_kind; const char *default_input = app_default_prompt_input(app, kind); if (app->prompt_input_len == 0 && kind != ECEX_PROMPT_COMPILE && (!default_input || default_input[0] == '\0')) { app_cancel_prompt(app); return; } char input[sizeof(app->prompt_input)]; snprintf(input, sizeof(input), "%s", app->prompt_input_len > 0 ? app->prompt_input : (default_input ? default_input : "")); app->mode = APP_MODE_EDIT; app->prompt_kind = ECEX_PROMPT_NONE; app->prompt_label[0] = '\0'; app->prompt_input[0] = '\0'; app->prompt_input_len = 0; char *expanded_input = ecex_path_expand_user(input); const char *path = expanded_input ? expanded_input : input; int result = -1; const char *verb = "Prompt"; switch (kind) { case ECEX_PROMPT_FIND_FILE: result = ecex_find_file(app->ed, path); verb = "Opened"; break; case ECEX_PROMPT_WRITE_FILE: result = ecex_write_current_buffer(app->ed, path); verb = "Wrote"; break; case ECEX_PROMPT_EVAL_FILE: result = ecex_eval_file(app->ed, path); verb = "Evaluated"; break; case ECEX_PROMPT_SWITCH_BUFFER: result = ecex_switch_buffer(app->ed, input); verb = "Switched"; break; case ECEX_PROMPT_KILL_BUFFER: result = ecex_kill_buffer(app->ed, input); verb = "Killed"; break; case ECEX_PROMPT_FORCE_KILL_BUFFER: result = ecex_kill_buffer_force(app->ed, input); verb = "Force killed"; break; case ECEX_PROMPT_COMPILE: result = ecex_compile(app->ed, input[0] ? input : "make"); verb = "Compiled"; break; case ECEX_PROMPT_GREP: result = ecex_grep(app->ed, input); verb = "Grep"; break; case ECEX_PROMPT_NONE: default: result = -1; break; } if (result == 0) app_sync_editor_ui(app); char msg[ECEX_MINIBUFFER_SIZE]; if (result == 0) { snprintf(msg, sizeof(msg), "%s: %s", verb, path); } else { snprintf(msg, sizeof(msg), "Failed: %s", path); } free(expanded_input); app_reset_prompt_completion(app); app_message(app, msg); } static void app_execute_mx(app_t *app) { if (!app) return; if (app->minibuffer_len == 0) { app_cancel_mx(app); return; } char command[ECEX_MINIBUFFER_SIZE]; snprintf(command, sizeof(command), "%s", app->minibuffer); app->mode = APP_MODE_EDIT; app->minibuffer[0] = '\0'; app->minibuffer_len = 0; app_reset_completion(app); if (app_execute_command(app, command) == 0) { if (app->mode == APP_MODE_EDIT) { char msg[ECEX_MINIBUFFER_SIZE]; snprintf(msg, sizeof(msg), "Executed: %s", command); app_message(app, msg); } } else { char msg[ECEX_MINIBUFFER_SIZE]; snprintf(msg, sizeof(msg), "No such command: %s", command); app_message(app, msg); } if (app->ed->should_quit) { glfwSetWindowShouldClose(app->window, GLFW_TRUE); glfwPostEmptyEvent(); } app->dirty = 1; } static void app_complete_mx(app_t *app) { if (!app) return; if (app->completion_active) { app_cycle_completion(app, 1); } else { app_begin_completion(app, 1); } } static void app_enter_prefix(app_t *app, const char *first_key) { if (!app || !first_key) return; app->mode = APP_MODE_PREFIX; app->minibuffer[0] = '\0'; app->minibuffer_len = 0; app_reset_completion(app); snprintf(app->prefix, sizeof(app->prefix), "%s", first_key); app->prefix_len = strlen(app->prefix); app->message[0] = '\0'; ecex_notify_prefix_hooks(app->ed, app->prefix, ECEX_PREFIX_HOOK_BEGIN); app_sync_editor_ui(app); app->dirty = 1; } static void app_cancel_prefix(app_t *app) { if (!app) return; ecex_notify_prefix_hooks(app->ed, app->prefix, ECEX_PREFIX_HOOK_CANCEL); app->mode = APP_MODE_EDIT; app->prefix[0] = '\0'; app->prefix_len = 0; app_message(app, "Prefix cancelled"); } static int app_append_prefix_key(app_t *app, const char *key_name) { if (!app || !key_name) return -1; size_t key_len = strlen(key_name); if (app->prefix_len + 1 + key_len + 1 >= sizeof(app->prefix)) { app_cancel_prefix(app); return -1; } app->prefix[app->prefix_len++] = ' '; memcpy(app->prefix + app->prefix_len, key_name, key_len + 1); app->prefix_len += key_len; return 0; } static int key_sequence_has_prefix(app_t *app, const char *prefix) { if (!app || !app->ed || !prefix) return 0; return ecex_key_sequence_has_prefix_for_buffer(app->ed, ecex_current_buffer(app->ed), prefix); } static void app_finish_prefix(app_t *app) { if (!app) return; const char *command = ecex_lookup_key_for_buffer(app->ed, ecex_current_buffer(app->ed), app->prefix); if (command) { ecex_notify_prefix_hooks(app->ed, app->prefix, ECEX_PREFIX_HOOK_FINISH); app_sync_editor_ui(app); if (app_execute_command(app, command) == 0) { if (app->mode == APP_MODE_EDIT) { char msg[ECEX_MINIBUFFER_SIZE]; snprintf(msg, sizeof(msg), "Executed: %s", command); app_message(app, msg); } } else { char msg[ECEX_MINIBUFFER_SIZE]; snprintf(msg, sizeof(msg), "Command failed: %s", command); app_message(app, msg); } if (app->ed->should_quit) { glfwSetWindowShouldClose(app->window, GLFW_TRUE); glfwPostEmptyEvent(); } if (app->mode != APP_MODE_PROMPT) { app->mode = APP_MODE_EDIT; } app->prefix[0] = '\0'; app->prefix_len = 0; app->dirty = 1; return; } if (key_sequence_has_prefix(app, app->prefix)) { ecex_notify_prefix_hooks(app->ed, app->prefix, ECEX_PREFIX_HOOK_UPDATE); app_sync_editor_ui(app); app->dirty = 1; return; } char msg[ECEX_MINIBUFFER_SIZE]; snprintf(msg, sizeof(msg), "Undefined key: %s", app->prefix); ecex_notify_prefix_hooks(app->ed, app->prefix, ECEX_PREFIX_HOOK_UNDEFINED); app->mode = APP_MODE_EDIT; app->prefix[0] = '\0'; app->prefix_len = 0; app_message(app, msg); } static float app_mono_cell_width(app_t *app) { float w = text_width(&app->font, "M"); return w > 1.0f ? w : app->font.size_px * 0.6f; } static int app_trace_callbacks_enabled(void) { const char *v = getenv("ECEX_TRACE_CALLBACKS"); return v && v[0] && v[0] != '0'; } static float app_status_height(app_t *app) { float line_h = app->font.line_height > 1.0f ? app->font.line_height : app->font.size_px * 1.2f; float pad_y = app->font.size_px * 0.35f; if (pad_y < 4.0f) pad_y = 4.0f; return line_h + pad_y * 2.0f; } static size_t app_minibuffer_row_count(app_t *app) { if (!app || app->mode != APP_MODE_PREFIX || !app->message[0]) return 1; size_t rows = 1; for (const char *p = app->message; *p; ++p) { if (*p != '\n') continue; rows++; if (rows >= ECEX_MINIBUFFER_MAX_ROWS) return ECEX_MINIBUFFER_MAX_ROWS; } return rows; } static float app_minibuffer_height(app_t *app) { float line_h = app->font.line_height > 1.0f ? app->font.line_height : app->font.size_px * 1.2f; float pad_y = app->font.size_px * 0.35f; if (pad_y < 4.0f) pad_y = 4.0f; return line_h * (float)app_minibuffer_row_count(app) + pad_y * 2.0f; } static int app_minibuffer_visible(app_t *app) { return app->mode == APP_MODE_MX || app->mode == APP_MODE_PREFIX || app->mode == APP_MODE_PROMPT || app->mode == APP_MODE_ISEARCH || app->message[0] != '\0'; } static size_t app_offset_for_line(buffer_t *buf, size_t target_line) { if (!buf || target_line == 0) return 0; size_t line = 0; for (size_t i = 0; i < buf->len; i++) { if (buf->data[i] == '\n') { line++; if (line == target_line) return i + 1; } } return buf->len; } static int app_focus_window_at(app_t *app, double px, double py) { if (!app || !app->ed || app->ed->window_count == 0) return -1; float editor_h = (float)app->height - app_status_height(app); if (app_minibuffer_visible(app)) editor_h -= app_minibuffer_height(app); if (editor_h < 1.0f) editor_h = 1.0f; for (size_t i = 0; i < app->ed->window_count; i++) { ecex_window_t *w = &app->ed->windows[i]; float x = w->x * (float)app->width; float y = w->y * editor_h; float ww = w->w * (float)app->width; float hh = w->h * editor_h; if (px >= x && px < x + ww && py >= y && py < y + hh) { app->ed->current_window_index = i; ecex_sync_current_buffer(app->ed); return (int)i; } } return -1; } static void app_move_point_to_pixel(app_t *app, double px, double py) { int wi = app_focus_window_at(app, px, py); if (wi < 0) return; ecex_window_t *w = &app->ed->windows[wi]; buffer_t *buf = w->buffer; if (!buf) return; float editor_h = (float)app->height - app_status_height(app); if (app_minibuffer_visible(app)) editor_h -= app_minibuffer_height(app); float wx = w->x * (float)app->width; float wy = w->y * editor_h; float pad_x = app->font.size_px * 0.75f; if (pad_x < 8.0f) pad_x = 8.0f; float pad_y = app->font.size_px * 0.35f; if (pad_y < 4.0f) pad_y = 4.0f; size_t row = py > wy + pad_y ? (size_t)((py - wy - pad_y) / app->font.line_height) : 0; size_t col = px > wx + pad_x ? (size_t)((px - wx - pad_x) / app_mono_cell_width(app)) : 0; size_t line = buf->scroll_line + row; size_t start = app_offset_for_line(buf, line); size_t end = buffer_line_end_at(buf, start); buffer_set_point(buf, start + ECEX_MIN(col + buf->scroll_col, end - start)); app->dirty = 1; } static int app_window_rect_at(app_t *app, double px, double py, size_t *out_index, float *out_x, float *out_y, float *out_w, float *out_h) { if (!app || !app->ed || app->ed->window_count == 0) return 0; float editor_h = (float)app->height - app_status_height(app); if (app_minibuffer_visible(app)) editor_h -= app_minibuffer_height(app); if (editor_h < 1.0f) editor_h = 1.0f; for (size_t i = 0; i < app->ed->window_count; i++) { ecex_window_t *w = &app->ed->windows[i]; float x = w->x * (float)app->width; float y = w->y * editor_h; float ww = w->w * (float)app->width; float hh = w->h * editor_h; if (px >= x && px < x + ww && py >= y && py < y + hh) { if (out_index) *out_index = i; if (out_x) *out_x = x; if (out_y) *out_y = y; if (out_w) *out_w = ww; if (out_h) *out_h = hh; return 1; } } return 0; } static int app_page_scroll_rendered_buffer(app_t *app, int direction) { if (!app || !app->ed || direction == 0) return 0; ecex_window_t *win = ecex_current_window(app->ed); buffer_t *buf = win && win->buffer ? win->buffer : ecex_current_buffer(app->ed); if (!buf || !buf->render_fn || !(buf->render_flags & ECEX_RENDER_REPLACE_CONTENT)) { return 0; } size_t step = 1; if (direction > 0) { size_t line_count = buffer_line_count(buf); size_t max_scroll = line_count > 0 ? line_count - 1 : 0; buf->scroll_line = buf->scroll_line + step > max_scroll ? max_scroll : buf->scroll_line + step; } else { buf->scroll_line = buf->scroll_line > step ? buf->scroll_line - step : 0; } app->dirty = 1; return 1; } static int app_dispatch_buffer_mouse(app_t *app, int event, double px, double py, int button) { if (!app || !app->ed) return 0; buffer_t *buf = NULL; float wx = 0.0f; float wy = 0.0f; float ww = 0.0f; float wh = 0.0f; if (app->mouse_capture_active && app->mouse_capture_buffer) { buf = app->mouse_capture_buffer; for (size_t i = 0; i < app->ed->window_count; i++) { if (app->ed->windows[i].buffer != buf) continue; float editor_h = (float)app->height - app_status_height(app); if (app_minibuffer_visible(app)) editor_h -= app_minibuffer_height(app); if (editor_h < 1.0f) editor_h = 1.0f; wx = app->ed->windows[i].x * (float)app->width; wy = app->ed->windows[i].y * editor_h; ww = app->ed->windows[i].w * (float)app->width; wh = app->ed->windows[i].h * editor_h; break; } } else { size_t wi = 0; if (!app_window_rect_at(app, px, py, &wi, &wx, &wy, &ww, &wh)) return 0; if (wi >= app->ed->window_count) return 0; buf = app->ed->windows[wi].buffer; } (void)ww; (void)wh; if (!buf || !buf->mouse_fn) return 0; int local_x = (int)(px - (double)wx); int local_y = (int)(py - (double)wy); if (local_x < -32768) local_x = -32768; if (local_y < -32768) local_y = -32768; if (app_trace_callbacks_enabled()) { ecex_logf("mouse_callback buffer=%p fn=%p event=%d x=%d y=%d button=%d userdata=%p", (void *)buf, (void *)buf->mouse_fn, event, local_x, local_y, button, buf->mouse_userdata); } int result = buf->mouse_fn(app->ed, buf, event, local_x, local_y, button, buf->mouse_userdata); if (result) app->dirty = 1; return result != 0; } static int app_mouse_button_id(int glfw_button) { if (glfw_button == GLFW_MOUSE_BUTTON_LEFT) return ECEX_MOUSE_BUTTON_LEFT; if (glfw_button == GLFW_MOUSE_BUTTON_RIGHT) return ECEX_MOUSE_BUTTON_RIGHT; if (glfw_button == GLFW_MOUSE_BUTTON_MIDDLE) return ECEX_MOUSE_BUTTON_MIDDLE; return glfw_button; } static void mouse_button_callback(GLFWwindow *window, int button, int action, int mods) { (void)mods; app_t *app = glfwGetWindowUserPointer(window); if (!app) return; glfwGetFramebufferSize(window, &app->width, &app->height); double x = 0.0, y = 0.0; glfwGetCursorPos(window, &x, &y); int ebutton = app_mouse_button_id(button); if (action == GLFW_PRESS) { size_t capture_wi = 0; float capture_wx = 0.0f, capture_wy = 0.0f, capture_ww = 0.0f, capture_wh = 0.0f; int have_capture_window = app_window_rect_at(app, x, y, &capture_wi, &capture_wx, &capture_wy, &capture_ww, &capture_wh); if (app_dispatch_buffer_mouse(app, ECEX_MOUSE_PRESS, x, y, ebutton)) { /* Start capture from the window resolved before dispatch. Do not * re-hit-test after the plugin callback; the callback may mark the * app dirty or mutate buffer/window state, and a second lookup can * fail or select a different target. Without capture, GLFW cursor * motion is delivered as plain ECEX_MOUSE_MOVE events, so rendered * widgets never receive ECEX_MOUSE_DRAG. */ if (have_capture_window && capture_wi < app->ed->window_count) { app->mouse_capture_buffer = app->ed->windows[capture_wi].buffer; app->mouse_capture_button = ebutton; app->mouse_capture_active = 1; if (app_trace_callbacks_enabled()) { ecex_logf("mouse_capture_start buffer=%p button=%d", (void *)app->mouse_capture_buffer, ebutton); } } return; } if (button == GLFW_MOUSE_BUTTON_LEFT) app_move_point_to_pixel(app, x, y); } else if (action == GLFW_RELEASE) { app_dispatch_buffer_mouse(app, ECEX_MOUSE_RELEASE, x, y, ebutton); if (!app->mouse_capture_active || app->mouse_capture_button == ebutton) { if (app_trace_callbacks_enabled() && app->mouse_capture_active) { ecex_logf("mouse_capture_end buffer=%p button=%d", (void *)app->mouse_capture_buffer, ebutton); } app->mouse_capture_active = 0; app->mouse_capture_buffer = NULL; app->mouse_capture_button = 0; } } } static void cursor_pos_callback(GLFWwindow *window, double x, double y) { app_t *app = glfwGetWindowUserPointer(window); if (!app) return; glfwGetFramebufferSize(window, &app->width, &app->height); if (app->mouse_capture_active) { app_dispatch_buffer_mouse(app, ECEX_MOUSE_DRAG, x, y, app->mouse_capture_button); } else { app_dispatch_buffer_mouse(app, ECEX_MOUSE_MOVE, x, y, ECEX_MOUSE_BUTTON_LEFT); } } static void scroll_callback(GLFWwindow *window, double xoffset, double yoffset) { (void)xoffset; app_t *app = glfwGetWindowUserPointer(window); if (!app || !app->ed) return; glfwGetFramebufferSize(window, &app->width, &app->height); double x = 0.0; double y = 0.0; glfwGetCursorPos(window, &x, &y); buffer_t *buf = NULL; size_t wi = 0; if (app_window_rect_at(app, x, y, &wi, NULL, NULL, NULL, NULL) && wi < app->ed->window_count) { buf = app->ed->windows[wi].buffer; } if (!buf) { ecex_window_t *win = ecex_current_window(app->ed); buf = win && win->buffer ? win->buffer : ecex_current_buffer(app->ed); } if (!buf) return; if (yoffset < 0.0) buf->scroll_line += 3; else if (yoffset > 0.0) buf->scroll_line = buf->scroll_line > 3 ? buf->scroll_line - 3 : 0; app->dirty = 1; } static void mark_dirty(GLFWwindow *window) { app_t *app = glfwGetWindowUserPointer(window); if (app) { app->dirty = 1; } } static void framebuffer_size_callback(GLFWwindow *window, int width, int height) { (void)width; (void)height; mark_dirty(window); } static void window_refresh_callback(GLFWwindow *window) { mark_dirty(window); } static const char *key_name_for_special(int key) { switch (key) { case GLFW_KEY_F1: return "F1"; case GLFW_KEY_F2: return "F2"; case GLFW_KEY_F3: return "F3"; case GLFW_KEY_F4: return "F4"; case GLFW_KEY_F5: return "F5"; case GLFW_KEY_LEFT: return "LEFT"; case GLFW_KEY_RIGHT: return "RIGHT"; case GLFW_KEY_UP: return "UP"; case GLFW_KEY_DOWN: return "DOWN"; case GLFW_KEY_ENTER: return "ENTER"; case GLFW_KEY_BACKSPACE: return "BACKSPACE"; case GLFW_KEY_DELETE: return "DELETE"; case GLFW_KEY_TAB: return "TAB"; case GLFW_KEY_SPACE: return "SPC"; case GLFW_KEY_ESCAPE: return "ESC"; case GLFW_KEY_HOME: return "HOME"; case GLFW_KEY_END: return "END"; case GLFW_KEY_PAGE_UP: return "PAGEUP"; case GLFW_KEY_PAGE_DOWN: return "PAGEDOWN"; default: return NULL; } } static int layout_key_name(int key, int scancode, char *out, size_t out_size) { if (!out || out_size == 0) return -1; out[0] = '\0'; const char *special = key_name_for_special(key); if (special) { snprintf(out, out_size, "%s", special); return 0; } const char *name = glfwGetKeyName(key, scancode); if (!name || !name[0]) return -1; if (name[1] != '\0') return -1; char c = name[0]; if (c >= 'A' && c <= 'Z') { c = (char)(c - 'A' + 'a'); } out[0] = c; out[1] = '\0'; return 0; } static int key_event_to_string(int key, int scancode, int mods, char *out, size_t out_size) { if (!out || out_size == 0) return -1; out[0] = '\0'; char base[32]; if (layout_key_name(key, scancode, base, sizeof(base)) != 0) { return -1; } char prefix[32]; prefix[0] = '\0'; if (mods & GLFW_MOD_CONTROL) { strncat(prefix, "C-", sizeof(prefix) - strlen(prefix) - 1); } if (mods & GLFW_MOD_ALT) { strncat(prefix, "M-", sizeof(prefix) - strlen(prefix) - 1); } if (mods & GLFW_MOD_SHIFT) { if (base[0] >= 'a' && base[0] <= 'z' && base[1] == '\0') { if (mods & (GLFW_MOD_CONTROL | GLFW_MOD_ALT | GLFW_MOD_SUPER)) { strncat(prefix, "S-", sizeof(prefix) - strlen(prefix) - 1); } else { base[0] = (char)(base[0] - 'a' + 'A'); } } else if (base[1] == '\0') { switch (base[0]) { case '`': base[0] = '~'; break; case '1': base[0] = '!'; break; case '2': base[0] = '@'; break; case '3': base[0] = '#'; break; case '4': base[0] = '$'; break; case '5': base[0] = '%'; break; case '6': base[0] = '^'; break; case '7': base[0] = '&'; break; case '8': base[0] = '*'; break; case '9': base[0] = '('; break; case '0': base[0] = ')'; break; case '-': base[0] = '_'; break; case '=': base[0] = '+'; break; case '[': base[0] = '{'; break; case ']': base[0] = '}'; break; case '\\': base[0] = '|'; break; case ';': base[0] = ':'; break; case '\'': base[0] = '"'; break; case ',': base[0] = '<'; break; case '.': base[0] = '>'; break; case '/': base[0] = '?'; break; default: strncat(prefix, "S-", sizeof(prefix) - strlen(prefix) - 1); break; } } else { strncat(prefix, "S-", sizeof(prefix) - strlen(prefix) - 1); } } if (mods & GLFW_MOD_SUPER) { strncat(prefix, "Super-", sizeof(prefix) - strlen(prefix) - 1); } snprintf(out, out_size, "%s%s", prefix, base); return 0; } static int is_layout_char_key(int key, int scancode, char wanted) { char name[32]; if (layout_key_name(key, scancode, name, sizeof(name)) != 0) { return 0; } if (wanted >= 'A' && wanted <= 'Z') { wanted = (char)(wanted - 'A' + 'a'); } return name[0] == wanted && name[1] == '\0'; } static int key_produces_text(int key, int scancode) { if (key_name_for_special(key)) { return 0; } const char *name = glfwGetKeyName(key, scancode); return name && name[0] && name[1] == '\0'; } static int is_modifier_key(int key) { return key == GLFW_KEY_LEFT_SHIFT || key == GLFW_KEY_RIGHT_SHIFT || key == GLFW_KEY_LEFT_CONTROL || key == GLFW_KEY_RIGHT_CONTROL || key == GLFW_KEY_LEFT_ALT || key == GLFW_KEY_RIGHT_ALT || key == GLFW_KEY_LEFT_SUPER || key == GLFW_KEY_RIGHT_SUPER || key == GLFW_KEY_CAPS_LOCK || key == GLFW_KEY_NUM_LOCK; } static void try_execute_keybind(app_t *app, const char *key_name) { const char *command = ecex_lookup_key_for_buffer(app->ed, ecex_current_buffer(app->ed), key_name); if (!command) return; if (app_execute_command(app, command) == 0) { app->dirty = 1; } else { char msg[ECEX_MINIBUFFER_SIZE]; snprintf(msg, sizeof(msg), "Bad keybind: %s -> %s", key_name, command); app_message(app, msg); } if (app->ed->should_quit) { glfwSetWindowShouldClose(app->window, GLFW_TRUE); glfwPostEmptyEvent(); } } static void char_callback(GLFWwindow *window, unsigned int codepoint) { app_t *app = glfwGetWindowUserPointer(window); if (!app || !app->ed) return; if (app->suppress_next_char) { app->suppress_next_char = 0; return; } if (app->mode == APP_MODE_EDIT) { if ((app->current_mods & GLFW_MOD_ALT) && codepoint == 'x') { app_enter_mx(app); return; } } if (app->mode == APP_MODE_MX) { if (codepoint >= 32 && codepoint <= 126) { if (app->minibuffer_len + 1 < sizeof(app->minibuffer)) { app_reset_completion(app); app->minibuffer[app->minibuffer_len++] = (char)codepoint; app->minibuffer[app->minibuffer_len] = '\0'; app->dirty = 1; } } return; } if (app->mode == APP_MODE_ISEARCH) { if (codepoint >= 32 && codepoint <= 126) { if (app->isearch_len + 1 < sizeof(app->isearch_query)) { app->isearch_query[app->isearch_len++] = (char)codepoint; app->isearch_query[app->isearch_len] = '\0'; app_update_isearch(app); } } return; } if (app->mode == APP_MODE_PROMPT) { if (codepoint >= 32 && codepoint <= 126) { if (app->prompt_input_len + 1 < sizeof(app->prompt_input)) { app_reset_prompt_completion(app); app->prompt_input[app->prompt_input_len++] = (char)codepoint; app->prompt_input[app->prompt_input_len] = '\0'; app->dirty = 1; } } return; } if (app->mode == APP_MODE_PREFIX) { return; } buffer_t *buf = ecex_current_buffer(app->ed); if (!buf) return; if (buffer_is_interactive(buf)) { return; } if (codepoint >= 32 && codepoint <= 126) { buffer_insert_char(buf, (char)codepoint); app->message[0] = '\0'; app->dirty = 1; } } static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) { if (action != GLFW_PRESS && action != GLFW_REPEAT) { return; } app_t *app = glfwGetWindowUserPointer(window); if (!app || !app->ed) return; app->current_mods = mods; if (is_modifier_key(key)) { return; } if ((mods & GLFW_MOD_CONTROL) && !(mods & (GLFW_MOD_ALT | GLFW_MOD_SHIFT | GLFW_MOD_SUPER)) && is_layout_char_key(key, scancode, 'g')) { if (app->mode == APP_MODE_MX) { app_cancel_mx(app); } else if (app->mode == APP_MODE_PROMPT) { app_cancel_prompt(app); } else if (app->mode == APP_MODE_ISEARCH) { app_cancel_isearch(app); } else if (app->mode == APP_MODE_PREFIX) { app_cancel_prefix(app); } else { buffer_t *buf = ecex_current_buffer(app->ed); if (buf) buffer_clear_mark(buf); app_message(app, "Quit"); } if (key_produces_text(key, scancode)) { app->suppress_next_char = 1; } return; } if (app->mode == APP_MODE_EDIT && (mods & GLFW_MOD_ALT) && !(mods & (GLFW_MOD_CONTROL | GLFW_MOD_SHIFT | GLFW_MOD_SUPER)) && is_layout_char_key(key, scancode, 'x')) { if (key_produces_text(key, scancode)) { app->suppress_next_char = 1; } app_enter_mx(app); return; } if (app->mode == APP_MODE_EDIT && (mods & GLFW_MOD_ALT) && !(mods & (GLFW_MOD_CONTROL | GLFW_MOD_SHIFT | GLFW_MOD_SUPER)) && is_layout_char_key(key, scancode, 'g')) { app_enter_prefix(app, "M-g"); return; } if (app->mode == APP_MODE_EDIT && (mods & GLFW_MOD_CONTROL) && !(mods & (GLFW_MOD_ALT | GLFW_MOD_SHIFT | GLFW_MOD_SUPER)) && is_layout_char_key(key, scancode, 'x')) { app_enter_prefix(app, "C-x"); return; } if (app->mode == APP_MODE_MX) { switch (key) { case GLFW_KEY_ESCAPE: app_cancel_mx(app); return; case GLFW_KEY_ENTER: app_execute_mx(app); return; case GLFW_KEY_TAB: app_complete_mx(app); return; case GLFW_KEY_DOWN: app_cycle_completion(app, 1); return; case GLFW_KEY_UP: app_cycle_completion(app, -1); return; case GLFW_KEY_BACKSPACE: if (app->minibuffer_len > 0) { app_reset_completion(app); app->minibuffer_len--; app->minibuffer[app->minibuffer_len] = '\0'; app->dirty = 1; } return; default: return; } } if (app->mode == APP_MODE_ISEARCH) { switch (key) { case GLFW_KEY_ESCAPE: app_cancel_isearch(app); return; case GLFW_KEY_ENTER: app_finish_isearch(app); return; case GLFW_KEY_BACKSPACE: if (app->isearch_len > 0) { app->isearch_len--; app->isearch_query[app->isearch_len] = '\0'; buffer_t *buf = ecex_current_buffer(app->ed); if (buf) { buffer_set_point(buf, app->isearch_origin); buffer_clear_mark(buf); } app_update_isearch(app); } return; default: if ((mods & GLFW_MOD_CONTROL) && is_layout_char_key(key, scancode, 's')) { app->isearch_backward = 0; app_update_isearch(app); return; } if ((mods & GLFW_MOD_CONTROL) && is_layout_char_key(key, scancode, 'r')) { app->isearch_backward = 1; app_update_isearch(app); return; } return; } } if (app->mode == APP_MODE_PROMPT) { switch (key) { case GLFW_KEY_ESCAPE: app_cancel_prompt(app); return; case GLFW_KEY_ENTER: app_submit_prompt(app); return; case GLFW_KEY_TAB: app_complete_prompt(app); return; case GLFW_KEY_DOWN: app_cycle_prompt_completion(app, 1); return; case GLFW_KEY_UP: app_cycle_prompt_completion(app, -1); return; case GLFW_KEY_BACKSPACE: if (app->prompt_input_len > 0) { app_reset_prompt_completion(app); app->prompt_input_len--; app->prompt_input[app->prompt_input_len] = '\0'; app->dirty = 1; } return; default: return; } } if (app->mode == APP_MODE_PREFIX) { if (key == GLFW_KEY_ESCAPE) { app_cancel_prefix(app); return; } char key_name[64]; if (key_event_to_string(key, scancode, mods, key_name, sizeof(key_name)) == 0) { if (key_produces_text(key, scancode)) { app->suppress_next_char = 1; } if (app_append_prefix_key(app, key_name) == 0) { app_finish_prefix(app); } return; } app_cancel_prefix(app); return; } char key_name[64]; if (key_event_to_string(key, scancode, mods, key_name, sizeof(key_name)) == 0) { const char *command = ecex_lookup_key_for_buffer(app->ed, ecex_current_buffer(app->ed), key_name); if (command) { if (key_produces_text(key, scancode) && !(mods & GLFW_MOD_CONTROL) && !(mods & GLFW_MOD_SUPER)) { app->suppress_next_char = 1; } try_execute_keybind(app, key_name); return; } if (key_sequence_has_prefix(app, key_name)) { if (key_produces_text(key, scancode)) { app->suppress_next_char = 1; } app_enter_prefix(app, key_name); return; } } if (key == GLFW_KEY_PAGE_DOWN) { if (app_page_scroll_rendered_buffer(app, 1)) return; } if (key == GLFW_KEY_PAGE_UP) { if (app_page_scroll_rendered_buffer(app, -1)) return; } if (key == GLFW_KEY_ESCAPE) return; } void app_init(app_t *app, ecex_t *ed) { if (!app) return; memset(app, 0, sizeof(*app)); app->ed = ed; app->dirty = 1; app->mode = APP_MODE_EDIT; app_reset_completion(app); app_reset_prompt_completion(app); } void app_set_window(app_t *app, GLFWwindow *window) { if (!app) return; app->window = window; if (app->ed) { ecex_set_clipboard_callbacks(app->ed, window ? app_clipboard_get : NULL, window ? app_clipboard_set : NULL, app); } if (window) { glfwSetWindowUserPointer(window, app); } } void app_install_callbacks(app_t *app) { if (!app || !app->window) return; glfwSetCharCallback(app->window, char_callback); glfwSetKeyCallback(app->window, key_callback); glfwSetFramebufferSizeCallback(app->window, framebuffer_size_callback); glfwSetWindowRefreshCallback(app->window, window_refresh_callback); glfwSetMouseButtonCallback(app->window, mouse_button_callback); glfwSetCursorPosCallback(app->window, cursor_pos_callback); glfwSetScrollCallback(app->window, scroll_callback); }