Merge branch 'refs/heads/char_encode'

# Conflicts:
#	include/builtins.h
#	include/data.h
#	include/editor_op.h
#	include/file_io.h
#	include/input.h
#	include/row_op.h
#	install.sh
#	main.c
#	meson.build
#	src/builtins.c
#	src/editor_op.c
#	src/file_io.c
#	src/init.c
#	src/input.c
#	src/output.c
#	src/row_op.c
This commit is contained in:
2026-05-04 00:31:48 +02:00
16 changed files with 350 additions and 330 deletions
+42 -95
View File
@@ -1,152 +1,99 @@
#include "../include/row_op.h"
#include "../include/data.h"
#include "../include/define.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "include/utf8.h"
extern struct editorConfig E;
int bufferRowCxToRx(frow *row, int cursor_x) {
int render_x = 0;
int i;
for (i = 0; i < cursor_x; ++i) {
if (row->chars[i] == '\t') {
render_x += (E.constantes.TAB_LENGTH - 1) - (render_x % E.constantes.TAB_LENGTH);
}
render_x++;
}
return render_x;
}
int bufferRowRxToCx(frow *row, int rx) {
int cur_rx = 0;
int cx;
for (cx = 0; cx < row->size; cx++) {
if (row->chars[cx] == '\t')
cur_rx += (E.constantes.TAB_LENGTH - 1) - (cur_rx % E.constantes.TAB_LENGTH);
cur_rx++;
if (cur_rx > rx) return cx;
}
return cx;
}
/**
* \fn bufferUpdatfrow(frow *row)
* \brief Copy content of \p row in \p row->render.
* */
void bufferUpdatfrow(frow *row) {
int i, i_render;
int tabs = 0;
// counting number of tabs
for (i = 0; i < row->size; ++i) {
tabs +=
(row->chars[i] == '\t'); /**< increment tabs of 1 if chars[i] is one. */
}
free(row->render);
row->render = malloc(row->size + tabs * (E.constantes.TAB_LENGTH - 1) +
1); /**< Tabs needs E.constantes.TAB_LENGTH chars so E.constantes.TAB_LENGTH - 1
more than the first already counted. */
// end of counting
i_render = 0;
for (i = 0; i < row->size; ++i) {
if (row->chars[i] == '\t') {
row->render[i_render++] = ' ';
while (i_render % E.constantes.TAB_LENGTH) {
row->render[i_render++] =
' '; /**< Addind the right amount of spaces for tabs */
}
} else {
row->render[i_render++] = row->chars[i];
}
}
row->render[i_render] = '\0'; // Don't forget the end of string character.
row->rsize = i_render;
}
void bufferInsertRow(struct buffer_t *buffer, int at, char *s, size_t len) {
if (at < 0 || at > buffer->numrows) {
return;
}
frow *tmp = (frow *)realloc(buffer->row, sizeof(frow) * (buffer->numrows + 1));
row_t *tmp = (row_t *)realloc(buffer->row, sizeof(row_t) * (buffer->numrows + 1));
if (!tmp) {
return;
}
buffer->row = tmp;
memmove(&buffer->row[at + 1], &buffer->row[at], sizeof(frow) * (buffer->numrows - at));
memmove(&buffer->row[at + 1], &buffer->row[at], sizeof(row_t) * (buffer->numrows - at));
buffer->row[at].size = len;
buffer->row[at].cap = len + 1;
buffer->row[at].chars = malloc(len + 1);
memcpy(buffer->row[at].chars, s, len);
buffer->row[at].chars[len] = '\0';
buffer->row[at].chars[len] = '\n';
buffer->row[at].rsize = 0;
buffer->row[at].render = NULL;
bufferUpdatfrow(&buffer->row[at]);
++buffer->numrows;
++buffer->dirty;
}
void bufferFrefrow(frow *row) {
free(row->render);
void bufferFreeRow(row_t *row) {
free(row->chars);
}
void bufferDelRow(struct buffer_t *buffer, int at) {
if (at < 0 || at >= buffer->numrows) {
return;
int editorRowCxToByte(const row_t *row, int cursor_x) {
int i = 0, col = 0;
while (col < cursor_x && i < row->size) {
int sl = utf8Seqlen((unsigned char)row->chars[i]);
if (sl < 1) sl = 1;
col++;
i += sl;
}
bufferFrefrow(&buffer->row[at]);
memmove(&buffer->row[at], &buffer->row[at + 1], sizeof(frow) * (buffer->numrows - at - 1));
--buffer->numrows;
++buffer->dirty;
return i;
}
/**
* \fn bufferRowInsertChar(frow *row, int at, int c)
* \fn editorRowInsertChar(erow *row, int at, int c)
* \param at Index of where we want to insert the char */
void bufferRowInsertChar(struct buffer_t *buffer, frow *row, int at, int c) {
void bufferRowInsertBytes(struct buffer_t *buffer, row_t *row, int at, char *src, int n) {
if (buffer->state == READ_ONLY)
return;
if (at < 0 || at > row->size) {
at = row->size;
if (row->size + n + 1 > row->cap) {
row->cap = (row->size + n + 1) * 2;
row->chars = realloc(row->chars, row->cap);
}
memmove(row->chars + at + n, row->chars + at, row->size - at);
memcpy(row->chars + at, src, n);
row->size += n;
row->chars = realloc(row->chars, row->size + 2);
memmove(&row->chars[at + 1], &row->chars[at], row->size - at + 1);
++row->size;
row->chars[at] = c;
bufferUpdatfrow(row);
++buffer->dirty;
}
void bufferRowAppendString(struct buffer_t *buffer, frow *row, char *s, size_t len) {
row->chars = realloc(row->chars, row->size + len + 1);
memcpy(&row->chars[row->size], s, len);
row->size += len;
row->chars[row->size] = '\0';
bufferUpdatfrow(row);
++buffer->dirty;
}
/**
* \fn bufferRowDelChar(struct bufferConfig *E, frow *frow, int at)
* \brief Delete the a char at the chosen position on the given row
* \param at Index of the char to delete
* \param row Row on operation is made */
void bufferRowDelchar(struct buffer_t *buffer, frow *row, int at) {
void bufferRowDelByte(struct buffer_t *buffer, row_t *row, int at, int n)
{
if (at < 0 || at >= row->size) {
return;
memmove(row->chars + at, row->chars + at + n, row->size - at - n);
row->size -= n;
row->chars[row->size] = '\0';
}
memmove(&row->chars[at], &row->chars[at + 1], row->size - at);
--row->size;
bufferUpdatfrow(row);
++buffer->dirty;
}
int editorRowCharCount(row_t *row)
{
int n = 0, i = 0;
while (i < row->size) {
int sl = utf8Seqlen((unsigned char)row->chars[i]);
if (sl < 1) sl = 1;
n++; i += sl;
}
return n;
}