-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
yacco.go
253 lines (215 loc) · 5.87 KB
/
yacco.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package main
import (
"flag"
"image"
"log"
"os"
"path/filepath"
"runtime/debug"
"runtime/pprof"
"strconv"
"strings"
"github.com/aarzilli/yacco/buf"
"github.com/aarzilli/yacco/clipboard"
"github.com/aarzilli/yacco/config"
"github.com/aarzilli/yacco/edit"
"github.com/aarzilli/yacco/util"
"golang.org/x/exp/shiny/driver"
"golang.org/x/exp/shiny/screen"
"net/http"
)
var Wnd Window
var sideChan chan func()
var AutoDumpPath string
var FirstOpenFile bool = true
var themeFlag = flag.String("t", "", "Theme to use (standard, evening, midnight, bw)")
var dumpFlag = flag.String("d", "", "Dump file to load")
var sizeFlag = flag.String("s", "", "Size of window")
var configFlag = flag.String("c", "", "Configuration file (defaults to ~/.config/yacco/rc)")
var acmeCompatFlag = flag.Bool("acme", false, "Uses acme file to listen")
var cpuprofileFlag = flag.String("cpuprofile", "", "Write cpu profile to file")
var memprofileFlag = flag.String("memprofile", "", "Write memory profile to file")
var pprofServerFlag = flag.Bool("pprof", false, "Start pprof server")
var fontSizeChangeFlag = flag.Int("fsc", 0, "Initial font size change")
var tagColors = [][]image.Uniform{
config.TheColorScheme.TagPlain,
config.TheColorScheme.TagSel1,
config.TheColorScheme.TagSel2,
config.TheColorScheme.TagSel3,
config.TheColorScheme.TagMatchingParenthesis,
}
var editorColors = [][]image.Uniform{
config.TheColorScheme.EditorPlain,
config.TheColorScheme.EditorSel1, // 0 first button selection
config.TheColorScheme.EditorSel2, // 1 second button selection
config.TheColorScheme.EditorSel3, // 2 third button selection
config.TheColorScheme.EditorMatchingParenthesis, // 3 matching parenthesis
}
func setTheme(t string) {
cs, ok := config.ColorSchemeMap[t]
if !ok {
cs = &config.AcmeColorScheme
}
config.TheColorScheme = *cs
tagColors[0] = config.TheColorScheme.TagPlain
tagColors[1] = config.TheColorScheme.TagSel1
tagColors[2] = config.TheColorScheme.TagSel2
tagColors[3] = config.TheColorScheme.TagSel3
tagColors[4] = config.TheColorScheme.TagMatchingParenthesis
editorColors[0] = config.TheColorScheme.EditorPlain
editorColors[1] = config.TheColorScheme.EditorSel1
editorColors[2] = config.TheColorScheme.EditorSel2
editorColors[3] = config.TheColorScheme.EditorSel3
editorColors[4] = config.TheColorScheme.EditorMatchingParenthesis
if Wnd.cols != nil {
for _, col := range Wnd.cols.cols {
for _, ed := range col.editors {
ed.sfr.Color = config.TheColorScheme.Scrollbar
}
}
}
}
func realmain(s screen.Screen) {
setTheme(*themeFlag)
width := config.StartupWidth
height := config.StartupHeight
os.Setenv("TERM", "dumb")
if *sizeFlag != "" {
v := strings.Split(*sizeFlag, "x")
if len(v) == 2 {
var err error
width, err = strconv.Atoi(v[0])
if err != nil {
width = 640
}
height, err = strconv.Atoi(v[1])
if err != nil {
height = 480
}
}
}
err := Wnd.Init(s, width, height)
if err != nil {
log.Fatalf(err.Error())
}
defer Wnd.Close()
Wnd.cols.AddAfter(NewCol(&Wnd, Wnd.cols.r), -1, 0.4)
if len(flag.Args()) != 1 {
rightcol := NewCol(&Wnd, Wnd.cols.r)
Wnd.cols.AddAfter(rightcol, -1, 0.4)
activeCol = rightcol
}
wd, _ := os.Getwd()
hasarg := false
if *dumpFlag == "" {
toline := -1
for _, arg := range flag.Args() {
if len(arg) > 0 && arg[0] == '+' {
toline, _ = strconv.Atoi(arg)
} else {
hasarg = true
ed, _ := EditFind(wd, arg, false, true)
if toline > 0 && ed != nil {
addr := edit.AddrList{
[]edit.Addr{&edit.AddrBase{"", strconv.Itoa(toline), +1},
&edit.AddrBase{"#", "0", -1}}}
ed.sfr.Fr.Sel = addr.Eval(ed.bodybuf, ed.sfr.Fr.Sel)
ed.BufferRefresh()
}
if toline <= 0 && ed != nil {
historyAdd(filepath.Join(ed.bodybuf.Dir, ed.bodybuf.Name))
}
}
}
} else {
dumpDest := getDumpPath(*dumpFlag, false)
if LoadFrom(dumpDest) {
hasarg = true
AutoDumpPath = dumpDest
setDumpTitle()
}
}
startWinTag := "Help"
if !hasarg {
EditFind(wd, ".", false, false)
LoadCmd(ExecContext{}, "")
} else if len(flag.Args()) == 1 {
startWinTag += " Load"
}
Wnd.tagbuf.Replace([]rune(startWinTag), &util.Sel{Wnd.tagbuf.Size(), Wnd.tagbuf.Size()}, true, nil, 0)
Wnd.BufferRefresh()
Wnd.FlushImage()
debug.FreeOSMemory()
Wnd.EventLoop()
}
func main() {
flag.Parse()
if *pprofServerFlag {
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
}
if fontSizeChangeFlag != nil {
config.FontSizeChange = *fontSizeChangeFlag
}
config.LoadConfiguration(*configFlag)
config.LoadTemplates()
LoadInit()
KeysInit()
clipboard.Start()
if *cpuprofileFlag != "" {
f, err := os.Create(*cpuprofileFlag)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
edit.Warnfn = Warn
edit.NewJob = func(wd, cmd, input string, buf *buf.Buffer, resultChan chan<- string) {
NewJob(wd, cmd, input, &ExecContext{buf: buf}, false, false, resultChan)
}
sideChan = make(chan func(), 5)
FsInit()
driver.Main(realmain)
}
func removeBuffer(b *buf.Buffer) {
Wnd.Words = util.Dedup(append(Wnd.Words, b.Words...))
}
func bufferExecContext(i int) *ExecContext {
done := make(chan *ExecContext)
sideChan <- func() {
for _, col := range Wnd.cols.cols {
for _, ed := range col.editors {
if ed.edid == i {
done <- editorColExecContext(ed, col)
return
}
}
}
done <- nil
return
}
return <-done
}
func editorExecContext(ed *Editor) *ExecContext {
for _, col := range Wnd.cols.cols {
for _, ed2 := range col.editors {
if ed == ed2 {
return editorColExecContext(ed, col)
}
}
}
return nil
}
func editorColExecContext(ed *Editor, col *Col) *ExecContext {
return &ExecContext{
col: col,
ed: ed,
br: ed.BufferRefresh,
fr: &ed.sfr.Fr,
buf: ed.bodybuf,
eventChan: ed.eventChan,
dir: ed.bodybuf.Dir,
}
}