From dbec0316a6b9ee464b88c8074e4593b5428e75dc Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Tue, 6 Mar 2018 16:25:46 -0500 Subject: [PATCH] Added selection helper functions --- buf.c | 19 +++++++++++++++++++ edit.h | 24 ++++++++---------------- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/buf.c b/buf.c index af16a9d..95c34f5 100644 --- a/buf.c +++ b/buf.c @@ -29,6 +29,22 @@ static size_t pagealign(size_t sz) { return sz; } +static Sel selconvert(Buf* buf, Sel* sel) { + if (!sel) sel = &(buf->selection); + if (sel->end < sel->beg) + return (Sel){ .beg = sel->end, .end = sel->beg, .col = sel->col }; + else + return (Sel){ .beg = sel->beg, .end = sel->end, .col = sel->col }; +} + +static void selupdate(Buf* buf, Sel* dest, Sel* src) { + if (!dest) dest = &(buf->selection); + if (dest->end < dest->beg) + dest->beg = src->end, dest->end = src->beg, dest->col = src->col; + else + dest->beg = src->beg, dest->end = src->end, dest->col = src->col; +} + /******************************************************************************/ void buf_init(Buf* buf, void (*errfn)(char*)) { @@ -110,11 +126,14 @@ void buf_save(Buf* buf) { Rune buf_getc(Buf* buf, Sel* sel) { + Sel lsel = selconvert(buf, sel); return 0; } void buf_putc(Buf* buf, Sel* sel, Rune rune, int fmtopts) { + Sel lsel = selconvert(buf, sel); + selupdate(buf, sel, &lsel); } void buf_last(Buf* buf, Sel* sel) diff --git a/edit.h b/edit.h index 61ff34c..1dcca79 100644 --- a/edit.h +++ b/edit.h @@ -1,11 +1,3 @@ -/* Utility Functions - *****************************************************************************/ -/* Memory-mapped file representation */ -typedef struct { - uint8_t* buf; /* memory mapped byte buffer */ - size_t len; /* length of the buffer */ -} FMap; - /* Buffer management functions *****************************************************************************/ /* undo/redo list item */ @@ -26,6 +18,13 @@ typedef struct Log { } data; } Log; +/* cursor/selection representation */ +typedef struct { + size_t beg; + size_t end; + size_t col; +} Sel; + /* gap buffer main data structure */ typedef struct { char* path; /* the path to the open file */ @@ -42,15 +41,9 @@ typedef struct { bool expand_tabs; /* tracks current mode */ uint transid; /* tracks the last used transaction id for log entries */ void (*errfn)(char*); /* callback for error messages */ + Sel selection; } Buf; -/* cursor/selection representation */ -typedef struct { - size_t beg; - size_t end; - size_t col; -} Sel; - enum { LEFT = -1, RIGHT = +1, @@ -62,7 +55,6 @@ void buf_init(Buf* buf, void (*errfn)(char*)); void buf_load(Buf* buf, Sel* sel, char* path); void buf_reload(Buf* buf); void buf_save(Buf* buf); - Rune buf_getc(Buf* buf, Sel* sel); void buf_putc(Buf* buf, Sel* sel, Rune rune, int fmtopts); void buf_last(Buf* buf, Sel* sel); -- 2.51.0