mirror of
https://github.com/weechat/weechat.git
synced 2026-06-28 22:06:38 +02:00
core: add command /eval, use expression in conditions for bars, add function "string_eval_expression" in plugin API
This commit is contained in:
@@ -460,6 +460,42 @@ weechat_lua_api_string_input_for_buffer (lua_State *L)
|
||||
API_RETURN_STRING(result);
|
||||
}
|
||||
|
||||
/*
|
||||
* weechat_lua_api_string_eval_expression: evaluate an expression and return
|
||||
* result
|
||||
*/
|
||||
|
||||
static int
|
||||
weechat_lua_api_string_eval_expression (lua_State *L)
|
||||
{
|
||||
const char *expr;
|
||||
struct t_hashtable *pointers, *extra_vars;
|
||||
char *result;
|
||||
|
||||
API_FUNC(1, "string_eval_expression", API_RETURN_EMPTY);
|
||||
if (lua_gettop (lua_current_interpreter) < 3)
|
||||
API_WRONG_ARGS(API_RETURN_EMPTY);
|
||||
|
||||
expr = lua_tostring (lua_current_interpreter, -3);
|
||||
pointers = weechat_lua_tohashtable (lua_current_interpreter, -2,
|
||||
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
|
||||
WEECHAT_HASHTABLE_STRING,
|
||||
WEECHAT_HASHTABLE_POINTER);
|
||||
extra_vars = weechat_lua_tohashtable (lua_current_interpreter, -1,
|
||||
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
|
||||
WEECHAT_HASHTABLE_STRING,
|
||||
WEECHAT_HASHTABLE_STRING);
|
||||
|
||||
result = weechat_string_eval_expression (expr, pointers, extra_vars);
|
||||
|
||||
if (pointers)
|
||||
weechat_hashtable_free (pointers);
|
||||
if (extra_vars)
|
||||
weechat_hashtable_free (extra_vars);
|
||||
|
||||
API_RETURN_STRING_FREE(result);
|
||||
}
|
||||
|
||||
/*
|
||||
* weechat_lua_api_mkdir_home: create a directory in WeeChat home
|
||||
*/
|
||||
@@ -2133,7 +2169,9 @@ weechat_lua_api_key_bind (lua_State *L)
|
||||
|
||||
context = lua_tostring (lua_current_interpreter, -2);
|
||||
hashtable = weechat_lua_tohashtable (lua_current_interpreter, -1,
|
||||
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE);
|
||||
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
|
||||
WEECHAT_HASHTABLE_STRING,
|
||||
WEECHAT_HASHTABLE_STRING);
|
||||
|
||||
num_keys = weechat_key_bind (context, hashtable);
|
||||
|
||||
@@ -2702,7 +2740,9 @@ weechat_lua_api_hook_process_hashtable (lua_State *L)
|
||||
|
||||
command = lua_tostring (lua_current_interpreter, -5);
|
||||
options = weechat_lua_tohashtable (lua_current_interpreter, -4,
|
||||
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE);
|
||||
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
|
||||
WEECHAT_HASHTABLE_STRING,
|
||||
WEECHAT_HASHTABLE_STRING);
|
||||
timeout = lua_tonumber (lua_current_interpreter, -3);
|
||||
function = lua_tostring (lua_current_interpreter, -2);
|
||||
data = lua_tostring (lua_current_interpreter, -1);
|
||||
@@ -3127,7 +3167,9 @@ weechat_lua_api_hook_hsignal_send (lua_State *L)
|
||||
|
||||
signal = lua_tostring (lua_current_interpreter, -2);
|
||||
hashtable = weechat_lua_tohashtable (lua_current_interpreter, -1,
|
||||
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE);
|
||||
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
|
||||
WEECHAT_HASHTABLE_STRING,
|
||||
WEECHAT_HASHTABLE_STRING);
|
||||
|
||||
weechat_hook_hsignal_send (signal, hashtable);
|
||||
|
||||
@@ -4899,7 +4941,9 @@ weechat_lua_api_info_get_hashtable (lua_State *L)
|
||||
|
||||
info_name = lua_tostring (lua_current_interpreter, -2);
|
||||
table = weechat_lua_tohashtable (lua_current_interpreter, -1,
|
||||
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE);
|
||||
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
|
||||
WEECHAT_HASHTABLE_STRING,
|
||||
WEECHAT_HASHTABLE_STRING);
|
||||
|
||||
result_hashtable = weechat_info_get_hashtable (info_name, table);
|
||||
|
||||
@@ -5695,7 +5739,9 @@ weechat_lua_api_hdata_update (lua_State *L)
|
||||
hdata = lua_tostring (lua_current_interpreter, -3);
|
||||
pointer = lua_tostring (lua_current_interpreter, -2);
|
||||
hashtable = weechat_lua_tohashtable (lua_current_interpreter, -1,
|
||||
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE);
|
||||
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
|
||||
WEECHAT_HASHTABLE_STRING,
|
||||
WEECHAT_HASHTABLE_STRING);
|
||||
|
||||
value = weechat_hdata_update (API_STR2PTR(hdata),
|
||||
API_STR2PTR(pointer),
|
||||
@@ -6296,6 +6342,7 @@ const struct luaL_Reg weechat_lua_api_funcs[] = {
|
||||
API_DEF_FUNC(string_remove_color),
|
||||
API_DEF_FUNC(string_is_command_char),
|
||||
API_DEF_FUNC(string_input_for_buffer),
|
||||
API_DEF_FUNC(string_eval_expression),
|
||||
API_DEF_FUNC(mkdir_home),
|
||||
API_DEF_FUNC(mkdir),
|
||||
API_DEF_FUNC(mkdir_parents),
|
||||
|
||||
@@ -108,20 +108,19 @@ weechat_lua_pushhashtable (lua_State *interpreter, struct t_hashtable *hashtable
|
||||
|
||||
/*
|
||||
* weechat_lua_hash_to_hashtable: get WeeChat hashtable with lua hash (on stack)
|
||||
* Hashtable returned has type string for
|
||||
* both keys and values
|
||||
* Note: hashtable has to be released after use
|
||||
* with call to weechat_hashtable_free()
|
||||
*/
|
||||
|
||||
struct t_hashtable *
|
||||
weechat_lua_tohashtable (lua_State *interpreter, int index, int hashtable_size)
|
||||
weechat_lua_tohashtable (lua_State *interpreter, int index, int size,
|
||||
const char *type_keys, const char *type_values)
|
||||
{
|
||||
struct t_hashtable *hashtable;
|
||||
|
||||
hashtable = weechat_hashtable_new (hashtable_size,
|
||||
WEECHAT_HASHTABLE_STRING,
|
||||
WEECHAT_HASHTABLE_STRING,
|
||||
hashtable = weechat_hashtable_new (size,
|
||||
type_keys,
|
||||
type_values,
|
||||
NULL,
|
||||
NULL);
|
||||
if (!hashtable)
|
||||
@@ -130,9 +129,20 @@ weechat_lua_tohashtable (lua_State *interpreter, int index, int hashtable_size)
|
||||
lua_pushnil (interpreter);
|
||||
while (lua_next (interpreter, index - 1) != 0)
|
||||
{
|
||||
weechat_hashtable_set (hashtable,
|
||||
lua_tostring (interpreter, -2),
|
||||
lua_tostring (interpreter, -1));
|
||||
if (strcmp (type_values, WEECHAT_HASHTABLE_STRING) == 0)
|
||||
{
|
||||
weechat_hashtable_set (hashtable,
|
||||
lua_tostring (interpreter, -2),
|
||||
lua_tostring (interpreter, -1));
|
||||
}
|
||||
else if (strcmp (type_values, WEECHAT_HASHTABLE_POINTER) == 0)
|
||||
{
|
||||
weechat_hashtable_set (hashtable,
|
||||
lua_tostring (interpreter, -2),
|
||||
plugin_script_str2ptr (weechat_lua_plugin,
|
||||
NULL, NULL,
|
||||
lua_tostring (interpreter, -1)));
|
||||
}
|
||||
/* remove value from stack (keep key for next iteration) */
|
||||
lua_pop (interpreter, 1);
|
||||
}
|
||||
@@ -209,7 +219,9 @@ weechat_lua_exec (struct t_plugin_script *script, int ret_type,
|
||||
else if (ret_type == WEECHAT_SCRIPT_EXEC_HASHTABLE)
|
||||
{
|
||||
ret_value = weechat_lua_tohashtable (lua_current_interpreter, -1,
|
||||
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE);
|
||||
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
|
||||
WEECHAT_HASHTABLE_STRING,
|
||||
WEECHAT_HASHTABLE_STRING);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -40,7 +40,9 @@ extern void weechat_lua_pushhashtable (lua_State *interpreter,
|
||||
struct t_hashtable *hashtable);
|
||||
extern struct t_hashtable *weechat_lua_tohashtable (lua_State *interpreter,
|
||||
int index,
|
||||
int hashtable_size);
|
||||
int size,
|
||||
const char *type_keys,
|
||||
const char *type_values);
|
||||
extern void *weechat_lua_exec (struct t_plugin_script *script, int ret_type,
|
||||
const char *function,
|
||||
const char *format, void **argv);
|
||||
|
||||
Reference in New Issue
Block a user