From 6598813b4c4e395eba0b2217cd6458aaa58b897f Mon Sep 17 00:00:00 2001 From: 3rd <3rd@users.noreply.github.com> Date: Wed, 25 Oct 2023 01:04:04 +0300 Subject: [PATCH] feat: hijack ft & expose API https://github.com/3rd/image.nvim/issues/61 --- lua/image/init.lua | 53 ++++++++++++++++++++++++++++++---------------- lua/types.lua | 2 ++ 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/lua/image/init.lua b/lua/image/init.lua index 0cc787b..37dde2d 100644 --- a/lua/image/init.lua +++ b/lua/image/init.lua @@ -39,6 +39,7 @@ local state = { remote_cache = {}, tmp_dir = vim.fn.tempname(), disable_decorator_handling = false, + hijacked_win_buf_images = {}, } ---@type API @@ -255,8 +256,6 @@ api.setup = function(options) -- hijack image filetypes if state.options.hijack_file_patterns and #state.options.hijack_file_patterns > 0 then - local hijacked_win_buf_pairs = {} - vim.api.nvim_create_autocmd({ "BufRead", "WinEnter" }, { group = group, pattern = state.options.hijack_file_patterns, @@ -265,22 +264,7 @@ api.setup = function(options) local win = vim.api.nvim_get_current_win() local path = vim.api.nvim_buf_get_name(buf) - local key = ("%s:%s"):format(win, buf) - if hijacked_win_buf_pairs[key] then return end - hijacked_win_buf_pairs[key] = true - - vim.bo[buf].modifiable = true - vim.api.nvim_buf_set_lines(buf, 0, -1, true, { "" }) - - vim.bo[buf].modifiable = false - vim.bo[buf].buftype = "nowrite" - vim.opt_local.colorcolumn = "0" - vim.opt_local.cursorline = false - vim.opt_local.number = false - vim.opt_local.signcolumn = "no" - - local img = api.from_file(path, { window = win, buffer = buf }) - img:render() + api.hijack_buffer(path, win, buf) end, }) end @@ -290,6 +274,39 @@ local guard_setup = function() if not state.backend then utils.throw("image.nvim is not setup. Call setup() first.") end end +---@param path string +---@param win number? if nil or 0, uses current window +---@param buf number? if nil or 0, uses current buffer +---@param options ImageOptions? +api.hijack_buffer = function(path, win, buf, options) + if not win or win == 0 then win = vim.api.nvim_get_current_win() end + if not buf or buf == 0 then buf = vim.api.nvim_get_current_buf() end + + local key = ("%s:%s"):format(win, buf) + if state.hijacked_win_buf_images[key] then return state.hijacked_win_buf_images[key] end + + vim.bo[buf].modifiable = true + vim.api.nvim_buf_set_lines(buf, 0, -1, true, { "" }) + + vim.bo[buf].modifiable = false + vim.bo[buf].buftype = "nowrite" + vim.bo[buf].filetype = "image_nvim" + vim.opt_local.colorcolumn = "0" + vim.opt_local.cursorline = false + vim.opt_local.number = false + vim.opt_local.signcolumn = "no" + + local opts = options or {} + opts.window = win + opts.buffer = buf + + local img = api.from_file(path, opts) + img:render() + + state.hijacked_win_buf_images[key] = img + return img +end + ---@param path string ---@param options? ImageOptions api.from_file = function(path, options) diff --git a/lua/types.lua b/lua/types.lua index 508fd77..5a80e7d 100644 --- a/lua/types.lua +++ b/lua/types.lua @@ -6,6 +6,7 @@ ---@field from_url fun(url: string, options?: ImageOptions, callback: fun(image: Image|nil)) ---@field clear fun(id?: string) ---@field get_images fun(opts?: { window?: number, buffer?: number }): Image[] +---@field hijack_buffer fun(path: string, window?: number, buffer?: number, options?: ImageOptions): Image|nil ---@class State ---@field backend Backend @@ -15,6 +16,7 @@ ---@field remote_cache { [string]: string } ---@field tmp_dir string ---@field disable_decorator_handling boolean +---@field hijacked_win_buf_images { [string]: Image } ---@class DocumentIntegrationOptions ---@field enabled? boolean