Skip to content

Commit ae12781

Browse files
authored
fix panic in Transpose (#70)
Fixes #69
1 parent 7b16f76 commit ae12781

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

runebuf.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@ func (r *runeBuffer) Len() int {
8787

8888
func (r *runeBuffer) MoveToLineStart() {
8989
r.Refresh(func() {
90-
if r.idx == 0 {
91-
return
92-
}
9390
r.idx = 0
9491
})
9592
}
@@ -248,21 +245,23 @@ func (r *runeBuffer) Kill() {
248245

249246
func (r *runeBuffer) Transpose() {
250247
r.Refresh(func() {
251-
if len(r.buf) == 1 {
252-
r.idx++
253-
}
254-
255-
if len(r.buf) < 2 {
248+
if r.idx == 0 {
249+
// match the GNU Readline behavior, Ctrl-T at the start of the line
250+
// is a no-op:
256251
return
257252
}
258253

259-
if r.idx == 0 {
260-
r.idx = 1
261-
} else if r.idx >= len(r.buf) {
262-
r.idx = len(r.buf) - 1
254+
// OK, we have at least one character behind us:
255+
if r.idx < len(r.buf) {
256+
// swap the character in front of us with the one behind us
257+
r.buf[r.idx], r.buf[r.idx-1] = r.buf[r.idx-1], r.buf[r.idx]
258+
// advance the cursor
259+
r.idx++
260+
} else if r.idx == len(r.buf) && len(r.buf) >= 2 {
261+
// swap the two characters behind us
262+
r.buf[r.idx-2], r.buf[r.idx-1] = r.buf[r.idx-1], r.buf[r.idx-2]
263+
// leave the cursor in place since there's nowhere to go
263264
}
264-
r.buf[r.idx], r.buf[r.idx-1] = r.buf[r.idx-1], r.buf[r.idx]
265-
r.idx++
266265
})
267266
}
268267

0 commit comments

Comments
 (0)