# Default shell
set -g default-shell /bin/zsh

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

# Create/destroy sessions
bind S command-prompt -p "New Session:" "new-session -A -s '%%' -c ~"
bind K confirm kill-session

# List sessions
bind s switch-client -nn

# List windows
bind w list-windows

# List sessions and windows
bind W command-prompt -p "List Windows:" "tmux list-windows"

# List all sessions
bind L command-prompt "tmux list-sessions"

# List all windows in the current session
bind l command-prompt "tmux list-windows"

# Reduce delay
set -s escape-time 1

# Start windows and panes at 1, not 0
set -g base-index 1
setw -g pane-base-index 1

# Split panes using h and v
bind b split-pane -h -c "#{pane_current_path}"
bind v split-pane -v -c "#{pane_current_path}"
unbind %
unbind '"'

# Switch panes using Alt-arrow without prefix
bind h select-pane -L
bind l select-pane -R
bind k select-pane -U
bind j select-pane -D

# List windows
bind p previous-window
bind n next-window

# Quick renaming
bind C switch-client -n -t "#{session_name}"
bind C-n command-prompt -p "Rename Window: " "rename-window '%%'"
bind C-r command-prompt -p "Rename Pane: " "rename-pane '%%'"

# Automatic renaming panes
setw -g automatic-rename on
set -g automatic-rename-format "#{b:pane_current_path}"

# Resizing panes with Prefix H, J, K, L
bind -r H resize-pane -U 5
bind -r L resize-pane -R 5
bind -r K resize-pane -U 5
bind -r J resize-pane -D 5

# Toggle synchronize-panes
bind S setw synchronize-panes

# Alternate clear screen prefix + ctrl l
bind C-l send-keys 'C-l'

# Popup session selector
bind C-j display-popup -E "tmux list-sessions | sed -E 's/:.*$//' | grep -v \"^$(tmux display-message -p '#S')\$\" | fzf --reverse | xargs tmux switch-client -t"

# Fuzzy search files and dir
bind C-f display-popup -E "fd -t f -t d . | fzf --preview 'if [ -d {} ]; then tree {}; else bat --color=always {}; fi' --preview-window down:50%:wrap --height=100% --border --prompt='Search: ' --no-reverse --ansi | while read -r selection; do \
if [ -d \"$selection\" ]; then \
    tmux send-keys -t \"\$(tmux display-message -p \#{pane_id})\" \"cd '$selection' && clear\" Enter > /dev/null 2>&1; \
else \
    tmux send-keys -t \"\$(tmux display-message -p \#{pane_id})\" \"cd '\$(dirname \"$selection\")' && \$EDITOR \\\"$selection\\\" && clear\" Enter > /dev/null 2>&1; \
fi; \
done"

# Enable activity alerts
setw -g monitor-activity on
set -g visual-activity on

# Remove switching delay
set-option -s escape-time 0

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

# Increase message display timeout
set-option -g display-time 1500

# Enable focus events
set-option -g focus-events

# Disable detach-on-destroy
set-option -g detach-on-destroy off

# Enable aggressive resize
set-window-option -g aggressive-resize on

# Seamless navigation with neovim panes
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 -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'

bind-key -T copy-mode-vi 'C-h' select-pane -L
bind-key -T copy-mode-vi 'C-j' select-pane -D
bind-key -T copy-mode-vi 'C-k' select-pane -U
bind-key -T copy-mode-vi 'C-l' select-pane -R

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

set -g @resurrect-strategy-nvim 'session'

set -g status-left "Host: #{hostname} | "
set -g status-right '#{prefix_highlight} | %a %Y-%m-%d %H:%M'
set -g status-style fg=default,bg=default

# Renaming Session the current dir's name
set-option -g status-interval 5
set-option -g automatic-rename on
set-option -g automatic-rename-format '#{b:pane_current_path}'

set -g @prefix_highlight_bg 'blue'  # default is 'colour04'

# Default TMUX keybindings
bind c new-window
bind '"' split-window -h
bind % split-window -v
bind h select-pane -L
bind l select-pane -R
bind Left select-pane -L
bind Right select-pane -R
bind Up select-pane -U
bind Down select-pane -D
bind S setw synchronize-panes
bind Space select-layout next
bind Tab select-pane -t:.+
bind q display-panes
bind -n C-b select-pane -T:.+
bind z resize-pane -Z
bind ! break-pane
bind -r H resize-pane -U 5
bind -r L resize-pane -R 5
bind x kill-pane

# Copy mode key mappings
setw -g mode-keys vi

# Navigation
bind-key -T copy-mode-vi 'h' send-keys -X cursor-left
bind-key -T copy-mode-vi 'j' send-keys -X cursor-down
bind-key -T copy-mode-vi 'k' send-keys -X cursor-up
bind-key -T copy-mode-vi 'l' send-keys -X cursor-right
bind-key -T copy-mode-vi 'w' send-keys -X next-word
bind-key -T copy-mode-vi 'b' send-keys -X previous-word

# Search
bind-key -T copy-mode-vi '/' send-keys -X search-forward
bind-key -T copy-mode-vi '?' send-keys -X search-backward
bind-key -T copy-mode-vi 'n' send-keys -X search-again
bind-key -T copy-mode-vi 'N' send-keys -X search-again-backward

# Scrolling
bind-key -T copy-mode-vi 'C-u' send-keys -X halfpage-up
bind-key -T copy-mode-vi 'C-d' send-keys -X halfpage-down

# Selection
bind-key -T copy-mode-vi 'v' send-keys -X begin-selection
bind-key -T copy-mode-vi 'y' send-keys -X copy-pipe "xclip -selection clipboard"

# Movement
bind-key -T copy-mode-vi 'g' send-keys -X start-of-line
bind-key -T copy-mode-vi 'G' send-keys -X end-of-line

# Copying and Pasting
bind-key -T copy-mode-vi 'space' send-keys -X set-mark
bind-key -T copy-mode-vi 'y' send-keys -X copy-pipe "xclip -selection clipboard"

# Miscellaneous
bind-key -T copy-mode-vi '?' send-keys -X list-keys
bind-key -T copy-mode-vi ':' command-prompt
bind-key -T copy-mode-vi '~' send-keys -X show-buffer

bind-key ? list-keys
bind-key : command-prompt
bind-key -n "^" show-buffer
