[elinks-dev] Use event system for keybinding.

Karsten Schölzel kuser at gmx.de
Wed Oct 1 05:16:33 PDT 2003


This patch uses my patch which adds a data pointer to event_handler and
event_hook to make the key binding functionality available for other
scripting backends.
-------------- next part --------------
Index: src/viewer/text/view.c
===================================================================
--- src/viewer/text/view.c	(revision 356)
+++ src/viewer/text/view.c	(revision 357)
@@ -34,6 +34,7 @@
 #include "protocol/http/auth.h"
 #include "protocol/uri.h"
 #include "sched/download.h"
+#include "sched/event.h"
 #include "sched/history.h"
 #include "sched/location.h"
 #include "sched/session.h"
@@ -1067,9 +1068,9 @@
 				dialog_lua_console(ses);
 #endif
 				goto x;
-			case ACT_LUA_FUNCTION:
-#ifdef HAVE_LUA
-				run_lua_func(ses, func_ref);
+			case ACT_SCRIPTING_FUNCTION:
+#ifdef HAVE_SCRIPTING
+				trigger_event(func_ref, ses);
 #endif
 				break;
 			case ACT_QUIT:
Index: src/scripting/lua/core.c
===================================================================
--- src/scripting/lua/core.c	(revision 356)
+++ src/scripting/lua/core.c	(revision 357)
@@ -34,6 +34,7 @@
 #include "lowlevel/home.h"
 #include "lowlevel/signals.h"
 #include "protocol/uri.h"
+#include "sched/event.h"
 #include "sched/session.h"
 #include "scripting/lua/core.h"
 #include "scripting/lua/hooks.h"
@@ -280,6 +281,8 @@
 {
 	int ref;
 	unsigned char *err;
+	struct string event_name = NULL_STRING;
+	int event_id;
 
 	if (!lua_isstring(S, 1) || !lua_isstring(S, 2)
 	    || !lua_isfunction(S, 3)) {
@@ -287,11 +290,18 @@
 		goto lua_error;
 	}
 
+	if (!init_string(&event_name)) goto lua_error;
+		
 	lua_pushvalue(S, 3);
 	ref = lua_ref(S, 1);
+	add_format_to_string(&event_name, "lua-run-func %i", ref);
+	event_id = register_event(event_name.source);
+	event_id = register_event_hook(event_id, run_lua_func, 0, (void *)ref); 
+	done_string(&event_name);
+	if (event_id == EVENT_NONE) goto lua_error;
 
-	err = bind_lua_func((uchar *)lua_tostring(S, 1),
-			    (uchar *)lua_tostring(S, 2), ref);
+	err = bind_scripting_func((uchar *)lua_tostring(S, 1),
+			    (uchar *)lua_tostring(S, 2), event_id);
 	if (err) {
 		lua_unref(S, ref);
 		alert_lua_error2("error in bind_key: ", err);
@@ -830,21 +840,25 @@
  * Helper to run Lua functions bound to keystrokes.
  */
 
-void
-run_lua_func(struct session *ses, int func_ref)
+enum evhook_status
+run_lua_func(va_list ap, void *data)
 {
+	struct session *ses = va_arg(ap, struct session *);
+	int func_ref = (int)data;
 	int err;
 
 	if (func_ref == LUA_NOREF) {
 		alert_lua_error("key bound to nothing (internal error)");
-		return;
+		return EHS_NEXT;
 	}
 
 	lua_getref(L, func_ref);
-	if (prepare_lua(ses)) return;
+	if (prepare_lua(ses)) return EHS_NEXT;
 	err = lua_call(L, 0, 2);
 	finish_lua();
 	if (!err) handle_standard_lua_returns("keyboard function");
+
+	return EHS_NEXT;
 }
 
 
Index: src/scripting/lua/core.h
===================================================================
--- src/scripting/lua/core.h	(revision 356)
+++ src/scripting/lua/core.h	(revision 357)
@@ -21,7 +21,7 @@
 void dialog_lua_console(struct session *);
 void free_lua_console_history(void);
 
-void run_lua_func(struct session *, int);
+enum evhook_status run_lua_func(va_list ap, void *data);
 
 extern struct scripting_backend lua_scripting_backend;
 
Index: src/config/kbdbind.h
===================================================================
--- src/config/kbdbind.h	(revision 356)
+++ src/config/kbdbind.h	(revision 357)
@@ -59,7 +59,6 @@
 	ACT_LINK_MENU,
 	ACT_JUMP_TO_LINK,
 	ACT_LUA_CONSOLE,
-	ACT_LUA_FUNCTION,
 	ACT_MENU,
 	ACT_NEXT_FRAME,
 	ACT_OPEN_NEW_WINDOW,
@@ -75,6 +74,7 @@
 	ACT_RESUME_DOWNLOAD,
 	ACT_RIGHT,
 	ACT_SAVE_FORMATTED,
+	ACT_SCRIPTING_FUNCTION,
 	ACT_SCROLL_DOWN,
 	ACT_SCROLL_LEFT,
 	ACT_SCROLL_RIGHT,
@@ -140,8 +140,8 @@
 unsigned char *bind_act(unsigned char *, unsigned char *);
 void bind_config_string(struct string *);
 
-#ifdef HAVE_LUA
-unsigned char *bind_lua_func(unsigned char *, unsigned char *, int);
+#ifdef HAVE_SCRIPTING
+unsigned char *bind_scripting_func(unsigned char *, unsigned char *, int);
 #endif
 
 #endif
Index: src/config/kbdbind.c
===================================================================
--- src/config/kbdbind.c	(revision 356)
+++ src/config/kbdbind.c	(revision 357)
@@ -15,15 +15,12 @@
 #include "config/kbdbind.h"
 #include "config/options.h"
 #include "intl/gettext/libintl.h"
+#include "sched/event.h"
 #include "scripting/lua/core.h"
 #include "terminal/kbd.h"
 #include "util/memory.h"
 #include "util/string.h"
 
-#ifndef HAVE_LUA
-#define LUA_NOREF	0
-#endif
-
 #define table table_dirty_workaround_for_name_clash_with_libraries_on_macos
 
 
@@ -111,9 +108,10 @@
 		del_from_list(kb->box_item);
 		mem_free(kb->box_item);
 	}
-#ifdef HAVE_LUA
-	if (kb->func_ref != LUA_NOREF)
-		lua_unref(lua_state, kb->func_ref);
+#ifdef HAVE_SCRIPTING
+/* TODO: unref function must be implemented. */
+/*	if (kb->func_ref != EVENT_NONE)
+		scripting_unref(kb->func_ref); */
 #endif
 	del_from_list(kb);
 	mem_free(kb);
@@ -178,7 +176,7 @@
 		if (key != kb->key || meta != kb->meta)
 			continue;
 
-		if (kb->action == ACT_LUA_FUNCTION && func_ref)
+		if (kb->action == ACT_SCRIPTING_FUNCTION && func_ref)
 			*func_ref = kb->func_ref;
 
 		return kb;
@@ -199,7 +197,7 @@
 		if (act != kb->action)
 			continue;
 
-		if (kb->action == ACT_LUA_FUNCTION && func_ref)
+		if (kb->action == ACT_SCRIPTING_FUNCTION && func_ref)
 			*func_ref = kb->func_ref;
 
 		return kb;
@@ -409,7 +407,7 @@
 #else
 	{ "lua-console", ACT_LUA_CONSOLE, N_("Open a Lua console (DISABLED)") },
 #endif
-	{ " *lua-function*", ACT_LUA_FUNCTION, NULL }, /* internal use only */
+	{ " *scripting-function*", ACT_SCRIPTING_FUNCTION, NULL }, /* internal use only */
 	{ "menu", ACT_MENU, N_("Activate the menu") },
 	{ "next-frame", ACT_NEXT_FRAME, N_("Move to the next frame") },
 	{ "open-new-window", ACT_OPEN_NEW_WINDOW, N_("Open a new window") },
@@ -473,7 +471,7 @@
 		add_to_list_bottom(kbdbind_box_items, box_item);
 		box_item->root = NULL;
 		init_list(box_item->child);
-		box_item->visible = (act->num != ACT_LUA_FUNCTION); /* XXX */
+		box_item->visible = (act->num != ACT_SCRIPTING_FUNCTION); /* XXX */
 		box_item->translated = 1;
 		box_item->udata = (void *) act->num;
 		box_item->type = BI_FOLDER;
@@ -558,7 +556,7 @@
 	action_ = read_action(action);
 	if (action_ < 0) return 3;
 
-	add_keybinding(keymap_, action_, key_, meta_, LUA_NOREF);
+	add_keybinding(keymap_, action_, key_, meta_, EVENT_NONE);
 	return 0;
 }
 
@@ -625,9 +623,9 @@
  * Bind to Lua function.
  */
 
-#ifdef HAVE_LUA
+#ifdef HAVE_SCRIPTING
 unsigned char *
-bind_lua_func(unsigned char *ckmap, unsigned char *ckey, int func_ref)
+bind_scripting_func(unsigned char *ckmap, unsigned char *ckey, int func_ref)
 {
 	unsigned char *err = NULL;
 	long key, meta;
@@ -638,7 +636,7 @@
 		err = gettext("Unrecognised keymap");
 	else if (parse_keystroke(ckey, &key, &meta) < 0)
 		err = gettext("Error parsing keystroke");
-	else if ((action = read_action(" *lua-function*")) < 0)
+	else if ((action = read_action(" *scripting-function*")) < 0)
 		err = gettext("Unrecognised action (internal error)");
 	else
 		add_keybinding(kmap, action, key, meta, func_ref);
@@ -792,11 +790,11 @@
 	 * defaults, can we? ;)) */
 
 	for (kb = default_main_keymap; kb->key; kb++)
-		add_keybinding(KM_MAIN, kb->action, kb->key, kb->meta, LUA_NOREF);
+		add_keybinding(KM_MAIN, kb->action, kb->key, kb->meta, EVENT_NONE);
 
 	for (kb = default_edit_keymap; kb->key; kb++)
-		add_keybinding(KM_EDIT, kb->action, kb->key, kb->meta, LUA_NOREF);
+		add_keybinding(KM_EDIT, kb->action, kb->key, kb->meta, EVENT_NONE);
 
 	for (kb = default_menu_keymap; kb->key; kb++)
-		add_keybinding(KM_MENU, kb->action, kb->key, kb->meta, LUA_NOREF);
+		add_keybinding(KM_MENU, kb->action, kb->key, kb->meta, EVENT_NONE);
 }


More information about the elinks-dev mailing list