# Default shell
set -g default-shell /bin/zsh
set-option -g prefix C-b

# Reload config
unbind r
bind R source-file ~/.config/tmux/tmux.conf; display-message "Config reloaded.."

# Session management
bind S command-prompt -p "New Session:" "new-session -A -s '%%' -c ~"

# List sessions
bind s switch-client -nn

# Popup session selector
bind C-j display-popup -E "tmux list-sessions | sed -E 's/:.*$//' | grep -v \"^$(tmux display-message -p '#S')\$\" | fzf --reverse | xargs tmux switch-client -t"

# Popup window selector with previous window first
bind w display-popup -E "tmux list-windows -F '#{window_index} #{window_name} (#{session_name})' | grep -v \"^$(tmux display-message -p '#I')\" | fzf --reverse | cut -d' ' -f1 | xargs -I{} tmux select-window -t {}"

# Use choose-tree for interactive session and window navigation
bind W choose-tree -Zw

# # Define a shell function to perform the search and display results in a tmux popup
# tmux_search() {
#     display-popup -E -t 3 "$(grep -nri "$1" .)"
# }
#
# # Bind a key to trigger the search function
# bind-key / command-prompt -p "Search term:" "run-shell 'tmux_search \"%%\"'"
# Bind a key to run the switch_or_create_session function
bind -r f run-shell "tmux neww -n sessonizer ~/.local/bin/scripts/sessionizer"

# Reduce delay
set -s escape-time 1

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

# Split panes using h and v
bind b split-pane -h -c "#{pane_current_path}"
bind v split-pane -v -c "#{pane_current_path}"

# Switch panes using Alt-arrow without prefix
bind h select-pane -L
bind l select-pane -R
bind k select-pane -U
bind j select-pane -D

# List windows
bind p previous-window
bind n next-window
bind Y last-window

# Quick renaming
bind C switch-client -n -t "#{session_name}"
bind C-n command-prompt -p "Rename Window: " "rename-window '%%'"
bind C-r command-prompt -p "Rename Pane: " "rename-pane '%%'"

# Automatic renaming panes
setw -g automatic-rename on
set -g automatic-rename-format "#{b:pane_current_path}"

# Resizing panes with Prefix H, J, K, L
bind -r H resize-pane -U 5
bind -r L resize-pane -R 5
bind -r K resize-pane -U 5
bind -r J resize-pane -D 5

# Alternate clear screen prefix + ctrl l
bind C-l send-keys 'C-l'

# Enable activity alerts
setw -g monitor-activity off
set -g visual-activity on

# Remove switching delay
set-option -s escape-time 0

# Increase scrollback buffer size
set-option -g history-limit 50000

# Increase message display timeout
set-option -g display-time 1500

# Enable aggressive resize
set-window-option -g aggressive-resize off

# Seamless navigation with neovim panes
is_vim="ps -o state= -o comm= -t '#{pane_tty}' | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"

# Navigate panes
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'

# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'
set -g @plugin 'rickstaa/tmux-notify'

set -g @tpm_install_dir "$HOME/.config/tmux/plugins"

run-shell "tmux source-file $HOME/.config/tmux/tmux.\${TMUX_THEME:-default}.theme"

set -g status-justify left
set -g status-interval 5
set -g status-left-length 200
set -g status-right-length 200
set -g status-position top


set -g @continuum-save-interval 15
set -g @continuum-restore 'on'
set -g @continuum-save-path "$HOME/.config/tmux/resurrect"

set -g @notify_show_alerts 'on'
set -g @notify_show_messages 'on'
set -g @notify_show_activity 'on'
set -g @notify_show_monitor_activity 'on'

set -g @resurrect-capture 'C'
set -g @resurrect-save 'R'
set -g @resurrect-save-script 'yes'
set -g @resurrect-save-path "$HOME/.config/tmux/resurrect"

# Renaming Session the current dir's name
set-option -g automatic-rename on
set-option -g automatic-rename-format '#{b:pane_current_path}'

# Default TMUX keybindings
bind c new-window -c '#{pane_current_path}'
bind '"' split-window -h -c '#{pane_current_path}'
bind % split-window -v -c '#{pane_current_path}'
bind h select-pane -L
bind l select-pane -R
bind Left select-pane -L
bind Right select-pane -R
bind Up select-pane -U
bind Down select-pane -D
bind C-s setw synchronize-panes
bind Space select-layout next
bind Tab select-pane -t:.+
bind q display-panes
bind -n C-b select-pane -T:.+
bind z resize-pane -Z
bind ! break-pane
bind -r H resize-pane -U 5
bind -r L resize-pane -R 5
bind x kill-pane

# Copy mode key mappings
setw -g mode-keys vi

# Specify a custom key combination to exit copy mode
bind-key -T copy-mode-vi Escape send-keys -X cancel

# Navigation
bind-key -T copy-mode-vi 'h' send-keys -X cursor-left
bind-key -T copy-mode-vi 'j' send-keys -X cursor-down
bind-key -T copy-mode-vi 'k' send-keys -X cursor-up
bind-key -T copy-mode-vi 'l' send-keys -X cursor-right
bind-key -T copy-mode-vi 'w' send-keys -X next-word
bind-key -T copy-mode-vi 'b' send-keys -X previous-word

# Search
bind-key -T copy-mode-vi '/' send-keys -X search-forward
bind-key -T copy-mode-vi '?' send-keys -X search-backward
bind-key -T copy-mode-vi 'n' send-keys -X search-again
bind-key -T copy-mode-vi 'N' send-keys -X search-again-backward

# Scrolling
# Change tpm dowload location
bind-key -T copy-mode-vi 'C-u' send-keys -X halfpage-up
bind-key -T copy-mode-vi 'C-d' send-keys -X halfpage-down

# Selection
bind-key -T copy-mode-vi 'v' send-keys -X begin-selection
bind-key -T copy-mode-vi 'y' send-keys -X copy-pipe "xclip -selection clipboard"

# Movement
bind-key -T copy-mode-vi 'g' send-keys -X start-of-line
bind-key -T copy-mode-vi 'G' send-keys -X end-of-line

# Copying and Pasting
bind-key -T copy-mode-vi 'space' send-keys -X set-mark
bind-key -T copy-mode-vi 'y' send-keys -X copy-pipe "xclip -selection clipboard"

# Miscellaneous
bind-key -T copy-mode-vi '?' send-keys -X list-keys
bind-key -T copy-mode-vi ':' command-prompt
bind-key -T copy-mode-vi 'F5' send-keys -X show-buffer

bind-key ? list-keys
bind-key : command-prompt
bind-key -n F5 show-buffer

run '$HOME/.config/tmux/plugins/tpm/tpm'

