Initial commit: X11 C code editor

This commit is contained in:
user committed 2026-11-30 22:12:00 +03:00
commit 8eae6763f6
21 files changed
+36389

No files matched your search

+10
View File
@@ -0,0 +1,10 @@
#ifndef INPUT_H
#define INPUT_H
#include "../editor.h"
void input_insert_bytes(Editor *ed, const char *bytes, int count);
void input_insert_newline(Editor *ed);
void input_indent_selection(Editor *ed, int unindent);
#endif
+276
View File
@@ -0,0 +1,276 @@
#include "input.h"
#include <string.h>
#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);
}
+276
View File
@@ -0,0 +1,276 @@
#include "input.h"
/* Возвращает: 0 - нет, 1 - статус-бар, 2 - вверх, 3 - вниз, 4 - влево, 5 - вправо */
static int get_hovered_ui_button(const Editor *ed, int mx, int my) {
if (ed->menu_open >= 0 || ed->confirm_mode != 0 || ed->fbrowser_open) return 0;
int st_h = editor_status_h(ed);
int v_track_x = ed->win_w - SCROLLBAR_W;
int v_full_y = TOP_UI_H;
int v_full_h = ed->win_h - TOP_UI_H - st_h - SCROLLBAR_W;
int h_track_y = ed->win_h - st_h - SCROLLBAR_W;
int h_full_x = GUTTER_W;
int h_full_w = ed->win_w - GUTTER_W - SCROLLBAR_W;
if (mx >= 0 && mx < GUTTER_W && my >= h_track_y && my < h_track_y + SCROLLBAR_W) {
return 1;
}
if (v_full_h >= SCROLLBAR_W * 2 && mx >= v_track_x && mx < ed->win_w) {
if (my >= v_full_y && my < v_full_y + SCROLLBAR_W) return 2;
if (my >= v_full_y + v_full_h - SCROLLBAR_W && my < v_full_y + v_full_h) return 3;
}
if (h_full_w >= SCROLLBAR_W * 2 && my >= h_track_y && my < h_track_y + SCROLLBAR_W) {
if (mx >= h_full_x && mx < h_full_x + SCROLLBAR_W) return 4;
if (mx >= v_track_x - SCROLLBAR_W && mx < v_track_x) return 5;
}
return 0;
}
int mouse_update_cursor(Editor *ed, int mx, int my, unsigned int state) {
(void)state;
int old_btn = ed->hover_btn;
ed->hover_btn = get_hovered_ui_button(ed, mx, my);
int want_hand = (ed->hover_sym_line >= 0) || (ed->hover_btn > 0) ||
(ed->hover_tab != -1) ||
(ed->confirm_mode != 0 && ed->confirm_hover_btn >= 0) ||
(ed->fbrowser_open && ed->fb_hover_btn >= 0);
if (want_hand && !ed->cursor_is_hand) {
XDefineCursor(ed->dpy, ed->win, ed->cursor_hand);
ed->cursor_is_hand = 1;
} else if (!want_hand && ed->cursor_is_hand) {
XUndefineCursor(ed->dpy, ed->win);
ed->cursor_is_hand = 0;
}
return (old_btn != ed->hover_btn);
}
static void set_cursor_from_mouse(Editor *ed, int mx, int my) {
int target_y = ed->scroll_y + ((my - TOP_UI_H) / ed->line_h);
if (target_y >= ed->num_lines) target_y = ed->num_lines - 1;
if (target_y < 0) target_y = 0;
ed->cy = target_y;
int target_col = ed->scroll_x + ((mx - GUTTER_W - 8 + ed->char_w / 2) / ed->char_w);
if (target_col < 0) target_col = 0;
ed->cx = utf8_byte_offset(ed->lines[ed->cy], target_col);
}
static void update_vscroll_from_mouse(Editor *ed, int my) {
int v_full_h = ed->win_h - TOP_UI_H - editor_status_h(ed) - SCROLLBAR_W;
int visible_lines = v_full_h / ed->line_h;
int max_sy = ed->num_lines - visible_lines;
if (max_sy <= 0) return;
int v_track_y = TOP_UI_H + SCROLLBAR_W;
int v_track_h = v_full_h - SCROLLBAR_W * 2;
if (v_track_h <= 0) return;
int thumb_h = (v_track_h * visible_lines) / ed->num_lines;
if (thumb_h < 16) thumb_h = 16;
int usable = v_track_h - thumb_h;
if (usable <= 0) return;
int pos = (my - v_track_y) - ed->drag_offset;
if (pos < 0) pos = 0;
if (pos > usable) pos = usable;
ed->scroll_y = (pos * max_sy) / usable;
}
static void update_hscroll_from_mouse(Editor *ed, int mx) {
int h_full_w = ed->win_w - GUTTER_W - SCROLLBAR_W;
int visible_cols = (h_full_w - 8) / ed->char_w;
int max_cols = editor_max_line_len(ed);
int max_sx = max_cols - visible_cols;
if (max_sx <= 0) return;
int h_track_x = GUTTER_W + SCROLLBAR_W;
int h_track_w = h_full_w - SCROLLBAR_W * 2;
if (h_track_w <= 0) return;
int thumb_w = (h_track_w * visible_cols) / max_cols;
if (thumb_w < 16) thumb_w = 16;
int usable = h_track_w - thumb_w;
if (usable <= 0) return;
int pos = (mx - h_track_x) - ed->drag_offset;
if (pos < 0) pos = 0;
if (pos > usable) pos = usable;
ed->scroll_x = (pos * max_sx) / usable;
}
void editor_handle_button_press(Editor *ed, XButtonEvent *bev) {
int mx = bev->x, my = bev->y;
if (ed->confirm_mode != 0) {
if (bev->button == Button1) confirm_handle_click(ed, mx, my);
mouse_update_cursor(ed, mx, my, bev->state);
return;
}
if (ed->fbrowser_open) {
fbrowser_handle_click(ed, mx, my, bev->button, bev->time);
mouse_update_cursor(ed, mx, my, bev->state);
return;
}
if (menu_handle_click(ed, mx, my, bev->button)) {
mouse_update_cursor(ed, mx, my, bev->state);
return;
}
if (tabs_handle_click(ed, mx, my, bev->button)) {
tabs_handle_motion(ed, mx, my);
mouse_update_cursor(ed, mx, my, bev->state);
return;
}
int st_h = editor_status_h(ed);
int status_y = ed->win_h - st_h;
int h_track_y = ed->win_h - st_h - SCROLLBAR_W;
if (bev->button == Button1 && mx >= 0 && mx < GUTTER_W &&
my >= h_track_y && my < h_track_y + SCROLLBAR_W) {
ed->pressed_btn = 1;
ed->status_collapsed = !ed->status_collapsed;
editor_ensure_cursor_visible(ed);
mouse_update_cursor(ed, mx, my, bev->state);
return;
}
if (bev->button == Button1 && !ed->status_collapsed && my >= status_y) {
if (ed->search_mode && mx >= ed->chk_x1 && mx <= ed->chk_x2) {
ed->search_highlight_all = !ed->search_highlight_all;
}
return;
}
int v_track_x = ed->win_w - SCROLLBAR_W;
int v_full_y = TOP_UI_H;
int v_full_h = ed->win_h - TOP_UI_H - st_h - SCROLLBAR_W;
int h_full_x = GUTTER_W;
int h_full_w = ed->win_w - GUTTER_W - SCROLLBAR_W;
int visible_lines = (v_full_h > 0) ? (v_full_h / ed->line_h) : 1;
int visible_cols = (h_full_w > 8) ? ((h_full_w - 8) / ed->char_w) : 1;
int max_sy = ed->num_lines - visible_lines;
if (max_sy < 0) max_sy = 0;
int max_sx = editor_max_line_len(ed) - visible_cols;
if (max_sx < 0) max_sx = 0;
if (bev->button == Button4) {
ed->scroll_y -= 3;
if (ed->scroll_y < 0) ed->scroll_y = 0;
return;
}
if (bev->button == Button5) {
ed->scroll_y += 3;
if (ed->scroll_y > max_sy) ed->scroll_y = max_sy;
return;
}
if (bev->button != Button1) return;
if (mx >= v_track_x && my >= v_full_y && my < v_full_y + v_full_h) {
if (my < v_full_y + SCROLLBAR_W) {
ed->pressed_btn = 2;
if (ed->scroll_y > 0) ed->scroll_y--;
return;
}
if (my >= v_full_y + v_full_h - SCROLLBAR_W) {
ed->pressed_btn = 3;
if (ed->scroll_y < max_sy) ed->scroll_y++;
return;
}
if (max_sy > 0) {
int v_track_y = v_full_y + SCROLLBAR_W;
int v_track_h = v_full_h - SCROLLBAR_W * 2;
int thumb_h = (v_track_h * visible_lines) / ed->num_lines;
if (thumb_h < 16) thumb_h = 16;
int thumb_y = v_track_y + (ed->scroll_y * (v_track_h - thumb_h)) / max_sy;
ed->drag_vscroll = 1;
if (my >= thumb_y && my <= thumb_y + thumb_h) ed->drag_offset = my - thumb_y;
else {
ed->drag_offset = thumb_h / 2;
update_vscroll_from_mouse(ed, my);
}
}
} else if (my >= h_track_y && my < h_track_y + SCROLLBAR_W && mx >= h_full_x && mx < v_track_x) {
if (mx < h_full_x + SCROLLBAR_W) {
ed->pressed_btn = 4;
if (ed->scroll_x > 0) ed->scroll_x--;
return;
}
if (mx >= v_track_x - SCROLLBAR_W) {
ed->pressed_btn = 5;
if (ed->scroll_x < max_sx) ed->scroll_x++;
return;
}
if (max_sx > 0) {
int h_track_x = h_full_x + SCROLLBAR_W;
int h_track_w = h_full_w - SCROLLBAR_W * 2;
int max_cols = editor_max_line_len(ed);
int thumb_w = (h_track_w * visible_cols) / max_cols;
if (thumb_w < 16) thumb_w = 16;
int thumb_x = h_track_x + (ed->scroll_x * (h_track_w - thumb_w)) / max_sx;
ed->drag_hscroll = 1;
if (mx >= thumb_x && mx <= thumb_x + thumb_w) ed->drag_offset = mx - thumb_x;
else {
ed->drag_offset = thumb_w / 2;
update_hscroll_from_mouse(ed, mx);
}
}
} else if (mx >= GUTTER_W && mx < v_track_x && my >= TOP_UI_H && my < h_track_y) {
int ctrl = (bev->state & ControlMask);
int shift = (bev->state & ShiftMask);
if (ctrl) {
set_cursor_from_mouse(ed, mx, my);
ed->has_sel = 0;
ed->hover_sym_line = -1;
menu_goto_symbol_at_cursor(ed);
mouse_update_cursor(ed, mx, my, bev->state);
return;
}
if (shift) {
if (!ed->has_sel) {
ed->sel_ax = ed->cx;
ed->sel_ay = ed->cy;
}
set_cursor_from_mouse(ed, mx, my);
ed->has_sel = (ed->cx != ed->sel_ax || ed->cy != ed->sel_ay);
} else {
set_cursor_from_mouse(ed, mx, my);
ed->has_sel = 0;
ed->sel_ax = ed->cx;
ed->sel_ay = ed->cy;
}
ed->drag_select = 1;
}
}
void editor_handle_motion(Editor *ed, XMotionEvent *mev) {
if (ed->drag_vscroll) update_vscroll_from_mouse(ed, mev->y);
else if (ed->drag_hscroll) update_hscroll_from_mouse(ed, mev->x);
else if (ed->drag_select) {
set_cursor_from_mouse(ed, mev->x, mev->y);
ed->has_sel = (ed->cx != ed->sel_ax || ed->cy != ed->sel_ay);
}
}
void editor_handle_button_release(Editor *ed, XButtonEvent *bev) {
if (bev->button == Button1) {
ed->drag_vscroll = 0;
ed->drag_hscroll = 0;
ed->drag_select = 0;
ed->pressed_btn = 0;
}
}
+180
View File
@@ -0,0 +1,180 @@
#include "input.h"
#include <string.h>
void editor_get_sel_bounds(const Editor *ed, int *sy, int *sx, int *ey, int *ex) {
if (ed->sel_ay < ed->cy || (ed->sel_ay == ed->cy && ed->sel_ax <= ed->cx)) {
*sy = ed->sel_ay; *sx = ed->sel_ax;
*ey = ed->cy; *ex = ed->cx;
} else {
*sy = ed->cy; *sx = ed->cx;
*ey = ed->sel_ay; *ex = ed->sel_ax;
}
}
void editor_delete_selection(Editor *ed) {
if (!ed->has_sel) return;
int sy, sx, ey, ex;
editor_get_sel_bounds(ed, &sy, &sx, &ey, &ex);
ed->has_sel = 0;
if (sy == ey && sx == ex) return;
if (sy == ey) {
int len = (int)strlen(ed->lines[sy]);
memmove(&ed->lines[sy][sx], &ed->lines[sy][ex], (size_t)(len - ex + 1));
} else {
int tail_len = (int)strlen(&ed->lines[ey][ex]);
if (sx + tail_len < MAX_LEN - 1) {
memcpy(&ed->lines[sy][sx], &ed->lines[ey][ex], (size_t)(tail_len + 1));
} else {
ed->lines[sy][sx] = '\0';
}
int removed = ey - sy;
for (int i = sy + 1; i < ed->num_lines - removed; i++) {
strcpy(ed->lines[i], ed->lines[i + removed]);
}
ed->num_lines -= removed;
}
ed->cy = sy;
ed->cx = sx;
}
void editor_ensure_cursor_visible(Editor *ed) {
int visible_lines = (ed->win_h - TOP_UI_H - editor_status_h(ed) - SCROLLBAR_W) / ed->line_h;
int visible_cols = (ed->win_w - GUTTER_W - SCROLLBAR_W - 8) / ed->char_w;
if (visible_lines < 1) visible_lines = 1;
if (visible_cols < 1) visible_cols = 1;
int cur_col = utf8_char_count(ed->lines[ed->cy], ed->cx);
if (ed->cy < ed->scroll_y) ed->scroll_y = ed->cy;
if (ed->cy >= ed->scroll_y + visible_lines) ed->scroll_y = ed->cy - visible_lines + 1;
if (cur_col < ed->scroll_x) ed->scroll_x = cur_col;
if (cur_col >= ed->scroll_x + visible_cols) ed->scroll_x = cur_col - visible_cols + 1;
}
void input_insert_bytes(Editor *ed, const char *bytes, int count) {
int len = (int)strlen(ed->lines[ed->cy]);
if (len + count < MAX_LEN - 1) {
memmove(&ed->lines[ed->cy][ed->cx + count], &ed->lines[ed->cy][ed->cx], (size_t)(len - ed->cx + 1));
memcpy(&ed->lines[ed->cy][ed->cx], bytes, (size_t)count);
ed->cx += count;
}
}
static void insert_raw_newline(Editor *ed) {
if (ed->num_lines < MAX_LINES) {
for (int i = ed->num_lines; i > ed->cy + 1; i--) {
strcpy(ed->lines[i], ed->lines[i - 1]);
}
strcpy(ed->lines[ed->cy + 1], &ed->lines[ed->cy][ed->cx]);
ed->lines[ed->cy][ed->cx] = '\0';
ed->num_lines++;
ed->cy++;
ed->cx = 0;
}
}
void input_insert_newline(Editor *ed) {
if (ed->num_lines >= MAX_LINES) return;
const char *cur_line = ed->lines[ed->cy];
/* Подсчитываем отступ текущей строки (в пробелах) до позиции курсора */
int indent_spaces = 0;
for (int i = 0; i < ed->cx && cur_line[i] != '\0'; i++) {
if (cur_line[i] == ' ') indent_spaces++;
else if (cur_line[i] == '\t') indent_spaces += 4;
else break;
}
/* Если перед курсором стоит открывающая фигурная скобка '{', добавляем +4 пробела */
int check = ed->cx - 1;
while (check >= 0 && (cur_line[check] == ' ' || cur_line[check] == '\t')) {
check--;
}
if (check >= 0 && cur_line[check] == '{') {
indent_spaces += 4;
}
insert_raw_newline(ed);
/* Вставляем вычисленный отступ в начало новой строки */
int tail_len = (int)strlen(ed->lines[ed->cy]);
if (indent_spaces > MAX_LEN - 1 - tail_len) {
indent_spaces = MAX_LEN - 1 - tail_len;
}
if (indent_spaces > 0) {
memmove(&ed->lines[ed->cy][indent_spaces], ed->lines[ed->cy], (size_t)(tail_len + 1));
memset(ed->lines[ed->cy], ' ', (size_t)indent_spaces);
ed->cx = indent_spaces;
}
}
void editor_insert_text(Editor *ed, const char *text) {
history_push_undo(ed);
if (ed->has_sel) editor_delete_selection(ed);
int i = 0;
while (text[i] != '\0') {
if (text[i] == '\r') {
i++;
continue;
}
if (text[i] == '\n') {
insert_raw_newline(ed);
i++;
} else if (text[i] == '\t') {
input_insert_bytes(ed, " ", 4);
i++;
} else if ((unsigned char)text[i] >= 32 && (unsigned char)text[i] != 127) {
int next = utf8_next_char_pos(text, i);
input_insert_bytes(ed, &text[i], next - i);
i = next;
} else {
i++;
}
}
editor_ensure_cursor_visible(ed);
}
void input_indent_selection(Editor *ed, int unindent) {
int sy = ed->cy, ey = ed->cy;
int sx = ed->cx, ex = ed->cx;
if (ed->has_sel) {
editor_get_sel_bounds(ed, &sy, &sx, &ey, &ex);
if (ey > sy && ex == 0) {
ey--;
}
}
for (int y = sy; y <= ey; y++) {
int len = (int)strlen(ed->lines[y]);
if (!unindent) {
if (len + 4 < MAX_LEN - 1) {
memmove(&ed->lines[y][4], ed->lines[y], (size_t)(len + 1));
memcpy(ed->lines[y], " ", 4);
if (y == ed->cy) ed->cx += 4;
if (ed->has_sel && y == ed->sel_ay) ed->sel_ax += 4;
}
} else {
int rem = 0;
if (ed->lines[y][0] == '\t') {
rem = 1;
} else {
while (rem < 4 && ed->lines[y][rem] == ' ') rem++;
}
if (rem > 0) {
memmove(ed->lines[y], &ed->lines[y][rem], (size_t)(len - rem + 1));
if (y == ed->cy) {
ed->cx = (ed->cx > rem) ? (ed->cx - rem) : 0;
}
if (ed->has_sel && y == ed->sel_ay) {
ed->sel_ax = (ed->sel_ax > rem) ? (ed->sel_ax - rem) : 0;
}
}
}
}
}
+35
View File
@@ -0,0 +1,35 @@
#include "input.h"
#include <string.h>
int utf8_char_count(const char *s, int max_bytes) {
int count = 0;
for (int i = 0; i < max_bytes && s[i] != '\0'; i++) {
if (((unsigned char)s[i] & 0xC0) != 0x80) count++;
}
return count;
}
int utf8_byte_offset(const char *s, int char_idx) {
int i = 0, count = 0;
while (s[i] != '\0' && count < char_idx) {
if (((unsigned char)s[i] & 0xC0) != 0x80) count++;
if (count <= char_idx) i++;
}
while (s[i] != '\0' && ((unsigned char)s[i] & 0xC0) == 0x80) i++;
return i;
}
int utf8_prev_char_pos(const char *s, int byte_pos) {
if (byte_pos <= 0) return 0;
int pos = byte_pos - 1;
while (pos > 0 && ((unsigned char)s[pos] & 0xC0) == 0x80) pos--;
return pos;
}
int utf8_next_char_pos(const char *s, int byte_pos) {
int len = (int)strlen(s);
if (byte_pos >= len) return len;
int pos = byte_pos + 1;
while (pos < len && ((unsigned char)s[pos] & 0xC0) == 0x80) pos++;
return pos;
}