From: Michael D. Lowis Date: Mon, 17 Sep 2018 18:38:09 +0000 (-0400) Subject: first pass at detecting dos line endings X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=2afa2706b9bd98c724e83f61d2a22eb6737dbe03;p=projs%2Ftide.git first pass at detecting dos line endings --- diff --git a/TODO.md b/TODO.md index 80edc47..fa52cf8 100644 --- a/TODO.md +++ b/TODO.md @@ -9,6 +9,7 @@ BUGS: * no trimming of trailing whitespace * gap buffer does not handle UTF-8 currently * mouse selection handling when mouse moves outside region +* move mouse handlers back into tide.c Up Next: diff --git a/lib/buf.c b/lib/buf.c index cfe47b5..63e9950 100644 --- a/lib/buf.c +++ b/lib/buf.c @@ -424,7 +424,7 @@ static int bytes_match(Buf* buf, size_t mbeg, size_t mend, char* str) { bool buf_iseol(Buf* buf, size_t off) { Rune r = buf_getrat(buf, off); - return (r == '\n'); + return (r == '\r' || r == '\n'); } size_t buf_bol(Buf* buf, size_t off) { diff --git a/lib/view.c b/lib/view.c index f8ac181..31bde21 100644 --- a/lib/view.c +++ b/lib/view.c @@ -74,7 +74,11 @@ 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); + 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'); + } } void view_reload(View* view) { @@ -263,11 +267,13 @@ void view_insert(View* view, Rune rune) { size_t off = buf_selbeg(BUF); size_t n = (TabWidth - ((off - buf_bol(BUF, off)) % TabWidth)); for (; n > 0; n--) buf_putc(BUF, ' '); - } else if (CopyIndent && rune == '\n') { + } else if (CopyIndent && rune == '\n') { 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++); - buf_putc(BUF, rune); + if (DosLineFeed) + buf_putc(BUF, '\r'); + buf_putc(BUF, '\n'); for (; beg < end; beg++) buf_putc(BUF, buf_getrat(BUF, beg)); } else {