]> git.mdlowis.com Git - archive/gapbuf.git/commitdiff
Added selection helper functions
authorMichael D. Lowis <mike.lowis@gentex.com>
Tue, 6 Mar 2018 21:25:46 +0000 (16:25 -0500)
committerMichael D. Lowis <mike.lowis@gentex.com>
Tue, 6 Mar 2018 21:25:46 +0000 (16:25 -0500)
buf.c
edit.h

diff --git a/buf.c b/buf.c
index af16a9d02bb1fd9d5ddc425068319ad652412e8f..95c34f5e21a129505dedbf4cfd08390589daa9b0 100644 (file)
--- 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 61ff34c3b56bb598465a0937fd1c21ac2d318668..1dcca7961b540e7d769004bc6a713649265adc56 100644 (file)
--- 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);