Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(zen): sync cursor with parent window #547

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

iniw
Copy link

@iniw iniw commented Jan 16, 2025

Description

The cursor position is now synchronized between the zen/zoom window and it's parent.

I'm not sure if this is the best way to achieve that. Initally I wanted to do set the parent's cursor position only when the zen buffer is closed, but I couldn't figure out a way to do that.

Related Issue(s)

Fixes #539

@dpetka2001
Copy link
Contributor

dpetka2001 commented Jan 18, 2025

@iniw I managed to do this another way and not in CursorMoved event which is triggered very often I would assume. Here's the diff

diff --git a/lua/snacks/zen.lua b/lua/snacks/zen.lua
index 06cc860..d925b64 100644
--- a/lua/snacks/zen.lua
+++ b/lua/snacks/zen.lua
@@ -170,6 +170,17 @@ function M.zen(opts)
   end
   opts.on_open(win)
 
+  -- save cursor position before leaving window
+  local cursor_pos
+  vim.api.nvim_create_autocmd("WinLeave", {
+    group = win.augroup,
+    buffer = win.buf,
+    callback = function()
+      cursor_pos = vim.fn.getcurpos()
+      cursor_pos = { cursor_pos[2], cursor_pos[3] - 1 }
+    end,
+  })
+
   -- restore toggle states when window is closed
   vim.api.nvim_create_autocmd("WinClosed", {
     group = win.augroup,
@@ -181,6 +192,9 @@ function M.zen(opts)
       for _, state in ipairs(states) do
         state.toggle:set(state.state)
       end
+      if vim.api.nvim_win_is_valid(parent_win) then
+        vim.api.nvim_win_set_cursor(parent_win, cursor_pos)
+      end
       opts.on_close(win)
     end),
   })
@@ -204,8 +218,14 @@ function M.zen(opts)
       end
       -- exit if other window is not a floating window
       if vim.api.nvim_win_get_config(w).relative == "" then
+        if zoom_indicator then
+          zoom_indicator:close()
+        end
         win:close()
       end
+      if vim.api.nvim_win_is_valid(parent_win) then
+        vim.api.nvim_win_set_cursor(parent_win, cursor_pos)
+      end
     end,
   })
   return win

It also adds a zoom_indicator:close() in WinEnter event because when you did for example <C-w>h to go to another non-floating window, the zoom indicator would stay visible.

Plz feel free to incorporate these changes if you find them to your liking and also test it on your end in case I might have missed something. This is just a suggestion on my part and you're not obliged to incorporate the changes if you disagree. Just in case so there won't be any misunderstandings 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug(zen.zoom): zoom mode doesn't properly restore cursor position
2 participants