|
3 | 3 | CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
4 | 4 |
|
5 | 5 | source "$CURRENT_DIR/helpers.sh"
|
| 6 | +AWK_CMD='awk' |
| 7 | +if command_exists gawk; then |
| 8 | + AWK_CMD='gawk' |
| 9 | +fi |
6 | 10 |
|
7 | 11 | # Extends a keyboard key.
|
8 | 12 | # Benefits: tmux won't report errors and everything will work fine even if the
|
9 | 13 | # script is deleted.
|
10 | 14 | extend_key() {
|
11 | 15 | local key="$1"
|
12 | 16 | local script="$2"
|
| 17 | + local cmd |
13 | 18 |
|
| 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. |
14 | 24 | 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 |
15 | 32 | # We save the previous mapping to a file in order to be able to recover
|
16 | 33 | # 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" |
19 | 36 | 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 | 37 | tmux bind-key -n "$key" run-shell "tmux send-keys '$key'; $script; true"
|
26 | 38 | fi
|
27 | 39 | }
|
|
0 commit comments