# NOTE: If the Alt+x shortcut keys don't work, add this to your ~/.Xresources:
# XTerm*.metaSendsEscape: true
# UXTerm*.metaSendsEscape: true
#
# Add this to your ~/.Xresources for clipboard integration:
#  XTerm*.disallowedWindowOps:	20,21,SetXprop
# UXTerm*.disallowedWindowOps:	20,21,SetXprop
#
# Equivelent to enable clipboard integration in Cygwin terminal:
#  Right click on titlebar -> Options -> Selection -> Allow setting selection

# Reload config:
bind-key -n M-C-r source-file ~/.tmux.conf

# Select panes:
bind-key -n M-h select-pane -L
bind-key -n M-j select-pane -D
bind-key -n M-k select-pane -U
bind-key -n M-l select-pane -R

# Move panes:
# These aren't quite what I want, as they *swap* panes *numerically* instead of
# *moving* the pane in a specified *direction*, but they will do for now.
bind-key -n M-H swap-pane -U
bind-key -n M-J swap-pane -D
bind-key -n M-K swap-pane -U
bind-key -n M-L swap-pane -D

# Resize panes (Note: Ctrl+Alt+L conflicts with the lock screen shortcut in
# many environments - you may want to consider the below alternative shortcuts
# for resizing instead):
bind-key -n M-C-h resize-pane -L
bind-key -n M-C-j resize-pane -D
bind-key -n M-C-k resize-pane -U
bind-key -n M-C-l resize-pane -R

# Alternative resize panes keys without ctrl+alt+l conflict:
# bind-key -n M-C-Left resize-pane -L
# bind-key -n M-C-Down resize-pane -D
# bind-key -n M-C-Up resize-pane -U
# bind-key -n M-C-Right resize-pane -R

# Quickly size pane to 81 columns
bind-key -n M-C-i resize-pane -x 81

# Split + spawn new shell:
# I would have used enter like wmii, but xterm already uses that, so I use the
# key next to it. The ultra useful default-path feature was dropped in tmux 1.9
# (why?), so explicitly set the working directory to the current directory,
# which should have the same effect.
# CHERE_INVOKING is to prevent cygwin from immediately changing back to $HOME
bind-key -n M-"'" split-window -v -c "#{pane_current_path}" "/usr/bin/env CHERE_INVOKING=1 $SHELL -l"
bind-key -n M-'"' split-window -h -c "#{pane_current_path}" "/usr/bin/env CHERE_INVOKING=1 $SHELL -l"

# Pane layouts (these use the same shortcut keys as wmii for similar actions,
# but don't really mirror it's behaviour):
bind-key -n M-d select-layout tiled
bind-key -n M-s select-layout main-vertical \; swap-pane -s 0
bind-key -n M-m select-layout main-horizontal \; swap-pane -s 0

# We can't use shift+PageUp, so use Alt+PageUp then release Alt to keep
# scrolling:
bind-key -n M-PageUp copy-mode -u

# Give us lots of scrollback history:
set-option -g history-limit 10000

## Don't interfere with vi keybindings:
# FIXME: 0 seems to cause a problem on Win11 WSL terminal, echoing random characters when launching tmux... 1 seems ok
#set-option -s escape-time 0
set-option -s escape-time 1

# Window navigation (Oh, how I would like a for loop right now...):
bind-key -n M-0 if-shell "tmux list-windows|grep ^0" "select-window -t 0" "new-window -t 0 -c '#{pane_current_path}' /usr/bin/env CHERE_INVOKING=1 $SHELL -l"
bind-key -n M-1 if-shell "tmux list-windows|grep ^1" "select-window -t 1" "new-window -t 1 -c '#{pane_current_path}' /usr/bin/env CHERE_INVOKING=1 $SHELL -l"
bind-key -n M-2 if-shell "tmux list-windows|grep ^2" "select-window -t 2" "new-window -t 2 -c '#{pane_current_path}' /usr/bin/env CHERE_INVOKING=1 $SHELL -l"
bind-key -n M-3 if-shell "tmux list-windows|grep ^3" "select-window -t 3" "new-window -t 3 -c '#{pane_current_path}' /usr/bin/env CHERE_INVOKING=1 $SHELL -l"
bind-key -n M-4 if-shell "tmux list-windows|grep ^4" "select-window -t 4" "new-window -t 4 -c '#{pane_current_path}' /usr/bin/env CHERE_INVOKING=1 $SHELL -l"
bind-key -n M-5 if-shell "tmux list-windows|grep ^5" "select-window -t 5" "new-window -t 5 -c '#{pane_current_path}' /usr/bin/env CHERE_INVOKING=1 $SHELL -l"
bind-key -n M-6 if-shell "tmux list-windows|grep ^6" "select-window -t 6" "new-window -t 6 -c '#{pane_current_path}' /usr/bin/env CHERE_INVOKING=1 $SHELL -l"
bind-key -n M-7 if-shell "tmux list-windows|grep ^7" "select-window -t 7" "new-window -t 7 -c '#{pane_current_path}' /usr/bin/env CHERE_INVOKING=1 $SHELL -l"
bind-key -n M-8 if-shell "tmux list-windows|grep ^8" "select-window -t 8" "new-window -t 8 -c '#{pane_current_path}' /usr/bin/env CHERE_INVOKING=1 $SHELL -l"
bind-key -n M-9 if-shell "tmux list-windows|grep ^9" "select-window -t 9" "new-window -t 9 -c '#{pane_current_path}' /usr/bin/env CHERE_INVOKING=1 $SHELL -l"

# Window moving (the sleep 0.1 here is a hack, anyone know a better way?):
bind-key -n M-')' if-shell "tmux list-windows|grep ^0" "join-pane -d -t :0" "new-window -d -t 0 'sleep 0.1' \; join-pane -d -t :0"
bind-key -n M-'!' if-shell "tmux list-windows|grep ^1" "join-pane -d -t :1" "new-window -d -t 1 'sleep 0.1' \; join-pane -d -t :1"
bind-key -n M-'@' if-shell "tmux list-windows|grep ^2" "join-pane -d -t :2" "new-window -d -t 2 'sleep 0.1' \; join-pane -d -t :2"
bind-key -n M-'#' if-shell "tmux list-windows|grep ^3" "join-pane -d -t :3" "new-window -d -t 3 'sleep 0.1' \; join-pane -d -t :3"
bind-key -n M-'$' if-shell "tmux list-windows|grep ^4" "join-pane -d -t :4" "new-window -d -t 4 'sleep 0.1' \; join-pane -d -t :4"
bind-key -n M-'%' if-shell "tmux list-windows|grep ^5" "join-pane -d -t :5" "new-window -d -t 5 'sleep 0.1' \; join-pane -d -t :5"
bind-key -n M-'^' if-shell "tmux list-windows|grep ^6" "join-pane -d -t :6" "new-window -d -t 6 'sleep 0.1' \; join-pane -d -t :6"
bind-key -n M-'&' if-shell "tmux list-windows|grep ^7" "join-pane -d -t :7" "new-window -d -t 7 'sleep 0.1' \; join-pane -d -t :7"
bind-key -n M-'*' if-shell "tmux list-windows|grep ^8" "join-pane -d -t :8" "new-window -d -t 8 'sleep 0.1' \; join-pane -d -t :8"
bind-key -n M-'(' if-shell "tmux list-windows|grep ^9" "join-pane -d -t :9" "new-window -d -t 9 'sleep 0.1' \; join-pane -d -t :9"

# Set default window number to 1 instead of 0 to make key combos easier:
set-option -g base-index 1

# Use vi style keybindings for copy-mode (if EDITOR or VISUAL are set this is
# unnecessary):
set-window-option -g mode-keys vi

# Enable mouse. Mostly to make selecting text within a pane not also grab pane
# borders or text from other panes. Unfortunately, tmux' mouse handling leaves
# something to be desired - no double/tripple click support to select a
# word/line, all mouse buttons are intercepted (middle click = I want to paste
# damnit!), no automatic X selection integration(*)...
#set-window-option -g mode-mouse on
#set-window-option -g mouse-select-pane on
#set-window-option -g mouse-resize-pane on
#set-window-option -g mouse-select-window on

# (*) This enables integration with the clipboard via termcap extensions. This
# relies on the terminal emulator passing this on to X, so you need to add this
# to your ~/.Xresources and run xrdb ~/.Xresources to reload it:
#
#  XTerm*.disallowedWindowOps:	20,21,SetXprop
# UXTerm*.disallowedWindowOps:	20,21,SetXprop
set-option -s set-clipboard on

# Handy shortcuts to make activity/silence signal a bell. Add
# '*.VT100.bellIsUrgent: true' to .XResources and reload with 'xrdb
# ~/.XResources' to cause this to set the urgent hint in xterm. i3 will then
# highlight the window & desktop when activitiy is present.
# NOTE: Monitoring in tmux only works when switched to a different window (alt+0)
# NOTE: If the activity notice / urgent hint is already set when the xterm is
#       switched away from, it will not be set again
bind-key -n M-a set-window-option bell-on-alert on \; set-window-option monitor-activity on
bind-key -n M-A set-window-option bell-on-alert on \; set-window-option monitor-silence 30

# Alt+y to copy pane to clipboard, Alt+Y to copy entire history to clipboard
if-shell -b "command -v clip > /dev/null" " \
	bind-key -n M-y run 'tmux capture-pane -p | clip'; \
	bind-key -n M-Y run 'tmux capture-pane -pS- | clip'"
if-shell -b "command -v xsel > /dev/null" " \
	bind-key -n M-y run 'tmux capture-pane -p | xsel -i'; \
	bind-key -n M-Y run 'tmux capture-pane -pS- | xsel -i'"

# Detach other clients:
bind-key -n M-d detach-client -a

# Set status line to cyan when starting/attaching to a session on a remote machine
# FIXME: How to detect focus changes outside of explicit attach? Hook on any activity? Seems excessive.
set-option -g update-environment SSH_CONNECTION
set-hook -g client-session-changed "if-shell 'test -n \"\$SSH_CONNECTION\"' \"set status-style bg=cyan,fg=black ; set pane-active-border-style fg=cyan\" \"set status-style bg=green,fg=black ; set pane-active-border-style fg=green\""