temp commit
This commit is contained in:
+39
-40
@@ -6,44 +6,46 @@
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "include/terminal.h"
|
||||
#include "include/utf8.h"
|
||||
|
||||
extern struct editorConfig E;
|
||||
|
||||
|
||||
void bufferInsertRow(struct buffer_t *buffer, int at, char *s, size_t len) {
|
||||
if (at < 0 || at > buffer->numrows)
|
||||
return;
|
||||
|
||||
if (at < 0 || at > buffer->numrows) {
|
||||
|
||||
row_t *tmp = realloc(buffer->row, sizeof(row_t) * (buffer->numrows + 1));
|
||||
if (!tmp)
|
||||
return;
|
||||
}
|
||||
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(row_t) * (buffer->numrows - at));
|
||||
|
||||
buffer->row[at].size = len;
|
||||
buffer->row[at].cap = len + 1;
|
||||
/* Shift existing rows to make room at 'at' — no at++ */
|
||||
if (at < buffer->numrows) {
|
||||
memmove(&buffer->row[at + 1], &buffer->row[at],
|
||||
sizeof(row_t) * (buffer->numrows - at)); /* not -at+1 */
|
||||
}
|
||||
|
||||
buffer->row[at].size = len;
|
||||
buffer->row[at].cap = len + 1;
|
||||
buffer->row[at].chars = malloc(len + 1);
|
||||
if (!buffer->row[at].chars)
|
||||
return;
|
||||
memcpy(buffer->row[at].chars, s, len);
|
||||
buffer->row[at].chars[len] = '\n';
|
||||
buffer->row[at].chars[len] = '\0'; /* always NUL-terminate */
|
||||
|
||||
|
||||
++buffer->numrows;
|
||||
++buffer->dirty;
|
||||
buffer->numrows++;
|
||||
buffer->dirty++;
|
||||
}
|
||||
|
||||
void bufferFreeRow(row_t *row) {
|
||||
free(row->chars);
|
||||
}
|
||||
void bufferFreeRow(row_t *row) { free(row->chars); }
|
||||
|
||||
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;
|
||||
if (sl < 1)
|
||||
sl = 1;
|
||||
col++;
|
||||
i += sl;
|
||||
}
|
||||
@@ -54,7 +56,8 @@ int editorRowCxToByte(const row_t *row, int cursor_x) {
|
||||
* \fn editorRowInsertChar(erow *row, int at, int c)
|
||||
* \param at Index of where we want to insert the char */
|
||||
|
||||
void bufferRowInsertBytes(struct buffer_t *buffer, row_t *row, int at, char *src, int n) {
|
||||
void bufferRowInsertBytes(struct buffer_t *buffer, row_t *row, int at,
|
||||
char *src, int n) {
|
||||
if (buffer->state == READ_ONLY)
|
||||
return;
|
||||
if (row->size + n + 1 > row->cap) {
|
||||
@@ -65,35 +68,31 @@ void bufferRowInsertBytes(struct buffer_t *buffer, row_t *row, int at, char *src
|
||||
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;
|
||||
++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 bufferRowDelByte(struct buffer_t *buffer, row_t *row, int at, int n)
|
||||
{
|
||||
if (at < 0 || at >= row->size) {
|
||||
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 + n, row->size - at - n);
|
||||
row->size -= n;
|
||||
row->chars[row->size] = '\0';
|
||||
buffer->x -= n;
|
||||
++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;
|
||||
int editorRowCharCount(row_t *row, int x) {
|
||||
int n = 0, i = 0;
|
||||
while (i < x && i < row->size) {
|
||||
int sl = utf8Seqlen((unsigned char)row->chars[i]);
|
||||
if (sl < 1)
|
||||
sl = 1;
|
||||
n++;
|
||||
i += sl;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user