stash undo3

This commit is contained in:
2025-11-29 12:36:11 -08:00
parent 85e9f33613
commit c47287e96e
8 changed files with 96 additions and 31 deletions

View File

@@ -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)

View File

@@ -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)

7
abuf.c
View File

@@ -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)
{

2
abuf.h
View File

@@ -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);

View File

@@ -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);

2
core.h
View File

@@ -36,4 +36,4 @@ void kwrite(int fd, const char *buf, int len);
void die(const char *s);
#endif
#endif

59
undo.c
View File

@@ -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);

10
undo.h
View File

@@ -1,13 +1,15 @@
#include <stddef.h>
#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