Files
test/input/text_ops.c
T

181 lines
5.9 KiB
C

#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;
}
}
}
}
}