#!/bin/bash
#
# Author: Twily 2015
# Description: Cut window in half and spawn a terminal in its remaining space
# Requires: wmctrl, xdotool, xwininfo, urxvt
# Usage: $ sh ./split.grid {H,V,Q,<none>}
# (See below for keybind alternative.
# Keybinds can be set in your WM)
#
ACT=$(xdotool getactivewindow)
G=24 # Gap between windows
D=0 # Decoration height
M=0 # Border width
let M=$M*2
if [ "$D" -gt "0" ]; then D=$(( $D + ( $M / 2 ) )); fi
function getAct {
unset X Y W H
eval $(xwininfo -id $ACT |
sed -n -e "s/^ \+Absolute upper-left X: \+\([0-9]\+\).*/X=\1/p" \
-e "s/^ \+Absolute upper-left Y: \+\([0-9]\+\).*/Y=\1/p" \
-e "s/^ \+Width: \+\([0-9]\+\).*/W=\1/p" \
-e "s/^ \+Height: \+\([0-9]\+\).*/H=\1/p" )
X=$(( $X - ( $M / 2 ) ))
Y=$(( $Y - ( $M / 2 ) - $D ))
}
getAct
function splitH {
# Spawn below
aH=$(( $H / 2 - ( $G / 2 ) - ( $M / 2 ) - $D ))
wmctrl -i -r $ACT -e 0,$X,$Y,$W,$aH
Y=$(( $Y + $aH + $G + $D + $M ))
H=$(( $H - $aH - $G - $D - $M ))
}
function splitV {
# Spawn right
aW=$(( $W / 2 - ( $G / 2 ) - $M ))
wmctrl -i -r $ACT -e 0,$X,$Y,$aW,$H
X=$(( $X + $aW + $G + $M ))
W=$(( $W - $aW - $G - $M ))
}
quad=false # [Keybind alternative]
case $1 in
'H') splitH # [W-A-h]
;;
'V') splitV # [W-A-v]
;;
'Q') splitH && quad=true # [W-A-b]
;;
*) if [ "$W" -gt "$H" ]; then splitV; else splitH; fi # [W-s]
;;
esac
urxvt -g "1x1+"$X"+"$Y &
function relocate {
for i in {0..49}; do # timeout
if ps -p $! >/dev/null; then
sleep .1
wmctrl -r :ACTIVE: -e 0,$X,$Y,$W,$H
break
fi
done
}
relocate
if $quad; then
TMP=$(xdotool getactivewindow)
getAct
splitV
urxvt -g "1x1+"$X"+"$Y &
relocate
ACT=$TMP
getAct
splitV
urxvt -g "1x1+"$X"+"$Y &
relocate
fi
exit 0
Top