-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
176 lines (162 loc) · 3.42 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
Comments/uncomments piped text
Usage of com:
|com
`com` uses the `commentstyle` you've configured a given file extension to
comment or uncomment a given selection in acme. Just as with `a+` or `a-`, you
can use `com` by writing `|com` to your scratch area, selecting the text you
want to un/comment, and then middle click on `|com` to execute the command.
*/
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"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))
comment := ft.Comment
if len(comment) == 0 {
comment = "# "
}
var startcom string = comment
var endcom string
parts := strings.Split(strings.TrimSuffix(comment, " "), " ")
if len(parts) > 1 {
if len(parts[0]) > 0 {
startcom = parts[0] + " "
}
if len(parts[1]) > 0 {
endcom = " " + parts[1]
}
}
startcomlen := len(startcom)
endcomlen := len(endcom)
var commentedLines, uncommentedLines int
var commentIdx int
var hasCommented bool
in := []byte{}
reader := bufio.NewReader(os.Stdin)
for {
b, err := reader.ReadByte()
if err != nil && err == io.EOF {
break
}
if commentIdx < len(startcom) && b == startcom[commentIdx] {
commentIdx++
if commentIdx == len(startcom) {
hasCommented = true
commentIdx = 0
}
} else {
commentIdx = 0
}
if b == '\n' {
if hasCommented {
commentedLines++
hasCommented = false
} else {
uncommentedLines++
}
}
in = append(in, b)
}
shouldComment := uncommentedLines > commentedLines
var i, comidx int
var hasstart bool
fromstart := 0
prevstartcom := -1
bufa := -1
for {
if i >= len(in) {
break
}
shouldStart := startcomlen > 0 && !hasstart && shouldComment
inlineWithPrevStart := prevstartcom >= 0 && fromstart >= prevstartcom && !hasstart
isNotWhitespace := in[i] != '\t' && in[i] != ' '
inComment := comidx > 0
if !shouldComment {
if comidx < len(endcom) && in[i] == endcom[comidx] {
if bufa < 0 {
bufa = i
}
comidx++
if comidx == len(endcom) {
bufa = -1
}
i++
continue
} else if comidx < len(startcom) && in[i] == startcom[comidx] {
if bufa < 0 {
bufa = i
}
comidx++
if comidx == len(startcom) {
for j := bufa + len(startcom); j < i; j++ {
fmt.Fprintf(os.Stdout, "%c", in[j])
}
bufa = -1
}
i++
continue
} else {
if bufa >= 0 {
for j := bufa; j < i; j++ {
fmt.Fprintf(os.Stdout, "%c", in[j])
}
comidx = 0
bufa = -1
} else {
comidx = 0
goto Out
}
}
} else if in[i] == '\n' {
if !hasstart {
fromstart = -1
prevstartcom = -1
goto Out
}
if endcomlen > 0 {
if comidx >= endcomlen {
comidx = 0
} else {
fmt.Fprintf(os.Stdout, "%c", endcom[comidx])
comidx++
fromstart++
continue
}
}
hasstart = false
fromstart = -1
} else if inlineWithPrevStart || (shouldStart && (isNotWhitespace || inComment)) {
if comidx >= startcomlen {
comidx = 0
hasstart = true
} else {
if prevstartcom < 0 {
prevstartcom = fromstart
}
fmt.Fprintf(os.Stdout, "%c", startcom[comidx])
comidx++
fromstart++
continue
}
}
Out:
fmt.Fprintf(os.Stdout, "%c", in[i])
i++
fromstart++
}
}