Skip to content

Commit

Permalink
Merge pull request #47 from jdelkins/commands
Browse files Browse the repository at this point in the history
Check whether nvim_add_user_command exists before using it
  • Loading branch information
hkupty authored Jan 20, 2022
2 parents 1e0a519 + 959964f commit 5ec3f23
Showing 1 changed file with 19 additions and 15 deletions.
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 @@ -134,6 +127,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

0 comments on commit 5ec3f23

Please sign in to comment.