Skip to content

Commit

Permalink
fix: image.nvim previews with preview_method=scratch
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Nov 22, 2024
1 parent bf81e2a commit 5acab3d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lua/oil/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,11 @@ M.open_preview = function(opts, callback)

local entry_is_file = not vim.endswith(normalized_url, "/")
local filebufnr
if entry_is_file and config.preview_win.preview_method ~= "load" then
if
entry_is_file
and config.preview_win.preview_method ~= "load"
and not util.file_matches_bufreadcmd(normalized_url)
then
filebufnr =
util.read_file_to_scratch_buffer(normalized_url, config.preview_win.preview_method)
end
Expand Down
22 changes: 22 additions & 0 deletions lua/oil/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -952,4 +952,26 @@ M.read_file_to_scratch_buffer = function(path, preview_method)
return bufnr
end

local _regcache = {}
---Check if a file matches a BufReadCmd autocmd
---@param filename string
---@return boolean
M.file_matches_bufreadcmd = function(filename)
local autocmds = vim.api.nvim_get_autocmds({
event = "BufReadCmd",
})
for _, au in ipairs(autocmds) do
local pat = _regcache[au.pattern]
if not pat then
pat = vim.fn.glob2regpat(au.pattern)
_regcache[au.pattern] = pat
end

if vim.fn.match(filename, pat) >= 0 then
return true
end
end
return false
end

return M

0 comments on commit 5acab3d

Please sign in to comment.