void buf_save(Buf* buf) {
if (0 == buf_end(buf)) return;
-
char* wptr;
long fd, nwrite = 0, towrite = 0;
if (buf->path && (fd = open(buf->path, O_WRONLY|O_CREAT, 0644)) >= 0) {
/******************************************************************************/
-static Sel getsel(Buf* buf, Sel* p_sel) {
+static Sel getsel(Buf* buf) {
size_t temp;
- Sel sel = (p_sel ? *p_sel : buf->selection);
+ Sel sel = buf->selection;
if (sel.end < sel.beg)
temp = sel.beg, sel.beg = sel.end, sel.end = temp;
return sel;
}
-static void setsel(Buf* buf, Sel* p_sel, Sel* p_newsel) {
- buf->selection = *p_newsel;
-}
-
static void putb(Buf* buf, char b, Sel* p_sel) {
syncgap(buf, p_sel->end);
*(buf->gapstart++) = b;
void buf_puts(Buf* buf, char* s) {
buf_del(buf);
- Sel sel = getsel(buf, NULL);
- while (s && *s) putb(buf, *(s++), &sel);
- setsel(buf, NULL, &sel);
+ while (s && *s) putb(buf, *(s++), &(buf->selection));
}
int buf_getc(Buf* buf) {
- Sel sel = getsel(buf, NULL);
- return buf_getrat(buf, sel.end);
+ return buf_getrat(buf, buf->selection.end);
}
char* buf_gets(Buf* buf) {
- Sel sel = getsel(buf, NULL);
+ Sel sel = getsel(buf);
size_t nbytes = sel.end - sel.beg;
char* str = malloc(nbytes+1);
for (size_t i = 0; i < nbytes; i++)
}
void buf_del(Buf* buf) {
- Sel sel = getsel(buf, NULL);
+ Sel sel = getsel(buf);
size_t nbytes = sel.end - sel.beg;
if (nbytes > 0) {
//char* str = buf_gets(buf, &sel);
syncgap(buf, sel.beg);
buf->gapend += nbytes;
sel.end = sel.beg;
- setsel(buf, NULL, &sel);
+ buf->selection = sel;
// update log here
// free(str);
}
}
void buf_lastins(Buf* buf) {
- Sel sel = getsel(buf, NULL);
+ Sel sel = getsel(buf);
// Set selection to last inserted text
- setsel(buf, NULL, &sel);
+ buf->selection = sel;
}
static void log_clear(Log** list) {
}
void buf_selline(Buf* buf) {
- Sel sel = getsel(buf, NULL);
+ Sel sel = getsel(buf);
sel.beg = buf_bol(buf, sel.end);
sel.end = buf_eol(buf, sel.end);
sel.end = buf_byrune(buf, sel.end, RIGHT);
- setsel(buf, NULL, &sel);
+ buf->selection = sel;
}
void buf_selword(Buf* buf, bool (*isword)(Rune)) {
- Sel sel = getsel(buf, NULL);
+ Sel sel = getsel(buf);
for (; isword(buf_getrat(buf, sel.beg-1)); sel.beg--);
for (; isword(buf_getrat(buf, sel.end)); sel.end++);
- setsel(buf, NULL, &sel);
+ buf->selection = sel;
}
void buf_selblock(Buf* buf, Rune first, Rune last) {
- Sel sel = getsel(buf, NULL);
+ Sel sel = getsel(buf);
int balance = 0, dir;
size_t beg, end = sel.end;
/* update the passed in selection */
if (end > beg) beg++; else end++;
- sel.beg = beg, sel.end = end;
- setsel(buf, NULL, &sel);
+ buf->selection.beg = beg, buf->selection.end = end;
}
void buf_selall(Buf* buf) {
- Sel sel = getsel(buf, NULL);
- sel = (Sel){ .beg = 0, .end = buf_end(buf) };
- setsel(buf, NULL, &sel);
+ buf->selection = (Sel){ .beg = 0, .end = buf_end(buf) };
}
void buf_selctx(Buf* buf, bool (*isword)(Rune)) {
- Sel sel = getsel(buf, NULL);
- size_t bol = buf_bol(buf, sel.end);
+ size_t bol = buf_bol(buf, buf->selection.end);
Rune r = buf_getc(buf);
if (r == '(' || r == ')')
buf_selblock(buf, '(', ')');
buf_selblock(buf, '[', ']');
else if (r == '{' || r == '}')
buf_selblock(buf, '{', '}');
- else if (sel.end == bol || r == '\n')
+ else if (buf->selection.end == bol || r == '\n')
buf_selline(buf);
else if (risword(r))
buf_selword(buf, isword);
size_t buf_byrune(Buf* buf, size_t pos, int count) {
int move = (count < 0 ? -1 : 1);
count *= move; // remove the sign if there is one
- for (; count > 0; count--) {
- if (move < 0) {
- if (pos > 0) pos--;
- } else {
- if (pos < buf_end(buf)) pos++;
- }
- }
+ for (; count > 0; count--)
+ if (pos > 0 || pos < buf_end(buf))
+ pos += move;
return pos;
}
for (; count > 0; count--) {
if (move < 0) {
if (pos > buf_eol(buf, 0))
- pos = buf_bol(buf, buf_bol(buf, pos)-1);
+ pos = buf_bol(buf, (buf_bol(buf, pos) - 1));
} else {
- size_t next = buf_eol(buf, pos)+1;
+ size_t next = (buf_eol(buf, pos) + 1);
if (next < buf_end(buf))
pos = next;
}
}
void buf_getcol(Buf* buf) {
- Sel sel = getsel(buf, NULL);
+ Sel sel = buf->selection; //getsel(buf, NULL);
size_t pos = sel.end, curr = buf_bol(buf, pos);
for (sel.col = 0; curr < pos; curr = buf_byrune(buf, curr, 1))
sel.col += runewidth(sel.col, buf_getrat(buf, curr));
- setsel(buf, NULL, &sel);
+ buf->selection = sel;
}
void buf_setcol(Buf* buf) {
- Sel sel = getsel(buf, NULL);
+ Sel sel = buf->selection; //getsel(buf, NULL);
size_t bol = buf_bol(buf, sel.end);
size_t curr = bol, len = 0, i = 0;
/* determine the length of the line in columns */
break;
i += width;
}
- setsel(buf, NULL, &sel);
+ buf->selection = sel;
}
static Rune nextrune(Buf* buf, size_t off, int move, bool (*testfn)(Rune)) {
/******************************************************************************/
size_t buf_selsz(Buf* buf) {
- Sel sel = getsel(buf, NULL);
+ Sel sel = getsel(buf);
return sel.end - sel.beg;
}
void buf_selclr(Buf* buf, int dir) {
- Sel sel = getsel(buf, NULL);
+ Sel sel = getsel(buf);
if (dir > 0) sel.beg = sel.end;
else sel.end = sel.beg;
- setsel(buf, NULL, &sel);
+ buf->selection = sel;
}
bool buf_insel(Buf* buf, size_t off) {
- Sel sel = getsel(buf, NULL);
+ Sel sel = getsel(buf);
return (off >= sel.beg && off < sel.end);
}