Skip to content

Commit 3372e1b

Browse files
authored
Merge pull request #119 from thalesmello/bugfix/merge-fix-upstream
Support Tmux 2.4 mappings
2 parents e8f6c26 + 44b23ff commit 3372e1b

19 files changed

+138
-171
lines changed

.travis.yml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
# generic packages
22
before_install:
33
- sudo apt-get update
4-
- sudo apt-get install -y git-core expect
4+
- sudo apt-get install -y git-core expect gawk
55
- sudo apt-get install -y python-software-properties software-properties-common
66

7-
# install latest Tmux 1.9a
7+
# install Tmux 2.5
88
install:
9-
- sudo add-apt-repository -y ppa:pi-rho/dev
10-
- sudo apt-get update
11-
- sudo apt-get install -y tmux=1.9a-1~ppa1~p
9+
- VERSION=2.5
10+
- sudo apt-get -y remove tmux
11+
- sudo apt-get -y install wget tar libevent-dev libncurses-dev make
12+
- wget https://github.com/tmux/tmux/releases/download/${VERSION}/tmux-${VERSION}.tar.gz
13+
- tar xf tmux-${VERSION}.tar.gz
14+
- rm -f tmux-${VERSION}.tar.gz
15+
- cd tmux-${VERSION}
16+
- ./configure
17+
- make
18+
- sudo make install
19+
- cd -
20+
- sudo rm -rf /usr/local/src/tmux-*
21+
- sudo mv tmux-${VERSION} /usr/local/src
1222

1323
# override PS1 and irb prompt, fetch a git repo used for testing
1424
before_script:

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
[![Build Status](https://travis-ci.org/tmux-plugins/tmux-copycat.png?branch=master)](https://travis-ci.org/tmux-plugins/tmux-copycat)
44

5-
**NOTE**: the changes in tmux 2.4 broke this plugin. I'm working on a fix. For
6-
now the plugin works on tmux 2.3.
7-
85
This plugin enables:
96

107
- regex searches

Vagrantfile

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
VAGRANTFILE_API_VERSION = '2'
22

33
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
4-
5-
config.vm.define :ubuntu do |ubuntu|
4+
config.vm.define :ubuntu_two_five do |ubuntu|
65
ubuntu.vm.box = 'hashicorp/precise32'
7-
ubuntu.vm.provision 'shell', path: 'vagrant_ubuntu_provisioning.sh'
8-
end
9-
10-
config.vm.define :centos do |centos|
11-
centos.vm.box = 'chef/centos-6.5'
12-
centos.vm.provision 'shell', path: 'vagrant_centos_provisioning.sh'
6+
ubuntu.vm.provision 'shell', path: 'vagrant_ubuntu_provisioning_two_five.sh'
137
end
14-
158
end

run-tests

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ run_tests_on_vm() {
5959
}
6060

6161
main() {
62-
run_tests_on_vm "ubuntu"
63-
run_tests_on_vm "centos"
62+
run_tests_on_vm "ubuntu_two_five"
6463

6564
exit_message "$EXIT_VALUE"
6665
exit "$EXIT_VALUE"

scripts/copycat_jump.sh

Lines changed: 12 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -114,28 +114,14 @@ _copycat_enter_mode() {
114114

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

126120
# "manually" go up in the scrollback for a number of lines
127121
_copycat_manually_go_up() {
128122
local line_number="$1"
129-
if [ "$TMUX_COPY_MODE" == "vi" ]; then
130-
# vi copy mode
131-
tmux send-keys "$line_number" k 0
132-
else
133-
# emacs copy mode
134-
for (( c=1; c<="$line_number"; c++ )); do
135-
tmux send-keys C-p
136-
done
137-
tmux send-keys C-a
138-
fi
123+
tmux send-keys -X -N "$line_number" cursor-up
124+
tmux send-keys -X start-of-line
139125
}
140126

141127
_copycat_create_padding_below_result() {
@@ -156,32 +142,17 @@ _copycat_create_padding_below_result() {
156142
return
157143
fi
158144

159-
if [ "$TMUX_COPY_MODE" == "vi" ]; then
160-
# vi copy mode
161-
tmux send-keys "$padding" j "$padding" k
162-
else
163-
# emacs copy mode
164-
for (( c=1; c<="$padding"; c++ )); do
165-
tmux send-keys C-n
166-
done
167-
for (( c=1; c<="$padding"; c++ )); do
168-
tmux send-keys C-p
169-
done
170-
fi
145+
tmux send-keys -X -N "$padding" cursor-down
146+
tmux send-keys -X -N "$padding" cursor-up
171147
}
172148

173149
# performs a jump to go to line
174150
_copycat_go_to_line_with_jump() {
175151
local line_number="$1"
176152
# first jumps to the "bottom" in copy mode so that jumps are consistent
177-
if [ "$TMUX_COPY_MODE" == "vi" ]; then
178-
# vi copy mode
179-
tmux send-keys G 0 :
180-
else
181-
# emacs copy mode
182-
tmux send-keys "M->" C-a g
183-
fi
184-
tmux send-keys "$line_number" C-m
153+
tmux send-keys -X history-bottom
154+
tmux send-keys -X start-of-line
155+
tmux send-keys -X goto-line $line_number
185156
}
186157

187158
# maximum line number that can be reached via tmux 'jump'
@@ -234,32 +205,16 @@ _copycat_position_to_match_start() {
234205
local match_line_position="$1"
235206
[ "$match_line_position" -eq "0" ] && return 0
236207

237-
if [ "$TMUX_COPY_MODE" == "vi" ]; then
238-
# vi copy mode
239-
tmux send-keys "$match_line_position" l
240-
else
241-
# emacs copy mode
242-
# emacs doesn't have repeat, so we're manually looping :(
243-
for (( c=1; c<="$match_line_position"; c++ )); do
244-
tmux send-keys C-f
245-
done
246-
fi
208+
tmux send-keys -X -N "$match_line_position" cursor-right
247209
}
248210

249211
_copycat_select() {
250212
local match="$1"
251213
local length="${#match}"
214+
tmux send-keys -X begin-selection
215+
tmux send-keys -X -N "$length" cursor-right
252216
if [ "$TMUX_COPY_MODE" == "vi" ]; then
253-
# vi copy mode
254-
tmux send-keys Space "$length" l h # selection correction for 1 char
255-
else
256-
# emacs copy mode
257-
tmux send-keys C-Space
258-
# emacs doesn't have repeat, so we're manually looping :(
259-
for (( c=1; c<="$length"; c++ )); do
260-
tmux send-keys C-f
261-
done
262-
# NO selection correction for emacs mode
217+
tmux send-keys -X cursor-left # selection correction for 1 char
263218
fi
264219
}
265220

scripts/copycat_mode_bindings.sh

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,35 @@
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

14-
# 1. 'key' is sent to tmux. This ensures the default key action is done.
19+
# 1. 'cmd' or 'key' is sent to tmux. This ensures the default key action is done.
1520
# 2. Script is executed.
1621
# 3. `true` command ensures an exit status 0 is returned. This ensures a
1722
# user never gets an error msg - even if the script file from step 2 is
1823
# deleted.
19-
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
30+
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"
2035
}
2136

2237
copycat_cancel_bindings() {

scripts/copycat_mode_quit.sh

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

2020
unbind_all_bindings() {
21-
unbind_cancel_bindings
22-
unbind_prev_next_bindings
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
2325
}
2426

2527
main() {

scripts/helpers.sh

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ tmux_copy_mode() {
4141
tmux show-option -gwv mode-keys
4242
}
4343

44+
tmux_copy_mode_string() {
45+
if [ $(tmux_copy_mode) == 'vi' ]; then
46+
echo copy-mode-vi
47+
else
48+
echo copy-mode
49+
fi
50+
}
51+
4452
# === copycat mode specific helpers ===
4553

4654
set_copycat_mode() {
@@ -150,11 +158,11 @@ copycat_prev_key() {
150158

151159
# function expected output: 'C-c Enter q'
152160
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" |
161+
local commands_that_quit_copy_mode="cancel"
162+
local copy_mode="$(tmux_copy_mode_string)"
163+
tmux list-keys -T "$copy_mode" |
156164
\grep "$commands_that_quit_copy_mode" |
157-
$AWK_CMD '{ print $4}' |
165+
$AWK_CMD '{ print $4 }' |
158166
sort -u |
159167
sed 's/C-j//g' |
160168
xargs echo

test/helpers/expect_copycat_helpers.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ proc tmux_ctrl_t {} {
3737

3838
proc search {text} {
3939
send "/"
40-
sleep 0.2
40+
sleep 0.5
4141
send "$text\r"
4242
sleep 0.7
4343
}

0 commit comments

Comments
 (0)