From 6b7df6babce2c9d13b3e3d1273ce9525a1969603 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Sat, 4 Nov 2023 15:00:03 +0100 Subject: [PATCH] acmego: allow arbitrary rc filters enclosed in [[ ]] --- acme/acmego/main.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/acme/acmego/main.go b/acme/acmego/main.go index 82567d8..8de6692 100644 --- a/acme/acmego/main.go +++ b/acme/acmego/main.go @@ -22,6 +22,9 @@ // // .rs - rustfmt // +// Alternatively, if the window tag contains text between "[[" and "]]" +// then the encosed text is executed with rc -c. For example: +// [[clang-format $1]]. package main import ( @@ -70,6 +73,16 @@ func main() { if event.Name == "" || event.Op != "put" { continue } + + if w, err := acme.Open(event.ID, nil); err == nil { + if wi, err := w.Info(); err == nil { + if f := findFilter(wi.Tag); f != "" { + reformat(event.ID, event.Name, []string{"rc", "-c", f}) + continue + } + } + } + for suffix, formatter := range formatters { if strings.HasSuffix(event.Name, suffix) { reformat(event.ID, event.Name, formatter) @@ -256,3 +269,23 @@ func findLines(text []byte, start, end int) []byte { endByte := i return text[startByte:endByte] } + +func findFilter(tag string) string { + const ( + tOpen = "[[" + tClose = "]]" + ) + t0 := strings.Index(tag, tOpen) + if t0 == -1 { + return "" + } + if e := strings.Index(tag, "Edit"); e >= 0 && e < t0 { + // Err on the side of caution in case "[[" is part of an Edit command. + return "" + } + t1 := strings.Index(tag, tClose) + if t1 < t0 { + return "" + } + return tag[t0+len(tOpen) : t1] +}