-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.go
352 lines (306 loc) · 7.48 KB
/
window.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
package edit
import (
"errors"
"fmt"
"log"
"math"
)
// A Window is a view to a buffer. It maintains a cursor position, and a
// rectangle view into the buffer.
type Window struct {
buffer Buffer
l, c int // line and column of the cursor
topLine, leftCol int // Index of the topmost visible line, column of the leftmost visibile column
tabSize int
width, height int
eventHandler *EventHandler
app *App
copyStartL, copyStartC int
copyEndL, copyEndC int
regionFirstL, regionFirstC int
regionLastL, regionLastC int
}
func NewWindow(buf Buffer) *Window {
return &Window{
buffer: buf,
tabSize: 4,
copyStartL: -1,
copyStartC: -1,
copyEndL: -1,
copyEndC: -1,
}
}
//
// General
//
func (w *Window) RegisterWithApp(app *App) {
w.eventHandler = app.GetEventHandler(w.buffer.Kind())
w.app = app
}
func (w *Window) App() *App {
return w.app
}
func (w *Window) HandleEvent(evt Event) (Action, error) {
if evt.EventType == Key || evt.EventType == Rune {
w.ResetHighlightRegion()
}
return w.eventHandler.HandleEvent(evt)
}
//
// Movement methods
//
// MoveCursor moves the cursor by a number of lines and columns.
func (w *Window) MoveCursor(dl, dc int) {
w.l, w.c = w.buffer.AdvancePos(w.l, w.c, dl, dc)
}
// MoveCursorTo moves the cursor to a given screen position.
func (w *Window) MoveCursorTo(x, y int) {
w.l, w.c = w.GetLineCol(x, y)
}
func (w *Window) GetLineCol(x, y int) (int, int) {
l := y + w.topLine
if l >= w.buffer.LineCount() {
l = w.buffer.LineCount() - 1
}
line, _ := w.buffer.GetLine(l, 0)
return w.buffer.AdvancePos(l, w.getPrinter().LineIndex(line, x), 0, 0)
}
// MoveCursorToLineStart moves the cursor to the start of the current line.
func (w *Window) MoveCursorToLineStart() {
w.l, w.c = w.buffer.AdvancePos(w.l, 0, 0, 0)
}
// MoveCursorToLineEnd moves the cursor to the end of the current line.
func (w *Window) MoveCursorToLineEnd() {
line, err := w.buffer.GetLine(w.l, w.c)
if err == nil {
w.l, w.c = w.buffer.AdvancePos(w.l, line.Len(), 0, 0)
}
}
func (w *Window) MoveCursorToEnd() {
l, c := w.buffer.EndPos()
w.l, w.c = w.buffer.AdvancePos(l, c, 0, 0)
}
// PageDown moves the cursor down by n pages (or up by -n pages if n < 0).
func (w *Window) PageDown(n int) {
if n == 0 {
return
}
lineOffset := (w.height - 2) + (n-1)*w.height
w.MoveCursor(lineOffset, 0)
}
// ScrollDown scrolls down by n lines, attempting to keep the cursor on the same
// buffer line.
func (w *Window) ScrollDown(n int) {
if n <= 0 {
return
}
w.topLine += n
maxTopLine := w.buffer.LineCount() - 1
if w.topLine > maxTopLine {
w.topLine = maxTopLine
}
dl := w.l - w.topLine
if dl < 0 {
w.MoveCursor(-dl, 0)
}
}
// ScrollUp scrolls up by n lines, attempting to keep the cursor on the same
// buffer line.
func (w *Window) ScrollUp(n int) {
if n <= 0 {
return
}
w.topLine -= n
if w.topLine < 0 {
w.topLine = 0
}
dl := w.topLine + w.height - 1 - w.l
if dl < 0 {
w.MoveCursor(dl, 0)
}
}
func (w *Window) StartHighlightRegion(x, y int) {
w.copyStartL, w.copyStartC = w.GetLineCol(x, y)
w.copyEndL, w.copyEndC = -1, -1
}
func (w *Window) MoveHighlightRegion(x, y int) {
w.copyEndL, w.copyEndC = w.GetLineCol(x, y)
}
func (w *Window) StopHightlightRegion(x, y int) bool {
w.copyEndL, w.copyEndC = w.GetLineCol(x, y)
if w.copyEndC == w.copyStartC && w.copyEndL == w.copyStartL {
w.ResetHighlightRegion()
return false
}
return true
}
func (w *Window) ResetHighlightRegion() {
w.copyStartL, w.copyStartC = -1, -1
w.copyEndL, w.copyEndC = -1, -1
}
func (w *Window) GetHighlightedString() (string, error) {
if w.copyEndC == -1 {
return "", fmt.Errorf("no highlight region")
}
return w.buffer.StringFromRegion(w.copyStartL, w.copyStartC, w.copyEndL, w.copyEndC)
}
func (w *Window) PasteString(s string) (err error) {
w.l, w.c, err = w.buffer.InsertString(s, w.l, w.c)
return
}
//
// Editing methods
//
// InsertRune inserts a character into the buffer at the cursor position.
func (w *Window) InsertRune(r rune) {
err := w.buffer.InsertRune(r, w.l, w.c)
if err != nil {
log.Printf("error inserting rune: %s", err)
return
}
w.c++
}
// DeleteRune deletes the character to the left of the cursor position.
func (w *Window) DeleteRune() (err error) {
if w.l == 0 && w.c == 0 {
return errors.New("start of buffer")
}
l, c := w.buffer.AdvancePos(w.l, w.c, 0, -1)
if l == w.l {
err = w.buffer.DeleteRuneAt(l, c)
} else {
err = w.buffer.MergeLineWithPrevious(w.l)
}
w.c = c
w.l = l
return
}
// SplitLine splits the current line at the cursor position. If move is true,
// the cursor is moved down otherwise it stays in the same position.
func (w *Window) SplitLine(move bool) error {
err := w.buffer.SplitLine(w.l, w.c)
if err != nil {
log.Printf("error splitting line: %s", err)
return err
}
if move {
w.l, w.c = w.buffer.AdvancePos(w.l+1, 0, 0, 0)
}
return nil
}
//
// Buffer inspection methods
//
// CurrentLine returns the line the cursor is on currently.
func (w *Window) CurrentLine() (Line, error) {
return w.buffer.GetLine(w.l, w.c)
}
func (w *Window) Buffer() Buffer {
return w.buffer
}
func (w *Window) CursorLine() int {
return w.l
}
func (w *Window) CursorPos() (int, int) {
return w.l, w.c
}
//
// Drawing methods
//
// Resize changes the size of the window.
func (w *Window) Resize(width, height int) {
w.width = width
w.height = height
}
// Draw draws the contents of the window on the screen.
func (w *Window) Draw(screen ScreenWriter) {
w.orderRegion()
sh := screen.Size().H
linesAvail := w.buffer.LineCount() - w.topLine
if linesAvail <= 0 {
return
}
if sh > linesAvail {
sh = linesAvail
}
lp := w.getPrinter()
for p := (Position{}); p.Y < sh; p.Y++ {
lp.Print(screen, p, w.StyledLineIter(p.Y+w.topLine, 0))
}
}
// DrawCursor highlights the cursor if it is visible.
func (w *Window) DrawCursor(screen ScreenWriter) {
line, _ := w.buffer.GetLine(w.l, w.c)
screen.Reverse(Position{
X: w.getPrinter().LineCol(line, w.c),
Y: w.l - w.topLine,
})
}
// FocusCursor adjusts the visible rectangle of the window if necessary to make
// the cursor visible.
func (w *Window) FocusCursor(screen ScreenWriter) {
sz := screen.Size()
line, _ := w.buffer.GetLine(w.l, w.c)
x := w.getPrinter().LineCol(line, w.c)
if x < 0 {
w.leftCol += x
} else if x >= sz.W {
w.leftCol += x - sz.W + 1
}
y := w.l - w.topLine
if y < 0 {
w.topLine = w.l
} else if y >= sz.H {
w.topLine = w.l - sz.H + 1
}
}
func (w *Window) getPrinter() Printer {
return Printer{
TabWidth: w.tabSize,
Offset: w.leftCol,
}
}
func (w *Window) orderRegion() {
l0, c0 := w.copyStartL, w.copyStartC
l1, c1 := w.copyEndL, w.copyEndC
if l1 < l0 || (l0 == l1 && c1 < c0) {
l0, c0, l1, c1 = l1, c1, l0, c0
}
w.regionFirstL, w.regionFirstC = l0, c0
w.regionLastL, w.regionLastC = l1, c1
}
func (w *Window) StyledLineIter(l, c int) StyledLineIter {
iter := w.buffer.StyledLineIter(l, c)
if w.copyEndL >= 0 && l >= w.regionFirstL && l <= w.regionLastL {
c1, c2 := 0, math.MaxInt
if l == w.regionFirstL {
c1 = w.regionFirstC - c
}
if l == w.regionLastL {
c2 = w.regionLastC - c
}
iter = &highlightIter{
iter: iter,
c1: c1,
c2: c2,
}
}
return iter
}
type highlightIter struct {
iter StyledLineIter
c1, c2 int
}
var _ StyledLineIter = (*highlightIter)(nil)
func (i *highlightIter) Next() (rune, Style) {
r, s := i.iter.Next()
if i.c1 <= 0 && i.c2 >= 0 {
s = s.Reverse(true)
}
i.c1--
i.c2--
return r, s
}
func (i *highlightIter) HasNext() bool {
return i.iter.HasNext()
}