forked from jroimartin/gocui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
edit.go
397 lines (355 loc) · 9.39 KB
/
edit.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
// Copyright 2014 The gocui Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gocui
const maxInt = int(^uint(0) >> 1)
// Editor interface must be satisfied by gocui editors.
type Editor interface {
Edit(v *View, key Key, ch rune, mod Modifier)
}
// The EditorFunc type is an adapter to allow the use of ordinary functions as
// Editors. If f is a function with the appropriate signature, EditorFunc(f)
// is an Editor object that calls f.
type EditorFunc func(v *View, key Key, ch rune, mod Modifier)
// Edit calls f(v, key, ch, mod)
func (f EditorFunc) Edit(v *View, key Key, ch rune, mod Modifier) {
f(v, key, ch, mod)
}
// DefaultEditor is the default editor.
var DefaultEditor Editor = EditorFunc(simpleEditor)
// simpleEditor is used as the default gocui editor.
func simpleEditor(v *View, key Key, ch rune, mod Modifier) {
switch {
case ch != 0 && mod == 0:
v.EditWrite(ch)
case key == KeySpace:
v.EditWrite(' ')
case key == KeyBackspace || key == KeyBackspace2:
v.EditDelete(true)
case key == KeyDelete:
v.EditDelete(false)
case key == KeyInsert:
v.Overwrite = !v.Overwrite
}
}
// EditWrite writes a rune at the cursor position.
func (v *View) EditWrite(ch rune) {
rx, ry, _ := v.realPosition(v.cx, v.cy)
if ch != ' ' {
v.Actions.Exec(NewWriteCmd(v, rx, ry, ch))
} else {
v.Actions.Exec(NewSpaceCmd(v, rx, ry))
}
v.writeRune(v.cx, v.cy, ch)
v.MoveCursor(1, 0, true)
}
// EditNewLine inserts a new line under the cursor.
func (v *View) EditNewLine() {
rx, ry, _ := v.realPosition(v.cx, v.cy)
v.Actions.Exec(NewNewLineCmd(v, rx, ry))
v.breakLine(v.cx, v.cy)
y := v.oy + v.cy
if y >= len(v.viewLines) || (y >= 0 && y < len(v.viewLines) &&
!(v.Wrap && v.cx == 0 && v.viewLines[y].linesX > 0)) {
// new line at the end of the buffer or
// cursor is not at the beginning of a wrapped line
v.ox = 0
v.cx = 0
v.MoveCursor(0, 1, true)
}
}
// EditPermutLines permuts the line at the cursor position
// with the upper or the lower one, given the boolean value
func (v *View) EditPermutLines(up bool) {
rx, ry, _ := v.realPosition(v.Cursor())
if up {
if err := v.permutLines(ry-1, ry); err == nil {
v.MoveCursor(0, -1, true)
v.Actions.Exec(NewUpPermutCmd(v, rx, ry, -1))
}
} else {
if err := v.permutLines(ry+1, ry); err == nil {
v.MoveCursor(0, 1, true)
v.Actions.Exec(NewDownPermutCmd(v, rx, ry, +1))
}
}
}
// EditDelete deletes a rune at the cursor position. back determines the
// direction.
func (v *View) EditDelete(back bool) {
x, y := v.ox+v.cx, v.oy+v.cy
if y < 0 {
return
} else if y >= len(v.viewLines) {
v.MoveCursor(-1, 0, true)
return
}
rx, ry, _ := v.realPosition(v.cx, v.cy)
maxX, _ := v.Size()
if back {
if x == 0 { // start of the line
if y < 1 {
return
}
var maxPrevWidth int
if v.Wrap {
maxPrevWidth = maxX
} else {
maxPrevWidth = maxInt
}
if v.viewLines[y].linesX == 0 { // regular line
px, py := 0, 0
if ry > 0 {
py = ry - 1
px = len(v.lines[py])
}
if err := v.mergeLines(v.cy - 1); err == nil {
v.Actions.Exec(NewBackDelLineCmd(v, px, py))
}
if len(v.viewLines[y-1].line) < maxPrevWidth {
v.MoveCursor(-1, 0, true)
}
} else { // wrapped line
v.Actions.Exec(NewBackDeleteCmd(v, rx, ry, v.lines[ry][rx-1]))
v.deleteRune(len(v.viewLines[y-1].line)-1, v.cy-1)
v.MoveCursor(-1, 0, true)
}
} else { // middle/end of the line
v.Actions.Exec(NewBackDeleteCmd(v, rx, ry, v.lines[ry][rx-1]))
v.deleteRune(v.cx-1, v.cy)
v.MoveCursor(-1, 0, true)
}
} else {
if x == len(v.viewLines[y].line) { // end of the line
if err := v.mergeLines(v.cy); err == nil {
v.Actions.Exec(NewFwdDelLineCmd(v, rx, ry))
}
} else { // start/middle of the line
v.Actions.Exec(NewFwdDeleteCmd(v, rx, ry, v.lines[ry][rx]))
v.deleteRune(v.cx, v.cy)
}
}
}
// isEmpty checks if the view has no line yet
func (v *View) isEmpty() bool {
return v.lines == nil
}
// bol : beginning of line
func (v *View) bol() bool {
return v.ox == 0 && v.cx == 0
}
// bob : beginning of buffer
func (v *View) bob() bool {
return v.cx == 0
}
// eol : end of line
func (v *View) eol() bool {
rx, ry, err := v.realPosition(v.cx, v.cy)
if err != nil {
return false
}
return rx == len(v.lines[ry])
}
// eob : end of line in the buffer
// warning : lines in a buffer does not end with '\0' or '\n'
func (v *View) eob() bool {
return v.cx+v.ox == len(v.viewLines[v.oy].line)
}
// eov : end of view
func (v *View) eov() bool {
return v.cx+1 == v.x1-v.x0-1
}
// firstLine checks if the current cursor is placed in the first line of the file
func (v *View) firstLine() bool {
return v.cy+v.oy == 0
}
// lastLine checks if the current cursor is placed in the lastLine line of the file
func (v *View) lastLine() bool {
_, ry, err := v.realPosition(v.cx, v.cy)
if err != nil {
return false
}
return ry+1 == len(v.lines)
}
// firstBufferLine checks if the current cursor is placed in the first line of the buffer
func (v *View) firstBufferLine() bool {
return v.cy == 0
}
// lastBufferLine checks if the current cursor is placed in the lastLine line of the buffer
func (v *View) lastBufferLine() bool {
return v.cy+1 == v.y1-v.y0-1
}
// lastValidateLineInView checks if the current cursor is placed in the
// last validated line in the view. If the view is full, this corresponds to
// check if the cursor is placed in the last line of the view. If the the text
// does not use all the view, this corresponds to check if the cursor is placed
// in the last line representing the file
func (v *View) lastValidateLineInView() bool {
return v.cy == len(v.viewLines)
}
// adjustPositionToCurrentString move the cursor and the origin of the view given
// the length of the string to suit to.
func (v *View) adjustPositionToCurrentString() {
vx := v.ox + v.cx
vy := v.oy + v.cy
if vx < 0 || vy < 0 {
return
}
if len(v.viewLines) == 0 {
// return vx, vy, nil
return
}
if vy < len(v.viewLines) {
vline := v.viewLines[vy]
if vx > len(vline.line) {
v.goToEndOfLine(len(vline.line))
}
}
}
// goToEndOfLine will place the cursor at the end of the line given the length
// of the line to feet
func (v *View) goToEndOfLine(lineLength int) {
maxX, _ := v.Size()
if lineLength-v.ox < maxX {
if lineLength < v.ox {
v.ox = lineLength
}
v.cx = lineLength - v.ox
} else {
v.ox = lineLength - maxX + 1
v.cx = maxX
}
}
// getPreviousLineLength will return the length of the previous line. The current
// cursor's position need to be contain into the view area.
// Take into account wrap and side effect for the first line of the view
func (v *View) getPreviousLineLength() (prevLineWidth int) {
maxX, _ := v.Size()
if v.firstBufferLine() {
_, ry, err := v.realPosition(v.cx, v.cy)
if err != nil {
return
}
line := v.lines[ry]
prevLineWidth = len(line) % maxX
} else {
prevLineWidth = len(v.viewLines[v.oy+v.cy-1].line)
}
return
}
// moveOneRuneForward will move the cursor one character forward and adjust the
// origin of the view if necessary.
func (v *View) moveOneRuneForward(writeMode bool) {
if v.isEmpty() || (v.lastLine() && v.eol()) {
return
}
if v.eol() && writeMode {
v.cx++
} else if v.eol() && !writeMode || v.eov() && v.Wrap {
_, oy := v.Origin()
if v.lastBufferLine() {
v.SetOrigin(0, oy+1)
} else {
v.SetOrigin(0, oy)
}
if v.eov() && v.Wrap {
v.cx = 1
} else {
v.cx = 0
}
v.cy++
} else if v.eov() && !v.Wrap {
v.ox++
} else {
v.cx++
}
}
// moveOneRuneBackward will move the cursor one character backward and adjust the
// origin of the view if necessary
func (v *View) moveOneRuneBackward() {
if v.firstLine() && v.bol() {
return
}
if v.bol() {
if v.firstBufferLine() {
v.oy--
}
if v.cy != 0 {
v.cy--
}
vline := v.viewLines[v.oy+v.cy]
len := len(vline.line)
if v.Wrap {
v.cx = len
} else {
v.goToEndOfLine(len)
}
} else if v.bob() {
v.ox--
} else {
v.cx--
}
return
}
// moveOneRuneForward will move the cursor one line upper and adjust the
// origin of the view if necessary
func (v *View) moveOneLineUpper() {
if v.firstLine() {
return
}
if v.firstBufferLine() {
v.oy--
} else {
v.cy--
}
return
}
// moveOneRuneForward will move the cursor one line lower and adjust the
// origin of the view if necessary
func (v *View) moveOneLineLower(writeMode bool) {
if v.isEmpty() || v.lastLine() || (v.lastValidateLineInView() && !writeMode) {
return
}
if v.lastValidateLineInView() && writeMode {
v.oy++
} else if v.lastBufferLine() {
v.oy++
} else {
v.cy++
}
return
}
// MoveCursor moves the cursor taking into account the width of the line/view,
// displacing the origin if necessary.
func (v *View) MoveCursor(dx, dy int, writeMode bool) {
if dy < 0 {
for i := 0; i > dy; i-- {
v.moveOneLineUpper()
}
} else if dy > 0 {
for i := 0; i < dy; i++ {
v.moveOneLineLower(writeMode)
}
}
if !writeMode {
v.adjustPositionToCurrentString()
}
// Run through columns
if dx < 0 {
for i := 0; i > dx; i-- {
v.moveOneRuneBackward()
v.adjustPositionToCurrentString()
}
} else if dx > 0 {
for i := 0; i < dx; i++ {
v.moveOneRuneForward(writeMode)
}
}
}
// Moves the cursor from the beginning taking into account
// the width of the line/view, displacing the origin if necessary.
func (v *View) AbsMoveCursor(x, y int, overWrite bool) {
v.SetOrigin(0, 0)
v.SetCursor(0, 0)
v.MoveCursor(x, y, overWrite)
}