From af96289dc7566c5841b93ef9167033664ae57489 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Mon, 17 Sep 2018 16:00:34 -0400 Subject: [PATCH] moved crlf detection into buf.c and removed usage of DosLineFeed from view_insert. This will be handled more appropriately in buf.c --- lib/buf.c | 37 ++++++++++++++++++++----------------- lib/view.c | 10 ++-------- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/lib/buf.c b/lib/buf.c index 63e9950..efeda37 100644 --- a/lib/buf.c +++ b/lib/buf.c @@ -16,6 +16,23 @@ static size_t pagealign(size_t sz) { return sz; } +static char getb(Buf* buf, size_t off) { + if (off >= buf_end(buf)) return '\n'; // TODO: get rid of this hack + size_t bsz = (buf->gapstart - buf->bufstart); + if (off < bsz) + return *(buf->bufstart + off); + else + return *(buf->gapend + (off - bsz)); +} + +static void putb(Buf* buf, char b, Sel* p_sel) { + buf_syncgap(buf, p_sel->end); + *(buf->gapstart++) = b; + p_sel->end = p_sel->end + 1u; + p_sel->beg = p_sel->end; + buf->status = MODIFIED; +} + void buf_init(Buf* buf) { /* cleanup old data if there is any */ if (buf->bufstart) { @@ -72,6 +89,9 @@ void buf_load(Buf* buf, char* path) { buf->status = NORMAL; buf->modtime = (uint64_t)sb.st_mtime; buf_logclear(buf); + + /* use the EOL style of the first line to determine EOL style */ + DosLineFeed = (getb(buf, buf_eol(buf, 0)) == '\r'); } void buf_reload(Buf* buf) { @@ -148,14 +168,6 @@ static Sel buf_getsel(Buf* buf) { /* Undo/Redo Operations ******************************************************************************/ -static void putb(Buf* buf, char b, Sel* p_sel) { - buf_syncgap(buf, p_sel->end); - *(buf->gapstart++) = b; - p_sel->end = p_sel->end + 1u; - p_sel->beg = p_sel->end; - buf->status = MODIFIED; -} - static Log* mklog(Buf* buf, size_t beg, size_t end, char* data, Log* next) { Log* log = calloc(1, sizeof(Log)); log->transid = (buf->transid < 0 ? 0 : buf->transid); @@ -295,15 +307,6 @@ void buf_lastins(Buf* buf) { /* Basic Operations and Accessors ******************************************************************************/ -static char getb(Buf* buf, size_t off) { - if (off >= buf_end(buf)) return '\n'; // TODO: get rid of this hack - size_t bsz = (buf->gapstart - buf->bufstart); - if (off < bsz) - return *(buf->bufstart + off); - else - return *(buf->gapend + (off - bsz)); -} - size_t buf_end(Buf* buf) { size_t bufsz = buf->bufend - buf->bufstart; size_t gapsz = buf->gapend - buf->gapstart; diff --git a/lib/view.c b/lib/view.c index 31bde21..c2c3e10 100644 --- a/lib/view.c +++ b/lib/view.c @@ -74,11 +74,7 @@ void view_init(View* view, char* file) { view->nvisible = 0; /* load the file and jump to the address returned from the load function */ buf_init(BUF); - if (file) { - buf_load(BUF, file); - /* use the EOL style of the first line to determine EOL style */ - DosLineFeed = (buf_getrat(BUF, buf_eol(BUF, 0)) == '\r'); - } + if (file) buf_load(BUF, file); } void view_reload(View* view) { @@ -271,9 +267,7 @@ void view_insert(View* view, Rune rune) { size_t off = buf_selbeg(BUF); size_t beg = buf_bol(BUF, off-1), end = beg; for (; end < buf_end(BUF) && (' ' == buf_getrat(BUF, end) || '\t' == buf_getrat(BUF, end)); end++); - if (DosLineFeed) - buf_putc(BUF, '\r'); - buf_putc(BUF, '\n'); + buf_putc(BUF, '\n'); for (; beg < end; beg++) buf_putc(BUF, buf_getrat(BUF, beg)); } else { -- 2.49.0