utf8 processing without struct

This commit is contained in:
2026-05-03 23:32:40 +02:00
parent eae85c32ca
commit 8e1b4d2f86
23 changed files with 637 additions and 906 deletions
+46 -68
View File
@@ -1,94 +1,72 @@
#include "../include/editor_op.h"
#include "../include/row_op.h"
#include "include/data.h"
#include "../include/data.h"
#include <stdio.h>
#include "../include/utf8.h"
extern struct editorConfig E;
void editorInsertChar(utf_8_char_t *c) {
void editorInsertChar(int c) {
if (E.state == READ_ONLY) return;
fprintf(stderr, "Insert char %s %d\n", c->c, c->len);
// If cursor is past end of file, add empty rows
if (E.cursor_y == E.numrows) {
if (E.cursor_y == E.numrows)
editorInsertRow(E.numrows, "", 0);
row_t *row = &E.rows[E.cursor_y];
int byte = editorRowCxToByte(row, E.cursor_x);
char buf[4];
int n;
if (c < 0x80) {
buf[0] = c;
n = 1;
} else {
n = utf8Encode((uint32_t)c, buf);
}
// Insert character at cursor position
editorRowInsertChar(&E.row[E.cursor_y], E.cursor_x, *c);
editorRowInsertBytes(row, byte, buf, n);
E.cursor_x++;
E.dirty = 1;
}
void editorInsertNewline(void) {
void editorInsertNewline(const char* s, int len) {
if (E.state == READ_ONLY) return;
if (E.cursor_x == 0) {
// Insert blank line before current line
editorInsertRow(E.cursor_y, "", 0);
} else {
// Split current line at cursor
erow *row = &E.row[E.cursor_y];
// Calculate byte length of remaining part
int remaining_chars = row->size - E.cursor_x;
// Allocate buffer for remaining characters
char *buf = malloc(remaining_chars * 4); // Max 4 bytes per UTF-8 char
int buf_len = 0;
// Convert utf_8_char_t to bytes
for (int i = E.cursor_x; i < row->size; i++) {
for (int j = 0; j < row->chars[i].len; j++) {
buf[buf_len++] = row->chars[i].c[j];
}
}
// Insert new row with remaining text
editorInsertRow(E.cursor_y + 1, buf, buf_len);
free(buf);
// Truncate current row at cursor
row = &E.row[E.cursor_y]; // Refresh pointer after realloc
row->size = E.cursor_x;
editorUpdateRow(row);
}
E.cursor_y++;
E.cursor_x = 0;
E.rows = realloc(E.rows, sizeof(row_t) * (E.numrows + 1));
row_t *r = &E.rows[E.numrows];
r->cap = len + 1;
r->chars = malloc(r->cap);
memcpy(r->chars, s, len);
r->size = len;
r->chars[len] = '\0';
E.numrows++;
}
void editorRowAppendRow(erow *dest, erow *src) {
// Allocate space for combined rows
utf_8_char_t *new_chars = realloc(dest->chars,
sizeof(utf_8_char_t) * (dest->size + src->size));
if (!new_chars) return;
dest->chars = new_chars;
// Copy source row characters
memcpy(&dest->chars[dest->size], src->chars, sizeof(utf_8_char_t) * src->size);
dest->size += src->size;
editorUpdateRow(dest);
++E.dirty;
}
void editorDelChar(void) {
void editorDelChar(void)
{
if (E.state == READ_ONLY) return;
if (E.cursor_y == E.numrows) return;
if (E.cursor_x == 0 && E.cursor_y == 0) return;
erow *row = &E.row[E.cursor_y];
row_t *r = &E.rows[E.cursor_y];
if (E.cursor_x > 0) {
// Delete character before cursor
editorRowDelchar(row, E.cursor_x - 1);
/* find byte of previous char */
int byte = editorRowCxToByte(r, E.cursor_x);
/* step back one character */
int start = byte;
/* walk from beginning to find start of char at cx-1 */
start = editorRowCxToByte(r, E.cursor_x - 1);
editorRowDelByte(r, start, byte - start);
E.cursor_x--;
E.dirty = 1;
} else {
// At beginning of line - join with previous line
E.cursor_x = E.row[E.cursor_y - 1].size;
editorRowAppendRow(&E.row[E.cursor_y - 1], row);
editorDelRow(E.cursor_y);
/* merge with previous row */
row_t *prev = &E.rows[E.cursor_y - 1];
E.cursor_x = editorRowCharCount(prev);
editorRowInsertBytes(prev, prev->size, r->chars, r->size);
free(r->chars);
memmove(&E.rows[E.cursor_y], &E.rows[E.cursor_y + 1],
sizeof(row_t) * (E.numrows - E.cursor_y - 1));
E.numrows--;
E.cursor_y--;
E.dirty = 1;
}
}