-
Notifications
You must be signed in to change notification settings - Fork 594
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
Access from different Lua process / Fennel REPL #2377
Comments
I just discovered End goal here is having a Fennel REPL into the Hammerspoon environment. |
Yes, you could trigger Hammerspoon code via However, it could be an interesting experiment to try and embed Fennel into Hammerspoon. I feel like this is something @asmagill might have an opinion on? |
Just to clarify, this is less about writing configuration files in Fennel and more about being able to directly interact with Hammerspoon via an interactive REPL session (potentially running in your editor, see conjure). To use Fennel files to configure Hammerspoon you can already use the following: -- cat init.lua
local fennel = require "fennel"
-- allow requiring of fennel modules
table.insert(package.loaders or package.searchers, fennel.searcher)
fennel.dofile("init.fnl", { allowedGlobals = false }) ;; cat init.fnl
(global logger (hs.logger.new "fennel" "debug"))
(logger.i "fennel loaded") |
The Hammerspoon modules themselves can only be loaded from within Hammerspoon because they rely on the common macOS Frameworks (Foundation, CoreGraphics, etc.) being linked against the application, as well as the Hammerspoon specific framework, LuaSkin. The only lua process which can successfully meet those requirements is the lua process embedded within Hammerspoon itself. As you identified above, it looks like you should be able to evaluate/execute fennel files exactly as you describe above (I haven't tried, just read through some of the files on the fennel repo, but sounds like you have). The And for piping in code from the outside, the only real mechanism to do so is ( There is an undocumented variable Sorry I don't have any better ideas at the moment... this looks interesting, and I kind of hope I get a chance to play with it myself at some point, but I won't have any time to mess with it myself or see if we can add an easier way to interface with fennel for the next week or so. |
It seems like one way to do this would be starting a networked REPL inside local jeejah = require("jeejah")
local coro = jeejah.start(12345, {debug=true, sandbox={x=12}})
EDIT resolved, see #2377 (comment). |
Try installing LuaSocket via luarocks: luarocks install --local luasocket |
@latenitefilms thanks for the swift response, I just tried that but it still can't find EDIT 2.20pm It looks like I was missing
require("luarocks.loader")
http = require("socket.http")
print(http.request("http://www.cs.princeton.edu/~diego/professional/luasocket")) Maybe it's useful to edit #363 (comment) to include a note on requiring EDIT 2.00pm This seemed promising but didn't end up working (same error): #363 (comment)
|
Also found this issue where some folks try to set up a Fennel REPL into Hammerspoon agzam/spacehammer#33 |
Using luarocks with --local installs into package.path = package.path .. ";" .. os.getenv("HOME") .. "/.luarocks/share/lua/5.3/?.lua;" .. os.getenv("HOME") .. "/.luarocks/share/lua/5.3/?/init.lua"
package.cpath = package.cpath .. ";" .. os.getenv("HOME") .. "/.luarocks/lib/lua/5.3/?.so" After doing this, I was able to do edit - forgot to prepend existing path the first time! |
Ah, I see, it's part of the luarocks installation itself... well, never needed it before, but I guess good to know it exists if I do start using rocks for much and have versioning issues to worry about. |
oops... should be: package.path = package.path .. ";" .. os.getenv("HOME") .. "/.luarocks/share/lua/5.3/?.lua;" .. os.getenv("HOME") .. "/.luarocks/share/lua/5.3/?/init.lua"
package.cpath = package.cpath .. ";" .. os.getenv("HOME") .. "/.luarocks/lib/lua/5.3/?.so" |
So I made a bit more progress here. I cloned the require("luarocks.loader")
local search_parent = string.format("%s?.lua;%s", "/Users/martinklepsch/etc/hammerspoon/jeejah/jeejah/", package.path)
local jeejah = dofile(package.searchpath("jeejah", search_parent)) The problem is that I'm still getting an error:
The I'm pretty unfamiliar with Lua's package loading but it seems to be that usually files loaded via Next I'll try inlining some of this code (i.e. cheat around actually loading it from separate files). |
I have successfully connected to Hammerspoon via Jeejah, with @technomancy's help, though there is a caveat at the end. I haven't tried bootstrapping this from the ground up, but I think the following are the essential steps. First, you need luasocket installed. I did this with the help of MacPorts and LuaRocks:
The rewriting of Once that's all done, you should be able to Next I cloned Jeejah into package.path = package.path .. ';jeejah/?.lua'
-- Fennel expects the "arg" table to exist, which (I think) is where
-- argv is stored in Lua.
arg = {}
fennel = require 'fennel'
table.insert(package.loaders or package.searchers, fennel.searcher)
require 'init_fennel' I will note that I believe technomancy recently fixed a bug or two in Jeejah that was causing it to break, so if you have a local copy or a clone or something, update to the latest from the Git repo. Next I put the following into (local jeejah (require "jeejah"))
(global jeejah-coro (jeejah.start))
(global jeejah-coro-freq 0.01)
(fn jeejah-spin []
(coroutine.resume jeejah-coro)
(when (not= (coroutine.status jeejah-coro)
"dead")
(hs.timer.doAfter jeejah-coro-freq jeejah-spin)))
(global jeejah-timer (hs.timer.doAfter jeejah-coro-freq jeejah-spin)) Once all that is done, reload your Hammerspoon config and you should see something like this in the Hammerspoon console:
🎉 I was able to successfully connect from Emacs with Monroe on port 7888. Here's the Caveat: with the current release of Hammerspoon, I'm pretty sure this is still broken because coroutines are broken. If I do And if I do I am hoping that #2306, which was closed this past April, means this will all be working in the next release of Hammerspoon. Building from source didn't look particularly easy this evening, so I'll probably try and retest after the next release. |
FYI: You can also use the latest CommandPost beta, which has the coroutine support merged in: |
Just a heads up -- the next major release of Hamemrspoon will be using Lua 5.4 internally... not sure if that will be an issue or not. If the |
Thanks, this was important. MacPorts has an open PR to upgrade to Lua 5.4, so in the interim I compiled my own. I now have a Fennel REPL working under the latest (just released) Hammerspoon. New instructions are probably something like:
After that, reload your Hammerspoon config, and you should see the "Server started on port 7888..." message. Now you can connect with e.g. Monroe and indeed I can now do things like This is really great, big thank you to all the people who volunteer your time to make Hammerspoon work! EDIT: Sorry, left out some bits in the original comment, now hopefully correct. |
Drafted a feature for consideration in spacehammer agzam/spacehammer#108 (comment) but realized it could be combined with other tools to work like a repl system The good news is that it's editor agnostic in this setup. I have hs config that's reading selected text, and sending it to a tmux session via task commands. The bad news is it depends on tmux, nodemon, and shevek. |
I'd like to access Hammerspoon from a separate Lua process. I'm thinking that maybe configuring
package.path
similar to how it is configured for Hammerspoon itself but couldn't figure out how to do that.Not sure if this is possible at all but I figured I might ask :)
Really having fun with Hammerspoon! 🙌
The text was updated successfully, but these errors were encountered: