rmagatti / goto-preview

A small Neovim plugin for previewing definitions using floating windows.
Apache License 2.0
779 stars 27 forks source link

Provide an option for making preview window unchangeable. #114

Closed swaroopanand10 closed 5 months ago

swaroopanand10 commented 6 months ago

Is your feature request related to a problem? Please describe. When preview window is open, it can be edited, sometimes accidental changes happen, and get's saved and go unnoticed.

Describe the solution you'd like It would be nice to have an option to make preview buffer read-only and hence unchangeable.

rmagatti commented 6 months ago

This is trickier than it sounds. nomodifiable can be set for a buffer, but not a window afaik. The closes you can get to this currently is previewing the window with the peek function, which doesn't focus the preview window and closes it when you move your cursor.

swaroopanand10 commented 6 months ago

Is it possible to achieve the following: 1). When preview window is opened, make the particular buffer shown by preview window "nomodifiable". 2). When that preview window is closed, make that same buffer "modifiable" again, so that it can be edited again. This will achieve that same functionality but in a different way, and will prevent the accidental modification.

rmagatti commented 6 months ago

With a hook, yes. When the post hook fires, you can grab the bufnr coming from the hook to set the buffer as nomodifiable. Then unset that when the preview window with the given buffer closes

swaroopanand10 commented 5 months ago

Thanks @rmagatti for the help and sorry for the late reply, since I achieved this functionality using post_open_hook, and post_close_hook. Should I close this issue?

rmagatti commented 4 months ago

@swaroopanand10 might be worth posting the code for your solution here to help others if they come across this.

swaroopanand10 commented 4 months ago

@rmagatti , Actually there is a problem in my solution that it works only 50% of time, and is inconsistent. ( I didn't realized it sooner) What I wrote is very straightforward, post_open_hook = function() vim.api.nvim_command("set nomodifiable") end, post_close_hook = function() vim.api.nvim_command('set modifiable') end,

But sometimes it just does not make the preview buffer unmodifiable and even if it does becomes "nomodifiable" successfully, after few times of pressing 'i' key, the buffer again becomes modifiable(weird behavior).

While, If I remove the "post_close_hook" from there and only keep "post_open_hook", then it always works and make the preview buffer "nomodifiable" 100% of time.

but the problem with that approach is then we have to make that preview buffer modifiable manually to edit it again. Maybe there is some problem with how I'm writing the "post_close_hook" function because it is taking effect before the preview window getting closed.

rmagatti commented 3 months ago

@swaroopanand10 at first glance it looks like your hook code should work but I believe using the bufnr argument passed down to the hooks would be more correct and perhaps result in a more consistent behaviour. e.g

post_open_hook = function(bufnr, _)
  vim.api.nvim_set_option_value("modifiable", false, { buf = bufnr })
end

post_close_hook = function(bufnr, _)
  vim.api.nvim_set_option_value("modifiable", true, { buf = bufnr })
end