Use options in regex highlights

Hello there,

I’d like to do something as

declare-option -docstring "The length of line" int linelength 80

add-highlighter shared/too-long regex "^[^\n]{%opt{linelength}}([^\n]*)$" 1:white,yellow

and use this in my filetype config

set-option buffer linelength 72
add-highlighter buffer/ ref too-long

But it seems that %opt{linelength} is expanded first with the value 80 and not 72.

Is it possible do to something like this ?

Thanks a lot!

I believe this happens because the option expansion occurs when the regex highlighter is intially defined, and not when you reference it in the buffer highlighter.

The only way I can think of to implement this sort of behavior is to define a command that defines the highlighter. Maybe something like this could work (warning: untested):

declare-option -docstring "The length of line" int linelength 80

define-command -docstring "Update the too-long highlighter" update-too-long %{
    remove-highlighter buffer/too-long
    add-highlighter buffer/too-long regex "^[^\n]{%opt{linelength}}([^\n]*)$" 1:white,yellow 

You can then do something like this in your filetype config:

set-option buffer linelength 72

Yeah I think ref only works when the highlighter is supposed to be the same everywhere it is referenced.

You can update the highlighter whenever the option is set:

hook global BufSetOption linelength=.* update-too-long

also WinSetOption.

If I am not missing something, the dynregex highlighter is exactly what you are looking for. It evaluates its argument dynamically, so you can use the following instead (note the single quotes):

add-highlighter shared/too-long dynregex '^[^\n]{%opt{linelength}}([^\n]*)$' 1:white,yellow

UwU this is perfeeeeect !