Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: rewrite to use mappings (for most modes) #59

Merged
merged 11 commits into from
Jul 4, 2024
Prev Previous commit
Next Next commit
feat: allow disabling mappings and update readme
  • Loading branch information
max397574 committed Jun 9, 2024
commit b62634710d0fef9fb10d58d7949956b89c7a0153
57 changes: 31 additions & 26 deletions lua/better_escape.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,35 +95,40 @@ local function map_keys()
return key
end, map_opts)
for subkey, mapping in pairs(subkeys) do
if not parent_keys[mode] then
parent_keys[mode] = {}
end
if not parent_keys[mode][subkey] then
parent_keys[mode][subkey] = {}
end
parent_keys[mode][subkey][key] = true
vim.keymap.set(mode, subkey, function()
-- In case the subkey happens to also be a starting key
if last_key == nil then
log_key(subkey)
return subkey
if mapping then
if not parent_keys[mode] then
parent_keys[mode] = {}
end
-- Make sure we are in the correct sequence
if not parent_keys[mode][subkey][last_key] then
return subkey
if not parent_keys[mode][subkey] then
parent_keys[mode][subkey] = {}
end
vim.api.nvim_input(undo_key[mode] or "")
vim.api.nvim_input(
("<cmd>setlocal %smodified<cr>"):format(
bufmodified and "" or "no"
parent_keys[mode][subkey][key] = true
vim.keymap.set(mode, subkey, function()
if not mapping then
return
end
-- In case the subkey happens to also be a starting key
if last_key == nil then
log_key(subkey)
return subkey
end
-- Make sure we are in the correct sequence
if not parent_keys[mode][subkey][last_key] then
return subkey
end
vim.api.nvim_input(undo_key[mode] or "")
vim.api.nvim_input(
("<cmd>setlocal %smodified<cr>"):format(
bufmodified and "" or "no"
)
)
)
if type(mapping) == "string" then
vim.api.nvim_input(mapping)
elseif type(mapping) == "function" then
mapping()
end
end, map_opts)
if type(mapping) == "string" then
vim.api.nvim_input(mapping)
elseif type(mapping) == "function" then
mapping()
end
end, map_opts)
end
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ end

Call the setup function with your options as arguments.

After the rewrite you can also use any function.
So you could for example map `<space><tab>` to jump with luasnip like this:
```lua
i = {
[" "] = {
["<tab>"] = function()
-- Defer execution to avoid side-effects
vim.defer_fn(function()
-- set undo point
vim.o.ul = vim.o.ul
require("luasnip").expand_or_jump()
end, 1)
end
}
}
```

To disable keys set them to `false` in the configuration.

<details>
<summary>Default Config</summary>

Expand Down