Skip to content

Commit 8f4d0ee

Browse files
committed
Support only Tmux 2.4
1 parent 5dd9645 commit 8f4d0ee

8 files changed

+42
-295
lines changed

scripts/copycat_jump.sh

Lines changed: 13 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -114,33 +114,14 @@ _copycat_enter_mode() {
114114

115115
# clears selection from a previous match
116116
_copycat_exit_select_mode() {
117-
if tmux_is_at_least 2.4; then
118-
tmux send-keys -X clear-selection
119-
elif [ "$TMUX_COPY_MODE" == "vi" ]; then
120-
# vi mode
121-
tmux send-keys Escape
122-
else
123-
# emacs mode
124-
tmux send-keys C-g
125-
fi
117+
tmux send-keys -X clear-selection
126118
}
127119

128120
# "manually" go up in the scrollback for a number of lines
129121
_copycat_manually_go_up() {
130122
local line_number="$1"
131-
if tmux_is_at_least 2.4; then
132-
tmux send-keys -X -N "$line_number" cursor-up
133-
tmux send-keys -X start-of-line
134-
elif [ "$TMUX_COPY_MODE" == "vi" ]; then
135-
# vi copy mode
136-
tmux send-keys "$line_number" k 0
137-
else
138-
# emacs copy mode
139-
for (( c=1; c<="$line_number"; c++ )); do
140-
tmux send-keys C-p
141-
done
142-
tmux send-keys C-a
143-
fi
123+
tmux send-keys -X -N "$line_number" cursor-up
124+
tmux send-keys -X start-of-line
144125
}
145126

146127
_copycat_create_padding_below_result() {
@@ -161,40 +142,17 @@ _copycat_create_padding_below_result() {
161142
return
162143
fi
163144

164-
if tmux_is_at_least 2.4; then
165-
tmux send-keys -X -N "$padding" cursor-down
166-
tmux send-keys -X -N "$padding" cursor-up
167-
elif [ "$TMUX_COPY_MODE" == "vi" ]; then
168-
# vi copy mode
169-
tmux send-keys "$padding" j "$padding" k
170-
else
171-
# emacs copy mode
172-
for (( c=1; c<="$padding"; c++ )); do
173-
tmux send-keys C-n
174-
done
175-
for (( c=1; c<="$padding"; c++ )); do
176-
tmux send-keys C-p
177-
done
178-
fi
145+
tmux send-keys -X -N "$padding" cursor-down
146+
tmux send-keys -X -N "$padding" cursor-up
179147
}
180148

181149
# performs a jump to go to line
182150
_copycat_go_to_line_with_jump() {
183151
local line_number="$1"
184152
# first jumps to the "bottom" in copy mode so that jumps are consistent
185-
if tmux_is_at_least 2.4; then
186-
tmux send-keys -X history-bottom
187-
tmux send-keys -X start-of-line
188-
tmux send-keys -X goto-line $line_number
189-
elif [ "$TMUX_COPY_MODE" == "vi" ]; then
190-
# vi copy mode
191-
tmux send-keys G 0 :
192-
tmux send-keys "$line_number" C-m
193-
else
194-
# emacs copy mode
195-
tmux send-keys "M->" C-a g
196-
tmux send-keys "$line_number" C-m
197-
fi
153+
tmux send-keys -X history-bottom
154+
tmux send-keys -X start-of-line
155+
tmux send-keys -X goto-line $line_number
198156
}
199157

200158
# maximum line number that can be reached via tmux 'jump'
@@ -247,40 +205,16 @@ _copycat_position_to_match_start() {
247205
local match_line_position="$1"
248206
[ "$match_line_position" -eq "0" ] && return 0
249207

250-
if tmux_is_at_least 2.4; then
251-
tmux send-keys -X -N "$match_line_position" cursor-right
252-
elif [ "$TMUX_COPY_MODE" == "vi" ]; then
253-
# vi copy mode
254-
tmux send-keys "$match_line_position" l
255-
else
256-
# emacs copy mode
257-
# emacs doesn't have repeat, so we're manually looping :(
258-
for (( c=1; c<="$match_line_position"; c++ )); do
259-
tmux send-keys C-f
260-
done
261-
fi
208+
tmux send-keys -X -N "$match_line_position" cursor-right
262209
}
263210

264211
_copycat_select() {
265212
local match="$1"
266213
local length="${#match}"
267-
if tmux_is_at_least 2.4; then
268-
tmux send-keys -X begin-selection
269-
tmux send-keys -X -N "$length" cursor-right
270-
if [ "$TMUX_COPY_MODE" == "vi" ]; then
271-
tmux send-keys -X cursor-left # selection correction for 1 char
272-
fi
273-
elif [ "$TMUX_COPY_MODE" == "vi" ]; then
274-
# vi copy mode
275-
tmux send-keys Space "$length" l h # selection correction for 1 char
276-
else
277-
# emacs copy mode
278-
tmux send-keys C-Space
279-
# emacs doesn't have repeat, so we're manually looping :(
280-
for (( c=1; c<="$length"; c++ )); do
281-
tmux send-keys C-f
282-
done
283-
# NO selection correction for emacs mode
214+
tmux send-keys -X begin-selection
215+
tmux send-keys -X -N "$length" cursor-right
216+
if [ "$TMUX_COPY_MODE" == "vi" ]; then
217+
tmux send-keys -X cursor-left # selection correction for 1 char
284218
fi
285219
}
286220

scripts/copycat_mode_bindings.sh

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,17 @@ extend_key() {
2121
# 3. `true` command ensures an exit status 0 is returned. This ensures a
2222
# user never gets an error msg - even if the script file from step 2 is
2323
# deleted.
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 $(tmux_copy_mode_string) | $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
32-
# We save the previous mapping to a file in order to be able to recover
33-
# the previous mapping when we unbind
34-
tmux list-keys -T $(tmux_copy_mode_string) | $AWK_CMD '$4 == "'$key'"' >> /tmp/copycat_$(whoami)_recover_keys
35-
tmux bind-key -T $(tmux_copy_mode_string) "$key" run-shell "tmux $cmd; $script; true"
36-
else
37-
tmux bind-key -n "$key" run-shell "tmux send-keys '$key'; $script; true"
24+
# We fetch the current behavior of the 'key' mapping in
25+
# variable 'cmd'
26+
cmd=$(tmux list-keys -T $(tmux_copy_mode_string) | $AWK_CMD '$4 == "'$key'"' | $AWK_CMD '{ $1=""; $2=""; $3=""; $4=""; sub(" ", " "); print }')
27+
# If 'cmd' is already a copycat command, we do nothing
28+
if echo "$cmd" | grep -q copycat; then
29+
return
3830
fi
31+
# We save the previous mapping to a file in order to be able to recover
32+
# the previous mapping when we unbind
33+
tmux list-keys -T $(tmux_copy_mode_string) | $AWK_CMD '$4 == "'$key'"' >> /tmp/copycat_$(whoami)_recover_keys
34+
tmux bind-key -T $(tmux_copy_mode_string) "$key" run-shell "tmux $cmd; $script; true"
3935
}
4036

4137
copycat_cancel_bindings() {

scripts/copycat_mode_quit.sh

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

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

3227
main() {

scripts/helpers.sh

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -158,26 +158,14 @@ copycat_prev_key() {
158158

159159
# function expected output: 'C-c Enter q'
160160
copycat_quit_copy_mode_keys() {
161-
if tmux_is_at_least 2.4; then
162-
local commands_that_quit_copy_mode="cancel"
163-
local copy_mode="$(tmux_copy_mode_string)"
164-
tmux list-keys -T "$copy_mode" |
165-
\grep "$commands_that_quit_copy_mode" |
166-
$AWK_CMD '{ print $4 }' |
167-
sort -u |
168-
sed 's/C-j//g' |
169-
xargs echo
170-
171-
else
172-
local commands_that_quit_copy_mode="cancel\|copy-selection\|copy-pipe"
173-
local copy_mode="$(tmux_copy_mode)-copy"
174-
tmux list-keys -t "$copy_mode" |
175-
\grep "$commands_that_quit_copy_mode" |
176-
$AWK_CMD '{ print $4 }' |
177-
sort -u |
178-
sed 's/C-j//g' |
179-
xargs echo
180-
fi
161+
local commands_that_quit_copy_mode="cancel"
162+
local copy_mode="$(tmux_copy_mode_string)"
163+
tmux list-keys -T "$copy_mode" |
164+
\grep "$commands_that_quit_copy_mode" |
165+
$AWK_CMD '{ print $4 }' |
166+
sort -u |
167+
sed 's/C-j//g' |
168+
xargs echo
181169
}
182170

183171
# === 'private' functions ===
@@ -202,33 +190,3 @@ _pane_unique_id() {
202190
tmux display-message -p "#{session_id}-#{window_index}-#{pane_index}" |
203191
sed 's/\$//'
204192
}
205-
206-
# Cache the TMUX version for speed.
207-
tmux_version="$(tmux -V | cut -d ' ' -f 2)"
208-
209-
tmux_is_at_least() {
210-
if [[ $tmux_version == "$1" || $tmux_version == "master" ]]
211-
then
212-
return 0
213-
fi
214-
215-
local IFS=.
216-
local i tver=($tmux_version) wver=($1)
217-
218-
# fill empty fields in tver with zeros
219-
for ((i=${#tver[@]}; i<${#wver[@]}; i++)); do
220-
tver[i]=0
221-
done
222-
223-
# fill empty fields in wver with zeros
224-
for ((i=${#wver[@]}; i<${#tver[@]}; i++)); do
225-
wver[i]=0
226-
done
227-
228-
for ((i=0; i<${#tver[@]}; i++)); do
229-
if ((10#${tver[i]} < 10#${wver[i]})); then
230-
return 1
231-
fi
232-
done
233-
return 0
234-
}

test/helpers/setup_tmux_conf.sh

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,7 @@ BASE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/../.." && pwd )"
77
setup_tmux_conf() {
88
# Copy mode (vi or emacs) is automatically determined from EDITOR
99
# environment variable set in test runner file `test/run-tests-within-vm`.
10-
if tmux_is_at_least 2.4; then
11-
echo "bind-key -T copy-mode-vi y send-keys -X copy-selection-and-cancel" > ~/.tmux.conf
12-
echo "bind-key -T copy-mode y send-keys -X copy-selection-and-cancel" >> ~/.tmux.conf
13-
echo "run-shell '$BASE_DIR/copycat.tmux'" >> ~/.tmux.conf
14-
else
15-
echo "bind-key -t vi-copy y copy-selection" > ~/.tmux.conf
16-
echo "bind-key -t emacs-copy y copy-selection" >> ~/.tmux.conf
17-
echo "run-shell '$BASE_DIR/copycat.tmux'" >> ~/.tmux.conf
18-
fi
10+
echo "bind-key -T copy-mode-vi y send-keys -X copy-selection-and-cancel" > ~/.tmux.conf
11+
echo "bind-key -T copy-mode y send-keys -X copy-selection-and-cancel" >> ~/.tmux.conf
12+
echo "run-shell '$BASE_DIR/copycat.tmux'" >> ~/.tmux.conf
1913
}
20-
21-
# Cache the TMUX version for speed.
22-
tmux_version="$(tmux -V | cut -d ' ' -f 2)"
23-
24-
tmux_is_at_least() {
25-
if [[ $tmux_version == "$1" || $tmux_version == "master" ]]
26-
then
27-
return 0
28-
fi
29-
30-
local IFS=.
31-
local i tver=($tmux_version) wver=($1)
32-
33-
# fill empty fields in tver with zeros
34-
for ((i=${#tver[@]}; i<${#wver[@]}; i++)); do
35-
tver[i]=0
36-
done
37-
38-
# fill empty fields in wver with zeros
39-
for ((i=${#wver[@]}; i<${#tver[@]}; i++)); do
40-
wver[i]=0
41-
done
42-
43-
for ((i=0; i<${#tver[@]}; i++)); do
44-
if ((10#${tver[i]} < 10#${wver[i]})); then
45-
return 1
46-
fi
47-
done
48-
return 0
49-
}
50-
51-
setup_tmux_conf

test/helpers/setup_tmux_conf_with_custom_searches.sh

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,8 @@ BASE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/../.." && pwd )"
88
setup_tmux_conf() {
99
# Copy mode (vi or emacs) is automatically determined from EDITOR
1010
# environment variable set in test runner file `test/run-tests-within-vm`.
11-
if tmux_is_at_least 2.4; then
12-
echo "bind-key -T copy-mode-vi y send-keys -X copy-selection-and-cancel" > ~/.tmux.conf
13-
echo "bind-key -T copy-mode y send-keys -X copy-selection-and-cancel" >> ~/.tmux.conf
14-
echo "set -g @copycat_search_C-t 'random string[[:digit:]]+'" >> ~/.tmux.conf
15-
echo "run-shell '$BASE_DIR/copycat.tmux'" >> ~/.tmux.conf
16-
else
17-
echo "bind-key -t vi-copy y copy-selection" > ~/.tmux.conf
18-
echo "bind-key -t emacs-copy y copy-selection" >> ~/.tmux.conf
19-
echo "set -g @copycat_search_C-t 'random string[[:digit:]]+'" >> ~/.tmux.conf
20-
echo "run-shell '$BASE_DIR/copycat.tmux'" >> ~/.tmux.conf
21-
fi
11+
echo "bind-key -T copy-mode-vi y send-keys -X copy-selection-and-cancel" > ~/.tmux.conf
12+
echo "bind-key -T copy-mode y send-keys -X copy-selection-and-cancel" >> ~/.tmux.conf
13+
echo "set -g @copycat_search_C-t 'random string[[:digit:]]+'" >> ~/.tmux.conf
14+
echo "run-shell '$BASE_DIR/copycat.tmux'" >> ~/.tmux.conf
2215
}
23-
# Cache the TMUX version for speed.
24-
tmux_version="$(tmux -V | cut -d ' ' -f 2)"
25-
26-
tmux_is_at_least() {
27-
if [[ $tmux_version == "$1" || $tmux_version == "master" ]]
28-
then
29-
return 0
30-
fi
31-
32-
local IFS=.
33-
local i tver=($tmux_version) wver=($1)
34-
35-
# fill empty fields in tver with zeros
36-
for ((i=${#tver[@]}; i<${#wver[@]}; i++)); do
37-
tver[i]=0
38-
done
39-
40-
# fill empty fields in wver with zeros
41-
for ((i=${#wver[@]}; i<${#tver[@]}; i++)); do
42-
wver[i]=0
43-
done
44-
45-
for ((i=0; i<${#tver[@]}; i++)); do
46-
if ((10#${tver[i]} < 10#${wver[i]})); then
47-
return 1
48-
fi
49-
done
50-
return 0
51-
}
52-
53-
setup_tmux_conf

vagrant_centos_provisioning.sh

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)