# Nested TMUX sessions (from
# https://medium.freecodecamp.org/tmux-in-practice-local-and-nested-remote-tmux-sessions-4f7ba5db8795)
bind -T root F11 \
        set prefix None \;\
        set key-table off \;\
        set status-right "#[fg=colour0,bg=colour1] DISABLED #[fg=colour3,bg=colour0] #{session_name}#[fg=colour1] | #[fg=colour2]%d %b %l:%M %p "
        refresh-client -S \;\

bind -T off F11 \
        set -u prefix \;\
        set -u key-table \;\
        set -u status-right \;\
        refresh-client -S
# improve colors
set -g default-terminal 'tmux-256color'
set -as terminal-overrides ',xterm*:Tc:smso=\E[3m'
set -g -a terminal-overrides ',*:Ss=\E[%p1%d q:Se=\E[2 q'
set-option -g allow-rename off
set-window-option -g automatic-rename off

# Status bar
set -g set-titles 'off'
set -g status-position 'top'
set -g status-left ""
set -g status-right-length 200
set -g status-right "#[fg=colour0,bg=colour5] vsim #[fg=colour0,bg=colour15] #(ps hux -u $(whoami) | grep 'vish' | grep -v -E 'grep|view' | wc -l) #[fg=colour3,bg=colour0] #[fg=colour0,bg=colour3] load #[fg=colour0,bg=colour15] #(cat /proc/loadavg | awk '{ print $3; }' | xargs printf '%2.2f') #[fg=colour3,bg=colour0] #[fg=colour0,bg=colour2] mem #[fg=colour0,bg=colour15] #(ps haux | awk -v user=$(whoami) '$1 ~ user { sum += $4 } END { print sum; }' | xargs printf '%2.2f')% #[fg=colour3,bg=colour0] #[fg=colour0,bg=colour1] time #[fg=colour0,bg=colour15] %d %b %l:%M %p #[fg=colour3,bg=colour0] #[fg=colour15,bg=colour8] #{session_name} #[fg=colour0,bg=colour0] "

# Tabs
setw -g window-status-format " #I #W "
setw -g window-status-style fg=colour15
setw -g window-status-current-format " #I #W "
setw -g window-status-current-style fg=red

set -g status-justify "left"
set -g status-bg "colour0"
set -g status-fg "colour7"
# set -g message-bg "colour0"
# set -g message-fg "colour8"

setw -g window-status-bell-style "fg=colour0,bg=colour5"

bind s split-window -v -c "#{pane_current_path}"
bind v split-window -h -c "#{pane_current_path}"
bind a split-window -v -p 15 -c "#{pane_current_path}"
# bind c new-window -c "#{pane_current_path}"
bind c command-prompt -p "Name:" "new-window -c \"#{pane_current_path}\" -n %%"

# Enable delete in command sequencer
bind h send-keys 'c-h'

# Open man pages in split
bind @ command-prompt -p "Man:" "split-window -v 'exec man %%'"

# act like vim
bind Escape copy-mode
set-window-option -g mode-keys vi
bind-key -T copy-mode-vi 'v' send -X begin-selection
bind-key -T copy-mode-vi 'V' send -X select-line
bind-key -T copy-mode-vi 'r' send -X rectangle-toggle
bind-key -T copy-mode-vi 'y' send -X copy-pipe-and-cancel 'xclip -in -selection clipboard'
unbind-key -T copy-mode-vi MouseDragEnd1Pane
unbind -T copy-mode-vi MouseDragEnd1Pane
# unbind -t vi-copy MouseDragEnd1Pane

# Smart pane switching with awareness of Vim splits.
# See: https://github.com/christoomey/vim-tmux-navigator
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
#act like GNU screen
unbind C-b
set -g prefix C-Space
bind Space send-prefix
set -g escape-time 20

# start window numbers at 1 to match keyboard order with tmux window order
set -g base-index 1

# renumber windows sequentially after closing any of them
set -g renumber-windows on

bind-key r source ~/.tmux.conf

# Fix hightlighting in less
set -ga terminal-overrides ',*:sitm@,ritm@'

# increase scrollback lines
set -g history-limit 100000

setw -g mouse on

set -g focus-events on

# Allow for visual bell when a command completes
set-window-option -g visual-bell on
set-window-option -g bell-action other

# Local config
if-shell "[ -f ~/.tmux.conf.local ]" 'source ~/.tmux.conf.local'

# Plugins
# set -g @plugin 'tmux-plugins/tpm'
# set -g @plugin 'nhdaly/tmux-better-mouse-mode'
# set -g @plugin 'tmux-plugins/tmux-yank'
# set -g @plugin 'Morantron/tmux-fingers'
# set -g @plugin 'laktak/extrakto'
# set -g @plugin 'tmux-plugins/tmux-resurrect'
# set -g @plugin 'tmux-plugins/tmux-continuum'


# Config
set -g @scroll-without-changing-pane "on"
set -g @emulate-scroll-for-no-mouse-alternate-buffer "on"

run-shell ~/.tmux/plugins/tmux-better-mouse-mode/scroll_copy_mode.tmux
run-shell ~/.tmux/plugins/tmux-yank/yank.tmux
run-shell ~/.tmux/plugins/tmux-thumbs/tmux-thumbs.tmux

set -g @thumbs-osc52 0
# Swap behaviour
set -g @thumbs-command 'tmux set-buffer -w -- {} && tmux paste-buffer && tmux display-message \"Copied {}\"'
set -g @thumbs-upcase-command 'tmux set-buffer -w -- {} && tmux display-message \"Copied {}\"'
set -g @thumbs-hint-fg-color red
set -g @thumbs-reverse enabled

set -g word-separators " "
bind -n C-DoubleClick1Pane if -F '#{m/r:^[^:]*:[0-9]+:,#{mouse_word}}' {
  run -C { popup -w90% -h90% -E -d '#{pane_current_path}' '
      nvim `echo #{mouse_word}|awk -F: "{print \"+\"\\$2,\\$1}"`
    ' }
} {
  run -C { popup -w90% -h90% -E -d '#{pane_current_path}' '
      nvim "#{mouse_word}" || read -p "Press key to continue.. " -n1 -s
    ' }
}

bind-key -n 'C-q' popup -w50% -h 90% -x 90% -E -d '#{pane_current_path}' chezmoi cd

bind-key -n 'C-z' if -F '#{window_zoomed_flag}' "resize-pane -Z" "send-keys C-z"

bind-key -n 'C-s' {
  copy-mode
  send -X clear-selection
  send -X start-of-line
  send -X start-of-line
  send -X cursor-up
  send -X cursor-up
  send -X start-of-line
  send -X start-of-line

  if -F "#{m:*->\u00A0*,#{copy_cursor_line}}" {
    send -X search-forward-text "->\u00A0"
    send -X stop-selection
    send -X -N 2 cursor-right
    send -X begin-selection
    send -X end-of-line
    send -X end-of-line
    if "#{m:*->\u00A0?*,#{copy_cursor_line}}" {
      send -X cursor-left
    }
  } {
    send -X end-of-line
    send -X end-of-line
    send -X begin-selection
    send -X search-backward-text "->\u00A0"
    send -X end-of-line
    send -X end-of-line
    send -X cursor-right
    send -X stop-selection
  }
}

bind-key -T copy-mode-vi 'e' send -X copy-pipe-and-cancel "cat > /tmp/error.txt && tmux new-window -n 'quickfix' -c \"#{pane_current_path}\" 'nvim -q /tmp/error.txt && rm -f /tmp/error.txt'"
