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

[bug] Image hijack does not work with :vs #60

Closed
pysan3 opened this issue Oct 23, 2023 · 6 comments
Closed

[bug] Image hijack does not work with :vs #60

pysan3 opened this issue Oct 23, 2023 · 6 comments

Comments

@pysan3
Copy link
Contributor

pysan3 commented Oct 23, 2023

Hi, thanks again for implementing the image preview feature for png etc files.

Please forgive me if I'm wrong, but as I was testing your code I think we need more events than only BufRead to work properly.

For example, when calling :vsplit, the image does not appear on the second window.

I guess we should use { "BufRead", "BufWinEnter" } to make this work?


To be exact, I believe this issue comes from the code below, where has_valid_filetype does not take options.hijack_file_patterns into account. But in real world, since the image buffer cannot be modified, we don't need for example CursorMoved, Insert*, nor opened as a new empty buffer (BufNew etc), which results in covering all cases with just BufRead, BufWinEnter?

local setup_autocommands = function(ctx)
local group_name = ("image.nvim:%s"):format(config.name)
local group = vim.api.nvim_create_augroup(group_name, { clear = true })
-- watch for window changes
vim.api.nvim_create_autocmd({ "WinNew", "BufWinEnter" }, {
group = group,
callback = function(args)
if not has_valid_filetype(ctx, vim.bo[args.buf].filetype) then return end
render(ctx)
end,
})
-- watch for text changes
vim.api.nvim_create_autocmd({ "BufAdd", "BufNew", "BufNewFile" }, {
group = group,
callback = function(args)
if not has_valid_filetype(ctx, vim.bo[args.buf].filetype) then return end
setup_text_change_watcher(ctx, args.buf)
render(ctx)
end,
})
if has_valid_filetype(ctx, vim.bo.filetype) then setup_text_change_watcher(ctx, vim.api.nvim_get_current_buf()) end
if ctx.options.only_render_image_at_cursor then
vim.api.nvim_create_autocmd({ "CursorMoved" }, {
group = group,
callback = function(args)
if not has_valid_filetype(ctx, vim.bo[args.buf].filetype) then return end
render(ctx)
end,
})
end
if ctx.options.clear_in_insert_mode then
vim.api.nvim_create_autocmd({ "InsertEnter" }, {
group = group,
callback = function(args)
if not has_valid_filetype(ctx, vim.bo[args.buf].filetype) then return end
local current_window = vim.api.nvim_get_current_win()
local images = ctx.api.get_images({ window = current_window })
for _, image in ipairs(images) do
image:clear()
end
end,
})
vim.api.nvim_create_autocmd({ "InsertLeave" }, {
group = group,
callback = function(args)
if not has_valid_filetype(ctx, vim.bo[args.buf].filetype) then return end
render(ctx)
end,
})
end
end

@pysan3 pysan3 changed the title [bug] Image preview does not work with :vs [bug] Image hijack does not work with :vs Oct 23, 2023
@3rd
Copy link
Owner

3rd commented Oct 23, 2023

Hey, thanks for the issue!
Yep, you're right, I'll fix that!

@3rd
Copy link
Owner

3rd commented Oct 24, 2023

Done, good thing you mentioned :split, wouldn't thought about adding BufWinEnter without thinking that it doesn't catch plain :split!

image-hijack-split.mp4

@3rd 3rd closed this as completed Oct 24, 2023
@pysan3
Copy link
Contributor Author

pysan3 commented Oct 25, 2023

Thanks for the implementation!

However, may I ask you why you chose WinEnter instead of BufWinEnter? @3rd

The following code does not work.

-- start with a blank (or a non-image) window

local leftwin = vim.api.nvim_get_current_win()

local test_img_path = "/path/to/image.png"

vim.cmd.vsplit(test_img_path)
local rightwin = vim.api.nvim_get_current_win()
local rightbuf = vim.api.nvim_get_current_buf()

-- `test_img_path` loaded into the right window.
-- works because this was the first time to load the image,
-- hence autocmd `BufRead` kicks in.

vim.api.nvim_win_set_buf(leftwin, rightbuf)

-- image should be loaded into the left window as well (without focusing!!)
-- cursor remains at the right window

vim.api.nvim_buf_get_name(vim.api.nvim_win_get_buf(leftwin)) == test_img_path
-- but image not rendered because it is not `BufRead`
-- AND not `WinEnter` because the buffer was loaded without focusing the left window

-- Image not rendered on left window !!!

To be clear, if this is intentional, I have no problem and I can deal with this problem inside neo-tree. Just wanted to make sure that it is possible to load a buffer inside a window without invoking WinEnter.

@3rd
Copy link
Owner

3rd commented Oct 27, 2023

Ah we need BufWinEnter as well, will add.

@3rd 3rd reopened this Oct 27, 2023
@pysan3
Copy link
Contributor Author

pysan3 commented Oct 27, 2023

If we have WinBufEnter, now I think we actually don't need WinEnter.

@3rd
Copy link
Owner

3rd commented Oct 27, 2023

BufWinEnter doesn't work with :split, because the buffer is already in a window and it doesn't trigger.

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

No branches or pull requests

2 participants