From: Michael D. Lowis Date: Wed, 30 Nov 2016 00:55:07 +0000 (-0500) Subject: Implemented backspace and delete with view_delete and enums for direction. Scaffoldin... X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=762fdf3031ddaae1bfbed78b9cf0bcb977186799;p=projs%2Ftide.git Implemented backspace and delete with view_delete and enums for direction. Scaffolding in place for delete by word --- diff --git a/inc/edit.h b/inc/edit.h index e9ca6d7..30f765c 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -146,6 +146,13 @@ typedef struct { Sel selection; /* range of currently selected text */ } View; +enum { + LEFT = -1, + RIGHT = +1, + UP = -1, + DOWN = +1 +}; + void view_init(View* view, char* file); size_t view_limitrows(View* view, size_t maxrows, size_t ncols); void view_resize(View* view, size_t nrows, size_t ncols); @@ -162,7 +169,7 @@ char* view_fetch(View* view, size_t row, size_t col); void view_find(View* view, size_t row, size_t col); void view_findstr(View* view, char* str); void view_insert(View* view, Rune rune); -void view_delete(View* view); +void view_delete(View* view, int dir, bool byword); void view_bol(View* view, bool extsel); void view_eol(View* view, bool extsel); void view_undo(View* view); diff --git a/libedit/view.c b/libedit/view.c index dd6b2b0..a8cf675 100644 --- a/libedit/view.c +++ b/libedit/view.c @@ -358,36 +358,32 @@ void view_findstr(View* view, char* str) { } void view_insert(View* view, Rune rune) { - if (rune == '\b') { - if (num_selected(view->selection)) - view_delete(view); - else if (view->selection.end > 0) - buf_del(&(view->buffer), --view->selection.end); - } else { - /* ignore non-printable control characters */ - if (!isspace(rune) && rune < 0x20) - return; - if (num_selected(view->selection)) - view_delete(view); - buf_ins(&(view->buffer), view->selection.end++, rune); - } + /* ignore non-printable control characters */ + if (!isspace(rune) && rune < 0x20) + return; + if (num_selected(view->selection)) + view_delete(view, RIGHT, false); + 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; } -void view_delete(View* view) { +void view_delete(View* view, int dir, bool byword) { Sel sel = view->selection; selswap(&sel); - size_t num = num_selected(view->selection); - if (num == 0) { - if (sel.end < buf_end(&(view->buffer))) - buf_del(&(view->buffer), sel.end); - } else { + size_t num = num_selected(sel); + if (num != 0) { for (size_t i = 0; i < num; i++) buf_del(&(view->buffer), sel.beg); + sel.end = sel.beg; + } else { + if ((dir == LEFT) && (sel.end > 0)) + buf_del(&(view->buffer), --sel.end); + else if ((dir == RIGHT) && (sel.end < buf_end(&(view->buffer)))) + buf_del(&(view->buffer), sel.end); } - view->selection.beg = view->selection.end = sel.beg; + view->selection.beg = view->selection.end = sel.end; view->selection.col = buf_getcol(&(view->buffer), view->selection.end); view->sync_needed = true; } diff --git a/xedit.c b/xedit.c index 6f402e5..31afa7d 100644 --- a/xedit.c +++ b/xedit.c @@ -26,6 +26,7 @@ static void redraw(int width, int height); // UI Callbacks static void delete(void); +static void backspace(void); static void cursor_home(void); static void cursor_end(void); static void cursor_up(void); @@ -138,7 +139,7 @@ static KeyBinding Bindings[] = { { ModNone, KEY_PGUP, page_up }, { ModNone, KEY_PGDN, page_dn }, { ModAny, KEY_DELETE, delete }, - //{ ModAny, KEY_BACKSPACE, backspace }, + { ModAny, KEY_BACKSPACE, backspace }, /* Cursor Movements */ { ModAny, KEY_HOME, cursor_home }, @@ -360,7 +361,13 @@ static void redraw(int width, int height) { /* UI Callbacks *****************************************************************************/ static void delete(void) { - view_delete(currview()); + bool byword = x11_keymodsset(ModCtrl); + view_delete(currview(), RIGHT, byword); +} + +static void backspace(void) { + bool byword = x11_keymodsset(ModCtrl); + view_delete(currview(), LEFT, byword); } static void cursor_home(void) { @@ -375,30 +382,30 @@ static void cursor_end(void) { static void cursor_up(void) { bool extsel = x11_keymodsset(ModShift); - view_byline(currview(), -1, extsel); + view_byline(currview(), UP, extsel); } static void cursor_dn(void) { bool extsel = x11_keymodsset(ModShift); - view_byline(currview(), +1, extsel); + view_byline(currview(), DOWN, extsel); } static void cursor_left(void) { bool extsel = x11_keymodsset(ModShift); - view_byrune(currview(), -1, extsel); + view_byrune(currview(), LEFT, extsel); } static void cursor_right(void) { bool extsel = x11_keymodsset(ModShift); - view_byrune(currview(), +1, true); + view_byrune(currview(), RIGHT, extsel); } static void page_up(void) { - view_scrollpage(currview(), -1); + view_scrollpage(currview(), UP); } static void page_dn(void) { - view_scrollpage(currview(), +1); + view_scrollpage(currview(), DOWN); } static void select_prev(void) {