vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit 5385c9f13622e978e52132622fc316ec49fee319 parent 132125f6bb35bbef9b2c2826bbf76c64af405f7c Author: Marc André Tanner <mat@brain-dump.org> Date: Wed, 17 Feb 2016 23:07:52 +0100 vis-lua: add text object ii to match based on syntax item/token Roughly speaking this should match whatever is colored the same way as the cursor. Diffstat:
| M | visrc.lua | | | 31 | +++++++++++++++++++++++++++++++ |
1 file changed, 31 insertions(+), 0 deletions(-)
diff --git a/visrc.lua b/visrc.lua @@ -29,6 +29,37 @@ vis.textobject_new = function(vis, key, textobject) return true end +vis:textobject_new("ii", function(win, pos) + + if win.syntax == nil then + return pos, pos + end + + local before, after = pos - 4096, pos + 4096; + if before < 0 then + before = 0 + end + -- TODO make sure we start at a line boundary? + + local lexer = vis.lexers.load(win.syntax) + local data = win.file:content(before, after - before) + local tokens = lexer:lex(data) + local cur = before + -- print(before..", "..pos..", ".. after) + + for i = 1, #tokens, 2 do + local name = tokens[i] + local token_next = before + tokens[i+1] - 1 + -- print(name..": ["..cur..", "..token_next.."] pos: "..pos) + if cur <= pos and pos < token_next then + return cur, token_next + end + cur = token_next + end + + return pos, pos +end) + vis.events.win_open = function(win) local files = {