# ~/.config/tmux/tmux.conf, or .dotfiles/config/tmux/tmux.conf
#
# Contains configuration from the following sources:
# - https://tmuxguide.readthedocs.io/en/latest/tmux/tmux.html
# - https://github.com/dreamsofcode-io/tmux/blob/main/tmux.conf
#
#  ╭──────────────────────────────────────────────────────────╮
#  │ Settings                                                 │
#  ╰──────────────────────────────────────────────────────────╯
# set [flags] [command] [value]
# Flags can be combined, e.g. -as
# -a : Append the value to the existing setting.
# -s : Apply the setting to the current session.
# -g : Apply the setting globally to all sessions.
# -o : Set the option only if it is not already set.
# -u : Unset the specified option.

set -ag terminal-overrides ",xterm-256color:RGB"
set -ag terminal-features 'xterm-256color:RGB'

set -g default-terminal "tmux-256color" # Set default terminal to 256 colors
set -g detach-on-destroy off            # don't detach tmux when killing a session
set -g display-time 0                   # Hide clock
set -g focus-events on                  # Focus events enabled for terminals that support them
set -g mouse on                         # Mouse support
set -g set-titles on                    # Allow tmux to set the terminal title
set -g status on                        # Setting status on
set -g status-keys vi                   # vi keys to move between panes
set -g history-limit 3000               # Scrollback history limit

# Activity Monitoring (for when something happens in another pain)
set -g monitor-activity on
set -g visual-activity off
set -g visual-bell off

# A bell in another window should cause a bell in the current window
set -g bell-action any

# Keep Tmux alive when the initial command is finished
set -g remain-on-exit off

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

# Activate with `DEBUG=1 tmux -vv`
if-shell '[ "$DEBUG" = "1" ]' 'set -g debug-file ~/.cache/tmux-debug.log'

#  ╭──────────────────────────────────────────────────────────╮
#  │ Theme                                                    │
#  ╰──────────────────────────────────────────────────────────╯

set -g pane-active-border-style "fg=#7aa2f7"
set -g pane-border-style "fg=#31748f"
set -g status-style "bg=default"
set -g status-justify "left"
set -g status-left ''
set -g status-left-length "0"
set -g status-position "bottom"
set -g status-right "#S@#h #{tmux_mode_indicator}"
set -g status-right-length "30"
set -g window-status-current-format ' #I:#W#{?window_zoomed_flag, ◈ ,} '
set -g window-status-format ' #I:#W '

#  ╭──────────────────────────────────────────────────────────╮
#  │ Bindings                                                 │
#  ╰──────────────────────────────────────────────────────────╯
# bind flags
# -N = Note / description of the command
# -r = repeatable, only needs prefix once
# -n = doesn't need prefix
# -t = binds to a certain key-table (root, copy-mode, prefix, etc.)

# Set <prefix> to Control + Space, keeping the default of C-b intact.
# C-Space send-prefix line doubles the prefix when in nested tmux session.
set -g prefix C-Space
bind C-Space send-prefix

# Use Alt-arrow keys without prefix key to switch panes
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

# Easier switching between window
bind C-n next-window
bind C-p previous-window
bind C-a last-window

# Reload tmux config with <prefix> + r
unbind r
bind r "source-file ~/.dotfiles/config/tmux/tmux.conf; display 'tmux cfg reloaded!'"

# copy mode using 'Esc'
unbind [
bind Escape copy-mode

# paste using 'p'
unbind p
bind p paste-buffer

# tms bindings
bind -N "tms"         t   display-popup -E "tms"
bind -N "tms windows" C-w display-popup -E "tms windows"
bind -N "tms switch"  C-s display-popup -E "tms switch"
bind -N "tms refresh" C-r display-popup -E "tms refresh"

#  ╭──────────────────────────────────────────────────────────╮
#  │ Plugins                                                  │
#  ╰──────────────────────────────────────────────────────────╯

# ── Plugins related configurations ────────────────────────────────────

# Plugin that lets you suspend local tmux session,
# so that you can work with nested remote tmux session painlessly.
# https://github.com/MunifTanjim/tmux-suspend
# set -g @suspend_key 'F8' # Default is F12

##  A plugin to name your tmux windows smartly.
## https://github.com/ofirgall/tmux-window-name
### Maximum name length of a window
set -g @tmux_window_name_max_name_len "20"
### Replace $HOME with ~ in window names
set -g @tmux_window_dir_programs "['nvim', 'vim', 'vi', 'git']"
set -g @tmux_window_name_ignored_programs "['sqlite3', 'antidote', 'direnv', 'md5']" # Default is []
set -g @tmux_window_name_shells "['bash', 'fish', 'sh', 'zsh']"
set -g @tmux_window_name_substitute_sets "[('.+ipython2', 'ipython2'), ('.+ipython3', 'ipython3'), ('.+\.local', '.local'), ('.+asdf', 'asdf')]"
set -g @tmux_window_name_use_tilde "True"

# https://github.com/erikw/tmux-dark-notify
set -g @dark-notify-theme-path-light "$HOME/.dotfiles/config/tmux/theme-light.conf"
set -g @dark-notify-theme-path-dark "$HOME/.dotfiles/config/tmux/theme-dark.conf"

## https://github.com/MunifTanjim/tmux-mode-indicator
set -g @mode_indicator_copy_mode_style 'bg=default,fg=yellow'
set -g @mode_indicator_empty_mode_style 'bg=default,fg=#7aa2f7'
set -g @mode_indicator_prefix_mode_style 'bg=default,fg=#7aa2f7'
set -g @mode_indicator_sync_mode_style 'bg=default,fg=red'

# https://github.com/wfxr/tmux-fzf-url
set -g @fzf-url-bind 'u'
set -g @fzf-url-history-limit '2000'

# https://github.com/tmux-plugins/tmux-continuum
set -g @continuum-restore 'on'

# ── Own scripts ───────────────────────────────────────────────────────

# If we started tmux with a session name, rename it.
run-shell "$HOME/.dotfiles/config/tmux/rename-session.sh"

# Load theme based on tmux-dark-notify state.
# This script helps states where dark-notify is not available,
# and we want to have light or dark state constantly available.
run-shell "$HOME/.dotfiles/config/tmux/theme-activate.sh"

# ── Sourcing the plugins ──────────────────────────────────────────────

run-shell "$HOME/.dotfiles/config/tmux/plugins/tmux-sensible/sensible.tmux"
run-shell "$HOME/.dotfiles/config/tmux/plugins/tmux-window-name/tmux_window_name.tmux"
run-shell "$HOME/.dotfiles/config/tmux/plugins/tmux-mode-indicator/mode_indicator.tmux"
run-shell "$HOME/.dotfiles/config/tmux/plugins/tmux-suspend/suspend.tmux"
run-shell "$HOME/.dotfiles/config/tmux/plugins/tmux-sessionist/sessionist.tmux"
run-shell "$HOME/.dotfiles/config/tmux/plugins/tmux-yank/yank.tmux"
run-shell "$HOME/.dotfiles/config/tmux/plugins/tmux-current-pane-hostname/current_pane_hostname.tmux"
run-shell "$HOME/.dotfiles/config/tmux/plugins/tmux-fzf-url/fzf-url.tmux"
run-shell "$HOME/.dotfiles/config/tmux/plugins/tmux-resurrect/resurrect.tmux"
run-shell "$HOME/.dotfiles/config/tmux/plugins/tmux-continuum/continuum.tmux"

# run-shell "$HOME/.dotfiles/config/tmux/plugins/tmux-dark-notify/main.tmux"
