cleaning up code

This commit is contained in:
2025-11-29 13:44:08 -08:00
parent 3b401e770e
commit 74637c8e83
7 changed files with 70 additions and 73 deletions

View File

@@ -10,6 +10,7 @@
#include "buffer.h" #include "buffer.h"
#include "core.h" #include "core.h"
#include "editor.h" #include "editor.h"
#include "undo.h"
#define NO_NAME "[No Name]" #define NO_NAME "[No Name]"
@@ -240,7 +241,8 @@ buffer_list_resize(void)
buffer **newlist = NULL; buffer **newlist = NULL;
if (editor.bufcount == editor.bufcap) { if (editor.bufcount == editor.bufcap) {
editor.bufcap = (size_t)cap_growth((int)editor.bufcap, (int)editor.bufcount + 1); editor.bufcap = (size_t)cap_growth((int)editor.bufcap,
(int)editor.bufcount + 1);
newlist = realloc(editor.buffers, sizeof(buffer *) * editor.bufcap); newlist = realloc(editor.buffers, sizeof(buffer *) * editor.bufcap);
assert(newlist != NULL); assert(newlist != NULL);
@@ -273,6 +275,8 @@ buffer_add_empty(void)
buf->mark_curx = 0; buf->mark_curx = 0;
buf->mark_cury = 0; buf->mark_cury = 0;
undo_tree_init(&buf->tree);
editor.buffers[editor.bufcount] = buf; editor.buffers[editor.bufcount] = buf;
idx = (int)editor.bufcount; idx = (int)editor.bufcount;
editor.bufcount++; editor.bufcount++;
@@ -280,20 +284,13 @@ buffer_add_empty(void)
} }
void
buffer_save_current(void)
{
/* No-op: editor no longer mirrors per-buffer fields */
(void)editor;
}
buffer * buffer *
buffer_current(void) buffer_current(void)
{ {
if (editor.bufcount == 0 || editor.curbuf >= editor.bufcount) { if (editor.bufcount == 0 || editor.curbuf >= editor.bufcount) {
return NULL; return NULL;
} }
return editor.buffers[editor.curbuf]; return editor.buffers[editor.curbuf];
} }
@@ -464,3 +461,4 @@ buffer_switch_by_name(void)
free(name); free(name);
} }

View File

@@ -19,9 +19,7 @@ typedef struct buffer {
} buffer; } buffer;
/* Access current buffer and convenient aliases for file-specific fields */
buffer *buffer_current(void); buffer *buffer_current(void);
#define CURBUF (buffer_current()) #define CURBUF (buffer_current())
#define EROW (CURBUF->row) #define EROW (CURBUF->row)
#define ENROWS (CURBUF->nrows) #define ENROWS (CURBUF->nrows)

2
core.c
View File

@@ -7,6 +7,7 @@
#include "core.h" #include "core.h"
#ifdef INCLUDE_STRNSTR #ifdef INCLUDE_STRNSTR
/* /*
* Find the first occurrence of find in s, where the search is limited to the * Find the first occurrence of find in s, where the search is limited to the
@@ -111,3 +112,4 @@ die(const char* s)
perror(s); perror(s);
exit(1); exit(1);
} }

View File

@@ -79,7 +79,6 @@ init_editor(void)
void void
reset_editor(void) reset_editor(void)
{ {
/* Reset the current buffer's contents/state. */
buffer *b = buffer_current(); buffer *b = buffer_current();
if (b == NULL) { if (b == NULL) {
return; return;
@@ -91,6 +90,7 @@ reset_editor(void)
} }
free(b->row); free(b->row);
} }
b->row = NULL; b->row = NULL;
b->nrows = 0; b->nrows = 0;
b->rowoffs = 0; b->rowoffs = 0;
@@ -107,3 +107,4 @@ reset_editor(void)
b->mark_curx = 0; b->mark_curx = 0;
b->mark_cury = 0; b->mark_cury = 0;
} }

5
main.c
View File

@@ -1719,12 +1719,10 @@ editor_openfile(void)
cur = buffer_current(); cur = buffer_current();
if (editor.bufcount == 1 && buffer_is_unnamed_and_empty(cur)) { if (editor.bufcount == 1 && buffer_is_unnamed_and_empty(cur)) {
open_file(filename); open_file(filename);
buffer_save_current();
} else { } else {
nb = buffer_add_empty(); nb = buffer_add_empty();
buffer_switch(nb); buffer_switch(nb);
open_file(filename); open_file(filename);
buffer_save_current();
} }
free(filename); free(filename);
@@ -2891,7 +2889,6 @@ main(int argc, char *argv[])
pending_line = 0; pending_line = 0;
} }
buffer_save_current();
first_loaded = 1; first_loaded = 1;
} else { } else {
nb = buffer_add_empty(); nb = buffer_add_empty();
@@ -2901,8 +2898,6 @@ main(int argc, char *argv[])
jump_to_position(0, pending_line - 1); jump_to_position(0, pending_line - 1);
pending_line = 0; pending_line = 0;
} }
buffer_save_current();
} }
} }

1
term.c
View File

@@ -84,3 +84,4 @@ get_winsz(size_t *rows, size_t *cols)
return 0; return 0;
} }

2
term.h
View File

@@ -3,6 +3,7 @@
#include "abuf.h" #include "abuf.h"
/* Terminal control/setup API */ /* Terminal control/setup API */
void enable_termraw(void); void enable_termraw(void);
void disable_termraw(void); void disable_termraw(void);
@@ -19,4 +20,5 @@ void display_clear(abuf *ab);
*/ */
int get_winsz(size_t *rows, size_t *cols); int get_winsz(size_t *rows, size_t *cols);
#endif /* KE_TERM_H */ #endif /* KE_TERM_H */