diff --git a/lua/neorg/core/utils.lua b/lua/neorg/core/utils.lua index af6e7fc94..4d7016067 100644 --- a/lua/neorg/core/utils.lua +++ b/lua/neorg/core/utils.lua @@ -202,23 +202,16 @@ function utils.set_operatorfunc(f) vim.go.operatorfunc = "v:lua.require'neorg'.utils._neorg_operatorfunc" end -function utils.wrap_dotrepeat(event_handler) - return function(event) +function utils.wrap_dotrepeat(callback) + return function(...) if vim.api.nvim_get_mode().mode == "i" then - event_handler(event) + callback(...) return end - utils._neorg_is_dotrepeat = false + local args = { ... } utils.set_operatorfunc(function() - if utils._neorg_is_dotrepeat then - local pos = assert(vim.fn.getpos(".")) - - event.buffer = pos[1] - event.cursor_position = { pos[2], pos[3] } - end - utils._neorg_is_dotrepeat = true - event_handler(event) + callback(unpack(args)) end) vim.cmd("normal! g@l") end diff --git a/lua/neorg/modules/core/keybinds/module.lua b/lua/neorg/modules/core/keybinds/module.lua index 50cca30dc..261d648c4 100644 --- a/lua/neorg/modules/core/keybinds/module.lua +++ b/lua/neorg/modules/core/keybinds/module.lua @@ -21,6 +21,66 @@ module.load = function() end end +module.config.public = { + -- Whether to use the default keybinds provided [here](https://github.com/nvim-neorg/neorg/blob/main/lua/neorg/modules/core/keybinds/keybinds.lua). + default_keybinds = true, + + -- Which keybind preset to use. + -- TODO: Docs + preset = "neorg", +} + +module.public = { + --- Adds a set of default keys for Neorg to bind. + --- Should be used exclusively by external modules wanting to provide their own default keymaps. + ---@param name string The name of the preset to extend (allows for providing default keymaps for various presets) + ---@param preset neorg.keybinds.preset The preset data itself. + extend_preset = function(name, preset) + local original_preset = assert(module.private.presets[name], "provided preset doesn't exist!") + + local function extend(a, b) + for k, v in pairs(b) do + if type(v) == "table" then + if vim.islist(v) then + vim.list_extend(a[k], v) + else + extend(a[k], v) + end + end + + a[k] = v + end + end + + extend(original_preset, preset) + module.public.bind_keys(vim.api.nvim_get_current_buf()) + end, + + bind_keys = function(buffer) + local is_norg = vim.bo.filetype == "norg" + + local preset = module.private.presets[module.config.public.preset] + assert(preset, string.format("keybind preset `%s` does not exist!", module.config.public.preset)) + + local function set_keys_for(data) + for mode, keybinds in pairs(data) do + for _, keybind in ipairs(keybinds) do + if vim.fn.hasmapto(keybind[2], mode, false) == 0 then + local opts = vim.tbl_deep_extend("force", { buffer = buffer or true }, keybinds.opts or {}) + vim.keymap.set(mode, keybind[1], keybind[2], opts) + end + end + end + end + + set_keys_for(preset.all) + + if is_norg then + set_keys_for(preset.norg) + end + end, +} + module.private = { presets = { ---@class neorg.keybinds.preset @@ -163,64 +223,4 @@ module.private = { }, } -module.config.public = { - -- Whether to use the default keybinds provided [here](https://github.com/nvim-neorg/neorg/blob/main/lua/neorg/modules/core/keybinds/keybinds.lua). - default_keybinds = true, - - -- Which keybind preset to use. - -- TODO: Docs - preset = "neorg", -} - -module.public = { - --- Adds a set of default keys for Neorg to bind. - --- Should be used exclusively by external modules wanting to provide their own default keymaps. - ---@param name string The name of the preset to extend (allows for providing default keymaps for various presets) - ---@param preset neorg.keybinds.preset The preset data itself. - extend_preset = function(name, preset) - local original_preset = assert(module.private.presets[name], "provided preset doesn't exist!") - - local function extend(a, b) - for k, v in pairs(b) do - if type(v) == "table" then - if vim.islist(v) then - vim.list_extend(a[k], v) - else - extend(a[k], v) - end - end - - a[k] = v - end - end - - extend(original_preset, preset) - module.public.bind_keys(vim.api.nvim_get_current_buf()) - end, - - bind_keys = function(buffer) - local is_norg = vim.bo.filetype == "norg" - - local preset = module.private.presets[module.config.public.preset] - assert(preset, string.format("keybind preset `%s` does not exist!", module.config.public.preset)) - - local function set_keys_for(data) - for mode, keybinds in pairs(data) do - for _, keybind in ipairs(keybinds) do - if vim.fn.hasmapto(keybind[2], mode, false) == 0 then - local opts = vim.tbl_deep_extend("force", { buffer = buffer or true }, keybinds.opts or {}) - vim.keymap.set(mode, keybind[1], keybind[2], opts) - end - end - end - end - - set_keys_for(preset.all) - - if is_norg then - set_keys_for(preset.norg) - end - end, -} - return module diff --git a/lua/neorg/modules/core/pivot/module.lua b/lua/neorg/modules/core/pivot/module.lua index 83e3c794f..74b58d060 100644 --- a/lua/neorg/modules/core/pivot/module.lua +++ b/lua/neorg/modules/core/pivot/module.lua @@ -37,7 +37,7 @@ end module.public = { ---@param invert boolean - change_list = function(invert) + change_list = neorg.utils.wrap_dotrepeat(function(invert) local buffer = vim.api.nvim_get_current_buf() local cursor = vim.api.nvim_win_get_cursor(0) @@ -83,7 +83,7 @@ module.public = { end end end - end, + end), } return module diff --git a/lua/neorg/modules/core/promo/module.lua b/lua/neorg/modules/core/promo/module.lua index 3d811ea7b..9da7b0b03 100644 --- a/lua/neorg/modules/core/promo/module.lua +++ b/lua/neorg/modules/core/promo/module.lua @@ -265,19 +265,19 @@ module.private = { ---@class core.promo module.public = { - promote = function() + promote = neorg.utils.wrap_dotrepeat(function() local buffer = vim.api.nvim_get_current_buf() local row = vim.api.nvim_win_get_cursor(0)[1] - 1 module.private.promote_or_demote(buffer, "promote", row, true, false) - end, - promote_nested = function() + end), + promote_nested = neorg.utils.wrap_dotrepeat(function() local buffer = vim.api.nvim_get_current_buf() local row = vim.api.nvim_win_get_cursor(0)[1] - 1 module.private.promote_or_demote(buffer, "promote", row, true, true) - end, - promote_range = function() + end), + promote_range = neorg.utils.wrap_dotrepeat(function() local buffer = vim.api.nvim_get_current_buf() local start_pos = vim.api.nvim_buf_get_mark(buffer, "<") local end_pos = vim.api.nvim_buf_get_mark(buffer, ">") @@ -286,20 +286,20 @@ module.public = { module.private.promote_or_demote(buffer, "promote", i - 1, false, false) end indent.reindent_range(buffer, start_pos[1], end_pos[1]) - end, - demote = function() + end), + demote = neorg.utils.wrap_dotrepeat(function() local buffer = vim.api.nvim_get_current_buf() local row = vim.api.nvim_win_get_cursor(0)[1] - 1 module.private.promote_or_demote(buffer, "demote", row, true, false) - end, - demote_nested = function() + end), + demote_nested = neorg.utils.wrap_dotrepeat(function() local buffer = vim.api.nvim_get_current_buf() local row = vim.api.nvim_win_get_cursor(0)[1] - 1 module.private.promote_or_demote(buffer, "demote", row, true, true) - end, - demote_range = function() + end), + demote_range = neorg.utils.wrap_dotrepeat(function() local buffer = vim.api.nvim_get_current_buf() local start_pos = vim.api.nvim_buf_get_mark(buffer, "<") local end_pos = vim.api.nvim_buf_get_mark(buffer, ">") @@ -308,7 +308,7 @@ module.public = { module.private.promote_or_demote(buffer, "demote", i - 1, false, false) end indent.reindent_range(buffer, start_pos[1], end_pos[1]) - end + end), } return module diff --git a/lua/neorg/modules/core/qol/todo_items/module.lua b/lua/neorg/modules/core/qol/todo_items/module.lua index 9fb12576b..f58af3adb 100644 --- a/lua/neorg/modules/core/qol/todo_items/module.lua +++ b/lua/neorg/modules/core/qol/todo_items/module.lua @@ -405,20 +405,20 @@ module.private = { end, } - local function task_set(character, name) - return function() - local buffer = vim.api.nvim_get_current_buf() - local cursor = vim.api.nvim_win_get_cursor(0) - - local todo_item_at_cursor = module.private.get_todo_item_from_cursor(buffer, cursor[1] - 1) +local function task_set(character, name) + return neorg.utils.wrap_dotrepeat(function() + local buffer = vim.api.nvim_get_current_buf() + local cursor = vim.api.nvim_win_get_cursor(0) - if not todo_item_at_cursor then - return - end + local todo_item_at_cursor = module.private.get_todo_item_from_cursor(buffer, cursor[1] - 1) - module.private.make_all(buffer, todo_item_at_cursor, name, character) + if not todo_item_at_cursor then + return end - end + + module.private.make_all(buffer, todo_item_at_cursor, name, character) + end) +end module.public = { ["task-done"] = task_set("x", "done"),