# Errol Sancaktar - Tmux Config
# ===================
# General Settings
# ===================

# Set default terminal and enable 256 colors
set -g default-terminal "screen-256color"
set -ag terminal-overrides ",xterm-256color:RGB"

# Set default shell to ZSH
set -g default-shell /bin/zsh

# Increase history limit
set-option -g history-limit 50000

# Enable mouse support
set -g mouse on

# Disable right-click menu for panes
unbind -n MouseDown3Pane

# Window and pane numbering starts at 1
set -g base-index 1
setw -g pane-base-index 1
set -g renumber-windows on

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

# Enable vi keys in copy mode
set-window-option -g mode-keys vi

# Clear screen
bind C-l send-keys 'C-l'

# ===================
# Key Bindings
# ===================

# Command prompt with semicolon
bind \; command-prompt

# Pane resizing with Ctrl + Arrow keys
bind -r C-Down resize-pane -D 5
bind -r C-Up resize-pane -U 5
bind -r C-Left resize-pane -L 5
bind -r C-Right resize-pane -R 5

# Toggle zoom for current pane
bind -r m resize-pane -Z

# Switch panes with Alt + Arrow keys (without prefix)
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D

# Window switching (Alt + Number)
bind-key -n M-1 select-window -t 1
bind-key -n M-2 select-window -t 2
bind-key -n M-3 select-window -t 3
bind-key -n M-4 select-window -t 4
bind-key -n M-5 select-window -t 5
bind-key -n M-6 select-window -t 6

# Mac Window switching (Alt + Number)
bind-key -n ¡ select-window -t 1
bind-key -n ™ select-window -t 2
bind-key -n £ select-window -t 3
bind-key -n ¢ select-window -t 4
bind-key -n ∞ select-window -t 5
bind-key -n § select-window -t 6

# Split panes and set working directory
unbind "%"
unbind '"'
bind-key "|" split-window -h -c "#{pane_current_path}"
bind-key "\\" split-window -fh -c "#{pane_current_path}"
bind-key "-" split-window -v -c "#{pane_current_path}"
bind-key "_" split-window -fv -c "#{pane_current_path}"

# Join and kill panes/windows
bind j choose-window 'join-pane -h -s "%%"'
bind J choose-window 'join-pane -s "%%"'
bind END kill-window
bind Space kill-pane
bind c new-window -c "#{pane_current_path}"
bind -n C-f last-window

# Swap windows
bind -r "<" swap-window -d -t -1
bind -r ">" swap-window -d -t +1

# Open notes in Vim
bind-key h split-window -h "vim ~/scratch/notes.md"

# Page up/down bindings
bind -n S-Pageup send-keys S-Pageup
bind -n S-Pagedown send-keys S-Pagedown

# ===================
# Copy-Paste Settings
# ===================

# Handle clipboard integration for macOS and Linux
#if-shell "uname | grep -q Darwin" {
#    bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "pbcopy"
#} {
#    bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "xclip -in -selection clipboard"
#}

# Right-click paste without adding extra newlines
#unbind -n MouseDown3Pane
#bind -n MouseDown3Pane run-shell "
#    tmux show-buffer |
#    tr -d '\n' |               # Remove newline characters
#    tmux load-buffer -;        # Reload sanitized buffer
#    tmux paste-buffer -;       # Paste sanitized content
#"

# Enable direct yank and paste from terminal (trimming newlines)
#bind y run-shell "tmux show-buffer | tr -d '\n' | tmux load-buffer -; tmux paste-buffer"

# ===================
# Status Bar
# ===================

# Status bar appearance
set -g status on
set -g status-interval 1
set-option -g status-style bg=colour237,fg=colour223
set-window-option -g window-status-style bg=colour237,fg=colour231
set-window-option -g window-status-activity-style bg=colour9,fg=colour248
set-window-option -g window-status-current-style bg=colour2,fg=colour237

# Status bar content
set -g status-left-length 30
set -g status-right-length 150
set -g status-left "\
#[fg=colour215, bg=colour241]#{?client_prefix,#[bg=colour167],} %a, %b %d %y %H:%M:%S \
#[fg=colour241, bg=colour237]#{?client_prefix,#[fg=colour167],}#{?window_zoomed_flag, 🔍,}"
set -g status-right '#[fg=colour117, bg=colour237]#(hostname | cut -d "." -f1)@#(host `hostname` | cut -d " " -f 4) '
set-window-option -g window-status-current-format "\
#[fg=colour237, bg=colour2]\
#[fg=colour239, bg=colour2] #I* \
#[fg=colour239, bg=colour2, bold] #W \
#[fg=colour2, bg=colour237]"
set-window-option -g window-status-format "\
#[fg=colour237,bg=colour239,noitalics]\
#[fg=colour231,bg=colour239] #I \
#[fg=colour231, bg=colour239] #W \
#[fg=colour231, bg=colour237]"


# ===================
# Better Mouse Mode Settings
# ===================
set -g @scroll-down-exit-copy-mode "off"
set -g @scroll-without-changing-pane "on"
set -g @emulate-scroll-for-no-mouse-alternate-buffer "on"

# ===================
# Plugins
# ===================

if-shell "uname | grep -q Darwin" {
    set-environment -g PATH "/opt/homebrew/bin:/bin:/usr/bin"
}

set -g @plugin 'tmux-plugins/tmux-open'
set -g @plugin 'ofirgall/tmux-browser'
set -g @plugin 'sainnhe/tmux-fzf'
set -g @plugin 'rickstaa/tmux-notify'
set -g @plugin 'alexwforsythe/tmux-which-key'
set -g @plugin 'nhdaly/tmux-better-mouse-mode'
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'xamut/tmux-spotify'
run '~/.tmux/plugins/tpm/tpm'
run-shell '~/.tmux/plugins/tpm/tpm'


