### GENERAL

set-option -g prefix C-a                   # Set prefix to <C-a>
bind a send-prefix                     # Send <C-a> with <C-a>a

bind R source-file ~/.tmux.conf \; display "~/.tmux.conf reloaded"
bind -n M-R source-file ~/.tmux.conf \; display "~/.tmux.conf reloaded"

set -g history-limit 10000                 # lines to keep in hisoty
set-option -g display-panes-time 3000      # Timeout for pane-numbering in ms
bind -n M-q display-panes
set-option -sg escape-time 0               # speed up commands
set -g mouse on                            # enable mouse (tmux 2.1+)
set -g base-index 1                        # start window numbering at 1
set -g pane-base-index 1                   # start pane numbering at 1
set -g renumber-windows on                 # renumber windows automatically
setw -g automatic-rename on                # rename window after process

# Clear window name before renaming
bind , rename-window "" \; command-prompt "rename-window '%%'"

#### APPEARANCE

set -g default-terminal "screen-256color"  # use 256 colors
setw -g aggressive-resize on               # resize window to smallest client

set -g pane-border-style fg=colour238            # border color for inactive panes
set -g pane-active-border-style fg=colour247     # border color for active panes

# Status bar colors and format
setw -g window-status-format ' #[fg=#999999]#I #[fg=$666666]#W '
setw -g window-status-current-format '#[fg=#ffffff] #I #W#[fg=#ffffff] '
setw -g window-status-separator '#[fg=#292929]|#[fg=default]'
set -g status-bg default                   # background color for status bar
set -g status-position bottom              # put status bar on top or bottom
set -g status-interval 2                   # interval in s to update status
set -g status-justify left                 # horizontal alignment
set -g message-style fg=white,bg=black     # appearance of status messages
set -g message-command-style fg=white      # appearance of status message cmds

# Left section of status bar
set -g status-left ""

# Status bar visibility
set -g status off
bind -r -n M-t set status on
bind -r -n M-T set status off
bind t set status on
bind T set status off

# Right section of status bar
if-shell 'uname | grep -qi Darwin' "set -g status-right \"#[fg=#81a2be]#(/usr/local/bin/mpc | head -n 1 | sed 's/volume.*$//') #[fg=cyan]#(~/bin/battery-osx) #(~/bin/mailstatus.sh) #[fg=yellow]#(uptime|sed 's/.* //') #[fg=#666666]%F #[fg=#bababa]%R\""

if-shell 'uname | grep -qi Linux' "set -g status-right \"#[fg=cyan]#(~/bin/battery-linux) #(~/bin/mailstatus.sh) #[fg=yellow]#(cat /proc/loadavg|awk '{print $1;}') #[fg=#666666]%F #[fg=#bababa]%R\""

# Scaling of status-bar sections
set -g status-right-length 40


#### NAVIGATION

# With C-a prefix
bind h select-pane -L                   # navigate left with <C-a>h
bind j select-pane -D                   # navigate down with <C-a>j
bind k select-pane -U                   # navigate up with <C-a>k
bind l select-pane -R                   # navigate right with <C-a>l
bind -r H resize-pane -L 5                 # resize pane left with <C-a>H
bind -r J resize-pane -D 5                 # resize pane down with <C-a>J
bind -r K resize-pane -U 5                 # resize pane up with <C-a>K
bind -r L resize-pane -R 5                 # resize pane right with <C-a>L

# Navigate panes with Meta (alt) modifier + hjkl
bind -r -n M-h select-pane -L              # navigate left with M-h
bind -r -n M-j select-pane -D              # navigate down with M-j
bind -r -n M-k select-pane -U              # navigate up with M-k
bind -r -n M-l select-pane -R              # navigate right with M-l
bind -r -n M-H resize-pane -L 5            # resize pane left with M-H
bind -r -n M-J resize-pane -D 5            # resize pane down with M-J
bind -r -n M-K resize-pane -U 5            # resize pane up with M-K
bind -r -n M-L resize-pane -R 5            # resize pane right with M-L

# Navigate windows with Meta (alt) modifier + number keys
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 :=9
bind -n M-0 select-window -t :=10

bind C-s last-window                   # go to last window with <C-a><C-s>
bind C-a last-pane                     # go to last pane with <C-a><C-a>
bind -n M-s last-window                # go to last pane with M-s
bind -n M-a last-pane                  # go to last pane with M-a
bind -r n next-window                  # next window with <C-a>n
bind -r b previous-window              # next window with <C-a>p

bind -n -r M-n next-window             # next window with <M-n>
bind -n -r M-b previous-window         # previous window with <M-n>
#bind -n M-, run-shell "tmux list-panes -as -F \"##I.##P ##{pane_current_command} . #{pane_current_path} (#W)#F\" | fzf-tmux | cut -d \" \" -f 1 | xargs tmux select-pane -t"
bind -n M-, run-shell "tmux list-windows -F \"##I:##W\" | fzf-tmux | cut -d \":\" -f 1 | xargs tmux select-window -t"
bind -n M-. run-shell "tmux list-sessions -F \"##S\" | fzf-tmux | xargs tmux switch -t"


#### LAYOUT CHANGING BINDINGS

# create panes in same directory
bind '"' split-window -c "#{pane_current_path}"
bind '%' split-window -h -c "#{pane_current_path}"

bind -r z resize-pane -Z                   # toggle pane zoom with <C-a>z
bind -r y next-layout                      # cycle to next pane layout with <C-a>y
bind -r Y previous-layout                  # cycle to previous pane layout with <C-a>Y
bind -r r rotate-window                    # rotate panes with <C-a>r

bind -n M-z resize-pane -Z                 # toggle pane zoom with <M-z>
bind -n -r M-y next-layout                 # cycle to next pane layout with <M-y>
bind -n -r M-Y previous-layout             # cycle to previous pane layout with <M-Y>
bind -n -r M-r rotate-window               # rotate panes with <M-r>

bind -r Left swap-window -t -1             # Swap window left
bind -r Right swap-window -t +1            # Swap window right

bind -r B swap-window -t -1                # Swap window left
bind -r N swap-window -t +1                # Swap window right
bind -n -r M-B swap-window -t -1           # Swap window left
bind -n -r M-N swap-window -t +1           # Swap window right

#### CLIPBOARD

# enable reattach-to-user-namespace which fixes pasteboard access and launchctl 
bind Space copy-mode                       # enter copy mode with <C-a><Space>
bind -n M-u copy-mode                      # enter copy mode with M-u
bind -T copy-mode-vi M-u send -X halfpage-up    # scroll up with M-u
bind -T copy-mode-vi M-d send -X halfpage-down  # scroll down with M-d
bind -T copy-mode-vi v send -X begin-selection  # start "visual" with v

# Copy (yank) with y
if-shell 'uname | grep -qi Linux && which xclip > /dev/null' 'bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "DISPLAY=:0 xclip -i -sel clipboard"'
if-shell 'uname | grep -qi Darwin' 'bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "pbcopy"'
if-shell 'uname | grep -qi Cygwin' 'bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "cat > /dev/clipboard"'

# Paste with C-a p or M-p
if-shell 'uname | grep -qi Linux && which xclip > /dev/null' 'bind p run "DISPLAY=:0 xclip -o | tmux load-buffer - ; tmux paste-buffer"'
if-shell 'uname | grep -qi Darwin && which reattach-to-user-namespace > /dev/null' 'bind p run "pbpaste | tmux load-buffer - ; tmux paste-buffer"'
if-shell 'uname | grep -qi Darwin' 'bind -n M-p run "pbpaste | tmux load-buffer - ; tmux paste-buffer"'
if-shell 'uname | grep -qi Cygwin' 'bind p run "cat /dev/clipboard | tmux load-buffer - ; tmux paste-buffer"'
if-shell 'uname | grep -qi Cygwin' 'bind -n M-p run "cat /dev/clipboard | tmux load-buffer - ; tmux paste-buffer"'


#### LAUNCH PROCESSES

# use urlview to follow URLs in current pane
bind u capture-pane -J \; \
        save-buffer "/tmp/active_tmux_buffer" \; \
        delete-buffer \; \
        split-window -l 10 "urlview '/tmp/active_tmux_buffer' && rm /tmp/active_tmux_buffer"

# Launch offlineimap in inactive splits
bind o split-window -p 25 '$SHELL -c "offlineimap -qf INBOX"' \; select-pane -l
bind O split-window -p 25 '$SHELL -c "offlineimap"' \; select-pane -l

# Use nested bindings (<C-a>l) for grouping process launch bindings
bind -n C-M-v new-window -n vim "/usr/local/bin/vim"
bind -n C-M-w new-window -n weather \
         "curl 'wttr.in/?m'; echo -e '\nPress <enter> to quit'; read -n 1 -s"

# Open new window and resize status accordingly (should be a hook instead)
bind Enter new-window -c "#{pane_current_path}" "$SHELL"
bind -n M-Enter new-window \
        "tmux set status-right-length `echo $(tput cols)/2|bc|tr -d '\n'`; zsh"

# Use nested bindings (<C-a>m) for grouping music-control bindings
bind m switchc -Tmpd
bind -n M-m switchc -Tmpd
bind -Tmpd v new-window -n vimpc "vimpc"
bind -Tmpd p display "#(mpc toggle | tr '\n' ' ')"
bind -Tmpd s display "#(mpc stop | tr '\n' ' ')"
bind -Tmpd n display "#(mpc next | tr '\n' ' ')"
bind -Tmpd b display "#(mpc prev | tr '\n' ' ')"
bind -Tmpd r display "#(mpc clear && mpc ls | mpc add && mpc random on && mpc play | tr '\n' ' ')"
#bind -Tmpd r new-window -n mpc "mpc clear && mpc ls | mpc add && mpc shuffle && mpc play"

bind -n C-M-p display "#(mpc toggle | tr '\n' ' ')"
bind -n C-M-s display "#(mpc stop | tr '\n' ' ')"
bind -n C-M-n display "#(mpc next | tr '\n' ' ')"
bind -n C-M-b display "#(mpc prev | tr '\n' ' ')"
#bind -n C-M-r display "#(mpc clear && mpc ls | mpc add && mpc random on && mpc play | tr '\n' ' ')"
bind -n C-M-a split-window -p 50 "source ~/code/fzf-mpd/fzf-mpd.zsh && fm"

# fzf-locate from entire file system and insert result in current pane (Alt-`)
bind -n 'M-`' run "tmux split-window -p 40 'tmux send-keys -t #{pane_id} \"$(locate / | fzf -m | paste -sd\\  -)\"'"

# Change to the previous pane, repeat the last command, change back
bind -n M-! last-pane \; send-keys C-p C-m \; last-pane
