-
Notifications
You must be signed in to change notification settings - Fork 1
/
repl-bridge.kak
107 lines (98 loc) · 3.1 KB
/
repl-bridge.kak
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
# repl bridge for executing things interactively
declare-option -hidden str repl_bridge_folder "/tmp/kakoune_repl_bridge/%val{session}"
declare-option -hidden str repl_bridge_source %sh{echo "${kak_source%/*}"}
declare-option bool repl_bridge_fifo_enabled false
define-command -docstring 'repl-bridge-enable-fifo <language>: Open FIFO and start terminal' \
repl-bridge-enable-fifo -params 1 %{
evaluate-commands %sh{
lang=$1
folder=$kak_opt_repl_bridge_folder/$lang
mkfifo $folder/fifo
echo "terminal tail -f $folder/fifo"
}
set-option global repl_bridge_fifo_enabled true
}
define-command -docstring 'repl-bridge-disable-fifo <language>: Close FIFO' \
repl-bridge-disable-fifo -params 1 %{
nop %sh{
lang=$1
folder=$kak_opt_repl_bridge_folder/$lang
rm $folder/fifo
}
set-option global repl_bridge_fifo_enabled false
}
define-command -docstring 'repl-bridge-start <language>: Create FIFOs and start repl' \
-params 1 \
repl-bridge-start %{
nop %sh{
lang=$1
folder=$kak_opt_repl_bridge_folder/$lang
if [ ! -d $folder ]; then
mkdir -p $folder
mkfifo $folder/in
mkfifo $folder/out
( python $kak_opt_repl_bridge_source/repl-bridge.py \
$folder/in \
$folder/out \
$kak_opt_repl_bridge_source/config/$lang
) >/dev/null 2>&1 </dev/null &
fi
}
}
define-command -docstring 'repl-bridge-stop <language>: Stop repl and remove FIFOs' \
-params 1 \
repl-bridge-stop %{
nop %sh{
lang=$1
folder=$kak_opt_repl_bridge_folder/$lang
if [ -d $folder ]; then
echo "!quit" > $folder/in
rm $folder/in
rm $folder/out
rmdir -p $folder
fi
}
}
define-command -docstring 'repl-bridge-send <language> [command]: Evaluate selections or argument using repl-bridge return result in " register' \
repl-bridge-send -params 1..2 %{
repl-bridge-start %arg{1}
evaluate-commands %sh{
lang=$1
shift
folder=$kak_opt_repl_bridge_folder/$lang
cat_command="cat $folder/out"
if $kak_opt_repl_bridge_fifo_enabled; then
cat_command="$cat_command | tee -a $folder/fifo"
fi
if [ $# -eq 0 ]; then
eval set -- "$kak_quoted_selections"
fi
out=""
while [ $# -gt 0 ]; do
output=$(eval $cat_command) && echo "set-register dquote %{$output}" &
echo "$1" > $folder/in &
wait
shift
done
}
}
define-command repl-bridge -params 2..3 \
-docstring "repl-bridge <language> <repl-bridge-command> [command]" \
-shell-script-candidates %{
for cmd in start stop send;
do echo $cmd;
done
} %{ evaluate-commands %sh{
if [ $# -eq 3 ]; then
echo "repl-bridge-$2 $1 $3"
else
echo "repl-bridge-$2 $1"
fi
}}
hook global KakEnd .* %{
evaluate-commands %sh{
for d in $kak_opt_repl_bridge_folder/*; do
echo "repl-bridge-stop ${d##*/}"
done
}
}