typedef struct erow -> erow

This commit is contained in:
2025-11-26 18:35:51 -08:00
parent 3800751bcf
commit 3ef6bab265

99
main.c
View File

@@ -85,7 +85,7 @@ struct abuf {
/* editor row */ /* editor row */
struct erow { typedef struct erow {
char *line; char *line;
char *render; char *render;
@@ -94,7 +94,7 @@ struct erow {
int cap; int cap;
int dirty; int dirty;
}; } erow;
/* /*
@@ -109,8 +109,8 @@ struct editor_t {
int mode; int mode;
int nrows; int nrows;
int rowoffs, coloffs; int rowoffs, coloffs;
struct erow *row; erow *row;
struct erow *killring; erow *killring;
int kill; /* KILL CHAIN (sounds metal) */ int kill; /* KILL CHAIN (sounds metal) */
int no_kill; /* don't kill in delete_row */ int no_kill; /* don't kill in delete_row */
char *filename; char *filename;
@@ -162,12 +162,12 @@ char nibble_to_hex(char c);
void swap_int(int *a, int *b); void swap_int(int *a, int *b);
/* editor rows */ /* editor rows */
int erow_render_to_cursor(struct erow *row, int cx); int erow_render_to_cursor(erow *row, int cx);
int erow_cursor_to_render(struct erow *row, int rx); int erow_cursor_to_render(erow *row, int rx);
int erow_init(struct erow *row, int len); int erow_init(erow *row, int len);
void erow_update(struct erow *row); void erow_update(erow *row);
void erow_insert(int at, char *s, int len); void erow_insert(int at, char *s, int len);
void erow_free(struct erow *row); void erow_free(erow *row);
/* kill ring, marking, etc... */ /* kill ring, marking, etc... */
@@ -196,8 +196,8 @@ void delete_next_word(void);
void find_prev_word(void); void find_prev_word(void);
void delete_prev_word(void); void delete_prev_word(void);
void delete_row(int at); void delete_row(int at);
void row_insert_ch(struct erow *row, int at, int16_t c); void row_insert_ch(erow *row, int at, int16_t c);
void row_delete_ch(struct erow *row, int at); void row_delete_ch(erow *row, int at);
void insertch(int16_t c); void insertch(int16_t c);
void deletech(uint8_t op); void deletech(uint8_t op);
void open_file(const char *filename); void open_file(const char *filename);
@@ -210,7 +210,7 @@ void editor_find_callback(char *query, int16_t c);
void editor_find(void); void editor_find(void);
char *editor_prompt(char*, void (*cb)(char*, int16_t)); char *editor_prompt(char*, void (*cb)(char*, int16_t));
void editor_openfile(void); void editor_openfile(void);
int first_nonwhitespace(struct erow *row); int first_nonwhitespace(erow *row);
void move_cursor_once(int16_t c, int interactive); void move_cursor_once(int16_t c, int interactive);
void move_cursor(int16_t c, int interactive); void move_cursor(int16_t c, int interactive);
void uarg_start(void); void uarg_start(void);
@@ -482,7 +482,7 @@ swap_int(int *a, int *b)
int int
erow_render_to_cursor(struct erow *row, int cx) erow_render_to_cursor(erow *row, int cx)
{ {
int rx = 0; int rx = 0;
size_t j = 0; size_t j = 0;
@@ -543,7 +543,7 @@ erow_render_to_cursor(struct erow *row, int cx)
int int
erow_cursor_to_render(struct erow *row, int rx) erow_cursor_to_render(erow *row, int rx)
{ {
int cur_rx = 0; int cur_rx = 0;
size_t j = 0; size_t j = 0;
@@ -602,7 +602,7 @@ erow_cursor_to_render(struct erow *row, int rx)
int int
erow_init(struct erow *row, int len) erow_init(erow *row, int len)
{ {
row->size = len; row->size = len;
row->rsize = 0; row->rsize = 0;
@@ -623,7 +623,7 @@ erow_init(struct erow *row, int len)
void void
erow_update(struct erow *row) erow_update(erow *row)
{ {
int i = 0, j; int i = 0, j;
int tabs = 0; int tabs = 0;
@@ -673,7 +673,7 @@ erow_update(struct erow *row)
void void
erow_insert(int at, char *s, int len) erow_insert(int at, char *s, int len)
{ {
struct erow row; erow row;
if (at < 0 || at > editor.nrows) { if (at < 0 || at > editor.nrows) {
return; return;
@@ -684,13 +684,13 @@ erow_insert(int at, char *s, int len)
row.line[len] = 0; row.line[len] = 0;
editor.row = realloc(editor.row, editor.row = realloc(editor.row,
sizeof(struct erow) * (editor.nrows + 1)); sizeof(erow) * (editor.nrows + 1));
assert(editor.row != NULL); assert(editor.row != NULL);
if (at < editor.nrows) { if (at < editor.nrows) {
memmove(&editor.row[at + 1], memmove(&editor.row[at + 1],
&editor.row[at], &editor.row[at],
sizeof(struct erow) * (editor.nrows - at)); sizeof(erow) * (editor.nrows - at));
} }
editor.row[at] = row; editor.row[at] = row;
@@ -700,7 +700,7 @@ erow_insert(int at, char *s, int len)
void void
erow_free(struct erow *row) erow_free(erow *row)
{ {
free(row->render); free(row->render);
free(row->line); free(row->line);
@@ -745,7 +745,7 @@ killring_yank(void)
void void
killring_start_with_char(unsigned char ch) killring_start_with_char(unsigned char ch)
{ {
struct erow *row = NULL; erow *row = NULL;
if (editor.killring != NULL) { if (editor.killring != NULL) {
erow_free(editor.killring); erow_free(editor.killring);
@@ -753,7 +753,7 @@ killring_start_with_char(unsigned char ch)
editor.killring = NULL; editor.killring = NULL;
} }
editor.killring = malloc(sizeof(struct erow)); editor.killring = malloc(sizeof(erow));
assert(editor.killring != NULL); assert(editor.killring != NULL);
assert(erow_init(editor.killring, 0) == 0); assert(erow_init(editor.killring, 0) == 0);
@@ -772,7 +772,7 @@ killring_start_with_char(unsigned char ch)
void void
killring_append_char(unsigned char ch) killring_append_char(unsigned char ch)
{ {
struct erow *row = NULL; erow *row = NULL;
if (editor.killring == NULL) { if (editor.killring == NULL) {
killring_start_with_char(ch); killring_start_with_char(ch);
@@ -797,7 +797,7 @@ killring_prepend_char(unsigned char ch)
return; return;
} }
struct erow *row = editor.killring; erow *row = editor.killring;
row->line = realloc(row->line, row->size + 2); row->line = realloc(row->line, row->size + 2);
assert(row->line != NULL); assert(row->line != NULL);
memmove(&row->line[1], &row->line[0], row->size + 1); memmove(&row->line[1], &row->line[0], row->size + 1);
@@ -965,8 +965,8 @@ indent_region(void)
void void
unindent_region(void) unindent_region(void)
{ {
int start_row, end_row, i, del; int start_row, end_row, i, del;
struct erow *row; erow *row;
if (!editor.mark_set) { if (!editor.mark_set) {
editor_set_status("Mark not set."); editor_set_status("Mark not set.");
@@ -1291,6 +1291,8 @@ delete_prev_word(void)
void void
delete_row(int at) delete_row(int at)
{ {
erow *row = NULL;
if (at < 0 || at >= editor.nrows) { if (at < 0 || at >= editor.nrows) {
return; return;
} }
@@ -1302,21 +1304,21 @@ delete_row(int at)
* newline itself and we must NOT also push the entire row here. * newline itself and we must NOT also push the entire row here.
*/ */
if (!editor.no_kill) { if (!editor.no_kill) {
struct erow *r = &editor.row[at]; row = &editor.row[at];
/* Start or continue the kill sequence based on editor.killing */ /* Start or continue the kill sequence based on editor.killing */
if (r->size > 0) { if (row->size > 0) {
/* push raw bytes of the line */ /* push raw bytes of the line */
if (!editor.kill) { if (!editor.kill) {
killring_start_with_char( killring_start_with_char(
(unsigned char)r->line[0]); (unsigned char)row->line[0]);
for (int i = 1; i < r->size; i++) { for (int i = 1; i < row->size; i++) {
killring_append_char( killring_append_char(
(unsigned char)r->line[i]); (unsigned char)row->line[i]);
} }
} else { } else {
for (int i = 0; i < r->size; i++) { for (int i = 0; i < row->size; i++) {
killring_append_char( killring_append_char(
(unsigned char)r->line[i]); (unsigned char)row->line[i]);
} }
} }
killring_append_char('\n'); killring_append_char('\n');
@@ -1334,14 +1336,14 @@ delete_row(int at)
erow_free(&editor.row[at]); erow_free(&editor.row[at]);
memmove(&editor.row[at], memmove(&editor.row[at],
&editor.row[at + 1], &editor.row[at + 1],
sizeof(struct erow) * (editor.nrows - at - 1)); sizeof(erow) * (editor.nrows - at - 1));
editor.nrows--; editor.nrows--;
editor.dirty++; editor.dirty++;
} }
void void
row_append_row(struct erow *row, char *s, int len) row_append_row(erow *row, char *s, int len)
{ {
row->line = realloc(row->line, row->size + len + 1); row->line = realloc(row->line, row->size + len + 1);
assert(row->line != NULL); assert(row->line != NULL);
@@ -1354,7 +1356,7 @@ row_append_row(struct erow *row, char *s, int len)
void void
row_insert_ch(struct erow *row, int at, int16_t c) row_insert_ch(erow *row, int at, int16_t c)
{ {
/* /*
* row_insert_ch just concerns itself with how to update a row. * row_insert_ch just concerns itself with how to update a row.
@@ -1375,7 +1377,7 @@ row_insert_ch(struct erow *row, int at, int16_t c)
void void
row_delete_ch(struct erow *row, int at) row_delete_ch(erow *row, int at)
{ {
if (at < 0 || at >= row->size) { if (at < 0 || at >= row->size) {
return; return;
@@ -1415,7 +1417,7 @@ insertch(int16_t c)
void void
deletech(uint8_t op) deletech(uint8_t op)
{ {
struct erow *row = NULL; erow *row = NULL;
unsigned char dch = 0; unsigned char dch = 0;
int prev = 0; int prev = 0;
@@ -1791,7 +1793,7 @@ editor_find_callback(char* query, int16_t c)
static int last_match = -1; /* row index of last match */ static int last_match = -1; /* row index of last match */
static int direction = 1; /* 1 = forward, -1 = backward */ static int direction = 1; /* 1 = forward, -1 = backward */
static char last_query[128] = {0}; /* remember last successful query */ static char last_query[128] = {0}; /* remember last successful query */
struct erow *row; erow *row;
int saved_cx = editor.curx; int saved_cx = editor.curx;
int saved_cy = editor.cury; int saved_cy = editor.cury;
size_t qlen = strlen(query); size_t qlen = strlen(query);
@@ -1920,7 +1922,7 @@ editor_openfile(void)
int int
first_nonwhitespace(struct erow *row) first_nonwhitespace(erow *row)
{ {
int pos; int pos;
wchar_t wc; wchar_t wc;
@@ -1969,8 +1971,8 @@ first_nonwhitespace(struct erow *row)
void void
move_cursor_once(int16_t c, int interactive) move_cursor_once(int16_t c, int interactive)
{ {
struct erow *row; erow *row;
int reps = 0; int reps = 0;
row = (editor.cury >= editor.nrows) ? NULL : &editor.row[editor.cury]; row = (editor.cury >= editor.nrows) ? NULL : &editor.row[editor.cury];
@@ -2089,7 +2091,7 @@ move_cursor(int16_t c, int interactive)
void void
newline(void) newline(void)
{ {
struct erow *row = NULL; erow *row = NULL;
if (editor.cury >= editor.nrows) { if (editor.cury >= editor.nrows) {
erow_insert(editor.cury, "", 0); erow_insert(editor.cury, "", 0);
@@ -2705,10 +2707,10 @@ draw_rows(struct abuf *ab)
{ {
assert(editor.cols >= 0); assert(editor.cols >= 0);
struct erow *row; erow *row;
char buf[editor.cols]; char buf[editor.cols];
int len, filerow, padding; int len, filerow, padding;
int y; int y;
for (y = 0; y < editor.rows; y++) { for (y = 0; y < editor.rows; y++) {
filerow = y + editor.rowoffs; filerow = y + editor.rowoffs;
@@ -2841,7 +2843,8 @@ draw_message_line(struct abuf *ab)
void void
scroll(void) scroll(void)
{ {
struct erow *row = NULL; erow *row = NULL;
editor.rx = 0; editor.rx = 0;
if (editor.cury < editor.nrows) { if (editor.cury < editor.nrows) {
row = &editor.row[editor.cury]; row = &editor.row[editor.cury];