Agofmt

A convenient script to easily run gofmt on your Go files from (p9p’s) Acme. By Micah Stetson.

#!/usr/bin/env 9 rc
fn aread { 9p read acme/$winid/$1 }
fn awrite { 9p write acme/$winid/$1 }
ftmp=`{mktemp /tmp/Agofmt.XXXXXX}
etmp=`{mktemp /tmp/Agofmt.XXXXXX}
if (aread body | gofmt $*>$ftmp >[2]$etmp) {
       # Don't dirty the window if you don't have to
       if (! aread body | cmp /dev/stdin $ftmp >/dev/null >[2=1]) {
               echo -n mark | awrite ctl
               echo -n nomark | awrite ctl
               echo -n , | awrite addr
               awrite data <$ftmp
               echo -n mark | awrite ctl
               # put us at the top of the file, rather than the bottom
               echo -n '#0' | awrite addr
               echo -n 'dot=addr' | awrite ctl
               echo -n show | awrite ctl
       }
}
if not {
       sed 's|^/dev/stdin|'^$%^'|' $etmp >[1=2]
}
rm -f $ftmp $etmp