Something went wrong. Try again.
A tiny plugin for writing configs in fennel
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192function compile_fennel_file(fnl_file, lua_file) -- make folder if it doesn't exist local lua_dir = vim.fn.fnamemodify(lua_file, ":h") if vim.fn.isdirectory(lua_dir) == 0 then vim.fn.mkdir(lua_dir, "p") end vim.system({'fennel', '--add-macro-path', vim.fn.stdpath("config") .. "/macros/?.fnlm", '--compile', fnl_file}, {text = true, timeout=3000}, function(obj) if obj.code == 0 then local file = io.open(lua_file, "w") if file then file:write(obj.stdout) file:close() else vim.schedule(function() vim.notify("herbivore: could not open file " .. lua_file .. " for writing", vim.log.levels.ERROR) end) end else vim.schedule(function() vim.notify("herbivore: could not compile " .. fnl_file .. ": " .. obj.stderr, vim.log.levels.ERROR) end) end end)end
-- if fennel isn't installed, throw an errorif not vim.fn.executable('fennel') then error("fennel not found in path, are you sure it's installed?")end-- On startup we want to check if any fnl files haven't been converted yetlocal fnl_files = vim.fn.glob(vim.fn.stdpath("config") .. "/fnl/**/*.fnl", false, true)if not fnl_files then vim.notify("herbivore: failed to find fnl files", vim.log.levels.WARN) returnend
for _, fnl_file in ipairs(fnl_files) do local fnl_time = vim.fn.getftime(fnl_file) local lua_file = fnl_file:gsub("%.config/nvim/fnl", ".config/nvim/lua"):gsub("%.fnl$", ".lua") local lua_time = vim.fn.getftime(lua_file)
-- if lua file doesn't exist, lua_time'll be -1 -- which is still is caught here, either way -- we want to compile that file if fnl_time > lua_time then compile_fennel_file(fnl_file, lua_file) endend
-- Next we check the other file folders local fnl_files = vim.fn.glob(vim.fn.stdpath("config") .. "/fnl-*/**/*.fnl", false, true)if fnl_files then for _, fnl_file in ipairs(fnl_files) do local fnl_time = vim.fn.getftime(fnl_file) local lua_file = fnl_file:gsub("%.config/nvim/fnl%-", ".config/nvim/"):gsub("%.fnl$", ".lua") local lua_time = vim.fn.getftime(lua_file) if fnl_time > lua_time then compile_fennel_file(fnl_file, lua_file) end endend
local grp = vim.api.nvim_create_augroup("Herbivore", {})
vim.api.nvim_create_autocmd("BufWritePost", { pattern = {vim.fn.resolve(vim.fn.stdpath("config") .. "/fnl/**/*.fnl")}, group = grp, callback = function(ev) compile_fennel_file(ev.match, ev.match:gsub("config/nvim/fnl", "config/nvim/lua"):gsub("%.fnl$", ".lua")) end,})
vim.api.nvim_create_autocmd("BufWritePost", { pattern = {vim.fn.resolve(vim.fn.stdpath("config") .. "/fnl-*/**/*.fnl")}, group = grp, callback = function(ev) compile_fennel_file(ev.match, ev.match:gsub("config/nvim/fnl%-", "config/nvim/"):gsub("%.fnl$", ".lua")) end,})vim.api.nvim_create_autocmd("BufWritePost", { pattern = {vim.fn.resolve(vim.fn.stdpath("config") .. "/fnl/*.fnl")}, group = grp, callback = function(ev) compile_fennel_file(ev.match, ev.match:gsub("config/nvim/fnl", "config/nvim/lua"):gsub("%.fnl$", ".lua")) end,})
vim.api.nvim_create_autocmd("BufWritePost", { pattern = {vim.fn.resolve(vim.fn.stdpath("config") .. "/fnl-*/*.fnl")}, group = grp, callback = function(ev) compile_fennel_file(ev.match, ev.match:gsub("config/nvim/fnl%-", "config/nvim/"):gsub("%.fnl$", ".lua")) end,})