Use a _ksi_ prefix for shell integration

Sadly function in zsh are global so prefix all function and global var
names with _ksi_ as poor mans namespacing.
This commit is contained in:
Kovid Goyal 2021-07-15 08:35:30 +05:30
parent 1bb87c71aa
commit 2263cd1355
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -1,24 +1,24 @@
() { () {
if [[ ! -o interactive ]]; then return; fi if [[ ! -o interactive ]]; then return; fi
if [[ -z "$kitty_shell_integration" ]]; then return; fi if [[ -z "$kitty_shell_integration" ]]; then return; fi
typeset -g -A kitty_prompt=([state]='first-run' [cursor]='y' [title]='y' [mark]='y' [complete]='y') typeset -g -A _ksi_prompt=([state]='first-run' [cursor]='y' [title]='y' [mark]='y' [complete]='y')
for i in ${=kitty_shell_integration}; do for i in ${=kitty_shell_integration}; do
if [[ "$i" == "no-cursor" ]]; then kitty_prompt[cursor]='n'; fi if [[ "$i" == "no-cursor" ]]; then _ksi_prompt[cursor]='n'; fi
if [[ "$i" == "no-title" ]]; then kitty_prompt[title]='n'; fi if [[ "$i" == "no-title" ]]; then _ksi_prompt[title]='n'; fi
if [[ "$i" == "no-prompt-mark" ]]; then kitty_prompt[mark]='n'; fi if [[ "$i" == "no-prompt-mark" ]]; then _ksi_prompt[mark]='n'; fi
if [[ "$i" == "no-complete" ]]; then kitty_prompt[complete]='n'; fi if [[ "$i" == "no-complete" ]]; then _ksi_prompt[complete]='n'; fi
done done
unset kitty_shell_integration unset kitty_shell_integration
function debug() { function _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) local b=$(printf "%s\n" "$1" | base64 | tr -d \\n)
printf "\eP@kitty-print|%s\e\\" "$b" printf "\eP@kitty-print|%s\e\\" "$b"
} }
function change-cursor-shape () { function _ksi_change_cursor_shape () {
# change cursor shape depending on mode # change cursor shape depending on mode
if [[ "$kitty_prompt[cursor]" == "y" ]]; then if [[ "$_ksi_prompt[cursor]" == "y" ]]; then
case $KEYMAP in case $KEYMAP in
vicmd | visual) vicmd | visual)
# the command mode for vi # the command mode for vi
@ -31,106 +31,106 @@
fi fi
} }
function kitty_zle_keymap_select() { function _ksi_zle_keymap_select() {
change-cursor-shape _ksi_change_cursor_shape
} }
function kitty_zle_keymap_select_with_original() { zle kitty-zle-keymap-select-original; kitty_zle_keymap_select } function _ksi_zle_keymap_select_with_original() { zle kitty-zle-keymap-select-original; _ksi_zle_keymap_select }
zle -A zle-keymap-select kitty-zle-keymap-select-original 2>/dev/null zle -A zle-keymap-select kitty-zle-keymap-select-original 2>/dev/null
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
zle -N zle-keymap-select kitty_zle_keymap_select_with_original zle -N zle-keymap-select _ksi_zle_keymap_select_with_original
else else
zle -N zle-keymap-select kitty_zle_keymap_select zle -N zle-keymap-select _ksi_zle_keymap_select
fi fi
function osc() { function _ksi_osc() {
printf "\e]%s\a" "$1" printf "\e]%s\a" "$1"
} }
function mark() { function _ksi_mark() {
# tell kitty to mark the current cursor position using OSC 133 # tell kitty to mark the current cursor position using OSC 133
if [[ "$kitty_prompt[mark]" == "y" ]]; then osc "133;$1"; fi if [[ "$_ksi_prompt[mark]" == "y" ]]; then _ksi_osc "133;$1"; fi
} }
kitty_prompt[start_mark]="%{$(mark A)%}" _ksi_prompt[start_mark]="%{$(_ksi_mark A)%}"
function set_title() { function _ksi_set_title() {
if [[ "$kitty_prompt[title]" == "y" ]]; then osc "2;$1"; fi if [[ "$_ksi_prompt[title]" == "y" ]]; then _ksi_osc "2;$1"; fi
} }
function install_kitty_completion() { function _ksi_install_completion() {
if [[ "$kitty_prompt[complete]" == "y" ]]; then if [[ "$_ksi_prompt[complete]" == "y" ]]; then
# compdef is only defined if compinit has been called # compdef is only defined if compinit has been called
if whence compdef > /dev/null; then if whence compdef > /dev/null; then
compdef _kitty kitty compdef _ksi_complete kitty
fi fi
fi fi
} }
function kitty_precmd() { function _ksi_precmd() {
local cmd_status=$? local cmd_status=$?
if [[ "$kitty_prompt[state]" == "first-run" ]]; then if [[ "$_ksi_prompt[state]" == "first-run" ]]; then
install_kitty_completion _ksi_install_completion
fi fi
# Set kitty window title to the cwd # Set kitty window title to the cwd
set_title "${PWD/$HOME/~}" _ksi_set_title "${PWD/$HOME/~}"
# Prompt marking # Prompt marking
if [[ "$kitty_prompt[mark]" == "y" ]]; then if [[ "$_ksi_prompt[mark]" == "y" ]]; then
if [[ "$kitty_prompt[state]" == "preexec" ]]; then if [[ "$_ksi_prompt[state]" == "preexec" ]]; then
mark "D;$cmd_status" _ksi_mark "D;$cmd_status"
else else
if [[ "$kitty_prompt[state]" != "first-run" ]]; then mark "D"; fi if [[ "$_ksi_prompt[state]" != "first-run" ]]; then _ksi_mark "D"; fi
fi fi
# we must use PS1 to set the prompt start mark as precmd functions are # we must use PS1 to set the prompt start mark as precmd functions are
# not called when the prompt is redrawn after a window resize or when a background # not called when the prompt is redrawn after a window resize or when a background
# job finishes # job finishes
if [[ "$PS1" != *"$kitty_prompt[start_mark]"* ]]; then PS1="$kitty_prompt[start_mark]$PS1" fi if [[ "$PS1" != *"$_ksi_prompt[start_mark]"* ]]; then PS1="$_ksi_prompt[start_mark]$PS1" fi
fi fi
kitty_prompt[state]="precmd" _ksi_prompt[state]="precmd"
} }
function kitty_zle_line_init() { function _ksi_zle_line_init() {
if [[ "$kitty_prompt[mark]" == "y" ]]; then mark "B"; fi if [[ "$_ksi_prompt[mark]" == "y" ]]; then _ksi_mark "B"; fi
change-cursor-shape; _ksi_change_cursor_shape
kitty_prompt[state]="line-init" _ksi_prompt[state]="line-init"
} }
function kitty_zle_line_init_with_orginal() { zle kitty-zle-line-init-original; kitty_zle_line_init } function _ksi_zle_line_init_with_orginal() { zle kitty-zle-line-init-original; _ksi_zle_line_init }
zle -A zle-line-init kitty-zle-line-init-original 2>/dev/null zle -A zle-line-init kitty-zle-line-init-original 2>/dev/null
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
zle -N zle-line-init kitty_zle_line_init_with_orginal zle -N zle-line-init _ksi_zle_line_init_with_orginal
else else
zle -N zle-line-init kitty_zle_line_init zle -N zle-line-init _ksi_zle_line_init
fi fi
function kitty_zle_line_finish() { function _ksi_zle_line_finish() {
change-cursor-shape; _ksi_change_cursor_shape
kitty_prompt[state]="line-finish" _ksi_prompt[state]="line-finish"
} }
function kitty_zle_line_finish_with_orginal() { zle kitty-zle-line-finish-original; kitty_zle_line_finish } function _ksi_zle_line_finish_with_orginal() { zle kitty-zle-line-finish-original; _ksi_zle_line_finish }
zle -A zle-line-finish kitty-zle-line-finish-original 2>/dev/null zle -A zle-line-finish kitty-zle-line-finish-original 2>/dev/null
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
zle -N zle-line-finish kitty_zle_line_finish_with_orginal zle -N zle-line-finish _ksi_zle_line_finish_with_orginal
else else
zle -N zle-line-finish kitty_zle_line_finish zle -N zle-line-finish _ksi_zle_line_finish
fi fi
function kitty_preexec() { function _ksi_preexec() {
if [[ "$kitty_prompt[mark]" == "y" ]]; then if [[ "$_ksi_prompt[mark]" == "y" ]]; then
mark "C"; _ksi_mark "C";
# remove the prompt mark sequence while the command is executing as it could read/modify the value of PS1 # remove the prompt mark sequence while the command is executing as it could read/modify the value of PS1
PS1="${PS1//$kitty_prompt[start_mark]/}" PS1="${PS1//$_ksi_prompt[start_mark]/}"
fi fi
# Set kitty window title to the currently executing command # Set kitty window title to the currently executing command
set_title "$1" _ksi_set_title "$1"
kitty_prompt[state]="preexec" _ksi_prompt[state]="preexec"
} }
typeset -a -g precmd_functions typeset -a -g precmd_functions
precmd_functions=($precmd_functions kitty_precmd) precmd_functions=($precmd_functions _ksi_precmd)
typeset -a -g preexec_functions typeset -a -g preexec_functions
preexec_functions=($preexec_functions kitty_preexec) preexec_functions=($preexec_functions _ksi_preexec)
# Completion for kitty # Completion for kitty
_kitty() { _ksi_complete() {
local src local src
# Send all words up to the word the cursor is currently on # Send all words up to the word the cursor is currently on
src=$(printf "%s\n" "${(@)words[1,$CURRENT]}" | kitty +complete zsh) src=$(printf "%s\n" "${(@)words[1,$CURRENT]}" | kitty +complete zsh)