From cc6992f1ca443f4da01cb4d57159d4cef37b36b7 Mon Sep 17 00:00:00 2001 From: Peter Aronoff Date: Sun, 17 Nov 2024 16:31:24 -0500 Subject: feat(defaults): dot-repeat [ #31186 Problem: `[` and `]` do not support repetition. Solution: use `operatorfunc` and `g@l` to make these mappings dot repeatable. --- runtime/lua/vim/_buf.lua | 23 +++++++++++++++++++++++ runtime/lua/vim/_defaults.lua | 16 ++++++++-------- 2 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 runtime/lua/vim/_buf.lua (limited to 'runtime/lua/vim') diff --git a/runtime/lua/vim/_buf.lua b/runtime/lua/vim/_buf.lua new file mode 100644 index 0000000000..0631c96f77 --- /dev/null +++ b/runtime/lua/vim/_buf.lua @@ -0,0 +1,23 @@ +local M = {} + +--- Adds one or more blank lines above or below the cursor. +-- TODO: move to _defaults.lua once it is possible to assign a Lua function to options #25672 +--- @param above? boolean Place blank line(s) above the cursor +local function add_blank(above) + local offset = above and 1 or 0 + local repeated = vim.fn['repeat']({ '' }, vim.v.count1) + local linenr = vim.api.nvim_win_get_cursor(0)[1] + vim.api.nvim_buf_set_lines(0, linenr - offset, linenr - offset, true, repeated) +end + +-- TODO: move to _defaults.lua once it is possible to assign a Lua function to options #25672 +function M.space_above() + add_blank(true) +end + +-- TODO: move to _defaults.lua once it is possible to assign a Lua function to options #25672 +function M.space_below() + add_blank() +end + +return M diff --git a/runtime/lua/vim/_defaults.lua b/runtime/lua/vim/_defaults.lua index 25afd83145..06f6ed6829 100644 --- a/runtime/lua/vim/_defaults.lua +++ b/runtime/lua/vim/_defaults.lua @@ -366,16 +366,16 @@ do -- Add empty lines vim.keymap.set('n', '[', function() - local repeated = vim.fn['repeat']({ '' }, vim.v.count1) - local linenr = vim.api.nvim_win_get_cursor(0)[1] - vim.api.nvim_buf_set_lines(0, linenr - 1, linenr - 1, true, repeated) - end, { desc = 'Add empty line above cursor' }) + -- TODO: update once it is possible to assign a Lua function to options #25672 + vim.go.operatorfunc = "v:lua.require'vim._buf'.space_above" + return 'g@l' + end, { expr = true, desc = 'Add empty line above cursor' }) vim.keymap.set('n', ']', function() - local repeated = vim.fn['repeat']({ '' }, vim.v.count1) - local linenr = vim.api.nvim_win_get_cursor(0)[1] - vim.api.nvim_buf_set_lines(0, linenr, linenr, true, repeated) - end, { desc = 'Add empty line below cursor' }) + -- TODO: update once it is possible to assign a Lua function to options #25672 + vim.go.operatorfunc = "v:lua.require'vim._buf'.space_below" + return 'g@l' + end, { expr = true, desc = 'Add empty line below cursor' }) end end -- cgit