Solve expression manually

This commit is contained in:
hrsh7th 2022-01-15 22:47:39 +09:00
parent 2ed0cdab36
commit 825a45f6f5

View file

@ -186,6 +186,8 @@ keymap.feed_map = function(map)
rhs = keymap.t(map.rhs)
end
rhs = keymap.expression(rhs)
if map.noremap then
vim.api.nvim_feedkeys(rhs, 'itn', true)
else
@ -225,4 +227,26 @@ misc.set(_G, { 'cmp', 'utils', 'keymap', 'set_map' }, function(id)
return keymap.set_map.callbacks[id]() or ''
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 })
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