Add barebones support for yank/paste (#120)

This commit is contained in:
Sagar Mittal 2017-10-01 20:57:50 -07:00 committed by chzyer
parent 045fff973b
commit 9f56defe66
4 changed files with 31 additions and 0 deletions

View File

@ -226,6 +226,8 @@ func (o *Operation) ioloop() {
o.Refresh() o.Refresh()
case MetaBackspace, CharCtrlW: case MetaBackspace, CharCtrlW:
o.buf.BackEscapeWord() o.buf.BackEscapeWord()
case CharCtrlY:
o.buf.Yank()
case CharEnter, CharCtrlJ: case CharEnter, CharCtrlJ:
if o.IsSearchMode() { if o.IsSearchMode() {
o.ExitSearchMode(false) o.ExitSearchMode(false)

View File

@ -29,9 +29,15 @@ type RuneBuffer struct {
offset string offset string
lastKill []rune
sync.Mutex sync.Mutex
} }
func (r* RuneBuffer) pushKill(text []rune) {
r.lastKill = append([]rune{}, text...)
}
func (r *RuneBuffer) OnWidthChange(newWidth int) { func (r *RuneBuffer) OnWidthChange(newWidth int) {
r.Lock() r.Lock()
r.width = newWidth r.width = newWidth
@ -187,6 +193,7 @@ func (r *RuneBuffer) Replace(ch rune) {
func (r *RuneBuffer) Erase() { func (r *RuneBuffer) Erase() {
r.Refresh(func() { r.Refresh(func() {
r.idx = 0 r.idx = 0
r.pushKill(r.buf[:])
r.buf = r.buf[:0] r.buf = r.buf[:0]
}) })
} }
@ -196,6 +203,7 @@ func (r *RuneBuffer) Delete() (success bool) {
if r.idx == len(r.buf) { if r.idx == len(r.buf) {
return return
} }
r.pushKill(r.buf[r.idx : r.idx+1])
r.buf = append(r.buf[:r.idx], r.buf[r.idx+1:]...) r.buf = append(r.buf[:r.idx], r.buf[r.idx+1:]...)
success = true success = true
}) })
@ -212,6 +220,7 @@ func (r *RuneBuffer) DeleteWord() {
} }
for i := init + 1; i < len(r.buf); i++ { for i := init + 1; i < len(r.buf); i++ {
if !IsWordBreak(r.buf[i]) && IsWordBreak(r.buf[i-1]) { if !IsWordBreak(r.buf[i]) && IsWordBreak(r.buf[i-1]) {
r.pushKill(r.buf[r.idx:i-1])
r.Refresh(func() { r.Refresh(func() {
r.buf = append(r.buf[:r.idx], r.buf[i-1:]...) r.buf = append(r.buf[:r.idx], r.buf[i-1:]...)
}) })
@ -247,6 +256,7 @@ func (r *RuneBuffer) KillFront() {
} }
length := len(r.buf) - r.idx length := len(r.buf) - r.idx
r.pushKill(r.buf[:r.idx])
copy(r.buf[:length], r.buf[r.idx:]) copy(r.buf[:length], r.buf[r.idx:])
r.idx = 0 r.idx = 0
r.buf = r.buf[:length] r.buf = r.buf[:length]
@ -255,6 +265,7 @@ func (r *RuneBuffer) KillFront() {
func (r *RuneBuffer) Kill() { func (r *RuneBuffer) Kill() {
r.Refresh(func() { r.Refresh(func() {
r.pushKill(r.buf[r.idx:])
r.buf = r.buf[:r.idx] r.buf = r.buf[:r.idx]
}) })
} }
@ -321,6 +332,7 @@ func (r *RuneBuffer) BackEscapeWord() {
} }
for i := r.idx - 1; i > 0; i-- { for i := r.idx - 1; i > 0; i-- {
if !IsWordBreak(r.buf[i]) && IsWordBreak(r.buf[i-1]) { if !IsWordBreak(r.buf[i]) && IsWordBreak(r.buf[i-1]) {
r.pushKill(r.buf[i:r.idx])
r.buf = append(r.buf[:i], r.buf[r.idx:]...) r.buf = append(r.buf[:i], r.buf[r.idx:]...)
r.idx = i r.idx = i
return return
@ -332,6 +344,20 @@ func (r *RuneBuffer) BackEscapeWord() {
}) })
} }
func (r *RuneBuffer) Yank() {
if len(r.lastKill) == 0 {
return
}
r.Refresh(func() {
buf := make([]rune, 0, len(r.buf) + len(r.lastKill))
buf = append(buf, r.buf[:r.idx]...)
buf = append(buf, r.lastKill...)
buf = append(buf, r.buf[r.idx:]...)
r.buf = buf
r.idx += len(r.lastKill)
})
}
func (r *RuneBuffer) Backspace() { func (r *RuneBuffer) Backspace() {
r.Refresh(func() { r.Refresh(func() {
if r.idx == 0 { if r.idx == 0 {

View File

@ -38,6 +38,7 @@ const (
CharTranspose = 20 CharTranspose = 20
CharCtrlU = 21 CharCtrlU = 21
CharCtrlW = 23 CharCtrlW = 23
CharCtrlY = 25
CharCtrlZ = 26 CharCtrlZ = 26
CharEsc = 27 CharEsc = 27
CharEscapeEx = 91 CharEscapeEx = 91

2
vim.go
View File

@ -72,6 +72,8 @@ func (o *opVim) handleVimNormalMovement(r rune, readNext func() rune) (t rune, h
case 'l': case 'l':
rb.Delete() rb.Delete()
} }
case 'p':
rb.Yank()
case 'b', 'B': case 'b', 'B':
rb.MoveToPrevWord() rb.MoveToPrevWord()
case 'w', 'W': case 'w', 'W':