From 3b70b1280062ae08fe239c095bf74b4dd430e026 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Wed, 17 May 2017 15:54:04 -0400 Subject: [PATCH] Make common action handlers shareable so xpick and term can use them --- inc/shortcuts.h | 127 +++++++++++++++++++++++++++++++++++++++++++++++ xedit.c | 129 +----------------------------------------------- 2 files changed, 128 insertions(+), 128 deletions(-) create mode 100644 inc/shortcuts.h diff --git a/inc/shortcuts.h b/inc/shortcuts.h new file mode 100644 index 0000000..91e1e42 --- /dev/null +++ b/inc/shortcuts.h @@ -0,0 +1,127 @@ +static void delete(void) { + bool byword = x11_keymodsset(ModCtrl); + view_delete(win_view(FOCUSED), RIGHT, byword); +} + +static void onpaste(char* text) { + view_putstr(win_view(FOCUSED), text); +} + +static void cut(void) { + View* view = win_view(FOCUSED); + /* select the current line if no selection */ + if (!view_selsize(view)) { + view_eol(view, false); + view_selctx(view); + } + /* now perform the cut */ + char* str = view_getstr(view, NULL); + x11_sel_set(CLIPBOARD, str); + if (str && *str) delete(); +} + +static void paste(void) { + assert(x11_sel_get(CLIPBOARD, onpaste)); +} + +static void copy(void) { + char* str = view_getstr(win_view(FOCUSED), NULL); + x11_sel_set(CLIPBOARD, str); +} + +static void del_to_bol(void) { + view_bol(win_view(FOCUSED), true); + if (view_selsize(win_view(FOCUSED)) > 0) + delete(); +} + +static void del_to_eol(void) { + view_eol(win_view(FOCUSED), true); + if (view_selsize(win_view(FOCUSED)) > 0) + delete(); +} + +static void del_to_bow(void) { + view_byword(win_view(FOCUSED), LEFT, true); + if (view_selsize(win_view(FOCUSED)) > 0) + delete(); +} + +static void backspace(void) { + bool byword = x11_keymodsset(ModCtrl); + view_delete(win_view(FOCUSED), LEFT, byword); +} + +static void cursor_bol(void) { + view_bol(win_view(FOCUSED), false); +} + +static void cursor_eol(void) { + view_eol(win_view(FOCUSED), false); +} + +static void cursor_home(void) { + bool extsel = x11_keymodsset(ModShift); + if (x11_keymodsset(ModCtrl)) + view_bof(win_view(FOCUSED), extsel); + else + view_bol(win_view(FOCUSED), extsel); +} + +static void cursor_end(void) { + bool extsel = x11_keymodsset(ModShift); + if (x11_keymodsset(ModCtrl)) + view_eof(win_view(FOCUSED), extsel); + else + view_eol(win_view(FOCUSED), extsel); +} + +static void cursor_up(void) { + bool extsel = x11_keymodsset(ModShift); + view_byline(win_view(FOCUSED), UP, extsel); +} + +static void cursor_dn(void) { + bool extsel = x11_keymodsset(ModShift); + view_byline(win_view(FOCUSED), DOWN, extsel); +} + +static void cursor_left(void) { + bool extsel = x11_keymodsset(ModShift); + if (x11_keymodsset(ModCtrl)) + view_byword(win_view(FOCUSED), LEFT, extsel); + else + view_byrune(win_view(FOCUSED), LEFT, extsel); +} + +static void cursor_right(void) { + bool extsel = x11_keymodsset(ModShift); + if (x11_keymodsset(ModCtrl)) + view_byword(win_view(FOCUSED), RIGHT, extsel); + else + view_byrune(win_view(FOCUSED), RIGHT, extsel); +} + +static void page_up(void) { + view_scrollpage(win_view(FOCUSED), UP); +} + +static void page_dn(void) { + view_scrollpage(win_view(FOCUSED), DOWN); +} + +static void select_prev(void) { + view_selprev(win_view(FOCUSED)); +} + +static void change_focus(void) { + win_setregion(win_getregion() == TAGS ? EDIT : TAGS); +} + +static void undo(void) { + view_undo(win_view(FOCUSED)); +} + +static void redo(void) { + view_redo(win_view(FOCUSED)); +} diff --git a/xedit.c b/xedit.c index 25701fd..234488d 100644 --- a/xedit.c +++ b/xedit.c @@ -4,6 +4,7 @@ #include #include #include +#include typedef struct { char* tag; @@ -105,37 +106,6 @@ static void exec(char* cmd) { /* Action Callbacks ******************************************************************************/ -static void delete(void) { - bool byword = x11_keymodsset(ModCtrl); - view_delete(win_view(FOCUSED), RIGHT, byword); -} - -static void onpaste(char* text) { - view_putstr(win_view(FOCUSED), text); -} - -static void cut(void) { - View* view = win_view(FOCUSED); - /* select the current line if no selection */ - if (!view_selsize(view)) { - view_eol(view, false); - view_selctx(view); - } - /* now perform the cut */ - char* str = view_getstr(view, NULL); - x11_sel_set(CLIPBOARD, str); - if (str && *str) delete(); -} - -static void paste(void) { - assert(x11_sel_get(CLIPBOARD, onpaste)); -} - -static void copy(void) { - char* str = view_getstr(win_view(FOCUSED), NULL); - x11_sel_set(CLIPBOARD, str); -} - static void quit(void) { static uint64_t before = 0; uint64_t now = getmillis(); @@ -197,103 +167,6 @@ void onmouseright(WinRegion id, size_t count, size_t row, size_t col) { /* Keyboard Handling ******************************************************************************/ -static void del_to_bol(void) { - view_bol(win_view(FOCUSED), true); - if (view_selsize(win_view(FOCUSED)) > 0) - delete(); -} - -static void del_to_eol(void) { - view_eol(win_view(FOCUSED), true); - if (view_selsize(win_view(FOCUSED)) > 0) - delete(); -} - -static void del_to_bow(void) { - view_byword(win_view(FOCUSED), LEFT, true); - if (view_selsize(win_view(FOCUSED)) > 0) - delete(); -} - -static void backspace(void) { - bool byword = x11_keymodsset(ModCtrl); - view_delete(win_view(FOCUSED), LEFT, byword); -} - -static void cursor_bol(void) { - view_bol(win_view(FOCUSED), false); -} - -static void cursor_eol(void) { - view_eol(win_view(FOCUSED), false); -} - -static void cursor_home(void) { - bool extsel = x11_keymodsset(ModShift); - if (x11_keymodsset(ModCtrl)) - view_bof(win_view(FOCUSED), extsel); - else - view_bol(win_view(FOCUSED), extsel); -} - -static void cursor_end(void) { - bool extsel = x11_keymodsset(ModShift); - if (x11_keymodsset(ModCtrl)) - view_eof(win_view(FOCUSED), extsel); - else - view_eol(win_view(FOCUSED), extsel); -} - -static void cursor_up(void) { - bool extsel = x11_keymodsset(ModShift); - view_byline(win_view(FOCUSED), UP, extsel); -} - -static void cursor_dn(void) { - bool extsel = x11_keymodsset(ModShift); - view_byline(win_view(FOCUSED), DOWN, extsel); -} - -static void cursor_left(void) { - bool extsel = x11_keymodsset(ModShift); - if (x11_keymodsset(ModCtrl)) - view_byword(win_view(FOCUSED), LEFT, extsel); - else - view_byrune(win_view(FOCUSED), LEFT, extsel); -} - -static void cursor_right(void) { - bool extsel = x11_keymodsset(ModShift); - if (x11_keymodsset(ModCtrl)) - view_byword(win_view(FOCUSED), RIGHT, extsel); - else - view_byrune(win_view(FOCUSED), RIGHT, extsel); -} - -static void page_up(void) { - view_scrollpage(win_view(FOCUSED), UP); -} - -static void page_dn(void) { - view_scrollpage(win_view(FOCUSED), DOWN); -} - -static void select_prev(void) { - view_selprev(win_view(FOCUSED)); -} - -static void change_focus(void) { - win_setregion(win_getregion() == TAGS ? EDIT : TAGS); -} - -static void undo(void) { - view_undo(win_view(FOCUSED)); -} - -static void redo(void) { - view_redo(win_view(FOCUSED)); -} - static void saveas(char* arg) { if (arg) { char* path = win_buf(EDIT)->path; -- 2.52.0