add support for resetting the editor

This commit is contained in:
2025-11-21 21:08:53 -08:00
parent d13176070f
commit 1cb7625812

View File

@@ -152,7 +152,70 @@ struct editor_t {
int dirtyex;
char msg[80];
time_t msgtm;
} editor;
} editor = {
.cols = 0,
.rows = 0,
.curx = 0,
.cury = 0,
.mode = 0,
.nrows = 0,
.rowoffs = 0,
.coloffs = 0,
.row = NULL,
.filename = NULL,
.dirty = 0,
.dirtyex = 0,
};
/*
* init_editor should set up the global editor struct.
*/
void
init_editor(void)
{
editor.cols = 0;
editor.rows = 0;
if (get_winsz(&editor.rows, &editor.cols) == -1) {
die("can't get window size");
}
editor.rows--; /* status bar */
editor.rows--; /* message line */
editor.curx = editor.cury = 0;
editor.rx = 0;
editor.nrows = 0;
editor.rowoffs = editor.coloffs = 0;
editor.row = NULL;
editor.msg[0] = '\0';
editor.msgtm = 0;
editor.dirty = 0;
}
/*
* reset_editor presumes that editor has been initialized.
*/
void
reset_editor(void)
{
for (int i = 0; i < editor.nrows; i++) {
erow_free(&editor.row[i]);
}
free(editor.row);
if (editor.filename != NULL) {
free(editor.filename);
editor.filename = NULL;
}
init_editor();
}
void
@@ -503,6 +566,8 @@ open_file(const char *filename)
editor.filename = strdup(filename);
assert(editor.filename != NULL);
reset_editor();
editor.dirty = 0;
if ((fp = fopen(filename, "r")) == NULL) {
if (errno == ENOENT) {
@@ -1408,35 +1473,6 @@ loop(void)
}
/*
* init_editor should set up the global editor struct.
*/
void
init_editor(void)
{
editor.cols = 0;
editor.rows = 0;
if (get_winsz(&editor.rows, &editor.cols) == -1) {
die("can't get window size");
}
editor.rows--; /* status bar */
editor.rows--; /* message line */
editor.curx = editor.cury = 0;
editor.rx = 0;
editor.nrows = 0;
editor.rowoffs = editor.coloffs = 0;
editor.row = NULL;
editor.msg[0] = '\0';
editor.msgtm = 0;
editor.dirty = 0;
}
int
main(int argc, char *argv[])
{