Skip to content

Commit 04d1105

Browse files
committed
Keep original behavior and unbind keys correctly
Because Tmux 2.4 doesn't forward bindings from the root prefix, we have to fetch the original behavior using the list-keys command. That way, we are able to forward it and to unbind it correctly.
1 parent e3384bf commit 04d1105

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

scripts/copycat_mode_bindings.sh

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,37 @@
33
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
44

55
source "$CURRENT_DIR/helpers.sh"
6+
AWK_CMD='awk'
7+
if command_exists gawk; then
8+
AWK_CMD='gawk'
9+
fi
610

711
# Extends a keyboard key.
812
# Benefits: tmux won't report errors and everything will work fine even if the
913
# script is deleted.
1014
extend_key() {
1115
local key="$1"
1216
local script="$2"
17+
local cmd
1318

19+
# 1. 'cmd' or 'key' is sent to tmux. This ensures the default key action is done.
20+
# 2. Script is executed.
21+
# 3. `true` command ensures an exit status 0 is returned. This ensures a
22+
# user never gets an error msg - even if the script file from step 2 is
23+
# deleted.
1424
if tmux_is_at_least 2.4; then
25+
# We fetch the current behavior of the 'key' mapping in
26+
# variable 'cmd'
27+
cmd=$(tmux list-keys -T copy-mode-$(tmux_copy_mode) | $AWK_CMD '$4 == "'$key'"' | $AWK_CMD '{ $1=""; $2=""; $3=""; $4=""; sub(" ", " "); print }')
28+
# If 'cmd' is already a copycat command, we do nothing
29+
if echo "$cmd" | grep -q copycat; then
30+
return
31+
fi
1532
# We save the previous mapping to a file in order to be able to recover
1633
# the previous mapping when we unbind
17-
tmux list-keys -T copy-mode-$(tmux_copy_mode) | grep -F "$key" >> /tmp/copycat_$(whoami)_recover_keys
18-
tmux bind-key -T copy-mode-$(tmux_copy_mode) "$key" run-shell "$script"
34+
tmux list-keys -T copy-mode-$(tmux_copy_mode) | $AWK_CMD '$4 == "'$key'"' >> /tmp/copycat_$(whoami)_recover_keys
35+
tmux bind-key -T copy-mode-$(tmux_copy_mode) "$key" run-shell "tmux $cmd; $script; true"
1936
else
20-
# 1. 'key' is sent to tmux. This ensures the default key action is done.
21-
# 2. Script is executed.
22-
# 3. `true` command ensures an exit status 0 is returned. This ensures a
23-
# user never gets an error msg - even if the script file from step 2 is
24-
# deleted.
2537
tmux bind-key -n "$key" run-shell "tmux send-keys '$key'; $script; true"
2638
fi
2739
}

scripts/copycat_mode_quit.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ unbind_prev_next_bindings() {
1919

2020
unbind_all_bindings() {
2121
if tmux_is_at_least 2.4; then
22-
while read key_cmd; do
22+
grep -v copycat </tmp/copycat_$(whoami)_recover_keys | while read key_cmd; do
2323
tmux $key_cmd
24-
done < /tmp/copycat_$(whoami)_recover_keys
24+
done < /dev/stdin
2525
rm /tmp/copycat_$(whoami)_recover_keys
2626
else
2727
unbind_cancel_bindings

0 commit comments

Comments
 (0)