How can I prevent undo and redo from creating multiple cursors?

I execute & and A keys in a command.
Thus, if I undo the command, I see two cursors. It is annoying.

How can I prevent undo and redo from creating multiple cursors?

helps if you tell us what the actuall command is, give some example where it creates multiple cursors and say exactly what you wanted to happen instead.

If you just don’t want the cursor to move at all try
exec -draft u
or
Zuz

I have to remove

execute-keys -draft '"wzA<space><space><space><space><esc>"

in order to prevent this from creating multiple cursors after doing undo.

declare-option \
    -docstring 'regex matching the head of special forms' \
    str-list janet_special_indent_forms \
    'def.*|while|for|fn\*?|if(-.*|)|let.*|loop|seq|with(-.*|)|when(-.*|)|' \
    'defer|do|match|var|try'

define-command -hidden janet-indent-on-new-line %{
    # registers: i = best align point so far; w = start of first word of form
    evaluate-commands -draft -save-regs '/"|^@iw' -itersel %{
        # Assign the left-most position to i register.
        execute-keys -draft 'gk"iZ'
        try %{
            # Set i and w registers to the position to the right of
            # the opening parenthesis
            execute-keys -draft '[bl"i<a-Z><gt>"wZ'
            execute-keys -draft '"wzA<space><space><space><space><esc>"'

            try %{
                # If it is a special form, indent (indentwidth - 1) spaces
                execute-keys -draft '"wz?\s<ret>H' \
                '<a-k>\A(?:' %opt{janet_special_indent_forms} ')\z<ret>'
                execute-keys -draft '"wz<a-l>s.{' \
                %sh{printf $(( kak_opt_indentwidth - 1 ))} \
                '}\K.*<ret><a-;>;"i<a-Z><gt>'
            } catch %{
                # If it is not special, and parameter appears on line 1,
                # indent to parameter
                execute-keys -draft '"wz?\s<ret>' \
                '<a-l>s\h\K[^\s].*<ret><a-;>;"i<a-Z><gt>'
            } catch %{
                # If it is not special, and parameters appear on next lines,
                # indent (indentwidth - 1) spaces
                execute-keys -draft '"wz<a-l>s.{' \
                %sh{printf $(( kak_opt_indentwidth - 1 ))} \
                '}\K.*<ret><a-;>;"i<a-Z><gt>'
            }
        }
        # Assign the position next to the left bracket to i register
        try %{ execute-keys -draft '[rl"i<a-Z><gt>' }
        # Assign the position next to the left brace to i register
        try %{ execute-keys -draft '[Bl"i<a-Z><gt>' }
        # Remove leading spaces in the current line.
        try %{ execute-keys -draft '<a-i><space>d' }
        # Align the current line with i register.
        # `;` eliminates selection so that `&` can work without an issue.
        execute-keys -draft ';"i<a-z>a&<space>'
        # Remove trailing spaces in the previous line
        try %{ execute-keys -draft 'kgl<a-i><space>d' }
        # Remove trailing spaces on the line that has the opening parenthesis.
        try %{ execute-keys -draft '"wzgl<a-i><space>d' }
    }
}

I think something’s going wrong with your indent hook if the & is changing the reference line. you want to get two single character selections, the first and only character on the empty line you want to indent and the character on the above line you want to set the indentation level to. with this setup only the bottom line gets modified so kakoune shouldn’t try and create a selection on the top line.

When the above line is too short, I need to pad it with spaces. I want to indent past the last letter of the above line.