tlbuf: when the last line lacks eol, append it - neatvi - [fork] simple vi-type editor with UTF-8 support (HTM) git clone git://src.adamsgaard.dk/neatvi (DIR) Log (DIR) Files (DIR) Refs (DIR) README --- (DIR) commit d2e6b8054daf567c687f0cb4b526b4a6f439d7d5 (DIR) parent 20cc8817475554d8448632970ff7c8d84ed2f9e2 (HTM) Author: Ali Gholami Rudi <ali@rudi.ir> Date: Mon, 6 Jun 2016 23:40:14 +0430 lbuf: when the last line lacks eol, append it Diffstat: M lbuf.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) --- (DIR) diff --git a/lbuf.c b/lbuf.c t@@ -133,7 +133,7 @@ static void lbuf_insertline(struct lbuf *lb, int pos, char *s) /* low-level replacement */ static void lbuf_replace(struct lbuf *lb, char *s, int pos, int n_del) { - char *r; + char *r, *n; int n_ins = 0; int i; for (i = 0; i < n_del; i++) t@@ -141,12 +141,16 @@ static void lbuf_replace(struct lbuf *lb, char *s, int pos, int n_del) memmove(lb->ln + pos, lb->ln + pos + n_del, (lb->ln_n - pos - n_del) * sizeof(lb->ln[0])); lb->ln_n -= n_del; - while (s && (r = strchr(s, '\n'))) { - char *n = malloc(r - s + 2); - memcpy(n, s, r - s + 1); + while (s && *s) { + r = strchr(s, '\n'); + if (!r) /* no eol */ + r = strchr(s, '\0'); + n = malloc(r - s + 2); + memcpy(n, s, r - s); + n[r - s + 0] = '\n'; n[r - s + 1] = '\0'; lbuf_insertline(lb, pos + n_ins++, n); - s = r + 1; + s = *r ? r + 1 : r; } for (i = 0; i < LEN(lb->mark); i++) { /* updating marks */ if (!s && lb->mark[i] >= pos && lb->mark[i] < pos + n_del)