Skip to content

Commit

Permalink
Merge pull request #46 from hkupty/drop-deprecated
Browse files Browse the repository at this point in the history
Remove deprecated functions
  • Loading branch information
hkupty committed Jan 19, 2022
2 parents 029e107 + 8db1471 commit 875ee0d
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 344 deletions.
74 changes: 16 additions & 58 deletions lua/nvimux/bindings.lua
Expand Up @@ -4,53 +4,8 @@ local fns = {}

local consts = {
terminal_quit = '<C-\\><C-n>',
esc = '<ESC>',
}

fns.nvim_do_bind = function(options)
local escape_prefix = options.escape_prefix or ''
local mode = options.mode
return function(cfg)
local suffix = cfg.suffix
local prefix = vars.local_prefix[mode] or vars.prefix
if suffix == nil then
suffix = string.sub(cfg.mapping, 1, 1) == ':' and '<CR>' or ''
end
vim.cmd(mode .. 'noremap <silent> ' .. prefix .. cfg.key .. ' ' .. escape_prefix .. cfg.mapping .. suffix)
end
end

fns.bind = {
t = fns.nvim_do_bind{mode = 't', escape_prefix = consts.terminal_quit},
i = fns.nvim_do_bind{mode = 'i', escape_prefix = consts.esc},
n = fns.nvim_do_bind{mode = 'n'},
v = fns.nvim_do_bind{mode = 'v'}
}

fns.bind_all_modes = function(options)
for _, mode in ipairs(options.modes) do
fns.bind[mode](options)
end
end

bindings.bind = function(options)
fns.bind_all_modes(options)
end

bindings.create_binding = function(modes, command)
local tbl = {}
tbl[table.concat(modes, "")] = { command }
return tbl

end

bindings.bind_all = function(options)
for _, bind in ipairs(options) do
local key, cmd, modes = unpack(bind)
bindings.mappings[key] = bindings.create_binding(modes, cmd)
end
end

if vim.keymap ~= nil then
bindings.set_keymap = vim.keymap.set
else
Expand Down Expand Up @@ -82,26 +37,29 @@ bindings.keymap = function(binding, context)
bindings.set_keymap(binding[1], context.prefix .. binding[2], binding[3], options)
elseif (type(binding[3]) == "string") then
local suffix = ''
local starts_with_colon = string.sub(binding[3], 1, 1) == ':'

if binding.suffix == nil then
-- TODO revisit
suffix = string.sub(binding[3], 1, 1) == ':' and '<CR>' or ''
suffix = starts_with_colon and '<CR>' or ''
else
suffix = binding.suffix
end

if vim.tbl_contains(binding[1], 't') then
binding[1] = vim.tbl_filter(function(mode) return mode ~= 't' end, binding[1])
bindings.set_keymap('t',
context.prefix .. binding[2],
consts.terminal_quit .. binding[3] .. suffix,
options)
elseif vim.tbl_contains(binding[1], 'i') then
binding[1] = vim.tbl_filter(function(mode) return mode ~= 'i' end, binding[1])
bindings.set_keymap('i',
context.prefix .. binding[2],
consts.esc .. binding[3] .. suffix,
options)
if starts_with_colon then
if vim.tbl_contains(binding[1], 't') then
binding[1] = vim.tbl_filter(function(mode) return mode ~= 't' end, binding[1])
bindings.set_keymap('t',
context.prefix .. binding[2],
consts.terminal_quit .. binding[3] .. suffix,
options)
elseif vim.tbl_contains(binding[1], 'i') then
binding[1] = vim.tbl_filter(function(mode) return mode ~= 'i' end, binding[1])
bindings.set_keymap('i',
context.prefix .. binding[2],
consts.esc .. binding[3] .. suffix,
options)
end
end

bindings.set_keymap(binding[1], context.prefix .. binding[2], binding[3] .. suffix, options)
Expand Down
30 changes: 14 additions & 16 deletions lua/nvimux/fns.lua
@@ -1,25 +1,23 @@
local fns = {}

fns.prompt = function(message)
vim.fn.inputsave()
local ret = vim.fn.input(message)
vim.fn.inputrestore()
return ret
local function capitalize_first(s)
return string.upper(string.sub(s,1,1))..string.sub(s,2)
end

fns.split = function(str)
local p = {}
for i=1, #str do
table.insert(p, str:sub(i, i))
end
return p
fns.snake_to_pascal = function(s)
return (string.gsub(capitalize_first(s),"_(%w+)",capitalize_first))
end

fns.build_cmd = function(options)
local nargs = options.nargs or 0
local cmd = options.cmd or options.lazy_cmd()

vim.cmd('command! -nargs=' .. nargs .. ' ' .. options.name .. ' ' .. cmd)
fns.fn_or_command = function(cmd)
local tp = type(cmd)
if tp == "function" then
cmd()
elseif tp == "string" then
vim.cmd(cmd)
else
print("nvimux: Cannot run command of type " .. tp)
end
end


return fns

0 comments on commit 875ee0d

Please sign in to comment.