Major codebase cleanup and overhaul.
+ editor removes per-buffer fields. + switching from internal use of 'int' to 'size_t'. + deleting old code + double checking relevancy of comments. A lot has changed in 5 years, even more so in the past week. + fixing a few vestigal memory errors from the overhaul. + fixing search behavior
This commit is contained in:
187
buffer.c
187
buffer.c
@@ -35,12 +35,13 @@ static int
|
||||
buffer_find_exact_by_name(const char *name)
|
||||
{
|
||||
buffer *b = NULL;
|
||||
size_t i = 0;
|
||||
|
||||
if (name == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < editor.bufcount; i++) {
|
||||
for (i = 0; i < editor.bufcount; i++) {
|
||||
b = editor.buffers[i];
|
||||
const char *full = b->filename;
|
||||
const char *base = buf_basename(full);
|
||||
@@ -66,11 +67,12 @@ static int
|
||||
buffer_collect_prefix_matches(const char *prefix, int *out_idx, const int max_out)
|
||||
{
|
||||
buffer *b = NULL;
|
||||
int count = 0;
|
||||
int matched = 0;
|
||||
int count = 0;
|
||||
int matched = 0;
|
||||
size_t i = 0;
|
||||
size_t plen = (prefix ? strlen(prefix) : 0);
|
||||
|
||||
for (int i = 0; i < editor.bufcount; i++) {
|
||||
for (i = 0; i < editor.bufcount; i++) {
|
||||
matched = 0;
|
||||
b = editor.buffers[i];
|
||||
|
||||
@@ -206,49 +208,6 @@ buffer_switch_prompt_cb(char *buf, const int16_t key)
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
buffer_bind_to_editor(const buffer *b)
|
||||
{
|
||||
if (b == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
editor.curx = b->curx;
|
||||
editor.cury = b->cury;
|
||||
editor.rx = b->rx;
|
||||
editor.nrows = b->nrows;
|
||||
editor.rowoffs = b->rowoffs;
|
||||
editor.coloffs = b->coloffs;
|
||||
editor.row = b->row;
|
||||
editor.filename = b->filename;
|
||||
editor.dirty = b->dirty;
|
||||
editor.mark_set = b->mark_set;
|
||||
editor.mark_curx = b->mark_curx;
|
||||
editor.mark_cury = b->mark_cury;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
buffer_extract_from_editor(buffer *b)
|
||||
{
|
||||
if (b == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
b->curx = editor.curx;
|
||||
b->cury = editor.cury;
|
||||
b->rx = editor.rx;
|
||||
b->nrows = editor.nrows;
|
||||
b->rowoffs = editor.rowoffs;
|
||||
b->coloffs = editor.coloffs;
|
||||
b->row = editor.row;
|
||||
b->filename = editor.filename;
|
||||
b->dirty = editor.dirty;
|
||||
b->mark_set = editor.mark_set;
|
||||
b->mark_curx = editor.mark_curx;
|
||||
b->mark_cury = editor.mark_cury;
|
||||
}
|
||||
|
||||
const char *
|
||||
buffer_name(buffer *b)
|
||||
{
|
||||
@@ -263,12 +222,12 @@ buffer_name(buffer *b)
|
||||
void
|
||||
buffers_init(void)
|
||||
{
|
||||
int idx = 0;
|
||||
int idx = 0;
|
||||
|
||||
editor.buffers = NULL;
|
||||
editor.bufcount = 0;
|
||||
editor.curbuf = -1;
|
||||
editor.bufcap = 0;
|
||||
editor.buffers = NULL;
|
||||
editor.bufcount = 0;
|
||||
editor.curbuf = 0;
|
||||
editor.bufcap = 0;
|
||||
|
||||
idx = buffer_add_empty();
|
||||
buffer_switch(idx);
|
||||
@@ -278,28 +237,28 @@ buffers_init(void)
|
||||
static void
|
||||
buffer_list_resize(void)
|
||||
{
|
||||
buffer **newlist = NULL;
|
||||
buffer **newlist = NULL;
|
||||
|
||||
if (editor.bufcount == (int)editor.bufcap) {
|
||||
editor.bufcap = cap_growth((int)editor.bufcap, editor.bufcount + 1);
|
||||
if (editor.bufcount == editor.bufcap) {
|
||||
editor.bufcap = (size_t)cap_growth((int)editor.bufcap, (int)editor.bufcount + 1);
|
||||
|
||||
newlist = realloc(editor.buffers, sizeof(buffer *) * editor.bufcap);
|
||||
assert(newlist != NULL);
|
||||
editor.buffers = newlist;
|
||||
}
|
||||
newlist = realloc(editor.buffers, sizeof(buffer *) * editor.bufcap);
|
||||
assert(newlist != NULL);
|
||||
editor.buffers = newlist;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
buffer_add_empty(void)
|
||||
{
|
||||
buffer *buf = NULL;
|
||||
int idx = 0;
|
||||
buffer *buf = NULL;
|
||||
int idx = 0;
|
||||
|
||||
buffer_list_resize();
|
||||
buffer_list_resize();
|
||||
|
||||
buf = calloc(1, sizeof(buffer));
|
||||
assert(buf != NULL);
|
||||
buf = calloc(1, sizeof(buffer));
|
||||
assert(buf != NULL);
|
||||
|
||||
buf->curx = 0;
|
||||
buf->cury = 0;
|
||||
@@ -314,34 +273,28 @@ buffer_add_empty(void)
|
||||
buf->mark_curx = 0;
|
||||
buf->mark_cury = 0;
|
||||
|
||||
editor.buffers[editor.bufcount] = buf;
|
||||
idx = editor.bufcount;
|
||||
editor.bufcount++;
|
||||
return idx;
|
||||
editor.buffers[editor.bufcount] = buf;
|
||||
idx = (int)editor.bufcount;
|
||||
editor.bufcount++;
|
||||
return idx;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
buffer_save_current(void)
|
||||
{
|
||||
buffer *b = NULL;
|
||||
|
||||
if (editor.curbuf < 0 || editor.curbuf >= editor.bufcount) {
|
||||
return;
|
||||
}
|
||||
|
||||
b = editor.buffers[editor.curbuf];
|
||||
buffer_extract_from_editor(b);
|
||||
/* No-op: editor no longer mirrors per-buffer fields */
|
||||
(void)editor;
|
||||
}
|
||||
|
||||
|
||||
buffer *
|
||||
buffer_current(void)
|
||||
{
|
||||
if (editor.curbuf < 0 || editor.curbuf >= editor.bufcount) {
|
||||
return NULL;
|
||||
}
|
||||
return editor.buffers[editor.curbuf];
|
||||
if (editor.bufcount == 0 || editor.curbuf >= editor.bufcount) {
|
||||
return NULL;
|
||||
}
|
||||
return editor.buffers[editor.curbuf];
|
||||
}
|
||||
|
||||
|
||||
@@ -375,75 +328,69 @@ buffer_is_unnamed_and_empty(const buffer *b)
|
||||
void
|
||||
buffer_switch(const int idx)
|
||||
{
|
||||
buffer *b = NULL;
|
||||
buffer *b = NULL;
|
||||
|
||||
if (idx < 0 || idx >= editor.bufcount) {
|
||||
return;
|
||||
}
|
||||
if (idx < 0 || (size_t)idx >= editor.bufcount) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (editor.curbuf == idx) {
|
||||
return;
|
||||
}
|
||||
if (editor.curbuf == (size_t)idx) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (editor.curbuf >= 0) {
|
||||
buffer_save_current();
|
||||
}
|
||||
|
||||
b = editor.buffers[idx];
|
||||
buffer_bind_to_editor(b);
|
||||
editor.curbuf = idx;
|
||||
editor.dirtyex = 1;
|
||||
editor_set_status("Switched to buffer %d: %s", editor.curbuf, buffer_name(b));
|
||||
b = editor.buffers[idx];
|
||||
editor.curbuf = (size_t)idx;
|
||||
editor.dirtyex = 1;
|
||||
editor_set_status("Switched to buffer %d: %s", editor.curbuf, buffer_name(b));
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
buffer_next(void)
|
||||
{
|
||||
int idx = 0;
|
||||
size_t idx = 0;
|
||||
|
||||
if (editor.bufcount <= 1) {
|
||||
return;
|
||||
}
|
||||
if (editor.bufcount <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
idx = (editor.curbuf + 1) % editor.bufcount;
|
||||
buffer_switch(idx);
|
||||
idx = (editor.curbuf + 1) % editor.bufcount;
|
||||
buffer_switch((int)idx);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
buffer_prev(void)
|
||||
{
|
||||
int idx = 0;
|
||||
size_t idx = 0;
|
||||
|
||||
if (editor.bufcount <= 1) {
|
||||
return;
|
||||
}
|
||||
if (editor.bufcount <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
idx = (editor.curbuf - 1 + editor.bufcount) % editor.bufcount;
|
||||
buffer_switch(idx);
|
||||
idx = (editor.curbuf == 0) ? (editor.bufcount - 1) : (editor.curbuf - 1);
|
||||
buffer_switch((int)idx);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
buffer_close_current(void)
|
||||
{
|
||||
buffer *b = NULL;
|
||||
int closing = 0;
|
||||
int target = 0;
|
||||
int nb = 0;
|
||||
buffer *b = NULL;
|
||||
size_t closing = 0;
|
||||
int target = 0;
|
||||
int nb = 0;
|
||||
|
||||
/* sanity check */
|
||||
if (editor.curbuf < 0 || editor.curbuf >= editor.bufcount) {
|
||||
if (editor.bufcount == 0 || editor.curbuf >= editor.bufcount) {
|
||||
editor_set_status("No buffer to close.");
|
||||
return;
|
||||
}
|
||||
|
||||
closing = editor.curbuf;
|
||||
|
||||
target = -1;
|
||||
if (editor.bufcount > 1) {
|
||||
target = (closing - 1 >= 0) ? (closing - 1) : (closing + 1);
|
||||
target = (closing > 0) ? (int) (closing - 1) : (int) (closing + 1);
|
||||
buffer_switch(target);
|
||||
} else {
|
||||
nb = buffer_add_empty();
|
||||
@@ -453,7 +400,7 @@ buffer_close_current(void)
|
||||
b = editor.buffers[closing];
|
||||
if (b) {
|
||||
if (b->row) {
|
||||
for (int i = 0; i < b->nrows; i++) {
|
||||
for (size_t i = 0; i < b->nrows; i++) {
|
||||
ab_free(&b->row[i]);
|
||||
}
|
||||
free(b->row);
|
||||
@@ -466,11 +413,11 @@ buffer_close_current(void)
|
||||
}
|
||||
|
||||
memmove(&editor.buffers[closing], &editor.buffers[closing + 1],
|
||||
sizeof(buffer *) * (editor.bufcount - closing - 1));
|
||||
sizeof(buffer *) * (editor.bufcount - closing - 1));
|
||||
|
||||
editor.bufcount--;
|
||||
if (editor.bufcount == 0) {
|
||||
editor.curbuf = -1;
|
||||
editor.curbuf = 0;
|
||||
} else {
|
||||
if (editor.curbuf > closing) {
|
||||
editor.curbuf--;
|
||||
@@ -479,7 +426,7 @@ buffer_close_current(void)
|
||||
|
||||
editor.dirtyex = 1;
|
||||
editor_set_status("Closed buffer. Now on %s",
|
||||
buffer_name(editor.buffers[editor.curbuf]));
|
||||
buffer_name(editor.buffers[editor.curbuf]));
|
||||
}
|
||||
|
||||
|
||||
@@ -512,7 +459,7 @@ buffer_switch_by_name(void)
|
||||
if (idx >= 0) {
|
||||
buffer_switch(idx);
|
||||
} else {
|
||||
editor_set_status("No such buffer: %s", name);
|
||||
editor_set_status("No open buffer: %s", name);
|
||||
}
|
||||
|
||||
free(name);
|
||||
|
||||
Reference in New Issue
Block a user