/
shell.go
141 lines (131 loc) · 3.13 KB
/
shell.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
package main
import (
"io"
"os/exec"
"strings"
"github.com/kballard/go-shellquote"
glisp "github.com/zhemao/glisp/interpreter"
)
func shellCmd(com string, args []string) (string, error) {
cmd := exec.Command(com, args...)
out, err := cmd.CombinedOutput()
// Gomacs doesn't like trailing newlines; strip 'em
if len(out) > 0 && out[len(out)-1] == '\n' {
out = out[:len(out)-1]
}
return string(out), err
}
func shellCmdAction(com string, args []string) {
result, err := shellCmd(com, args)
if err == nil {
if Global.SetUniversal {
spitRegion(Global.CurrentB.cx, Global.CurrentB.cy, result)
} else {
showMessages(result)
}
} else {
if Global.SetUniversal {
spitRegion(Global.CurrentB.cx, Global.CurrentB.cy, err.Error()+"\n"+result)
} else {
showMessages(err.Error() + "\n" + result)
}
AddErrorMessage(err.Error())
}
}
func doShellCmd() {
var words []string
var err error
for len(words) == 0 {
words, err = shellquote.Split(editorPrompt("Shell command", nil))
if err != nil {
Global.Input = err.Error()
return
}
}
shellCmdAction(words[0], words[1:])
}
func shellCmdWithInput(input, com string, args []string) (string, error) {
cmd := exec.Command(com, args...)
stdin, err := cmd.StdinPipe()
if err != nil {
return "", err
}
go func() {
defer stdin.Close()
io.WriteString(stdin, input)
}()
out, err := cmd.CombinedOutput()
if out[len(out)-1] == '\n' {
out = out[:len(out)-1]
}
return string(out), err
}
func shellCmdRegion(com string, args []string) {
if Global.SetUniversal {
transposeRegionCmd(func(input string) string {
result, err := shellCmdWithInput(input, com, args)
if err == nil {
return result
} else {
return err.Error() + "\n" + result
}
})
} else {
regionCmd(func(buf *EditorBuffer, startc, endc, startl, endl int) string {
result, err := shellCmdWithInput(getRegionText(buf, startc, endc, startl, endl), com, args)
if err == nil {
showMessages(result)
} else {
showMessages(err.Error() + "\n" + result)
}
return ""
})
}
}
func doShellCmdRegion() {
var words []string
var err error
for len(words) == 0 {
words, err = shellquote.Split(editorPrompt("Shell command", nil))
if err != nil {
Global.Input = err.Error()
return
}
}
shellCmdRegion(words[0], words[1:])
}
func replaceBufferWithShellCommand(buf *EditorBuffer, com string, args []string, env *glisp.Glisp) {
if buf.NumRows == 0 {
return
}
output, err := shellCmdWithInput(getRegionText(buf, 0, buf.Rows[buf.NumRows-1].Size, 0, buf.NumRows-1), com, args)
if err != nil {
showMessages(err.Error(), output)
return
}
lines := strings.Split(output, "\n")
ll := len(lines)
buf.Rows = make([]*EditorRow, ll)
buf.NumRows = ll
for i, line := range lines {
newrow := &EditorRow{}
newrow.Data = line
newrow.idx = i
newrow.Size = len(line)
rowUpdateRender(newrow)
buf.Rows[i] = newrow
}
if buf.Highlighter != nil {
buf.Highlight()
}
buf.Undo = nil
buf.Redo = nil
if buf.cy >= buf.NumRows {
buf.cy = buf.NumRows - 1
}
if buf.cx >= buf.Rows[buf.cy].Size {
buf.cx = buf.Rows[buf.cy].Size
}
editorRowCxToRx(buf.Rows[buf.cy])
editorBufSave(buf, env)
}