Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to set global varable in Hammerspoon Lua #3601

Open
Mikeno-Philippe opened this issue Feb 10, 2024 · 13 comments
Open

Unable to set global varable in Hammerspoon Lua #3601

Mikeno-Philippe opened this issue Feb 10, 2024 · 13 comments

Comments

@Mikeno-Philippe
Copy link

The following code runs well under Lua:

counter, offset = 0, 1

function reset()
	counter = 0
	print(counter)
end

function inc()
	counter = counter + offset
	print(counter)
end

reset() --> 0
inc()    --> 1
inc()    --> 2
reset() --> 0

... but running the same code under Hammerspoon Lua doesn't work as expected, the last print statement gives 2, meaning that the global variable counter has not been reseted.
Thx for your help

@Mikeno-Philippe
Copy link
Author

I found how to solve this issue using rawset

function declare (name, initval)
	rawset(_G, name, initval or false)
end

counter, offset = 0, 1
declare("counter", 0)
declare("offset", 1)

@Rhys-T
Copy link

Rhys-T commented Feb 12, 2024

It works fine on my Hammerspoon. Do you have any code in your config (or loaded Spoons) that would be setting a metatable on the global environment?

What does this code print for you when pasted into the console?

local mt = getmetatable(_G)
if mt then
	print(hs.inspect(mt))
	for k,v in pairs(mt) do
		if type(v) == 'function' then
			local info = debug.getinfo(v)
			if what ~= 'C' then
				print(hs.inspect(info))
			end
		end
	end
else
	print('no metatable on _G')
end

@Mikeno-Philippe
Copy link
Author

Mikeno-Philippe commented Feb 12, 2024 via email

@Rhys-T
Copy link

Rhys-T commented Feb 12, 2024

Weird… When you ran into the issue, was it in the console or the config file? If it was the config, try putting the code I sent there instead of in the console and see if that prints anything different.

Actually, just in case _G isn't what's getting used for some reason, can you try using this version instead?

local function checkMT(name, tbl)
	local mt = getmetatable(tbl)
	if mt then
		print(hs.inspect(mt))
		for k,v in pairs(mt) do
			if type(v) == 'function' then
				local info = debug.getinfo(v)
				if what ~= 'C' then
					print(hs.inspect(info))
				end
			end
		end
	else
		print('no metatable on '..name)
	end
end
checkMT('_G', _G)
if rawequal(_ENV, _G) then
	print '_ENV is _G'
else
	print '_ENV is not _G'
	checkMT('_ENV', _ENV)
end

@Mikeno-Philippe
Copy link
Author

Mikeno-Philippe commented Feb 12, 2024 via email

@Rhys-T
Copy link

Rhys-T commented Feb 12, 2024

What about when you run it from within your reset and/or inc functions? Also, is there anywhere you're declaring counter and/or offset as locals (or function argument names)?

@Mikeno-Philippe
Copy link
Author

Mikeno-Philippe commented Feb 12, 2024 via email

@Rhys-T
Copy link

Rhys-T commented Feb 12, 2024

I don't know of anything else that could interfere with setting global variables. As far as I know, Hammerspoon should be using an unmodified Lua interpreter.

The only other thing I can think of to try right now is moving your entire init.lua file aside and making a new one with just the code from your initial post. If that works correctly, we can try to narrow down exactly where it starts breaking. If it still fails, I'm not sure what to do.

@Mikeno-Philippe
Copy link
Author

Mikeno-Philippe commented Feb 12, 2024 via email

@Mikeno-Philippe
Copy link
Author

Mikeno-Philippe commented Feb 12, 2024 via email

@Rhys-T
Copy link

Rhys-T commented Feb 12, 2024

I see a couple of issues in that code that might explain why the counter isn't updating properly:

  • I don't see a definition for that setOffset function anywhere, and even if it exists, any change it makes to offset will get overwritten by the offset = part of the menu item's fn - changing it to whatever setOffset's return value was.
  • This version of reset doesn't seem to actually set counter at all - it just updates the menu's title and shows the alert. Also, it's setting offset to reset's return value, which is nil - meaning that any Increase/Decrease commands after that point will throw an attempt to perform arithmetic on a nil value error and not actually change the counter.

These next two aren't directly related to the counter problem, but:

  • $ and ^ aren't their own keys, at least on most keyboard layouts that I know of, so they don't have keycodes defined in hs.keycodes.map and can't be passed directly to hs.hotkey.bind like that. Assuming that they're Shift-4 and Shift-6 like on my keyboard, you'll need to do something like:
hyper = {"cmd","alt","ctrl"}
hyperShift = {"cmd","alt","ctrl","shift"}

hs.hotkey.bind(hyperShift, "4", function()
	updateCounter(offset)
end)

hs.hotkey.bind(hyperShift, "6", function()
	updateCounter(-offset)
end)
  • "Set offset (" .. offset .. ")": This title will get evaluated once when your config first runs, and won't update when the offset is changed. You can either include something like:
	menuMenu[4].title = "Set offset (" .. offset .. ")"
	menu:setMenu(menuMenu)

in your setOffset function, or change menuMenu to a function so it gets re-evaluated each time the menu opens:

function menuMenu()
	return {
		{ title = "Increase", fn = function() updateCounter(offset) end },
		{ title = "Decrease", fn = function() updateCounter(-offset) end },
		{ title = "-" },
		{ title = "Set offset (" .. offset .. ")" , fn = function() offset = setOffset(offset) end },
		{ title = "Reset counter", fn = function() offset = reset() end }
	}
end

@Mikeno-Philippe
Copy link
Author

Mikeno-Philippe commented Feb 13, 2024 via email

@Rhys-T
Copy link

Rhys-T commented Feb 13, 2024

Glad to hear it's working for you.

Concerning both keys (« $ » and « ^ »), they are direct own keys on a French keyboard!

Gotcha. For some reason I wasn't sure if Hammerspoon knew that, though, but I see now that it does properly update hs.keycodes.map to include $ and ^ when you're on that layout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants