diff --git a/plugin/commands.lua b/plugin/commands.lua index 5fcf51a..b40e90b 100644 --- a/plugin/commands.lua +++ b/plugin/commands.lua @@ -165,3 +165,27 @@ vim.api.nvim_create_user_command('CheckboxToggle', function(opts) if x == 'x' then x = ' ' else x = 'x' end vim.fn.setline(linenr, string.sub(line, 1, pos) .. x .. string.sub(line, pos + 2)) 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 })