aboutsummaryrefslogtreecommitdiff
path: root/autoload/repl.kak
blob: 1e3d3698d7d65fd63b0b5e0ecd6eb2441a91484f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
declare-option bool xrepl_running false;
declare-option str xrepl_current_cmd;
declare-option str xrepl_current_transform;
declare-option bool xrepl_current_clear_screen false;

# TODO: Use register set to selection (paragraph) instead of selection
# TODO: Add env
# TODO: Migrate curly
# TODO: Preserve original selection with send paragraph

declare-user-mode repl-mode-select
define-command define-repl-mode -params 3 %{
  # TODO: USe a hidden command and use for keymap
  map global repl-mode-select %arg{1} -docstring %arg{2} %sh{
    kak_escape() { printf "'"; printf '%s' "$1" | sed "s/'/''/g"; printf "'"; }
    printf ": xrepl-quit<ret>"
    printf ": set global xrepl_current_transform \"\"<ret>"
    printf ": set global xrepl_current_clear_screen false<ret>"
    printf ": evaluate-commands $(kak_escape "$(echo "$3" | tr '\n' ';')")<ret>"
    printf ": xrepl-begin<ret>"
  }
}

define-repl-mode n 'Node' %{
  set global xrepl_current_cmd 'node'
}
define-repl-mode s 'Shell' %{
  set global xrepl_current_cmd '$SHELL'
}
# TODO: Only cd inside sh
define-repl-mode j 'Jest' %{
  set global xrepl_current_cmd '$SHELL'
  set global xrepl_current_transform 'cat > /dev/null
    echo "sh -c \\"cd ''$(dirname "$kak_buffile")''; npx jest --runTestsByPath ''$kak_buffile''\\";"
  '
  set global xrepl_current_clear_screen true
}
define-repl-mode c 'Cypress' %{
  set global xrepl_current_cmd '$SHELL'
  set global xrepl_current_transform 'cat > /dev/null
    echo "npx cypress run --headless --e2e --spec ''$kak_buffile'';"
  '
  set global xrepl_current_clear_screen true
}
define-repl-mode a 'AI: Gemini' %{
  set global xrepl_current_cmd 'gemini'
}

define-command xrepl-send-prompt %{
  prompt -buffer-completion 'Send to repl: ' %{ repl-send-text %val{text} }
}

define-command xrepl-send-command %{
  evaluate-commands -draft %sh{
    if [ "$kak_opt_xrepl_current_clear_screen" == "true" ]; then
      echo "xrepl-send-keys C-l"
    fi

    transform="$kak_opt_xrepl_current_transform"
    value="$kak_selection"
    if ! [ -z "$transform" ]; then
      export kak_buffile kak_selection kak_selection_desc kak_cursor_line kak_cursor_column
      value=$(echo "$kak_selection" | sh -c "$transform")
    fi
    echo -e "repl-send-text %{$value\n}"
  }
}

define-command xrepl-send-paragraph %{
  execute-keys '<a-i>p'
  xrepl-send-command
}

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{
    if ! [ -z "$kak_opt_tmux_repl_id" ]; then
      tmux kill-pane -t "$kak_opt_tmux_repl_id";
    fi
  } }
}

define-command xrepl-begin %{
  evaluate-commands %sh{
    if [ "$kak_opt_xrepl_running" = "false" ]; then
      init_cmd="$kak_opt_xrepl_current_cmd"
      if [ -z "$init_cmd" ]; then init_cmd="$SHELL"; fi
      echo "set-option global xrepl_running true"
      echo "repl-new -l 45% $init_cmd"
      echo "nop %sh{ tmux last-pane }"
    fi
  }
}

declare-user-mode repl
map global normal <c-t> ': enter-user-mode repl<ret>'
map global repl <c-t> ': xrepl-begin<ret>'
map global repl t ': xrepl-begin<ret>'
map global repl <ret> ': xrepl-send-paragraph<ret>'
map global repl l ': xrepl-send-command<ret>'
map global repl r ': xrepl-send-keys Enter<ret>'
map global repl c ': xrepl-send-keys C-c<ret>'
map global repl q ': xrepl-quit<ret>'
map global repl p ': xrepl-send-prompt<ret>'
map global repl <tab> ': enter-user-mode repl-mode-select<ret>'