Skip to content

Commit

Permalink
feat: add dotrepeat to all common commands
Browse files Browse the repository at this point in the history
  • Loading branch information
vhyrro committed Jul 13, 2024
1 parent 4f09926 commit 6b49397
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 97 deletions.
17 changes: 5 additions & 12 deletions lua/neorg/core/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
120 changes: 60 additions & 60 deletions lua/neorg/modules/core/keybinds/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions lua/neorg/modules/core/pivot/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -83,7 +83,7 @@ module.public = {
end
end
end
end,
end),
}

return module
24 changes: 12 additions & 12 deletions lua/neorg/modules/core/promo/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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, ">")
Expand All @@ -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, ">")
Expand All @@ -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
22 changes: 11 additions & 11 deletions lua/neorg/modules/core/qol/todo_items/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit 6b49397

Please sign in to comment.