commit fc42dd0fcc03d5cf83004538f03f6f9cc89a88ae Author: Özgür Kesim Date: Sun Dec 8 22:41:16 2024 +0100 init diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c0a76f4 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module Uncrust + +go 1.23.4 + +require ( + 9fans.net/go v0.0.7 // indirect + github.com/akedrou/textdiff v0.1.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ae0c1ea --- /dev/null +++ b/go.sum @@ -0,0 +1,36 @@ +9fans.net/go v0.0.7 h1:H5CsYJTf99C8EYAQr+uSoEJnLP/iZU8RmDuhyk30iSM= +9fans.net/go v0.0.7/go.mod h1:Rxvbbc1e+1TyGMjAvLthGTyO97t+6JMQ6ly+Lcs9Uf0= +dmitri.shuralyov.com/gpu/mtl v0.0.0-20201218220906-28db891af037/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= +github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/akedrou/textdiff v0.1.0 h1:K7nbOVQju7/coCXnJRJ2fsltTwbSvC+M4hKBUJRBRGY= +github.com/akedrou/textdiff v0.1.0/go.mod h1:a9CCC49AKtFTmVDNFHDlCg7V/M7C7QExDAhb2SkL6DQ= +github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4= +golang.org/x/exp v0.0.0-20210405174845-4513512abef3/go.mod h1:I6l2HNBLBZEcrOoCpyKLdY2lHoRZ8lI4x60KMCQDft4= +golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= +golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= +golang.org/x/mobile v0.0.0-20201217150744-e6ae53a27f4f/go.mod h1:skQtrUTUwhdJvXM/2KKJzY8pDgNr9I/FOMqDVRPBUS4= +golang.org/x/mobile v0.0.0-20210220033013-bdb1ca9a1e08/go.mod h1:skQtrUTUwhdJvXM/2KKJzY8pDgNr9I/FOMqDVRPBUS4= +golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= +golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= +golang.org/x/mod v0.1.1-0.20191209134235-331c550502dd/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.1-0.20200828183125-ce943fd02449/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210415045647-66c3f260301c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200117012304-6edc0a871e69/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/tools v0.0.0-20200207183749-b753a1ba74fa/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/uncrust.go b/uncrust.go new file mode 100644 index 0000000..a8a362e --- /dev/null +++ b/uncrust.go @@ -0,0 +1,118 @@ +package main + +import ( + "bytes" + "flag" + "fmt" + "log" + "os" + "os/exec" + "strconv" + "strings" + + "9fans.net/go/acme" + "github.com/akedrou/textdiff" +) + +var ( + fl_lang = flag.String("l", "C", "Language") + fl_verbose = flag.Bool("v", false, "Verbosity on") +) + +func main() { + samfile := os.Getenv("samfile") + id := os.Getenv("winid") + if "" == id { + log.Fatalf("no $winid set, not running in acme?") + } + + wid, e := strconv.Atoi(id) + if e != nil { + log.Fatalf("couldn't parse $winid: %s\n", e) + } + + win, e := acme.Open(wid, nil) + if e != nil { + log.Fatalf("couldn't open acme window id %d: %s\n", wid, e) + } + + body, e := win.ReadAll("body") + if e != nil { + log.Fatalf("couldn't read body of window id %d: %s\n", wid, e) + } + + args := []string{"-l", *fl_lang} + stderr := &bytes.Buffer{} + stdout := &bytes.Buffer{} + + cmd := exec.Command("uncrustify", args...) + cmd.Stdin = bytes.NewBuffer(body) + cmd.Stdout = stdout + cmd.Stderr = stderr + + if e = cmd.Run(); e != nil { + if _, ok := e.(*exec.ExitError); ok { + str := stderr.String() + if len(samfile) != 0 { + str = strings.ReplaceAll(str, "stdin, open_line is ", samfile+":") + } + log.Fatal("error: " + str) + } + log.Fatalf("error: %s\n", e) + return + } + + after := stdout.String() + before := string(body) + edits := textdiff.Strings(before, after) + textdiff.SortEdits(edits) + + // We have to calculate the rune position from the byte position. + rcount := 0 + idx := 0 + cont := false + for p := range before { + cont = false + for i := range edits[idx:] { + cont = true + if edits[i].Start == p { + edits[i].Start = rcount + } + if edits[i].End == p { + edits[i].End = rcount + idx++ + } + } + rcount++ + if !cont { + break + } + } + + if len(edits) > 0 { + win.Write("ctl", []byte("mark")) + win.Write("ctl", []byte("nomark")) + if *fl_verbose { + log.Printf("Applying %d diffs:\n%s\n", len(edits), pretty(edits, samfile)) + } + } + + for i := len(edits) - 1; i >= 0; i-- { + edit := edits[i] + + addr := fmt.Sprintf("#%d,#%d", edit.Start, edit.End) + e = win.Addr(addr) + if e != nil { + log.Fatalf("error writing to acme/%d/addr: %s\n", wid, e) + } + win.Write("data", []byte(edit.New)) + } +} + +func pretty(e []textdiff.Edit, p string) string { + b := &bytes.Buffer{} + for _, e := range e { + fmt.Fprintf(b, "%s:#%d,#%d\n", p, e.Start, e.End) + } + return b.String() +}