From 7489fb46e5141e14f82fb7927e1697374459b17b Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Wed, 16 Nov 2016 20:36:17 -0500 Subject: [PATCH] Implemented cursor movements for beginning and end of line as well as quit keyboard combo --- inc/edit.h | 2 + libedit/view.c | 12 +++++ xedit.c | 128 +++++++++++++++++++++---------------------------- 3 files changed, 69 insertions(+), 73 deletions(-) diff --git a/inc/edit.h b/inc/edit.h index c7e1a5f..23e7184 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -155,6 +155,8 @@ void view_setcursor(View* view, size_t row, size_t col); void view_selext(View* view, size_t row, size_t col); void view_insert(View* view, Rune rune); void view_delete(View* view); +void view_bol(View* view); +void view_eol(View* view); //size_t view_getoff(View* view, size_t pos, size_t row, size_t col); //void view_getsize(View* view, size_t* nrows, size_t* ncols); diff --git a/libedit/view.c b/libedit/view.c index 8877a17..7bbcf60 100644 --- a/libedit/view.c +++ b/libedit/view.c @@ -271,3 +271,15 @@ void view_delete(View* view) { view->selection.col = buf_getcol(&(view->buffer), view->selection.end); view->sync_needed = true; } + +void view_bol(View* view) { + view->selection.beg = view->selection.end = buf_bol(&(view->buffer), view->selection.end); + view->selection.col = buf_getcol(&(view->buffer), view->selection.end); + view->sync_needed = true; +} + +void view_eol(View* view) { + view->selection.beg = view->selection.end = buf_eol(&(view->buffer), view->selection.end); + view->selection.col = buf_getcol(&(view->buffer), view->selection.end); + view->sync_needed = true; +} diff --git a/xedit.c b/xedit.c index da1689a..8ebcc74 100644 --- a/xedit.c +++ b/xedit.c @@ -76,6 +76,14 @@ static void cursor_right(void) { view_byrune(currview(), +1); } +static void cursor_bol(void) { + view_bol(currview()); +} + +static void cursor_eol(void) { + view_eol(currview()); +} + static void change_focus(void) { if (Focused == TAGS) { if (TagWinExpanded) { @@ -90,6 +98,49 @@ static void change_focus(void) { } } +static void quit(void) { + static uint32_t num_clicks = 0; + static uint32_t prevtime = 0; + uint32_t now = getmillis(); + num_clicks = (now - prevtime < 250 ? num_clicks+1 : 1); + prevtime = now; + if (!getbuf(EDIT)->modified || num_clicks >= 2) + exit(0); +} +// +//static void write(void) { +// buf_save(&Buffer); +//} +// +//static void undo(void) { +// SelBeg = SelEnd = buf_undo(&Buffer, SelEnd); +// TargetCol = buf_getcol(&Buffer, SelEnd); +//} +// +//static void redo(void) { +// SelBeg = SelEnd = buf_redo(&Buffer, SelEnd); +// TargetCol = buf_getcol(&Buffer, SelEnd); +//} +// +//static void cut(void) { +// char* str = buf_getstr(&Buffer, SelBeg, SelEnd); +// cmdwrite(CopyCmd, str); +// free(str); +// delete(); +//} +// +//static void copy(void) { +// char* str = buf_getstr(&Buffer, SelBeg, SelEnd); +// cmdwrite(CopyCmd, str); +// free(str); +//} +// +//static void paste(void) { +// char* str = cmdread(PasteCmd); +// buf_putstr(&Buffer, SelBeg, SelEnd, str); +// free(str); +//} + /* Mouse Handling *****************************************************************************/ static void mouse_left(enum RegionId id, size_t count, size_t row, size_t col) { @@ -181,17 +232,16 @@ static KeyBinding Insert[] = { { KEY_DOWN, cursor_dn }, { KEY_LEFT, cursor_left }, { KEY_RIGHT, cursor_right }, - //{ KEY_CTRL_Q, quit }, + { KEY_HOME, cursor_bol }, + { KEY_END, cursor_eol }, + { KEY_CTRL_T, change_focus }, + { KEY_CTRL_Q, quit }, //{ KEY_CTRL_S, write }, - { KEY_CTRL_T, change_focus }, //{ KEY_CTRL_Z, undo }, //{ KEY_CTRL_Y, redo }, //{ KEY_CTRL_X, cut }, //{ KEY_CTRL_C, copy }, //{ KEY_CTRL_V, paste }, - //{ KEY_HOME, cursor_bol }, - //{ KEY_END, cursor_eol }, - //{ KEY_BACKSPACE, backspace }, { 0, NULL } }; @@ -360,74 +410,6 @@ static char* PasteCmd[] = { "xsel", "-bo", NULL }; /* Keyboard Actions *****************************************************************************/ -static void delete(void) { - if (SelEnd == buf_end(&Buffer)) return; - size_t n = SelEnd - SelBeg; - for (size_t i = 0; i < n; i++) - buf_del(&Buffer, SelBeg); - SelEnd = SelBeg; - TargetCol = buf_getcol(&Buffer, SelEnd); -} - -static void backspace(void) { - if (SelBeg > 0 && SelBeg == SelEnd) SelBeg--; - while (SelBeg < SelEnd) - buf_del(&Buffer, --SelEnd); - TargetCol = buf_getcol(&Buffer, SelEnd); -} - -static void quit(void) { - static uint32_t num_clicks = 0; - static uint32_t prevtime = 0; - uint32_t now = getmillis(); - num_clicks = (now - prevtime < 250 ? num_clicks+1 : 1); - prevtime = now; - if (!Buffer.modified || num_clicks >= 2) - exit(0); -} - -static void write(void) { - buf_save(&Buffer); -} - -static void undo(void) { - SelBeg = SelEnd = buf_undo(&Buffer, SelEnd); - TargetCol = buf_getcol(&Buffer, SelEnd); -} - -static void redo(void) { - SelBeg = SelEnd = buf_redo(&Buffer, SelEnd); - TargetCol = buf_getcol(&Buffer, SelEnd); -} - -static void cut(void) { - char* str = buf_getstr(&Buffer, SelBeg, SelEnd); - cmdwrite(CopyCmd, str); - free(str); - delete(); -} - -static void copy(void) { - char* str = buf_getstr(&Buffer, SelBeg, SelEnd); - cmdwrite(CopyCmd, str); - free(str); -} - -static void paste(void) { - char* str = cmdread(PasteCmd); - buf_putstr(&Buffer, SelBeg, SelEnd, str); - free(str); -} - -static void cursor_bol(void) { - SelBeg = SelEnd = buf_bol(&Buffer, SelEnd); - TargetCol = 0; -} - -static void cursor_eol(void) { - SelBeg = SelEnd = buf_eol(&Buffer, SelEnd); - TargetCol = (size_t)-1; -} static void tagwin(void) { TagWinExpanded = !TagWinExpanded; -- 2.52.0