# ^s as prefix
unbind C-b
set -g prefix C-s
bind-key s send-prefix

# reload tmux.conf
bind-key T source-file "$HOME/.tmux.conf" \; display-message "source-file done"
unbind r
bind r source-file "$HOME/.tmux.conf"

# set -as terminal-overrides ',xterm*:smxx=\E[9m'
set -g default-terminal "screen-256color"

# sane defaults
set -g mouse on

# honestly not exactly sure?
# set -sg escape-time 1
set -g escape-time 0

# show window activity
# TODO: this needs to be properly setup - presumably with tmux-session-list
setw -g monitor-activity on
setw -g visual-bell on
setw -g bell-action other

# common window sizes
bind-key W resize-pane -x 100
bind-key f resize-pane -x 120
bind-key F resize-pane -x 125
bind-key 8 resize-pane -x 80
bind-key 2 resize-pane -y 20

# splitting
bind-key v split-window -h
bind-key b split-window
bind-key h split-window

# show tmux status bar by default with (C-s, H) to toggle it
set -g status on
set -g history-limit 102400000
bind-key H set -s status

# simple session switching
bind-key j switch-client -p
bind-key h switch-client -p
bind-key k switch-client -n
bind-key l switch-client -n
# bind s run-shell "tmux new-window sh -c '$DOTFILES_PATH/bin/tmuxswitcher'"
bind s run-shell "tmux new-window 'sh -c ~/.config/dotfiles/bin/tmuxswitcher'"

# reset the working directory of the current session
bind-key P attach-session -t . -c '#{pane_current_path}' \; display-message 'Reset session dir to #{pane_current_path}'

# status bar and messages
set -g status-position bottom
set -g status-bg colour0
set -g status-fg colour8
set -g status-interval 5
set -g status-right-length 80
set -g status-right "#[fg=colour4]#W#[fg=default]#[bg=default] #H"
set -g status-left-length 200
set -g status-left "#[fg=colour7]#(~/.config/dotfiles/bin/tmux-session-list #S)"
set -g message-style 'fg=colour7 bg=colour18'

bind-key O display-message "#(tmux-save-buffer #S)"

# pane split line colors
set -g pane-active-border-style bg=black,fg=blue
set -g pane-border-style bg=black,fg=colour18

# cLocK mOdE Is sUpEr 3LeEt
set -g clock-mode-colour colour8

# windows (which I don't use much...?)
# TODO: do I have a use case for windows?
setw -g window-status-current-format ''

# vim copying
setw -g mode-keys vi

# 1-based indexing for key binding simplicity
set -g base-index 1
set -g pane-base-index 1

# custom predefined layouts
bind M-l source-file ~/.tmux/layouts/dev.tmux

# present a menu of urls to open from the visible pane
# TODO: fzf-ify this
bind-key u capture-pane \;\
	save-buffer /tmp/tmux-buffer \;\
	split-window -l 10 "urlscan /tmp/tmux-buffer"

# seamlessly handle pane switching in and out of vim panes
# see GitHub repos tmux-plugins/vim-tmux-focus-events and christoomey/vim-tmux-navigator
is_vim="ps -o state= -o comm= -t '#{pane_tty}' \
	| grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"
bind-key -n C-h if-shell "$is_vim" "send-keys C-h" "select-pane -L"
bind-key -Tcopy-mode-vi 'C-h' send -X "cancel select-pane -L"
bind-key -n C-j if-shell "$is_vim" "send-keys C-j" "select-pane -D"
bind-key -n C-k if-shell "$is_vim" "send-keys C-k" "select-pane -U"
bind-key -n C-l if-shell "$is_vim" "send-keys C-l" "select-pane -R"
# not sure about this binding? possibly switch to previous pane...?
# bind-key -n C-\ if-shell "$is_vim" "send-keys C-\\" "select-pane -l"

# YANKING
bind-key -Tcopy-mode-vi 'y' send -X copy-selection

# list of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'

# prefix+[ for select mode
# v for visual mode
# yank to clipboard with y
# paste with prefix+]
set -g @plugin 'tmux-plugins/tmux-yank'

set -g @plugin 'tmux-plugins/tmux-copycat'
set -g @plugin 'tmux-plugins/tmux-sessionist'
set -g @plugin 'christoomey/vim-tmux-navigator'

# plugin options

# enable continuum
set -g @continuum-restore 'on'

# let continuum startup on boot
set -g @continuum-boot 'on'

# bindings for tmux-resurrect
set -g @resurrect-save 'C-v'
set -g @resurrect-restore 'R'

# initialize tmux plugin manager
run '$XDG_CONFIG_HOME/tmux/plugins/tpm/tpm'

# attempt at integrating nested tmux sessions nicely
# bind -T root F12 \
# 	set prefix None \;\
# 	set key-table off \;\
# 	set status off \
# 	if -F '#{pane_in_mode}' 'send-keys -X cancel' \;\
# 	refresh-client -S \;\

# bind -T off F12 \
# 	set -u prefix \;\
# 	set -u key-table \;\
# 	set status on \;\
# 	refresh-client -S

unbind-key C-p