# `set` is alias for `set-option`
# `set -g` for global window/session (window or session is inferred if not -w or -s is specified)
# `set -a` to append

# Use tmux* TERM over xterm* for better tmux compatibility
# Disabled in favor of foot
# set -g default-terminal "tmux-256color"

################
### Features ###
################
## Ref: http://man.openbsd.org/OpenBSD-current/man1/tmux.1#terminal-features__
# Supports 256 colours with the SGR escape sequences.
set -ag terminal-features ",*:256"
# Allows setting the system clipboard.
set -ag terminal-features ",*:clipboard"
# Allows setting the cursor colour.
set -ag terminal-features ",*:ccolour"
# Allows setting the cursor style.
set -ag terminal-features ",*:cstyle"
# Supports focus reporting.
set -ag terminal-features ",*:focus"
# Supports OSC 8 hyperlinks.
set -ag terminal-features ",*:hyperlinks"
# Supports xterm(1) mouse sequences.
set -ag terminal-features ",*:mouse"
# Supports the overline SGR attribute.
set -ag terminal-features ",*:overline"
# Supports the DECFRA rectangle fill escape sequence.
set -ag terminal-features ",*:rectfill"
# Supports RGB colour with the SGR escape sequences.
set -ag terminal-features ",*:RGB"
# Supports the strikethrough SGR escape sequence.
set -ag terminal-features ",*:strikethrough"
# Supports xterm(1) title setting.
set -ag terminal-features ",*:title"
# Allows underscore style and colour to be set.
set -ag terminal-features ",*:usstyle"

#################
### Overrides ###
#################
# Undercurl color support
set -ag terminal-overrides ',*:Smulx=\E[4::%p1%dm'
# Underline / Underscore color support
set -ag terminal-overrides ',*:Setulc=\E[58::2::::%p1%{65536}%/%d::%p1%{256}%/%{255}%&%d::%p1%{255}%&%d%;m'

#############
### Title ###
#############
set -g set-titles on
set -g set-titles-string "#T"

##############
### Status ###
##############
set -g status on
set -g status-bg default
set -g status-fg white
set -g status-style bg=default
set -g status-position bottom
set -g status-right "#H #(\
    $TMUX_PLUGIN_MANAGER_PATH/tmux-mem-cpu-load/tmux-mem-cpu-load \
    --interval 1 \
    --graph-lines 0 \
    --mem-mode 1 \
    --averages-count 0 \
) %H:%M %d-%m-%Y"
set -g status-right-length 120
set -g window-status-format '#I:#T#F'
# Fix initial title being the hostname for a split second, see:
# https://man7.org/linux/man-pages/man1/tmux.1.html#NAMES_AND_TITLES
set -g window-status-current-format '#I:#{s/^#{host}$/~/:pane_title}#F'

############
### Misc ###
############
set -g update-environment 'DISPLAY TERM XAUTHORITY WINDOWID'
set -g default-command 'zsh'
setw -g pane-border-style 'fg=black'
setw -g pane-active-border-style 'fg=black'
set -g mouse on
set -g message-command-style bg=black,fg=white
set -g message-style bg=black,fg=white
set -g repeat-time 1000
set -g mode-keys vi
set -g status-interval 1
set -g set-clipboard external
set -g escape-time 0

################
### Bindings ###
################
# Default bindings
# bind-key        C-b send-prefix
# bind-key        C-o rotate-window
# bind-key        C-z suspend-client
# bind-key      Space next-layout
# bind-key          ! break-pane
# bind-key          " split-window
# bind-key          # list-buffers
# bind-key          $ command-prompt -I #S "rename-session '%%'"
# bind-key          % split-window -h
# bind-key          & confirm-before -p "kill-window #W? (y/n)" kill-window
# bind-key          ' command-prompt -p index "select-window -t ':%%'"
# bind-key          ( switch-client -p
# bind-key          ) switch-client -n
# bind-key          , command-prompt -I #W "rename-window '%%'"
# bind-key          - delete-buffer
# bind-key          . command-prompt "move-window -t '%%'"
# bind-key          0 select-window -t :0
# bind-key          1 select-window -t :1
# bind-key          2 select-window -t :2
# bind-key          3 select-window -t :3
# bind-key          4 select-window -t :4
# bind-key          5 select-window -t :5
# bind-key          6 select-window -t :6
# bind-key          7 select-window -t :7
# bind-key          8 select-window -t :8
# bind-key          9 select-window -t :9
# bind-key          : command-prompt
# bind-key          ; last-pane
# bind-key          = choose-buffer
# bind-key          ? list-keys
# bind-key          D choose-client
# bind-key          L switch-client -l
# bind-key          [ copy-mode
# bind-key          ] paste-buffer
# bind-key          c new-window
# bind-key          d detach-client
# bind-key          f command-prompt "find-window '%%'"
# bind-key          i display-message
# bind-key          l last-window
# bind-key          n next-window
# bind-key          o select-pane -t :.+
# bind-key          p previous-window
# bind-key          q display-panes
# bind-key          s choose-tree
# bind-key          t clock-mode
# bind-key          w choose-window
# bind-key          x confirm-before -p "kill-pane #P? (y/n)" kill-pane
# bind-key          z resize-pane -Z
# bind-key          { swap-pane -U
# bind-key          } swap-pane -D
# bind-key          ~ show-messages
# bind-key      PPage copy-mode -u
# bind-key -r      Up select-pane -U
# bind-key -r    Down select-pane -D
# bind-key -r    Left select-pane -L
# bind-key -r   Right select-pane -R
# bind-key        M-1 select-layout even-horizontal
# bind-key        M-2 select-layout even-vertical
# bind-key        M-3 select-layout main-horizontal
# bind-key        M-4 select-layout main-vertical
# bind-key        M-5 select-layout tiled
# bind-key        M-n next-window -a
# bind-key        M-o rotate-window -D
# bind-key        M-p previous-window -a
# bind-key -r    M-Up resize-pane -U 5
# bind-key -r  M-Down resize-pane -D 5
# bind-key -r  M-Left resize-pane -L 5
# bind-key -r M-Right resize-pane -R 5
# bind-key -r    C-Up resize-pane -U
# bind-key -r  C-Down resize-pane -D
# bind-key -r  C-Left resize-pane -L
# bind-key -r C-Right resize-pane -R

# Custom
# -r == repeatable
# -n == no prefix

# Prefix
set -g prefix C-z
unbind          C-b
bind-key        C-z send-prefix

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

# Use Ctrl to select
unbind      -T root         MouseDrag1Pane
unbind      -T copy-mode-vi MouseDrag1Pane
bind-key    -T root         C-MouseDrag1Pane    if-shell -F "#{||:#{pane_in_mode},#{mouse_any_flag}}" { send-keys -M } { copy-mode -M }
bind-key    -T copy-mode-vi C-MouseDrag1Pane    select-pane \; send-keys -X begin-selection

# Don't stop copy mode after selecting
unbind      -T copy-mode-vi MouseDragEnd1Pane

# More vim-like selection
bind-key    -T copy-mode-vi 'v'     send -X begin-selection     # Begin selection in copy mode.
bind-key    -T copy-mode-vi 'C-v'   send -X rectangle-toggle    # Begin selection in copy mode.
bind-key    -T copy-mode-vi 'y'     send -X copy-selection      # Yank selection in copy mode.

bind-key    k   select-pane -U
bind-key    j   select-pane -D
bind-key    h   select-pane -L
bind-key    l   select-pane -R
bind-key -r C-k resize-pane -U
bind-key -r C-j resize-pane -D
bind-key -r C-h resize-pane -L
bind-key -r C-l resize-pane -R
bind-key    o   last-window

bind -T copy-mode-vi C-n send-keys -X next-prompt
bind -T copy-mode-vi C-p send-keys -X previous-prompt

# Plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'thewtex/tmux-mem-cpu-load'

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'"

run '~/.config/tmux/plugins/tpm/tpm'
