Hi! Here is a patch to let Lua access ELinks configuration variables.
It adds two new functions available from Lua scripts: set_option(name, value)
and get_option(name).
Sergey
diff -ur elinks-0.11-20050329.orig/src/scripting/lua/core.c
elinks-0.11-20050329/src/scripting/lua/core.c
--- elinks-0.11-20050329.orig/src/scripting/lua/core.c 2005-03-28
01:58:11.000000000 +0400
+++ elinks-0.11-20050329/src/scripting/lua/core.c 2005-03-31
01:39:25.000000000 +0400
@@ -19,12 +19,16 @@
#include "elinks.h"
#include "bfu/dialog.h"
+#include "bfu/listbox.h"
#include "cache/cache.h"
#include "config/kbdbind.h"
+#include "config/options.h"
+#include "config/opttypes.h"
#include "document/document.h"
#include "document/renderer.h"
#include "document/view.h"
#include "intl/gettext/libintl.h"
+#include "intl/charsets.h"
#include "lowlevel/home.h"
#include "lowlevel/signals.h"
#include "modules/module.h"
@@ -477,6 +481,107 @@
/* End xdialog bit. */
+/* Set/get option */
+
+static int
+l_set_option(LS)
+{
+ int nargs;
+ struct option *opt, *current;
+ const char *name;
+
+ nargs = lua_gettop(S);
+ if (nargs != 2)
+ goto lua_error;
+
+ /* Get option record */
+ name = lua_tostring(S, 1);
+ opt = get_opt_rec(config_options, (unsigned char *)name);
+ if (opt == NULL)
+ return 0;
+
+ /* Set option */
+ if (opt->type == OPT_BOOL || opt->type == OPT_INT ||
+ opt->type == OPT_LONG) {
+ int value;
+
+ value = lua_tonumber(S, 2);
+ option_types[opt->type].set(opt, (unsigned char *)(&value));
+ }
+ else
+ option_types[opt->type].set(opt, (unsigned char
*)lua_tostring(S, 2));
+
+ /* Call hook */
+ current = opt;
+ while (current && (!current->change_hook ||
+ !current->change_hook(lua_ses, current, opt))) {
+ if (current->root)
+ current = current->root;
+ else
+ break;
+ }
+ return 1;
+
+lua_error:
+ lua_pushnil(S);
+ return 1;
+}
+
+static int
+l_get_option(LS)
+{
+ int nargs;
+ struct option *opt;
+ const char *name;
+
+ /* Get option record */
+ nargs = lua_gettop(S);
+ if (nargs != 1)
+ goto lua_error;
+ name = lua_tostring(S, 1);
+ opt = get_opt_rec(config_options, (unsigned char *)name);
+
+ /* Convert to an appropriate Lua type */
+ if (opt == NULL || opt->type == OPT_COMMAND)
+ lua_pushnil(S);
+ else if (opt->type == OPT_BOOL)
+ lua_pushboolean(S, opt->value.number);
+ else if (opt->type == OPT_INT || opt->type == OPT_LONG)
+ lua_pushnumber(S, opt->value.number);
+ else if (opt->type == OPT_STRING)
+ lua_pushstring(S, opt->value.string);
+ else if (opt->type == OPT_CODEPAGE) {
+ char *cp_name;
+ cp_name = get_cp_mime_name(opt->value.number);
+ lua_pushstring(S, cp_name);
+ }
+ else if (opt->type == OPT_LANGUAGE) {
+ char *lang;
+
+#ifdef ENABLE_NLS
+ lang = language_to_name(current_language);
+#else
+ lang = "System";
+#endif
+ lua_pushstring(S, lang);
+ }
+ else if (opt->type == OPT_COLOR) {
+ color_T color;
+ unsigned char hexcolor[8];
+ unsigned char *strcolor;
+
+ color = opt->value.color;
+ strcolor = get_color_string(color, hexcolor);
+ lua_pushstring(S, strcolor);
+ }
+ return 1;
+
+lua_error:
+ lua_pushnil(S);
+ return 1;
+}
+
+/* End of set/get option */
/* Initialisation */
@@ -521,6 +626,8 @@
lua_register(L, "bind_key", l_bind_key);
lua_register(L, "edit_bookmark_dialog", l_edit_bookmark_dialog);
lua_register(L, "xdialog", l_xdialog);
+ lua_register(L, "set_option", l_set_option);
+ lua_register(L, "get_option", l_get_option);
lua_dostring(L, "function set_elinks_home(s) elinks_home = s end");
lua_getglobal(L, "set_elinks_home");
_______________________________________________
elinks-dev mailing list
[email protected]
http://linuxfromscratch.org/mailman/listinfo/elinks-dev