From 24b3be5aa175642360a4ccd7fd3532ca1c71f4d9 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Thu, 1 Dec 2016 21:29:16 -0500 Subject: [PATCH] implemented expand tabs mode --- TODO.md | 5 +---- inc/edit.h | 2 +- libedit/buf.c | 34 +++++++++++++++++++++------------- libedit/view.c | 4 ++-- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/TODO.md b/TODO.md index bba87b2..89d3c46 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,6 @@ # Implementation Tweaks and Bug Fixes +* Auto indent mode * Should not be able to undo initial tag line text insertion * Disallow scrolling past end of buffer * track down double click bug for selecting whole line @@ -7,12 +8,8 @@ * Implement minimal regex search (per Kernighan article) * Implement fuzzy file/buffer/tag picker * Implement omnicomplete pop-up -* Mode to expand tabs to spaces -* Auto indent mode * off by one error on scrolling up with wrapped lines * block selection should handle brace-balancing -* Right click should fetch file if it exists, search otherwise - * directories in browse? # Internals and Miscellaneous diff --git a/inc/edit.h b/inc/edit.h index 2bd4971..d71a61d 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -57,7 +57,7 @@ typedef struct { void buf_load(Buf* buf, char* path); void buf_save(Buf* buf); void buf_init(Buf* buf); -void buf_ins(Buf* buf, unsigned pos, Rune); +unsigned buf_ins(Buf* buf, unsigned pos, Rune); void buf_del(Buf* buf, unsigned pos); unsigned buf_undo(Buf* buf, unsigned pos); unsigned buf_redo(Buf* buf, unsigned pos); diff --git a/libedit/buf.c b/libedit/buf.c index 658cd19..e4179cb 100644 --- a/libedit/buf.c +++ b/libedit/buf.c @@ -74,16 +74,17 @@ static void syncgap(Buf* buf, unsigned off) { } void buf_init(Buf* buf) { - buf->modified = false; - buf->charset = DEFAULT_CHARSET; - buf->crlf = DEFAULT_CRLF; - buf->bufsize = BufSize; - buf->bufstart = (Rune*)malloc(buf->bufsize * sizeof(Rune)); - buf->bufend = buf->bufstart + buf->bufsize; - buf->gapstart = buf->bufstart; - buf->gapend = buf->bufend; - buf->undo = NULL; - buf->redo = NULL; + buf->modified = false; + buf->expand_tabs = true; + buf->charset = DEFAULT_CHARSET; + buf->crlf = DEFAULT_CRLF; + buf->bufsize = BufSize; + buf->bufstart = (Rune*)malloc(buf->bufsize * sizeof(Rune)); + buf->bufend = buf->bufstart + buf->bufsize; + buf->gapstart = buf->bufstart; + buf->gapend = buf->bufend; + buf->undo = NULL; + buf->redo = NULL; } static void log_insert(Log** list, unsigned beg, unsigned end) { @@ -151,11 +152,18 @@ static void clear_redo(Buf* buf) { buf->redo = NULL; } -void buf_ins(Buf* buf, unsigned off, Rune rune) { +unsigned buf_ins(Buf* buf, unsigned off, Rune rune) { buf->modified = true; - log_insert(&(buf->undo), off, off+1); + if (buf->expand_tabs && rune == '\t') { + size_t n = (TabWidth - ((off - buf_bol(buf, off)) % TabWidth)); + log_insert(&(buf->undo), off, off+n); + for(; n > 0; n--) insert(buf, off++, ' '); + } else { + log_insert(&(buf->undo), off, off+1); + insert(buf, off++, rune); + } clear_redo(buf); - insert(buf, off, rune); + return off; } static void delete(Buf* buf, unsigned off) { diff --git a/libedit/view.c b/libedit/view.c index 1d11bb7..8908f8e 100644 --- a/libedit/view.c +++ b/libedit/view.c @@ -372,10 +372,10 @@ void view_insert(View* view, Rune rune) { return; if (num_selected(view->selection)) view_delete(view, RIGHT, false); - buf_ins(&(view->buffer), view->selection.end++, rune); + view->selection.end = buf_ins(&(view->buffer), view->selection.end, rune); view->selection.beg = view->selection.end; view->selection.col = buf_getcol(&(view->buffer), view->selection.end); - view->sync_needed = true; + view->sync_needed = true; } void view_delete(View* view, int dir, bool byword) { -- 2.52.0