-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor_cmd_line.go
663 lines (581 loc) · 15.5 KB
/
editor_cmd_line.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
package main
import (
"bufio"
"bytes"
"io"
"os"
"os/exec"
"strconv"
"strings"
"github.com/mandolyte/mdtopdf/v2"
"github.com/slzatz/vimango/vim"
)
var e_lookup_C = map[string]func(*Editor){
"write": (*Editor).writeNote,
"w": (*Editor).writeNote,
"wa": (*Editor).writeAll,
"qa": (*Editor).quitAll,
"read": (*Editor).readFile,
"readfile": (*Editor).readFile,
"vertical resize": (*Editor).verticalResize,
"vert res": (*Editor).verticalResize,
"resize": (*Editor).resize,
"res": (*Editor).resize,
"compile": (*Editor).compile,
"c": (*Editor).compile,
"run": (*Editor).run,
"r": (*Editor).run,
"save": (*Editor).saveNoteToFile,
"savefile": (*Editor).saveNoteToFile,
"syntax": (*Editor).syntax,
"number": (*Editor).number,
"num": (*Editor).number,
"ha": (*Editor).printNote,
"quit": (*Editor).quitActions,
"q": (*Editor).quitActions,
"quit!": (*Editor).quitActions,
"q!": (*Editor).quitActions,
"x": (*Editor).quitActions,
"fmt": (*Editor).goFormat,
"pdf": (*Editor).createPDF,
"print": (*Editor).printDocument,
//"spell": (*Editor).spell,
}
func (e *Editor) saveNoteToFile() {
pos := strings.Index(e.command_line, " ")
if pos == -1 {
sess.showEdMessage("You need to provide a filename")
return
}
filename := e.command_line[pos+1:]
f, err := os.Create(filename)
if err != nil {
sess.showEdMessage("Error creating file %s: %v", filename, err)
return
}
defer f.Close()
_, err = f.WriteString(strings.Join(e.ss, "\n"))
if err != nil {
sess.showEdMessage("Error writing file %s: %v", filename, err)
return
}
sess.showEdMessage("Note written to file %s", filename)
}
func (e *Editor) writeNote() {
text := e.bufferToString()
if taskFolder(e.id) == "code" {
go updateCodeFile(e.id, text)
}
updateNote(e.id, text)
//explicitly writes note to set isModified to false
//vim.Execute("w")
e.bufferTick = vim.BufferGetLastChangedTick(e.vbuf)
e.drawStatusBar() //need this since now refresh won't do it unless redraw =true
sess.showEdMessage("isModified = %t", e.isModified())
}
func (e *Editor) readFile() {
pos := strings.Index(e.command_line, " ")
if pos == -1 {
sess.showEdMessage("You need to provide a filename")
return
}
filename := e.command_line[pos+1:]
err := e.readFileIntoNote(filename)
if err != nil {
sess.showEdMessage("%v", err)
return
}
sess.showEdMessage("Note generated from file: %s", filename)
}
// testing abs move but may revert to this one
func (e *Editor) verticalResize__() {
pos := strings.LastIndex(e.command_line, " ")
if pos == -1 {
sess.showEdMessage("You need to provide a a number 0 - 100")
return
}
pct, err := strconv.Atoi(e.command_line[pos+1:])
if err != nil {
sess.showEdMessage("You need to provide a number 0 - 100")
return
}
moveDividerPct(pct)
}
func (e *Editor) verticalResize() {
pos := strings.LastIndex(e.command_line, " ")
opt := e.command_line[pos+1:]
width, err := strconv.Atoi(opt)
if opt[0] == '+' || opt[0] == '-' {
width = sess.screenCols - sess.divider + width
}
if err != nil {
sess.showEdMessage("The format is :vert[ical] res[ize] N")
return
}
moveDividerAbs(width)
}
func (e *Editor) resize() {
pos := strings.Index(e.command_line, " ")
opt := e.command_line[pos+1:]
if opt[0] == '+' || opt[0] == '-' {
num, err := strconv.Atoi(opt[1:])
if err != nil {
sess.showEdMessage("The format is [+/-]N")
return
}
for i := 0; i < num; i++ {
e.changeSplit(int(opt[0]))
}
} else {
num, err := strconv.Atoi(opt)
if err != nil {
sess.showEdMessage("The format is [+/-]N")
return
}
if sess.textLines-num < 3 || num < 2 {
return
}
e.screenlines = num
op := e.output
op.screenlines = sess.textLines - num - 1
op.top_margin = num + 3
sess.eraseRightScreen()
sess.drawRightScreen()
}
}
func (e *Editor) compile() {
var dir string
var cmd *exec.Cmd
lang := Languages[taskContext(e.id)]
if lang == "cpp" {
dir = "/home/slzatz/clangd_examples/"
cmd = exec.Command("make")
} else if lang == "go" {
dir = "/home/slzatz/go_fragments/"
//cmd = exec.Command("go", "build", "main.go")
cmd = exec.Command("go", "build")
} else if lang == "python" {
sess.showEdMessage("You don't have to compile python")
return
} else {
sess.showEdMessage("I don't recognize %q", taskContext(e.id))
return
}
cmd.Dir = dir
stdout, err := cmd.StdoutPipe()
if err != nil {
sess.showEdMessage("Error in compile creating stdout pipe: %v", err)
return
}
stderr, err := cmd.StderrPipe()
if err != nil {
sess.showEdMessage("Error in compile creating stderr pipe: %v", err)
return
}
err = cmd.Start()
if err != nil {
sess.showEdMessage("Error in compile starting command: %v", err)
return
}
buffer_out := bufio.NewReader(stdout)
buffer_err := bufio.NewReader(stderr)
var rows []string
rows = append(rows, "------------------------")
for {
bytes, _, err := buffer_out.ReadLine()
if err == io.EOF {
break
}
rows = append(rows, string(bytes))
}
for {
bytes, _, err := buffer_err.ReadLine()
if err == io.EOF {
break
}
rows = append(rows, string(bytes))
}
if len(rows) == 1 {
rows = append(rows, "The code compiled successfully")
}
rows = append(rows, "------------------------")
op := e.output
op.rowOffset = 0
op.rows = rows
op.drawText()
// no need to call drawFrame or drawStatusBar
}
func (e *Editor) run() {
var args []string
pos := strings.Index(e.command_line, " ")
if pos != -1 {
args = strings.Split(e.command_line[pos+1:], " ")
}
var dir string
var obj string
var cmd *exec.Cmd
//if getFolderTid(e.id) == 18 {
if taskContext(e.id) == "cpp" {
obj = "./test_cpp"
dir = "/home/slzatz/clangd_examples/"
} else {
//obj = "./main"
obj = "./go_fragments"
dir = "/home/slzatz/go_fragments/"
}
lang := Languages[taskContext(e.id)]
if lang == "cpp" {
dir = "/home/slzatz/clangd_examples/"
cmd = exec.Command("make")
} else if lang == "go" {
dir = "/home/slzatz/go_fragments/"
cmd = exec.Command("go", "build", "main.go")
} else if lang == "python" {
obj = "./main.py"
dir = "/home/slzatz/python_fragments/"
} else {
sess.showEdMessage("I don't recognize %q", taskContext(e.id))
return
}
cmd = exec.Command(obj, args...)
cmd.Dir = dir
stdout, err := cmd.StdoutPipe()
if err != nil {
sess.showEdMessage("Error in run creating stdout pipe: %v", err)
return
}
stderr, err := cmd.StderrPipe()
if err != nil {
sess.showEdMessage("Error in run creating stderr pipe: %v", err)
return
}
err = cmd.Start()
if err != nil {
sess.showEdMessage("Error in run starting command: %v", err)
return
}
buffer_out := bufio.NewReader(stdout)
buffer_err := bufio.NewReader(stderr)
var rows []string
rows = append(rows, "%-----------------------")
for {
bytes, _, err := buffer_out.ReadLine()
if err == io.EOF {
break
}
rows = append(rows, string(bytes))
}
for {
bytes, _, err := buffer_err.ReadLine()
if err == io.EOF {
break
}
rows = append(rows, string(bytes))
}
rows = append(rows, "------------------------")
op := e.output
op.rowOffset = 0
op.rows = rows
op.drawText()
// no need to call drawFrame or drawStatusBar
}
func (e *Editor) syntax() {
e.highlightSyntax = !e.highlightSyntax
if e.highlightSyntax {
e.left_margin_offset = LEFT_MARGIN_OFFSET
//e.checkSpelling = false // can't syntax highlight(including markdown) and check spelling
}
e.drawText()
// no need to call drawFrame or drawStatusBar
sess.showEdMessage("Syntax highlighting is %v", e.highlightSyntax)
}
func (e *Editor) printNote() {
vim.Execute("ha")
}
/*
// debugging
func (e *Editor) modified() {
var result bool
err := v.BufferOption(0, "modified", &result) //or e.vbuf
if err != nil {
sess.showEdMessage("%s", err)
return
}
sess.showEdMessage("Modified = %t", result)
}
*/
func (e *Editor) quitActions() {
cmd := e.command_line
if cmd == "x" {
text := e.bufferToString()
updateNote(e.id, text)
} else if cmd == "q!" || cmd == "quit!" {
// do nothing = allow editor to be closed
} else if e.isModified() {
e.mode = NORMAL
e.command = ""
e.command_line = ""
sess.showEdMessage("No write since last change")
return
}
vim.Execute("bw") // wipout the buffer
index := -1
for i, w := range windows {
if w == e {
index = i
break
}
}
copy(windows[index:], windows[index+1:])
windows = windows[:len(windows)-1]
if e.output != nil {
index = -1
for i, w := range windows {
if w == e.output {
index = i
break
}
}
copy(windows[index:], windows[index+1:])
windows = windows[:len(windows)-1]
}
//if len(windows) > 0 {
if sess.numberOfEditors() > 0 {
// easier to just go to first window which has to be an editor (at least right now)
for _, w := range windows {
if ed, ok := w.(*Editor); ok { //need the type assertion
p = ed //p is the global current editor
break
}
}
//p = windows[0].(*Editor)
vim.BufferSetCurrent(p.vbuf)
sess.positionWindows()
sess.eraseRightScreen()
sess.drawRightScreen()
} else { // we've quit the last remaining editor(s)
// unless commented out earlier sess.p.quit <- causes panic
//sess.p = nil
sess.editorMode = false
vim.BufferSetCurrent(org.vbuf) ///////////////////////////////////////////////////////////
sess.eraseRightScreen()
if sess.divider < 10 {
sess.edPct = 80
moveDividerPct(80)
}
//org.readTitleIntoBuffer() // shouldn't be necessary
org.drawPreview()
sess.returnCursor() //because main while loop if started in editor_mode -- need this 09302020
}
}
func (e *Editor) writeAll() {
for _, w := range windows {
if ed, ok := w.(*Editor); ok {
vim.BufferSetCurrent(ed.vbuf)
ed.writeNote()
}
}
vim.BufferSetCurrent(e.vbuf)
e.command_line = ""
e.mode = NORMAL
}
func (e *Editor) quitAll() {
for _, w := range windows {
if ed, ok := w.(*Editor); ok {
if ed.isModified() {
continue
} else {
index := -1
for i, w := range windows {
if w == ed {
index = i
break
}
}
copy(windows[index:], windows[index+1:])
windows = windows[:len(windows)-1]
if ed.output != nil {
index = -1
for i, w := range windows {
if w == ed.output {
index = i
break
}
}
copy(windows[index:], windows[index+1:])
windows = windows[:len(windows)-1]
}
}
}
}
if sess.numberOfEditors() > 0 { // we could not quit some editors because they were in modified state
for _, w := range windows {
if ed, ok := w.(*Editor); ok { //need this type assertion to have statement below
p = ed //p is the global representing the current editor
break
}
}
vim.BufferSetCurrent(p.vbuf)
sess.positionWindows()
sess.eraseRightScreen()
sess.drawRightScreen()
sess.showEdMessage("Some editors had no write since the last change")
} else { // we've been able to quit all editors because none were in modified state
sess.editorMode = false
vim.BufferSetCurrent(org.vbuf) ///////////////////////////////////////////////////////////
sess.eraseRightScreen()
if sess.divider < 10 {
sess.edPct = 80
moveDividerPct(80)
}
//org.readTitleIntoBuffer() // shouldn't be necessary
org.drawPreview()
sess.returnCursor() //because main while loop if started in editor_mode -- need this 09302020
}
}
/*
func (e *Editor) spell() {
e.checkSpelling = !e.checkSpelling
if e.checkSpelling {
e.highlightSyntax = false // when you check spelling syntax highlighting off
err := v.Command("set spell")
if err != nil {
sess.showEdMessage("Error in setting spelling %v", err)
}
} else {
err := v.Command("set nospell")
if err != nil {
sess.showEdMessage("Error in setting no spelling %v", err)
}
}
e.drawText()
sess.showEdMessage("Spelling is %t", e.checkSpelling)
}
*/
func (e *Editor) number() {
e.numberLines = !e.numberLines
if e.numberLines {
e.left_margin_offset = LEFT_MARGIN_OFFSET
} else {
e.left_margin_offset = 0
}
e.drawText()
sess.showEdMessage("Line numbering is %t", e.numberLines)
}
func (e *Editor) goFormat() {
ss := []string{}
//cmd := exec.Command("gofmt")
cmd := exec.Command("goimports")
stdout, err := cmd.StdoutPipe()
if err != nil {
sess.showEdMessage("Problem in goimports stdout: %v", err)
return
}
buf_out := bufio.NewReader(stdout)
stdin, err := cmd.StdinPipe()
if err != nil {
sess.showEdMessage("Problem in goimports stdin: %v", err)
return
}
err = cmd.Start()
if err != nil {
sess.showEdMessage("Problem in cmd.Start (goimports) stdin: %v", err)
return
}
for _, row := range e.ss {
io.WriteString(stdin, row+"\n")
}
stdin.Close()
for {
s, err := buf_out.ReadString('\n')
if err == io.EOF {
break
}
ss = append(ss, s[:len(s)-1])
}
if len(ss) == 0 {
sess.showOrgMessage("Return from goimports has length zero - likely code errors")
return
}
e.ss = ss
vim.BufferSetLines(e.vbuf, 0, -1, e.ss, len(e.ss))
lines := vim.BufferGetLineCount(e.vbuf)
sess.showOrgMessage("Number of lines in the formatted text = %d", lines)
vim.CursorSetPosition(1, 0)
e.fr = 0
e.fc = 0
e.scroll()
e.drawText()
sess.returnCursor()
}
func (e *Editor) createPDF() {
pos := strings.Index(e.command_line, " ")
if pos == -1 {
sess.showEdMessage("You need to provide a filename")
return
}
filename := e.command_line[pos+1:]
pf := mdtopdf.NewPdfRenderer("", "", filename, "trace.log", nil, mdtopdf.LIGHT)
/*
pf.Pdf.SetSubject("How to convert markdown to PDF", true)
pf.Pdf.SetTitle("Example PDF converted from Markdown", true)
pf.THeader = mdtopdf.Styler{Font: "Times", Style: "IUB", Size: 20, Spacing: 2,
TextColor: mdtopdf.Color{Red: 0, Green: 0, Blue: 0},
FillColor: mdtopdf.Color{Red: 179, Green: 179, Blue: 255}}
*/
pf.TBody = mdtopdf.Styler{Font: "Arial", Style: "", Size: 12, Spacing: 2,
TextColor: mdtopdf.Color{Red: 0, Green: 0, Blue: 0},
FillColor: mdtopdf.Color{Red: 255, Green: 255, Blue: 255}}
content := strings.Join(e.ss, "\n")
err := pf.Process([]byte(content))
if err != nil {
sess.showEdMessage("pdf error:%v", err)
}
}
func (e *Editor) printDocument() {
if taskFolder(e.id) == "code" {
c := taskContext(e.id)
var ok bool
var lang string
if lang, ok = Languages[c]; !ok {
sess.showEdMessage("I don't recognize the language")
return
}
note := readNoteIntoString(e.id)
var buf bytes.Buffer
// github seems to work pretty well for printer output
_ = Highlight(&buf, note, lang, "html", "github")
f, err := os.Create("output.html")
if err != nil {
sess.showEdMessage("Error creating output.html: %v", err)
return
}
defer f.Close()
_, err = f.WriteString(buf.String())
if err != nil {
sess.showEdMessage("Error writing output.html: %s: %v", err)
return
}
cmd := exec.Command("wkhtmltopdf", "--enable-local-file-access",
"--no-background", "--minimum-font-size", "16", "output.html", "output.pdf")
err = cmd.Run()
if err != nil {
sess.showEdMessage("Error creating pdf from code: %v", err)
}
} else {
content := strings.Join(e.ss, "\n")
pf := mdtopdf.NewPdfRenderer("", "", "output.pdf", "trace.log", nil, mdtopdf.LIGHT)
pf.TBody = mdtopdf.Styler{Font: "Arial", Style: "", Size: 12, Spacing: 2,
TextColor: mdtopdf.Color{Red: 0, Green: 0, Blue: 0},
FillColor: mdtopdf.Color{Red: 255, Green: 255, Blue: 255}}
err := pf.Process([]byte(content))
if err != nil {
sess.showEdMessage("pdf error:%v", err)
}
}
cmd := exec.Command("lpr", "output.pdf")
err := cmd.Run()
if err != nil {
sess.showEdMessage("Error printing document: %v", err)
}
}