# List of plugins
set -g @plugin 'tmux-plugins/tpm'

###############################################################################
###############################################################################
###                        general config                                   ###
###############################################################################
###############################################################################

set -g @plugin 'tmux-plugins/tmux-sensible'

bind r source-file ~/.tmux.conf \; display "Reloaded!" # Reload with ctrl-r
set -sg escape-time 1 # quicker responses

set -g base-index 1 # Numbering of windows
setw -g pane-base-index 1 # Numbering of Panes

set -g mouse on
set-option -gw xterm-keys on

bind-key -n C-t new-window
bind-key -n C-n new-window -c "#{pane_current_path}"

bind '"' split-window -v -c "#{pane_current_path}"
bind % split-window -h -c "#{pane_current_path}"

### set-environment -g PATH "$HOMEBREW_PREFIX/bin:$PATH"
bind C-o popup -E -E -d "#{pane_current_path}" -w 90% -h 90% "opencode"

bind-key -n C-S-Down next-window
bind-key -n C-S-Up previous-window
bind-key -n C-S-Left swap-window -t -1\; select-window -t -1
bind-key -n C-S-Right swap-window -t +1\; select-window -t +1

setw -g mode-keys vi
bind-key -T copy-mode-vi v send-keys -X begin-selection
bind-key -T copy-mode-vi y send-keys -X copy-selection-and-cancel

set -g set-clipboard on
set -g focus-events on

# Increase scrollback buffer size
set -g history-limit 50000

# Improve terminal scrolling
set -ga terminal-overrides ',*:smcup@:rmcup@'

set-option -g default-shell $SHELL
set-option -g allow-rename off

set-option -sa terminal-features ',alacritty:RGB'
set-option -ga terminal-features ",alacritty:usstyle"
set-option -ga terminal-overrides ',alacritty:Tc'
set -g default-terminal "alacritty"

###############################################################################
###############################################################################
###                   theme and misc plugins                                ###
###############################################################################
###############################################################################

set -g @plugin 'rose-pine/tmux'
set -g @rose_pine_variant 'main' # Options are 'main', 'moon' or 'dawn'

set -g @rose_pine_date_time '%Y-%m-%d %H:%M '
set -g @rose_pine_directory 'on'
set -g @rose_pine_disable_active_window_menu 'on'

set -g @plugin 'MunifTanjim/tmux-mode-indicator'
set -g @rose_pine_status_left_prepend_section '#{tmux_mode_indicator}'

# bind F5 to theme switch
bind-key . run-shell "$DOTFILES/scripts/hcf/rose_pine_switch"

set -g @plugin 'ofirgall/tmux-window-name'

set -g @tmux_window_name_substitute_sets "[('.+bash', 'bash'), ('cbonsai.+', 'cbonsai')]"
set -g @tmux_window_name_shells "['bash', 'fish', 'sh', 'zsh']"
set -g @tmux_window_dir_programs "['nvim', 'vim', 'vi', 'git', 'cbonsai']"
set -g @tmux_window_name_use_tilde "True"
set -g @tmux_window_name_max_name_len "100"

set -g @plugin 'tmux-plugins/tmux-open'

set -g @plugin 'pschmitt/tmux-ssh-split'
set-option -g @ssh-split-keep-cwd "true"
set-option -g @ssh-split-keep-remote-cwd "true"
set-option -g @ssh-split-fail "true"
set-option -g @ssh-split-no-env "false"
set-option -g @ssh-split-no-shell "false"
set-option -g @ssh-split-strip-cmd "true"
set-option -g @ssh-split-verbose "true"
set-option -g @ssh-split-debug "false"

set-option -g @ssh-split-h-key "V"
set-option -g @ssh-split-v-key 'H'
set-option -g @ssh-split-w-key "N"
set-option -g @ssh-split-r-key "R"

###############################################################################
###############################################################################
###                           tmux-continuum                                ###
###############################################################################
###############################################################################

set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'

set -g @resurrect-capture-pane-contents 'on'
set -g @resurrect-dir '~/.tmux/resurrect/'

set -g @continuum-save-interval '15'
set -g @continuum-restore 'on'
set -g @continuum-boot 'on'

### tmux-resurrect
set -g @resurrect-save 'S'     # prefix + Shift-s - save
set -g @resurrect-restore 'R'  # prefix + Shift-r - restore
# for neovim
set -g @resurrect-strategy-nvim 'session'

# Platform-specific settings
if-shell "uname | grep -q Darwin" {
  # macOS specific settings
  set -g @continuum-boot-options 'alacritty,fullscreen'
  set -g default-command "reattach-to-user-namespace -l $SHELL"
} {
  # Linux specific settings
  set -g @continuum-boot-options 'alacritty,fullscreen'
}

###############################################################################
###############################################################################
###                 Nvim-tmux-navigation setup                              ###
###############################################################################
###############################################################################

# decide whether we're in a Vim process
is_vim="~/.is_vim.sh '#{pane_tty}'"

bind-key -n 'S-Left' if-shell "$is_vim" 'send-keys S-Left' 'select-pane -L'
bind-key -n 'S-Down' if-shell "$is_vim" 'send-keys S-Down' 'select-pane -D'
bind-key -n 'S-Up' if-shell "$is_vim" 'send-keys S-Up' 'select-pane -U'
bind-key -n 'S-Right' if-shell "$is_vim" 'send-keys S-Right' 'select-pane -R'

tmux_version='$(tmux -V | sed -En "s/^tmux ([0-9]+(.[0-9]+)?).*/\1/p")'

if-shell -b '[ "$(echo "$tmux_version < 3.0" | bc)" = 1 ]' \
    "bind-key -n 'C-\\' if-shell \"$is_vim\" 'send-keys C-\\'  'select-pane -l'"
if-shell -b '[ "$(echo "$tmux_version >= 3.0" | bc)" = 1 ]' \
    "bind-key -n 'C-\\' if-shell \"$is_vim\" 'send-keys C-\\\\'  'select-pane -l'"

bind-key -n 'C-Space' if-shell "$is_vim" 'send-keys C-Space' 'select-pane -t:.+'

bind-key -T copy-mode-vi 'S-Left' select-pane -L
bind-key -T copy-mode-vi 'S-Down' select-pane -D
bind-key -T copy-mode-vi 'S-Up' select-pane -U
bind-key -T copy-mode-vi 'S-Right' select-pane -R
bind-key -T copy-mode-vi 'C-\' select-pane -l
bind-key -T copy-mode-vi 'C-Space' select-pane -t:.+

run '~/.tmux/plugins/tpm/tpm'
