-
I have been trying to write a function to open the image currently under the cursor in a full screen preview buffer, but currently run into the situation where it either shows the large image and also the image at the original cursor location (but large) or nothing. Any pointers appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hey, would it be an option to set Something like: local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
vim.fn.system({
"git",
"clone",
"--filter=blob:none",
"--branch=stable",
"https://github.com/folke/lazy.nvim.git",
lazypath,
})
end
vim.opt.rtp:prepend(lazypath)
_G.log = function(...)
local objects = vim.tbl_map(vim.inspect, { ... })
print(unpack(objects))
end
require("lazy").setup({
{
"3rd/image.nvim",
dir = "/home/rabbit/.config/nvim/plugins/image.nvim",
event = "VeryLazy",
dependencies = {
{
"nvim-treesitter/nvim-treesitter",
build = ":TSUpdate",
config = function()
require("nvim-treesitter.configs").setup({
ensure_installed = { "markdown" },
highlight = { enable = true },
})
end,
},
},
opts = {
backend = "kitty",
integrations = {
markdown = {
enabled = true,
clear_in_insert_mode = false,
download_remote_images = true,
only_render_image_at_cursor = false,
filetypes = { "markdown", "vimwiki" }, -- markdown extensions (ie. quarto) can go here
},
neorg = {
enabled = true,
clear_in_insert_mode = false,
download_remote_images = true,
only_render_image_at_cursor = false,
filetypes = { "norg" },
},
},
max_width = nil,
max_height = nil,
max_width_window_percentage = nil,
max_height_window_percentage = 50,
kitty_method = "normal",
window_overlap_clear_enabled = true,
},
},
})
package.path = package.path .. ";" .. vim.fn.expand("$HOME") .. "/.luarocks/share/lua/5.1/?/init.lua;"
package.path = package.path .. ";" .. vim.fn.expand("$HOME") .. "/.luarocks/share/lua/5.1/?.lua;"
vim.opt.number = true
vim.opt.conceallevel = 2
vim.opt.winbar = "image.nvim demo"
vim.opt.signcolumn = "yes:2"
local content = [[
# Hello World
![This is a remote image](https://gist.ro/s/remote.png)
]]
local initial_buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(initial_buf, 0, -1, true, vim.split(content, "\n"))
vim.api.nvim_set_option_value("filetype", "markdown", { buf = initial_buf })
vim.api.nvim_set_current_buf(initial_buf)
-- sample below this line
local api = require("image")
local get_image_at_cursor = function(bufnr)
local images = api.get_images({ buffer = bufnr })
local row = vim.api.nvim_win_get_cursor(0)[1]
for _, img in ipairs(images) do
if img.geometry ~= nil and img.geometry.y == row then return img end
end
return nil
end
local create_preview_window = function()
local buf = vim.api.nvim_create_buf(false, true)
local win_width = vim.api.nvim_get_option_value("columns", {})
local win_height = vim.api.nvim_get_option_value("lines", {})
local win = vim.api.nvim_open_win(buf, true, {
relative = "editor",
style = "minimal",
width = win_width,
height = win_height,
row = 0,
col = 0,
zindex = 1000,
})
vim.keymap.set("n", "q", function()
vim.api.nvim_win_close(win, true)
end, { buffer = buf })
return { buf = buf, win = win }
end
local handle_zoom = function(bufnr)
local image = get_image_at_cursor(bufnr)
if image == nil then return end
local preview = create_preview_window()
api.hijack_buffer(image.path, preview.win, preview.buf)
end
vim.keymap.set("n", "K", function()
local bufnr = vim.api.nvim_get_current_buf()
handle_zoom(bufnr)
end, { buffer = true }) |
Beta Was this translation helpful? Give feedback.
Hey, would it be an option to set
window_overlap_clear_enabled = true
?It should work with that, and it's a bit more complicated without as the integration doesn't have the capability to pause its work.
We can add that, but maybe the overlap trick is enough.
Something like: