-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.go
65 lines (54 loc) · 1.25 KB
/
text.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
package nyne
import (
"log"
"unicode/utf8"
)
// WinFunc retrieves the Win by its ID
type WinFunc func(int) (*Win, error)
// TabwidthFunc returns the tabwidth based on properties of the Event
type TabwidthFunc func(Event) int
// Tab constructs a tab character or the equivalent width of spaces
// depending on if expand is set
func Tab(width int, expand bool) []byte {
if expand {
var tab []byte
for i := 0; i < width; i++ {
tab = append(tab, ' ')
}
return tab
}
return []byte{0x09}
}
// Tabexpand expands tabs to spaces
func Tabexpand(condition Condition, win WinFunc, tabwidth TabwidthFunc) (rune, Handler) {
return '\t', func(e Event) (Event, bool) {
ok := true
if !condition(e) {
return e, ok
}
w, err := win(e.ID)
if err != nil {
log.Println(err)
return e, ok
}
tab := Tab(tabwidth(e), true)
// select current character
err = w.SetAddr("#%d;+#1", e.SelBegin)
if err != nil {
log.Println(err)
w.WriteEvent(e)
}
// replace character with tab
w.SetData(tab)
// update the event to reflect the change
rc := utf8.RuneCount(tab)
selEnd := e.SelBegin + rc
e.Origin = WindowFiles
e.Action = BodyInsert
e.SelEnd = selEnd
e.OrigSelEnd = selEnd
e.NumRunes = rc
e.Text = Text(tab)
return e, ok
}
}