martanne / vis

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

Changing parenthesis highlighting #935

Open simalaia opened 3 years ago

simalaia commented 3 years ago

Is there a way to modify the default parenthesis highlighting? I can't find how to do this. I edit a lot of scheme code and needing to have the cursor before a (instead of after) close paren in order to highlight its match is annoying.

ghost commented 3 years ago

If you are willing to build vis from source code, you can apply this commit as a patch.

BTW, there is an open ticket for this behavior - #626 .

simalaia commented 3 years ago

Thanks, I'll look into that when I have a moment. Incidentally I actually think it would be better to expose the functionality to choose how, and which characters to highlight so one could write custom highlighters.

ghost commented 3 years ago

Custom highlighting can already be done, but the problem remains that the built-in match highlighting can't be turned off.

Below's a naive attempt that you can test-drive by putting it in visrc.lua. It's off by a character so you can actually see that it's working.

local opposite = {
    ["["] = "]",
    ["("] = ")",
    ["{"] = "}",
    ["]"] = "[",
    [")"] = "(",
    ["}"] = "{",
}

vis.events.subscribe(vis.events.WIN_HIGHLIGHT, function(win)
    local cur = win.file:content(win.selection.pos, 1)
    local forward = cur:match"[({[]"
    local backward = cur:match"[]})]"
    local start, match
    if forward then
        start = win.selection.pos
        local range = win.file:content(start, win.viewport.finish)
        _, match = range:find("^%b" .. forward .. opposite[forward])
    elseif backward then
        start = win.viewport.start
        local range = win.file:content(start + 1, win.selection.pos - start)
        match = range:find("%b" .. opposite[backward] .. backward .. "$")
        match = match and match + 1
    end
    if match then
        win:style(win.STYLE_CURSOR, start + match, start + match)
    end
end)
simalaia commented 3 years ago

Oh, in that case it may actually work reasonably. May well be simpler to just patch vis to allow builtin highlighting to be turned off. I'll check out your code thank you! :)

ghost commented 3 years ago

May well be simpler to just patch vis to allow builtin highlighting to be turned off. @simalaia

Or, instead, non-Lua builds could keep the current C implementation, and Lua builds could use something like the above code. (Just like all the files under lua/plugins/ contain features only available in Lua builds.) It can be customized without recompiling, and the performance difference with the C implementation won't be a big deal as match highlighting is not called all that often, and only works (and makes sense) on the currently visible portion of the text.