Skip to content

Commit e3384bf

Browse files
committed
Support 2.4 sintax when binding and unbinding keys
1 parent 0e4656a commit e3384bf

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

scripts/copycat_mode_bindings.sh

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,19 @@ extend_key() {
1111
local key="$1"
1212
local script="$2"
1313

14-
# 1. 'key' is sent to tmux. This ensures the default key action is done.
15-
# 2. Script is executed.
16-
# 3. `true` command ensures an exit status 0 is returned. This ensures a
17-
# user never gets an error msg - even if the script file from step 2 is
18-
# deleted.
19-
tmux bind-key -n "$key" run-shell "tmux send-keys '$key'; $script; true"
14+
if tmux_is_at_least 2.4; then
15+
# We save the previous mapping to a file in order to be able to recover
16+
# 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"
19+
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.
25+
tmux bind-key -n "$key" run-shell "tmux send-keys '$key'; $script; true"
26+
fi
2027
}
2128

2229
copycat_cancel_bindings() {

scripts/copycat_mode_quit.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,15 @@ unbind_prev_next_bindings() {
1818
}
1919

2020
unbind_all_bindings() {
21-
unbind_cancel_bindings
22-
unbind_prev_next_bindings
21+
if tmux_is_at_least 2.4; then
22+
while read key_cmd; do
23+
tmux $key_cmd
24+
done < /tmp/copycat_$(whoami)_recover_keys
25+
rm /tmp/copycat_$(whoami)_recover_keys
26+
else
27+
unbind_cancel_bindings
28+
unbind_prev_next_bindings
29+
fi
2330
}
2431

2532
main() {

scripts/helpers.sh

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,26 @@ copycat_prev_key() {
150150

151151
# function expected output: 'C-c Enter q'
152152
copycat_quit_copy_mode_keys() {
153-
local commands_that_quit_copy_mode="cancel\|copy-selection\|copy-pipe"
154-
local copy_mode="$(tmux_copy_mode)-copy"
155-
tmux list-keys -t "$copy_mode" |
156-
\grep "$commands_that_quit_copy_mode" |
157-
$AWK_CMD '{ print $4}' |
158-
sort -u |
159-
sed 's/C-j//g' |
160-
xargs echo
153+
if tmux_is_at_least 2.4; then
154+
local commands_that_quit_copy_mode="cancel"
155+
local copy_mode="copy-mode-$(tmux_copy_mode)"
156+
tmux list-keys -T "$copy_mode" |
157+
\grep "$commands_that_quit_copy_mode" |
158+
$AWK_CMD '{ print $4 }' |
159+
sort -u |
160+
sed 's/C-j//g' |
161+
xargs echo
162+
163+
else
164+
local commands_that_quit_copy_mode="cancel\|copy-selection\|copy-pipe"
165+
local copy_mode="$(tmux_copy_mode)-copy"
166+
tmux list-keys -t "$copy_mode" |
167+
\grep "$commands_that_quit_copy_mode" |
168+
$AWK_CMD '{ print $4 }' |
169+
sort -u |
170+
sed 's/C-j//g' |
171+
xargs echo
172+
fi
161173
}
162174

163175
# === 'private' functions ===

0 commit comments

Comments
 (0)