From 5d9a6f0f97d5e0d451a56a94e6e6616b4c8c3a40 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Mon, 13 Oct 2025 20:37:17 +0530 Subject: Add repl integration --- autoload/+init.kak | 5 ++++- autoload/git.kak | 36 ++++++++++++++++++++---------- autoload/repl.kak | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 12 deletions(-) create mode 100644 autoload/repl.kak diff --git a/autoload/+init.kak b/autoload/+init.kak index d2a4233..66374c4 100644 --- a/autoload/+init.kak +++ b/autoload/+init.kak @@ -33,6 +33,7 @@ add-highlighter global/ show-matching -previous hook global RegisterModified '/' %{ add-highlighter -override global/search regex "%reg{/}" 0:search } map global user '' ':set-register slash ""' map global user '/' '/(?i)' +map global user 'r' '*%s' # Preserve count for user modes (look for alternatives) # TODO: Reset count on modechange? @@ -56,13 +57,15 @@ hook global ModeChange .*:insert:.* %{ try %{ # Tmux window mode declare-user-mode win map global normal ':enter-user-mode win' -docstring 'Window mode' -map global win q ':quit' -docstring 'Find files' +map global win q ':quit' -docstring 'Quit' map global win v ':tmux-terminal-horizontal kak -c %val{session}' -docstring 'Split vertical' map global win s ':tmux-terminal-vertical kak -c %val{session}' -docstring 'Split horizontal' map global win h ':nop %sh{tmux select-pane -L}' -docstring 'Jump left' map global win j ':nop %sh{tmux select-pane -D}' -docstring 'Jump down' map global win k ':nop %sh{tmux select-pane -U}' -docstring 'Jump up' map global win l ':nop %sh{tmux select-pane -R}' -docstring 'Jump right' +map global win z ':wq' + # Clipboard management mappings map global user y " xclip -selection clipboard" -docstring "yank the selection into the clipboard" diff --git a/autoload/git.kak b/autoload/git.kak index 86f6d76..6f4015c 100644 --- a/autoload/git.kak +++ b/autoload/git.kak @@ -1,26 +1,40 @@ -define-command gitui -params .. %{ connect terminal env "EDITOR=kcr edit" gitu %arg{@} } +define-command gitui -params .. %{ + connect terminal env "EDITOR=kak -c %val{session}" "VISUAL=kak -c %val{session}" gitu %arg{@} +} declare-user-mode git -map global user g ':enter-user-mode git' -docstring 'Git mode' -map global git s ':gitui' -docstring 'Git tui' -map global git A ':git add %val{buffile}' -docstring 'Add file' -map global git c ':git add commit' -docstring 'Commit' -map global git C ':git add commit --amend' -docstring 'Amend commit' +map global user g ': enter-user-mode git' -docstring 'Git mode' +map global git s ': gitui' -docstring 'Git tui' +map global git A ': git add %val{buffile}' -docstring 'Add file' +map global git c ': git add commit' -docstring 'Commit' +map global git C ': git add commit --amend' -docstring 'Amend commit' # Hunk -map global git n ':git next-hunk' -docstring 'Next hunk' -map global git p ':git prev-hunk' -docstring 'Previous hunk' +map global git n ': git next-hunk' -docstring 'Next hunk' +map global git p ': git prev-hunk' -docstring 'Previous hunk' +map global git h ': git-toggle-diff' -docstring 'Toggle hunk indicators' set-option global git_diff_add_char "+" set-option global git_diff_del_char "-" set-option global git_diff_mod_char "~" set-option global git_diff_top_char "^" +define-command git-toggle-diff %{ + try %{ remove-highlighter window/git-diff } catch %{ git show-diff } +} + +# TODO: Toggle this hook global -group git-diff-if-repo EnterDirectory .* %{ - # TODO: Disable git hunk highlight for non-git repos remove-hooks global git-diff - hook global -group git-diff ModeChange .*:.*:normal %{ try %{git show-diff} } - hook global -group git-diff WinCreate .* %{ try %{git show-diff} } + evaluate-commands %sh{ + if (git rev-parse --is-inside-work-tree >/dev/null 2>&1); then + echo " + hook global -group git-diff ModeChange .*:.*:normal %{ try %{git update-diff} } + hook global -group git-diff WinCreate .* %{ try %{git update-diff} } + hook global -group git-diff BufReload .* %{ try %{git update-diff} } + " + fi + } } # Git window actions diff --git a/autoload/repl.kak b/autoload/repl.kak new file mode 100644 index 0000000..31922f0 --- /dev/null +++ b/autoload/repl.kak @@ -0,0 +1,64 @@ +declare-option str xrepl_init_cmd "$SHELL"; +declare-option str xrepl_input_transform; +declare-option bool xrepl_running false; + +declare-option str-list xrepl_modes; + +set-option -add global xrepl_modes "node" +declare-option str xrepl_init_cmd_node "node" + +define-command xrepl-set-mode -params 1 %{ + xrepl-quit + evaluate-commands %sh{ + cmd="$(eval echo "\$kak_opt_xrepl_init_cmd_$1")" + input_transform="$(eval echo "\$kak_opt_xrepl_input_transform_$1")" + echo "info %{$kak_opt_xrepl_init_cmd_node; $cmd:$input_transform}" + if [ -z "$cmd" ]; then cmd="$SHELL"; fi + if [ -z "$input_transform" ]; then input_transform=""; fi + echo "set global xrepl_init_cmd '$(printf '%q' $cmd)'" + echo "set global xrepl_input_transform '$(printf '%q' $input_transform)'" + } +} + +define-command xrepl-send-command %{ + evaluate-commands %sh{ + value="$kak_selection" + if ! [ -z "$kak_xrepl_input_transform" ]; then + value=$(echo "$value" | "$kak_xrepl_input_transform") + fi + echo "repl-send-text %{$value}" + } +} + +define-command xrepl-send-keys -params 1.. %{ + nop %sh{ tmux send-keys -t "$kak_opt_tmux_repl_id" "$@" } +} + +define-command xrepl-quit %{ + set-option global xrepl_running false + try %{ nop %sh{ + [ -z "$kak_opt_tmux_repl_id" ] && exit 0; + tmux kill-pane -t "$kak_opt_tmux_repl_id"; + } } +} + +define-command xrepl-begin %{ + # TODO: Open if not running + evaluate-commands %sh{ + if [ "$kak_opt_xrepl_running" = "false" ]; then + echo "repl-new %opt{xrepl_init_cmd}" + echo "nop %sh{ tmux last-pane }" + echo "set-option global xrepl_running true" + fi + } +} + +declare-user-mode repl +map global normal ': enter-user-mode repl' +map global repl ': xrepl-begin' +map global repl t ': xrepl-begin' +map global repl 'p: xrepl-send-command' +map global repl l ': xrepl-send-command' +map global repl r ': xrepl-send-keys Enter' +map global repl c ': xrepl-send-keys C-c' +map global repl q ': xrepl-quit' -- cgit v1.3.1