forked from elves/elvish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert.go
282 lines (242 loc) · 6.37 KB
/
insert.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
package edit
import (
"io"
"strings"
"unicode"
"unicode/utf8"
"github.com/elves/elvish/util"
)
// Builtins related to insert and command mode.
type insert struct {
quotePaste bool
// The number of consecutive key inserts. Used for abbreviation expansion.
literalInserts int
// Indicates whether a key was inserted (via insert-default). A hack for
// maintaining the inserts field.
insertedLiteral bool
}
func (*insert) Mode() ModeType {
return modeInsert
}
// Insert mode is the default mode and has an empty mode.
func (ins *insert) ModeLine(width int) *buffer {
if ins.quotePaste {
return makeModeLine(" INSERT (quote paste) ", width)
}
return nil
}
type command struct{}
func (*command) Mode() ModeType {
return modeCommand
}
func (*command) ModeLine(width int) *buffer {
return makeModeLine(" COMMAND ", width)
}
func startInsert(ed *Editor) {
ed.mode = &ed.insert
}
func startCommand(ed *Editor) {
ed.mode = &ed.command
}
func killLineLeft(ed *Editor) {
sol := util.FindLastSOL(ed.line[:ed.dot])
ed.line = ed.line[:sol] + ed.line[ed.dot:]
ed.dot = sol
}
func killLineRight(ed *Editor) {
eol := util.FindFirstEOL(ed.line[ed.dot:]) + ed.dot
ed.line = ed.line[:ed.dot] + ed.line[eol:]
}
// NOTE(xiaq): A word is a run of non-space runes. When killing a word,
// trimming spaces are removed as well. Examples:
// "abc xyz" -> "abc ", "abc xyz " -> "abc ".
func killWordLeft(ed *Editor) {
if ed.dot == 0 {
return
}
space := strings.LastIndexFunc(
strings.TrimRightFunc(ed.line[:ed.dot], unicode.IsSpace),
unicode.IsSpace) + 1
ed.line = ed.line[:space] + ed.line[ed.dot:]
ed.dot = space
}
// NOTE(xiaq): A small word is either a run of alphanumeric (Unicode category L
// or N) runes or a run of non-alphanumeric runes. This is consistent with vi's
// definition of word, except that "_" is not considered alphanumeric. When
// killing a small word, trimming spaces are removed as well. Examples:
// "abc/~" -> "abc", "~/abc" -> "~/", "abc* " -> "abc"
func killSmallWordLeft(ed *Editor) {
left := strings.TrimRightFunc(ed.line[:ed.dot], unicode.IsSpace)
// The case of left == "" is handled as well.
r, _ := utf8.DecodeLastRuneInString(left)
if isAlnum(r) {
left = strings.TrimRightFunc(left, isAlnum)
} else {
left = strings.TrimRightFunc(
left, func(r rune) bool { return !isAlnum(r) })
}
ed.line = left + ed.line[ed.dot:]
ed.dot = len(left)
}
func isAlnum(r rune) bool {
return unicode.IsLetter(r) || unicode.IsNumber(r)
}
func killRuneLeft(ed *Editor) {
if ed.dot > 0 {
_, w := utf8.DecodeLastRuneInString(ed.line[:ed.dot])
ed.line = ed.line[:ed.dot-w] + ed.line[ed.dot:]
ed.dot -= w
} else {
ed.flash()
}
}
func killRuneRight(ed *Editor) {
if ed.dot < len(ed.line) {
_, w := utf8.DecodeRuneInString(ed.line[ed.dot:])
ed.line = ed.line[:ed.dot] + ed.line[ed.dot+w:]
} else {
ed.flash()
}
}
func moveDotLeft(ed *Editor) {
_, w := utf8.DecodeLastRuneInString(ed.line[:ed.dot])
ed.dot -= w
}
func moveDotRight(ed *Editor) {
_, w := utf8.DecodeRuneInString(ed.line[ed.dot:])
ed.dot += w
}
func moveDotLeftWord(ed *Editor) {
if ed.dot == 0 {
return
}
space := strings.LastIndexFunc(
strings.TrimRightFunc(ed.line[:ed.dot], unicode.IsSpace),
unicode.IsSpace) + 1
ed.dot = space
}
func moveDotRightWord(ed *Editor) {
// Move to first space
p := strings.IndexFunc(ed.line[ed.dot:], unicode.IsSpace)
if p == -1 {
ed.dot = len(ed.line)
return
}
ed.dot += p
// Move to first nonspace
p = strings.IndexFunc(ed.line[ed.dot:], notSpace)
if p == -1 {
ed.dot = len(ed.line)
return
}
ed.dot += p
}
func notSpace(r rune) bool {
return !unicode.IsSpace(r)
}
func moveDotSOL(ed *Editor) {
sol := util.FindLastSOL(ed.line[:ed.dot])
ed.dot = sol
}
func moveDotEOL(ed *Editor) {
eol := util.FindFirstEOL(ed.line[ed.dot:]) + ed.dot
ed.dot = eol
}
func moveDotUp(ed *Editor) {
sol := util.FindLastSOL(ed.line[:ed.dot])
if sol == 0 {
ed.flash()
return
}
prevEOL := sol - 1
prevSOL := util.FindLastSOL(ed.line[:prevEOL])
width := util.Wcswidth(ed.line[sol:ed.dot])
ed.dot = prevSOL + len(util.TrimWcwidth(ed.line[prevSOL:prevEOL], width))
}
func moveDotDown(ed *Editor) {
eol := util.FindFirstEOL(ed.line[ed.dot:]) + ed.dot
if eol == len(ed.line) {
ed.flash()
return
}
nextSOL := eol + 1
nextEOL := util.FindFirstEOL(ed.line[nextSOL:]) + nextSOL
sol := util.FindLastSOL(ed.line[:ed.dot])
width := util.Wcswidth(ed.line[sol:ed.dot])
ed.dot = nextSOL + len(util.TrimWcwidth(ed.line[nextSOL:nextEOL], width))
}
func insertLastWord(ed *Editor) {
if ed.store == nil {
ed.addTip("store offline")
return
}
_, lastLine, err := ed.store.LastCmd(-1, "")
if err == nil {
ed.insertAtDot(lastWord(lastLine))
} else {
ed.addTip("db error: %s", err.Error())
}
}
func lastWord(s string) string {
s = strings.TrimRightFunc(s, unicode.IsSpace)
i := strings.LastIndexFunc(s, unicode.IsSpace) + 1
return s[i:]
}
func insertKey(ed *Editor) {
k := ed.lastKey
ed.insertAtDot(string(k.Rune))
}
func returnLine(ed *Editor) {
ed.nextAction = action{typ: exitReadLine, returnLine: ed.line}
}
func smartEnter(ed *Editor) {
if ed.parseErrorAtEnd {
// There is a parsing error at the end. Insert a newline and copy
// indents from previous line.
indent := findLastIndent(ed.line[:ed.dot])
ed.insertAtDot("\n" + indent)
} else {
returnLine(ed)
}
}
func findLastIndent(s string) string {
line := s[util.FindLastSOL(s):]
trimmed := strings.TrimLeft(line, " \t")
return line[:len(line)-len(trimmed)]
}
func returnEOF(ed *Editor) {
if len(ed.line) == 0 {
ed.nextAction = action{typ: exitReadLine, returnErr: io.EOF}
}
}
func toggleQuotePaste(ed *Editor) {
ed.insert.quotePaste = !ed.insert.quotePaste
}
func defaultInsert(ed *Editor) {
k := ed.lastKey
if likeChar(k) {
insertKey(ed)
// Match abbreviations.
literals := ed.line[ed.dot-ed.insert.literalInserts-1 : ed.dot]
for abbr, full := range ed.abbreviations {
if strings.HasSuffix(literals, abbr) {
ed.line = ed.line[:ed.dot-len(abbr)] + full + ed.line[ed.dot:]
ed.dot += len(full) - len(abbr)
return
}
}
// No match.
ed.insert.insertedLiteral = true
} else {
ed.Notify("Unbound: %s", k)
}
}
func defaultCommand(ed *Editor) {
k := ed.lastKey
ed.Notify("Unbound: %s", k)
}
// likeChar returns if a key looks like a character meant to be input (as
// opposed to a function key).
func likeChar(k Key) bool {
return k.Mod == 0 && k.Rune > 0 && unicode.IsGraphic(k.Rune)
}