Skip to content

Instantly share code, notes, and snippets.

@mkhl
Created May 16, 2018 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkhl/2a7752457ec279e447e1888880ab6916 to your computer and use it in GitHub Desktop.
Save mkhl/2a7752457ec279e447e1888880ab6916 to your computer and use it in GitHub Desktop.
Acme auto commands - like acmego, but generic

Autoacme

https://godoc.org/github.com/mkhl/cmd/acme/autoacme

When invoked like this

autoacme acme-onevent

it will execute its argument for each event in the acme log, i.e. an event like this

42 put /path/to/file.ext

will result in autoacme executing something like

winid=42 acme-onevent put /path/to/file.ext

and whatever happens from there is up to you!

I use something similar to the included script to run formatters and possibly linters, although I’m not sure how best to run both yet.

Acmepipe

https://godoc.org/github.com/mkhl/cmd/acme/acmepipe

Along the way I learned that making an Acme window Get its file cleared the undo stack, so to help with that I wrote acmepipe.

It is similar in spirit to both Fmt and apipe.

The main difference to those tools is that acmepipe does not assume that is’s running inside Acme and explicitly writes diagnostic output (stderr) to acme/$winid/errors.

Linting

When I started writing autoacme, I planned to run formatters on put and linters on get, and specifically to run formatters so they’d modify the file directly and then to write get to acme/$winid/ctl, which would reload the changed file and also trigger the linter.

As I noted above though, Get clears the undo stack, so I decided to use a different approach, but now I’m short a nice hook to trigger linters from.

The get event doesn’t work as nicely because it’s cumbersome to invoke manually, put will currently report wrong line numbers if the source was modified by a formatter, focus would probably be obnoxious.

I’m not sure how to approach these yet.

#!/usr/local/plan9/bin/rc
# this script gets passed the event and target
# and can do any kind of post processing
event=$1
target=$2
# $winid is also set (if the window exists)
# stdout and stderr go to acme/$winid/errors by default,
# but we can choose to handle each case differently
switch ($event$target) {
case put*.go
# when saving a .go file, we can format it
acmepipe gofmt $target
case put*.rs
# rustfmt needs extra options to write to stdout
acmepipe rustfmt --write-mode plain $target
case put*.js
# we can also run a linter, like eslint for .js
npx eslint --format unix $target
# the options make sure that acme can interpret its output directly
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment