"local" copy of luakeys
kalekje opened this issue · comments
This might be more of a general Lua question. I have a few LaTeX packages that use luakeys, and modify the opts table to suit the need. What I would like to do with my packages is create a copy of luakeys for use within the package only. Is this possible? I think a deep-copy is needed, but am wondering otherwise. I think this would be beneficial, although memory inefficient, it would allow users to have their own luakeys interface without fear of users messing up their settings.
I might be overthinking this: would a better solution be to define a set of opts for local usage of luakeys?
Here is an example.
package.path = package.path .. ";C:\\Users\\me\\AppData\\Local\\Programs\\MiKTeX\\tex\\luatex\\luakeys\\/?.lua"
mymodule = {}
mymodule.luakeys = require'luakeys'
-- my module modifies luakeys
function toggle_bool(t)
for k, v in pairs(t) do
if (type(k) == 'string') and k:find( '!') then
t[k:sub(2)] = not v
t[k] = nil
end
end
end
mymodule.luakeys.opts.hooks.result=toggle_bool
t = mymodule.luakeys.parse('bold, !color')
for k, v in pairs(t) do
print(k,v)
end
-- I want the 'pure' version of luakeys to be unchanged
luakeys = require'luakeys'
t = luakeys.parse('bold, !color')
for k, v in pairs(t) do
print(k,v)
end
-- but luakeys points to the same object as mymodule.luakeys
From the Lua manual https://www.lua.org/pil/8.1.html:
The other main job of require is to avoid loading the same file twice. For that purpose, it keeps a table with the names of all loaded files. If a required file is already in the table, require simply returns.
https://stackoverflow.com/a/2812556
mymodule.luakeys = require'luakeys'
package.loaded.luakeys = nil
I added a new function to address this issue:
local my_luakeys = require('luakeys').get_new_instance()
local result = my_luakeys.parse('key=value')
local l1 = require('luakeys') -- table: 0x564ea6ca4160
local l2 = require('luakeys') -- table: 0x564ea6ca4160
local l3 = require('luakeys').get_new_instance() -- table: 0x563574d51470
local l4 = require('luakeys').get_new_instance() -- table: 0x563574d86ac0