You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thank you for your hard work and a great plugin that saves people lot of typing time.
I've been messing about with ConTeXt, and I can't use the features that VimTeX offers like in_math(), in_text() etc. However, I have tried to replicate some of those features with a few Lua functions. I have a feeling that my is_inside_env(name) is still not quite right and missing something.
If anyone has the experience of how VimTeX handles its in_inside function, please share yours with me.
-- This module contains functions to check if the cursor is in a particular environment
local M = {}
-- Get the current line number and column
local function get_cursor_pos()
local cursor = vim.api.nvim_win_get_cursor(0)
return tonumber(cursor[1]), tonumber(cursor[2])
end
-- Function to check if a pattern exists in a given location
local function contains_pattern(location, pattern)
return location:find(pattern) ~= nil
end
-- Check if the current line is inside an inline math environment
function M.in_inline_math()
local current_row, current_col = get_cursor_pos()
local line = vim.api.nvim_buf_get_lines(0, current_row - 1, current_row, false)[1]
local before_found = contains_pattern(line:sub(1, current_col), "$")
local after_found = contains_pattern(line:sub(current_col + 1), "$")
return before_found and after_found
end
-- Function to check for patterns in a given location
local function contains_patterns(location, patterns)
for _, pattern in ipairs(patterns) do
if contains_pattern(location, pattern) then
return true
end
end
return false
end
-- Check for math start and stop patterns above or below the current line
local function check_math_patterns(direction, start_patterns, stop_patterns)
local math_mode = false
local current_row = get_cursor_pos()
local line_num = current_row
-- -1 or 1 depending on the direction
local increment = (direction == "above" or direction == "before") and -1 or 1
while line_num > 0 and line_num <= vim.api.nvim_buf_line_count(0) do
local line = vim.api.nvim_buf_get_lines(0, line_num - 1, line_num, false)[1]
if line:len() > 0 then
local start_found = contains_patterns(line, start_patterns)
local stop_found = contains_patterns(line, stop_patterns)
if start_found and not stop_found then
math_mode = true
break
elseif stop_found and not start_found then
math_mode = false
break
end
end
line_num = line_num + increment
end
return math_mode
end
-- Check if the current line is inside an environment within the range of 40 lines.
-- Checking the whole buffer is not very efficient.
local function is_inside_env(name)
local current_row = get_cursor_pos()
local lines = vim.api.nvim_buf_get_lines(0, math.max(current_row - 20, 0), math.min(current_row + 20, -1), false)
local start_tag = "\\start" .. name
local stop_tag = "\\stop" .. name
local inside = false
for _, line in ipairs(lines) do
if line:find(start_tag) then
inside = true
end
if line:find(stop_tag) then
inside = false
end
end
return inside
end
-- Check if the cursor is inside a math zone
function M.in_math()
return check_math_patterns("above", {"\\startformula"}, {"\\stopformula"}) or check_math_patterns("below", {"\\startformula"}, {"\\stopformula"})
end
-- Check if the cursor is inside a text zone
function M.in_text()
return is_inside_env("text") and not M.in_math()
end
-- Check if the cursor is inside tikzpicture
function M.in_tikz()
return is_inside_env("tikzpicture")
end
-- Check if the cursor is inside an itemize or enumerate
function M.in_bullets()
return is_inside_env("itemize") or is_inside_env("enumerate")
end
-- Check if the cursor is inside MPcode
function M.in_MPcode()
return is_inside_env("MPcode")
end
return M
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Thank you for your hard work and a great plugin that saves people lot of typing time.
I've been messing about with ConTeXt, and I can't use the features that VimTeX offers like
in_math()
,in_text()
etc. However, I have tried to replicate some of those features with a few Lua functions. I have a feeling that myis_inside_env(name
) is still not quite right and missing something.If anyone has the experience of how VimTeX handles its
in_inside
function, please share yours with me.Beta Was this translation helpful? Give feedback.
All reactions