Skip to content

Commit

Permalink
fix: use a weak callback for mapping hacks
Browse files Browse the repository at this point in the history
  • Loading branch information
declancm committed Jul 14, 2024
1 parent 4cced28 commit 6ac53b8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
1 change: 1 addition & 0 deletions lua/cinnamon/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local defaults = {
---@class ScrollOptions
options = {
callback = nil, ---@type function?
_weak_callback = nil, ---@type function?
delay = 5, ---@type number
step_size = {
vertical = 1, ---@type number
Expand Down
12 changes: 6 additions & 6 deletions lua/cinnamon/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ M.setup = function(user_config)
end)
end
end
vim.keymap.set("n", "n", function() M.scroll("n", { callback = next_search_cb }) end)
vim.keymap.set("n", "N", function() M.scroll("N", { callback = prev_search_cb }) end)
vim.keymap.set("n", "*", function() M.scroll("*", { callback = next_search_cb }) end)
vim.keymap.set("n", "#", function() M.scroll("#", { callback = prev_search_cb }) end)
vim.keymap.set("n", "g*", function() M.scroll("g*", { callback = next_search_cb }) end)
vim.keymap.set("n", "g#", function() M.scroll("g#", { callback = prev_search_cb }) end)
vim.keymap.set("n", "n", function() M.scroll("n", { _weak_callback = next_search_cb }) end)
vim.keymap.set("n", "N", function() M.scroll("N", { _weak_callback = prev_search_cb }) end)
vim.keymap.set("n", "*", function() M.scroll("*", { _weak_callback = next_search_cb }) end)
vim.keymap.set("n", "#", function() M.scroll("#", { _weak_callback = prev_search_cb }) end)
vim.keymap.set("n", "g*", function() M.scroll("g*", { _weak_callback = next_search_cb }) end)
vim.keymap.set("n", "g#", function() M.scroll("g#", { _weak_callback = prev_search_cb }) end)

-- Previous/next cursor location:
vim.keymap.set("n", "<C-o>", function() M.scroll("<C-o>") end)
Expand Down
5 changes: 3 additions & 2 deletions lua/cinnamon/scroll.lua
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,9 @@ H.scroller = {
end,

cleanup = function(self)
if self.options.callback ~= nil then
local success, message = pcall(self.options.callback)
local callback = self.options.callback or self.options._weak_callback
if callback ~= nil then
local success, message = pcall(callback)
if not success then
utils.notify("Error executing callback: " .. message, "warn")
end
Expand Down

0 comments on commit 6ac53b8

Please sign in to comment.