Squash commits
[vis-motsel.git] / init.lua
blob454ffc1e5b044b48209f69868f8ad461136cebc0
1 require("vis")
2 local vis = vis
4 local progname = ...
6 local M = {prefix = "<Enter>"} -- ensures hands alternating
8 local function selection_new_from_motion(keys)
9 -- XXX: only motions with one-key mappings are supported:
10 if #keys < 1 then return -1 end
11 -- XXX: do not consume the count argument:
12 if tonumber(keys) then return -1 end
13 -- XXX: avoid entering an infinite loop:
14 if keys == M.prefix then return #keys end
16 for _ = 1, vis.count or 1 do
17 local first = vis.win.selections[1].pos
18 local last = vis.win.selections[#vis.win.selections].pos
19 vis:feedkeys("<vis-mark>a<vis-selections-save><vis-selections-remove-all>")
21 -- test the motion direction:
22 local pos = vis.win.selection.pos
23 vis:feedkeys(keys)
24 local forward = vis.win.selection.pos > pos
26 -- perform the motion again, this time on the appropriate end of the selection set:
27 vis.win.selection.pos = forward and last or first
28 vis:feedkeys(keys)
30 vis:feedkeys("<vis-mark>a<vis-selections-union>")
32 -- for convenience, set the primary selection to either the first or last one,
33 -- according to the motion direction:
34 if forward then
35 vis:feedkeys("<vis-selection-prev>")
36 end
37 end
38 return #keys
39 end
41 vis.events.subscribe(vis.events.INIT, function()
42 local function h(msg)
43 return string.format("|@%s| %s", progname, msg)
44 end
46 vis:map(vis.modes.NORMAL, M.prefix, selection_new_from_motion, h"Create a new selection at the end of the motion that follows")
47 end)
49 return M