#!/bin/rc rfork en # splits a window into an h by v grid of windows, with that window # resized into the top-left of the grid vars=(wsplitifs flags flagh flagv flagw flagi cmd window wcf reldim x0 xn y0 yn) vars=($vars xd yd xd__ yd__ x y x_ y_ x__ y__ flagfmt args vars) wsplitifs=' ' flagfmt='h cols,v rows,s,w labelre,i winid' args='[cmd args...]' if(! ifs=() eval `$wsplitifs{aux/getflags $*} || {! ~ $#flagi 0 && ! ~ $#flagw 0 }) { aux/usage echo >[1=2] '-h: number of columns to create' echo >[1=2] '-v: number of rows to create' echo >[1=2] '-s: set scroll flag in all windows' echo >[1=2] '-i: id of window to split (default this window)' echo >[1=2] '-w: regex of window label to split (default this window)' echo >[1=2] 'it should be obvious that -i and -w are mutually exclusive' } if (~ $#* 0) cmd=rc if not cmd=($*) if (! ~ $#flagi 0 && ! ~ $#flagw 0) { echo >[1=2] 'can''t specify both -i and -w' exit 'bad combination of flags' } if (! ~ $#flagv 0 && ! test $flagv -ge 1) { echo >[1=2] 'bogus -v value' exit 'bad v splitnum' } if (! ~ $#flagh 0 && ! test $flagh -ge 1) { echo >[1=2] 'bogus -h value' exit 'bad h splitnum' } if (! ~ $#flagw 0) { window=`'\12'{grep -l -- $"flagw /mnt/wsys/wsys/*/label} if (! test $#window -eq 1) { echo >[1=2] $0: -w regex matched $#window windows exit 'bad window count' } if not wcf=`$wsplitifs{echo $"window | sed 's:/label$:/wctl:'} } if not # default to current window wcf=/mnt/wsys/wctl if (! ~ $#flagi 0) wcf=/mnt/wsys/wsys/$flagi/wctl if (~ $#flagh 0) flagh=1 if (~ $#flagv 0) flagv=1 # create a window using absolute coordinates # only sane way to do this (at least without C) is to use the old-style # attach specifier syntax: # Npid,minx,miny,maxx,maxy # where pid must match the process in the new window if you want del to # work. fn win__ { @{ rfork ns { # & block and unmount allow the calling window to die before children {unmount /mnt/acme /dev; unmount $wsys /dev} >[2]/dev/null if(mount $wsys /mnt/wsys N`{cat /dev/ppid}^,$1,$2,$3,$4) { bind -b /mnt/wsys /dev echo -n `{basename $"cmd} > /dev/label >[2] /dev/null if(! ~ $#flags 0) echo scroll > /dev/wctl rm -f /env/^$vars # exec to avoid changing pid again exec $cmd < /dev/cons > /dev/cons >[2] /dev/cons } } & } } fn floorall { for (i in $*) hoc -e 'int('^$i^')' } reldim=`$wsplitifs{dd -count 1 < $wcf >[2]/dev/null} x0=$reldim(1) y0=$reldim(2) xn=$reldim(3) yn=$reldim(4) xd=`$wsplitifs{hoc -e '('^$xn^-^$x0^')/'^$flagh} yd=`$wsplitifs{hoc -e '('^$yn^-^$y0^')/'^$flagv} xd__=`{hoc -e 'int('^$xd^')'} yd__=`{hoc -e 'int('^$yd^')'} currentid=`$wsplitifs{cat /mnt/wsys/winid} y=$y0 # do calculations with x_ and y_ (keep decimal points) and window operations # with x__ and y__ (same values floored). for (vi in `$wsplitifs{seq 1 1 $flagv}) { x=$x0 y_=`$wsplitifs{hoc -e $y^+$yd} for (hi in `$wsplitifs{seq 1 1 $flagh}) { x_=`$wsplitifs{hoc -e $x^+$xd} if (test $hi -eq 1 && test $vi -eq 1) { echo 'current' > $wcf if (! ~ $#flags 0) echo 'scroll' > $wcf echo resize -dx $xd__ -dy $yd__ > $"wcf if (! ~ $#flagw 0) echo 'current' > /mnt/wsys/wsys/$currentid/wctl } if not win__ `$wsplitifs{floorall $x $y $x_ $y_} $cmd x=$x_ } y=$y_ } # only run cmd in this window if we resized it if (! ~ $* '' && ~ $#flagi 0 && ~ $#flagw 0) { $cmd }