# Scott Rochford's tmux configuration
#
# change the prefix to the GNU screen default (avoids clash with page up in vi)
set -g prefix C-a
unbind-key C-b
bind-key C-a send-prefix
# toggle sending input to all panes
bind-key b set-window-option synchronize-panes
# alternative to ',' which doesn't pre-fill the prompt with the existing name
bind-key < command-prompt "rename-window '%%'"

# Disabled all of these in favour of changing 'default-command' below.
#bind-key C-p pipe-pane -o 'cat >>~/tmux_logs/output.$(echo #I-#P-#W-#T | sed "s/[^[:alnum:].-]/_/g")' \; display-message 'Toggled logging'
# From http://unix.stackexchange.com/questions/5832/is-there-an-equivalent-of-gnu-screens-log-command-in-tmux
# bind-key H pipe-pane -o "exec cat >>$HOME/'#W-tmux.log'" \; display-message 'Toggled logging to $HOME/#W-tmux.log'
#bind-key H pipe-pane "exec cat >>$HOME/'#W-tmux.log'" \; display-message 'Started logging to $HOME/#W-tmux.log'
#bind-key h pipe-pane \; display-message 'Ended logging to $HOME/#W-tmux.log'

#set -g utf8 on

set-option -g history-limit 32768

# no longer available in 2.2
#set-option -g mouse-select-pane on
#set-option -g mouse-select-window on
set-option -g mouse on

# increase the amount of time status bar messages are displayed for (default 1000 I think)
set-option -g display-time 1500
# unfortunately this seems to have no effect in putty :-(
set-option -g set-clipboard on
set-option -g default-command 'tmux pipe-pane -o "cat >>~/tmux_logs/output-`date +%Y%m%d-%H%M%S-$$`" ; /bin/ksh -l'
#
# allow yank into system clipboard
# from http://stackoverflow.com/questions/17255031/how-to-copy-from-tmux-running-in-putty-to-windows-clipbard
# 
# for some reason this is wrapping at 80 cols, using save- instead of show- helps
# -b for background is needed because xclip continues to run to service the clipboard paste requests until the
# clipboard buffer is replaced with some new contents
#bind C-y run-shell -b "tmux save-buffer - | DISPLAY=$(<~/.xdisplay) xclip -selection clipboard -in && tmux display-message 'xclipped successfully'"
bind C-y save-buffer ~/etc/clipboard.pipe
#
# this was just for testing, but interestingly for some reason tmux-show-buffer >/tmp/t never terminates, writing to a pipe works fine??
#bind C-z run-shell "tmux show-buffer | cat >/tmp/t"
# move x clipboard into tmux paste buffer
#bind C-p run-shell -b "xclip -o -selection clipboard | tmux load-buffer - ; tmux paste-buffer"
bind C-p run-shell "DISPLAY=$(<~/.xdisplay) xclip -o -selection clipboard | tmux load-buffer - ; tmux paste-buffer"

# switch to last-but-one window (like prefix-l but last, last)
# only works on tmux-2.4 + with Nicholas Marriott's patch from my feature request, unless it reached mainline....
#bind k run-shell "tmux select-window -t $(tmux list-windows -F '#{session_stack}' | awk -F, '{print $3;exit}END{print $1}')"
bind k run-shell "tmux select-window -t $(echo #{session_stack} | awk -F, '{w=$1}NF>=3{w=$3;exit}END{print w}')"

# switch to oldest window (for clean-up), not sure why brackets are required around (NF) here...
bind K run-shell "tmux select-window -t $(echo #{session_stack} | awk -F, '{print $(NF)}')"

# prompt for hosts to connect to, open a new synchronized window with horizontally split panes for each host, supports brace expansion 
bind N command-prompt -p hosts: 'run-shell -b "bash -c \"~/lbin/nw %% >/dev/null\""'

# seems to cause unexpected resizes when focussing on putty :-(
#set-option mouse-resize-pane on

#05:59 < Celti> annihilannic: I believe the #{pane_in_mode} format does what you want
#05:59 < Celti> put it in your statusline
#05:59 < Celti> annihilannic: No, my mistake, I should have read farther down, you want #{pane_synchronized}
# only works in tmux 2.0?, higher than 1.6.3 anyway
set-option -g window-status-format         ' #I:#W#F#{?pane_synchronized,S,}'
#set-option -g window-status-current-format ' #I:#W#{?pane_synchronized,[sync],}#F'
# to highlight in red when sync is on... not sure why I did this with set-window-option instead of set-option, perhaps
# both work?
set-window-option -g window-status-current-format "#{?pane_synchronized,#[bg=red],}#{?window_zoomed_flag,#[bg=yellow],} #I:#W#F#{?pane_synchronized,S,}"
#
# also only in 2.0?  if I use this, don't need #F in window-status-*-format? - actually, nah,
# still useful for showing [Z]oomed, or - last active, etc.
set-option -g window-status-current-style bg=blue

# Toggle input on a pane (from Thomas Sattler)
bind-key R if -F '#{pane_input_off}' "select-pane -e; select-pane -P fg=default" "select-pane -d; select-pane -P fg=yellow"
