#include "editor.h" #include #include #include #define TAB_MIN_W 110 #define TAB_MAX_W 210 #define TAB_NEW_BTN_W 28 static const char *base_filename(const char *path) { const char *slash = strrchr(path, '/'); return slash ? (slash + 1) : path; } static void free_tab_storage(EditorTab *t) { free(t->lines); t->lines = NULL; for (int i = 0; i < t->undo_count; i++) { free(t->undo_stack[i].lines); t->undo_stack[i].lines = NULL; } for (int i = 0; i < t->redo_count; i++) { free(t->redo_stack[i].lines); t->redo_stack[i].lines = NULL; } t->undo_count = 0; t->redo_count = 0; } static void save_active_to_tab(Editor *ed) { if (ed->active_tab < 0 || ed->active_tab >= ed->num_tabs) return; EditorTab *t = &ed->tabs[ed->active_tab]; if (!t->lines) { t->lines = malloc((size_t)MAX_LINES * MAX_LEN); } if (t->lines) { memcpy(t->lines, ed->lines, (size_t)ed->num_lines * MAX_LEN); } t->num_lines = ed->num_lines; t->cx = ed->cx; t->cy = ed->cy; t->scroll_x = ed->scroll_x; t->scroll_y = ed->scroll_y; t->has_sel = ed->has_sel; t->sel_ax = ed->sel_ax; t->sel_ay = ed->sel_ay; memcpy(t->undo_stack, ed->undo_stack, sizeof(ed->undo_stack)); t->undo_count = ed->undo_count; memset(ed->undo_stack, 0, sizeof(ed->undo_stack)); ed->undo_count = 0; memcpy(t->redo_stack, ed->redo_stack, sizeof(ed->redo_stack)); t->redo_count = ed->redo_count; memset(ed->redo_stack, 0, sizeof(ed->redo_stack)); ed->redo_count = 0; memcpy(t->nav_back, ed->nav_back, sizeof(ed->nav_back)); t->nav_back_count = ed->nav_back_count; memcpy(t->nav_fwd, ed->nav_fwd, sizeof(ed->nav_fwd)); t->nav_fwd_count = ed->nav_fwd_count; t->dirty = ed->dirty; t->saved_undo_level = ed->saved_undo_level; snprintf(t->filename, sizeof(t->filename), "%s", ed->filename); } static void load_tab_to_active(Editor *ed, int idx) { EditorTab *t = &ed->tabs[idx]; ed->active_tab = idx; if (t->lines) { memcpy(ed->lines, t->lines, (size_t)t->num_lines * MAX_LEN); } else { **ed->lines = '\0'; } ed->num_lines = (t->num_lines > 0) ? t->num_lines : 1; ed->cx = t->cx; ed->cy = t->cy; ed->scroll_x = t->scroll_x; ed->scroll_y = t->scroll_y; ed->has_sel = t->has_sel; ed->sel_ax = t->sel_ax; ed->sel_ay = t->sel_ay; memcpy(ed->undo_stack, t->undo_stack, sizeof(ed->undo_stack)); ed->undo_count = t->undo_count; memset(t->undo_stack, 0, sizeof(t->undo_stack)); t->undo_count = 0; memcpy(ed->redo_stack, t->redo_stack, sizeof(ed->redo_stack)); ed->redo_count = t->redo_count; memset(t->redo_stack, 0, sizeof(t->redo_stack)); t->redo_count = 0; memcpy(ed->nav_back, t->nav_back, sizeof(ed->nav_back)); ed->nav_back_count = t->nav_back_count; memcpy(ed->nav_fwd, t->nav_fwd, sizeof(ed->nav_fwd)); ed->nav_fwd_count = t->nav_fwd_count; snprintf(ed->filename, sizeof(ed->filename), "%s", t->filename); ed->saved_undo_level = t->saved_undo_level; editor_set_dirty(ed, t->dirty); ed->hover_sym_line = -1; } static int calc_tab_width(const Editor *ed) { if (ed->num_tabs <= 0) return TAB_MAX_W; int avail = ed->win_w - TAB_NEW_BTN_W - 8; int w = avail / ed->num_tabs; if (w > TAB_MAX_W) w = TAB_MAX_W; if (w < TAB_MIN_W) w = TAB_MIN_W; return w; } void tabs_new(Editor *ed, const char *fname) { if (ed->num_tabs >= MAX_TABS) { snprintf(ed->status_msg, sizeof(ed->status_msg), "Максимум %d вкладок", MAX_TABS); return; } if (ed->num_tabs > 0) { save_active_to_tab(ed); } int idx = ed->num_tabs++; memset(&ed->tabs[idx], 0, sizeof(EditorTab)); ed->active_tab = idx; ed->cx = 0; ed->cy = 0; ed->scroll_x = 0; ed->scroll_y = 0; ed->has_sel = 0; ed->undo_count = 0; ed->redo_count = 0; ed->nav_back_count = 0; ed->nav_fwd_count = 0; ed->saved_undo_level = 0; if (fname && *fname != '\0') { editor_load(ed, fname); } else { char def_name[FNAME_LEN]; snprintf(def_name, sizeof(def_name), "untitled_%d.c", idx + 1); snprintf(ed->filename, sizeof(ed->filename), "%s", def_name); ed->num_lines = 1; **ed->lines = '\0'; editor_set_dirty(ed, 0); snprintf(ed->status_msg, sizeof(ed->status_msg), "New tab: %s", def_name); } save_active_to_tab(ed); load_tab_to_active(ed, idx); } void tabs_open_file(Editor *ed, const char *fname) { if (!fname || *fname == '\0') return; for (int i = 0; i < ed->num_tabs; i++) { const char *tab_name = (i == ed->active_tab) ? ed->filename : ed->tabs[i].filename; if (strcmp(tab_name, fname) == 0) { tabs_switch(ed, i); snprintf(ed->status_msg, sizeof(ed->status_msg), "Switched to open tab: %s", fname); return; } } tabs_new(ed, fname); } void tabs_switch(Editor *ed, int idx) { if (idx < 0 || idx >= ed->num_tabs || idx == ed->active_tab) return; save_active_to_tab(ed); load_tab_to_active(ed, idx); snprintf(ed->status_msg, sizeof(ed->status_msg), "Tab: %s", ed->filename); } void tabs_next(Editor *ed, int prev) { if (ed->num_tabs <= 1) return; int next = ed->active_tab + (prev ? -1 : 1); if (next < 0) next = ed->num_tabs - 1; if (next >= ed->num_tabs) next = 0; tabs_switch(ed, next); } void tabs_close_force(Editor *ed, int idx) { if (idx < 0 || idx >= ed->num_tabs) return; if (ed->num_tabs == 1) { history_free_all(ed); free_tab_storage(ed->tabs); ed->num_tabs = 0; ed->active_tab = -1; tabs_new(ed, NULL); return; } save_active_to_tab(ed); free_tab_storage(&ed->tabs[idx]); for (int i = idx; i < ed->num_tabs - 1; i++) { ed->tabs[i] = ed->tabs[i + 1]; } memset(&ed->tabs[ed->num_tabs - 1], 0, sizeof(EditorTab)); ed->num_tabs--; int next_idx = ed->active_tab; if (idx < ed->active_tab) next_idx--; if (next_idx >= ed->num_tabs) next_idx = ed->num_tabs - 1; if (next_idx < 0) next_idx = 0; ed->active_tab = -1; load_tab_to_active(ed, next_idx); snprintf(ed->status_msg, sizeof(ed->status_msg), "Closed tab"); } void tabs_request_close(Editor *ed, int idx) { if (idx < 0 || idx >= ed->num_tabs) return; int is_dirty = (idx == ed->active_tab) ? ed->dirty : ed->tabs[idx].dirty; if (is_dirty) { ed->confirm_mode = 1; ed->confirm_target_tab = idx; ed->confirm_hover_btn = 0; } else { tabs_close_force(ed, idx); } } void tabs_request_quit(Editor *ed) { for (int i = 0; i < ed->num_tabs; i++) { int is_dirty = (i == ed->active_tab) ? ed->dirty : ed->tabs[i].dirty; if (is_dirty) { ed->confirm_mode = 2; ed->confirm_target_tab = i; ed->confirm_hover_btn = 0; return; } } ed->running = 0; } void tabs_init(Editor *ed, int argc, char *argv[]) { ed->num_tabs = 0; ed->active_tab = -1; ed->hover_tab = -1; ed->hover_tab_close = -1; if (argc > 1) { for (int i = 1; i < argc && ed->num_tabs < MAX_TABS; i++) { tabs_new(ed, argv[i]); } tabs_switch(ed, 0); } else { tabs_new(ed, "main.c"); } } void tabs_free_all(Editor *ed) { for (int i = 0; i < ed->num_tabs; i++) { free_tab_storage(&ed->tabs[i]); } ed->num_tabs = 0; } void tabs_render(Editor *ed) { int y0 = MENUBAR_H; XSetForeground(ed->dpy, ed->gc, ed->col_gutter); XFillRectangle(ed->dpy, ed->backbuf, ed->gc, 0, y0, ed->win_w, TABBAR_H); XSetForeground(ed->dpy, ed->gc, ed->col_sb_thumb); XDrawLine(ed->dpy, ed->backbuf, ed->gc, 0, y0 + TABBAR_H - 1, ed->win_w, y0 + TABBAR_H - 1); int tw = calc_tab_width(ed); int base_y = y0 + (TABBAR_H + ed->font->ascent - ed->font->descent) / 2; for (int i = 0; i < ed->num_tabs; i++) { int tx = i * tw; if (tx >= ed->win_w - TAB_NEW_BTN_W) break; int is_active = (i == ed->active_tab); int is_dirty = is_active ? ed->dirty : ed->tabs[i].dirty; const char *full_name = is_active ? ed->filename : ed->tabs[i].filename; const char *short_name = base_filename(full_name); unsigned long bg = is_active ? ed->col_bg : ((ed->hover_tab == i) ? ed->col_curline : ed->col_gutter); XSetForeground(ed->dpy, ed->gc, bg); XFillRectangle(ed->dpy, ed->backbuf, ed->gc, tx, y0, tw, is_active ? TABBAR_H : (TABBAR_H - 1)); XSetForeground(ed->dpy, ed->gc, ed->col_sb_thumb); XDrawLine(ed->dpy, ed->backbuf, ed->gc, tx + tw - 1, y0, tx + tw - 1, y0 + TABBAR_H - 1); if (is_active) { XSetForeground(ed->dpy, ed->gc, ed->xft_link.pixel); XFillRectangle(ed->dpy, ed->backbuf, ed->gc, tx, y0, tw - 1, 2); } char label[FNAME_LEN + 8]; snprintf(label, sizeof(label), "%s%s", is_dirty ? "*" : "", short_name); int max_chars = (tw - 26) / ed->char_w; if (max_chars < 1) max_chars = 1; int slen = (int)strlen(label); if (slen > max_chars) { label[max_chars] = '\0'; slen = max_chars; } XftColor *tcol = is_dirty ? &ed->xft_num : (is_active ? &ed->xft_fg : &ed->xft_lineno); XftDrawStringUtf8(ed->xft_draw, tcol, ed->font, tx + 8, base_y, (const FcChar8 *)label, slen); int cx = tx + tw - 18; int cy = y0 + (TABBAR_H - 14) / 2; if (ed->hover_tab_close == i) { XSetForeground(ed->dpy, ed->gc, ed->col_sel); XFillRectangle(ed->dpy, ed->backbuf, ed->gc, cx, cy, 14, 14); } XftColor *xcol = (ed->hover_tab_close == i) ? &ed->xft_num : &ed->xft_lineno; XftDrawStringUtf8(ed->xft_draw, xcol, ed->font, cx + 3, base_y, (const FcChar8 *)"×", 2); } int plus_x = ed->num_tabs * tw; if (plus_x + TAB_NEW_BTN_W <= ed->win_w) { if (ed->hover_tab == -2) { XSetForeground(ed->dpy, ed->gc, ed->col_curline); XFillRectangle(ed->dpy, ed->backbuf, ed->gc, plus_x + 2, y0 + 2, TAB_NEW_BTN_W - 4, TABBAR_H - 5); } XftDrawStringUtf8(ed->xft_draw, &ed->xft_fg, ed->font, plus_x + 9, base_y, (const FcChar8 *)"+", 1); } } int tabs_handle_motion(Editor *ed, int mx, int my) { int old_ht = ed->hover_tab; int old_hc = ed->hover_tab_close; ed->hover_tab = -1; ed->hover_tab_close = -1; if (ed->menu_open < 0 && ed->confirm_mode == 0 && my >= MENUBAR_H && my < TOP_UI_H) { int tw = calc_tab_width(ed); int idx = mx / tw; if (idx >= 0 && idx < ed->num_tabs && (idx * tw) < ed->win_w - TAB_NEW_BTN_W) { ed->hover_tab = idx; int cx = idx * tw + tw - 18; if (mx >= cx && mx < cx + 14) { ed->hover_tab_close = idx; } } else { int plus_x = ed->num_tabs * tw; if (mx >= plus_x && mx < plus_x + TAB_NEW_BTN_W) { ed->hover_tab = -2; } } } return (old_ht != ed->hover_tab || old_hc != ed->hover_tab_close); } int tabs_handle_click(Editor *ed, int mx, int my, int button) { if (my < MENUBAR_H || my >= TOP_UI_H) return 0; if (ed->menu_open >= 0) return 0; int tw = calc_tab_width(ed); int idx = mx / tw; if (idx >= 0 && idx < ed->num_tabs && (idx * tw) < ed->win_w - TAB_NEW_BTN_W) { int cx = idx * tw + tw - 18; if (button == Button2 || (button == Button1 && mx >= cx && mx < cx + 14)) { tabs_request_close(ed, idx); } else if (button == Button1) { tabs_switch(ed, idx); } return 1; } int plus_x = ed->num_tabs * tw; if (button == Button1 && mx >= plus_x && mx < plus_x + TAB_NEW_BTN_W) { tabs_new(ed, NULL); return 1; } return 1; } /* --- Модальный диалог подтверждения закрытия / выхода --- */ #define DLG_W 440 #define DLG_H 128 static void confirm_apply_choice(Editor *ed, int choice) { int mode = ed->confirm_mode; int target = ed->confirm_target_tab; ed->confirm_mode = 0; if (choice == 2) return; /* Отмена */ if (mode == 1) { if (choice == 0) { int prev = ed->active_tab; tabs_switch(ed, target); editor_save(ed); if (prev != target) tabs_switch(ed, prev); } tabs_close_force(ed, target); } else if (mode == 2) { if (choice == 0) { for (int i = 0; i < ed->num_tabs; i++) { int d = (i == ed->active_tab) ? ed->dirty : ed->tabs[i].dirty; if (d) { tabs_switch(ed, i); editor_save(ed); } } } ed->running = 0; } } void confirm_render(Editor *ed) { if (ed->confirm_mode == 0) return; int dx = (ed->win_w - DLG_W) / 2; int dy = (ed->win_h - DLG_H) / 2; XSetForeground(ed->dpy, ed->gc, ed->col_gutter); XFillRectangle(ed->dpy, ed->backbuf, ed->gc, dx, dy, DLG_W, DLG_H); XSetForeground(ed->dpy, ed->gc, ed->col_fg); XDrawRectangle(ed->dpy, ed->backbuf, ed->gc, dx, dy, DLG_W - 1, DLG_H - 1); XSetForeground(ed->dpy, ed->gc, ed->col_status); XFillRectangle(ed->dpy, ed->backbuf, ed->gc, dx + 1, dy + 1, DLG_W - 2, 26); const char *hdr = (ed->confirm_mode == 1) ? "Несохранённые изменения во вкладке" : "Несохранённые изменения"; XftDrawStringUtf8(ed->xft_draw, &ed->xft_fg, ed->font, dx + 12, dy + 18, (const FcChar8 *)hdr, (int)strlen(hdr)); char msg[STATUS_LEN]; if (ed->confirm_mode == 1) { const char *fn = (ed->confirm_target_tab == ed->active_tab) ? ed->filename : ed->tabs[ed->confirm_target_tab].filename; snprintf(msg, sizeof(msg), "Сохранить изменения в «%s»?", base_filename(fn)); } else { snprintf(msg, sizeof(msg), "Сохранить изменения перед выходом?"); } XftDrawStringUtf8(ed->xft_draw, &ed->xft_fg, ed->font, dx + 14, dy + 54, (const FcChar8 *)msg, (int)strlen(msg)); const char *btns[] = {"Сохранить (S)", "Не сохранять (N)", "Отмена (Esc)"}; int bw = 132, bh = 28, gap = 10; int start_x = dx + (DLG_W - (bw * 3 + gap * 2)) / 2; int by = dy + DLG_H - bh - 14; for (int i = 0; i < 3; i++) { int bx = start_x + i * (bw + gap); unsigned long bbg = (ed->confirm_hover_btn == i) ? ed->col_match_cur : ed->col_status; XSetForeground(ed->dpy, ed->gc, bbg); XFillRectangle(ed->dpy, ed->backbuf, ed->gc, bx, by, bw, bh); XSetForeground(ed->dpy, ed->gc, ed->col_fg); XDrawRectangle(ed->dpy, ed->backbuf, ed->gc, bx, by, bw - 1, bh - 1); int tlen = (int)strlen(btns[i]); int tcols = utf8_char_count(btns[i], tlen); int tx = bx + (bw - tcols * ed->char_w) / 2; int ty = by + (bh + ed->font->ascent - ed->font->descent) / 2; XftDrawStringUtf8(ed->xft_draw, &ed->xft_fg, ed->font, tx, ty, (const FcChar8 *)btns[i], tlen); } } int confirm_handle_motion(Editor *ed, int mx, int my) { if (ed->confirm_mode == 0) return 0; int old = ed->confirm_hover_btn; ed->confirm_hover_btn = -1; int dx = (ed->win_w - DLG_W) / 2; int dy = (ed->win_h - DLG_H) / 2; int bw = 132, bh = 28, gap = 10; int start_x = dx + (DLG_W - (bw * 3 + gap * 2)) / 2; int by = dy + DLG_H - bh - 14; if (my >= by && my < by + bh) { for (int i = 0; i < 3; i++) { int bx = start_x + i * (bw + gap); if (mx >= bx && mx < bx + bw) { ed->confirm_hover_btn = i; break; } } } return (old != ed->confirm_hover_btn); } int confirm_handle_click(Editor *ed, int mx, int my) { if (ed->confirm_mode == 0) return 0; confirm_handle_motion(ed, mx, my); if (ed->confirm_hover_btn >= 0) { confirm_apply_choice(ed, ed->confirm_hover_btn); } return 1; } void confirm_handle_key(Editor *ed, KeySym ks) { if (ed->confirm_mode == 0) return; if (ks == XK_Escape || ks == XK_c || ks == XK_C) confirm_apply_choice(ed, 2); else if (ks == XK_s || ks == XK_S || ks == XK_Return || ks == XK_KP_Enter) confirm_apply_choice(ed, 0); else if (ks == XK_n || ks == XK_N) confirm_apply_choice(ed, 1); else if (ks == XK_Left) ed->confirm_hover_btn = (ed->confirm_hover_btn + 2) % 3; else if (ks == XK_Right || ks == XK_Tab) ed->confirm_hover_btn = (ed->confirm_hover_btn + 1) % 3; }