gofmt command for acme

1,030 views
Skip to first unread message

andrey mirtchovski

unread,
May 31, 2011, 11:13:24 PM5/31/11
to golang-nuts
The attached patch adds a new option (-g) to the acme editor which
enables a tighter integration with gofmt. The patch adds a new
command, Fmt, which, when executed in a editing window, will pass the
content of the window to gofmt ensuring that if gofmt detects any
errors it will output the file's name in its notification so that the
error location can be immediately accessed. I've been using this for a
couple of days and I've found it convenient, although YMMV. Its
utility is mostly when doing small changes, it's not that convenient
when typing new, unfinished code, but then again neither is gofmt :)

to apply against latest p9p tip:
cd $PLAN9 && patch -p1 < patch-acme.diff
mk and run with -g

The workflow for a go program is as follows:

type code
execute Put
execute Fmt (which is right next to Put)
if error execute Undo and go to the error location pointed to by gofmt's output
else execute Put again

BUGS: Since there's no way to check on an executed program's return
value an Fmt necessarily dirties the window, even if gofmt makes no
changes.

andrey

patch-acme.diff

Russ Cox

unread,
Jun 1, 2011, 12:03:29 AM6/1/11
to andrey mirtchovski, golang-nuts
You don't need to patch acme. I believe this does the same.
If you wrote a C program you could even read the original
selection address (requires opening addr before executing addr=dot)
and restore it.

#!/bin/sh

set -e
tmp=/tmp/acmefmt.$$
trap "rm -f $tmp $tmp.1 $tmp.2" 0 1 2

w() {
9p write acme/$winid/$1
}
r() {
9p read acme/$winid/$1
}
ctl() {
echo -n "$@" | w ctl
}

wintag=$(r tag | sed 's/ .*//')
r body >$tmp
if ! gofmt <$tmp >$tmp.1 2>$tmp.2; then
sed "s:^<standard input>:$wintag:" $tmp.2
else
if ! cmp $tmp $tmp.1 >/dev/null; then
echo -n , | w addr
ctl dot=addr
cat $tmp.1 | w wrsel
echo -n 0 | w addr
ctl dot=addr
fi
fi

andrey mirtchovski

unread,
Jun 1, 2011, 1:03:05 AM6/1/11
to r...@golang.org, golang-nuts
Not to drag this any further, but I like the native acme solution
because it places the 'Fmt' command conveniently (to me) right next to
'Put', it doesn't require that the command be typed in every window
and the solution is relatively simple, not requiring any temporary
files.

I admit I didn't think of the '9p' command at all.

Russ Cox

unread,
Jun 1, 2011, 10:00:36 AM6/1/11
to andrey mirtchovski, golang-nuts
On Wed, Jun 1, 2011 at 01:03, andrey mirtchovski <mirtc...@gmail.com> wrote:
> Not to drag this any further, but I like the native acme solution
> because it places the 'Fmt' command conveniently (to me) right next to
> 'Put', it doesn't require that the command be typed in every window
> and the solution is relatively simple, not requiring any temporary
> files.

If you do it in C (or Go; there are bindings) then you can
still make a real external program without any temporary files,
and it will work everywhere, not just where people have
patched acme. If you want it to be more convenient you
could install the binary as "P". :-)

Russ

Jason Catena

unread,
Jun 2, 2011, 11:32:54 AM6/2/11
to golang-nuts
"Edit ,|os -T gofmt" works for me in acme-sac, but I'm used to copying
commands into the tag.

Steve McCoy

unread,
Jun 2, 2011, 12:07:29 PM6/2/11
to golan...@googlegroups.com
"Edit , |gofmt $wintag" gets the job done in regular acme. Another option is to set up a version control hook that runs gofmt when you commit.

Steve McCoy

unread,
Jun 2, 2011, 1:23:24 PM6/2/11
to golan...@googlegroups.com
Oops, ignore the $wintag.

Fazlul Shahriar

unread,
Jun 3, 2011, 1:48:07 AM6/3/11
to golan...@googlegroups.com
On Thu, Jun 2, 2011 at 12:07 PM, Steve McCoy <mcc...@gmail.com> wrote:
> Another option is to set up a version control hook that runs gofmt when you
> commit.

Yes, that is what I do, using this: https://bitbucket.org/fhs/hgstyle

It's too easy to forget to run gofmt.

fhs

Reply all
Reply to author
Forward
0 new messages