diff --git a/buffer.c b/buffer.c index 7f17f14..a19b312 100644 --- a/buffer.c +++ b/buffer.c @@ -10,6 +10,7 @@ #include "buffer.h" #include "core.h" #include "editor.h" +#include "undo.h" #define NO_NAME "[No Name]" @@ -222,12 +223,12 @@ buffer_name(buffer *b) void buffers_init(void) { - int idx = 0; + int idx = 0; - editor.buffers = NULL; - editor.bufcount = 0; - editor.curbuf = 0; - editor.bufcap = 0; + editor.buffers = NULL; + editor.bufcount = 0; + editor.curbuf = 0; + editor.bufcap = 0; idx = buffer_add_empty(); buffer_switch(idx); @@ -237,28 +238,29 @@ buffers_init(void) static void buffer_list_resize(void) { - buffer **newlist = NULL; + buffer **newlist = NULL; - if (editor.bufcount == editor.bufcap) { - editor.bufcap = (size_t)cap_growth((int)editor.bufcap, (int)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; @@ -273,28 +275,23 @@ buffer_add_empty(void) buf->mark_curx = 0; buf->mark_cury = 0; - editor.buffers[editor.bufcount] = buf; - idx = (int)editor.bufcount; - editor.bufcount++; - return idx; -} + undo_tree_init(&buf->tree); - -void -buffer_save_current(void) -{ - /* No-op: editor no longer mirrors per-buffer fields */ - (void)editor; + editor.buffers[editor.bufcount] = buf; + idx = (int)editor.bufcount; + editor.bufcount++; + return idx; } buffer * buffer_current(void) { - if (editor.bufcount == 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]; } @@ -328,58 +325,58 @@ buffer_is_unnamed_and_empty(const buffer *b) void buffer_switch(const int idx) { - buffer *b = NULL; + buffer *b = NULL; - if (idx < 0 || (size_t)idx >= editor.bufcount) { - return; - } + if (idx < 0 || (size_t)idx >= editor.bufcount) { + return; + } - if (editor.curbuf == (size_t)idx) { - return; - } + if (editor.curbuf == (size_t)idx) { + return; + } - 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)); + 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) { - size_t idx = 0; - - if (editor.bufcount <= 1) { - return; - } - - idx = (editor.curbuf + 1) % editor.bufcount; - buffer_switch((int)idx); + size_t idx = 0; + + if (editor.bufcount <= 1) { + return; + } + + idx = (editor.curbuf + 1) % editor.bufcount; + buffer_switch((int)idx); } void buffer_prev(void) { - size_t idx = 0; - - if (editor.bufcount <= 1) { - return; - } - - idx = (editor.curbuf == 0) ? (editor.bufcount - 1) : (editor.curbuf - 1); - buffer_switch((int)idx); + size_t idx = 0; + + if (editor.bufcount <= 1) { + return; + } + + idx = (editor.curbuf == 0) ? (editor.bufcount - 1) : (editor.curbuf - 1); + buffer_switch((int)idx); } void buffer_close_current(void) { - buffer *b = NULL; - size_t 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.bufcount == 0 || editor.curbuf >= editor.bufcount) { @@ -464,3 +461,4 @@ buffer_switch_by_name(void) free(name); } + diff --git a/buffer.h b/buffer.h index 0d6ea6c..bad853c 100644 --- a/buffer.h +++ b/buffer.h @@ -19,9 +19,7 @@ typedef struct 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 EROW (CURBUF->row) #define ENROWS (CURBUF->nrows) diff --git a/core.c b/core.c index a7f89b3..64d4627 100644 --- a/core.c +++ b/core.c @@ -7,6 +7,7 @@ #include "core.h" + #ifdef INCLUDE_STRNSTR /* * 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); exit(1); } + diff --git a/editor.c b/editor.c index 1083e54..875b2d9 100644 --- a/editor.c +++ b/editor.c @@ -16,7 +16,7 @@ * Global editor instance */ struct editor editor = { - .cols = 0, + .cols = 0, .rows = 0, .mode = 0, .killring = NULL, @@ -79,7 +79,6 @@ init_editor(void) void reset_editor(void) { - /* Reset the current buffer's contents/state. */ buffer *b = buffer_current(); if (b == NULL) { return; @@ -91,6 +90,7 @@ reset_editor(void) } free(b->row); } + b->row = NULL; b->nrows = 0; b->rowoffs = 0; @@ -107,3 +107,4 @@ reset_editor(void) b->mark_curx = 0; b->mark_cury = 0; } + diff --git a/main.c b/main.c index f8b84e6..3bb4d20 100644 --- a/main.c +++ b/main.c @@ -1719,12 +1719,10 @@ editor_openfile(void) cur = buffer_current(); if (editor.bufcount == 1 && buffer_is_unnamed_and_empty(cur)) { open_file(filename); - buffer_save_current(); } else { nb = buffer_add_empty(); buffer_switch(nb); open_file(filename); - buffer_save_current(); } free(filename); @@ -2891,7 +2889,6 @@ main(int argc, char *argv[]) pending_line = 0; } - buffer_save_current(); first_loaded = 1; } else { nb = buffer_add_empty(); @@ -2901,8 +2898,6 @@ main(int argc, char *argv[]) jump_to_position(0, pending_line - 1); pending_line = 0; } - - buffer_save_current(); } } diff --git a/term.c b/term.c index f7ff999..ed74a47 100644 --- a/term.c +++ b/term.c @@ -84,3 +84,4 @@ get_winsz(size_t *rows, size_t *cols) return 0; } + diff --git a/term.h b/term.h index f2d4953..7da0f51 100644 --- a/term.h +++ b/term.h @@ -3,6 +3,7 @@ #include "abuf.h" + /* Terminal control/setup API */ void enable_termraw(void); void disable_termraw(void); @@ -19,4 +20,5 @@ void display_clear(abuf *ab); */ int get_winsz(size_t *rows, size_t *cols); + #endif /* KE_TERM_H */