#include "app.h" #include "common.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; } static char *app_expand_user_path(const char *path) { if (!path) return NULL; if (path[0] != '~' || (path[1] != '\0' && path[1] != '/')) { char *copy = malloc(strlen(path) + 1); if (!copy) return NULL; strcpy(copy, path); return copy; } const char *home = getenv("HOME"); if (!home || !home[0]) { char *copy = malloc(strlen(path) + 1); if (!copy) return NULL; strcpy(copy, path); return copy; } size_t home_len = strlen(home); size_t rest_len = strlen(path + 1); char *expanded = malloc(home_len + rest_len + 1); if (!expanded) return NULL; memcpy(expanded, home, home_len); memcpy(expanded + home_len, path + 1, rest_len + 1); return expanded; } typedef struct command_candidate { const char *name; int score; size_t order; } command_candidate_t; static int ascii_lower(int c) { if (c >= 'A' && c <= 'Z') return c - 'A' + 'a'; return c; } static int ascii_strncasecmp_local(const char *a, const char *b, size_t n) { for (size_t i = 0; i < n; i++) { int ac = ascii_lower((unsigned char)a[i]); int bc = ascii_lower((unsigned char)b[i]); if (ac != bc || ac == '\0' || bc == '\0') { return ac - bc; } } return 0; } static int ascii_contains_ci(const char *haystack, const char *needle) { if (!haystack || !needle) return 0; if (needle[0] == '\0') return 1; size_t needle_len = strlen(needle); for (size_t i = 0; haystack[i]; i++) { if (ascii_strncasecmp_local(haystack + i, needle, needle_len) == 0) { return 1; } } return 0; } static int command_fuzzy_score(const char *candidate, const char *query) { if (!candidate || !query) return -1; if (query[0] == '\0') { return 0; } int score = 0; int consecutive = 0; int last_match = -1; size_t ci = 0; size_t qi = 0; while (candidate[ci] && query[qi]) { char c = candidate[ci]; char q = query[qi]; if (c >= 'A' && c <= 'Z') c = (char)(c - 'A' + 'a'); if (q >= 'A' && q <= 'Z') q = (char)(q - 'A' + 'a'); if (c == q) { score += 10; if ((int)ci == last_match + 1) { consecutive++; score += 5 * consecutive; } else { consecutive = 0; } if (ci == 0) { score += 20; } if (ci > 0 && (candidate[ci - 1] == '-' || candidate[ci - 1] == '_' || candidate[ci - 1] == ' ')) { score += 15; } last_match = (int)ci; qi++; } ci++; } if (query[qi] != '\0') { return -1; } score -= (int)strlen(candidate); if (strncmp(candidate, query, strlen(query)) == 0) { score += 100; } if (ascii_contains_ci(candidate, query)) { score += 75; } return score; } 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 = command_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->score != pb->score) { return pb->score - pa->score; } if (pa->is_dir != pb->is_dir) { return pb->is_dir - pa->is_dir; } 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 = app_expand_user_path(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 = command_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; } static const char *path_candidate_at(const char *query, size_t index, size_t *out_count) { static char candidate[1024]; size_t count = 0; path_candidate_t *items = collect_path_candidates(query, &count); if (!items || count == 0) { free_path_candidates(items, count); return NULL; } snprintf(candidate, sizeof(candidate), "%s", items[index % count].path); if (out_count) { *out_count = count; } free_path_candidates(items, count); return candidate; } static void app_update_prompt_completion_preview(app_t *app) { if (!app || !app->prompt_completion_active) return; size_t count = 0; path_candidate_t *items = collect_path_candidates(app->prompt_completion_query, &count); if (!items || count == 0) { free_path_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].path); app->prompt_completion_preview_count++; } free_path_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; path_candidate_t *items = collect_path_candidates(app->prompt_completion_query, &count); if (!items || count == 0) { free_path_candidates(items, count); app_reset_prompt_completion(app); app_message(app, "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].path); free_path_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 = path_candidate_at(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_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; } 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_reload_font_if_needed(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 void app_submit_prompt(app_t *app) { if (!app || !app->ed) return; if (app->prompt_input_len == 0 && app->prompt_kind != ECEX_PROMPT_COMPILE) { app_cancel_prompt(app); return; } char input[sizeof(app->prompt_input)]; snprintf(input, sizeof(input), "%s", app->prompt_input); ecex_prompt_request_t kind = app->prompt_kind; 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 = app_expand_user_path(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_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_reload_font_if_needed(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'; app->dirty = 1; } static void app_cancel_prefix(app_t *app) { if (!app) return; 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) { 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)) { app->dirty = 1; return; } char msg[ECEX_MINIBUFFER_SIZE]; snprintf(msg, sizeof(msg), "Undefined key: %s", app->prefix); 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 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 float app_minibuffer_height(app_t *app) { return app_status_height(app); } 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 void mouse_button_callback(GLFWwindow *window, int button, int action, int mods) { (void)mods; if (button != GLFW_MOUSE_BUTTON_LEFT || action != GLFW_PRESS) return; 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); app_move_point_to_pixel(app, x, y); } static void scroll_callback(GLFWwindow *window, double xoffset, double yoffset) { (void)xoffset; app_t *app = glfwGetWindowUserPointer(window); if (!app || !app->ed) return; buffer_t *buf = 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_SUPER) { strncat(prefix, "S-", 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 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->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 (app->mode == APP_MODE_EDIT && key == GLFW_KEY_F1) { app_enter_mx(app); return; } if (app->mode == APP_MODE_EDIT && (mods & GLFW_MOD_ALT) && is_layout_char_key(key, scancode, 'g')) { app_enter_prefix(app, "M-g"); return; } if (app->mode == APP_MODE_EDIT && (mods & GLFW_MOD_CONTROL) && 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_cycle_prompt_completion(app, 1); 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 == GLFW_KEY_ESCAPE) { glfwSetWindowShouldClose(window, GLFW_TRUE); glfwPostEmptyEvent(); 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); glfwSetScrollCallback(app->window, scroll_callback); }