martanne / vis

A vi-like editor based on Plan 9's structural regular expressions
Other
4.25k stars 258 forks source link

We have rotate, what about reverse? #1024

Closed hyphenrf closed 2 years ago

hyphenrf commented 2 years ago

in visual mode, the :help page says that we can use + and - to rotate selected elements. Is there a way to reverse them?

ninewise commented 2 years ago

It's not built-in, but it should be possible to get this from a lua plugin using vis.win:selections_iterator().

ninewise commented 2 years ago
vis:map(vis.modes.VISUAL, ',', function(keys)
        local ranges = {}
        local contents = {}
        for selection in vis.win:selections_iterator() do
                table.insert(ranges, selection.range)
                table.insert(contents, vis.win.file:content(selection.range))
        end
        for source = 1, #ranges do
                local target = #ranges - source + 1
                vis.win.file:delete(ranges[target])
                vis.win.file:insert(ranges[target].start, contents[source])
        end
        vis.win.selections = {}
        vis.mode = vis.modes.NORMAL
        vis:feedkeys("<vis-redraw>")
end, "reverse selections")

Here's a start. You probably still want to add something to keep the selections after reversing but you'd have to to some index-juggling.