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

Check whether nvim_add_user_command exists before using it #47

Merged
merged 2 commits into from
Jan 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions lua/nvimux/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ nvimux.term = {}
nvimux.commands = setmetatable({},{

__newindex = function(tbl, key, value)
vim.api.nvim_add_user_command("Nvimux"..fns.snake_to_pascal(key),
function(opts)
value(opts.args, opts)
end, {})
if vim.api.nvim_add_user_command then
vim.api.nvim_add_user_command("Nvimux"..fns.snake_to_pascal(key),
function(opts)
value(opts.args, opts)
end, {})
else
vim.api.nvim_command('command! Nvimux'..fns.snake_to_pascal(key).." lua require'nvimux'.commands."..key.."()")
end
rawset(tbl, key, value)
end
})
Expand All @@ -34,17 +38,6 @@ local tab_cmd = function(create_window)
fns.fn_or_command(nvimux.context.new_tab)
end

-- [[ Commands
-- Commands defined in `nvimux.commands` will be automatically converted to nvim's command
nvimux.commands.horizontal_split = function() return win_cmd[[spl|wincmd j]] end
nvimux.commands.vertical_split = function() return win_cmd[[vspl|wincmd l]] end
nvimux.commands.new_tab = function() return tab_cmd[[tabe]] end
nvimux.commands.previous_tab = nvimux.go_to_last_tab
nvimux.commands.term_paste = function(reg) vim.paste(vim.fn.getreg(reg or '"', 1, true), -1) end
nvimux.commands.toggle_term = nvimux.term.toggle
nvimux.commands.term_rename = nvimux.term.rename
-- ]]

-- [[ Top-level helper functions
nvimux.set_last_tab = function(tabn)
if tabn == nil then
Expand Down Expand Up @@ -126,6 +119,17 @@ end

-- ]]

-- [[ Commands
-- Commands defined in `nvimux.commands` will be automatically converted to nvim's command
nvimux.commands.horizontal_split = function() return win_cmd[[spl|wincmd j]] end
nvimux.commands.vertical_split = function() return win_cmd[[vspl|wincmd l]] end
nvimux.commands.new_tab = function() return tab_cmd[[tabe]] end
nvimux.commands.previous_tab = nvimux.go_to_last_tab
nvimux.commands.term_paste = function(reg) vim.paste(vim.fn.getreg(reg or '"', 1, true), -1) end
nvimux.commands.toggle_term = nvimux.term.toggle
nvimux.commands.term_rename = nvimux.term.rename
-- ]]

local autocmds = {
{event = "TabLeave", target="*", cmd = [[lua require('nvimux').set_last_tab()]]},
}
Expand Down