diff --git a/lua/neo-tree/sources/common/commands.lua b/lua/neo-tree/sources/common/commands.lua index a158282b0..85c8a6edf 100644 --- a/lua/neo-tree/sources/common/commands.lua +++ b/lua/neo-tree/sources/common/commands.lua @@ -669,8 +669,8 @@ M.toggle_preview = function(state) Preview.toggle(state) end -M.scroll_preview = function(state) - Preview.scroll(state) +M.scroll_preview = function(state, fallback) + Preview.scroll(state, fallback) end M.focus_preview = function() diff --git a/lua/neo-tree/sources/common/preview.lua b/lua/neo-tree/sources/common/preview.lua index 51c067a06..7aec65c48 100644 --- a/lua/neo-tree/sources/common/preview.lua +++ b/lua/neo-tree/sources/common/preview.lua @@ -489,15 +489,20 @@ Preview.focus = function() end end -Preview.scroll = function(state) +local CTRL_E = utils.keycode("") +local CTRL_Y = utils.keycode("") +Preview.scroll = function(state, fallback) local direction = state.config.direction - -- NOTE: Chars below are raw escape codes for / - local input = direction < 0 and [[]] or [[]] + local input = direction < 0 and CTRL_E or CTRL_Y local count = math.abs(direction) if Preview:is_active() then vim.api.nvim_win_call(instance.winid, function() - vim.cmd([[normal! ]] .. count .. input) + vim.cmd(("normal! %s%s"):format(count, input)) + end) + else + vim.api.nvim_buf_call(state.bufnr, function() + vim.cmd(("normal! %s"):format(utils.keycode(fallback))) end) end end diff --git a/lua/neo-tree/ui/renderer.lua b/lua/neo-tree/ui/renderer.lua index 717b2afc8..941cf12d3 100644 --- a/lua/neo-tree/ui/renderer.lua +++ b/lua/neo-tree/ui/renderer.lua @@ -13,7 +13,7 @@ local log = require("neo-tree.log") local windows = require("neo-tree.ui.windows") local M = { resize_timer_interval = 50 } -local ESC_KEY = vim.api.nvim_replace_termcodes("", true, false, true) +local ESC_KEY = utils.keycode("") local default_popup_size = { width = 60, height = "80%" } local draw, create_tree, render_tree @@ -852,7 +852,7 @@ local set_buffer_mappings = function(state) if type(func) == "function" then resolved_mappings[cmd].handler = function() state.config = config - return func(state) + return func(state, cmd) end keymap.set(state.bufnr, "n", cmd, resolved_mappings[cmd].handler, map_options) if type(vfunc) == "function" then diff --git a/lua/neo-tree/utils/init.lua b/lua/neo-tree/utils/init.lua index 4868e4776..7887d4a61 100644 --- a/lua/neo-tree/utils/init.lua +++ b/lua/neo-tree/utils/init.lua @@ -1363,6 +1363,14 @@ M.index_by_path = function(tbl, key) return value end +---Backport of vim.keycode +---@see vim.keycode +---@param str string +---@return string representation Internal representation of the keycodes +function M.keycode(str) + return vim.api.nvim_replace_termcodes(str, true, true, true) +end + ---Iterate through a table, sorted by its keys. ---Compared to vim.spairs, it also accepts a method that specifies how to sort the table by key. ---