nvim-config

Log | Files | Refs | Submodules | README

commit d017dea1f97f50c79bda5e48390c679b54472b1a
parent ff59616dcba75c440f911ec9e94930a58a94c1a6
Author: Thomas Vigouroux <thomas.vigouroux@protonmail.com>
Date:   Wed, 21 Sep 2022 11:10:23 +0200

latex: add mapping to wrap in function call

Diffstat:
Mafter/ftplugin/tex.lua | 38++++++++++++++++++++++++++++++++++++++
1 file changed, 38 insertions(+), 0 deletions(-)

diff --git a/after/ftplugin/tex.lua b/after/ftplugin/tex.lua @@ -17,6 +17,8 @@ end local edit = require"architext.edit" local ts_utils = require'nvim-treesitter.ts_utils' local p = require'nvim-treesitter.parsers' + +-- Replace the surrounding environment local query = vim.treesitter.parse_query("latex", [[ (generic_environment (begin (curly_group_text text: (_) @envbegin)) @@ -39,3 +41,39 @@ vim.keymap.set('n', 'ge', function() end end) end, { buffer = true }) + +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 "." + + 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 text = vim.api.nvim_buf_get_text(curbuf, start_line, start_col, end_line, end_col, {}) + vim.pretty_print(text) + + vim.ui.input({ prompt = "Function name" }, function(input) + if not input then return end + + text[1] = string.format('\\%s{%s', input, text[1]) + text[#text] = text[#text] .. '}' + + vim.pretty_print(text) + vim.api.nvim_buf_set_text(curbuf, start_line, start_col, end_line, end_col, text) + end) +end, { buffer = true })