#include "input.h" #include #define KEY_BUF_LEN 64 static void update_selection_on_nav(Editor *ed, int shift, int old_x, int old_y) { if (shift) { if (!ed->has_sel) { ed->has_sel = 1; ed->sel_ax = old_x; ed->sel_ay = old_y; } else if (ed->sel_ax == ed->cx && ed->sel_ay == ed->cy) { ed->has_sel = 0; } } else { ed->has_sel = 0; } } static void handle_search_key(Editor *ed, KeySym ks, int ctrl, int shift, const char *buf, int n) { if (ks == XK_Escape || (ctrl && (ks == XK_f || ks == XK_F || ks == XK_Cyrillic_a || ks == XK_Cyrillic_A))) { ed->search_mode = 0; return; } if (ks == XK_Return || ks == XK_KP_Enter || ks == XK_F3) { if (shift) search_find_prev(ed); else search_find_next(ed, 0); return; } if (ks == XK_BackSpace) { int qlen = (int)strlen(ed->search_query); if (qlen > 0) { int prev = utf8_prev_char_pos(ed->search_query, qlen); ed->search_query[prev] = '\0'; search_find_next(ed, 1); } return; } if (n > 0 && !ctrl && (unsigned char)(*buf) >= 32 && (unsigned char)(*buf) != 127) { int qlen = (int)strlen(ed->search_query); if (qlen + n < SEARCH_LEN - 1) { memcpy(&ed->search_query[qlen], buf, (size_t)n); ed->search_query[qlen + n] = '\0'; search_find_next(ed, 1); } } } static int handle_ctrl_shortcuts(Editor *ed, KeySym ks, int shift) { if (ks == XK_Tab || ks == XK_ISO_Left_Tab) { tabs_next(ed, shift || ks == XK_ISO_Left_Tab); return 1; } if (ks == XK_n || ks == XK_N || ks == XK_Cyrillic_te || ks == XK_Cyrillic_TE) { tabs_new(ed, NULL); return 1; } if (ks == XK_o || ks == XK_O || ks == XK_Cyrillic_shcha || ks == XK_Cyrillic_SHCHA) { if (shift) editor_open_system_dialog(ed); else fbrowser_show(ed); return 1; } if (ks == XK_w || ks == XK_W || ks == XK_Cyrillic_tse || ks == XK_Cyrillic_TSE) { tabs_request_close(ed, ed->active_tab); return 1; } if (ks == XK_q || ks == XK_Q || ks == XK_Cyrillic_shorti || ks == XK_Cyrillic_SHORTI) { tabs_request_quit(ed); return 1; } if (ks == XK_s || ks == XK_S || ks == XK_Cyrillic_yeru || ks == XK_Cyrillic_YERU) { editor_save(ed); return 1; } if (ks == XK_f || ks == XK_F || ks == XK_Cyrillic_a || ks == XK_Cyrillic_A) { ed->status_collapsed = 0; ed->search_mode = 1; if (ed->has_sel) { int sy, sx, ey, ex; editor_get_sel_bounds(ed, &sy, &sx, &ey, &ex); if (sy == ey && ex > sx) { int clen = ex - sx; if (clen >= SEARCH_LEN) clen = SEARCH_LEN - 1; memcpy(ed->search_query, &ed->lines[sy][sx], (size_t)clen); ed->search_query[clen] = '\0'; } } return 1; } if (ks == XK_z || ks == XK_Z || ks == XK_Cyrillic_ya || ks == XK_Cyrillic_YA) { if (shift) history_redo(ed); else history_undo(ed); return 1; } if (ks == XK_y || ks == XK_Y || ks == XK_Cyrillic_en || ks == XK_Cyrillic_EN) { history_redo(ed); return 1; } if (ks == XK_a || ks == XK_A || ks == XK_Cyrillic_ef || ks == XK_Cyrillic_EF) { ed->has_sel = 1; ed->sel_ax = 0; ed->sel_ay = 0; ed->cy = ed->num_lines - 1; ed->cx = (int)strlen(ed->lines[ed->cy]); return 1; } if (ks == XK_c || ks == XK_C || ks == XK_Cyrillic_es || ks == XK_Cyrillic_ES) { clipboard_copy(ed); return 1; } if (ks == XK_x || ks == XK_X || ks == XK_Cyrillic_che || ks == XK_Cyrillic_CHE) { if (ed->has_sel) { clipboard_copy(ed); history_push_undo(ed); editor_delete_selection(ed); editor_ensure_cursor_visible(ed); } return 1; } if (ks == XK_v || ks == XK_V || ks == XK_Cyrillic_em || ks == XK_Cyrillic_EM) { clipboard_request_paste(ed); return 1; } return 0; } void editor_handle_key(Editor *ed, XKeyEvent *kev) { char buf[KEY_BUF_LEN]; memset(buf, 0, sizeof(buf)); KeySym ks = NoSymbol; Status status; int n = ed->xic ? Xutf8LookupString(ed->xic, kev, buf, sizeof(buf) - 1, &ks, &status) : XLookupString(kev, buf, sizeof(buf) - 1, &ks, NULL); int ctrl = (kev->state & ControlMask); int shift = (kev->state & ShiftMask); int alt = (kev->state & Mod1Mask); if (ed->confirm_mode != 0) { confirm_handle_key(ed, ks); return; } if (ed->fbrowser_open) { fbrowser_handle_key(ed, ks, ctrl, buf, n); return; } if (ed->menu_open >= 0) { if (ks == XK_Escape) menu_init(ed); return; } if (ed->search_mode) { handle_search_key(ed, ks, ctrl, shift, buf, n); return; } if (ctrl && handle_ctrl_shortcuts(ed, ks, shift)) return; if (alt) { if (ks == XK_Left) { nav_go_back(ed); return; } if (ks == XK_Right) { nav_go_forward(ed); return; } } if (ks == XK_F3) { if (shift) search_find_prev(ed); else search_find_next(ed, 0); return; } int old_x = ed->cx, old_y = ed->cy; if (ks == XK_Up) { if (ed->cy > 0) { int col = utf8_char_count(ed->lines[ed->cy], ed->cx); ed->cy--; ed->cx = utf8_byte_offset(ed->lines[ed->cy], col); } update_selection_on_nav(ed, shift, old_x, old_y); } else if (ks == XK_Down) { if (ed->cy < ed->num_lines - 1) { int col = utf8_char_count(ed->lines[ed->cy], ed->cx); ed->cy++; ed->cx = utf8_byte_offset(ed->lines[ed->cy], col); } update_selection_on_nav(ed, shift, old_x, old_y); } else if (ks == XK_Left) { if (ed->cx > 0) ed->cx = utf8_prev_char_pos(ed->lines[ed->cy], ed->cx); else if (ed->cy > 0) { ed->cy--; ed->cx = (int)strlen(ed->lines[ed->cy]); } update_selection_on_nav(ed, shift, old_x, old_y); } else if (ks == XK_Right) { int len = (int)strlen(ed->lines[ed->cy]); if (ed->cx < len) ed->cx = utf8_next_char_pos(ed->lines[ed->cy], ed->cx); else if (ed->cy < ed->num_lines - 1) { ed->cy++; ed->cx = 0; } update_selection_on_nav(ed, shift, old_x, old_y); } else if (ks == XK_Home) { ed->cx = 0; update_selection_on_nav(ed, shift, old_x, old_y); } else if (ks == XK_End) { ed->cx = (int)strlen(ed->lines[ed->cy]); update_selection_on_nav(ed, shift, old_x, old_y); } else if (ks == XK_Return || ks == XK_KP_Enter) { history_push_undo(ed); if (ed->has_sel) editor_delete_selection(ed); input_insert_newline(ed); } else if (ks == XK_BackSpace) { if (ed->has_sel) { history_push_undo(ed); editor_delete_selection(ed); } else { int len = (int)strlen(ed->lines[ed->cy]); if (ed->cx > 0) { history_push_undo(ed); int prev = utf8_prev_char_pos(ed->lines[ed->cy], ed->cx); memmove(&ed->lines[ed->cy][prev], &ed->lines[ed->cy][ed->cx], (size_t)(len - ed->cx + 1)); ed->cx = prev; } else if (ed->cy > 0) { int prev_len = (int)strlen(ed->lines[ed->cy - 1]); if (prev_len + len < MAX_LEN - 1) { history_push_undo(ed); strcat(ed->lines[ed->cy - 1], ed->lines[ed->cy]); for (int i = ed->cy; i < ed->num_lines - 1; i++) strcpy(ed->lines[i], ed->lines[i + 1]); ed->num_lines--; ed->cy--; ed->cx = prev_len; } } } } else if (ks == XK_Delete) { if (ed->has_sel) { history_push_undo(ed); editor_delete_selection(ed); } else { int len = (int)strlen(ed->lines[ed->cy]); if (ed->cx < len) { history_push_undo(ed); int next = utf8_next_char_pos(ed->lines[ed->cy], ed->cx); memmove(&ed->lines[ed->cy][ed->cx], &ed->lines[ed->cy][next], (size_t)(len - next + 1)); } else if (ed->cy < ed->num_lines - 1) { int next_len = (int)strlen(ed->lines[ed->cy + 1]); if (len + next_len < MAX_LEN - 1) { history_push_undo(ed); strcat(ed->lines[ed->cy], ed->lines[ed->cy + 1]); for (int i = ed->cy + 1; i < ed->num_lines - 1; i++) strcpy(ed->lines[i], ed->lines[i + 1]); ed->num_lines--; } } } } else if (ks == XK_Tab || ks == XK_ISO_Left_Tab) { int unindent = (shift || ks == XK_ISO_Left_Tab); history_push_undo(ed); if (ed->has_sel || unindent) { input_indent_selection(ed, unindent); } else { input_insert_bytes(ed, " ", 4); } } else if (n > 0 && !ctrl && (unsigned char)(*buf) >= 32 && (unsigned char)(*buf) != 127) { history_push_undo(ed); if (ed->has_sel) editor_delete_selection(ed); input_insert_bytes(ed, buf, n); } editor_ensure_cursor_visible(ed); }