From 5acab3d8a9bc85a571688db432f2702dd7d901a4 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Thu, 21 Nov 2024 17:36:22 -0800 Subject: [PATCH] fix: image.nvim previews with preview_method=scratch --- lua/oil/init.lua | 6 +++++- lua/oil/util.lua | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 7645f35..bbd290d 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -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 diff --git a/lua/oil/util.lua b/lua/oil/util.lua index 24b714b..441421b 100644 --- a/lua/oil/util.lua +++ b/lua/oil/util.lua @@ -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