Unfunction kitty-integration in zsh

This commit is contained in:
Roman Perepelitsa 2021-12-23 18:13:49 +01:00
parent 59505d17d5
commit 9bba38bd11
3 changed files with 210 additions and 217 deletions

View File

@ -33,6 +33,7 @@ fi
if [[ -r "$_ksi_file" ]]; then if [[ -r "$_ksi_file" ]]; then
'builtin' 'autoload' '-Uz' '--' "$_ksi_file" 'builtin' 'autoload' '-Uz' '--' "$_ksi_file"
"${_ksi_file:t}" "${_ksi_file:t}"
'builtin' 'unfunction' '--' "${_ksi_file:t}"
fi fi
fi fi
'builtin' 'unset' '_ksi_file' 'builtin' 'unset' '_ksi_file'

View File

@ -12,6 +12,7 @@
# if [[ -n $KITTY_INSTALLATION_DIR ]]; then # if [[ -n $KITTY_INSTALLATION_DIR ]]; then
# autoload -Uz -- "$KITTY_INSTALLATION_DIR"/shell-integration/zsh/kitty-integration # autoload -Uz -- "$KITTY_INSTALLATION_DIR"/shell-integration/zsh/kitty-integration
# kitty-integration # kitty-integration
# unfunction kitty-integration
# fi # fi
# #
# Implementation note: We can assume that alias expansion is disabled in this # Implementation note: We can assume that alias expansion is disabled in this
@ -23,15 +24,12 @@ builtin emulate -L zsh -o no_warn_create_global
[[ -o interactive ]] || builtin return 0 # non-interactive shell [[ -o interactive ]] || builtin return 0 # non-interactive shell
[[ -n $KITTY_SHELL_INTEGRATION ]] || builtin return 0 # integration disabled [[ -n $KITTY_SHELL_INTEGRATION ]] || builtin return 0 # integration disabled
(( ! _ksi_state )) || builtin return 0 # already initialized (( ! $+_ksi_state )) || builtin return 0 # already initialized
if (( ! $+_ksi_state )); then # 0: no OSC 133 [AC] marks have been written yet.
# 0: not initialized; deferred initialization can start now. # 1: the last written OSC 133 C has not been closed with D yet.
# 1: not initialized; waiting for deferred initialization. # 2: none of the above.
# 2: initialized; no OSC 133 [AC] marks have been written yet. builtin typeset -gi _ksi_state
# 3: initialized; the last written OSC 133 C has not been closed with D yet.
# 4: initialized; none of the above.
builtin typeset -gi _ksi_state=1
# Asks Kitty to print $@ to its stdout. This is for debugging. # Asks Kitty to print $@ to its stdout. This is for debugging.
_ksi_debug_print() { _ksi_debug_print() {
@ -40,11 +38,6 @@ if (( ! $+_ksi_state )); then
builtin printf '\eP@kitty-print|%s\e\\' "${data//$'\n'}" builtin printf '\eP@kitty-print|%s\e\\' "${data//$'\n'}"
} }
_ksi_deferred_init() {
(( _ksi_state = 0, 1 )) # `, 1` in case err_return is set
kitty-integration
}
# We defer initialization until precmd for several reasons: # We defer initialization until precmd for several reasons:
# #
# - Oh My Zsh and many other configs remove zle-line-init and # - Oh My Zsh and many other configs remove zle-line-init and
@ -55,13 +48,9 @@ if (( ! $+_ksi_state )); then
# intialization to avoid redundant marks from our code. # intialization to avoid redundant marks from our code.
builtin typeset -ag precmd_functions builtin typeset -ag precmd_functions
precmd_functions+=(_ksi_deferred_init) precmd_functions+=(_ksi_deferred_init)
builtin return
fi
# The rest of kitty-integration performs deferred initialization. We are being _ksi_deferred_init() {
# run from _ksi_deferred_init here. builtin emulate -L zsh -o no_warn_create_global
(( _ksi_state = 2 ))
# Recognized options: no-cursor, no-title, no-prompt-mark, no-complete. # Recognized options: no-cursor, no-title, no-prompt-mark, no-complete.
builtin local -a opt builtin local -a opt
@ -129,12 +118,12 @@ if (( ! opt[(Ie)no-prompt-mark] )); then
# with LF and prompst_sp is set (it is by default). In this case # with LF and prompst_sp is set (it is by default). In this case
# we'll incorrectly state that '%' from prompt_sp is a part of the # we'll incorrectly state that '%' from prompt_sp is a part of the
# command's output. # command's output.
if (( _ksi_state == 3 )); then if (( _ksi_state == 1 )); then
# The last written OSC 133 C has not been closed with D yet. # The last written OSC 133 C has not been closed with D yet.
# Close it and supply status. # Close it and supply status.
builtin printf '\e]133;D;%s\a' $cmd_status builtin printf '\e]133;D;%s\a' $cmd_status
(( _ksi_state = 4 )) (( _ksi_state = 2 ))
elif (( _ksi_state == 4 )); then elif (( _ksi_state == 2 )); then
# There might be an unclosed OSC 133 C. Close that. # There might be an unclosed OSC 133 C. Close that.
builtin print -n '\e]133;D\a' builtin print -n '\e]133;D\a'
fi fi
@ -159,7 +148,7 @@ if (( ! opt[(Ie)no-prompt-mark] )); then
# - False negative (with prompt_subst): PS1='$mark1' # - False negative (with prompt_subst): PS1='$mark1'
[[ $PS1 == *$mark1* ]] || PS1=${mark1}${PS1} [[ $PS1 == *$mark1* ]] || PS1=${mark1}${PS1}
[[ $PS2 == *$mark2* ]] || PS2=${mark2}${PS2} [[ $PS2 == *$mark2* ]] || PS2=${mark2}${PS2}
(( _ksi_state = 4 )) (( _ksi_state = 2 ))
else else
# If our precmd hook is not the last, we cannot rely on prompt # If our precmd hook is not the last, we cannot rely on prompt
# changes to stick, so we don't even try. At least we can move # changes to stick, so we don't even try. At least we can move
@ -175,7 +164,7 @@ if (( ! opt[(Ie)no-prompt-mark] )); then
# it. If it doesn't, there is nothing we can do. # it. If it doesn't, there is nothing we can do.
if ! builtin zle; then if ! builtin zle; then
builtin print -rn -- $mark1[3,-3] builtin print -rn -- $mark1[3,-3]
(( _ksi_state = 4 )) (( _ksi_state = 2 ))
fi fi
fi fi
elif ! builtin zle; then elif ! builtin zle; then
@ -183,7 +172,7 @@ if (( ! opt[(Ie)no-prompt-mark] )); then
# mark, except when we are invoked from zle. In the latter case we # mark, except when we are invoked from zle. In the latter case we
# cannot do anything. # cannot do anything.
builtin print -rn -- $mark1[3,-3] builtin print -rn -- $mark1[3,-3]
(( _ksi_state = 4 )) (( _ksi_state = 2 ))
fi fi
} }
@ -205,7 +194,7 @@ if (( ! opt[(Ie)no-prompt-mark] )); then
# belonging to the command (as if the user typed it into zle) rather # belonging to the command (as if the user typed it into zle) rather
# than command output. # than command output.
builtin print -n '\e]133;C\a' builtin print -n '\e]133;C\a'
(( _ksi_state = 3 )) (( _ksi_state = 1 ))
} }
functions[_ksi_zle_line_init]+=' functions[_ksi_zle_line_init]+='
@ -261,6 +250,8 @@ else
precmd_functions=(${precmd_functions:#_ksi_deferred_init}) precmd_functions=(${precmd_functions:#_ksi_deferred_init})
fi fi
# Unfunction what we don't need to save memory. # Unfunction _ksi_deferred_init to save memory. Don't unfunction
builtin unfunction _ksi_deferred_init kitty-integration # kitty-integration though because decent public functions aren't supposed to
builtin autoload -Uz -- $self_dir/kitty-integration # to unfunction themselves when invoked.
builtin unfunction _ksi_deferred_init
}

View File

@ -16,5 +16,6 @@
if [[ -r "$_ksi_file" ]]; then if [[ -r "$_ksi_file" ]]; then
'builtin' 'autoload' '-Uz' '--' "$_ksi_file" 'builtin' 'autoload' '-Uz' '--' "$_ksi_file"
"${_ksi_file:t}" "${_ksi_file:t}"
'builtin' 'unfunction' '--' "${_ksi_file:t}"
fi fi
'builtin' 'unset' '_ksi_file' 'builtin' 'unset' '_ksi_file'