From 52eae494b1cf1d9f77ae0ce89506b84e9d499844 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Sun, 8 Apr 2018 21:29:11 -0400 Subject: [PATCH] minor refactoring --- inc/win.h | 2 +- lib/x11.c | 7 +-- tide.c | 149 +++++++++++++++++++++++++----------------------------- 3 files changed, 73 insertions(+), 85 deletions(-) diff --git a/inc/win.h b/inc/win.h index 51caae1..7651154 100644 --- a/inc/win.h +++ b/inc/win.h @@ -32,7 +32,7 @@ View* win_view(WinRegion id); Buf* win_buf(WinRegion id); bool win_btnpressed(int btn); WinRegion win_getregion(void); -bool win_setregion(WinRegion id); +void win_setregion(WinRegion id); void win_setscroll(double offset, double visible); /* move these to x11.c when possible */ diff --git a/lib/x11.c b/lib/x11.c index fecfaf9..0a7354c 100644 --- a/lib/x11.c +++ b/lib/x11.c @@ -166,11 +166,8 @@ WinRegion win_getregion(void) { return Focused; } -bool win_setregion(WinRegion id) { - bool changed = true; - if (Focused != id && (id == TAGS || id == EDIT)) - changed = true, Focused = id; - return changed; +void win_setregion(WinRegion id) { + Focused = id; } View* win_view(WinRegion id) { diff --git a/tide.c b/tide.c index 9c4e750..3c1340e 100644 --- a/tide.c +++ b/tide.c @@ -29,10 +29,78 @@ char* FetchCmd[] = { "tfetch", NULL, NULL }; #define CMD_TO_UNIX "|dos2unix" #define CMD_COMPLETE "tag) { + if (!strncmp(tags->tag, cmd, len)) + return tags; + tags++; + } + return NULL; +} + +static void tag_exec(Tag* tag, char* arg) { + /* if we didnt get an arg, find one in the selection */ + if (!arg) arg = view_getstr(win_view(TAGS)); + if (!arg) arg = view_getstr(win_view(EDIT)); + /* execute the tag handler */ + tag->action(arg); + free(arg); +} + +static void cmd_exec(char* cmd) { + /* parse the command sigils */ + char op = '\0', **execcmd = NULL; + if (*cmd == ':' || *cmd == '!' || *cmd == '<' || *cmd == '|' || *cmd == '>') + op = *cmd, cmd++; + execcmd = (op == ':' ? SedCmd : ShellCmd); + execcmd[2] = cmd; + /* get the selection that the command will operate on */ + if (op && op != '<' && op != '!' && !view_selsize(win_view(EDIT))) + view_selectall(win_view(EDIT)); + char* input = view_getstr(win_view(EDIT)); + size_t len = (input ? strlen(input) : 0); + View *tags = win_view(TAGS), *edit = win_view(EDIT), *curr = win_view(FOCUSED); + + /* execute the job */ + if (op == '!') + free(input), job_start(execcmd, NULL, 0, NULL); + else if (op == '>') + job_start(execcmd, input, len, tags); + else if (op == '|' || op == ':') + job_start(execcmd, input, len, edit); + else + job_start(execcmd, input, len, (op != '<' ? curr : edit)); +} + +static void cmd_execwitharg(char* cmd, char* arg) { + cmd = (arg ? strmcat(cmd, " '", arg, "'", 0) : strmcat(cmd)); + cmd_exec(cmd); + free(cmd); +} + +void exec(char* cmd) { + /* skip leading space */ + for (; *cmd && isspace(*cmd); cmd++); + if (!*cmd) return; + /* see if it matches a builtin tag */ + Tag* tag = tag_lookup(cmd); + if (tag) { + while (*cmd && !isspace(*cmd++)); + tag_exec(tag, (*cmd ? stringdup(cmd) : NULL)); + } else { + cmd_exec(cmd); + } +} + +/* Keyboard and Tag Handlers + ******************************************************************************/ static void select_line(char* arg) { View* view = win_view(FOCUSED); view_eol(view, false); @@ -114,8 +182,6 @@ static void cursor_eol(char* arg) { view_eol(win_view(FOCUSED), false); } -/******************************************************************************/ - static void cursor_mvlr(int dir) { bool extsel = x11_keymodsset(ModShift); if (x11_keymodsset(ModCtrl)) @@ -140,8 +206,6 @@ static void cursor_home_end( linefn(win_view(FOCUSED), extsel); } -/******************************************************************************/ - static void cursor_home(char* arg) { cursor_home_end(view_bof, view_bol); } @@ -190,77 +254,6 @@ static void redo(char* arg) { view_redo(win_view(FOCUSED)); } -/* Tag/Cmd Execution - ******************************************************************************/ -static Tag* tag_lookup(char* cmd) { - size_t len = 0; - Tag* tags = Builtins; - for (char* tag = cmd; *tag && !isspace(*tag); tag++, len++); - while (tags->tag) { - if (!strncmp(tags->tag, cmd, len)) - return tags; - tags++; - } - return NULL; -} - -static void tag_exec(Tag* tag, char* arg) { - /* if we didnt get an arg, find one in the selection */ - if (!arg) arg = view_getstr(win_view(TAGS)); - if (!arg) arg = view_getstr(win_view(EDIT)); - /* execute the tag handler */ - tag->action(arg); - free(arg); -} - -static void cmd_exec(char* cmd) { - /* parse the command sigils */ - char op = '\0', **execcmd = NULL; - if (*cmd == ':' || *cmd == '!' || *cmd == '<' || *cmd == '|' || *cmd == '>') - op = *cmd, cmd++; - execcmd = (op == ':' ? SedCmd : ShellCmd); - execcmd[2] = cmd; - - /* get the selection that the command will operate on */ - if (op && op != '<' && op != '!' && !view_selsize(win_view(EDIT))) - view_selectall(win_view(EDIT)); - char* input = view_getstr(win_view(EDIT)); - size_t len = (input ? strlen(input) : 0); - View *tags = win_view(TAGS), *edit = win_view(EDIT), *curr = win_view(FOCUSED); - - /* execute the job */ - if (op == '!') - free(input), job_start(execcmd, NULL, 0, NULL); - else if (op == '>') - job_start(execcmd, input, len, tags); - else if (op == '|' || op == ':') - job_start(execcmd, input, len, edit); - else - job_start(execcmd, input, len, (op != '<' ? curr : edit)); -} - -static void cmd_execwitharg(char* cmd, char* arg) { - cmd = (arg ? strmcat(cmd, " '", arg, "'", 0) : strmcat(cmd)); - cmd_exec(cmd); - free(cmd); -} - -void exec(char* cmd) { - /* skip leading space */ - for (; *cmd && isspace(*cmd); cmd++); - if (!*cmd) return; - /* see if it matches a builtin tag */ - Tag* tag = tag_lookup(cmd); - if (tag) { - while (*cmd && !isspace(*cmd++)); - tag_exec(tag, (*cmd ? stringdup(cmd) : NULL)); - } else { - cmd_exec(cmd); - } -} - -/* Action Callbacks - ******************************************************************************/ static void quit(char* arg) { win_quit(); } @@ -280,8 +273,6 @@ static void get(char* arg) { view_reload(win_view(EDIT)); } -/* Keyboard Handling - ******************************************************************************/ static void tag_undo(char* arg) { view_undo(win_view(EDIT)); } -- 2.52.0