Save implementation of other layers

pull/3/head
Iron-E 4 years ago
parent dc16475788
commit eb6f39b43a
No known key found for this signature in database
GPG Key ID: 19B71B7B7B021D22

@ -707,10 +707,10 @@ FUNCTIONS *libmodal-lua-Layer-functions*
layer:enter()
-- Start a timer for five seconds.
vim.loop.new_timer():start(5000, 0, vim.schedule_wrap(
vim.loop.new_timer():start(5000, 0,
-- Exit the layer. `gg` should return to normal.
function() layer:exit() end
))
vim.schedule_wrap(layer:exit())
)
<

@ -456,9 +456,9 @@ LAYERS *libmodal-examples-layers*
local exitFunc = libmodal.layer.exit()
-- exit the mode in five seconds
vim.loop.new_timer():start(5000, 0, vim.schedule_wrap(
function() exitFunc() end
))
vim.loop.new_timer():start(5000, 0,
vim.schedule_wrap(exitFunc)
)
<
------------------------------------------------------------------------------
PROMPTS *libmodal-examples-prompts*

@ -0,0 +1,19 @@
local libmodal = require('libmodal')
-- create a new layer.
local exitFunc = libmodal.layer.enter('FOO', {
['n'] = { -- normal mode mappings
['gg'] = { -- remap `gg`
['rhs'] = 'G', -- map it to `G`
['noremap'] = true, -- don't recursively map.
},
['G'] = { -- remap `G`
['rhs'] = 'gg', -- map it to `gg`
['noremap'] = true -- don't recursively map.
}
}
})
vim.loop.new_timer():start(5000, 0, vim.schedule_wrap(
function() exitFunc(); print('EXITED.') end
))

@ -16,6 +16,7 @@ local Layer = {['TYPE'] = 'libmodal-layer'}
local _BUFFER_CURRENT = 0
local _RESTORED = nil
local _ERR_NO_MAP = 'E5555: API call: E31: No such mapping'
local function convertKeymap(keymapEntry)
local lhs = keymapEntry.lhs
@ -76,7 +77,6 @@ function _metaLayer:enter()
end
end
print(vim.inspect(priorKeymap))
self._priorKeymap = priorKeymap
end
@ -139,7 +139,6 @@ function _metaLayer:map(mode, lhs, rhs, options)
self._keymap[mode][lhs] = vim.tbl_extend('force',
options, {['rhs'] = rhs}
)
print(vim.inspect(self._priorKeymap))
end
----------------------------------------------
@ -155,8 +154,6 @@ function _metaLayer:_unmapFromBuffer(mode, lhs)
local priorKeymap = self._priorKeymap
local priorMapping = self._priorKeymap[mode][lhs]
print('unmapping ' .. mode .. ':' .. lhs)
if not priorKeymap then error(
"You can't undo a map from a buffer without activating the layer first."
) end
@ -168,14 +165,13 @@ function _metaLayer:_unmapFromBuffer(mode, lhs)
-- set the prior mapping as restored.
priorKeymap[mode][lhs] = _RESTORED
print('reverted mapping')
else
-- just delete the buffer mapping.
local noErrors, err = pcall(api.nvim_buf_del_keymap, _BUFFER_CURRENT, mode, lhs)
if not noErrors then print(err) end
print('deleted mapping')
if not noErrors and err ~= _ERR_NO_MAP then
print(err)
end
end
end

@ -14,6 +14,40 @@ local ModeLayer = {['TYPE'] = 'libmodal-mode-layer'}
local _metaModeLayer = require('libmodal/src/classes').new(ModeLayer.TYPE)
function _metaModeLayer:map(keys, mapping)
local priorInstruction = self._priorInstruction
local layerInstruction = self._instruction
if priorInstruction then
local modeInstruction = self._mode._instruction
-- only save the value from `mode` when adding a new mapping.
if not layerInstruction[keys] then
priorInstruction[keys] = modeInstruction:parseGet(keys)
end
-- map the keys to mode.
modeInstruction:parsePut(keys, mapping)
end
-- add the keys to the instruction.
layerInstruction[keys] = mapping
end
function _metaModeLayer:unmap(keys)
local priorInstruction = self._priorInstruction or {}
local layerInstruction = self._instruction
if priorInstruction[keys] then
self._mode._instruction:parsePut(keys, priorInstruction[keys])
priorInstruction[keys] = nil
end
-- remove `keys` from the instruction.
layerInstruction[keys] = nil
end
-------------------------------
--[[ SUMMARY:
* Enter the `ModeLayer`, replacing any conflicting mappings.

Loading…
Cancel
Save