Skip to content

Commit

Permalink
acmego: allow arbitrary rc filters enclosed in [[ ]]
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobvosmaer committed Nov 26, 2023
1 parent 48951ee commit 6b7df6b
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions acme/acmego/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]
}

0 comments on commit 6b7df6b

Please sign in to comment.