-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.go
554 lines (509 loc) · 13.9 KB
/
select.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
// Copyright (c) 2023, The GoKi 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 texteditor
import (
"goki.dev/gi/v2/gi"
"goki.dev/gi/v2/keyfun"
"goki.dev/gi/v2/texteditor/textbuf"
"goki.dev/girl/states"
"goki.dev/goosi/events"
"goki.dev/goosi/mimedata"
"goki.dev/icons"
"goki.dev/pi/v2/filecat"
"goki.dev/pi/v2/lex"
)
//////////////////////////////////////////////////////////
// Regions
// HighlightRegion creates a new highlighted region,
// triggers updating.
func (ed *Editor) HighlightRegion(reg textbuf.Region) {
ed.Highlights = []textbuf.Region{reg}
ed.SetNeedsRender()
}
// ClearHighlights clears the Highlights slice of all regions
func (ed *Editor) ClearHighlights() {
if len(ed.Highlights) == 0 {
return
}
ed.Highlights = ed.Highlights[:0]
ed.SetNeedsRender()
}
// ClearScopelights clears the Highlights slice of all regions
func (ed *Editor) ClearScopelights() {
if len(ed.Scopelights) == 0 {
return
}
updt := ed.UpdateStart()
defer ed.UpdateEndRender(updt)
sl := make([]textbuf.Region, len(ed.Scopelights))
copy(sl, ed.Scopelights)
ed.Scopelights = ed.Scopelights[:0]
}
//////////////////////////////////////////////////////////
// Selection
// ClearSelected resets both the global selected flag and any current selection
func (ed *Editor) ClearSelected() {
// ed.WidgetBase.ClearSelected()
ed.SelectReset()
}
// HasSelection returns whether there is a selected region of text
func (ed *Editor) HasSelection() bool {
if ed.SelectReg.Start.IsLess(ed.SelectReg.End) {
return true
}
return false
}
// Selection returns the currently selected text as a textbuf.Edit, which
// captures start, end, and full lines in between -- nil if no selection
func (ed *Editor) Selection() *textbuf.Edit {
if ed.HasSelection() {
return ed.Buf.Region(ed.SelectReg.Start, ed.SelectReg.End)
}
return nil
}
// SelectModeToggle toggles the SelectMode, updating selection with cursor movement
func (ed *Editor) SelectModeToggle() {
if ed.SelectMode {
ed.SelectMode = false
} else {
ed.SelectMode = true
ed.SelectStart = ed.CursorPos
ed.SelectRegUpdate(ed.CursorPos)
}
ed.SavePosHistory(ed.CursorPos)
}
// SelectAll selects all the text
func (ed *Editor) SelectAll() {
updt := ed.UpdateStart()
defer ed.UpdateEndRender(updt)
ed.SelectReg.Start = lex.PosZero
ed.SelectReg.End = ed.Buf.EndPos()
}
// WordBefore returns the word before the lex.Pos
// uses IsWordBreak to determine the bounds of the word
func (ed *Editor) WordBefore(tp lex.Pos) *textbuf.Edit {
txt := ed.Buf.Line(tp.Ln)
ch := tp.Ch
ch = min(ch, len(txt))
st := ch
for i := ch - 1; i >= 0; i-- {
if i == 0 { // start of line
st = 0
break
}
r1 := txt[i]
r2 := txt[i-1]
if lex.IsWordBreak(r1, r2) {
st = i + 1
break
}
}
if st != ch {
return ed.Buf.Region(lex.Pos{Ln: tp.Ln, Ch: st}, tp)
}
return nil
}
// IsWordStart returns true if the cursor is just before the start of a word
// word is a string of characters none of which are classified as a word break
func (ed *Editor) IsWordStart(tp lex.Pos) bool {
txt := ed.Buf.Line(ed.CursorPos.Ln)
sz := len(txt)
if sz == 0 {
return false
}
if tp.Ch >= len(txt) { // end of line
return false
}
if tp.Ch == 0 { // start of line
r := txt[0]
if lex.IsWordBreak(r, rune(-1)) {
return false
}
return true
}
r1 := txt[tp.Ch-1]
r2 := txt[tp.Ch]
if lex.IsWordBreak(r1, rune(-1)) && !lex.IsWordBreak(r2, rune(-1)) {
return true
}
return false
}
// IsWordEnd returns true if the cursor is just past the last letter of a word
// word is a string of characters none of which are classified as a word break
func (ed *Editor) IsWordEnd(tp lex.Pos) bool {
txt := ed.Buf.Line(ed.CursorPos.Ln)
sz := len(txt)
if sz == 0 {
return false
}
if tp.Ch >= len(txt) { // end of line
r := txt[len(txt)-1]
if lex.IsWordBreak(r, rune(-1)) {
return true
}
return false
}
if tp.Ch == 0 { // start of line
r := txt[0]
if lex.IsWordBreak(r, rune(-1)) {
return false
}
return true
}
r1 := txt[tp.Ch-1]
r2 := txt[tp.Ch]
if !lex.IsWordBreak(r1, rune(-1)) && lex.IsWordBreak(r2, rune(-1)) {
return true
}
return false
}
// IsWordMiddle - returns true if the cursor is anywhere inside a word,
// i.e. the character before the cursor and the one after the cursor
// are not classified as word break characters
func (ed *Editor) IsWordMiddle(tp lex.Pos) bool {
txt := ed.Buf.Line(ed.CursorPos.Ln)
sz := len(txt)
if sz < 2 {
return false
}
if tp.Ch >= len(txt) { // end of line
return false
}
if tp.Ch == 0 { // start of line
return false
}
r1 := txt[tp.Ch-1]
r2 := txt[tp.Ch]
if !lex.IsWordBreak(r1, rune(-1)) && !lex.IsWordBreak(r2, rune(-1)) {
return true
}
return false
}
// SelectWord selects the word (whitespace, punctuation delimited) that the cursor is on
// returns true if word selected
func (ed *Editor) SelectWord() bool {
txt := ed.Buf.Line(ed.CursorPos.Ln)
sz := len(txt)
if sz == 0 {
return false
}
reg := ed.WordAt()
ed.SelectReg = reg
ed.SelectStart = ed.SelectReg.Start
return true
}
// WordAt finds the region of the word at the current cursor position
func (ed *Editor) WordAt() (reg textbuf.Region) {
reg.Start = ed.CursorPos
reg.End = ed.CursorPos
txt := ed.Buf.Line(ed.CursorPos.Ln)
sz := len(txt)
if sz == 0 {
return reg
}
sch := min(ed.CursorPos.Ch, sz-1)
if !lex.IsWordBreak(txt[sch], rune(-1)) {
for sch > 0 {
r2 := rune(-1)
if sch-2 >= 0 {
r2 = txt[sch-2]
}
if lex.IsWordBreak(txt[sch-1], r2) {
break
}
sch--
}
reg.Start.Ch = sch
ech := ed.CursorPos.Ch + 1
for ech < sz {
r2 := rune(-1)
if ech < sz-1 {
r2 = rune(txt[ech+1])
}
if lex.IsWordBreak(txt[ech], r2) {
break
}
ech++
}
reg.End.Ch = ech
} else { // keep the space start -- go to next space..
ech := ed.CursorPos.Ch + 1
for ech < sz {
if !lex.IsWordBreak(txt[ech], rune(-1)) {
break
}
ech++
}
for ech < sz {
r2 := rune(-1)
if ech < sz-1 {
r2 = rune(txt[ech+1])
}
if lex.IsWordBreak(txt[ech], r2) {
break
}
ech++
}
reg.End.Ch = ech
}
return reg
}
// SelectReset resets the selection
func (ed *Editor) SelectReset() {
ed.SelectMode = false
if !ed.HasSelection() {
return
}
ed.SelectReg = textbuf.RegionNil
ed.PrevSelectReg = textbuf.RegionNil
}
// RenderSelectLines renders the lines within the current selection region
func (ed *Editor) RenderSelectLines() {
ed.PrevSelectReg = ed.SelectReg
}
///////////////////////////////////////////////////////////////////////////////
// Cut / Copy / Paste
// ViewClipHistory is the text view clipboard history -- everything that has been copied
var ViewClipHistory [][]byte
// Maximum amount of clipboard history to retain
var ViewClipHistMax = 100
// ViewClipHistAdd adds the given clipboard bytes to top of history stack
func ViewClipHistAdd(clip []byte) {
max := ViewClipHistMax
if ViewClipHistory == nil {
ViewClipHistory = make([][]byte, 0, max)
}
ch := &ViewClipHistory
sz := len(*ch)
if sz > max {
*ch = (*ch)[:max]
}
if sz >= max {
copy((*ch)[1:max], (*ch)[0:max-1])
(*ch)[0] = clip
} else {
*ch = append(*ch, nil)
if sz > 0 {
copy((*ch)[1:], (*ch)[0:sz])
}
(*ch)[0] = clip
}
}
// ViewClipHistChooseLen is the max length of clip history to show in chooser
var ViewClipHistChooseLen = 40
// ViewClipHistChooseList returns a string slice of length-limited clip history, for chooser
func ViewClipHistChooseList() []string {
cl := make([]string, len(ViewClipHistory))
for i, hc := range ViewClipHistory {
szl := len(hc)
if szl > ViewClipHistChooseLen {
cl[i] = string(hc[:ViewClipHistChooseLen])
} else {
cl[i] = string(hc)
}
}
return cl
}
// PasteHist presents a chooser of clip history items, pastes into text if selected
func (ed *Editor) PasteHist() {
if ViewClipHistory == nil {
return
}
// TODO(kai/menu): StringsChooserPopup
/*
cl := ViewClipHistChooseList()
gi.StringsChooserPopup(cl, "", ed, func(ac *gi.Button) {
idx := ac.Data.(int)
clip := ViewClipHistory[idx]
if clip != nil {
updt := ed.UpdateStart()
defer ed.UpdateEndRender(updt)
ed.EventMgr().ClipBoard().Write(mimedata.NewTextBytes(clip))
ed.InsertAtCursor(clip)
ed.SavePosHistory(ed.CursorPos)
}
})
*/
}
// Cut cuts any selected text and adds it to the clipboard, also returns cut text
func (ed *Editor) Cut() *textbuf.Edit {
if !ed.HasSelection() {
return nil
}
updt := ed.UpdateStart()
defer ed.UpdateEndRender(updt)
org := ed.SelectReg.Start
cut := ed.DeleteSelection()
if cut != nil {
cb := cut.ToBytes()
ed.EventMgr().ClipBoard().Write(mimedata.NewTextBytes(cb))
ViewClipHistAdd(cb)
}
ed.SetCursorShow(org)
ed.SavePosHistory(ed.CursorPos)
return cut
}
// DeleteSelection deletes any selected text, without adding to clipboard --
// returns text deleted as textbuf.Edit (nil if none)
func (ed *Editor) DeleteSelection() *textbuf.Edit {
tbe := ed.Buf.DeleteText(ed.SelectReg.Start, ed.SelectReg.End, EditSignal)
ed.SelectReset()
return tbe
}
// Copy copies any selected text to the clipboard, and returns that text,
// optionally resetting the current selection
func (ed *Editor) Copy(reset bool) *textbuf.Edit {
tbe := ed.Selection()
if tbe == nil {
return nil
}
updt := ed.UpdateStart()
defer ed.UpdateEndRender(updt)
cb := tbe.ToBytes()
ViewClipHistAdd(cb)
ed.EventMgr().ClipBoard().Write(mimedata.NewTextBytes(cb))
if reset {
ed.SelectReset()
}
ed.SavePosHistory(ed.CursorPos)
return tbe
}
// Paste inserts text from the clipboard at current cursor position
func (ed *Editor) Paste() {
updt := ed.UpdateStart()
defer ed.UpdateEndRender(updt)
data := ed.EventMgr().ClipBoard().Read([]string{filecat.TextPlain})
if data != nil {
ed.InsertAtCursor(data.TypeData(filecat.TextPlain))
ed.SavePosHistory(ed.CursorPos)
}
}
// InsertAtCursor inserts given text at current cursor position
func (ed *Editor) InsertAtCursor(txt []byte) {
updt := ed.UpdateStart()
defer ed.UpdateEndRender(updt)
if ed.HasSelection() {
tbe := ed.DeleteSelection()
ed.CursorPos = tbe.AdjustPos(ed.CursorPos, textbuf.AdjustPosDelStart) // move to start if in reg
}
tbe := ed.Buf.InsertText(ed.CursorPos, txt, EditSignal)
if tbe == nil {
return
}
pos := tbe.Reg.End
if len(txt) == 1 && txt[0] == '\n' {
pos.Ch = 0 // sometimes it doesn't go to the start..
}
ed.SetCursorShow(pos)
ed.SetCursorCol(ed.CursorPos)
}
///////////////////////////////////////////////////////////
// Rectangular regions
// ViewClipRect is the internal clipboard for Rect rectangle-based
// regions -- the raw text is posted on the system clipboard but the
// rect information is in a special format.
var ViewClipRect *textbuf.Edit
// CutRect cuts rectangle defined by selected text (upper left to lower right)
// and adds it to the clipboard, also returns cut text.
func (ed *Editor) CutRect() *textbuf.Edit {
if !ed.HasSelection() {
return nil
}
updt := ed.UpdateStart()
defer ed.UpdateEndRender(updt)
npos := lex.Pos{Ln: ed.SelectReg.End.Ln, Ch: ed.SelectReg.Start.Ch}
cut := ed.Buf.DeleteTextRect(ed.SelectReg.Start, ed.SelectReg.End, EditSignal)
if cut != nil {
cb := cut.ToBytes()
ed.EventMgr().ClipBoard().Write(mimedata.NewTextBytes(cb))
ViewClipRect = cut
}
ed.SetCursorShow(npos)
ed.SavePosHistory(ed.CursorPos)
return cut
}
// CopyRect copies any selected text to the clipboard, and returns that text,
// optionally resetting the current selection
func (ed *Editor) CopyRect(reset bool) *textbuf.Edit {
tbe := ed.Buf.RegionRect(ed.SelectReg.Start, ed.SelectReg.End)
if tbe == nil {
return nil
}
updt := ed.UpdateStart()
defer ed.UpdateEndRender(updt)
cb := tbe.ToBytes()
ed.EventMgr().ClipBoard().Write(mimedata.NewTextBytes(cb))
ViewClipRect = tbe
if reset {
ed.SelectReset()
}
ed.SavePosHistory(ed.CursorPos)
return tbe
}
// PasteRect inserts text from the clipboard at current cursor position
func (ed *Editor) PasteRect() {
if ViewClipRect == nil {
return
}
updt := ed.UpdateStart()
defer ed.UpdateEndRender(updt)
ce := ViewClipRect.Clone()
nl := ce.Reg.End.Ln - ce.Reg.Start.Ln
nch := ce.Reg.End.Ch - ce.Reg.Start.Ch
ce.Reg.Start.Ln = ed.CursorPos.Ln
ce.Reg.End.Ln = ed.CursorPos.Ln + nl
ce.Reg.Start.Ch = ed.CursorPos.Ch
ce.Reg.End.Ch = ed.CursorPos.Ch + nch
tbe := ed.Buf.InsertTextRect(ce, EditSignal)
pos := tbe.Reg.End
ed.SetCursorShow(pos)
ed.SetCursorCol(ed.CursorPos)
ed.SavePosHistory(ed.CursorPos)
}
// ReCaseSelection changes the case of the currently-selected text.
// Returns the new text -- empty if nothing selected.
func (ed *Editor) ReCaseSelection(c textbuf.Cases) string {
if !ed.HasSelection() {
return ""
}
sel := ed.Selection()
nstr := textbuf.ReCaseString(string(sel.ToBytes()), c)
ed.Buf.ReplaceText(sel.Reg.Start, sel.Reg.End, sel.Reg.Start, nstr, EditSignal, ReplaceNoMatchCase)
return nstr
}
///////////////////////////////////////////////////////////
// Context Menu
// ShowContextMenu displays the context menu with options dependent on situation
func (ed *Editor) ShowContextMenu(e events.Event) {
if !ed.HasSelection() && ed.Buf.IsSpellEnabled(ed.CursorPos) {
if ed.Buf.Spell != nil {
if ed.OfferCorrect() {
return
}
}
}
ed.WidgetBase.ShowContextMenu(e)
}
// ContextMenu builds the text editor context menu
func (ed *Editor) ContextMenu(m *gi.Scene) {
gi.NewButton(m).SetText("Copy").SetIcon(icons.ContentCopy).SetKey(keyfun.Copy).SetState(!ed.HasSelection(), states.Disabled).
OnClick(func(e events.Event) {
ed.Copy(true)
})
if !ed.IsReadOnly() {
gi.NewButton(m).SetText("Cut").SetIcon(icons.ContentCopy).SetKey(keyfun.Cut).SetState(!ed.HasSelection(), states.Disabled).
OnClick(func(e events.Event) {
ed.Cut()
})
gi.NewButton(m).SetText("Paste").SetIcon(icons.ContentPaste).SetKey(keyfun.Paste).SetState(ed.EventMgr().ClipBoard().IsEmpty(), states.Disabled).
OnClick(func(e events.Event) {
ed.Paste()
})
} else {
gi.NewButton(m).SetText("Clear").SetIcon(icons.ClearAll).
OnClick(func(e events.Event) {
ed.Clear()
})
}
}