Skip to content

Commit b3227d6

Browse files
authored
add actions: CursorToViewTop, CursorToViewCenter, CursorToViewBottom (zyedidia#3506)
1 parent 2c6dc32 commit b3227d6

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

internal/action/actions.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,52 @@ func (h *BufPane) Center() bool {
160160
return true
161161
}
162162

163+
// CursorToViewTop moves the cursor to the top of the view,
164+
// offset by scrollmargin unless at the beginning or end of the file
165+
func (h *BufPane) CursorToViewTop() bool {
166+
v := h.GetView()
167+
h.Buf.ClearCursors()
168+
scrollmargin := int(h.Buf.Settings["scrollmargin"].(float64))
169+
bStart := display.SLoc{0, 0}
170+
if v.StartLine == bStart {
171+
scrollmargin = 0
172+
}
173+
h.Cursor.GotoLoc(h.LocFromVLoc(display.VLoc{
174+
SLoc: h.Scroll(v.StartLine, scrollmargin),
175+
VisualX: 0,
176+
}))
177+
return true
178+
}
179+
180+
// CursorToViewCenter moves the cursor to the center of the view
181+
func (h *BufPane) CursorToViewCenter() bool {
182+
v := h.GetView()
183+
h.Buf.ClearCursors()
184+
h.Cursor.GotoLoc(h.LocFromVLoc(display.VLoc{
185+
SLoc: h.Scroll(v.StartLine, h.BufView().Height/2),
186+
VisualX: 0,
187+
}))
188+
return true
189+
}
190+
191+
// CursorToViewBottom moves the cursor to the bottom of the view,
192+
// offset by scrollmargin unless at the beginning or end of the file
193+
func (h *BufPane) CursorToViewBottom() bool {
194+
v := h.GetView()
195+
h.Buf.ClearCursors()
196+
scrollmargin := int(h.Buf.Settings["scrollmargin"].(float64))
197+
bEnd := h.SLocFromLoc(h.Buf.End())
198+
lastLine := h.Scroll(v.StartLine, h.BufView().Height-1)
199+
if lastLine == bEnd {
200+
scrollmargin = 0
201+
}
202+
h.Cursor.GotoLoc(h.LocFromVLoc(display.VLoc{
203+
SLoc: h.Scroll(lastLine, -scrollmargin),
204+
VisualX: 0,
205+
}))
206+
return true
207+
}
208+
163209
// MoveCursorUp is not an action
164210
func (h *BufPane) MoveCursorUp(n int) {
165211
if !h.Buf.Settings["softwrap"].(bool) {

internal/action/bufpane.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,9 @@ var BufKeyActions = map[string]BufKeyAction{
738738
"CursorRight": (*BufPane).CursorRight,
739739
"CursorStart": (*BufPane).CursorStart,
740740
"CursorEnd": (*BufPane).CursorEnd,
741+
"CursorToViewTop": (*BufPane).CursorToViewTop,
742+
"CursorToViewCenter": (*BufPane).CursorToViewCenter,
743+
"CursorToViewBottom": (*BufPane).CursorToViewBottom,
741744
"SelectToStart": (*BufPane).SelectToStart,
742745
"SelectToEnd": (*BufPane).SelectToEnd,
743746
"SelectUp": (*BufPane).SelectUp,

runtime/help/keybindings.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ CursorLeft
168168
CursorRight
169169
CursorStart
170170
CursorEnd
171+
CursorToViewTop
172+
CursorToViewCenter
173+
CursorToViewBottom
171174
SelectToStart
172175
SelectToEnd
173176
SelectUp

0 commit comments

Comments
 (0)