function 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 error if 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 yet local 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) return end 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) end end -- 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 end end 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, })