Add :Tac command to replace :!tac & be cross-platform

This commit is contained in:
Kenneth Benzie 2024-09-21 10:26:46 +01:00
parent 55f5da7db7
commit 7963c56fd9

View File

@ -165,3 +165,27 @@ vim.api.nvim_create_user_command('CheckboxToggle', function(opts)
if x == 'x' then x = ' ' else x = 'x' end if x == 'x' then x = ' ' else x = 'x' end
vim.fn.setline(linenr, string.sub(line, 1, pos) .. x .. string.sub(line, pos + 2)) vim.fn.setline(linenr, string.sub(line, 1, pos) .. x .. string.sub(line, pos + 2))
end, {}) end, {})
-- :Tac reverse the order of lines in the selected range or the whole file if
-- no range is specificed, works as if executing :!tac but purely in Lua making
-- it cross-platform.
vim.api.nvim_create_user_command('Tac', function(opts)
local buffer = vim.api.nvim_get_current_buf()
local line1 = opts.line1 - 1
local line2 = opts.line2
if opts.range == 0 then
line1 = 0
line2 = -1
end
print(opts.range, line1, line2)
local lines = vim.api.nvim_buf_get_lines(buffer, line1, line2, false)
if lines and #lines > 1 then
local temp = nil
for n = 1, math.floor(#lines / 2) do
temp = lines[n]
lines[n] = lines[#lines - (n - 1)]
lines[#lines - (n - 1)] = temp
end
vim.api.nvim_buf_set_lines(buffer, line1, line2, true, lines)
end
end, { range = true })