Solve expression manually

This commit is contained in:
hrsh7th
2022-01-15 22:48:24 +09:00
parent 2ed0cdab36
commit ec972710d3

View File

@@ -186,6 +186,8 @@ keymap.feed_map = function(map)
rhs = keymap.t(map.rhs) rhs = keymap.t(map.rhs)
end end
rhs = keymap.expression(rhs)
if map.noremap then if map.noremap then
vim.api.nvim_feedkeys(rhs, 'itn', true) vim.api.nvim_feedkeys(rhs, 'itn', true)
else else
@@ -225,4 +227,27 @@ misc.set(_G, { 'cmp', 'utils', 'keymap', 'set_map' }, function(id)
return keymap.set_map.callbacks[id]() or '' return keymap.set_map.callbacks[id]() or ''
end) end)
---Resolve expression.
---@param expr string
---@return string
keymap.expression = function(expr)
local spans = {}
local f = 0
for i = 1, #expr do
local c = string.sub(expr, i, i)
if f == 0 and c == keymap.t('<C-r>') then
f = i
end
if f ~= 0 and c == keymap.t('<CR>') then
table.insert(spans, { s = f, e = i })
f = 0
end
end
for i = #spans, 1, -1 do
local s, e = spans[i].s, spans[i].e
expr = string.sub(expr, 1, s - 1) .. keymap.expression(vim.api.nvim_eval(string.sub(expr, s + 2, e - 1))) .. string.sub(expr, e + 1)
end
return expr
end
return keymap return keymap