Skip to content

Commit

Permalink
fix: make the plugin work properly with v0.8-0.9 (#65)
Browse files Browse the repository at this point in the history
* refactor: rewrite to use mappings

* fix: formatting

* refactor: properly detect when a sequence shouldn't continue

* cleanup

* softer deprecation

* update readme for rewrite

* fix: remove nowait to allow remapping keys

* feat: allow disabling mappings and update readme

* refactor: swap expr-mappings with `feedkeys`

to allow modifying the buffer in functions

* docs: update readme

* docs: update readme

* Revert "refactor: swap expr-mappings with `feedkeys`"

This reverts commit 9162b6e because it
assumes the user has stable-v10.0.0.

* refactor: remove the function 't' because it's no longer used.

---------

Co-authored-by: Max <[email protected]>
  • Loading branch information
Sam-programs and max397574 authored Jul 5, 2024
1 parent 3973b07 commit 4061c8d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 30 deletions.
40 changes: 11 additions & 29 deletions lua/better_escape.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
local M = {}
local uv = vim.uv
local function t(str)
if vim.keycode then
return vim.keycode(str)
else
return vim.api.nvim_replace_termcodes(str, true, true, true)
end
end

M.waiting = false

Expand Down Expand Up @@ -74,10 +67,7 @@ local function log_key(key)
end)
end

vim.on_key(function(mappings, typed)
if typed == "" then
return
end
vim.on_key(function()
if recorded_key then
recorded_key = false
return
Expand All @@ -94,15 +84,15 @@ local undo_key = {
s = "",
}
local parent_keys = {}

local function map_keys()
parent_keys = {}
for mode, keys in pairs(settings.mappings) do
local map_opts = { expr = true }
for key, subkeys in pairs(keys) do
vim.keymap.set(mode, key, function()
log_key(key)
vim.api.nvim_feedkeys(t(key), "in", false)
end)
return key
end, map_opts)
for subkey, mapping in pairs(subkeys) do
if mapping then
if not parent_keys[mode] then
Expand All @@ -116,32 +106,24 @@ local function map_keys()
-- In case the subkey happens to also be a starting key
if last_key == nil then
log_key(subkey)
vim.api.nvim_feedkeys(t(subkey), "in", false)
return
return subkey
end
-- Make sure we are in the correct sequence
if not parent_keys[mode][subkey][last_key] then
vim.api.nvim_feedkeys(t(subkey), "in", false)
return
return subkey
end
vim.api.nvim_feedkeys(
t(undo_key[mode] or ""),
"in",
false
)
vim.api.nvim_feedkeys(
t("<cmd>setlocal %smodified<cr>"):format(
vim.api.nvim_input(undo_key[mode] or "")
vim.api.nvim_input(
("<cmd>setlocal %smodified<cr>"):format(
bufmodified and "" or "no"
),
"in",
false
)
)
if type(mapping) == "string" then
vim.api.nvim_input(mapping)
elseif type(mapping) == "function" then
mapping()
end
end)
end, map_opts)
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ k = function()
vim.api.nvim_input("<esc>")
local current_line = vim.api.nvim_get_current_line()
if current_line:match("^%s+j$") then
vim.api.nvim_set_current_line("")
vim.schedule(function()
vim.api.nvim_set_current_line("")
end)
end
end
```
Expand Down

0 comments on commit 4061c8d

Please sign in to comment.