add utf8_char_t struct
This commit is contained in:
+71
-24
@@ -1,47 +1,94 @@
|
||||
#include "../include/editor_op.h"
|
||||
#include "../include/row_op.h"
|
||||
#include "include/data.h"
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
extern struct editorConfig E;
|
||||
|
||||
void editorInsertChar(int c) {
|
||||
if (E.cursor_y == E.numrows) {
|
||||
editorInsertRow(E.numrows, "", 0);
|
||||
}
|
||||
editorRowInsertChar(&E.row[E.cursor_y], E.cursor_x, c);
|
||||
E.cursor_x++;
|
||||
void editorInsertChar(utf_8_char_t *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) {
|
||||
editorInsertRow(E.numrows, "", 0);
|
||||
}
|
||||
|
||||
// Insert character at cursor position
|
||||
editorRowInsertChar(&E.row[E.cursor_y], E.cursor_x, *c);
|
||||
E.cursor_x++;
|
||||
}
|
||||
|
||||
void editorInsertNewLine() {
|
||||
erow *row;
|
||||
if (!E.cursor_x) {
|
||||
void editorInsertNewline(void) {
|
||||
if (E.state == READ_ONLY) return;
|
||||
|
||||
if (E.cursor_x == 0) {
|
||||
// Insert blank line before current line
|
||||
editorInsertRow(E.cursor_y, "", 0);
|
||||
} else {
|
||||
row = &E.row[E.cursor_y];
|
||||
editorInsertRow(E.cursor_y + 1, &row->chars[E.cursor_x],
|
||||
row->size - E.cursor_x);
|
||||
row = &E.row[E.cursor_y];
|
||||
// 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;
|
||||
row->chars[row->size] = '\0';
|
||||
editorUpdateRow(row);
|
||||
}
|
||||
++E.cursor_y;
|
||||
|
||||
E.cursor_y++;
|
||||
E.cursor_x = 0;
|
||||
}
|
||||
|
||||
void editorDelChar() {
|
||||
erow *row;
|
||||
if (E.cursor_y == E.numrows || !(E.cursor_x || E.cursor_y)) {
|
||||
return;
|
||||
}
|
||||
row = &E.row[E.cursor_y];
|
||||
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) {
|
||||
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];
|
||||
|
||||
if (E.cursor_x > 0) {
|
||||
// Delete character before cursor
|
||||
editorRowDelchar(row, E.cursor_x - 1);
|
||||
--E.cursor_x;
|
||||
E.cursor_x--;
|
||||
} else {
|
||||
// At beginning of line - join with previous line
|
||||
E.cursor_x = E.row[E.cursor_y - 1].size;
|
||||
editorRowAppendString(&E.row[E.cursor_y - 1], row->chars, row->size);
|
||||
editorRowAppendRow(&E.row[E.cursor_y - 1], row);
|
||||
editorDelRow(E.cursor_y);
|
||||
--E.cursor_y;
|
||||
E.cursor_y--;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user