# ~/.config/tmux/tmux.conf

# ┌
# │ NOTE: $KITTY_LISTEN_ON is not being updated no matter
# │ how I try
# │ unless the server is killed and restarted.
# │ On macOS, I accidentally Cmd+Q often and therefore lose
# │ the socket
# └
set -g update-environment -r
setenv -g KITTY_LISTEN_ON $KITTY_LISTEN_ON
set -ag update-environment 'KITTY_LISTEN_ON'

# needed for nvim zen-mode
set -g allow-passthrough on

set -g escape-time 0
set -g default-terminal "tmux-256color"
set -ag terminal-overrides "screen-256color,xterm-256color:Tc,xterm-kitty:Tc,alacritty:Tc"
set -g history-limit 150000
set -g display-time 2500
set -g status-interval 10
set -g focus-events on
set -g status-position top

# SSH agent forwarding
set-environment -g SSH_AUTH_SOCK $SSH_AUTH_SOCK

# activity
set -g monitor-activity off
set -g visual-activity off

# Start indexes at 1 instead of 0
set -g base-index 1
set -g pane-base-index 1
set -g renumber-windows on

# Set term title to current session window
set -g set-titles on
set -g set-titles-string "#I: #W"

set -g automatic-rename on
set -g allow-rename on

# Change prefix from ctrl+b to ctrl+a
set -g prefix C-a
unbind C-b
# nested session handling?
bind-key C-a last-window #send-prefix
bind-key a send-prefix

# Split windows with | and -
unbind %
unbind '"'
bind-key -N "Add a new pane to the right of the currently active pane" | split-window -h -c "#{pane_current_path}"
bind-key -N "Add a new pane under the currently active pane" - split-window -v -c "#{pane_current_path}"

# Reload config
unbind R
bind-key -N "Reload tmux configuration" R source-file ~/.config/tmux/tmux.conf \; display "Config reloaded."

# Toggle status bar
bind-key -N "Toggle the status bar for current session" S set status

# switch to last session
bind-key BTab switch-client -l

# Pane navigation
bind-key -N "Navigate one pane to the left" -n C-h select-pane -L
bind-key -N "Navigate one pane down" -n C-j select-pane -D
bind-key -N "Navigate one pane up" -n C-k select-pane -U
bind-key -N "Navigate one pane to the right" -n C-l select-pane -R

# swap panes
bind-key > swap-pane -D
bind-key < swap-pane -U

# Resize panes
bind-key -N "Resize pane left" -r h resize-pane -L 5
bind-key -N "Resize pane down" -r j resize-pane -D 5
bind-key -N "Resize pane up" -r k resize-pane -U 5
bind-key -N "Resize pane right" -r l resize-pane -R 5

# window layout
bind-key -N "Cycle window layout" enter next-layout
bind-key -N "Rotate panes clockwise around window" -r 'M-\' rotate-window

# Fullscreen current pane
bind-key -N "Zoom current pane (fill window)" -r m resize-pane -Z

# create new windows in the same cwd
bind-key -N "Create a new window within CWD" c new-window -c "#{pane_current_path}"

# Prompt to rename new windows on creation
# set-hook -g after-new-window 'command-prompt -I "#{window_pane}" "rename-window '%%'"'

# Window navigation
bind-key -N "Move to previous window" -n C-p previous-window
bind-key -N "Move to next window" -n C-n next-window

# selection mode
set -g mouse on
setw -g mode-keys vi
setw -g status-keys vi

unbind -T copy-mode-vi Space;
unbind -T copy-mode-vi Enter;
unbind -T copy-mode-vi MouseDragEnd1Pane

bind-key -N "Enter copy mode" -T copy-mode-vi [ copy-mode
bind-key -N "Begin selection" -T copy-mode-vi v send -X begin-selection
bind-key -N "Copy selection to buffer" -T copy-mode-vi y send -X copy-selection
bind-key -N "Copy selection and paste" -T copy-mode-vi Y send -X copy-selection-and-cancel
bind-key -N "Select to start of line" -T copy-mode-vi _ send -X start-of-line
bind-key -N "Select to end of line" -T copy-mode-vi $ send -X end-of-line
bind-key -N "Cancel selection" -T copy-mode-vi Escape send -X cancel

# paste buffers
bind-key b list-buffers    # list buffers
bind-key p paste-buffer -p # paste from buffer
bind-key P choose-buffer   # choose a buffer to paste from

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

set -g @plugin 'tmux-plugins/tpm'

set -g @plugin 'christoomey/vim-tmux-navigator'
set -g @plugin 'cdanesi/nordtheme-tmux'
set -g @plugin 'tmux-plugins/tmux-battery'   # display remaining battery pct
set -g @plugin 'tmux-plugins/tmux-yank'
set -g @plugin 'tmux-plugins/tmux-resurrect' # persist tmux sessions across reboot
set -g @plugin 'tmux-plugins/tmux-continuum' # automatically saves session every 15 mins
set -g @plugin 'laktak/extrakto'             # fzf search in tmux (prefix+tab)
set -g @plugin 'sainnhe/tmux-fzf'            # <prefix> F
set -g @plugin 'niqodea/tmux-matryoshka'     # nested tmux (def F1-F3)

# Session restore options
set -g @continuum-boot 'off'       # automatically start tmux session on boot
set -g @continuum-boot-options 'alacritty,fullscreen'    # start tmux in fullscreen alacritty on macOS
set -g @continuum-restore 'on'
set -g @resurrect-strategy-nvim 'session'
set -g @resurrect-capture-pane-contents 'on'
set -g @resurrect-processes 'ssh nvim man less tail btop htop vi vim npm npx' # space separated list of programs to restore

set -g @matryoshka_down_keybind 'M-d' # keybind to disable outer-most active tmux
set -g @matryoshka_up_keybind 'M-e' # keybind to enable inner-most inactive tmux
set -g @matryoshka_up_recursive_keybind 'M-U' # keybind to recursively enable all tmux instances
set -g @matryoshka_inactive_status_style 'fg=default,bg=brightblack' # TODO: Try to make this look better..
# set -g @matryoshka_status_style_option 'my-status-style'

# use fzf to switch sessions
unbind s
bind-key -N "Attach to another session" s run-shell -b "$HOME/.config/tmux/plugins/tmux-fzf/scripts/session.sh switch"
# use fzf to switch windows
unbind w
bind-key -N "Switch to another window (works across any session)" w run-shell -b "$HOME/.config/tmux/plugins/tmux-fzf/scripts/window.sh switch"

##### FZF settings
TMUX_FZF_SESSION_FORMAT="#{session_windows} windows"

bind-key -N "User FZF menu" M run-shell -b "$HOME/.config/tmux/plugins/tmux-fzf/scripts/menu.sh"

# needs 1 or 3+ entries.
TMUX_FZF_MENU=\
"rename window\n$HOME/.config/tmux/plugins/tmux-fzf/scripts/window.sh rename\n"\
"switch session\n$HOME/.config/tmux/plugins/tmux-fzf/scripts/session.sh switch\n"\
"change pane layout\n$HOME/.config/tmux/plugins/tmux-fzf/scripts/pane.sh layout\n"\
"break out pane\n$HOME/.config/tmux/plugins/tmux-fzf/scripts/pane.sh break\n"\
"join pane\n$HOME/.config/tmux/plugins/tmux-fzf/scripts/pane.sh join\n"\
"session options\n$HOME/.config/tmux/plugins/tmux-fzf/scripts/session.sh\n"\
"window options\n$HOME/.config/tmux/plugins/tmux-fzf/scripts/window.sh\n"\
"pane options\n$HOME/.config/tmux/plugins/tmux-fzf/scripts/pane.sh\n"\
"new session\n$HOME/.config/tmux/plugins/tmux-fzf/scripts/session.sh new\n"

run '~/.config/tmux/plugins/tpm/tpm'