# Environment
# Reload the profile as a system update could have happened since tmux was launched.
set-environment -g -r __ETC_PROFILE_DONE
set-environment -g -r __NIXOS_SET_ENVIRONMENT_DONE
set-environment -g -r __HM_SESS_VARS_SOURCED

# Terminal
set -g default-terminal "screen-256color"
#set -g default-terminal "tmux"

# Activity
set -g monitor-activity off # NOTE: disabled to keep the bell working
set -g visual-activity off

# Bell
set -g bell-action any
set -g visual-bell off
#set -g bell-on-alert on

# Encoding
# DEPRECATED: set -g utf8 on
# DEPRECATED: set -g status-utf8 on

# Keys
set -g mode-keys vi
setw -g xterm-keys on

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

# Setting the prefix from C-b to C-a.
set -g prefix C-a
# Free the original Ctrl-b prefix keybinding.
unbind C-b
unbind-key C-b

# Window
bind C-c new-window -c "#{pane_current_path}"
bind C-p previous-window
bind p   previous-window
bind C-n next-window
bind n   next-window
bind -N 'Select window 8'  8 select-window -t:=8
bind -N 'Select window 10' 0 select-window -t:=10

# Buffer
bind Space choose-buffer

# Ensure that we can send Ctrl-a to other apps.
bind a send-prefix
bind-key a send-prefix

# Copy/Paste
bind Escape copy-mode
unbind [
unbind v
bind v paste-buffer -p
#bind -t vi-copy 'v' begin-selection
#bind -t vi-copy 'y' copy-selection
#bind -t vi-copy 'Space' halfpage-down
#bind -t vi-copy 'Bspace' halfpage-up

# Fill Copy/Paste clipboard
set -ga terminal-override ',rxvt-uni*:XT:Ms=\E]52;%p1%s;%p2%s\007'

# Allow xterm titles in terminal window, terminal scrolling with scrollbar,
# and setting overrides of C-Up, C-Down, C-Left, C-Right
#set -g terminal-overrides "xterm*:XT:smcup@:rmcup@:kUP5=\eOA:kDN5=\eOB:kLFT5=\eOD:kRIT5=\eOC"

# ICCCM clipboard
bind Y run "tmux save-buffer - | xclip -i -sel clipboard"
bind P run "tmux set-buffer \"$(xclip -o -sel clipboard)\"; tmux paste-buffer -p"
#bind -T copy-mode-vi Enter send-keys -X copy-pipe-and-cancel "xclip -i -f -selection primary | xclip -i -selection clipboard"
#bind y run-shell "tmux show-buffer | xclip -sel clip -i" \; display-message "Copied tmux buffer to system clipboard"

# Reload the file with Ctrl-a+r
bind r source-file ~/.tmux.conf \; display "Reloaded!"

# Splitting panes
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"

# Moving between panes
bind j select-pane -L
bind k select-pane -D
bind i select-pane -U
bind l select-pane -R

# Moving between windows
bind -r C-j select-window -t :-
bind -r C-l select-window -t :+
bind-key C-a last-window

# Pane resizing
bind -r J resize-pane -L 5
bind -r K resize-pane -D 5
bind -r I resize-pane -U 5
bind -r L resize-pane -R 5
# Set current window pane to 80 columns
bind-key 8 resize-pane -x 80

# Maximize and restore a pane
unbind z
bind z resize-pane -Z

# Log output to a text file on demand.
#bind P pipe-pane -o "cat >>~/#W.log" \; display "Toggled logging to ~/#W.log"

#
## Mouse
###
set -g mouse on
# DEPRECATED: set -g mode-mouse on
# DEPRECATED: set -g mouse-select-pane on
# DEPRECATED: set -g mouse-resize-pane on
# DEPRECATED: set -g mouse-select-window on

bind-key m set -g mouse on  \; display 'Mouse: ON'
bind-key M set -g mouse off \; display 'Mouse: OFF'

# tmux < 2.5
#bind -t vi-copy y copy-pipe 'xclip -in -selection clipboard'
# tmux >= 2.5
#bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'xclip -in -selection clipboard'

# To copy, left click and drag to highlight text in yellow, 
# once you release left click yellow text will disappear and will automatically be available in clibboard
# Update default binding of `Enter` to also use copy-pipe
#unbind -T copy-mode-vi Enter
#bind-key -T copy-mode-vi Enter send-keys -X copy-pipe-and-cancel "xclip -selection c"
#bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "xclip -in -selection clipboard"

# Mouse scroll
# see https://github.com/tmux/tmux/issues/145#issuecomment-150736967
#bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'select-pane -t=; copy-mode -e; send-keys -M'"
#bind -n WheelDownPane select-pane -t= \; send-keys -M
#bind -n C-WheelUpPane select-pane -t= \; copy-mode -e \; send-keys -M
#bind -t vi-copy    C-WheelUpPane   halfpage-up
#bind -t vi-copy    C-WheelDownPane halfpage-down

#bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'select-pane -t=; copy-mode -e; send-keys -M'"
#bind -n WheelDownPane select-pane -t= \; send-keys -M
#bind -n C-WheelUpPane select-pane -t= \; copy-mode -e \; send-keys -M
#bind -T copy-mode-vi    C-WheelUpPane   send-keys -X halfpage-up
#bind -T copy-mode-vi    C-WheelDownPane send-keys -X halfpage-down
#bind -T copy-mode-emacs C-WheelUpPane   send-keys -X halfpage-up
#bind -T copy-mode-emacs C-WheelDownPane send-keys -X halfpage-down

#
## Style
###

# pane
set -g pane-base-index 1
set -g pane-border-style 'fg=green,bg=black'
set -g pane-active-border-style 'fg=white,bg=yellow'
set -g pane-border-style 'fg=colour245'
set -g pane-active-border-style 'bg=colour39'
set -g pane-active-border-style 'fg=green,bg=black'
set -g pane-border-style 'fg=white,bg=black'
#set -g pane-border-status bottom
#set -g pane-border-format " #P: #{pane_current_command} "
#set -g pane-border-format " #P: #{pane_current_path} "

# window
setw -g base-index 1
setw -g aggressive-resize on
setw -g window-size latest
setw -g window-status-activity-style 'fg=yellow'
setw -g window-status-bell-style 'fg=red'
setw -g window-status-current-format "#I #W "
setw -g window-status-current-style 'fg=white'
setw -g window-status-format "#I #W "
setw -g window-status-style 'fg=blue,bg=black'

# window title
setw -g set-titles on
setw -g set-titles-string "#T"
setw -g allow-rename on
setw -g automatic-rename off
#set -g automatic-rename-format '#{b:pane_current_path}'

# message
set -g message-style 'fg=black,bg=green'

# status
set -g status-left-length 52
set -g status-right-length 451
set -g status-justify left
set -g status-style 'fg=white,bg=black'
set -g status-left-length 40
set -g status-right-length 80
set -g status-left ""
set -g status-right '#[fg=red]#h'
#set -g status-interval 5
#set -g status-justify centre

# Solarized color.
#source-file ~/.tmux/vendor/tmux-colors-solarized/tmuxcolors.conf

# plugins
## List of plugins
#set -g @plugin 'tmux-plugins/tpm'
#set -g @plugin 'tmux-plugins/tmux-sensible'
#set -g @plugin 'tmux-plugins/tmux-yank'
#set -g @yank_selection 'clipboard' # 'primary', or 'secondary' or 'clipboard'

# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
#run -b '~/.tmux/plugins/tpm/tpm'