Skip to content

Commit

Permalink
feat: hijack ft & expose API #61
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd committed Oct 24, 2023
1 parent d7ba5d3 commit 6598813
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
53 changes: 35 additions & 18 deletions lua/image/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ local state = {
remote_cache = {},
tmp_dir = vim.fn.tempname(),
disable_decorator_handling = false,
hijacked_win_buf_images = {},
}

---@type API
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lua/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 6598813

Please sign in to comment.