summaryrefslogtreecommitdiff
path: root/.config/nvim/lua
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2023-09-14 22:05:28 +0200
committerThomas Voss <mail@thomasvoss.com> 2023-09-14 22:05:28 +0200
commit15a7c2d8e005391d4aaac919b5776b5fa2822a41 (patch)
tree1494c7d7bed4ebf8daf9a60d33d3b54444a70360 /.config/nvim/lua
parent0d2929454871b43eab2169d694ae07bc39ef3789 (diff)
nvim: Preserve registers when transposing chars
Diffstat (limited to '.config/nvim/lua')
-rw-r--r--.config/nvim/lua/mango/lib.lua16
-rw-r--r--.config/nvim/lua/mango/remap.lua16
2 files changed, 29 insertions, 3 deletions
diff --git a/.config/nvim/lua/mango/lib.lua b/.config/nvim/lua/mango/lib.lua
index 4c38986..9182a06 100644
--- a/.config/nvim/lua/mango/lib.lua
+++ b/.config/nvim/lua/mango/lib.lua
@@ -18,4 +18,20 @@ function M.remap(modes, from, to, opts)
vim.keymap.set(ct, from, to, opts)
end
+function M.save_regs(regs, callback)
+ local rs = {}
+ for r in regs:gmatch('.') do
+ rs[r] = {
+ s = vim.fn.getreg(r),
+ t = vim.fn.getregtype(r),
+ }
+ end
+
+ callback()
+
+ for k, v in pairs(rs) do
+ vim.fn.setreg(k, v.s, v.t)
+ end
+end
+
return M
diff --git a/.config/nvim/lua/mango/remap.lua b/.config/nvim/lua/mango/remap.lua
index a43ba19..0c4f1a9 100644
--- a/.config/nvim/lua/mango/remap.lua
+++ b/.config/nvim/lua/mango/remap.lua
@@ -1,4 +1,5 @@
-local remap = require('mango.lib').remap
+local lib = require('mango.lib')
+local remap = lib.remap
vim.g.mapleader = ' '
@@ -46,5 +47,14 @@ remap('n', '<leader>Y', '"+Y')
remap('nv', "'", '`')
remap('nv', '`', "'")
--- Transpose characters, because ‘xp’ is kinda awkward
-remap('n', '<leader>t', 'xp')
+-- Transpose characters without clobbering registers
+local function transpose_chars(rev)
+ lib.save_regs('a', function()
+ vim.cmd.normal('"a' .. (rev and 'X' or 'x') .. '"ap')
+ end)
+end
+
+remap('n', '<leader>t', transpose_chars)
+remap('n', '<leader>T', function()
+ transpose_chars(true)
+end)