##########################################
### Defaults 
##########################################

# get tmux version
run-shell "tmux setenv -g TMUX_VERSION $(tmux -V | cut -c 6-)"

# bind ctrl+a to tmux prefix 
unbind C-b
set -g prefix C-a
bind C-a send-prefix

# terminal mode
set -g default-terminal "screen-256color"

# don't detach tmux when killing a session
set-option -g detach-on-destroy off

# Scroll History
set-option -g history-limit 10000

# keep tmux msgs around longer
set -g display-time 2000

# Set ability to capture on start and restore on exit window data when running an application
setw -g alternate-screen on

# Lower escape timing from 500ms to 50ms for quicker response to scroll-buffer access.
set -s escape-time 50

# loud or quiet?
set-window-option -g monitor-activity off
set-option -g visual-activity off
set-option -g visual-bell off
set-option -g visual-silence off
set-option -g bell-action none

# set mouse modes
if-shell '[[ $(echo "$(tmux -V | cut -d" " -f2) >= 2.1" | bc) -eq 1 ]]' \
	'set -g mouse on' \
	'set -g mode-mouse on; set -g mouse-resize-pane on; set -g mouse-select-pane on; set -g mouse-select-window on'

##########################################
### Styling
##########################################

# panes
##mf##set -g pane-border-bg colour235
##mf##set -g pane-border-fg colour238
##mf##set -g pane-active-border-bg colour236
##mf##set -g pane-active-border-fg colour51

# status line
# UTF8 is autodetected in 2.2 onwards, but errors if explicitly set
if-shell '[[ $(echo "$(tmux -V | cut -d" " -f2) < 2.2" | bc) -eq 1 ]]' \
	'set -g status-utf8 on;'

set -g status-justify left
set -g status-interval 2
set -g status-bg colour234
set -g status-fg colour137

# status-left
set -g status-left-length 35
set -g status-left "#[fg=colour233,bg=colour229,bold] #(whoami)#[fg=colour229,bg=colour139,bold]#[fg=colour233,bg=colour139,bold] #h #[fg=colour139,bg=colour66,bold] #[fg=colour233,bg=colour66,bold]#(curl ipecho.net/plain;echo)#[fg=colour66,bg=colour234,bold]"

# status-right
set -g status-right-length 60
set -g status-right "#{prefix_highlight} #[fg=colour234,bg=colour66,bold] #[fg=colour233,bg=colour66,bold]#(tmux-mem-cpu-load -g 5 --interval 1)#[fg=colour66,bg=colour24,bold] #[fg=colour233,bg=colour24,bold]%H:%M:%S "

# messaging
##mf##set -g message-attr bold
##mf##set -g message-fg colour232
##mf##set -g message-bg colour166
##mf##set -g message-command-fg blue
##mf##set -g message-command-bg black

##mf###window mode
##mf##setw -g clock-mode-colour colour135
##mf##setw -g mode-attr bold
##mf##setw -g mode-fg colour196
##mf##setw -g mode-bg colour238

##mf### window status
##mf##setw -g window-status-current-fg colour81
##mf##setw -g window-status-current-bg colour238
##mf##setw -g window-status-current-attr bold
##mf##setw -g window-status-current-format '#[fg=colour234,bg=colour238,bold]#[fg=colour138,bg=colour238,bold] #I#[fg=colour250]:#[fg=colour255]#W#[fg=colour50]#F#[fg=colour238,bg=colour234,bold]'

##mf##setw -g window-status-fg colour138
##mf##setw -g window-status-bg colour235
##mf##setw -g window-status-attr none
##mf##setw -g window-status-format '#[fg=colour234,bg=colour235,bold]#[fg=colour138,bg=colour235,bold] #I#[fg=colour237]:#[fg=colour250]#W#[fg=colour244]#F#[fg=colour235,bg=colour234,bold]'

##mf##setw -g window-status-bell-attr bold
##mf##setw -g window-status-bell-fg colour255
##mf##setw -g window-status-bell-bg colour1

setw -g window-status-separator ''

##########################################
### some tmux tweaks
##########################################

# Use vim mode keys
set-option -g status-keys vi
set-option -g mode-keys vi

##mf### Setup 'v' to begin selection as in Vim
##mf##bind-key vi-copy v begin-selection
##mf##bind-key vi-copy y copy-pipe "reattach-to-user-namespace pbcopy"

# Update default binding of `Enter` to also use copy-pipe
##mf##unbind -t vi-copy Enter
##mf##bind-key -t vi-copy Enter copy-pipe "reattach-to-user-namespace pbcopy"

# split panes using | and -, open new pane in current directory
bind | send-keys " ~/.tmux/newpanes -h" C-m
bind - send-keys " ~/.tmux/newpanes -v" C-m
unbind '"'
unbind %

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

# reload config file (change file location to your the tmux.conf you want to use)
bind r source-file ~/.tmux.conf \; display "tmux config reloaded..."

# Setup 'x' zoom into a panes (maximize) an save/restore other panes in the current window
#bind x run ". ~/.tmux/zoom"

# set terminal title
set -g set-titles on
set -g set-titles-string '#(whoami)@#h | #(curl ipecho.net/plain;echo)'

# duplicate input to all panes in the same window
bind S set-window-option synchronize-panes on
bind s set-window-option synchronize-panes off

# open a man page in new pane or window
bind m command-prompt -p "man page:" "split-window -h 'exec man %%'"
bind M command-prompt -p "Man page:" "new-window -n 'man %1' 'exec man %1'"

# Double tapping the prefix jumps to last window
bind-key C-a last-window

# R for reorder windows 1 3 6 9 => 1 2 3 4
bind R move-window -r\; display-message "Windows reordered..."

# special settings for MacOS
##if-shell 'test "$(uname)" = "Darwin"' 'source ~/.tmux-osx.conf'

##########################################
### Plugins
##########################################

# prefix_highlight (https://github.com/tmux-plugins/tmux-prefix-highlight)
run-shell ~/.tmux/plugins/tmux-prefix-highlight/prefix_highlight.tmux
set -g @prefix_highlight_fg 'white' # default is 'colour231'
set -g @prefix_highlight_bg 'blue'  # default is 'colour04'
set -g @prefix_highlight_show_copy_mode 'on'
set -g @prefix_highlight_copy_mode_attr 'fg=black,bg=yellow,bold' # default is 'fg=default,bg=yellow'

# tmux-copycat (https://github.com/tmux-plugins/tmux-copycat)
run-shell ~/.tmux/plugins/tmux-copycat/copycat.tmux
set -g @copycat_search_C-d "[[:digit:]]+"
set -g @copycat_search_C-f "(^|^\.|[[:space:]]|[[:space:]]\.|[[:space:]]\.\.|^\.\.)[[:alnum:]~_-]*/[][[:alnum:]_.#$%&+=/@-]*"
set -g @copycat_search_C-u "(https?://|git@|git://|ssh://|ftp://|file:///)[[:alnum:]?=%/_.:,;~@!#$&()*+-]*"
set -g @copycat_search_M-h "\b[0-9a-f]{7,40}\b"
set -g @copycat_search_M-i "[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}"

# tmux-resurrect (https://github.com/tmux-plugins/tmux-resurrect)
run-shell ~/.tmux/plugins/tmux-resurrect/resurrect.tmux
set -g @resurrect-save 'a'
set -g @resurrect-restore 'A'

set -g @resurrect-strategy-vim 'session'      # save vim sessions (may require the tpope/vim-obsession plugin)
set -g @resurrect-strategy-nvim 'session'     # save neovim sessions (may require the tpope/vim-obsession plugin)
set -g @resurrect-capture-pane-contents 'on'  # save pane contents
set -g @continuum-restore 'on'                # restore session on tmux start
set -g @continuum-save-interval '5'           # save session every 5 minutes (default is 15)