################################################################################
#                                   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'
set -g @plugin 'catppuccin/tmux'

################################################################################
#                                   General                                    #
################################################################################

# Set shell to fish.
set -g default-shell /bin/fish

# Make window index start at 1.
set -g base-index 1

# Make pane index start at 1.
setw -g pane-base-index 1

# Automatically renumber windows when one is deleted.
set -g renumber-windows on

set -g default-terminal "tmux-256color"
set -ag terminal-overrides ",*:RGB"

set -g @continuum-boot 'on'
set -g @continuum-restore 'on'

################################################################################
#                                 Key Mappings                                 #
################################################################################

# Remap prefix.
unbind C-b
set-option -g prefix C-a
bind C-a send-prefix

# Easy reload config.
bind r source-file ~/.config/tmux/tmux.conf \; display-message "Reloaded ~/.config/tmux/tmux.conf"

# Better split commands.
bind -N 'vsplit' |  split-window -h -c "#{pane_current_path}"
bind -N 'vsplit' \\ split-window -h -c "#{pane_current_path}"
bind -N 'hsplit' -  split-window -v -c "#{pane_current_path}"
unbind '"'
unbind %

# Smart pane switching with awareness of vim/fzf.
IS_VIM="ps -o state= -o comm= -t '#{pane_tty}' | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"
IS_FZF="ps -o state= -o comm= -t '#{pane_tty}' | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?fzf$'"

bind -n -N 'Switch to pane (left)'  C-h if-shell "${IS_VIM} || ${IS_FZF}" 'send C-h' 'select-pane -L'
bind -n -N 'Switch to pane (down)'  C-j if-shell "${IS_VIM} || ${IS_FZF}" 'send C-j' 'select-pane -D'
bind -n -N 'Switch to pane (up)'    C-k if-shell "${IS_VIM} || ${IS_FZF}" 'send C-k' 'select-pane -U'
bind -n -N 'Switch to pane (right)' C-l if-shell "${IS_VIM} || ${IS_FZF}" 'send C-l' 'select-pane -R'

# Copy-mode.
bind -T copy-mode-vi -N 'Copy mode: switch to pane (left)'  C-h if-shell "${IS_VIM} || ${IS_FZF}" 'send C-h' 'select-pane -L'
bind -T copy-mode-vi -N 'Copy mode: Switch to pane (down)'  C-j if-shell "${IS_VIM} || ${IS_FZF}" 'send C-j' 'select-pane -D'
bind -T copy-mode-vn -N 'Copy mode: Switch to pane (up)'    C-k if-shell "${IS_VIM} || ${IS_FZF}" 'send C-k' 'select-pane -U'
bind -T copy-mode-vn -N 'Copy mode: Switch to pane (right)' C-l if-shell "${IS_VIM} || ${IS_FZF}" 'send C-l' 'select-pane -R'

# Vi-like resizing.
bind -r -N 'Resize pane (left)'  M-h resize-pane -L 5
bind -r -N 'Resize pane (down)'  M-j resize-pane -D 5
bind -r -N 'Resize pane (up)'    M-k resize-pane -U 5
bind -r -N 'Resize pane (right)' M-l resize-pane -R 5

# Even out panes.
bind -N 'Evenly distribute panes' = select-layout -E

# Swap panes.
bind -r -N 'Swap current pane with the next'     H swap-pane -U
bind -r -N 'Swap current pane with the previous' L swap-pane -D

# Window switching.
bind -N 'Previous window' Left previous-window
bind -N 'Next window'     Right next-window

# Popup.
unbind i
bind -N 'Popup shell' i if-shell -F '#{==:#{session_name},scratch}' {
    detach-client 
  } { 
    if-shell "tmux has-session -t scratch" {
      display-popup -E -h 90% -w 90% "tmux attach-session -t scratch"
    } {
      display-popup -E -h 90% -w 90% "tmux new-session -d -c '#{pane_current_path}' -s scratch && tmux set-option -t scratch status off && tmux attach-session -t scratch"
    }
}

# Vi copy mode.
setw -g mode-keys vi
set -g set-clipboard external

unbind [
bind -N 'Enter normal (copy) mode' Escape copy-mode

unbind p
bind p paste-buffer

bind -T copy-mode-vi v send -X begin-selection
bind -T copy-mode-vi y send -X copy-pipe-and-cancel 'copyq copy -'
bind -T copy-mode-vi ? command-prompt -p '?' 'send -X search-backward %1'
bind -T copy-mode-vi / command-prompt -p '/' 'send -X search-forward %1'
bind -T copy-mode-vi q send -X cancel
bind -T copy-mode-vi Escape if-shell -F '#{selection_present}' 'send -X clear-selection' 'send -X cancel'

################################################################################
#                                  Catppuccin                                  #
################################################################################

# catppuccin
set -g @catppuccin_flavour {{ .drapeau.colors.flavor | squote }}

set -g @catppuccin_window_left_separator ""
set -g @catppuccin_window_right_separator " "
set -g @catppuccin_window_middle_separator " █"
set -g @catppuccin_window_number_position "right"

set -g @catppuccin_window_default_fill "number"
set -g @catppuccin_window_default_text "#W"

set -g @catppuccin_window_current_fill "number"
set -g @catppuccin_window_current_text "#W"

set -g @catppuccin_status_modules_right "directory user host session"
set -g @catppuccin_status_left_separator  " "
set -g @catppuccin_status_right_separator ""
set -g @catppuccin_status_right_separator_inverse "no"
set -g @catppuccin_status_fill "icon"
set -g @catppuccin_status_connect_separator "no"

set -g @catppuccin_directory_text "#{pane_current_path}"
set -g @catppuccin_date_time_text "%b %d • %I:%M %p"

################################################################################
#                                     tpm                                      #
################################################################################

# Install tpm if not already installed.
if "test ! -d ~/.config/tmux/plugins/tpm" \
   "run 'git clone https://github.com/tmux-plugins/tpm ~/.config/tmux/plugins/tpm && ~/.config/tmux/plugins/tpm/bin/install_plugins'"

# Init tmux plugin manager.
# Must be very last line!!
run '~/.config/tmux/plugins/tpm/tpm'
