From 1ede343c5315e59b1e624db4c49296fa22395329 Mon Sep 17 00:00:00 2001 From: alohaia Date: Fri, 25 Sep 2026 12:30:05 +0800 Subject: [PATCH 1/3] fix: Restore the original key mappings after handling or --- lua/r/edit.lua | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lua/r/edit.lua b/lua/r/edit.lua index 8bf4897f..fdae00c8 100644 --- a/lua/r/edit.lua +++ b/lua/r/edit.lua @@ -68,6 +68,8 @@ M.pipe = function() local temp_remaps = { "", "" } for _, key in pairs(temp_remaps) do + local old = vim.fn.maparg(key, "i", false, true) + vim.keymap.set("i", key, function() local cursor = vim.api.nvim_win_get_cursor(0) if not cursor then return end @@ -82,20 +84,21 @@ M.pipe = function() -- Insert the newline vim.api.nvim_input(key) end, { buffer = 0 }) - end - -- Set a single-use autocommand so that if the user changes the text, i.e. - -- keeps typing after inserting the pipe, the above keymappings are removed - vim.schedule(function() - vim.api.nvim_create_autocmd({ "TextChangedI", "CursorMovedI", "InsertLeave" }, { - once = true, - callback = function() - for _, key in pairs(temp_remaps) do + vim.schedule(function() + vim.api.nvim_create_autocmd({ "TextChangedI", "CursorMovedI", "InsertLeave" }, { + callback = function(args) pcall(vim.keymap.del, "i", key, { buffer = 0 }) + if not vim.tbl_isempty(old) then + vim.notify(string.format("Recovery %s", key)) + vim.fn.mapset(old) + end + + pcall(vim.api.nvim_del_autocmd, args.id) -- real once end - end, - }) - end) + }) + end) + end end M.buf_enter = function() From 4c4ea2f26748e18a870409e719f14fce2d0e8919 Mon Sep 17 00:00:00 2001 From: Philippe Massicotte Date: Fri, 25 Sep 2026 11:19:00 -0400 Subject: [PATCH 2/3] restore buffer-local mappings once per buffer --- lua/r/edit.lua | 57 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/lua/r/edit.lua b/lua/r/edit.lua index fdae00c8..0f52a91f 100644 --- a/lua/r/edit.lua +++ b/lua/r/edit.lua @@ -33,6 +33,20 @@ M.assign = function() end end +local pipe_saved_maps = {} + +local restore_pipe_maps = function(bufnr) + local saved = pipe_saved_maps[bufnr] + if not saved then return end + pipe_saved_maps[bufnr] = nil + for key, old in pairs(saved) do + pcall(vim.keymap.del, "i", key, { buffer = bufnr }) + if old and vim.api.nvim_buf_is_valid(bufnr) then + vim.api.nvim_buf_call(bufnr, function() vim.fn.mapset("i", false, old) end) + end + end +end + M.pipe = function() local pipe_opts = { native = " |> ", @@ -67,9 +81,29 @@ M.pipe = function() -- mode. local temp_remaps = { "", "" } - for _, key in pairs(temp_remaps) do - local old = vim.fn.maparg(key, "i", false, true) + local bufnr = vim.api.nvim_get_current_buf() + if not pipe_saved_maps[bufnr] then + local saved = {} + for _, key in pairs(temp_remaps) do + local old = vim.fn.maparg(key, "i", false, true) + saved[key] = old.buffer == 1 and old or false + end + pipe_saved_maps[bufnr] = saved + + vim.schedule(function() + vim.api.nvim_create_autocmd( + { "TextChangedI", "CursorMovedI", "InsertLeave" }, + { + buffer = bufnr, + once = true, + callback = function() restore_pipe_maps(bufnr) end, + } + ) + end) + end + + for _, key in pairs(temp_remaps) do vim.keymap.set("i", key, function() local cursor = vim.api.nvim_win_get_cursor(0) if not cursor then return end @@ -79,25 +113,10 @@ M.pipe = function() -- Move the cursor back one column. This only makes a difference if -- a pipe has been inserted mid-line vim.api.nvim_win_set_cursor(0, { row, col - 1 }) - -- Delete this keymapping so whitespace stripping doesn't happen again - vim.keymap.del("i", key, { buffer = 0 }) + restore_pipe_maps(bufnr) -- Insert the newline vim.api.nvim_input(key) - end, { buffer = 0 }) - - vim.schedule(function() - vim.api.nvim_create_autocmd({ "TextChangedI", "CursorMovedI", "InsertLeave" }, { - callback = function(args) - pcall(vim.keymap.del, "i", key, { buffer = 0 }) - if not vim.tbl_isempty(old) then - vim.notify(string.format("Recovery %s", key)) - vim.fn.mapset(old) - end - - pcall(vim.api.nvim_del_autocmd, args.id) -- real once - end - }) - end) + end, { buffer = bufnr }) end end From e24a74aa320ada577ca3ee8ed189c74158047464 Mon Sep 17 00:00:00 2001 From: alohaia Date: Sun, 27 Sep 2026 10:19:12 +0800 Subject: [PATCH 3/3] fix: explicitly delete autocmd to prevent triggering on each event --- lua/r/edit.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lua/r/edit.lua b/lua/r/edit.lua index 0f52a91f..486f53c1 100644 --- a/lua/r/edit.lua +++ b/lua/r/edit.lua @@ -96,8 +96,10 @@ M.pipe = function() { "TextChangedI", "CursorMovedI", "InsertLeave" }, { buffer = bufnr, - once = true, - callback = function() restore_pipe_maps(bufnr) end, + callback = function(args) + restore_pipe_maps(bufnr) + pcall(vim.api.nvim_del_autocmd, args.id) + end, } ) end)