Repository: zyedidia/micro
File: internal/action/actions.go (Go)

Code before:
	h.ScrollDown(util.IntOpt(h.Buf.Settings["scrollspeed"]))
	return true
}

// Center centers the view on the cursor
func (h *BufPane) Center() bool {
	v := h.GetView()
	v.StartLine = h.Scroll(h.SLocFromLoc(h.Cursor.Loc), -h.BufView().Height/2)
	h.SetView(v)
	h.ScrollAdjust()
	return true
}

// CursorToView moves the cursor within the view if it's currently out of view.
// The cursor is moved to an offset of 25% from the top of the view.
func (h *BufPane) CursorToView() bool {
	v := h.GetView()
	height := h.BufView().Height
	cursorSloc := h.SLocFromLoc(h.Cursor.Loc)
	if cursorSloc.LessThan(v.StartLine) || cursorSloc.GreaterThan(h.Scroll(v.StartLine, height-1)) {
		h.Cursor.Deselect(true)
		h.Cursor.GotoLoc(h.LocFromVLoc(display.VLoc{
			SLoc:    h.Scroll(v.StartLine, height/4),
			VisualX: 0,
		}))
		h.RemoveAllMultiCursors()
		return true
	}
	return false
}

// MoveCursorUp is not an action
func (h *BufPane) MoveCursorUp(n int) {
	if !h.Buf.Settings["softwrap"].(bool) {
		h.Cursor.UpN(n)
	} else {
		vloc := h.VLocFromLoc(h.Cursor.Loc)
		sloc := h.Scroll(vloc.SLoc, -n)
		if sloc == vloc.SLoc {
			// we are at the beginning of buffer
			h.Cursor.Loc = h.Buf.Start()
			h.Cursor.LastVisualX = 0
		} else {
			vloc.SLoc = sloc
			vloc.VisualX = h.Cursor.LastVisualX
			h.Cursor.Loc = h.LocFromVLoc(vloc)
		}
	}
}

// MoveCursorDown is not an action

Review comment (suggestion):
It probably makes more sense to remove multicursors before moving the cursor, not after it, since we want to move just one cursor? (Although the end result is probably the same.)

Probably even before deselect. See e.g. `GotoCmd()` and `JumpCmd()` in `internal/action/command.go`.

Code after:
	h.ScrollDown(util.IntOpt(h.Buf.Settings["scrollspeed"]))
	return true
}

// Center centers the view on the cursor
func (h *BufPane) Center() bool {
	v := h.GetView()
	v.StartLine = h.Scroll(h.SLocFromLoc(h.Cursor.Loc), -h.BufView().Height/2)
	h.SetView(v)
	h.ScrollAdjust()
	return true
}

// CursorToViewTop moves the cursor to the top of the view,
// offset by scrollmargin unless at the beginning or end of the file
func (h *BufPane) CursorToViewTop() bool {
	v := h.GetView()
	h.Buf.ClearCursors()
	scrollmargin := int(h.Buf.Settings["scrollmargin"].(float64))
	bStart := display.SLoc{0, 0}
	if v.StartLine == bStart {
		scrollmargin = 0
	}
	h.Cursor.GotoLoc(h.LocFromVLoc(display.VLoc{
		SLoc:    h.Scroll(v.StartLine, scrollmargin),
		VisualX: 0,
	}))
	return true
}

// CursorToViewCenter moves the cursor to the center of the view
func (h *BufPane) CursorToViewCenter() bool {
	v := h.GetView()
	h.Buf.ClearCursors()
	h.Cursor.GotoLoc(h.LocFromVLoc(display.VLoc{
		SLoc:    h.Scroll(v.StartLine, h.BufView().Height/2),
		VisualX: 0,
	}))
	return true
}

// CursorToViewBottom moves the cursor to the bottom of the view,
// offset by scrollmargin unless at the beginning or end of the file
func (h *BufPane) CursorToViewBottom() bool {
	v := h.GetView()
	h.Buf.ClearCursors()
	scrollmargin := int(h.Buf.Settings["scrollmargin"].(float64))
	bEnd := h.SLocFromLoc(h.Buf.End())
	lastLine := h.Scroll(v.StartLine, h.BufView().Height-1)
	if lastLine == bEnd {
		scrollmargin = 0