-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
82 lines (76 loc) · 1.73 KB
/
main.go
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
/*
Unindents selected source code.
`a+` and `a-` use your indentation settings to indent or unindent
your selection in acme using either tabs or spaces depending on
what is configured. To use these commands, write `|a+` or `|a-` to
the scratch area in your acme window, select the text you want to
indent, and then middle click on `|a+` to indent or `|a-` to unindent
your selection. These commands are also used by `nynetab` for
indenting/unindenting text depending on the provided flags.
*/
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"github.com/dnjp/nyne"
)
func main() {
filename := os.Getenv("samfile")
if filename == "" {
filename = os.Getenv("%")
}
if filename == "" {
fmt.Fprintf(os.Stderr, "$samfile and $%% are empty. are you sure you're in acme?")
os.Exit(1)
}
ft, _ := nyne.FindFiletype(nyne.Filename(filename))
tw := ft.Tabwidth
te := ft.Tabexpand
if tw == 0 {
tab := os.Getenv("tabstop")
if tab == "" {
tw = 8
} else {
ntw, err := strconv.Atoi(tab)
if err != nil {
panic(fmt.Errorf("invalid $tabstop: %v", err))
}
tw = ntw
}
}
tab := nyne.Tab(tw, te)
var i, indentidx, lastnl, indentc, nlc int
lastnl = -1
buf := make([]byte, 0)
reader := bufio.NewReader(os.Stdin)
for {
b, err := reader.ReadByte()
if err != nil && err == io.EOF {
break
}
if b == '\n' {
nlc++
lastnl = i
}
inindent := b == tab[indentidx]
if i == 0 && inindent || ((i > 0 && i-1 == lastnl) && inindent) {
indentidx++
if indentidx == len(tab) {
buf = make([]byte, 0)
indentidx = 0
indentc++
}
buf = append(buf, b)
i++
continue
} else if indentidx > 0 || len(buf) > 0 {
buf = make([]byte, 0)
indentidx = 0
}
fmt.Fprintf(os.Stdout, "%c", b)
i++
}
}