From 2d3085e40154813b034bce1ad034f0d323c0c581 Mon Sep 17 00:00:00 2001 From: aznhe21 Date: Sat, 8 Oct 2022 21:34:16 +0900 Subject: [PATCH] fix: do not change current window --- lua/hop/window.lua | 58 ++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/lua/hop/window.lua b/lua/hop/window.lua index 290dae26..d4ab1c0a 100644 --- a/lua/hop/window.lua +++ b/lua/hop/window.lua @@ -3,35 +3,37 @@ local hint = require'hop.hint' local M = {} local function window_context(win_handle, cursor_pos) - -- get a bunch of information about the window and the cursor - vim.api.nvim_set_current_win(win_handle) - local win_info = vim.fn.getwininfo(win_handle)[1] - local win_view = vim.fn.winsaveview() - local top_line = win_info.topline - 1 - local bot_line = win_info.botline - - -- NOTE: due to an (unknown yet) bug in neovim, the sign_width is not correctly reported when shifting the window - -- view inside a non-wrap window, so we can’t rely on this; for this reason, we have to implement a weird hack that - -- is going to disable the signs while hop is running (I’m sorry); the state is restored after jump - -- local left_col_offset = win_info.variables.context.number_width + win_info.variables.context.sign_width - local win_width = nil - - -- hack to get the left column offset in nowrap - if not vim.wo.wrap then - vim.api.nvim_win_set_cursor(win_handle, { cursor_pos[1], 0 }) - local left_col_offset = vim.fn.wincol() - 1 - vim.fn.winrestview(win_view) - win_width = win_info.width - left_col_offset - end + -- get window context without changing current window + return vim.api.nvim_win_call(win_handle, function() + -- get a bunch of information about the window and the cursor + local win_info = vim.fn.getwininfo(win_handle)[1] + local win_view = vim.fn.winsaveview() + local top_line = win_info.topline - 1 + local bot_line = win_info.botline + + -- NOTE: due to an (unknown yet) bug in neovim, the sign_width is not correctly reported when shifting the window + -- view inside a non-wrap window, so we can’t rely on this; for this reason, we have to implement a weird hack that + -- is going to disable the signs while hop is running (I’m sorry); the state is restored after jump + -- local left_col_offset = win_info.variables.context.number_width + win_info.variables.context.sign_width + local win_width = nil + + -- hack to get the left column offset in nowrap + if not vim.wo.wrap then + vim.api.nvim_win_set_cursor(win_handle, { cursor_pos[1], 0 }) + local left_col_offset = vim.fn.wincol() - 1 + vim.fn.winrestview(win_view) + win_width = win_info.width - left_col_offset + end - return { - hwin = win_handle, - cursor_pos = cursor_pos, - top_line = top_line, - bot_line = bot_line, - win_width = win_width, - col_offset = win_view.leftcol - } + return { + hwin = win_handle, + cursor_pos = cursor_pos, + top_line = top_line, + bot_line = bot_line, + win_width = win_width, + col_offset = win_view.leftcol + } + end) end -- Collect all multi-windows's context: