return (sel.end < sel.beg ? selswap(sel) : sel);
}
-//static void selset(Buf* buf, Sel sel)
-//{
-// buf->selection = (sel.end < sel.beg ? selswap(sel) : sel);
-//}
-
/* Creation, Resizing, Loading, and Saving
******************************************************************************/
static size_t pagealign(size_t sz)
void buf_logstart(Buf* buf)
{
+ require(buf != NULL);
buf->transid = abs(buf->transid);
}
void buf_logstop(Buf* buf)
{
+ require(buf != NULL);
if (buf->transid > 0)
{
buf->transid = -(buf->transid + 1);
void buf_undo(Buf* buf)
{
+ require(buf != NULL);
log_swap(buf, &(buf->undo), &(buf->redo));
ensure(buf_valid(buf));
}
void buf_redo(Buf* buf)
{
+ require(buf != NULL);
log_swap(buf, &(buf->redo), &(buf->undo));
ensure(buf_valid(buf));
}
void buf_logclear(Buf* buf)
{
+ require(buf != NULL);
log_clear(&(buf->redo));
log_clear(&(buf->undo));
}
void buf_lastins(Buf* buf)
{
+ require(buf != NULL);
Log* log = buf->undo;
if (log)
{
******************************************************************************/
size_t buf_end(Buf* buf)
{
+ require(buf != NULL);
size_t bufsz = buf->bufend - buf->bufstart;
size_t gapsz = buf->gapend - buf->gapstart;
return (bufsz - gapsz);
int buf_getrat(Buf* buf, size_t off)
{
+ require(buf != NULL);
size_t rlen = 0;
Rune rune = 0;
if (getb(buf, off) == '\r' && getb(buf, off+1) == '\n')
void buf_putc(Buf* buf, int c)
{
+ require(buf != NULL);
char utf8buf[UTF_MAX+1] = {0};
(void)utf8encode(utf8buf, c);
buf_puts(buf, utf8buf);
void buf_puts(Buf* buf, char* s)
{
+ require(buf != NULL);
buf_del(buf);
size_t beg = buf_selbeg(buf);
if (s && *s)
int buf_getc(Buf* buf)
{
+ require(buf != NULL);
return buf_getrat(buf, buf->selection.end);
}
char* buf_gets(Buf* buf)
{
+ require(buf != NULL);
Sel sel = selget(buf);
size_t nbytes = sel.end - sel.beg;
char* str = malloc(nbytes+1);
char* buf_getsat(Buf* buf, size_t beg, size_t end)
{
+ require(buf != NULL);
Sel sel = selget(buf);
buf->selection = (Sel){ .beg = beg, .end = end };
char* str = buf_gets(buf);
void buf_del(Buf* buf)
{
+ require(buf != NULL);
Sel sel = selget(buf);
size_t nbytes = sel.end - sel.beg;
if (nbytes > 0)
bool buf_isbol(Buf* buf, size_t off)
{
+ require(buf != NULL);
size_t bol = buf_bol(buf, off);
return (bol == off);
}
bool buf_iseol(Buf* buf, size_t off)
{
+ require(buf != NULL);
Rune r = buf_getrat(buf, off);
return (r == '\r' || r == '\n');
}
size_t buf_bol(Buf* buf, size_t off)
{
+ require(buf != NULL);
for (; !buf_iseol(buf, off-1); off--)
{
}
size_t buf_eol(Buf* buf, size_t off)
{
+ require(buf != NULL);
for (; !buf_iseol(buf, off); off++)
{
}
void buf_selword(Buf* buf, bool (*isword)(Rune))
{
+ require(buf != NULL);
Sel sel = selget(buf);
for (; isword(buf_getrat(buf, sel.beg-1)); sel.beg--)
{
void buf_selall(Buf* buf)
{
+ require(buf != NULL);
buf->selection = (Sel){ .beg = 0, .end = buf_end(buf) };
}
void buf_selctx(Buf* buf, bool (*isword)(Rune))
{
+ require(buf != NULL);
size_t bol = buf_bol(buf, buf->selection.end);
Rune curr = buf_getc(buf);
if (curr == '(' || curr == ')')
{
selline(buf);
}
+
else if (selquote(buf, '"') || selquote(buf, '`') || selquote(buf, '\''))
{
; /* condition performs selection */
size_t buf_byrune(Buf* buf, size_t pos, int count)
{
+ require(buf != NULL);
int move = (count < 0 ? -1 : 1);
count *= move; // remove the sign if there is one
for (; count > 0; count--)
size_t buf_byword(Buf* buf, size_t off, int count)
{
+ require(buf != NULL);
int move = (count < 0 ? -1 : 1);
while (nextrune(buf, off, move, risblank))
size_t buf_byline(Buf* buf, size_t pos, int count)
{
+ require(buf != NULL);
int move = (count < 0 ? -1 : 1);
count *= move; // remove the sign if there is one
for (; count > 0; count--)
bool buf_findstr(Buf* buf, int dir, char* str)
{
+ require(buf != NULL);
bool found = false;
size_t len = strlen(str);
- size_t start = buf->selection.beg,
- mbeg = (start + dir),
- mend = (mbeg + len);
- while (mbeg != start)
+ size_t start = buf->selection.beg;
+ size_t mbeg = (start + dir);
+ size_t mend = (mbeg + len);
+ size_t nleft = buf_end(buf);
+ for (; (mbeg != start) && nleft; nleft--)
{
if ((getb(buf, mbeg) == str[0]) &&
(getb(buf, mend-1) == str[len-1]) &&
void buf_setln(Buf* buf, size_t line)
{
+ require(buf != NULL);
size_t curr = 0, end = buf_end(buf);
while (line > 1 && curr < end)
{
void buf_getln(Buf* buf, size_t* begln, size_t* endln)
{
+ require(buf != NULL);
size_t line = 1, curr = 0, end = buf_end(buf);
size_t sbeg = buf_selbeg(buf), send = buf_selend(buf);
while (curr < end)
void buf_getcol(Buf* buf)
{
+ require(buf != NULL);
Sel sel = buf->selection;
size_t pos = sel.end, curr = buf_bol(buf, pos);
for (sel.col = 0; curr < pos; curr = buf_byrune(buf, curr, 1))
void buf_setcol(Buf* buf)
{
+ require(buf != NULL);
Sel sel = buf->selection;
size_t bol = buf_bol(buf, sel.end);
size_t curr = bol, len = 0, i = 0;
size_t buf_selbeg(Buf* buf)
{
+ require(buf != NULL);
return selget(buf).beg;
}
size_t buf_selend(Buf* buf)
{
+ require(buf != NULL);
return selget(buf).end;
}
size_t buf_selsz(Buf* buf)
{
+ require(buf != NULL);
return (selget(buf).end - selget(buf).beg);
}
void buf_selln(Buf* buf)
{
+ require(buf != NULL);
/* Expand the selection to completely select the lines covered */
Sel sel = selget(buf);
sel.beg = buf_bol(buf, sel.beg);
static char* DumpPath = NULL;
static void (*DumpFn)(FILE*) = NULL;
-static void dump_and_abort(char* msg) {
+static void dump_and_abort(char* msg)
+{
FILE* f = (DumpPath ? fopen(DumpPath, "w") : stderr);
fprintf(f, "%s\n\n", msg);
if (DumpFn) DumpFn(f);
+ fprintf(f, "\n%s\n", msg);
fclose(f);
_Exit(1);
}
-static void handle_signal(int sig) {
- if (SIGABRT == sig) {
+static void handle_signal(int sig)
+{
+ if (SIGABRT == sig)
+ {
dump_and_abort("SIGABRT - Process abort signal");
- } else if (SIGBUS == sig) {
+ }
+ else if (SIGBUS == sig)
+ {
dump_and_abort("SIGBUS - Access to an undefined portion of a memory object");
- } else if (SIGFPE == sig) {
+ }
+ else if (SIGFPE == sig)
+ {
dump_and_abort("SIGFPE - Erroneous arithmetic operation");
- } else if (SIGILL == sig) {
+ }
+ else if (SIGILL == sig)
+ {
dump_and_abort("SIGILL - Illegal instruction");
- } else if (SIGSEGV == sig) {
+ }
+ else if (SIGSEGV == sig)
+ {
dump_and_abort("SIGSEGV - Invalid memory reference");
- } else if (SIGALRM == sig) {
+ }
+ else if (SIGALRM == sig)
+ {
dump_and_abort("SIGALRM - Watchdog timer expired");
}
}
-void dbc_init(char* path, void (*dumpfn)(FILE*)) {
+void dbc_init(char* path, void (*dumpfn)(FILE*))
+{
DumpPath = path;
DumpFn = dumpfn;
signal(SIGABRT, handle_signal);
signal(SIGSEGV, handle_signal);
}
-void dbc_require(bool success, char* text, char* file, int line) {
- if (!success) {
+void dbc_require(bool success, char* text, char* file, int line)
+{
+ if (!success)
+ {
snprintf(ErrMsg, sizeof(ErrMsg), "%s:%d: pre-condition failed (%s)", file, line, text);
dump_and_abort(ErrMsg);
}
}
-void dbc_ensure(bool success, char* text, char* file, int line) {
- if (!success) {
+void dbc_ensure(bool success, char* text, char* file, int line)
+{
+ if (!success)
+ {
snprintf(ErrMsg, sizeof(ErrMsg), "%s:%d: post-condition failed (%s)", file, line, text);
dump_and_abort(ErrMsg);
}
}
-void dbc_wdtkick(void) {
+void dbc_wdtkick(void)
+{
alarm(0); /* Cancel previous alarm */
signal(SIGALRM, handle_signal);
alarm(1); /* Start new alarm */