summaryrefslogtreecommitdiff
path: root/uncrust.go
blob: 85c3f9badb27c4946c5279368f041c5bfee47274 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main

import (
	"bytes"
	"flag"
	"fmt"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"regexp"
	"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")

	pos_rx = regexp.MustCompile(`.* orig line is ([\d]+), orig col is ([\d]+)`)
	lin_rx = regexp.MustCompile(`.* orig line is ([\d]+)`)
	grb_rx = regexp.MustCompile(`stdin:([\d]+) Garbage in col ([\d]+)`)
)

func main() {
	flag.Parse()

	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

	samfile = filepath.Base(samfile)

	if e = cmd.Run(); e != nil {
		if _, ok := e.(*exec.ExitError); ok {
			str := stderr.String()
			str += "\n" + stdout.String()
			if len(samfile) != 0 {
				str = strings.ReplaceAll(str, "stdin, open_line is ", samfile+":")
				str = pos_rx.ReplaceAllString(str, fmt.Sprintf("%s:$1:#$2", samfile))
				str = lin_rx.ReplaceAllString(str, fmt.Sprintf("%s:$1", samfile))
				str = grb_rx.ReplaceAllString(str, fmt.Sprintf("%s:$1:#$2", 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
	for p := range before {
		for i := range edits[idx:] {
			if edits[i].Start == p {
				edits[i].Start = rcount
			}
			if edits[i].End == p {
				edits[i].End = rcount
				idx++
			}
		}
		rcount++
		if idx == len(edits) {
			break
		}
	}

	if len(edits) > 0 {
		if *fl_verbose {
			log.Printf("Applying %d diffs:\n%s\n", len(edits), pretty(edits, samfile))
		}
		win.Write("ctl", []byte("mark"))
		win.Write("ctl", []byte("nomark"))
	}

	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: »%s«\n", p, e.Start, e.End, e.New)
	}
	return b.String()
}