#-----------------------------
# ~ 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.
##

# remove all previous bindings, useful when exp with new bindings,
# and old ones aren't removed, see: https://unix.stackexchange.com/a/57648/33002
# unbind-key -a

# 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 terminfo file
set-option -sa terminal-overrides ',xterm*:Tc' # enable 24 bit color
#
# NOTE: setting this to `fish` solves my issues related to out of order $PATH entries
# REF: https://unix.stackexchange.com/a/548516/33002
# NOTE: TODO: below setting may be problematic if the fish shell is not installed on box
set-option -g default-command fish

## nova's
# 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 show -g`
##
# NOTE: `set` is short for `set-option`, it is an internal alias within tmux.
# set -g utf8 on #INVALID, nova
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
### TODO: see repo README, for style window titles to fit zoom icon into empty space
### ...presently zoom icon appends to window name, thus moving layout of all text in status bar ...BOO!
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
##
# TODO: tmux requires adding single quotes around `-` and `\` whereas prior versions did not
# ...time permitting conditionally change settings based tmux version if not backward compatible

# split pane and preserve path

bind '-' split-window -v -c "#{pane_current_path}" 
bind '\' split-window -h -c "#{pane_current_path}" 

# exp, get pane from different window and put within the current window
# TODO: provide example of how cmd works
bind-key @ command-prompt -p "create pane from:"  "join-pane -s ':%%'"

#-----------------------------
# 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
##
# NOTE: going to use below binding to create a new nested tmux session
# bind -n C-n new-window -c "#{pane_current_path}" # new window preserve path
#
# Create a new nested tmux (prefix + ctrl + n)
bind C-n new-window ~/.tmux.conf.d/nested-tmux/new-tmux \; \
  rename-window '' \; \
  command-prompt -I "#W" "rename-window -- '%%'"

bind -n S-down new-window -c "#{pane_current_path}" # new window preserve path
# bind -n C-down new-window -c "#{pane_current_path}" # new window preserve path
# bind-key -n space next-window # goto the next window
# bind-key -n bspace previous-window # goto the previous window
bind -n S-right next-window
bind -n S-left previous-window

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

# NOTE: TODO: tmp disable binding, to exp with switching between present and last window
# bind-key l swap-window -t :+ # move window to the right
bind-key l last-window # switch to previous window

#-----------------------------
# seemless integration between vim & tmux keybindings
#-
# Smart pane switching with awareness of Vim splits.
# See: https://github.com/christoomey/vim-tmux-navigator

# See: https://github.com/christoomey/vim-tmux-navigator/issues/218#issuecomment-495002332
is_vim="ps -o state= -o comm= -t '#{pane_tty}' \
    | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|vim|nvim|ssh|mosh|tmux?)(diff)?$'"

# is_vim="ps -o state= -o comm= -t '#{pane_tty}' \
    | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?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'
tmux_version='$(tmux -V | sed -En "s/^tmux ([0-9]+(.[0-9]+)?).*/\1/p")'
if-shell -b '[ "$(echo "$tmux_version < 3.0" | bc)" = 1 ]' \
    "bind-key -n 'C-\\' if-shell \"$is_vim\" 'send-keys C-\\'  'select-pane -l'"
if-shell -b '[ "$(echo "$tmux_version >= 3.0" | bc)" = 1 ]' \
    "bind-key -n 'C-\\' if-shell \"$is_vim\" 'send-keys C-\\\\'  'select-pane -l'"

bind-key -T copy-mode-vi 'C-h' select-pane -L
bind-key -T copy-mode-vi 'C-j' select-pane -D
bind-key -T copy-mode-vi 'C-k' select-pane -U
bind-key -T copy-mode-vi 'C-l' select-pane -R
# bind-key -T copy-mode-vi 'C-\' select-pane -l 

#------------------------------
#- smart pane switching 
#- NOTE:  🔥 below key mappings are req'd for switching between panes when using ssh
#--
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 or pane to current job or 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` macos specific
# NOTE: reattach-to-user-namespace is not required for tmux ≥ 2.6
# TODO: is possible to check if tmux ≥ 2.6 and conditionally not load config
##
if-shell "uname | grep -q Darwin" "source-file ~/.tmux/tmux.macos.conf"


##############################
# double tap, doubletap left shift
##
# NOTE: below option requires `-g` flag
# NOTE: this broke when tmux was upgraded to 3.2a i believe
# TODO: fix broken double tap
set-option -g prefix2 "C-S-M-f1" # WORK!!!
bind-key C-S-M-f1 resize-pane -Z # toggle min/max pane in window, prefix not required
# double tap left shift relies on ke for macos

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


##############################
# tmux / DANGER ZONE!!!! / place to break current tmux config, and store experimental features
##

# NOTE: list available tmux commands
# tmux: list-commands
# :list-commands
  # move-pane
  # resize-pane

# bind-key -n F11 display "F11 pressed" 
# unbind-key -n F11 # NOTE: the `-n` flag is required

# TODO: eventually want `F12` to toggle between local and remote / nested tmux session
# bind-key -n F12 display "F12 pressed" 
#SEE: https://gist.github.com/ipatch/514e60d8eae01d7e461e68305d96316d for why `bind-key` is required
unbind-key -n F12 # NOTE: the `-n` flag is required

# TODO: tmux add aditional key binding
# TODO: tmux add keypress then keypress binding
# bind -n shift shift resize-pane

# NOTE: `bind -T root` == `bind -n`

# bind -T root F12 \
# bind -T root '"\e[24~"' \
# bind -n F12
# set prefix None \;\
# set key-table off \;\
# adjust color of status bar
# set -g status-bg "#3e93b2" # default `blue background color"
# set -g status-bg "#9b9b9b" # light grey # LINK: https://www.color-hex.com/color-wheel/

# if-shell -F '#{pane_in_mode}' 'send-keys -X cancel' \;\
refresh-client -S \;\

# bind-key -n off F12 \
set -u prefix \;\
set -u key-table \;\
# set -u status-style \;\

# set -g status-bg "#3e93b2" # default `blue background color"

# refresh-client -S


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