diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f34406..0c69498 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ project(ke C) # Specify C language explicitly set(CMAKE_C_STANDARD 99) set(KE_VERSION "2.1.1") -set(CMAKE_C_FLAGS "-Wall -Wextra -pedantic -Wshadow -Werror -std=c99 -g -Werror=stringop-truncation") +set(CMAKE_C_FLAGS "-Wall -Wextra -pedantic -Wshadow -Werror -std=c99 -g") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_DEFAULT_SOURCE -D_XOPEN_SOURCE") # Optionally enable AddressSanitizer (ASan) @@ -19,16 +19,28 @@ endif() include(GNUInstallDirs) -# Add executable -add_executable(ke +set(SOURCES + core.c abuf.c term.c buffer.c editor.c - core.c - core.h + undo.c main.c ) + +set(HEADERS + core.h + abuf.h + term.h + buffer.h + editor.h + undo.h +) + + +# Add executable +add_executable(ke ${SOURCES} ${HEADERS}) target_compile_definitions(ke PRIVATE KE_VERSION="ke version ${KE_VERSION}") install(TARGETS ke RUNTIME DESTINATION bin) install(FILES ke.1 TYPE MAN) diff --git a/Makefile b/Makefile index 92f718a..4c8771b 100644 --- a/Makefile +++ b/Makefile @@ -11,8 +11,8 @@ LDFLAGS := -fsanitize=address all: $(TARGET) test.txt -SRCS := main.c abuf.c term.c buffer.c editor.c core.c -HDRS := abuf.h term.h buffer.h editor.h core.h +SRCS := main.c abuf.c core.c term.c buffer.c editor.c undo.c +HDRS := abuf.h core.h term.h buffer.h editor.h undo.h $(TARGET): $(SRCS) $(CC) $(CFLAGS) -o $(TARGET) $(SRCS) $(LDFLAGS) diff --git a/abuf.c b/abuf.c index a1f0dd5..c302cb0 100644 --- a/abuf.c +++ b/abuf.c @@ -71,6 +71,13 @@ ab_append(abuf *buf, const char *s, size_t len) } +void +ab_append_ab(abuf *buf, const abuf *other) +{ + ab_append(buf, other->b, other->size); +} + + void ab_prependch(abuf *buf, const char c) { diff --git a/abuf.h b/abuf.h index 90d87f3..96fcec6 100644 --- a/abuf.h +++ b/abuf.h @@ -22,8 +22,10 @@ void ab_init_cap(abuf *buf, size_t cap); void ab_resize(abuf *buf, size_t cap); void ab_appendch(abuf *buf, char c); void ab_append(abuf *buf, const char *s, size_t len); +void ab_append_ab(abuf *buf, const abuf *other); void ab_prependch(abuf *buf, const char c); void ab_prepend(abuf *buf, const char *s, const size_t len); +void ab_prepend_ab(abuf *buf, const abuf *other); void ab_free(abuf *buf); diff --git a/buffer.h b/buffer.h index 1289801..0d6ea6c 100644 --- a/buffer.h +++ b/buffer.h @@ -2,20 +2,23 @@ #define KE_BUFFER_H #include "abuf.h" +#include "undo.h" typedef struct buffer { - size_t curx, cury; - size_t rx; - size_t nrows; - size_t rowoffs, coloffs; - abuf *row; - char *filename; - int dirty; - int mark_set; - size_t mark_curx, mark_cury; + size_t curx, cury; + size_t rx; + size_t nrows; + size_t rowoffs, coloffs; + abuf *row; + char *filename; + int dirty; + int mark_set; + size_t mark_curx, mark_cury; + undo_tree tree; } buffer; + /* Access current buffer and convenient aliases for file-specific fields */ buffer *buffer_current(void); diff --git a/core.h b/core.h index f2d0c3b..0e400ca 100644 --- a/core.h +++ b/core.h @@ -36,4 +36,4 @@ void kwrite(int fd, const char *buf, int len); void die(const char *s); -#endif \ No newline at end of file +#endif diff --git a/undo.c b/undo.c index 50551c8..fa07057 100644 --- a/undo.c +++ b/undo.c @@ -27,8 +27,6 @@ undo_node_new(undo_kind kind) void undo_node_free(undo_node *node) { - undo_node *next = NULL; - if (node == NULL) { return; } @@ -100,15 +98,56 @@ undo_begin(undo_tree *tree, undo_kind kind) void undo_prepend(undo_tree *tree, abuf *buf) { + assert(tree != NULL); + assert(tree->pending != NULL); + + ab_prepend_ab(tree->pending->text, buf); +} + + +void +undo_append(undo_tree *tree, abuf *buf) +{ + assert(tree != NULL); + assert(tree->pending != NULL); + + ab_append_ab(tree->pending->text, buf); +} + + +void +undo_prependch(undo_tree *tree, char c) +{ + assert(tree != NULL); + assert(tree->pending != NULL); + + ab_prependch(tree->pending->text, c); +} + + +void +undo_appendch(undo_tree *tree, char c) +{ + assert(tree != NULL); + assert(tree->pending != NULL); + + ab_appendch(tree->pending->text, c); +} + + +void +undo_commit(undo_tree *tree) +{ + assert(tree != NULL); + + if (tree->pending == NULL) { + return; + } + } -void undo_append(undo_tree *tree, abuf *buf); -void undo_prependch(undo_tree *tree, char c); -void undo_appendch(undo_tree *tree, char c); -void undo_commit(undo_tree *tree); -void undo_apply(struct editor *editor); -void editor_undo(undo_tree *tree); -void editor_redo(undo_tree *tree); - +void undo_apply(struct buffer *buf); +void editor_undo(struct buffer *buf); +void editor_redo(struct buffer *buf); diff --git a/undo.h b/undo.h index f33e5f5..198e47e 100644 --- a/undo.h +++ b/undo.h @@ -1,13 +1,15 @@ #include #include "abuf.h" -#include "editor.h" #ifndef KE_UNDO_H #define KE_UNDO_H +struct buffer; + + typedef enum undo_kind { UNDO_INSERT = 1 << 0, UNDO_UNKNOWN = 1 << 1, @@ -42,9 +44,9 @@ void undo_append(undo_tree *tree, abuf *buf); void undo_prependch(undo_tree *tree, char c); void undo_appendch(undo_tree *tree, char c); void undo_commit(undo_tree *tree); -void undo_apply(struct editor *editor); -void editor_undo(undo_tree *tree); -void editor_redo(undo_tree *tree); +void undo_apply(struct buffer *buf); +void editor_undo(struct buffer *buf); +void editor_redo(struct buffer *buf); #endif