From 4061c8d0cc3e76d94f0f80be1baea4d6565482af Mon Sep 17 00:00:00 2001 From: Sam <130783534+Sam-programs@users.noreply.github.com> Date: Fri, 5 Jul 2024 14:51:19 +0300 Subject: [PATCH] fix: make the plugin work properly with v0.8-0.9 (#65) * 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 9162b6ecfc16a1188d121681725f35e6807ceb3e 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 --- lua/better_escape.lua | 40 +++++++++++----------------------------- readme.md | 4 +++- 2 files changed, 14 insertions(+), 30 deletions(-) diff --git a/lua/better_escape.lua b/lua/better_escape.lua index d7ef464..010ac2c 100644 --- a/lua/better_escape.lua +++ b/lua/better_escape.lua @@ -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 @@ -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 @@ -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 @@ -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("setlocal %smodified"):format( + vim.api.nvim_input(undo_key[mode] or "") + vim.api.nvim_input( + ("setlocal %smodified"):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 diff --git a/readme.md b/readme.md index 4ef3f42..078b347 100644 --- a/readme.md +++ b/readme.md @@ -52,7 +52,9 @@ k = function() vim.api.nvim_input("") 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 ```