# GENERAL SETTINGS
set -g default-terminal "screen-256color"                                                                 # 256 color
set -sa terminal-features ",*256col*:RGB"                                                                 # True color support
set -as terminal-overrides ',*:Smulx=\E[4::%p1%dm'                                                        # Undercurl support
set -as terminal-overrides ',*:Setulc=\E[58::2::%p1%{65536}%/%d::%p1%{256}%/%{255}%&%d::%p1%{255}%&%d%;m' # Underscore colors
set -sg escape-time 0                                                                                     # Time for tmux to wait for key sequence
set -g history-limit 50000                                                                                # Maximum number of lines held in window history
set -s buffer-limit 20                                                                                    # Number of buffers, older than limit buffer will be removed
set -g display-time 1500                                                                                  # Time for which status line messages and indicators are displayed in miliseconds, 0 means diplay until a key is pressed
setw -g remain-on-exit off                                                                                # Destroy window when program exits
set -g repeat-time 500                                                                                    # Typing command without pressing prefix key again in the specified time in miliseconds
setw -g automatic-rename on                                                                               # Automatic window renaming
setw -g automatic-rename-format '#{pane_current_command}'                                                 # Format of automatic window renaming
setw -g allow-rename off                                                                                  # Allow programs to change the window name
setw -g xterm-keys on                                                                                     # Xterm style function key sequences
set -g mouse on                                                                                           # Enable mouse support
set -g status-keys vi                                                                                     # Use vi style keybindings in command prompt
setw -g mode-keys vi                                                                                      # Use vi style keybindings in copy mode
set -g base-index 1                                                                                       # Starting index for new window
set -g pane-base-index 1                                                                                  # Starting index for new pane
setw -g aggressive-resize on                                                                              # Fix from a smaller client issue

# KEYBINDING SETTINGS
# Unbind the default key bindings
unbind C-b     # send-prefix
unbind '"'     # split-window
unbind '$'     # command-prompt -I'#S' \"rename-session -- '%%'\"
unbind %       # split-window -h
unbind &       # confirm-before -p\"kill-window #W? (y/n)\" kill-window
unbind ,       # command-prompt -I'#W' \"rename-window -- '%%'\"
unbind -       # delete-buffer
unbind .       # command-prompt \"move-window -t '%%'\"
unbind \;      # last-pane
unbind =       # choose-buffer -Z
unbind E       # select-layout -E
unbind L       # switch-client -l
unbind M       # select-pane -M
unbind [       # copy-mode
unbind ]       # paste-buffer
unbind f       # command-prompt \"find-window -Z -- '%%'\"
unbind l       # last-window
unbind n       # next-window
unbind o       # select-pane -t:.+
unbind p       # previous-window
unbind r       # refresh-client
unbind s       # choose-tree -Zs
unbind w       # choose-tree -Zw
unbind x       # confirm-before -p\"kill-pane #P? (y/n)\" kill-pane"
unbind '{'     # swap-pane -U
unbind '}'     # swap-pane -D
unbind M-n     # next-window -a
unbind M-p     # previous-window -a
unbind M-Up    # resize-pane -U 5
unbind M-Down  # resize-pane -D 5
unbind M-Left  # resize-pane -L 5
unbind M-Right # resize-pane -R 5
unbind C-Up    # resize-pane -U
unbind C-Down  # resize-pane -D
unbind C-Left  # resize-pane -L
unbind C-Right # resize-pane -R

# Now set our custom key bindings
set -g prefix M-a       # Prefix key is now Alt+a
bind -n M-z send-prefix # Prefix key for client Tmux is Alt+z

# After pressing prefix key, you can continue with keybindings below to do stuffs
bind M-r source-file $HOME/.tmux.conf \; display 'Tmux Reloaded' # Alt+r to reload configuration
bind f choose-tree                                               # f to find session, window, or pane from a list
bind M-s split-window -h                                         # Alt+s to make new split window
bind M-v split-window -v                                         # Alt+v to make new vertical split window
bind r command-prompt -I'#W' "rename-window -- '%%'"             # r to rename current window
bind R command-prompt -I'#S' "rename-session -- '%%'"            # R to rename current session

# Alt+e to edit tmux.conf.local file
bind M-e new-window "sh -c '\${EDITOR:-vim} ~/.tmux.conf.local && tmux source ~/.tmux.conf && tmux display \"Tmux Reloaded\"'"

# Moving between panes
bind -r h select-pane -L # h to move left
bind -r j select-pane -D # j to move down
bind -r k select-pane -U # k to move up
bind -r l select-pane -R # l to move right
bind -r > swap-pane -D   # > to swap to the next pane
bind -r < swap-pane -U   # < to swap to the previous pane

# Resizing panes
bind -r H resize-pane -L 2 # H to resize to the left
bind -r J resize-pane -D 2 # J to resize downwards
bind -r K resize-pane -U 2 # K to resize upwards
bind -r L resize-pane -R 2 # L to resize to the right

# Moving between windows
bind -r M-n next-window                     # Alt+n to move next window
bind -r M-p previous-window                 # Alt+p to move previous window
bind -r Tab next-window                     # Tab to move next window (in case your window manager use M-l already)
bind S choose-window "join-pane -v -t "%%"" # S to join pane to selected window in horizontal split
bind V choose-window "join-pane -h -t "%%"" # V to join pane to selected window in vertical split

# Closing everything
bind x kill-pane                                                       # x to close pane
bind X kill-window                                                     # X to close window
bind M-x confirm-before -p"kill other windows? (y/n)" "kill-window -a" # Alt+x to close other windows

# Copy and paste to bufer
bind a copy-mode     # a to trigger copy mode
bind p paste-buffer  # p to paste from top buffer
bind P choose-buffer # Alt+p to choose buffer to paste

# Copy mode keybindings
bind -T copy-mode-vi v send -X begin-selection           # v in copy mode to begin selection
bind -T copy-mode-vi M-v send -X rectangle-toggle        # Alt+v in copy mode to toggle rectangle select
bind -T copy-mode-vi y send -X copy-selection-and-cancel # y in copy mode to yank selection
bind -T copy-mode-vi Y send -X copy-line                 # Y in copy mode to yank a line
bind -T copy-mode-vi Escape send -X cancel               # Esc in copy mode to exit copy mode

# macOS clipboard support with pbcopy
if -b "command -v reattach-to-user-namespace > /dev/null 2>&1" "\
    run 'tmux set -g default-command \"exec $(tmux show -gv default-shell) 2>/dev/null & reattach-to-user-namespace -l $(tmux show -gv default-shell)\"'"
if -b "command -v pbcopy > /dev/null 2>&1" " \
    bind -T copy-mode-vi y send -X copy-pipe-and-cancel 'reattach-to-user-namespace pbcopy'; \
    bind -T copy-mode-vi Y send -X copy-line \\; run 'tmux save-buffer - | reattach-to-user-namespace pbcopy'; \
    bind -T copy-mode-vi D send -X copy-end-of-line \\; run 'tmux save-buffer - | reattach-to-user-namespace pbcopy'; \
    bind -T copy-mode-vi MouseDragEnd1Pane send -X copy-pipe-and-cancel 'reattach-to-user-namespace pbcopy'"

# Linux clipboard support with xsel or xclip
if -b "command -v xsel > /dev/null 2>&1" " \
    bind -T copy-mode-vi y send -X copy-pipe-and-cancel 'xsel -i -b'; \
    bind -T copy-mode-vi Y send -X copy-line \\; run 'tmux save-buffer - | xsel -i -b'; \
    bind -T copy-mode-vi D send -X copy-end-of-line \\; run 'tmux save-buffer - | xsel -i -b'; \
    bind -T copy-mode-vi MouseDragEnd1Pane send -X copy-pipe-and-cancel 'xsel -i -b'"
if -b "! command -v xsel > /dev/null 2>&1 && command -v xclip > /dev/null 2>&1" " \
    bind -T copy-mode-vi y send -X copy-pipe-and-cancel 'xclip -i -selection clipboard >/dev/null 2>&1'; \
    bind -T copy-mode-vi Y send -X copy-line \\; run 'tmux save-buffer - | xclip -i -selection clipboard >/dev/null 2>&1'; \
    bind -T copy-mode-vi D send -X copy-end-of-line \\; run 'tmux save-buffer - | xclip -i -selection clipboard >/dev/null 2>&1'; \
    bind -T copy-mode-vi MouseDragEnd1Pane send -X copy-pipe-and-cancel 'xclip -i -selection clipboard >/dev/null 2>&1'"

# APPEARANCE SETTINGS
# Default value for statusline format
POWERLINE_SEPARATOR_LEFT=""
POWERLINE_SEPARATOR_RIGHT=""
POWERLINE_SESSION_ICON="  "
POWERLINE_USER_ICON=" "
POWERLINE_WINDOW_ICON=""
POWERLINE_WINDOW_ACTIVE_ICON=" "
POWERLINE_CALENDAR_ICON=" "
POWERLINE_CLOCK_ICON=" "
POWERLINE_PREFIX_ACTIVE_ICON="󰌿 "
POWERLINE_PREFIX_ICON="󰌾 "

setw -g mode-style "fg=black,bg=yellow"       # Window mode style
set -g message-style "fg=green"               # Status line message style
set -g message-command-style "fg=green"       # Status line message command style
set -g status on                              # Show or hide status line
set -g status-interval 1                      # Update status line every interval second
set -g status-justify left                    # Set the position of the window list component of status line
set -g status-position bottom                 # Set the position of status line
setw -g clock-mode-colour yellow              # Set clock color
setw -g clock-mode-style 12                   # Set clock hour format
setw -g window-status-activity-style "fg=red" # Window with activity alert style
setw -g window-status-separator ""            # Set the separator drawn between windows in status line
set -g status-left-length 100                 # Set the maximum length of left status line
set -g status-right-length 100                # Set the maximum length of right status line
set -g status-style "fg=white"                # Status line style
setw -g pane-border-status off                # Pane border status line
setw -g pane-border-style "fg=blue"           # Pane border style
setw -g pane-active-border-style "fg=red"     # Active pane border style

# Move status line to top if tmux is nested inside another tmux so they don't colide
if -b "test -n '$SSH_CLIENT'" "set -g status-position top"

# Left status line configurations
set -g status-left " #[fg=blue,bold]$POWERLINE_SEPARATOR_LEFT$POWERLINE_SESSION_ICON#S$POWERLINE_SEPARATOR_RIGHT#[fg=yellow] $POWERLINE_SEPARATOR_LEFT$POWERLINE_USER_ICON#(whoami)$POWERLINE_SEPARATOR_RIGHT#[bg=terminal] #{?client_prefix,#[fg=green]$POWERLINE_PREFIX_ACTIVE_ICON,#[fg=red]$POWERLINE_PREFIX_ICON}"

# Right status line configurations
set -g status-right "#[fg=green,bold]$POWERLINE_SEPARATOR_LEFT$POWERLINE_CALENDAR_ICON %a, %d %b %y$POWERLINE_SEPARATOR_RIGHT#[fg=magenta] $POWERLINE_SEPARATOR_LEFT$POWERLINE_CLOCK_ICON %I:%M %p$POWERLINE_SEPARATOR_RIGHT "

# Window status line configurations
setw -g window-status-current-format "#[fg=blue,bold]$POWERLINE_SEPARATOR_LEFT$POWERLINE_WINDOW_ACTIVE_ICON#I:#W$POWERLINE_SEPARATOR_RIGHT "
setw -g window-status-format "#[bg=terminal,fg=white,bold]#I:$POWERLINE_WINDOW_ICON#W "

# Use user defined overrides if .config/tmux/tmux.conf.local file is found
if "[-f ~/.config/tmux/tmux.conf.local]" "source ~/.config/tmux/tmux.conf.local"
