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*)) {
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)
-/* 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 */
} 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 */
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,
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);