nvim-config

Log | Files | Refs | Submodules | README

commit 5f8866ff32f300ce2452c0b6d8faf7d44639ff71
parent ec2e60ada0bd3929f4acb34cbc8e124a800918ec
Author: Thomas Vigouroux <thomas.vigouroux@protonmail.com>
Date:   Wed,  5 Oct 2022 11:07:33 +0200

tex: also add an operator mapping

Diffstat:
Mafter/ftplugin/tex.lua | 72+++++++++++++++++++++++++++++++++++++++++++++++++-----------------------
1 file changed, 49 insertions(+), 23 deletions(-)

diff --git a/after/ftplugin/tex.lua b/after/ftplugin/tex.lua @@ -52,9 +52,38 @@ local function charpos_to_bytepos(buf, row, col) return vim.str_byteindex(line, col) end +local function correct_position_wrap(ty, spos, epos, func) + local curbuf = vim.api.nvim_get_current_buf() + local start = vim.fn.getcharpos(spos) + local stop = vim.fn.getcharpos(epos) + + if start[2] > stop[2] or (start[2] == stop[2] and start[3] > stop[3]) then + -- Swap these if the order is wrong + start, stop = stop, start + end + + local start_line = start[2] - 1 + local start_col = charpos_to_bytepos(curbuf, start_line, start[3] - 1) + + local end_line = stop[2] - 1 + local end_col = charpos_to_bytepos(curbuf, end_line, stop[3]) + + -- Now if we are in line mode, correct the offsets to span the whole lines + if ty == 'line' then + start_col = 0 + end_col = buf_line_length(curbuf, end_line) + end + + local text = vim.api.nvim_buf_get_text(curbuf, start_line, start_col, end_line, end_col, {}) + + func(ty, curbuf, start_line, start_col, end_line, end_col, text) +end + +TexDispatch=function() end + local function vis_mapping(map, func) + -- First handle the visual mapping vim.keymap.set('v', map, function() - -- First determine the current mode local vmode = vim.fn.mode() local ty @@ -66,31 +95,28 @@ local function vis_mapping(map, func) ty = 'block' end - local curbuf = vim.api.nvim_get_current_buf() - local start = vim.fn.getcharpos 'v' - local stop = vim.fn.getcharpos '.' - - if start[2] > stop[2] or (start[2] == stop[2] and start[3] > stop[3]) then - -- Swap these if the order is wrong - start, stop = stop, start - end + correct_position_wrap(ty, 'v', '.', func) + end, { buffer = true }) - local start_line = start[2] - 1 - local start_col = charpos_to_bytepos(curbuf, start_line, start[3] - 1) + -- Then create the operator mapping + vim.keymap.set('n', map, function() - local end_line = stop[2] - 1 - local end_col = charpos_to_bytepos(curbuf, end_line, stop[3]) + -- Ensure that the operator func is this correct one + local tmpopf = vim.o.operatorfunc + vim.o.operatorfunc = 'v:lua.TexDispatch' - -- Now if we are in line mode, correct the offsets to span the whole lines - if ty == 'line' then - start_col = 0 - end_col = buf_line_length(curbuf, end_line) + _G.TexDispatch = function(ty) + correct_position_wrap(ty, "'[", "']", func) + vim.o.operatorfunc = tmpopf end - local text = vim.api.nvim_buf_get_text(curbuf, start_line, start_col, end_line, end_col, {}) + return 'g@' + end, { buffer = true, expr = true }) - func(ty, curbuf, start_line, start_col, end_line, end_col, text) - end, { buffer = true }) + -- Doubling works on the current line + vim.keymap.set('n', map .. map, function() + correct_position_wrap('line', '.', '.', func) + end) end local function vis_input_change(map, input_name, func) @@ -128,16 +154,16 @@ local function preffix_suffix_change(func) end -- Wrap the selection in a call -vis_input_change('<Leader>f', 'Function name', preffix_suffix_change(function(input) +vis_input_change('<LocalLeader>f', 'Function name', preffix_suffix_change(function(input) return string.format('\\%s{', input), '}' end)) -- Wrap the selection in an inline environment -vis_input_change('<Leader>e', 'Inline env name', preffix_suffix_change(function(input) +vis_input_change('<LocalLeader>e', 'Inline env name', preffix_suffix_change(function(input) return '{\\' .. input, '}' end)) -- Wrap the selection in a color block -vis_input_change('<Leader>c', 'Color name', preffix_suffix_change(function(input) +vis_input_change('<LocalLeader>c', 'Color name', preffix_suffix_change(function(input) return string.format('{\\color{%s}', input), '}' end))