# just some funky stuff to try if zle can work with
# kitty extended keyboard protocol (and it somewhat can!)
if (( ${+ENABLE_KITTY_STUFF} ))
then
    local esc="\x1b"
    local csi="${esc}["
    local KITTY_ESCAPE="${csi}27u"
    local KITTY_ENTER="${csi}13u"
    local KITTY_TAB="${csi}9u"
    local KITTY_BACKSPACE="${csi}127u"
    local KITTY_INSERT="${csi}2~"
    local KITTY_DELETE="${csi}3~"
    local KITTY_LEFT="${csi}1D"
    local KITTY_RIGHT="${csi}1C"
    local KITTY_UP="${csi}1A"
    local KITTY_DOWN="${csi}1B"
    local KITTY_PAGE_UP="${csi}5~"
    local KITTY_PAGE_DOWN="${csi}6~"
    local KITTY_HOME="${csi}1H or 7 ~"
    local KITTY_END="${csi}1F or 8 ~"
    local KITTY_CAPS_LOCK="${csi}57358u"
    local KITTY_SCROLL_LOCK="${csi}57359u"
    local KITTY_NUM_LOCK="${csi}57360u"
    local KITTY_PRINT_SCREEN="${csi}57361u"
    local KITTY_PAUSE="${csi}57362u"
    local KITTY_MENU="${csi}57363u"
    local KITTY_F1="${csi}1P or 11 ~"
    local KITTY_F2="${csi}1Q or 12 ~"
    local KITTY_F3="${csi}1R or 13 ~"
    local KITTY_F4="${csi}1S or 14 ~"
    local KITTY_F5="${csi}15~"
    local KITTY_F6="${csi}17~"
    local KITTY_F7="${csi}18~"
    local KITTY_F8="${csi}19~"
    local KITTY_F9="${csi}20~"
    local KITTY_F10="${csi}21~"
    local KITTY_F11="${csi}23~"
    local KITTY_F12="${csi}24~"
    local KITTY_F13="${csi}57376u"
    local KITTY_F14="${csi}57377u"
    local KITTY_F15="${csi}57378u"
    local KITTY_F16="${csi}57379u"
    local KITTY_F17="${csi}57380u"
    local KITTY_F18="${csi}57381u"
    local KITTY_F19="${csi}57382u"
    local KITTY_F20="${csi}57383u"
    local KITTY_F21="${csi}57384u"
    local KITTY_F22="${csi}57385u"
    local KITTY_F23="${csi}57386u"
    local KITTY_F24="${csi}57387u"
    local KITTY_F25="${csi}57388u"
    local KITTY_F26="${csi}57389u"
    local KITTY_F27="${csi}57390u"
    local KITTY_F28="${csi}57391u"
    local KITTY_F29="${csi}57392u"
    local KITTY_F30="${csi}57393u"
    local KITTY_F31="${csi}57394u"
    local KITTY_F32="${csi}57395u"
    local KITTY_F33="${csi}57396u"
    local KITTY_F34="${csi}57397u"
    local KITTY_F35="${csi}57398u"
    local KITTY_KP_0="${csi}57399u"
    local KITTY_KP_1="${csi}57400u"
    local KITTY_KP_2="${csi}57401u"
    local KITTY_KP_3="${csi}57402u"
    local KITTY_KP_4="${csi}57403u"
    local KITTY_KP_5="${csi}57404u"
    local KITTY_KP_6="${csi}57405u"
    local KITTY_KP_7="${csi}57406u"
    local KITTY_KP_8="${csi}57407u"
    local KITTY_KP_9="${csi}57408u"
    local KITTY_KP_DECIMAL="${csi}57409u"
    local KITTY_KP_DIVIDE="${csi}57410u"
    local KITTY_KP_MULTIPLY="${csi}57411u"
    local KITTY_KP_SUBTRACT="${csi}57412u"
    local KITTY_KP_ADD="${csi}57413u"
    local KITTY_KP_ENTER="${csi}57414u"
    local KITTY_KP_EQUAL="${csi}57415u"
    local KITTY_KP_SEPARATOR="${csi}57416u"
    local KITTY_KP_LEFT="${csi}57417u"
    local KITTY_KP_RIGHT="${csi}57418u"
    local KITTY_KP_UP="${csi}57419u"
    local KITTY_KP_DOWN="${csi}57420u"
    local KITTY_KP_PAGE_UP="${csi}57421u"
    local KITTY_KP_PAGE_DOWN="${csi}57422u"
    local KITTY_KP_HOME="${csi}57423u"
    local KITTY_KP_END="${csi}57424u"
    local KITTY_KP_INSERT="${csi}57425u"
    local KITTY_KP_DELETE="${csi}57426u"
    local KITTY_KP_BEGIN="${csi}1E or 57427 ~"
    local KITTY_MEDIA_PLAY="${csi}57428u"
    local KITTY_MEDIA_PAUSE="${csi}57429u"
    local KITTY_MEDIA_PLAY_PAUSE="${csi}57430u"
    local KITTY_MEDIA_REVERSE="${csi}57431u"
    local KITTY_MEDIA_STOP="${csi}57432u"
    local KITTY_MEDIA_FAST_FORWARD="${csi}57433u"
    local KITTY_MEDIA_REWIND="${csi}57434u"
    local KITTY_MEDIA_TRACK_NEXT="${csi}57435u"
    local KITTY_MEDIA_TRACK_PREVIOUS="${csi}57436u"
    local KITTY_MEDIA_RECORD="${csi}57437u"
    local KITTY_LOWER_VOLUME="${csi}57438u"
    local KITTY_RAISE_VOLUME="${csi}57439u"
    local KITTY_MUTE_VOLUME="${csi}57440u"
    local KITTY_LEFT_SHIFT="${csi}57441u"
    local KITTY_LEFT_CONTROL="${csi}57442u"
    local KITTY_LEFT_ALT="${csi}57443u"
    local KITTY_LEFT_SUPER="${csi}57444u"
    local KITTY_LEFT_HYPER="${csi}57445u"
    local KITTY_LEFT_META="${csi}57446u"
    local KITTY_RIGHT_SHIFT="${csi}57447u"
    local KITTY_RIGHT_CONTROL="${csi}57448u"
    local KITTY_RIGHT_ALT="${csi}57449u"
    local KITTY_RIGHT_SUPER="${csi}57450u"
    local KITTY_RIGHT_HYPER="${csi}57451u"
    local KITTY_RIGHT_META="${csi}57452u"
    local KITTY_ISO_LEVEL3_SHIFT="${csi}57453u"
    local KITTY_ISO_LEVEL5_SHIFT="${csi}57454u"

local -A KITTY_CTRL KITTY_ALT KITTY_CTRL_ALT
# a-z
for i in {97..126}
do
    KITTY_CTRL[${(#)i}]="${csi}$i;5u"
    KITTY_ALT[${(#)i}]="${csi}$i;3u"
    KITTY_CTRL_ALT[${(#)i}]="${csi}$i;8u"
done

    start-kitty-keys() {
        echo -n "${csi}>1u"
    }

    stop-kitty-keys() {
        echo -n "${csi}<u"
    }

    autoload -Uz add-zle-hook-widget \
        && add-zle-hook-widget -Uz line-init start-kitty-keys \
        && add-zle-hook-widget -Uz line-finish stop-kitty-keys

    # bindkey -M emacs "${KITTY_CTRL[@]}" set-mark-command
    bindkey -M emacs "${KITTY_CTRL[a]}" beginning-of-line
    bindkey -M emacs "${KITTY_HOME}" beginning-of-line
    bindkey -M emacs "${KITTY_CTRL[b]}" backward-char
    bindkey -M emacs "${KITTY_CTRL[d]}" delete-char-or-list
    bindkey -M emacs "${KITTY_CTRL[e]}" end-of-line
    bindkey -M emacs "${KITTY_END}" end-of-line
    bindkey -M emacs "${KITTY_CTRL[f]}" forward-char
    bindkey -M emacs "${KITTY_CTRL[g]}" send-break
    bindkey -M emacs "${KITTY_CTRL[h]}" backward-delete-char
    bindkey -M emacs "${KITTY_CTRL[i]}" expand-or-complete
    bindkey -M emacs "${KITTY_CTRL[j]}" accept-line
    bindkey -M emacs "${KITTY_CTRL[k]}" kill-line
    bindkey -M emacs "${KITTY_CTRL[l]}" clear-screen
    bindkey -M emacs "${KITTY_CTRL[m]}" accept-line
    bindkey -M emacs "${KITTY_CTRL[n]}" down-line-or-history
    bindkey -M emacs "${KITTY_PAGE_DOWN}" down-line-or-history
    bindkey -M emacs "${KITTY_CTRL[o]}" accept-line-and-down-history
    bindkey -M emacs "${KITTY_CTRL[p]}" up-line-or-history
    bindkey -M emacs "${KITTY_PAGE_UP}" up-line-or-history
    bindkey -M emacs "${KITTY_CTRL[q]}" push-line
    bindkey -M emacs "${KITTY_CTRL[r]}" fzf-history-widget
    bindkey -M emacs "${KITTY_CTRL[s]}" history-incremental-search-forward
    bindkey -M emacs "${KITTY_CTRL[t]}" fzf-file-widget
    bindkey -M emacs "${KITTY_CTRL[u]}" kill-whole-line
    bindkey -M emacs "${KITTY_CTRL[v]}" quoted-insert
    bindkey -M emacs "${KITTY_CTRL[w]}" backward-kill-word
    bindkey -M emacs "${KITTY_CTRL[x]}{KITTY_CTRL[b]}" vi-match-bracket
    bindkey -M emacs "${KITTY_CTRL[x]}{KITTY_CTRL[e]}" edit-command-line
    bindkey -M emacs "${KITTY_CTRL[x]}{KITTY_CTRL[f]}" vi-find-next-char
    bindkey -M emacs "${KITTY_CTRL[x]}{KITTY_CTRL[j]}" vi-join
    bindkey -M emacs "${KITTY_CTRL[x]}{KITTY_CTRL[k]}" kill-buffer
    bindkey -M emacs "${KITTY_CTRL[x]}{KITTY_CTRL[n]}" infer-next-history
    bindkey -M emacs "${KITTY_CTRL[x]}{KITTY_CTRL[o]}" overwrite-mode
    bindkey -M emacs "${KITTY_CTRL[x]}{KITTY_CTRL[r]}" _read_comp
    bindkey -M emacs "${KITTY_CTRL[x]}{KITTY_CTRL[u]}" undo
    bindkey -M emacs "${KITTY_CTRL[x]}{KITTY_CTRL[v]}" vi-cmd-mode
    bindkey -M emacs "${KITTY_CTRL[x]}{KITTY_CTRL[x]}" exchange-point-and-mark
    bindkey -M emacs "${KITTY_CTRL[x]}*" expand-word
    bindkey -M emacs "${KITTY_CTRL[x]}=" what-cursor-position
    bindkey -M emacs "${KITTY_CTRL[x]}?" _complete_debug
    bindkey -M emacs "${KITTY_CTRL[x]}C" _correct_filename
    bindkey -M emacs "${KITTY_CTRL[x]}G" list-expand
    bindkey -M emacs "${KITTY_CTRL[x]}a" _expand_alias
    bindkey -M emacs "${KITTY_CTRL[x]}c" _correct_word
    bindkey -M emacs "${KITTY_CTRL[x]}d" _list_expansions
    bindkey -M emacs "${KITTY_CTRL[x]}e" _expand_word
    bindkey -M emacs "${KITTY_CTRL[x]}g" list-expand
    bindkey -M emacs "${KITTY_CTRL[x]}h" _complete_help
    bindkey -M emacs "${KITTY_CTRL[x]}m" _most_recent_file
    bindkey -M emacs "${KITTY_CTRL[x]}n" _next_tags
    bindkey -M emacs "${KITTY_CTRL[x]}r" history-incremental-search-backward
    bindkey -M emacs "${KITTY_CTRL[x]}s" history-incremental-search-forward
    bindkey -M emacs "${KITTY_CTRL[x]}t" _complete_tag
    bindkey -M emacs "${KITTY_CTRL[x]}u" undo
    bindkey -M emacs "${KITTY_CTRL[x]}~" _bash_list-choices
    bindkey -M emacs "${KITTY_CTRL[y]}" yank
    bindkey -M emacs "^[^D" list-choices
    bindkey -M emacs "^[^G" send-break
    bindkey -M emacs "^[^H" backward-kill-word
    bindkey -M emacs "^[^I" self-insert-unmeta
    bindkey -M emacs "^[^J" self-insert-unmeta
    bindkey -M emacs "^[^L" clear-screen
    bindkey -M emacs "^[^M" self-insert-unmeta
    bindkey -M emacs "^[^[" sudo-command-line
    bindkey -M emacs "^[^[[C" forward-word
    bindkey -M emacs "^[^[[D" backward-word
    bindkey -M emacs "^[^_" copy-prev-word
    bindkey -M emacs "^[ " expand-history
    bindkey -M emacs "^[!" expand-history
    bindkey -M emacs "^[\"" quote-region
    bindkey -M emacs "^[\$" spell-word
    bindkey -M emacs "^['" quote-line
    bindkey -M emacs "^[," _history-complete-newer
    bindkey -M emacs "^[-" neg-argument
    bindkey -M emacs "^[." insert-last-word
    bindkey -M emacs "^[/" _history-complete-older
    bindkey -M emacs "^[0" digit-argument
    bindkey -M emacs "^[1" digit-argument
    bindkey -M emacs "^[2" digit-argument
    bindkey -M emacs "^[3" digit-argument
    bindkey -M emacs "^[4" digit-argument
    bindkey -M emacs "^[5" digit-argument
    bindkey -M emacs "^[6" digit-argument
    bindkey -M emacs "^[7" digit-argument
    bindkey -M emacs "^[8" digit-argument
    bindkey -M emacs "^[9" digit-argument
    bindkey -M emacs "^[<" beginning-of-buffer-or-history
    bindkey -M emacs "^[>" end-of-buffer-or-history
    bindkey -M emacs "^[?" which-command
    bindkey -M emacs "^[A" accept-and-hold
    bindkey -M emacs "^[B" backward-word
    bindkey -M emacs "^[C" capitalize-word
    bindkey -M emacs "^[D" kill-word
    bindkey -M emacs "^[F" forward-word
    bindkey -M emacs "^[G" get-line
    bindkey -M emacs "^[H" run-help
    bindkey -M emacs "^[L" down-case-word
    bindkey -M emacs "^[N" history-search-forward
    bindkey -M emacs "^[OA" up-line-or-beginning-search
    bindkey -M emacs "^[OB" down-line-or-beginning-search
    bindkey -M emacs "^[OC" forward-char
    bindkey -M emacs "^[OD" backward-char
    bindkey -M emacs "^[OF" end-of-line
    bindkey -M emacs "^[OH" beginning-of-line
    bindkey -M emacs "^[Oc" forward-word
    bindkey -M emacs "^[Od" backward-word
    bindkey -M emacs "^[P" history-search-backward
    bindkey -M emacs "^[Q" push-line
    bindkey -M emacs "^[S" spell-word
    bindkey -M emacs "^[T" transpose-words
    bindkey -M emacs "^[U" up-case-word
    bindkey -M emacs "^[W" copy-region-as-kill
    bindkey -M emacs "^[[1;5C" forward-word
    bindkey -M emacs "^[[1;5D" backward-word
    bindkey -M emacs "^[[200~" bracketed-paste
    bindkey -M emacs "^[[2~" overwrite-mode
    bindkey -M emacs "^[[3~" delete-char
    bindkey -M emacs "^[[5C" forward-word
    bindkey -M emacs "^[[5D" backward-word
    bindkey -M emacs "^[[5~" up-line-or-history
    bindkey -M emacs "^[[6~" down-line-or-history
    bindkey -M emacs "^[[A" up-line-or-history
    bindkey -M emacs "^[[B" down-line-or-history
    bindkey -M emacs "^[[C" forward-char
    bindkey -M emacs "^[[D" backward-char
    bindkey -M emacs "^[[Z" reverse-menu-complete
    bindkey -M emacs "^[_" insert-last-word
    bindkey -M emacs "^[a" accept-and-hold
    bindkey -M emacs "^[b" backward-word
    bindkey -M emacs "^[c" fzf-cd-widget
    bindkey -M emacs "^[d" kill-word
    bindkey -M emacs "^[f" forward-word
    bindkey -M emacs "^[g" get-line
    bindkey -M emacs "^[h" run-help
    bindkey -M emacs "^[l" down-case-word
    bindkey -M emacs "^[n" history-search-forward
    bindkey -M emacs "^[p" history-search-backward
    bindkey -M emacs "^[q" push-line
    bindkey -M emacs "^[s" spell-word
    bindkey -M emacs "^[t" transpose-words
    bindkey -M emacs "^[u" up-case-word
    bindkey -M emacs "^[w" copy-region-as-kill
    bindkey -M emacs "^[x" execute-named-cmd
    bindkey -M emacs "^[y" yank-pop
    bindkey -M emacs "^[z" execute-last-named-cmd
    bindkey -M emacs "^[|" vi-goto-column
    bindkey -M emacs "^[~" _bash_complete-word
    bindkey -M emacs "^[^?" backward-kill-word
    bindkey -M emacs "^_" undo
    bindkey -M emacs " " magic-space
    bindkey -M emacs "!"-"~" self-insert
    bindkey -M emacs "^?" backward-delete-char
    bindkey -M emacs "\M-^@"-"\M-^?" self-insert
fi
