From fc628e15e651776808ef1e339d4c8e9d307da42a Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Fri, 2 Dec 2016 20:09:48 -0500 Subject: [PATCH] added logic to open files at a given line number --- TODO.md | 1 - inc/edit.h | 2 +- libedit/buf.c | 16 +++++++++++----- libedit/view.c | 6 ++++-- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/TODO.md b/TODO.md index d05a861..c15d465 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,4 @@ # Implementation Tweaks and Bug Fixes - * Use select to check for error strings in exec.c * Should not be able to undo initial tag line text insertion * Disallow scrolling past end of buffer diff --git a/inc/edit.h b/inc/edit.h index 5657d5a..d7b6597 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -55,7 +55,7 @@ typedef struct { size_t col; } Sel; -void buf_load(Buf* buf, char* path); +unsigned buf_load(Buf* buf, char* path); void buf_save(Buf* buf); void buf_init(Buf* buf); unsigned buf_ins(Buf* buf, bool indent, unsigned off, Rune rune); diff --git a/libedit/buf.c b/libedit/buf.c index d0e38c0..5289281 100644 --- a/libedit/buf.c +++ b/libedit/buf.c @@ -3,15 +3,18 @@ #include #include -void buf_load(Buf* buf, char* path) { - if (!strcmp(path,"-")) { +unsigned buf_load(Buf* buf, char* path) { + unsigned off = 0; + buf->path = stringdup(path); + char* addr = strrchr(buf->path, ':'); + if (addr) *addr = '\0', addr++; + if (!strcmp(buf->path,"-")) { buf->charset = UTF_8; Rune r; while (RUNE_EOF != (r = fgetrune(stdin))) buf_ins(buf, false, buf_end(buf), r); } else { - FMap file = fmap(path); - buf->path = stringdup(path); + FMap file = fmap(buf->path); buf->charset = (file.buf ? charset(file.buf, file.len, &buf->crlf) : UTF_8); /* load the file contents if it has any */ if (buf->charset > UTF_8) { @@ -22,10 +25,13 @@ void buf_load(Buf* buf, char* path) { utf8load(buf, file); } funmap(file); + if (addr) + off = buf_setln(buf, strtoul(addr, NULL, 0)); } buf->modified = false; free(buf->undo); buf->undo = NULL; + return off; } void buf_save(Buf* buf) { @@ -235,7 +241,7 @@ unsigned buf_redo(Buf* buf, unsigned pos) { unsigned buf_setln(Buf* buf, unsigned line) { unsigned off = 0; - while (line > 0 && off < buf_end(buf)) + while (line > 1 && off < buf_end(buf)) line--, off = buf_byline(buf, off, DOWN); return off; } diff --git a/libedit/view.c b/libedit/view.c index 1f45d43..b3e918d 100644 --- a/libedit/view.c +++ b/libedit/view.c @@ -153,8 +153,10 @@ static size_t getoffset(View* view, size_t row, size_t col) { void view_init(View* view, char* file) { memset(view, 0, sizeof(View)); buf_init(&(view->buffer)); - if (file) - buf_load(&(view->buffer), file); + if (file) { + view->selection.end = buf_load(&(view->buffer), file); + view->selection.beg = view->selection.end; + } } size_t view_limitrows(View* view, size_t maxrows, size_t ncols) { -- 2.51.0