1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-09-29 19:56:07 -04:00

Started Lua command callbacks

This commit is contained in:
James Booth 2013-09-15 18:38:08 +01:00
parent ab1c140935
commit e1f17861fd
4 changed files with 60 additions and 48 deletions

View File

@ -132,7 +132,8 @@ fi
AM_CFLAGS="-Wall -export-dynamic"
if test "x$PACKAGE_STATUS" = xdevelopment; then
AM_CFLAGS="$AM_CFLAGS -Wunused -Werror"
# AM_CFLAGS="$AM_CFLAGS -Wunused -Werror"
AM_CFLAGS="$AM_CFLAGS -Wunused"
fi
LIBS="$LIBS $DEPS_LIBS $NOTIFY_LIBS"

View File

@ -20,7 +20,12 @@
*
*/
#include <stdlib.h>
#include <string.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <glib.h>
@ -46,27 +51,19 @@ lua_api_cons_show(lua_State *L)
static int
lua_api_register_command(lua_State *L)
{
/*
const char *command_name = NULL;
int min_args = 0;
int max_args = 0;
const char *usage = NULL;
const char *short_help = NULL;
const char *long_help = NULL;
PyObject *p_callback = NULL;
const char *command_name = lua_tostring(L, -7);
int min_args = lua_tonumber(L, -6);
int max_args = lua_tonumber(L, -5);
const char *usage = lua_tostring(L, -4);
const char *short_help = lua_tostring(L, -3);
const char *long_help = lua_tostring(L, -2);
if (!PyArg_ParseTuple(args, "siisssO", &command_name, &min_args, &max_args,
&usage, &short_help, &long_help, &p_callback)) {
return Py_BuildValue("");
}
int *p_callback_ref = malloc(sizeof(int));
*p_callback_ref = luaL_ref(L, LUA_REGISTRYINDEX);
if (p_callback && PyCallable_Check(p_callback)) {
api_register_command(command_name, min_args, max_args, usage,
short_help, long_help, p_callback, python_command_callback);
}
api_register_command(command_name, min_args, max_args, usage, short_help,
long_help, p_callback_ref, lua_command_callback);
return Py_BuildValue("");
*/
return 0;
}
@ -106,7 +103,7 @@ static int
lua_api_send_line(lua_State *L)
{
const char *line = lua_tostring(L, -1);
api_send_line(line);
api_send_line(strdup(line));
return 0;
}
@ -203,7 +200,7 @@ lua_api_win_process_line(lua_State *L)
{
const char *tag = lua_tostring(L, -2);
const char *line = lua_tostring(L, -1);
api_win_process_line(tag, line);
api_win_process_line(tag, strdup(line));
return 0;
}
@ -219,44 +216,50 @@ lua_api_win_show(lua_State *L)
void
lua_command_callback(PluginCommand *command, gchar **args)
{
/*
PyObject *p_args = NULL;
lua_State *L = lua_get_state();
int *p_ref = (int *)command->callback;
int num_args = g_strv_length(args);
if (num_args == 0) {
if (command->max_args == 1) {
p_args = Py_BuildValue("(O)", Py_BuildValue(""));
PyObject_CallObject(command->callback, p_args);
Py_XDECREF(p_args);
lua_pushnil(L);
lua_rawgeti(L, LUA_REGISTRYINDEX, *p_ref);
lua_pcall(L, 1, 0, 0);
} else {
PyObject_CallObject(command->callback, p_args);
lua_rawgeti(L, LUA_REGISTRYINDEX, *p_ref);
lua_pcall(L, 0, 0, 0);
}
} else if (num_args == 1) {
p_args = Py_BuildValue("(s)", args[0]);
PyObject_CallObject(command->callback, p_args);
Py_XDECREF(p_args);
lua_pushstring(L, args[0]);
lua_rawgeti(L, LUA_REGISTRYINDEX, *p_ref);
lua_pcall(L, 1, 0, 0);
} else if (num_args == 2) {
p_args = Py_BuildValue("ss", args[0], args[1]);
PyObject_CallObject(command->callback, p_args);
Py_XDECREF(p_args);
lua_pushstring(L, args[0]);
lua_pushstring(L, args[1]);
lua_rawgeti(L, LUA_REGISTRYINDEX, *p_ref);
lua_pcall(L, 2, 0, 0);
} else if (num_args == 3) {
p_args = Py_BuildValue("sss", args[0], args[1], args[2]);
PyObject_CallObject(command->callback, p_args);
Py_XDECREF(p_args);
lua_pushstring(L, args[0]);
lua_pushstring(L, args[1]);
lua_pushstring(L, args[2]);
lua_rawgeti(L, LUA_REGISTRYINDEX, *p_ref);
lua_pcall(L, 3, 0, 0);
} else if (num_args == 4) {
p_args = Py_BuildValue("ssss", args[0], args[1], args[2], args[3]);
PyObject_CallObject(command->callback, p_args);
Py_XDECREF(p_args);
lua_pushstring(L, args[0]);
lua_pushstring(L, args[1]);
lua_pushstring(L, args[2]);
lua_pushstring(L, args[3]);
lua_rawgeti(L, LUA_REGISTRYINDEX, *p_ref);
lua_pcall(L, 4, 0, 0);
} else if (num_args == 5) {
p_args = Py_BuildValue("sssss", args[0], args[1], args[2], args[3], args[4]);
PyObject_CallObject(command->callback, p_args);
Py_XDECREF(p_args);
lua_pushstring(L, args[0]);
lua_pushstring(L, args[1]);
lua_pushstring(L, args[2]);
lua_pushstring(L, args[3]);
lua_pushstring(L, args[4]);
lua_rawgeti(L, LUA_REGISTRYINDEX, *p_ref);
lua_pcall(L, 5, 0, 0);
}
if (PyErr_Occurred()) {
PyErr_Print();
PyErr_Clear();
}
*/
}
void

View File

@ -25,6 +25,8 @@
#include <lua.h>
lua_State * lua_get_state(void);
void lua_env_init(void);
void lua_api_init(lua_State *L);
void lua_shutdown(void);

View File

@ -39,6 +39,12 @@ static lua_State *L;
static void _l_stackDump(lua_State *L);
lua_State *
lua_get_state(void)
{
return L;
}
void
lua_env_init(void)
{