Skip to content

Commit

Permalink
implement ScrollTop event (key mapping: zt)
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Apr 29, 2018
1 parent 03ee911 commit de8947c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions editor/key.go
Expand Up @@ -34,6 +34,7 @@ func defaultKeyManagers() map[mode.Mode]*key.Manager {
km.Register(event.CursorEnd, "$")
km.Register(event.ScrollUp, "c-y")
km.Register(event.ScrollDown, "c-e")
km.Register(event.ScrollTop, "z", "t")
km.Register(event.PageUp, "c-b")
km.Register(event.PageDown, "c-f")
km.Register(event.PageUpHalf, "c-u")
Expand Down Expand Up @@ -153,6 +154,7 @@ func defaultKeyManagers() map[mode.Mode]*key.Manager {
km.Register(event.CursorEnd, "$")
km.Register(event.ScrollUp, "c-y")
km.Register(event.ScrollDown, "c-e")
km.Register(event.ScrollTop, "z", "t")
km.Register(event.PageUp, "c-b")
km.Register(event.PageDown, "c-f")
km.Register(event.PageUpHalf, "c-u")
Expand Down
1 change: 1 addition & 0 deletions event/event.go
Expand Up @@ -37,6 +37,7 @@ const (
CursorGoto
ScrollUp
ScrollDown
ScrollTop
PageUp
PageDown
PageUpHalf
Expand Down
15 changes: 15 additions & 0 deletions window/window.go
Expand Up @@ -113,6 +113,8 @@ func (w *window) run() {
w.scrollUp(e.Count)
case event.ScrollDown:
w.scrollDown(e.Count)
case event.ScrollTop:
w.scrollTop(e.Count)
case event.PageUp:
w.pageUp()
case event.PageDown:
Expand Down Expand Up @@ -469,6 +471,19 @@ func (w *window) scrollDown(count int64) {
}
}

func (w *window) scrollTop(count int64) {
if count > 0 {
w.cursor = mathutil.MinInt64(
mathutil.MinInt64(
count*w.width+w.cursor%w.width,
(mathutil.MaxInt64(w.length, 1)-1)/w.width*w.width+w.cursor%w.width,
),
mathutil.MaxInt64(w.length, 1)-1,
)
}
w.offset = w.cursor / w.width * w.width
}

func (w *window) pageUp() {
w.offset = mathutil.MaxInt64(w.offset-(w.height-2)*w.width, 0)
if w.offset == 0 {
Expand Down
19 changes: 19 additions & 0 deletions window/window_test.go
Expand Up @@ -477,6 +477,25 @@ func TestWindowScreenMotions(t *testing.T) {
if s.Offset != 0 {
t.Errorf("s.Offset should be %d but got %d", 0, s.Offset)
}

window.scrollTop(5)
s, _ = window.state(width, height)
if s.Cursor != 80 {
t.Errorf("s.Cursor should be %d but got %d", 80, s.Cursor)
}
if s.Offset != 80 {
t.Errorf("s.Offset should be %d but got %d", 80, s.Offset)
}

window.cursorDown(4)
window.scrollTop(0)
s, _ = window.state(width, height)
if s.Cursor != 144 {
t.Errorf("s.Cursor should be %d but got %d", 144, s.Cursor)
}
if s.Offset != 144 {
t.Errorf("s.Offset should be %d but got %d", 144, s.Offset)
}
}

func TestWindowDeleteBytes(t *testing.T) {
Expand Down

0 comments on commit de8947c

Please sign in to comment.