# Dynamic configuration file generated by ~/Makefile from <%= __FILE__ %>
#
#     DO NOT EDIT THIS FILE BY HAND --
#     YOUR CHANGES WILL BE OVERWRITTEN
#
<% tmux_version = `tmux -V`.split.last %>
  # Enthium layout - https://github.com/sunaku/enthium
  navigate_back  = 'v'
  navigate_left  = 'h'
  navigate_up    = 't'
  navigate_down  = 'n'
  navigate_right = 's'
%>

#-----------------------------------------------------------------------------
# appearance
#-----------------------------------------------------------------------------
set        -g status-style                <%= status_style                %>
setw -g window-status-current-style <%= window_status_current_style %>
set        -g pane-border-style           <%= pane_border_style           %>
set        -g pane-active-border-style    <%= pane_active_border_style    %>
set        -g message-style               <%= window_status_current_style %>
setw -g mode-style                  <%= status_login_style %>

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

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

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

# toggle status bar position
bind -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 -g status-position top

# list windows on left side of status bar
set -g status-left ''

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

# show pane title, pane identifier, and hostname on right side of status bar
set -g status-right-length 64
set -g status-right '#{=32:pane_title} \
#[<%= status_ruler_style %>] #S:#I.#P \
#(test -n "$SSH_TTY" && echo "#[<%= status_login_style %>] #H ")'

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

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

# rename window
bind -n M-E command-prompt -I '#W' 'rename-window "%%%"'
setw -g automatic-rename off

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

# focus window
bind -n 'M-,' previous-window
bind -n 'M-;' next-window
bind -n 'M-.' last-window

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

# swap window
<% if tmux_version >= '3.0' %>
bind -n 'M-<' swap-window -t :-1 \; select-window -t :-1
bind -n 'M-:' swap-window -t :+1 \; select-window -t :+1
<% else %>
bind -n 'M-<' swap-window -t :-1
bind -n 'M-:' swap-window -t :+1
<% end %>

# monitor window
set -g visual-activity on
set -g visual-silence on
<% if tmux_version < '2.6' %>
set -g bell-on-alert on
<% end %>

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

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

bind -n "M-|" \
  if-shell 'tmux show-window-option -v monitor-silence | awk "{exit(\$NF>0)}"' \
    'set-window-option monitor-silence 60' \
    'set-window-option monitor-silence 0' \;\
  display-message 'monitor-silence #{?monitor-silence,#{monitor-silence},off}'

bind -n "M-\\" \
  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 C-a \
  set-option synchronize-panes \;\
  display-message 'synchronize-panes #{?synchronize-panes,on,off}'

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

# create pane (below, above, left, right)
bind -n M-o split-window -c '#{pane_current_path}'
bind -n M-O split-window -c '#{pane_current_path}' -b
bind -n M-A split-window -c '#{pane_current_path}' -b -h
bind -n M-a split-window -c '#{pane_current_path}'    -h

# join pane (above, left, below, right)
bind -n M-K move-pane    -t .-1 -s . -h # join pane at right of prev pane
bind -n M-J move-pane    -t .-1 -s .    # join pane at bottom of prev pane

# resize pane
bind -r <%= navigate_left.upcase  %> resize-pane -L 5
bind -r <%= navigate_up.upcase    %> resize-pane -U 5
bind -r <%= navigate_down.upcase  %> resize-pane -D 5
bind -r <%= navigate_right.upcase %> resize-pane -R 5
bind -n M-z resize-pane -y '100%'
bind -n M-w resize-pane -x '100%'

# find pane
bind -n M-f choose-tree

# zoom pane
bind -n M-m resize-pane -Z
bind -n M-M resize-pane -x '100%' -y '100%'

# swap pane
bind -n M-j swap-pane -D
bind -n M-k swap-pane -U
<%
  shift = lambda do |char|
    case char
    when ',' then '<'
    when '.' then '>'
    else char.upcase
    end
  end
%>
bind -n 'M-<%= shift.(navigate_back)  %>' run-shell 'tmux select-pane -l \; swap-pane -d -s #D'
bind -n 'M-<%= shift.(navigate_left)  %>' run-shell 'tmux select-pane -L \; swap-pane -d -s #D'
bind -n 'M-<%= shift.(navigate_up)    %>' run-shell 'tmux select-pane -U \; swap-pane -d -s #D'
bind -n 'M-<%= shift.(navigate_down)  %>' run-shell 'tmux select-pane -D \; swap-pane -d -s #D'
bind -n 'M-<%= shift.(navigate_right) %>' run-shell 'tmux select-pane -R \; swap-pane -d -s #D'

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

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

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

bind -n M-V select-layout tiled
bind -n M-Z select-layout even-vertical
bind -n M-W select-layout even-horizontal

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

# binary space partitioned layouts (dwindle, spiral)
# https://sunaku.github.io/tmux-layout-dwindle.html
bind -n M-d run-shell 'tmux-layout-dwindle brhc && tmux-redraw-vim'
bind    M-d run-shell 'tmux-layout-dwindle trhc && tmux-redraw-vim'
bind -n M-l run-shell 'tmux-layout-dwindle brvc && tmux-redraw-vim'
bind    M-l run-shell 'tmux-layout-dwindle blvc && tmux-redraw-vim'

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

# buffer length
set -g history-limit 9999 # NOTE: the maximum allowed limit is 32767

# search buffer using copy mode
bind -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 -n M-P                               \
  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 -n M-p                           \
  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: shift+PageUp
bind -n S-PPage copy-mode -eu
bind -n S-NPage send-keys -X page-down

# copy text from buffer
bind -n M-c copy-mode
setw -g mode-keys vi
bind -T copy-mode-vi v send-keys -X begin-selection
bind -T copy-mode-vi y send-keys -X copy-selection
bind -T copy-mode-vi - send-keys -X jump-again
bind -T copy-mode-vi _ send-keys -X jump-reverse
bind -T copy-mode-vi ? command-prompt -p 'search-backward:' -I '#{pane_search_string}' -i 'send-keys -X search-backward-incremental "%%%"'
bind -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 -T copy-mode-vi Y send-keys -X copy-pipe 'yank > #{pane_tty}'
# open the visual selection with xdg-open(1)
bind -T copy-mode-vi O send-keys -X copy-pipe 'xargs -r xdg-open'

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

# paste previously copied text (chosen from a menu)
bind -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 -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 -n M-Y choose-buffer 'run-shell "tmux save-buffer -b \"%%%\" - | yank > #{pane_tty}"'
