forked from reeflective/readline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
selection.go
243 lines (192 loc) · 5.57 KB
/
selection.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
package readline
import (
"regexp"
)
//
// Main selection helpers ------------------------------------------------------ //
//
// markSelection starts an active region at the specified mark position.
func (rl *Instance) markSelection(mark int) {
rl.mark = mark
rl.activeRegion = true
}
// Compute begin and end of region.
func (rl *Instance) getSelectionPos() (bpos, epos, cpos int) {
if rl.mark < rl.pos {
bpos = rl.mark
epos = rl.pos + 1
} else {
bpos = rl.pos
epos = rl.mark
}
// Here, compute for visual line mode if needed.
// We select the whole line.
if rl.visualLine {
bpos = 0
for i, char := range rl.line[epos:] {
if string(char) == "\n" {
break
}
epos = epos + i
}
}
// Ensure nothing is out of bounds
if epos > len(rl.line)-1 {
epos = len(rl.line)
}
if bpos < 0 {
bpos = 0
}
cpos = bpos
return
}
// popSelection returns the active region and resets it.
func (rl *Instance) popSelection() (s string, bpos, epos, cpos int) {
bpos, epos, cpos = rl.getSelectionPos()
s = string(rl.line[bpos:epos])
rl.resetSelection()
return
}
// insertBlock inserts a given string at the specified indexes, with
// an optional string to surround the block with. Everything before
// bpos and after epos is retained from the line, word inserted in the middle.
func (rl *Instance) insertBlock(bpos, epos int, word, surround string) {
if surround != "" {
word = surround + word + surround
}
begin := string(rl.line[:bpos])
end := string(rl.line[epos:])
newLine := append([]rune(begin), []rune(word)...)
newLine = append(newLine, []rune(end)...)
rl.line = newLine
}
// insertSelection works the same as insertBlock, but directly uses the
// current active region as a block to insert. Resets the selection once done.
// Returns the computed cursor position after insert.
func (rl *Instance) insertSelection(surround string) (wlen, cpos int) {
bpos, epos, cpos := rl.getSelectionPos()
selection := string(rl.line[bpos:epos])
if surround != "" {
selection = surround + selection + surround
}
begin := string(rl.line[:bpos])
end := string(rl.line[epos:])
newLine := append([]rune(begin), []rune(selection)...)
newLine = append(newLine, []rune(end)...)
rl.line = newLine
rl.resetSelection()
return len(selection), cpos
}
// yankSelection copies the active selection in the active/default register.
func (rl *Instance) yankSelection() {
// Get the selection.
bpos, epos, cpos := rl.getSelectionPos()
selection := string(rl.line[bpos:epos])
// The visual line mode always adds a newline
if rl.local == visual && rl.visualLine {
selection += "\n"
}
// And copy to active register
rl.saveBufToRegister([]rune(selection))
// and reset the cursor position if not in visual mode
if !rl.visualLine {
rl.pos = cpos
}
}
// yankSelection deletes the active selection.
func (rl *Instance) deleteSelection() {
var newline []rune
// Get the selection.
bpos, epos, cpos := rl.getSelectionPos()
selection := string(rl.line[bpos:epos])
// Save it and update the line
rl.saveBufToRegister([]rune(selection))
newline = append(rl.line[:bpos], rl.line[epos:]...)
rl.line = newline
rl.pos = cpos
// Reset the selection since it does not exist anymore.
rl.resetSelection()
}
// resetSelection unmarks the mark position and deactivates the region.
func (rl *Instance) resetSelection() {
rl.activeRegion = false
rl.mark = -1
}
//
// Selection search/modification helpers ----------------------------------------- //
//
// selectInWord returns the entire non-blank word around specified cursor position.
func (rl *Instance) selectInWord(cpos int) (bpos, epos int) {
pattern := "[0-9a-zA-Z_]"
bpos, epos = cpos, cpos
if match, _ := regexp.MatchString(pattern, string(rl.line[cpos])); !match {
pattern = "[^0-9a-zA-Z_ ]"
}
// To first space found backward
for ; bpos >= 0; bpos-- {
if match, _ := regexp.MatchString(pattern, string(rl.line[bpos])); !match {
break
}
}
// And to first space found forward
for ; epos < len(rl.line); epos++ {
if match, _ := regexp.MatchString(pattern, string(rl.line[epos])); !match {
break
}
}
bpos++
// Ending position must be greater than 0
if epos > 0 {
epos--
}
return
}
// searchSurround returns the index of the enclosing rune (either matching signs
// or the rune itself) of the input line, as well as each enclosing char.
func (rl *Instance) searchSurround(r rune) (bpos, epos int, bchar, echar rune) {
posInit := rl.pos
bchar, echar = rl.matchSurround(r)
bpos = rl.substrPos(bchar, false)
epos = rl.substrPos(echar, true)
if bpos == epos {
rl.pos++
epos = rl.substrPos(echar, true)
if epos == -1 {
rl.pos--
epos = rl.substrPos(echar, false)
if epos != -1 {
tmp := epos
epos = bpos
bpos = tmp
}
}
}
rl.pos = posInit
return
}
// adjustSurroundQuotes returns the correct mark and cursor positions when
// we want to know where a shell word enclosed with quotes (and potentially
// having inner ones) starts and ends.
func adjustSurroundQuotes(dBpos, dEpos, sBpos, sEpos int) (mark, cpos int) {
mark = -1
cpos = -1
if (sBpos == -1 || sEpos == -1) && (dBpos == -1 || dEpos == -1) {
return
}
doubleFirstAndValid := (dBpos < sBpos && // Outtermost
dBpos >= 0 && // Double found
sBpos >= 0 && // compared with a found single
dEpos > sEpos) // ensuring that we are not comparing unfound
singleFirstAndValid := (sBpos < dBpos &&
sBpos >= 0 &&
dBpos >= 0 &&
sEpos > dEpos)
if (sBpos == -1 || sEpos == -1) || doubleFirstAndValid {
mark = dBpos
cpos = dEpos
} else if (dBpos == -1 || dEpos == -1) || singleFirstAndValid {
mark = sBpos
cpos = sEpos
}
return
}