nvim-config

Log | Files | Refs | Submodules | README

commit ec2e60ada0bd3929f4acb34cbc8e124a800918ec
parent 1ede5c325e15b9b212d495bb9079245d0f597722
Author: Thomas Vigouroux <thomas.vigouroux@protonmail.com>
Date:   Wed,  5 Oct 2022 10:44:57 +0200

tex: greatly enhance mappings

Diffstat:
Mafter/ftplugin/tex.lua | 108+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
1 file changed, 86 insertions(+), 22 deletions(-)

diff --git a/after/ftplugin/tex.lua b/after/ftplugin/tex.lua @@ -42,38 +42,102 @@ vim.keymap.set('n', 'ge', function() end) end, { buffer = true }) +local function buf_line_length(buf, row) + return vim.api.nvim_buf_get_offset(buf, row + 1) - vim.api.nvim_buf_get_offset(buf, row) - 1 +end + +--- Utilities to wrap visual text into something local function charpos_to_bytepos(buf, row, col) local line = vim.api.nvim_buf_get_lines(buf, row, row + 1, true)[1] return vim.str_byteindex(line, col) end --- Wrap the selection in a call -vim.keymap.set('v', 'gf', function() - local curbuf = vim.api.nvim_get_current_buf() - local start = vim.fn.getcharpos "v" - local stop = vim.fn.getcharpos "." +local function vis_mapping(map, func) + vim.keymap.set('v', map, function() + + -- First determine the current mode + local vmode = vim.fn.mode() + local ty + if vmode == 'v' then + ty = 'char' + elseif vmode == 'V' then + ty = 'line' + else + ty = 'block' + end - 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 curbuf = vim.api.nvim_get_current_buf() + local start = vim.fn.getcharpos 'v' + local stop = vim.fn.getcharpos '.' - local start_line = start[2] - 1 - local start_col = charpos_to_bytepos(curbuf, start_line, start[3] - 1) + 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]) + local end_line = stop[2] - 1 + local end_col = charpos_to_bytepos(curbuf, end_line, stop[3]) - local text = vim.api.nvim_buf_get_text(curbuf, start_line, start_col, end_line, end_col, {}) - vim.pretty_print(text) + -- 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 - vim.ui.input({ prompt = "Function name" }, function(input) - if not input then return 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, { buffer = true }) +end - text[1] = string.format('\\%s{%s', input, text[1]) - text[#text] = text[#text] .. '}' +local function vis_input_change(map, input_name, func) + vis_mapping(map, function(type, curbuf, start_line, start_col, end_line, end_col, text) + vim.ui.input({ prompt = input_name }, function(input) + if not input then return end - vim.pretty_print(text) - vim.api.nvim_buf_set_text(curbuf, start_line, start_col, end_line, end_col, text) + -- Func must modify the text array + func(type, text, input) + vim.api.nvim_buf_set_text(curbuf, start_line, start_col, end_line, end_col, text) + end) end) -end, { buffer = true }) +end + +local function preffix_suffix_change(func) + return function(type, text, input) + + local preffix, suffix = func(input) + + if type == 'char' then + if preffix:sub(#preffix) ~= '{' then + -- Add an articial space here + preffix = preffix .. ' ' + end + + text[1] = preffix .. text[1] + text[#text] = text[#text] .. suffix + elseif type == 'line' then + table.insert(text, 1, preffix) + text[#text + 1] = suffix + elseif type == 'block' then + error "Block type not supported" + end + end +end + +-- Wrap the selection in a call +vis_input_change('<Leader>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) + return '{\\' .. input, '}' +end)) + +-- Wrap the selection in a color block +vis_input_change('<Leader>c', 'Color name', preffix_suffix_change(function(input) + return string.format('{\\color{%s}', input), '}' +end))