Skip to content

Commit

Permalink
LuaFAR: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
shmuz committed Nov 29, 2024
1 parent 3319b16 commit 1ee581d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 31 deletions.
11 changes: 6 additions & 5 deletions luafar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ add_custom_command(
DEPENDS ${SDK}/farplug-wide.h ${SDK}/farcolor.h ${SDK}/farkeys.h ${MAKEFLAGS} ${FARSRC}/DlgGuid.hpp
)

if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Android")
set(MYTIMER_C src/mytimer.c)
endif()

set(SOURCES
${CMAKE_CURRENT_BINARY_DIR}/farflags.c
src/bit64.c
Expand All @@ -27,7 +23,6 @@ set(SOURCES
src/luamacro.c
src/lusercontrol.c
src/lutf8lib.c
${MYTIMER_C}
src/service.c
src/slnunico.c
src/sysutils.c
Expand All @@ -44,6 +39,12 @@ set(SOURCES
src/LPeg/lpvm.c
)

if (NOT (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD" OR
${CMAKE_SYSTEM_NAME} MATCHES "DragonFly" OR
${CMAKE_SYSTEM_NAME} MATCHES "Android"))
set(SOURCES ${SOURCES} src/mytimer.c)
endif()

add_library (${PROJECT_NAME} SHARED ${SOURCES})
set_property(TARGET ${PROJECT_NAME} PROPERTY C_STANDARD 11)
target_include_directories(${PROJECT_NAME} PRIVATE ${LIBLUA_INCLUDE_DIRS})
Expand Down
18 changes: 9 additions & 9 deletions luafar/lua_share/far2/test/macrotest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ local function assert_nil(v) assert(v==nil) return v; end
local function assert_false(v) assert(v==false) return v; end
local function assert_true(v) assert(v==true) return v; end

local function assert_range(val, low, high)
if low then assert(val >= low) end
if high then assert(val <= high) end
return val
local function assert_range(v, low, high)
if low then assert(v >= low) end
if high then assert(v <= high) end
return v
end

local function assert_numint(v)
Expand All @@ -52,7 +52,7 @@ local function pack (...)
return { n=select("#",...), ... }
end

local TmpFileName = far.InMyTemp("tmp.tmp")
local TmpFileName = far.InMyTemp("macrotest.tmp")

local function WriteTmpFile(...)
local fp = assert(io.open(TmpFileName,"w"))
Expand Down Expand Up @@ -111,9 +111,9 @@ end

local function test_mf_akey()
assert_eq(akey, mf.akey)
local k0,k1 = akey(0),akey(1)
assert(k0==far.NameToKey(MacroKey1) and k1==MacroKey1 or
k0==far.NameToKey(MacroKey2) and k1==MacroKey2)
local key,name = akey(0),akey(1)
assert(key==far.NameToKey(MacroKey1) and name==MacroKey1 or
key==far.NameToKey(MacroKey2) and name==MacroKey2)
-- (the 2nd parameter is tested in function test_mf_eval).
end

Expand Down Expand Up @@ -413,7 +413,7 @@ local function test_mf_key()
assert_eq (mf.key("foobar"), "")
end

-- Separate tests for mf.float and mf.string are locale-dependant, thus they are tested together.
-- Separate tests for mf.float and mf.string are locale-dependent, thus they are tested together.
local function test_mf_float_and_string()
local t = { 0, -0, 2.56e1, -5.37, -2.2e100, 2.2e-100 }
for _,num in ipairs(t) do
Expand Down
6 changes: 3 additions & 3 deletions luafar/src/makeflags.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ typedef struct {


local file_bottom = [[
void add_flags (lua_State *L)
void push_far_flags (lua_State *L)
{
int i;
int nelem = sizeof(flags) / sizeof(flags[0]);
for (i=0; i<nelem; ++i) {
lua_createtable(L, 0, nelem);
for (int i=0; i<nelem; ++i) {
lua_pushnumber(L, flags[i].val);
lua_setfield(L, -2, flags[i].key);
}
Expand Down
4 changes: 0 additions & 4 deletions luafar/src/mytimer.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#if !defined(__FreeBSD__) && !defined(__DragonFly__)

//initial source: https://qnaplus.com/implement-periodic-timer-linux/

#include <stdint.h>
Expand Down Expand Up @@ -394,5 +392,3 @@ int luaopen_timer(lua_State *L)
lua_pushnil(L);
return 1;
}

#endif // #if !defined(__FreeBSD__) && !defined(__DragonFly__)
18 changes: 8 additions & 10 deletions luafar/src/service.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "util.h"
#include "service.h"

extern void add_flags (lua_State *L); // from generated file farflags.c
extern void push_far_flags (lua_State *L); // from generated file farflags.c

extern int luaopen_far_host(lua_State *L);
extern int luaopen_regex (lua_State*);
Expand Down Expand Up @@ -6244,8 +6244,7 @@ static int luaopen_far (lua_State *L)
lua_setfield(L, LUA_REGISTRYINDEX, FAR_VIRTUALKEYS);

luaL_register(L, "far", far_funcs);
lua_createtable(L, 0, 1600);
add_flags(L);
push_far_flags(L);
lua_pushvalue(L, -1);
lua_setfield(L, -3, "Flags");
lua_pushvalue(L, -1); // for compatibility with Far3 scripts
Expand All @@ -6255,6 +6254,12 @@ static int luaopen_far (lua_State *L)
luaopen_far_host(L);
lua_setfield(L, -2, "Host");

#if !defined(__FreeBSD__) && !defined(__DragonFly__) && !defined(__ANDROID__)
lua_pushcfunction(L, luaopen_timer);
lua_call(L, 0, 1);
lua_setfield(L, -2, "Timer");
#endif

if (pd->Private && pd->PluginId == LuamacroId)
{
lua_pushcfunction(L, far_MacroCallFar);
Expand All @@ -6277,13 +6282,6 @@ static int luaopen_far (lua_State *L)
lua_setfield(L, -2, "__index");
luaL_register(L, NULL, filefilter_methods);

#if !defined(__FreeBSD__) && !defined(__DragonFly__) && !defined(__ANDROID__)
lua_getglobal(L, "far");
lua_pushcfunction(L, luaopen_timer);
lua_call(L, 0, 1);
lua_setfield(L, -2, "Timer");
#endif

lua_pushcfunction(L, luaopen_usercontrol);
lua_call(L, 0, 0);

Expand Down

0 comments on commit 1ee581d

Please sign in to comment.