diff options
| author | Robert Günzler <r@gnzler.io> | 2022-08-17 15:27:37 +0200 |
|---|---|---|
| committer | Robert Günzler <r@gnzler.io> | 2022-08-17 15:27:37 +0200 |
| commit | 4727b38cfeb30ac72635a9854a26ef13bc63592b (patch) | |
| tree | 8b731456a38c99206f7a7ed167db6dd9913c157c | |
| parent | 5feb09d4f6e9c4d2b8a6f2b8ac29911850824046 (diff) | |
vim: move custom exrc loading into module
| -rw-r--r-- | .vim/init.lua | 18 | ||||
| -rw-r--r-- | .vim/lua/exrc.lua | 49 |
2 files changed, 50 insertions, 17 deletions
diff --git a/.vim/init.lua b/.vim/init.lua index 5c77827..97cfc13 100644 --- a/.vim/init.lua +++ b/.vim/init.lua @@ -296,23 +296,7 @@ vim.api.nvim_create_user_command('Find', function(o) misc.find_files(table.conca _G.statusline_enable_notifysend = false require('statusline') - -local load_local_rc = function() - local root = vim.fn.getcwd() - - if root and vim.loop.fs_stat(root .. '/.nvimrc') then - vim.cmd('luafile ' .. root .. '/.nvimrc') - vim.notify('loaded local rc') - end - - -- try harder... - -- root = require('lspconfig.util').root_pattern('.nvimrc')(vim.fn.getcwd()) - -- if root and vim.loop.fs_stat(root .. '/.nvimrc') then - -- vim.cmd('luafile ' .. root .. '/.nvimrc') - -- vim.notify('loaded local rc') - -- end -end -load_local_rc() +require('exrc').setup {} -- TODO: remove eventually vim.filetype.add { diff --git a/.vim/lua/exrc.lua b/.vim/lua/exrc.lua new file mode 100644 index 0000000..6b7742b --- /dev/null +++ b/.vim/lua/exrc.lua @@ -0,0 +1,49 @@ +local root_pattern = require('lspconfig.util').root_pattern + +local M = { + config = { + filenames = {'.nvimrc'} + } +} + +M.setup = function (opt) + vim.validate { + filenames = { opt.filenames, 'table', true }, + } + + M.config = vim.tbl_extend('force', M.config, opt) + + local group = vim.api.nvim_create_augroup('ExrcLoad', { clear = true }) + vim.api.nvim_create_autocmd('VimEnter', { + callback = M.load, + group = group, + }) +end + +local load = function (root, filename) + local fpath = root .. '/' .. filename + if root and vim.loop.fs_stat(fpath) then + vim.cmd('luafile ' .. fpath) + vim.notify('[exrc] loaded') + else + error(string.format('[exrc] %s not found', fpath)) + end +end + +M.load = function () + local root = vim.fn.getcwd() + for _, fname in ipairs(M.config.filenames) do + local ok = false + ok, _ = pcall(load, root, fname) + if ok then return true end + + -- try harder by searching up the dir tree + root = root_pattern(fname)(root) + ok, _ = pcall(load, root, fname) + if ok then return true end + end + + return false +end + +return M |