##############################
# ~ tmux.conf ~ for Chris Jones
# github > @ipatch
# twitter > @truckmonth
## 

##############################
# NOTE: `display` is short for `display-message`
# NOTE: `bind` is short for `bind-key`
# NOTE: `-n` option when creating a key-binding does not require the prefix
# NOTE: `set-option` controls the look / feel of tmux.
# NOTE: official tmux documentation, 
#       ... https://github.com/tmux/tmux/wiki/FAQ 
#       ....uses double quotes and not single.
##

# NOTE: the default "prefix" key in tmux is `C-b` ie. <Control>+b
unbind C-b # ubind `C-b` 🤸

# NOTE: `-g` flag makes the `prefix` global to ALL tmux sessions.
set -g prefix C-s # reassign the `prefix` key.

bind r source-file ~/.tmux.conf \; display "~/.tmux.conf reloaded"

# Neovim recommends the below `escape` time setting
set -sg escape-time 10

set -g default-terminal "tmux-256color" # use custom compiled in terminfo file
set-option -sa terminal-overrides ",xterm*:Tc" # enable 24 bit color
# set -ga terminal-overrides ",screen-256color:Tc"
# set -as terminal-overrides ',xterm*:Tc:sitm=\E[3m'

# Experimental
set-window-option -g xterm-keys on

##############################
# STYLES / STYLING > status line / bar
# NOTE: to print options that can be styled `tmux showw -g`
##
# NOTE: `set` is short for `set-option`, it is an internal alias within tmux.
# set -g utf8 on #INVALID
set -g status-bg "#3e93b2"
set -g status-fg "#01070a"
set -g status-justify centre # center the window list in status line

# NOTE: the below option sets / displays the tmux session name up to 50 characters long.  It can be seen in the lower left of the tmux status bar. ;)
set -g status-left-length 50
set -g status-left "#S #{?client_prefix,#[fg=colour208]^S,}" # modify the default left status bar, rm `[]`
set -g status-right "#(~/.local/bin/battery) #[fg=purple]%d %b %R"

# styles for windows
## styles for current active window
setw -g window-status-current-format "#[fg=#000000]#[bg=colour208] #I:#W#{?window_zoomed_flag,🔍,}#{?pane_synchronized,🏊🏊,}"
## styles for inactive windows, ie. background windows
setw -g window-status-format "#I:#W#{?window_zoomed_flag,🔍,}#{?pane_synchronized,🏊🏊,}"

#############################
# STYLES / STYLING > panes
## NOTE: may be deprecated in tmux ≥ 2.9
###############
# borders
# set-option -g pane-active-border-bg default
# set-option -g pane-active-border-fg colour208 # an orangish unbuntu color

##############################
# History
# NOTE: to remove scroll back history within a tmux pane `prefix + ctrl+k`
##
bind C-l send-keys "C-l" # `prefix + C-l` to clear the screen
bind C-k send-keys -R \; send-keys "C-l" \; clear-history
set -g history-limit 200000    # Scrollback for weeks (default is 2000)

# toggle pane syncronization
bind e setw synchronize-panes

##############################
# Winow / pane splitting
##
bind - split-window -v -c "#{pane_current_path}" # split pane and preserve path
bind \ split-window -h -c "#{pane_current_path}" # split pane and preserve path

##############################
# NOTE: `setw` is short for `set-window-option`
##
setw -g base-index 1 # begin window numbering at 1 instead of 0
setw -g renumber-windows on
setw -g mode-keys vi # Vim style navigation in copy mode
set -g default-shell $SHELL
set -g mouse on

##############################
# Set keys for easier window management
##
bind -n S-Up new-window -c "#{pane_current_path}" # new window preserve path
bind -n S-down new-window -c "#{pane_current_path}" # new window preserve path
bind -n S-left prev # goto the previous window
bind -n S-right next # goto the next window

bind-key h swap-window -t :- # move window to the left
bind-key l swap-window -t :+ # move window to the right

##############################
# seemless integration between vim & tmux keybindings
##
is_vim="ps -o state= -o comm= -t '#{pane_tty}'\
  | grep -iqE '^[^TXZ]+ +(\\S+\V)?g?(view\n?vim?x?)(diff)?$'"
bind-key -n C-h if-shell "$is_vim" "send-keys C-h" "select-pane -L"
bind-key -n C-j if-shell "$is_vim" "send-keys C-j" "select-pane -D"
bind-key -n C-k if-shell "$is_vim" "send-keys C-k" "select-pane -U"
bind-key -n C-l if-shell "$is_vim" "send-keys C-l" "select-pane -R"
bind-key -n C-\ if-shell "$is_vim" "send-keys C-\\" "select-pane -l"

##############################
# smart pane switching
##
bind-key -n C-h run "(tmux display-message -p '#{pane_current_command}' | grep -iqE '(vim|emacs)' && tmux send-keys C-h) || tmux select-pane -L"
bind-key -n C-j run "(tmux display-message -p '#{pane_current_command}' | grep -iqE '(vim|emacs)' && tmux send-keys C-j) || tmux select-pane -D"
bind-key -n C-k run "(tmux display-message -p '#{pane_current_command}' | grep -iqE '(vim|emacs)' && tmux send-keys C-k) || tmux select-pane -U"
bind-key -n C-l run "(tmux display-message -p '#{pane_current_command}' | grep -iqE '(vim|emacs)' && tmux send-keys C-l) || tmux select-pane -R"

##############################
# TERM adapting tmux to work with term title bars
##
set -g set-titles on # set-titles-string      
set -g set-titles-string "#{session_name} - #T"

# rename window / pane to current job / process.
# NOTE: doesn't play nice with `Terminal.app`
set -g automatic-rename on

##############################
##
set -g monitor-activity on # highlight windows that have unseen activity
set -g visual-activity off # don't show a notification when there is activity in a window


##############################
# Logging
##
bind P pipe-pane -o "cat >>~/logs/#W.tmux.log" \; display "Toggled logging to ~/logs/#W.tmux.log"

##############################
# `reattach-to-user-namespace`
##
if-shell "uname | grep -q Darwin" "source-file ~/.tmux/tmux.macos.conf"

##############################
# tmux 1.9+ SEE: `man tmux` for more info
## 
set-option -g focus-events on

##############################
# tmux - plugins
##
set -g @plugin "tmux-plugins/tpm"
set -g @plugin "tmux-plugins/tmux-resurrect"

##############################
# tmux > plugin > tmux-resurrect
##
set -g @resurrect-strategy-nvim 'session'

# Initialize TMUX plugin manager 
# 🚨 ALERT: 👇 must be last line in `tmux.conf`
run "~/.tmux/plugins/tpm/tpm"
