Dont break if user aliases or redefines various builtins we use

This commit is contained in:
Kovid Goyal
2022-01-14 22:42:29 +05:30
parent 4c5673dc4d
commit 914f4e9ec9

View File

@@ -2,27 +2,28 @@
# this is defined outside _ksi_main to make it global without using declare -g # this is defined outside _ksi_main to make it global without using declare -g
# which is not available on older bash # which is not available on older bash
declare -A _ksi_prompt=( [cursor]='y' [title]='y' [mark]='y' [complete]='y' [ps0]='' [ps1]='' [ps2]='' ) builtin declare -A _ksi_prompt
_ksi_prompt=( [cursor]='y' [title]='y' [mark]='y' [complete]='y' [ps0]='' [ps1]='' [ps2]='' )
_ksi_main() { _ksi_main() {
if [[ $- != *i* ]] ; then return; fi # check in interactive mode if [[ $- != *i* ]] ; then builtin return; fi # check in interactive mode
if [[ -z "$KITTY_SHELL_INTEGRATION" ]]; then return; fi if [[ -z "$KITTY_SHELL_INTEGRATION" ]]; then builtin return; fi
set -f builtin set -f
for i in ${KITTY_SHELL_INTEGRATION[@]}; do for i in ${KITTY_SHELL_INTEGRATION[@]}; do
set +f builtin set +f
if [[ "$i" == "no-cursor" ]]; then _ksi_prompt[cursor]='n'; fi if [[ "$i" == "no-cursor" ]]; then _ksi_prompt[cursor]='n'; fi
if [[ "$i" == "no-title" ]]; then _ksi_prompt[title]='n'; fi if [[ "$i" == "no-title" ]]; then _ksi_prompt[title]='n'; fi
if [[ "$i" == "no-prompt-mark" ]]; then _ksi_prompt[mark]='n'; fi if [[ "$i" == "no-prompt-mark" ]]; then _ksi_prompt[mark]='n'; fi
if [[ "$i" == "no-complete" ]]; then _ksi_prompt[complete]='n'; fi if [[ "$i" == "no-complete" ]]; then _ksi_prompt[complete]='n'; fi
done done
set +f builtin set +f
unset KITTY_SHELL_INTEGRATION builtin unset KITTY_SHELL_INTEGRATION
_ksi_debug_print() { _ksi_debug_print() {
# print a line to STDOUT of parent kitty process # print a line to STDOUT of parent kitty process
local b=$(printf "%s\n" "$1" | base64 | tr -d \\n) builtin local b=$(builtin printf "%s\n" "$1" | base64 | tr -d \\n)
printf "\eP@kitty-print|%s\e\\" "$b" builtin printf "\eP@kitty-print|%s\e\\" "$b"
# " # "
} }
@@ -34,7 +35,7 @@ _ksi_main() {
_ksi_set_mark end _ksi_set_mark end
_ksi_set_mark start_secondary _ksi_set_mark start_secondary
_ksi_set_mark end_secondary _ksi_set_mark end_secondary
unset -f _ksi_set_mark builtin unset -f _ksi_set_mark
_ksi_prompt[secondary_prompt]="\n${_ksi_prompt[start_secondary_mark]}\[\e]133;A;k=s\a\]${_ksi_prompt[end_secondary_mark]}" _ksi_prompt[secondary_prompt]="\n${_ksi_prompt[start_secondary_mark]}\[\e]133;A;k=s\a\]${_ksi_prompt[end_secondary_mark]}"
_ksi_prompt_command() { _ksi_prompt_command() {
@@ -82,16 +83,16 @@ _ksi_main() {
if [[ "${_ksi_prompt[complete]}" == "y" ]]; then if [[ "${_ksi_prompt[complete]}" == "y" ]]; then
_ksi_completions() { _ksi_completions() {
local src builtin local src
local limit builtin local limit
# Send all words up to the word the cursor is currently on # Send all words up to the word the cursor is currently on
let limit=1+$COMP_CWORD builtin let limit=1+$COMP_CWORD
src=$(printf "%s\n" "${COMP_WORDS[@]: 0:$limit}" | kitty +complete bash) src=$(builtin printf "%s\n" "${COMP_WORDS[@]: 0:$limit}" | kitty +complete bash)
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
eval ${src} builtin eval ${src}
fi fi
} }
complete -o nospace -F _ksi_completions kitty builtin complete -o nospace -F _ksi_completions kitty
fi fi
# wrap our prompt additions in markers we can use to remove them using # wrap our prompt additions in markers we can use to remove them using
@@ -105,10 +106,10 @@ _ksi_main() {
if [[ -n "${_ksi_prompt[ps2]}" ]]; then if [[ -n "${_ksi_prompt[ps2]}" ]]; then
_ksi_prompt[ps2]="${_ksi_prompt[start_mark]}${_ksi_prompt[ps2]}${_ksi_prompt[end_mark]}" _ksi_prompt[ps2]="${_ksi_prompt[start_mark]}${_ksi_prompt[ps2]}${_ksi_prompt[end_mark]}"
fi fi
unset _ksi_prompt[start_mark] builtin unset _ksi_prompt[start_mark]
unset _ksi_prompt[end_mark] builtin unset _ksi_prompt[end_mark]
unset _ksi_prompt[start_secondary_mark] builtin unset _ksi_prompt[start_secondary_mark]
unset _ksi_prompt[end_secondary_mark] builtin unset _ksi_prompt[end_secondary_mark]
# install our prompt command, using an array if it is unset or already an array, # install our prompt command, using an array if it is unset or already an array,
# otherwise append a string # otherwise append a string
@@ -121,6 +122,6 @@ _ksi_main() {
fi fi
} }
_ksi_main _ksi_main
unset -f _ksi_main builtin unset -f _ksi_main
# freeze _ksi_prompt to prevent it from being changed # freeze _ksi_prompt to prevent it from being changed
declare -r _ksi_prompt builtin declare -r _ksi_prompt