Target window/buffer in client

I’m integrating some long running asynchronous processes (think ci, linters, style checkers, etc…) and I’ve set them up to send eval -client commands to create highlighters as issues are found. It works as long as I don’t switch buffers between the start and result of the external process, otherwise the highlighter gets applied to the wrong window/buffer. As a minimal example based on Kakoune’s interfacing document, run this in one buffer then switch to another buffer before the sleep expires.

:nop %sh{ {
    sleep 10
    echo "eval -client '$kak_client' 'execute-keys %{ohello wrong buffer<esc>}'" |
        kak -p ${kak_session}
} > /dev/null 2>&1 < /dev/null & }

Is there a way to target the window scope for a specific buffer for a specific client?

As always seems to be the case, I stumbled on to a solution after finally pulling the trigger on a post. Using execute-keys -draft with a :buffer seems to do the trick.

:nop %sh{ {
    sleep 10
    echo "eval -client '$kak_client' 'execute-keys -draft %{:buffer $kak_bufname<ret>ohello right buffer<esc>}'" |
        kak -p ${kak_session}
} > /dev/null 2>&1 < /dev/null & }

There’s also eval -buffer $kak_bufname which lets you execute KakScript in the context of an open buffer, whether or not it’s visible in any particular client.

The problem I’ve had with eval is that it can’t target the window scope.

eval -client client0 %[ eval -buffer test.c %[ set-option window custom_hl_range various_matching_ranges ] ]

fails with

Error: 1:1: 'eval': 1:2: 'eval': 1:2: 'set-option': no window in context

EDIT: And again I stumble on the solution after posting. If you reverse the nested evals it works.

eval -buffer test.c %[ eval -client client0 %[ set-option window custom_hl_range various_matching_ranges ] ]