Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions doc/render-latex.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ and:
Inline math is intentionally not image-rendered. It uses highlights and conceal
for smooth editing and predictable cursor behavior.

Images are hidden while popups or floating windows are open. Plain command-line
typing does not hide images by default; opt in with
`render.hide_on_cmdline = true`. When command-line hiding is disabled, cmdline
UI floats are ignored but completion popups still hide images until
command-line mode exits.
Images are hidden while completion popups are open. Plain command-line typing
does not hide images by default; opt in with `render.hide_on_cmdline = true`.
Floating windows from other plugins do not hide images, but active float details
are reported by `:RenderLatex doctor` for troubleshooting.

===============================================================================
REQUIREMENTS *render-latex-requirements*
Expand Down
21 changes: 8 additions & 13 deletions lua/render_latex/annotations.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local Config = require("render_latex.config")
local Compat = require("render_latex.compat")
local Detect = require("render_latex.detect")
local Util = require("render_latex.util")

local M = {}

Expand Down Expand Up @@ -326,19 +327,13 @@ function M.render_inline_fallback(bufnr, ns, state, ranges)
end
local active = {}

for _, winid in ipairs(vim.fn.win_findbuf(bufnr)) do
if vim.api.nvim_win_is_valid(winid) then
local cursor = vim.api.nvim_win_get_cursor(winid)
local cursor_row = cursor[1] - 1
local cursor_col = cursor[2]
for index, item in ipairs(items) do
if
cursor_row == item.row
and cursor_col >= item.start_col
and cursor_col < item.end_col
then
active[index] = true
end
for _, winid in ipairs(Util.current_tab_wins_for_buf(bufnr)) do
local cursor = vim.api.nvim_win_get_cursor(winid)
local cursor_row = cursor[1] - 1
local cursor_col = cursor[2]
for index, item in ipairs(items) do
if cursor_row == item.row and cursor_col >= item.start_col and cursor_col < item.end_col then
active[index] = true
end
end
end
Expand Down
63 changes: 49 additions & 14 deletions lua/render_latex/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@ local function register_autocmds()

local function queue_visible_buffers()
local seen = {}
for _, winid in ipairs(vim.api.nvim_list_wins()) do
local cfg = vim.api.nvim_win_get_config(winid)
if cfg.relative == nil or cfg.relative == "" then
local bufnr = vim.api.nvim_win_get_buf(winid)
if not seen[bufnr] and should_attach(bufnr) then
seen[bufnr] = true
Renderer.attach(bufnr)
Renderer.queue(bufnr)
end
local Util = require("render_latex.util")
for _, winid in ipairs(Util.current_tab_normal_wins()) do
local bufnr = vim.api.nvim_win_get_buf(winid)
if not seen[bufnr] and should_attach(bufnr) then
seen[bufnr] = true
Renderer.attach(bufnr)
Renderer.queue(bufnr)
end
end
end
Expand All @@ -58,8 +56,6 @@ local function register_autocmds()
end

vim.api.nvim_create_autocmd({
"BufEnter",
"BufWinEnter",
"InsertEnter",
"InsertLeave",
"CursorMoved",
Expand All @@ -83,6 +79,17 @@ local function register_autocmds()
end,
})

vim.api.nvim_create_autocmd({ "BufEnter", "BufWinEnter", "TabEnter", "WinEnter" }, {
group = Config.augroup,
callback = function(args)
local bufnr = args.buf or vim.api.nvim_get_current_buf()
if not should_attach(bufnr) then
Renderer.detach(bufnr)
end
update_transient_ui_suppression(true)
end,
})

vim.api.nvim_create_autocmd("ColorScheme", {
group = Config.augroup,
callback = function()
Expand All @@ -92,6 +99,13 @@ local function register_autocmds()
end,
})

vim.api.nvim_create_autocmd({ "BufLeave", "TabLeave", "WinLeave" }, {
group = Config.augroup,
callback = function()
Renderer.hide_visible()
end,
})

vim.api.nvim_create_autocmd("WinScrolled", {
group = Config.augroup,
callback = function(args)
Expand Down Expand Up @@ -129,12 +143,13 @@ local function register_autocmds()
vim.api.nvim_create_autocmd("BufWinLeave", {
group = Config.augroup,
callback = function(args)
Renderer.detach_window(args.buf, vim.api.nvim_get_current_win())
Renderer.hide_visible()
Renderer.detach_inactive_windows(args.buf)
update_transient_ui_suppression(false)
end,
})

vim.api.nvim_create_autocmd({ "CmdlineEnter", "CmdlineChanged", "CmdlineLeave", "WinEnter" }, {
vim.api.nvim_create_autocmd({ "CmdlineEnter", "CmdlineChanged", "CmdlineLeave" }, {
group = Config.augroup,
callback = function()
update_transient_ui_suppression(true)
Expand All @@ -144,6 +159,7 @@ local function register_autocmds()
vim.api.nvim_create_autocmd("WinClosed", {
group = Config.augroup,
callback = function(args)
Renderer.hide_visible()
local winid = tonumber(args.match)
if winid ~= nil then
Renderer.detach_winid(winid)
Expand Down Expand Up @@ -261,6 +277,7 @@ function M.doctor_lines()
local Install = require("render_latex.install")
local Integrations = require("render_latex.integrations")
local Renderer = require("render_latex.renderer")
local Ui = require("render_latex.ui")
local Worker = require("render_latex.worker")

local bufnr = vim.api.nvim_get_current_buf()
Expand All @@ -272,6 +289,7 @@ function M.doctor_lines()
local render = Renderer.resolved_options()
local source = require("render_latex.sources").status(bufnr)
local suppression = Renderer.suppression_status()
local floats = Ui.floating_windows()
local filetype = vim.bo[bufnr].filetype

local lines = {
Expand Down Expand Up @@ -313,7 +331,24 @@ function M.doctor_lines()
"conceallevel: " .. tostring(vim.wo.conceallevel),
"concealcursor: " .. tostring(vim.wo.concealcursor),
"suppressed by cmdline: " .. tostring(suppression.cmdline),
"image placement suppressed by popups/floating windows: " .. tostring(suppression.floating),
"image placement suppressed by popup/cmdline UI: " .. tostring(suppression.floating),
"active floating windows: " .. tostring(#floats),
})

for _, float in ipairs(floats) do
lines[#lines + 1] = table.concat({
"- winid: " .. tostring(float.winid),
"relative: " .. tostring(float.relative),
"focusable: " .. tostring(float.focusable),
"zindex: " .. tostring(float.zindex),
"row: " .. tostring(float.row),
"col: " .. tostring(float.col),
"width: " .. tostring(float.width),
"height: " .. tostring(float.height),
}, ", ")
end

vim.list_extend(lines, {
"",
"## Worker",
"",
Expand Down
65 changes: 43 additions & 22 deletions lua/render_latex/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -444,15 +444,13 @@ end
local function visible_inline_ranges(bufnr)
local ranges = {}
local line_count = vim.api.nvim_buf_line_count(bufnr)
for _, winid in ipairs(vim.fn.win_findbuf(bufnr)) do
if Util.win_is_valid(winid) then
local top = math.max(0, vim.fn.line("w0", winid) - 1)
local bottom = math.min(line_count - 1, vim.fn.line("w$", winid) - 1)
ranges[#ranges + 1] = { start_row = top, end_row = bottom }
end
for _, winid in ipairs(Util.current_tab_wins_for_buf(bufnr)) do
local top = math.max(0, vim.fn.line("w0", winid) - 1)
local bottom = math.min(line_count - 1, vim.fn.line("w$", winid) - 1)
ranges[#ranges + 1] = { start_row = top, end_row = bottom }
end
if #ranges == 0 then
return nil
return {}
end
return Sources.inline_ranges(bufnr, ranges)
end
Expand Down Expand Up @@ -614,7 +612,7 @@ function M.hide_all()
end
end

local function hide_visible()
function M.hide_visible()
local backend = ImageBackend.get()
if backend ~= nil then
pcall(backend.del, math.huge)
Expand Down Expand Up @@ -766,19 +764,17 @@ local function collect_window_snapshot(bufnr, viewport_state, prefetch)
windows = {},
}

for _, winid in ipairs(vim.fn.win_findbuf(bufnr)) do
if Util.win_is_valid(winid) then
local top, bottom = Viewport.viewport_range(viewport_state, winid, prefetch)
local text_top, text_bottom = Viewport.visible_text_bounds(winid)
snapshot.winids[#snapshot.winids + 1] = winid
snapshot.ranges[#snapshot.ranges + 1] = { top = top, bottom = bottom }
snapshot.windows[winid] = {
width = vim.api.nvim_win_get_width(winid),
text_top = text_top,
text_bottom = text_bottom,
cursor_row = vim.api.nvim_win_get_cursor(winid)[1] - 1,
}
end
for _, winid in ipairs(Util.current_tab_wins_for_buf(bufnr)) do
local top, bottom = Viewport.viewport_range(viewport_state, winid, prefetch)
local text_top, text_bottom = Viewport.visible_text_bounds(winid)
snapshot.winids[#snapshot.winids + 1] = winid
snapshot.ranges[#snapshot.ranges + 1] = { top = top, bottom = bottom }
snapshot.windows[winid] = {
width = vim.api.nvim_win_get_width(winid),
text_top = text_top,
text_bottom = text_bottom,
cursor_row = vim.api.nvim_win_get_cursor(winid)[1] - 1,
}
end

return snapshot
Expand Down Expand Up @@ -1395,6 +1391,31 @@ function M.detach_window(bufnr, winid)
clear_window_images(bufnr, state, winid)
end

---@param bufnr integer
function M.detach_inactive_windows(bufnr)
local state = buffers[bufnr]
if state == nil then
return
end

local winids = {}
for winid, _ in pairs(state.images) do
winids[winid] = true
end
for winid, _ in pairs(state.focused_keys) do
winids[winid] = true
end
for winid, _ in pairs(state.viewports) do
winids[winid] = true
end

for winid, _ in pairs(winids) do
if not Util.win_is_valid(winid) or vim.api.nvim_win_get_buf(winid) ~= bufnr then
clear_window_images(bufnr, state, winid)
end
end
end

---@param winid integer
function M.detach_winid(winid)
for bufnr, state in pairs(buffers) do
Expand Down Expand Up @@ -1499,7 +1520,7 @@ function M.set_suppressed(reason, value)
if suppression.cmdline then
M.hide_all()
elseif suppression.floating then
hide_visible()
M.hide_visible()
end
end

Expand Down
28 changes: 22 additions & 6 deletions lua/render_latex/ui.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
local M = {}

function M.has_popup_or_floating_windows()
if vim.fn.pumvisible() == 1 then
return true
end
local Config = require("render_latex.config")

function M.floating_windows()
local floats = {}
for _, winid in ipairs(vim.api.nvim_list_wins()) do
local cfg = vim.api.nvim_win_get_config(winid)
if cfg.relative ~= nil and cfg.relative ~= "" then
return true
floats[#floats + 1] = {
winid = winid,
relative = cfg.relative,
focusable = cfg.focusable,
zindex = cfg.zindex,
row = cfg.row,
col = cfg.col,
width = cfg.width,
height = cfg.height,
}
end
end
return false
return floats
end

function M.has_popup_or_floating_windows()
if vim.fn.pumvisible() == 1 then
return true
end

return Config.render.hide_on_cmdline and vim.fn.getcmdtype() ~= ""
end

return M
32 changes: 32 additions & 0 deletions lua/render_latex/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,38 @@ function M.win_is_valid(winid)
return winid ~= nil and vim.api.nvim_win_is_valid(winid)
end

---@param winid integer
function M.win_is_normal(winid)
if not M.win_is_valid(winid) then
return false
end
local cfg = vim.api.nvim_win_get_config(winid)
return cfg.relative == nil or cfg.relative == ""
end

---@return integer[]
function M.current_tab_normal_wins()
local wins = {}
for _, winid in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
if M.win_is_normal(winid) then
wins[#wins + 1] = winid
end
end
return wins
end

---@param bufnr integer
---@return integer[]
function M.current_tab_wins_for_buf(bufnr)
local wins = {}
for _, winid in ipairs(M.current_tab_normal_wins()) do
if vim.api.nvim_win_get_buf(winid) == bufnr then
wins[#wins + 1] = winid
end
end
return wins
end

---@param value string
---@return string
function M.escape_pattern(value)
Expand Down
9 changes: 4 additions & 5 deletions lua/render_latex/viewport.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local Config = require("render_latex.config")
local Util = require("render_latex.util")

local M = {}

Expand Down Expand Up @@ -77,11 +78,9 @@ end
---@return { top: integer, bottom: integer }[]
function M.viewport_ranges(bufnr, viewport_state, prefetch)
local ranges = {}
for _, winid in ipairs(vim.fn.win_findbuf(bufnr)) do
if vim.api.nvim_win_is_valid(winid) then
local top, bottom = M.viewport_range(viewport_state, winid, prefetch)
ranges[#ranges + 1] = { top = top, bottom = bottom }
end
for _, winid in ipairs(Util.current_tab_wins_for_buf(bufnr)) do
local top, bottom = M.viewport_range(viewport_state, winid, prefetch)
ranges[#ranges + 1] = { top = top, bottom = bottom }
end
return ranges
end
Expand Down
Loading
Loading