# Dynamic configuration file generated by ~/Makefile from /home/sunny/.tmux.conf.erb
#
#     DO NOT EDIT THIS FILE BY HAND --
#     YOUR CHANGES WILL BE OVERWRITTEN
#



bind-key R source ~/.tmux.conf \; display-message 'config reloaded!'

#-----------------------------------------------------------------------------
# terminal
#-----------------------------------------------------------------------------

# enable mouse support for general selection and control
set-option -g mouse on

# auto-set terminal title to current window pane's title
set-option -g set-titles on

# enable 256-color support for pretty colorschemes in Vim
set-option -g default-terminal 'screen-256color'

# allow Vim to receive focus events from terminal window
set-option -g focus-events on

# allow Vim to recieve modifier keys: Shift, Control, Alt
set-window-option -g xterm-keys on

# prevent tmux from catching modifier keys meant for Vim
set-option -s escape-time 0

# enable 24-bit true color RGB escape sequences under st
# https://sunaku.github.io/tmux-24bit-color.html
set-option -ga terminal-overrides ',st-256color:Tc'
set-option -ga terminal-overrides ',xterm-256color:Tc' # hterm (ChromeOS)

# allow set-titles to change the window title under XTerm
# http://opennomad.com/content/goodbye-screen-hello-tmux
set-option -ga terminal-overrides ',xterm*:XT'

# allow set-titles to change the window title under XTerm
# http://opennomad.com/content/goodbye-screen-hello-tmux
# http://stackoverflow.com/questions/15195624
set-option -ga terminal-overrides ',st-256color:smkx=\E='

# yank to system clipboard rather than primary selection
# http://invisible-island.net/xterm/terminfo-contents.html#tic-xterm_tmux
set-option -ga terminal-overrides ',xterm*:Ms=\E]52;c;%p2%s\007'

# KiTTY always appends to clipboard; must clear it first
# https://sw.kovidgoyal.net/kitty/protocol-extensions.html#pasting-to-clipboard
set-option -ga terminal-overrides ',xterm-kitty:Ms=\E]52;c;!\007\E]52;c;%p2%s\007'

# prevent standout from appearing as italics under URxvt
# http://comments.gmane.org/gmane.comp.terminal-emulators.tmux.user/1927
set-option -ga terminal-overrides ',rxvt-unicode*:sitm@'

#-----------------------------------------------------------------------------
# appearance
#-----------------------------------------------------------------------------

# Colors from the "lucius" and "gruvbox" themes in the vim-airline plugin:
# https://github.com/bling/vim-airline/blob/master/autoload/airline/themes/lucius.vim
# https://github.com/morhetz/gruvbox/blob/master/autoload/airline/themes/gruvbox.vim

set-option        -g status-style                fg=colour246,bg=colour237
set-window-option -g window-status-current-style fg=colour214,bg=colour239
set-option        -g pane-border-style           fg=colour239
set-option        -g pane-active-border-style    fg=colour208
set-option        -g message-style               fg=colour214,bg=colour239
set-window-option -g mode-style                  fg=colour214,bg=colour239,bold,reverse

# Common UI interaction cues from Blueprint CSS:
# http://blueprintcss.org/tests/parts/forms.html
set-window-option -g window-status-bell-style     'bg=#205791,fg=#d5edf8' # info (blue)
set-window-option -g window-status-activity-style 'bg=#8a1f11,fg=#fbe3e4' # error (red)

#-----------------------------------------------------------------------------
# status bar
#-----------------------------------------------------------------------------

# toggle status bar visibility
bind-key -n M-` set-option -g status

# toggle status bar position
bind-key -n M-~ \
  if-shell 'tmux show-option -g status-position | grep -q top$' \
    'set-option -g status-position bottom' \
    'set-option -g status-position top'

# put status bar at the top of the screen
set-option -g status-position top

# list windows on left side of status bar
set-option -g status-left-length 0

# make window list easier to scan
set-window-option -g window-status-format         ' #[bold]#I#F#[nobold]#W '
set-window-option -g window-status-current-format ' #[bold]#I#F#[nobold]#W '
set-window-option -g window-status-separator      ''

# show pane title, pane identifier, and hostname on right side of status bar
set-option -g status-right-length 64
set-option -g status-right '#{=32:pane_title} \
#[fg=colour214,bg=colour239] #S:#I.#P \
#(test -n "$SSH_TTY" && echo "#[fg=colour214,bg=colour239,bold,reverse] #H ")'

#-----------------------------------------------------------------------------
# windows
#-----------------------------------------------------------------------------

# create window
bind-key -n M-e new-window

# rename window
bind-key -n M-E command-prompt -I '#W' 'rename-window "%%%"'

set-window-option -g automatic-rename off


# break off pane to a new window
bind-key -n M-x \
  command-prompt -p 'break-pane:' -I '#W' \
    'break-pane ; rename-window "%%%"'
bind-key -n M-X break-pane

# focus window
bind-key -n M-, previous-window
bind-key -n M-. next-window
bind-key -n M-o last-window

# focus by number
set-option -g base-index 1
set-window-option -g pane-base-index 1
set-option -g renumber-windows on
bind-key -n M-0 choose-window
bind-key -n M-1 select-window -t :1
bind-key -n M-2 select-window -t :2
bind-key -n M-3 select-window -t :3
bind-key -n M-4 select-window -t :4
bind-key -n M-5 select-window -t :5
bind-key -n M-6 select-window -t :6
bind-key -n M-7 select-window -t :7
bind-key -n M-8 select-window -t :8
bind-key -n M-9 select-window -t :1 \; select-window -t :-1

# swap window
bind-key -n M-< swap-window -t :-1
bind-key -n M-> swap-window -t :+1

# monitor window
set-option -g visual-activity on
set-option -g visual-silence on


bind-key -n M-k \
  set-window-option monitor-activity \;\
  display-message 'monitor-activity #{?monitor-activity,on,off}'

bind-key -n M-K \
  if-shell 'tmux show-window-option -g monitor-activity | grep -q off$' \
    'set-window-option -g monitor-activity on' \
    'set-window-option -g monitor-activity off' \;\
  display-message 'monitor-activity #{?monitor-activity,on,off} (global)'

bind-key -n M-j \
  command-prompt -p 'monitor-silence (seconds):' -I '#{monitor-silence}' \
    'set-window-option monitor-silence %% ;\
      display-message "monitor-silence #{?monitor-silence,on,off}"'

#-----------------------------------------------------------------------------
# panes
#-----------------------------------------------------------------------------

# send input to all panes in window (toggle)
bind-key C-a \
  set-option synchronize-panes \;\
  display-message 'synchronize-panes #{?synchronize-panes,on,off}'

# clear the screen in all panes in window
bind-key C-l \
  set-option synchronize-panes on \;\
  send-keys C-l \;\
  set-option synchronize-panes off

# create pane (below, above, left, right)
bind-key -n M-c split-window -c '#{pane_current_path}'
bind-key -n M-C split-window -c '#{pane_current_path}' -b
bind-key -n M-R split-window -c '#{pane_current_path}' -b -h
bind-key -n M-r split-window -c '#{pane_current_path}'    -h

# join pane (above, left, below, right)
bind-key -n M-g move-pane    -t .-1 -s .    # join pane at bottom of prev pane
bind-key -n M-l move-pane    -t .-1 -s . -h # join pane at right of prev pane
bind-key -n M-G move-pane -d -s .+1 -t .    # join next pane at bottom
bind-key -n M-L move-pane -d -s .+1 -t . -h # join next pane at right

# Intelligently navigate tmux panes and Vim splits using the same keys.
# See https://sunaku.github.io/tmux-select-pane.html for documentation.
#
#      +-------------+------------+-----------------------------+
#      | inside Vim? | is Zoomed? | Action taken by key binding |
#      +-------------+------------+-----------------------------+
#      | No          | No         | Focus directional tmux pane |
#      | No          | Yes        | Nothing: ignore key binding |
#      | Yes         | No         | Seamlessly focus Vim / tmux |
#      | Yes         | Yes        | Focus directional Vim split |
#      +-------------+------------+-----------------------------+
#
vim_navigation_timeout=0.05 # number of seconds we give Vim to navigate
navigate='                                                             \
  pane_is_zoomed() {                                                   \
    test #{window_zoomed_flag} -eq 1;                                  \
  };                                                                   \
  pane_title_changed() {                                               \
    test "#{pane_title}" != "$(tmux display -p "##{pane_title}")";     \
  };                                                                   \
  command_is_vim() {                                                   \
    case "${1%% *}" in                                                 \
      (vi|?vi|vim*|?vim*|view|?view|vi??*) true ;;                     \
      (*) false ;;                                                     \
    esac;                                                              \
  };                                                                   \
  pane_contains_vim() {                                                \
    case "#{=3:pane_current_command}" in                               \
      (git|ssh|sh) command_is_vim "#{=5:pane_title}" ;;                \
      (*) command_is_vim "#{=5:pane_current_command}" ;;               \
    esac;                                                              \
  };                                                                   \
  pane_contains_neovim_terminal() {                                    \
    test "#{=12:pane_title}" = "nvim term://";                         \
  };                                                                   \
  navigate() {                                                         \
    tmux_navigation_command=$1;                                        \
    vim_navigation_command=$2;                                         \
    vim_navigation_only_if=${3:-true};                                 \
    if pane_contains_vim && eval "$vim_navigation_only_if"; then       \
      if pane_contains_neovim_terminal; then                           \
        tmux send-keys C-\\ C-n;                                       \
      fi;                                                              \
      eval "$vim_navigation_command";                                  \
      if ! pane_is_zoomed; then                                        \
        sleep $vim_navigation_timeout; : wait for Vim to change title; \
        if ! pane_title_changed; then                                  \
          eval "$tmux_navigation_command";                             \
        fi;                                                            \
      fi;                                                              \
    elif ! pane_is_zoomed; then                                        \
      eval "$tmux_navigation_command";                                 \
    fi;                                                                \
  };                                                                   \
navigate '
navigate_left=" $navigate 'tmux select-pane -L'  'tmux send-keys C-w h'"
navigate_down=" $navigate 'tmux select-pane -D'  'tmux send-keys C-w j'"
navigate_up="   $navigate 'tmux select-pane -U'  'tmux send-keys C-w k'"
navigate_right="$navigate 'tmux select-pane -R'  'tmux send-keys C-w l'"
navigate_back=" $navigate 'tmux select-pane -l || tmux select-pane -t1'\
                          'tmux send-keys C-w p'                       \
                          'pane_is_zoomed'                             "

## QWERTY keys - comment these out if you don't use QWERTY layout!
#bind-key -n M-h run-shell -b "$navigate_left"
#bind-key -n M-j run-shell -b "$navigate_down"
#bind-key -n M-k run-shell -b "$navigate_up"
#bind-key -n M-l run-shell -b "$navigate_right"
#bind-key -n M-\ run-shell -b "$navigate_back"

# Dvorak keys - comment these out if you don't use Dvorak layout!
bind-key -n M-d run-shell -b "$navigate_back"
bind-key -n M-h run-shell -b "$navigate_left"
bind-key -n M-t run-shell -b "$navigate_up"
bind-key -n M-n run-shell -b "$navigate_down"
bind-key -n M-s run-shell -b "$navigate_right"

# resize pane
bind-key -r H resize-pane -L 5
bind-key -r T resize-pane -U 5
bind-key -r N resize-pane -D 5
bind-key -r S resize-pane -R 5

# zoom pane
bind-key -n M-m resize-pane -Z

# swap pane
bind-key -n M-- swap-pane -D
bind-key -n M-_ swap-pane -U
bind-key -n M-D run-shell 'tmux select-pane -l \; swap-pane -d -s #D'
bind-key -n M-H run-shell 'tmux select-pane -L \; swap-pane -d -s #D'
bind-key -n M-T run-shell 'tmux select-pane -U \; swap-pane -d -s #D'
bind-key -n M-N run-shell 'tmux select-pane -D \; swap-pane -d -s #D'
bind-key -n M-S run-shell 'tmux select-pane -R \; swap-pane -d -s #D'

# attach by number
bind-key -n 'M-!' join-pane -t :1
bind-key -n 'M-@' join-pane -t :2
bind-key -n 'M-#' join-pane -t :3
bind-key -n 'M-$' join-pane -t :4
bind-key -n 'M-%' join-pane -t :5
bind-key -n 'M-^' join-pane -t :6
bind-key -n 'M-&' join-pane -t :7
bind-key -n 'M-*' join-pane -t :8
bind-key -n 'M-(' run-shell 'tmux select-window -t :1 \;\
                                  select-window -t :-1 \;\
                                  join-pane -s "#{pane_id}"'
bind-key -n 'M-)' choose-window 'join-pane -t "%%%"'

# promote pane (toggle)
bind-key -n M-Enter \
  if-shell 'test #P -ne 1' \
    'select-pane -t 1' \
    'last-pane; swap-pane -s 1'

# rotate panes
bind-key -n M-a rotate-window -D
bind-key -n M-A rotate-window -U

#-----------------------------------------------------------------------------
# layouts
#-----------------------------------------------------------------------------

bind-key M-w select-layout main-horizontal
bind-key M-W select-layout even-vertical
bind-key M-v select-layout main-vertical
bind-key M-V select-layout even-horizontal
bind-key M-z select-layout tiled

# half-screen tiling layouts (horizontal, vertical)
# https://sunaku.github.io/tmux-half-screen-tiling-layouts.html
bind-key -n M-w select-layout main-horizontal \;\
  run-shell 'tmux resize-pane -t 1 -y $(( #{window_height} / 2 ))'
bind-key -n M-v select-layout main-vertical   \;\
  run-shell 'tmux resize-pane -t 1 -x $(( #{window_width}  / 2 ))'

# binary space partitioned layouts (dwindle, spiral)
# https://sunaku.github.io/tmux-layout-dwindle.html
bind-key -n M-w run-shell 'tmux-layout-dwindle brhc && tmux-redraw-vim'
bind-key -n M-W run-shell 'tmux-layout-dwindle trhc && tmux-redraw-vim'
bind-key -n M-v run-shell 'tmux-layout-dwindle brvc && tmux-redraw-vim'
bind-key -n M-V run-shell 'tmux-layout-dwindle blvc && tmux-redraw-vim'
bind-key -n M-z select-layout tiled

#-----------------------------------------------------------------------------
# scrollback buffer
#-----------------------------------------------------------------------------

# buffer length
set-option -g history-limit 32767

# search buffer using copy mode
bind-key -n M-/ copy-mode \;\
  command-prompt -p 'search-backward (press up):' \
    -i 'send-keys -X search-backward-incremental "%%%"'

# search buffer using Vim or less
bind-key -n M-|                               \
  capture-pane -J -S - \;                     \
  new-window -n '#S:#I.#P' -a '               \
    tmux save-buffer - \; delete-buffer | {   \
      if command -v vim;                      \
      then vim -R -c "set nofen is hls ic" -; \
      else less;                              \
      fi;                                     \
    };                                        \
  ' \;                                        \
  run-shell 'tmux send-keys G \?'

# search colored buffer using less
bind-key -n M-?                           \
  capture-pane -e -J -S - \;              \
  new-window -n '#S:#I.#P' -a '           \
    tmux save-buffer - \; delete-buffer | \
    less -R                               \
  ' \;                                    \
  run-shell 'tmux send-keys G \?'

# scroll buffer
# NOTE: set "URxvt.saveLines: 0" in ~/.Xdefaults to make Shift+PageUp bindable
# NOTE: see http://aperiodic.net/screen/interface for doing the same in XTerm
bind-key -n S-PPage copy-mode -u

# copy text from buffer
bind-key -n M-u copy-mode
set-window-option -g mode-keys vi
bind-key -T copy-mode-vi v send-keys -X begin-selection
bind-key -T copy-mode-vi y send-keys -X copy-selection
bind-key -T copy-mode-vi - send-keys -X jump-again
bind-key -T copy-mode-vi _ send-keys -X jump-reverse
bind-key -T copy-mode-vi ? command-prompt -p 'search-backward:' -I '#{pane_search_string}' -i 'send-keys -X search-backward-incremental "%%%"'
bind-key -T copy-mode-vi / command-prompt -p 'search-forward:' -I '#{pane_search_string}' -i 'send-keys -X search-forward-incremental "%%%"'

# transfer copied text to attached terminal with yank:
# https://github.com/sunaku/home/blob/master/bin/yank
bind-key -T copy-mode-vi Y send-keys -X copy-pipe 'yank > #{pane_tty}'
# open the visual selection with xdg-open(1)
bind-key -T copy-mode-vi O send-keys -X copy-pipe 'xargs -r xdg-open'

# paste most-recently copied text
bind-key -n M-i paste-buffer

# paste previously copied text (chosen from a menu)
bind-key -n M-I choose-buffer

# transfer most-recently copied text to attached terminal with yank:
# https://github.com/sunaku/home/blob/master/bin/yank
bind-key -n M-y run-shell 'tmux save-buffer - | yank > #{pane_tty}'

# transfer previously copied text (chosen from a menu) to attached terminal:
# https://github.com/sunaku/home/blob/master/bin/yank
bind-key -n M-Y choose-buffer 'run-shell "tmux save-buffer -b \"%%%\" - | yank > #{pane_tty}"'

#-----------------------------------------------------------------------------
# TMUX plugin manager https://github.com/tmux-plugins/tpm
#-----------------------------------------------------------------------------

set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @resurrect-capture-pane-contents on

set -g @plugin 'Morantron/tmux-fingers'
set -g @fingers-key '-n M-U'
set -g @fingers-compact-hints 1
set -g @fingers-hint-format '#[fg=yellow,bold,reverse]%s'
set -g @fingers-hint-labels ' \
  a o e u i d h t n s         \
        p y f g c r l         \
    q j k x b m w v z         \
  A O E U I D H T N S         \
        P Y F G C R L         \
    Q J K X B M W V Z         \
'

run-shell ~/.tmux/plugins/tpm/tpm