From: Sadrul Habib Chowdhury Date: Sat, 21 Feb 2009 08:40:28 +0000 (-0500) Subject: Allow a callback after every command X-Git-Url: https://repo.or.cz/w/screen-lua.git/commitdiff_plain/ac07470f553eda8ef349bf26e508cee491da1f3d Allow a callback after every command Scripts can choose to do some additional task after selected commands. It may be possible to have some callbacks that will be triggered before the original callback, thus possibly overriding it. --- diff --git a/src/lua.c b/src/lua.c index fcd7f4a..7b51085 100644 --- a/src/lua.c +++ b/src/lua.c @@ -690,6 +690,32 @@ LuaProcessCaption(const char *caption, struct win *win, int len) return LuaCallProcess("process_caption", params); } +static void +push_stringarray(lua_State *L, void *data) +{ + char **args = (char **)data; + int i; + lua_newtable(L); + for (i = 1; args && *args; i++) { + lua_pushinteger(L, i); + lua_pushstring(L, *args++); + lua_settable(L, -3); + } +} + +int +LuaCommandExecuted(const char *command, const char **args, int argc) +{ + if (!L) + return 0; + struct fn_def params[] = { + {lua_pushstring, command}, + {push_stringarray, args}, + {NULL, NULL} + }; + return LuaCallProcess("command_executed", params); +} + /** }}} */ struct ScriptFuncs LuaFuncs = @@ -698,6 +724,7 @@ struct ScriptFuncs LuaFuncs = LuaFinit, LuaForeWindowChanged, LuaSource, - LuaProcessCaption + LuaProcessCaption, + LuaCommandExecuted }; diff --git a/src/process.c b/src/process.c index ccc8e5d..e08740e 100644 --- a/src/process.c +++ b/src/process.c @@ -4406,6 +4406,8 @@ int key; #endif break; } + if (nr < RC_LAST) + ScriptCommandExecuted(comms[nr].name, args, argc); if (display != odisplay) { for (display = displays; display; display = display->d_next) diff --git a/src/script.c b/src/script.c index ca9f512..e950099 100644 --- a/src/script.c +++ b/src/script.c @@ -81,6 +81,13 @@ int ScriptProcessCaption(const char *str, struct win *win, int len) return ret; } +int ScriptCommandExecuted(const char *command, const char **args, int argc) +{ + int ret = 0; + ALL_SCRIPTS(sf_CommandExecuted, (command, args, argc), 0); + return ret; +} + #define HAVE_LUA 1 /* XXX: Remove */ #if HAVE_LUA extern struct ScriptFuncs LuaFuncs; diff --git a/src/script.h b/src/script.h index 5a3c0c5..78259b9 100644 --- a/src/script.h +++ b/src/script.h @@ -28,6 +28,7 @@ struct ScriptFuncs int (*sf_ForeWindowChanged) __P((void)); int (*sf_Source) __P((const char *)); int (*sf_ProcessCaption) __P((const char *, struct win *, int len)); + int (*sf_CommandExecuted) __P((const char *, const char **, int)); }; void LoadScripts(void); diff --git a/src/scripts/cmdcallback.lua b/src/scripts/cmdcallback.lua new file mode 100644 index 0000000..cc5d2a3 --- /dev/null +++ b/src/scripts/cmdcallback.lua @@ -0,0 +1,13 @@ +--[[ For now, this sample function will simply record all the commands executed ]]-- +function command_executed(name, args) + local f = io.open('/tmp/debug/ll', 'a') + f:write("Command executed: " .. name) + + for i, c in pairs(args) do + f:write(" " .. c) + end + + f:write("\n") + f:close() +end +