Inserting new line literal into a buffer from expansion

I’ve been working on a proof of concept for working with Kakoune expansions that will work something like the create-context function in this example.

define-command confirm-file-deletion %{
    prompt "Confirm Deletion? (YES/NO)" %{
        # %var{text} holds the contents of the user prompt
        create-context %var{text} %{
            try %{
                # is the user input the string yes?
                exec _s(?i)^yes$<ret>
            catch %{ try %{
                # is the user input the string no? Then exit function.
                exec _s(?i)^no$<ret>
            } catch %{
                # input was invalid, ask for input again

Essentially I want to be able to treat the output of %opt{}, %var{}, %reg{} and %sh{} blocks as if they were a temporary buffer (plus some extra stuff like selecting each str in a declare-option str-list automatically). The intent is be able to program some basic logic using Kakoune’s text editing primitives and without using the shell. One of the restrictions at the moment is getting the contents of something like %var{selection} or %sh{find} into a buffer the same way and while preserving new lines.

Does anyone know a way to achieve this? I’m currently using

exec "i%arg{0}"

inside my create-context function but because this prints new line literals and not <ret> everything is one one line. In an ideal world there would probably be something like echo -to-buffer 'temporary-buffer' %arg{0}.

IIUC you’re trying to test user input. Why not to use shell expansion? Like this

define-command -docstring "Ask before repeating last command (with `.' (dot)) for next search match" \
query-repeat %{ try %{
    execute-keys n
    prompt "confirm? [yn]: " -on-change %{ execute-keys %sh{
        case ${kak_text} in
            (y) printf "%s\n" "<esc>.: query-repeat<ret>";;
            (n) printf "%s\n" "<esc>: query-repeat<ret>" ;;
            (*) ;;
    }} nop
} catch %{
    fail "no search pattern"

Or do you want to create a temporary buffer with user input and make any Kakoune commands act upon it as if it was active buffer, without making it visible?

The snippet I posted was just an example of how someone might use the create-context command and it actually works just fine. I just want to be able to do more with it and for that I need to figure out how to insert a multi-line expansion into a buffer

I’m not using shell expansions because I was experimenting with different ways to implement logic inside of Kakoune and using it’s text editing abilities with the textual variables, options and other expansions seemed natural to me.