Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: uses TMUX_CMD_PATH to handle multiple tmux installation #148

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 28 additions & 25 deletions scripts/copy_line.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,90 +6,93 @@ TMUX_COPY_MODE=""

REMOTE_SHELL_WAIT_TIME="0.4"

# shellcheck source=scripts/tmux_cmd_path.sh
source "$CURRENT_DIR/tmux_cmd_path.sh"

# shellcheck source=scripts/helpers.sh
source "${HELPERS_DIR}/helpers.sh"

# sets a TMUX_COPY_MODE that is used as a global variable
get_tmux_copy_mode() {
TMUX_COPY_MODE="$(tmux show-option -gwv mode-keys)"
TMUX_COPY_MODE="$($TMUX_CMD_PATH show-option -gwv mode-keys)"
}

# The command when on ssh with latency. To make it work in this case too,
# sleep is added.
add_sleep_for_remote_shells() {
local pane_command
pane_command="$(tmux display-message -p '#{pane_current_command}')"
pane_command="$($TMUX_CMD_PATH display-message -p '#{pane_current_command}')"
if [[ $pane_command =~ (ssh|mosh) ]]; then
sleep "$REMOTE_SHELL_WAIT_TIME"
fi
}

go_to_the_beginning_of_current_line() {
if [ "$(shell_mode)" == "emacs" ]; then
tmux send-key 'C-a'
$TMUX_CMD_PATH send-key 'C-a'
else
tmux send-key 'Escape' '0'
$TMUX_CMD_PATH send-key 'Escape' '0'
fi
}

enter_tmux_copy_mode() {
tmux copy-mode
$TMUX_CMD_PATH copy-mode
}

start_tmux_selection() {
if tmux_is_at_least 2.4; then
tmux send -X begin-selection
$TMUX_CMD_PATH send -X begin-selection
elif [ "$TMUX_COPY_MODE" == "vi" ]; then
# vi copy mode
tmux send-key 'Space'
$TMUX_CMD_PATH send-key 'Space'
else
# emacs copy mode
tmux send-key 'C-Space'
$TMUX_CMD_PATH send-key 'C-Space'
fi
}

# works when command spans accross multiple lines
end_of_line_in_copy_mode() {
if tmux_is_at_least 2.4; then
tmux send -X -N 150 'cursor-down' # 'down' key. 'vi' mode is faster so we're
$TMUX_CMD_PATH send -X -N 150 'cursor-down' # 'down' key. 'vi' mode is faster so we're
# jumping more lines than emacs.
tmux send -X 'end-of-line' # End of line (just in case we are already at the last line).
tmux send -X 'previous-word' # Beginning of the previous word.
tmux send -X 'next-word-end' # End of next word.
$TMUX_CMD_PATH send -X 'end-of-line' # End of line (just in case we are already at the last line).
$TMUX_CMD_PATH send -X 'previous-word' # Beginning of the previous word.
$TMUX_CMD_PATH send -X 'next-word-end' # End of next word.
elif [ "$TMUX_COPY_MODE" == "vi" ]; then
# vi copy mode
# This sequence of keys consistently selects multiple lines
tmux send-key '150' # Go to the bottom of scrollback buffer by using
tmux send-key 'j' # 'down' key. 'vi' mode is faster so we're
$TMUX_CMD_PATH send-key '150' # Go to the bottom of scrollback buffer by using
$TMUX_CMD_PATH send-key 'j' # 'down' key. 'vi' mode is faster so we're
# jumping more lines than emacs.
tmux send-key '$' # End of line (just in case we are already at the last line).
tmux send-key 'b' # Beginning of the previous word.
tmux send-key 'e' # End of next word.
$TMUX_CMD_PATH send-key '$' # End of line (just in case we are already at the last line).
$TMUX_CMD_PATH send-key 'b' # Beginning of the previous word.
$TMUX_CMD_PATH send-key 'e' # End of next word.
else
# emacs copy mode
for ((c = 1; c <= '30'; c++)); do # go to the bottom of scrollback buffer
tmux send-key 'C-n'
$TMUX_CMD_PATH send-key 'C-n'
done
tmux send-key 'C-e'
tmux send-key 'M-b'
tmux send-key 'M-f'
$TMUX_CMD_PATH send-key 'C-e'
$TMUX_CMD_PATH send-key 'M-b'
$TMUX_CMD_PATH send-key 'M-f'
fi
}

yank_to_clipboard() {
if tmux_is_at_least 2.4; then
# shellcheck disable=SC2119
tmux send -X copy-pipe-and-cancel "$(clipboard_copy_command)"
$TMUX_CMD_PATH send -X copy-pipe-and-cancel "$(clipboard_copy_command)"
else
tmux send-key "$(yank_wo_newline_key)"
$TMUX_CMD_PATH send-key "$(yank_wo_newline_key)"
fi
}

go_to_the_end_of_current_line() {
if [ "$(shell_mode)" == "emacs" ]; then
tmux send-keys 'C-e'
$TMUX_CMD_PATH send-keys 'C-e'
else
tmux send-keys '$' 'a'
$TMUX_CMD_PATH send-keys '$' 'a'
fi
}

Expand Down
7 changes: 5 additions & 2 deletions scripts/copy_pane_pwd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HELPERS_DIR="$CURRENT_DIR"

# shellcheck source=scripts/tmux_cmd_path.sh
source "$CURRENT_DIR/tmux_cmd_path.sh"

# shellcheck source=scripts/helpers.sh
source "${HELPERS_DIR}/helpers.sh"

pane_current_path() {
tmux display -p -F "#{pane_current_path}"
$TMUX_CMD_PATH display -p -F "#{pane_current_path}"
}

display_notice() {
Expand All @@ -22,7 +25,7 @@ main() {
payload="$(pane_current_path | tr -d '\n')"
# $copy_command below should not be quoted
echo "$payload" | $copy_command
tmux set-buffer "$payload"
$TMUX_CMD_PATH set-buffer "$payload"
display_notice
}
main
15 changes: 10 additions & 5 deletions scripts/helpers.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#!bash
# shellcheck disable=SC2239

CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# shellcheck source=scripts/tmux_cmd_path.sh
source "$CURRENT_DIR/tmux_cmd_path.sh"

yank_line="y"
yank_line_option="@yank_line"

Expand Down Expand Up @@ -45,7 +50,7 @@ get_tmux_option() {
local option="$1"
local default_value="$2"
local option_value
option_value=$(tmux show-option -gqv "$option")
option_value=$($TMUX_CMD_PATH show-option -gqv "$option")
if [ -z "$option_value" ]; then
echo "$default_value"
else
Expand Down Expand Up @@ -121,13 +126,13 @@ display_message() {
saved_display_time=$(get_tmux_option "display-time" "750")

# sets message display time to 5 seconds
tmux set-option -gq display-time "$display_duration"
$TMUX_CMD_PATH set-option -gq display-time "$display_duration"

# displays message
tmux display-message "$message"
$TMUX_CMD_PATH display-message "$message"

# restores original 'display-time' value
tmux set-option -gq display-time "$saved_display_time"
$TMUX_CMD_PATH set-option -gq display-time "$saved_display_time"
}

command_exists() {
Expand Down Expand Up @@ -174,7 +179,7 @@ clipboard_copy_command() {
}

# Cache the TMUX version for speed.
tmux_version="$(tmux -V | cut -d ' ' -f 2 | sed 's/next-//')"
tmux_version="$($TMUX_CMD_PATH -V | cut -d ' ' -f 2 | sed 's/next-//')"

tmux_is_at_least() {
if [[ $tmux_version == "$1" ]] || [[ $tmux_version == master ]]; then
Expand Down
19 changes: 19 additions & 0 deletions scripts/tmux_cmd_path.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

# Written by michaellee8 <[email protected]> in Nov 2020 for
# https://github.com/tmux-plugins/tpm/issues/189 to prevent problems caused by
# tmux version mismatch between client and server. This script is
# licensed in public domain.

# Try to get the process ID of the running tmux server,
# would be empty if not found
# shellcheck disable=SC2009
TMUX_SERVER_PID=$(ps -eo pid=,comm= | grep 'tmux: server' | awk '{ print $1; }')

if [[ -n "$TMUX_SERVER_PID" ]]; then
TMUX_CMD_PATH=$(realpath "/proc/$TMUX_SERVER_PID/exe" 2> /dev/null || echo "tmux" | sed -z '$ s/\n$//')
else
TMUX_CMD_PATH='tmux'
fi

export TMUX_CMD_PATH
55 changes: 29 additions & 26 deletions yank.tmux
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPTS_DIR="${CURRENT_DIR}/scripts"
HELPERS_DIR="${CURRENT_DIR}/scripts"

# shellcheck source=scripts/tmux_cmd_path.sh
source "$SCRIPTS_DIR/tmux_cmd_path.sh"

# shellcheck source=scripts/helpers.sh
source "${HELPERS_DIR}/helpers.sh"

Expand All @@ -17,11 +20,11 @@ set_error_bindings() {
key_bindings="$(yank_key) $(put_key) $(yank_put_key)"
for key in $key_bindings; do
if tmux_is_at_least 2.4; then
tmux bind-key -T copy-mode-vi "$key" send-keys -X copy-pipe-and-cancel "tmux display-message 'Error! tmux-yank dependencies not installed!'"
tmux bind-key -T copy-mode "$key" send-keys -X copy-pipe-and-cancel "tmux display-message 'Error! tmux-yank dependencies not installed!'"
$TMUX_CMD_PATH bind-key -T copy-mode-vi "$key" send-keys -X copy-pipe-and-cancel "$TMUX_CMD_PATH display-message 'Error! tmux-yank dependencies not installed!'"
$TMUX_CMD_PATH bind-key -T copy-mode "$key" send-keys -X copy-pipe-and-cancel "$TMUX_CMD_PATH display-message 'Error! tmux-yank dependencies not installed!'"
else
tmux bind-key -t vi-copy "$key" copy-pipe "tmux display-message 'Error! tmux-yank dependencies not installed!'"
tmux bind-key -t emacs-copy "$key" copy-pipe "tmux display-message 'Error! tmux-yank dependencies not installed!'"
$TMUX_CMD_PATH bind-key -t vi-copy "$key" copy-pipe "$TMUX_CMD_PATH display-message 'Error! tmux-yank dependencies not installed!'"
$TMUX_CMD_PATH bind-key -t emacs-copy "$key" copy-pipe "$TMUX_CMD_PATH display-message 'Error! tmux-yank dependencies not installed!'"
fi
done
}
Expand All @@ -43,43 +46,43 @@ set_copy_mode_bindings() {
local copy_command_mouse
copy_command_mouse="$(clipboard_copy_command "true")"
if tmux_is_at_least 2.4; then
tmux bind-key -T copy-mode-vi "$(yank_key)" send-keys -X "$(yank_action)" "$copy_command"
tmux bind-key -T copy-mode-vi "$(put_key)" send-keys -X copy-pipe-and-cancel "tmux paste-buffer"
tmux bind-key -T copy-mode-vi "$(yank_put_key)" send-keys -X copy-pipe-and-cancel "$copy_command; tmux paste-buffer"
tmux bind-key -T copy-mode-vi "$(yank_wo_newline_key)" send-keys -X "$(yank_action)" "$copy_wo_newline_command"
$TMUX_CMD_PATH bind-key -T copy-mode-vi "$(yank_key)" send-keys -X "$(yank_action)" "$copy_command"
$TMUX_CMD_PATH bind-key -T copy-mode-vi "$(put_key)" send-keys -X copy-pipe-and-cancel "$TMUX_CMD_PATH paste-buffer"
$TMUX_CMD_PATH bind-key -T copy-mode-vi "$(yank_put_key)" send-keys -X copy-pipe-and-cancel "$copy_command; $TMUX_CMD_PATH paste-buffer"
$TMUX_CMD_PATH bind-key -T copy-mode-vi "$(yank_wo_newline_key)" send-keys -X "$(yank_action)" "$copy_wo_newline_command"
if [[ "$(yank_with_mouse)" == "on" ]]; then
tmux bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X "$(yank_action)" "$copy_command_mouse"
$TMUX_CMD_PATH bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X "$(yank_action)" "$copy_command_mouse"
fi

tmux bind-key -T copy-mode "$(yank_key)" send-keys -X "$(yank_action)" "$copy_command"
tmux bind-key -T copy-mode "$(put_key)" send-keys -X copy-pipe-and-cancel "tmux paste-buffer"
tmux bind-key -T copy-mode "$(yank_put_key)" send-keys -X copy-pipe-and-cancel "$copy_command; tmux paste-buffer"
tmux bind-key -T copy-mode "$(yank_wo_newline_key)" send-keys -X "$(yank_action)" "$copy_wo_newline_command"
$TMUX_CMD_PATH bind-key -T copy-mode "$(yank_key)" send-keys -X "$(yank_action)" "$copy_command"
$TMUX_CMD_PATH bind-key -T copy-mode "$(put_key)" send-keys -X copy-pipe-and-cancel "$TMUX_CMD_PATH paste-buffer"
$TMUX_CMD_PATH bind-key -T copy-mode "$(yank_put_key)" send-keys -X copy-pipe-and-cancel "$copy_command; $TMUX_CMD_PATH paste-buffer"
$TMUX_CMD_PATH bind-key -T copy-mode "$(yank_wo_newline_key)" send-keys -X "$(yank_action)" "$copy_wo_newline_command"
if [[ "$(yank_with_mouse)" == "on" ]]; then
tmux bind-key -T copy-mode MouseDragEnd1Pane send-keys -X "$(yank_action)" "$copy_command_mouse"
$TMUX_CMD_PATH bind-key -T copy-mode MouseDragEnd1Pane send-keys -X "$(yank_action)" "$copy_command_mouse"
fi
else
tmux bind-key -t vi-copy "$(yank_key)" copy-pipe "$copy_command"
tmux bind-key -t vi-copy "$(put_key)" copy-pipe "tmux paste-buffer"
tmux bind-key -t vi-copy "$(yank_put_key)" copy-pipe "$copy_command; tmux paste-buffer"
tmux bind-key -t vi-copy "$(yank_wo_newline_key)" copy-pipe "$copy_wo_newline_command"
$TMUX_CMD_PATH bind-key -t vi-copy "$(yank_key)" copy-pipe "$copy_command"
$TMUX_CMD_PATH bind-key -t vi-copy "$(put_key)" copy-pipe "$TMUX_CMD_PATH paste-buffer"
$TMUX_CMD_PATH bind-key -t vi-copy "$(yank_put_key)" copy-pipe "$copy_command; $TMUX_CMD_PATH paste-buffer"
$TMUX_CMD_PATH bind-key -t vi-copy "$(yank_wo_newline_key)" copy-pipe "$copy_wo_newline_command"
if [[ "$(yank_with_mouse)" == "on" ]]; then
tmux bind-key -t vi-copy MouseDragEnd1Pane copy-pipe "$copy_command_mouse"
$TMUX_CMD_PATH bind-key -t vi-copy MouseDragEnd1Pane copy-pipe "$copy_command_mouse"
fi

tmux bind-key -t emacs-copy "$(yank_key)" copy-pipe "$copy_command"
tmux bind-key -t emacs-copy "$(put_key)" copy-pipe "tmux paste-buffer"
tmux bind-key -t emacs-copy "$(yank_put_key)" copy-pipe "$copy_command; tmux paste-buffer"
tmux bind-key -t emacs-copy "$(yank_wo_newline_key)" copy-pipe "$copy_wo_newline_command"
$TMUX_CMD_PATH bind-key -t emacs-copy "$(yank_key)" copy-pipe "$copy_command"
$TMUX_CMD_PATH bind-key -t emacs-copy "$(put_key)" copy-pipe "$TMUX_CMD_PATH paste-buffer"
$TMUX_CMD_PATH bind-key -t emacs-copy "$(yank_put_key)" copy-pipe "$copy_command; $TMUX_CMD_PATH paste-buffer"
$TMUX_CMD_PATH bind-key -t emacs-copy "$(yank_wo_newline_key)" copy-pipe "$copy_wo_newline_command"
if [[ "$(yank_with_mouse)" == "on" ]]; then
tmux bind-key -t emacs-copy MouseDragEnd1Pane copy-pipe "$copy_command_mouse"
$TMUX_CMD_PATH bind-key -t emacs-copy MouseDragEnd1Pane copy-pipe "$copy_command_mouse"
fi
fi
}

set_normal_bindings() {
tmux bind-key "$(yank_line_key)" run-shell -b "$SCRIPTS_DIR/copy_line.sh"
tmux bind-key "$(yank_pane_pwd_key)" run-shell -b "$SCRIPTS_DIR/copy_pane_pwd.sh"
$TMUX_CMD_PATH bind-key "$(yank_line_key)" run-shell -b "$SCRIPTS_DIR/copy_line.sh"
$TMUX_CMD_PATH bind-key "$(yank_pane_pwd_key)" run-shell -b "$SCRIPTS_DIR/copy_pane_pwd.sh"
}

main() {
Expand Down