# ==========================
# ===  General settings  ===
# ==========================
set-option -sa terminal-overrides ",xterm-kitty:RGB"
set -g default-terminal "screen-256color"

# Change prefix key to C-a, easier to type, same to "screen"
unbind C-b
set -g prefix C-a

# Set parent terminal title to reflect current window in tmux session 
set -g set-titles on
set -g set-titles-string "#I:#W"

# Start index of window/pane with 1, because we're humans, not computers
set -g base-index 1
setw -g pane-base-index 1

# Enable mouse support
set -g mouse on

set-option -g status-position bottom
set-option -g renumber-windows on

# ==========================
# ===  Cursor Dark theme ===
# ==========================
# Powerline status bar, ported from nordtheme/tmux with Cursor Dark colors.
# Requires a patched (Nerd) font for the U+E0B0-E0B3 separators.
#
#   bar bg   #181818   raised segment #2b2b2b   text #cccccc
#   session  #2472c8   current window / host #11a8cd

set -g status on
set -g status-interval 1
set -g status-justify left
set -g status-style "bg=#181818,fg=#cccccc"
set -g status-left-length 40
set -g status-right-length 80

#+--- Bars ---+
set -g status-left "#[fg=#ffffff,bg=#2472c8,bold] #S #[fg=#2472c8,bg=#181818,nobold,noitalics,nounderscore]"
set -g status-right "#[fg=#2b2b2b,bg=#181818,nobold,noitalics,nounderscore]#[fg=#cccccc,bg=#2b2b2b] %Y-%m-%d #[fg=#cccccc,bg=#2b2b2b,nobold,noitalics,nounderscore]#[fg=#cccccc,bg=#2b2b2b] %H:%M #[fg=#11a8cd,bg=#2b2b2b,nobold,noitalics,nounderscore]#[fg=#181818,bg=#11a8cd,bold] #H "

#+--- Windows ---+
set -g window-status-format "#[fg=#181818,bg=#2b2b2b,nobold,noitalics,nounderscore] #[fg=#cccccc,bg=#2b2b2b]#I #[fg=#cccccc,bg=#2b2b2b,nobold,noitalics,nounderscore] #[fg=#cccccc,bg=#2b2b2b]#W #F #[fg=#2b2b2b,bg=#181818,nobold,noitalics,nounderscore]"
set -g window-status-current-format "#[fg=#181818,bg=#11a8cd,nobold,noitalics,nounderscore] #[fg=#181818,bg=#11a8cd]#I #[fg=#181818,bg=#11a8cd,nobold,noitalics,nounderscore] #[fg=#181818,bg=#11a8cd]#W #F #[fg=#11a8cd,bg=#181818,nobold,noitalics,nounderscore]"
set -g window-status-separator ""
# Let the formats above own the colors; activity/bell still show via the #F flag
setw -g window-status-activity-style "none"
setw -g window-status-bell-style "none"

#+--- Panes ---+
set -g pane-border-style "bg=default,fg=#2b2b2b"
set -g pane-active-border-style "bg=default,fg=#2472c8"
set -g display-panes-colour "#666666"
set -g display-panes-active-colour "#2472c8"

#+--- Messages ---+
set -g message-style "bg=#2b2b2b,fg=#11a8cd"
set -g message-command-style "bg=#2b2b2b,fg=#11a8cd"
setw -g mode-style "bg=#264f78,fg=#e5e5e5"

#+--- Clock ---+
setw -g clock-mode-colour "#11a8cd"

# ================================================
# ===     Copy mode, scroll and clipboard      ===
# ================================================
set -g @copy_use_osc52_fallback on

# Prefer vi style key table
setw -g mode-keys vi

bind p paste-buffer
bind C-p choose-buffer

# trigger copy mode by
bind -n M-Up copy-mode

# Scroll up/down by 1 line, half screen, whole screen
bind -T copy-mode-vi M-Up              send-keys -X scroll-up
bind -T copy-mode-vi M-Down            send-keys -X scroll-down
bind -T copy-mode-vi M-PageUp          send-keys -X halfpage-up
bind -T copy-mode-vi M-PageDown        send-keys -X halfpage-down
bind -T copy-mode-vi PageDown          send-keys -X page-down
bind -T copy-mode-vi PageUp            send-keys -X page-up

# When scrolling with mouse wheel, reduce number of scrolled rows per tick to "2" (default is 5)
bind -T copy-mode-vi WheelUpPane       select-pane \; send-keys -X -N 2 scroll-up
bind -T copy-mode-vi WheelDownPane     select-pane \; send-keys -X -N 2 scroll-down


# split panes using | and -
bind | split-window -h
bind - split-window -v
unbind '"'
unbind %

# switch panes using Alt-arrow 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


# wrap default shell in reattach-to-user-namespace if available
# there is some hack with `exec & reattach`, credits to "https://github.com/gpakosz/.tmux"
# don't really understand how it works, but at least window are not renamed to "reattach-to-user-namespace"
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)\"'"

#yank="~/.tmux/yank.sh"

# Copy selected text
bind -T copy-mode-vi Enter send-keys -X copy-pipe-and-cancel "$yank"
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "$yank"
bind -T copy-mode-vi Y send-keys -X copy-line \;\
    run "tmux save-buffer - | $yank"
bind-key -T copy-mode-vi D send-keys -X copy-end-of-line \;\
    run "tmux save-buffer - | $yank"
bind -T copy-mode-vi C-j send-keys -X copy-pipe-and-cancel "$yank"
bind-key -T copy-mode-vi A send-keys -X append-selection-and-cancel \;\
    run "tmux save-buffer - | $yank"

# Copy selection on drag end event, but do not cancel copy mode and do not clear selection
# clear select on subsequence mouse click
bind -T copy-mode-vi MouseDragEnd1Pane \
    send-keys -X copy-pipe "$yank"
bind -T copy-mode-vi MouseDown1Pane select-pane \;\
   send-keys -X clear-selection
    
# iTerm2 works with clipboard out of the box, set-clipboard already set to "external"
# tmux show-options -g -s set-clipboard
# set-clipboard on|external

# ==========================
# ===       Plugins      ===
# ==========================
# Nothing under ~/.tmux/plugins is tracked in git - tpm owns that directory and
# clones everything listed below itself. Do not commit the plugin folders: they
# would be checked out as empty dirs, and tpm would then skip installing them.

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

# tpm and its installer both resolve the plugin dir from this tmux environment
# variable, and only the tpm script itself sets it. Pin it up-front so the
# installer below already works on the very first run.
run 'tmux set-environment -g TMUX_PLUGIN_MANAGER_PATH "$HOME/.tmux/plugins/"'

# Bootstrap tpm itself on a machine that has never run it
if "test ! -d ~/.tmux/plugins/tpm" \
   "run 'git clone -q --single-branch https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm'"

# Clone any declared plugin that is missing. Deliberately in the foreground, so
# plugins installed here are already on disk when tpm sources them below.
run '~/.tmux/plugins/tpm/bin/install_plugins'

# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'


