Skip to content

Commit

Permalink
LuaFAR: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
shmuz committed May 19, 2024
1 parent cdba947 commit e409d24
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 33 deletions.
2 changes: 1 addition & 1 deletion plugins/luamacro/_globalinfo.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function export.GetGlobalInfo()
return {
Version = { 3, 0, 0, 832 },
Version = { 3, 0, 0, 833 },
MinFarVersion = { 3, 0, 0, 6327 },
Guid = win.Uuid("4EBBEFC8-2084-4B7F-94C0-692CE136894D"),
Title = "LuaMacro",
Expand Down
4 changes: 4 additions & 0 deletions plugins/luamacro/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
shmuel 2024-05-19 11:12:10+03:00 - build 833

1. LuaFAR: refactoring.

shmuel 2024-05-15 17:51:04+03:00 - build 832

1. Add command lm:browser (or macro:browser).
Expand Down
69 changes: 38 additions & 31 deletions plugins/luamacro/luafar/service.c
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,10 @@ void PutMouseEvent(lua_State *L, const MOUSE_EVENT_RECORD* rec, BOOL table_exist

// convert a string from utf-8 to wide char and put it into a table,
// to prevent stack overflow and garbage collection
const wchar_t* StoreTempString(lua_State *L, int store_stack_pos, int* index)
static const wchar_t* StoreTempString(lua_State *L, int store_stack_pos)
{
const wchar_t *s = check_utf8_string(L,-1,NULL);
lua_rawseti(L, store_stack_pos, ++(*index));
luaL_ref(L, store_stack_pos);
return s;
}

Expand Down Expand Up @@ -966,7 +966,7 @@ static int editor_UndoRedo(lua_State *L)

static void FillKeyBarTitles(lua_State *L, int src_pos, struct KeyBarTitles *kbt)
{
int store=0, store_pos, i;
int store_pos, i;
size_t size;
lua_newtable(L);
store_pos = lua_gettop(L);
Expand All @@ -992,10 +992,10 @@ static void FillKeyBarTitles(lua_State *L, int src_pos, struct KeyBarTitles *kbt
kbt->Labels[i].Key.ControlKeyState = CAST(DWORD,CheckFlagsFromTable(L, -1, "ControlKeyState"));
//-----------------------------------------------------------------------
lua_getfield(L, -1, "Text");
kbt->Labels[i].Text = StoreTempString(L, store_pos, &store);
kbt->Labels[i].Text = StoreTempString(L, store_pos);
//-----------------------------------------------------------------------
lua_getfield(L, -1, "LongText");
kbt->Labels[i].LongText = StoreTempString(L, store_pos, &store);
kbt->Labels[i].LongText = StoreTempString(L, store_pos);
//-----------------------------------------------------------------------
lua_pop(L, 1);
}
Expand Down Expand Up @@ -1684,52 +1684,59 @@ static int editor_SubscribeChangeEvent(lua_State *L)
// a nil -- menu canceled by the user
static int far_Menu(lua_State *L)
{
enum {
POS_PROPS = 1, // properties
POS_ITEMS = 2, // items
POS_BKEYS = 3, // break keys
POS_STORE = 4, // temporary storage
};

TPluginData *pd = GetPluginData(L);
int X = -1, Y = -1, MaxHeight = 0;
UINT64 Flags = FMENU_WRAPMODE;
const wchar_t *Title = L"Menu", *Bottom = NULL, *HelpTopic = NULL;
intptr_t SelectIndex = 0, ItemsNumber, ret;
int store = 0, i;
int i;
intptr_t BreakCode = 0, *pBreakCode;
int NumBreakCodes = 0;
const GUID* MenuGuid = NULL;
struct FarMenuItem *Items, *pItem;
struct FarKey *pBreakKeys;
lua_settop(L, 3); // cut unneeded parameters; make stack predictable
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checktype(L, 2, LUA_TTABLE);
ItemsNumber = lua_objlen(L, 2);
lua_settop(L, POS_BKEYS); // cut unneeded parameters; make stack predictable
luaL_checktype(L, POS_PROPS, LUA_TTABLE);
luaL_checktype(L, POS_ITEMS, LUA_TTABLE);
ItemsNumber = lua_objlen(L, POS_ITEMS);

if (!lua_isnil(L,3) && !lua_istable(L,3) && lua_type(L,3)!=LUA_TSTRING)
return luaL_argerror(L, 3, "must be table, string or nil");
if (!lua_isnil(L,POS_BKEYS) && !lua_istable(L,POS_BKEYS) && lua_type(L,POS_BKEYS)!=LUA_TSTRING)
return luaL_argerror(L, POS_BKEYS, "must be table, string or nil");

lua_newtable(L); // temporary store; at stack position 4
// Properties
lua_pushvalue(L,1); //+1
lua_pushvalue(L, POS_PROPS); //+1
X = GetOptIntFromTable(L, "X", -1);
Y = GetOptIntFromTable(L, "Y", -1);
MaxHeight = GetOptIntFromTable(L, "MaxHeight", 0);
lua_getfield(L, 1, "Flags"); //+2
lua_getfield(L, POS_PROPS, "Flags"); //+2

if (!lua_isnil(L, -1)) Flags = CheckFlags(L, -1);

lua_getfield(L, 1, "Title"); //+3
lua_getfield(L, POS_PROPS, "Title"); //+3

if (lua_isstring(L,-1)) Title = StoreTempString(L, 4, &store);
if (lua_isstring(L,-1)) Title = StoreTempString(L, POS_STORE);

lua_getfield(L, 1, "Bottom"); //+3
lua_getfield(L, POS_PROPS, "Bottom"); //+3

if (lua_isstring(L,-1)) Bottom = StoreTempString(L, 4, &store);
if (lua_isstring(L,-1)) Bottom = StoreTempString(L, POS_STORE);

lua_getfield(L, 1, "HelpTopic"); //+3
lua_getfield(L, POS_PROPS, "HelpTopic"); //+3

if (lua_isstring(L,-1)) HelpTopic = StoreTempString(L, 4, &store);
if (lua_isstring(L,-1)) HelpTopic = StoreTempString(L, POS_STORE);

lua_getfield(L, 1, "SelectIndex"); //+3
lua_getfield(L, POS_PROPS, "SelectIndex"); //+3
if ((SelectIndex = lua_tointeger(L,-1)) > ItemsNumber)
SelectIndex = 0;

lua_getfield(L, 1, "Id"); //+4
lua_getfield(L, POS_PROPS, "Id"); //+4
if (lua_type(L,-1)==LUA_TSTRING && lua_objlen(L,-1)==sizeof(GUID))
MenuGuid = (const GUID*)lua_tostring(L, -1);

Expand All @@ -1743,15 +1750,15 @@ static int far_Menu(lua_State *L)
{
static const char key[] = "text";
lua_pushinteger(L, i+1);
lua_gettable(L, 2);
lua_gettable(L, POS_ITEMS);

if (!lua_istable(L, -1))
return luaLF_SlotError(L, i+1, "table");

//-------------------------------------------------------------------------
lua_getfield(L, -1, key);

if (lua_isstring(L,-1)) pItem->Text = StoreTempString(L, 4, &store);
if (lua_isstring(L,-1)) pItem->Text = StoreTempString(L, POS_STORE);
else if (!lua_isnil(L,-1)) return luaLF_FieldError(L, key, "string");

if (!pItem->Text)
Expand Down Expand Up @@ -1809,9 +1816,9 @@ static int far_Menu(lua_State *L)
// Break Keys
pBreakKeys = NULL;
pBreakCode = NULL;
if (lua_type(L,3) == LUA_TSTRING)
if (lua_type(L, POS_BKEYS) == LUA_TSTRING)
{
const char *q, *ptr = lua_tostring(L,3);
const char *q, *ptr = lua_tostring(L, POS_BKEYS);
lua_newtable(L);
while (*ptr)
{
Expand All @@ -1824,10 +1831,10 @@ static int far_Menu(lua_State *L)
lua_setfield(L,-2,"BreakKey");
lua_rawseti(L,-2,++NumBreakCodes);
}
lua_replace(L,3);
lua_replace(L, POS_BKEYS);
}
else
NumBreakCodes = lua_istable(L,3) ? (int)lua_objlen(L,3) : 0;
NumBreakCodes = lua_istable(L,POS_BKEYS) ? (int)lua_objlen(L,POS_BKEYS) : 0;

if (NumBreakCodes)
{
Expand All @@ -1838,7 +1845,7 @@ static int far_Menu(lua_State *L)
lua_pushstring(L, FAR_VIRTUALKEYS);
lua_rawget(L, LUA_REGISTRYINDEX);
// push breakkeys table on top
lua_pushvalue(L, 3); // vk=-2; bk=-1;
lua_pushvalue(L, POS_BKEYS); // vk=-2; bk=-1;

for(ind=0; ind < NumBreakCodes; ind++)
{
Expand Down Expand Up @@ -1925,14 +1932,14 @@ static int far_Menu(lua_State *L)
if (NumBreakCodes && (BreakCode != -1))
{
lua_pushinteger(L, BreakCode+1);
lua_gettable(L, 3);
lua_gettable(L, POS_BKEYS);
}
else if (ret == -1)
return lua_pushnil(L), 1;
else
{
lua_pushinteger(L, ret+1);
lua_gettable(L, 2);
lua_gettable(L, POS_ITEMS);
}

lua_pushinteger(L, ret+1);
Expand Down
2 changes: 1 addition & 1 deletion plugins/luamacro/luafar/version.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#include <farversion.hpp>

#define PLUGIN_BUILD 832
#define PLUGIN_BUILD 833

0 comments on commit e409d24

Please sign in to comment.