From efaf9faa38f49f95a891260ae3a306fc8a251986 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 17 Nov 2022 20:48:20 +0530 Subject: [PATCH] Implement edit-in-kitty using kitty-tool Fixes #5546 Fixes #5630 --- docs/changelog.rst | 2 + gen-go-code.py | 4 +- kitty_tests/completion.py | 2 - shell-integration/bash/kitty.bash | 77 +------------ .../vendor_completions.d/edit-in-kitty.fish | 7 -- .../kitty-shell-integration.fish | 101 +----------------- shell-integration/zsh/kitty-integration | 73 +------------ 7 files changed, 10 insertions(+), 256 deletions(-) delete mode 100644 shell-integration/fish/vendor_completions.d/edit-in-kitty.fish diff --git a/docs/changelog.rst b/docs/changelog.rst index 5a9295d21..c692f4d4b 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -47,6 +47,8 @@ Detailed list of changes - Allow using the cwd of the original process for :option:`launch --cwd` (:iss:`5672`) +- Implement :ref:`edit-in-kitty ` using the new ``kitty-tool`` static executable (:iss:`5546`, :iss:`5630`) + 0.26.5 [2022-11-07] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/gen-go-code.py b/gen-go-code.py index b003e9508..8da084006 100755 --- a/gen-go-code.py +++ b/gen-go-code.py @@ -144,9 +144,7 @@ def generate_completions_for_kitty() -> None: # clone-in-kitty, edit-in-kitty print('cik := root.AddSubCommand(&cli.Command{Name:"clone-in-kitty"})') - print('eik := root.AddSubCommand(&cli.Command{Name:"edit-in-kitty"})') - completion_for_launch_wrappers('cik', 'eik') - print(''.join(CompletionSpec.from_string('type:file mime:text/* group:"Text files"').as_go_code('eik.ArgCompleter', ' = '))) + completion_for_launch_wrappers('cik') print('}') print('func init() {') diff --git a/kitty_tests/completion.py b/kitty_tests/completion.py index eacf4e896..b8606e44e 100644 --- a/kitty_tests/completion.py +++ b/kitty_tests/completion.py @@ -163,8 +163,6 @@ def make_file(path, mode=None): add('kitty +kitten hyperlinked_grep ', is_delegate(2, 'rg')) add('clone-in-kitty --ty', has_words('--type')) - make_file('editable.txt') - add('edit-in-kitty ', has_words('editable.txt')) add('kitty bash ', is_delegate(1, 'bash')) add('kitty -1 bash ', is_delegate(2, 'bash')) diff --git a/shell-integration/bash/kitty.bash b/shell-integration/bash/kitty.bash index 41acc01a9..b197087ee 100644 --- a/shell-integration/bash/kitty.bash +++ b/shell-integration/bash/kitty.bash @@ -209,6 +209,7 @@ _ksi_main() { _ksi_prompt[ps0]+="\[\e]133;C\a\]" fi + alias edit-in-kitty="kitty-tool edit-in-kitty" if [[ "${_ksi_prompt[complete]}" == "y" ]]; then _ksi_completions() { builtin local src @@ -346,82 +347,6 @@ clone-in-kitty() { _ksi_transmit_data "$data" "clone" "save_history" } -edit-in-kitty() { - builtin local data="" - builtin local ed_filename="" - builtin local usage="Usage: edit-in-kitty [OPTIONS] FILE" - data="cwd=$(builtin printf "%s" "$PWD" | builtin command base64)" - while :; do - case "$1" in - "") break;; - -h|--help) - builtin printf "%s\n\n%s\n\n%s\n" "$usage" "Edit the specified file in a kitty overlay window. Works over SSH as well." "For usage instructions see: https://sw.kovidgoyal.net/kitty/shell-integration/#edit-file" - return - ;; - *) data="$data,a=$(builtin printf "%s" "$1" | builtin command base64)"; ed_filename="$1";; - esac - shift - done - [ -z "$ed_filename" ] && { - builtin echo "$usage" > /dev/stderr - return 1 - } - [ -r "$ed_filename" -a -w "$ed_filename" ] || { - builtin echo "$ed_filename is not readable and writable" > /dev/stderr - return 1 - } - [ ! -f "$ed_filename" ] && { - builtin echo "$ed_filename is not a file" > /dev/stderr - return 1 - } - builtin local stat_result="" - stat_result=$(builtin command stat -L --format '%d:%i:%s' "$ed_filename" 2> /dev/null) - [ $? != 0 ] && stat_result=$(builtin command stat -L -f '%d:%i:%z' "$ed_filename" 2> /dev/null) - [ -z "$stat_result" ] && { builtin echo "Failed to stat the file: $ed_filename" > /dev/stderr; return 1; } - data="$data,file_inode=$stat_result" - builtin local file_size=$(builtin echo "$stat_result" | builtin command cut -d: -f3) - [ "$file_size" -gt $((8 * 1024 * 1024)) ] && { builtin echo "File is too large for performant editing"; return 1; } - data="$data,file_data=$(builtin command base64 < "$ed_filename")" - _ksi_transmit_data "$data" "edit" - data="" - builtin echo "Waiting for editing to be completed..." - _ksi_wait_for_complete() { - builtin local started="n" - builtin local line="" - builtin local old_tty_settings=$(builtin command stty -g) - builtin command stty "-echo" - builtin trap -- "builtin command stty '$old_tty_settings'" RETURN - builtin trap -- "builtin command stty '$old_tty_settings'; _ksi_transmit_data 'abort_signaled=interrupt' 'edit'; builtin exit 1;" SIGINT SIGTERM - while :; do - started="n" - while IFS= builtin read -r line; do - if [ "$started" = "y" ]; then - [ "$line" = "UPDATE" ] && break - [ "$line" = "DONE" ] && { started="done"; break; } - builtin printf "%s\n" "$line" > /dev/stderr - return 1 - else - [ "$line" = "KITTY_DATA_START" ] && started="y" - fi - done - [ "$started" = "n" ] && continue - data="" - while IFS= builtin read -r line; do - [ "$line" = "KITTY_DATA_END" ] && break - data="$data$line" - done - [ -n "$data" -a "$started" != "done" ] && { - builtin echo "Updating $ed_filename..." - builtin printf "%s" "$data" | builtin command base64 -d > "$ed_filename" - } - [ "$started" = "done" ] && break - done - } - $(_ksi_wait_for_complete > /dev/tty) - builtin local rc=$? - builtin unset -f _ksi_wait_for_complete - return $rc -} ;; esac diff --git a/shell-integration/fish/vendor_completions.d/edit-in-kitty.fish b/shell-integration/fish/vendor_completions.d/edit-in-kitty.fish deleted file mode 100644 index e4f0e4189..000000000 --- a/shell-integration/fish/vendor_completions.d/edit-in-kitty.fish +++ /dev/null @@ -1,7 +0,0 @@ -function __ksi_completions - set --local ct (commandline --current-token) - set --local tokens (commandline --tokenize --cut-at-cursor --current-process) - printf "%s\n" $tokens $ct | command kitty-tool __complete__ fish | source - -end - -complete -f -c edit-in-kitty -a "(__ksi_completions)" diff --git a/shell-integration/fish/vendor_conf.d/kitty-shell-integration.fish b/shell-integration/fish/vendor_conf.d/kitty-shell-integration.fish index d1510eedd..e8ce92cd5 100644 --- a/shell-integration/fish/vendor_conf.d/kitty-shell-integration.fish +++ b/shell-integration/fish/vendor_conf.d/kitty-shell-integration.fish @@ -155,6 +155,10 @@ function __ksi_schedule --on-event fish_prompt -d "Setup kitty integration after end end +function edit-in-kitty --wraps "kitty-tool edit-in-kitty" + kitty-tool edit-in-kitty $argv +end + function __ksi_transmit_data -d "Transmit data to kitty using chunked DCS escapes" set --local data_len (string length -- "$argv[1]") set --local pos 1 @@ -188,100 +192,3 @@ function clone-in-kitty -d "Clone the current fish session into a new kitty wind set --prepend data "shell=fish" "pid=$fish_pid" "cwd=$b64_cwd" "env=$b64_envs" __ksi_transmit_data (string join "," -- $data | string replace --regex --all "\s" "") "clone" end - -function edit-in-kitty -d "Edit the specified file in a new kitty overlay using your preferred editor, even over SSH" - set --local data - set --local ed_filename "" - set --local usage "Usage: edit-in-kitty [OPTIONS] FILE" - for a in $argv - if contains -- "$a" -h --help - echo "$usage" - echo - echo "Edit the specified file in a kitty overlay window. Works over SSH as well." - echo - echo "For usage instructions see: https://sw.kovidgoyal.net/kitty/shell-integration/#edit-file" - return - end - set --local ea (printf "%s" "$a" | base64) - set --append data "a=$ea" - set ed_filename "$a" - end - if test -z "$ed_filename" - echo "$usage" > /dev/stderr - return 1 - end - if test ! \( -r "$ed_filename" -a -w "$ed_filename" \) - echo "$ed_filename is not readable and writable" > /dev/stderr - return 1 - end - if test ! -f "$ed_filename" - echo "$ed_filename is not a file" > /dev/stderr - return 1 - end - set --local stat_result (stat -L --format '%d:%i:%s' "$ed_filename" 2> /dev/null) - if test "$status" -ne 0 - set stat_result (stat -L -f '%d:%i:%z' "$ed_filename" 2> /dev/null) - end - if test "$status" -ne 0 || test -z "$stat_result" - echo "Failed to stat the file: $ed_filename" > /dev/stderr - return 1 - end - set --append data "file_inode=$stat_result" - set --local file_size (string match -rg '\d+:\d+:(\d+)' "$stat_result") - if test "$file_size" -gt (math "8 * 1024 * 1024") - echo "File is too large for performant editing" > /dev/stderr - return 1 - end - set --local file_data (base64 < "$ed_filename") - set --append data "file_data=$file_data" - __ksi_transmit_data (string join "," -- $data | string replace --regex --all "\s" "") "edit" - set --erase data - echo "Waiting for editing to be completed..." - set --global __ksi_waiting_for_edit "y" - - function __ksi_react_to_interrupt --on-signal SIGINT - functions --erase __ksi_react_to_interrupt - if test "$__ksi_waiting_for_edit" = "y" - set --erase __ksi_waiting_for_edit - __ksi_transmit_data "abort_signaled=interrupt" "edit" - end - end - - while true - set --local started "n" - while true - stty "-echo" - set --local line (head -n1 < /dev/tty) - test -z "$line" && break - if test "$started" = "y" - test "$line" = "UPDATE" && break - if test "$line" = "DONE" - set started "done" - break - end - printf "%s\n" "$line" > /dev/stderr - set --erase __ksi_waiting_for_edit - functions --erase __ksi_react_to_interrupt - return 1 - else - test "$line" = "KITTY_DATA_START" && set started "y" - end - end - test "$started" = "n" && continue - set --local data "" - while true - stty "-echo" - set --local line (head -n1 < /dev/tty) - test -z "$line" && break - test "$line" = "KITTY_DATA_END" && break - set data "$data$line" - end - if test -n "$data" -a "$started" != "done" - echo "Updating $ed_filename..." - printf "%s" "$data" | base64 -d > "$ed_filename" - end - test "$started" = "done" && break - end - set --erase __ksi_waiting_for_edit - functions --erase __ksi_react_to_interrupt -end diff --git a/shell-integration/zsh/kitty-integration b/shell-integration/zsh/kitty-integration index 40364cf96..e79b34a94 100644 --- a/shell-integration/zsh/kitty-integration +++ b/shell-integration/zsh/kitty-integration @@ -106,7 +106,6 @@ _ksi_deferred_init() { builtin unset "functions[_kitty]" builtin autoload -Uz -- $comp_dir/_kitty compdef _kitty kitty - compdef _kitty edit-in-kitty compdef _kitty clone-in-kitty compdef _kitty kitty-tool fi @@ -383,6 +382,8 @@ _ksi_deferred_init() { fi builtin unset KITTY_IS_CLONE_LAUNCH KITTY_CLONE_SOURCE_STRATEGIES + alias edit-in-kitty="kitty-tool edit-in-kitty" + # Unfunction _ksi_deferred_init to save memory. Don't unfunction # kitty-integration though because decent public functions aren't supposed to # to unfunction themselves when invoked. Unfunctioning is done by calling code. @@ -428,73 +429,3 @@ clone-in-kitty() { data="$data,env=$(builtin printf "%s" "$env" | builtin command base64)" _ksi_transmit_data "$data" "clone" "save_history" } - -edit-in-kitty() { - builtin local data="" - builtin local ed_filename="" - builtin local usage="Usage: edit-in-kitty [OPTIONS] FILE" - data="cwd=$(builtin printf "%s" "$PWD" | builtin command base64)" - while :; do - case "$1" in - "") break;; - -h|--help) - builtin printf "%s\n\n%s\n\n%s\n" "$usage" "Edit the specified file in a kitty overlay window. Works over SSH as well." "For usage instructions see: https://sw.kovidgoyal.net/kitty/shell-integration/#edit-file" - return - ;; - *) data="$data,a=$(builtin printf "%s" "$1" | builtin command base64)"; ed_filename="$1";; - esac - shift - done - [ -z "$ed_filename" ] && { - builtin echo "$usage" > /dev/stderr - return 1 - } - [ -r "$ed_filename" -a -w "$ed_filename" ] || { - builtin echo "$ed_filename is not readable and writable" > /dev/stderr - return 1 - } - [ ! -f "$ed_filename" ] && { - builtin echo "$ed_filename is not a file" > /dev/stderr - return 1 - } - builtin zmodload -F zsh/stat b:zstat - builtin typeset -A stat_result - builtin zstat -H stat_result "$ed_filename" || { builtin echo "Failed to stat the file: $ed_filename" > /dev/stderr; return 1; } - [ "${stat_result[size]}" -gt $((8 * 1024 * 1024)) ] && { builtin echo "File is too large for performant editing"; return 1; } - data="$data,file_inode=${stat_result[device]}:${stat_result[inode]}:${stat_result[size]}" - data="$data,file_data=$(builtin command base64 < "$ed_filename")" - _ksi_transmit_data "$data" "edit" - data="" - builtin echo "Waiting for editing to be completed..." - builtin local started="n" - builtin local line="" - builtin set -o localoptions -o localtraps - builtin local old_tty_settings=$(builtin command stty -g) - builtin command stty "-echo" - builtin trap "builtin command stty '$old_tty_settings'" EXIT - builtin trap "builtin command stty '$old_tty_settings'; _ksi_transmit_data 'abort_signaled=interrupt' 'edit'; return 1;" INT TERM - while :; do - started="n" - while IFS= builtin read -r line; do - if [ "$started" = "y" ]; then - [ "$line" = "UPDATE" ] && break; - [ "$line" = "DONE" ] && { started="done"; break; } - builtin printf "%s\n" "$line" > /dev/stderr - return 1 - else - [ "$line" = "KITTY_DATA_START" ] && started="y" - fi - done - [ "$started" = "n" ] && continue - data="" - while IFS= builtin read -r line; do - [ "$line" = "KITTY_DATA_END" ] && break - data="$data$line" - done - [ -n "$data" -a "$started" != "done" ] && { - builtin echo "Updating $ed_filename..." - builtin printf "%s" "$data" | builtin command base64 -d > "$ed_filename" - } - [ "$started" = "done" ] && break - done -}