diff --git a/cmd/micro/actions.go b/cmd/micro/actions.go index 73c9cf67..c238aafa 100644 --- a/cmd/micro/actions.go +++ b/cmd/micro/actions.go @@ -44,6 +44,25 @@ func PostActionCall(funcName string) bool { return relocate } +// Center centers the view on the cursor +func (v *View) Center(usePlugin bool) bool { + if usePlugin && !PreActionCall("Center") { + return false + } + + v.Topline = v.Cursor.Y - v.height/2 + if v.Topline < 0 { + v.Topline = 0 + } else if v.Topline+v.height > v.Buf.NumLines { + v.Topline = v.Buf.NumLines - v.height + } + + if usePlugin { + return PostActionCall("Center") + } + return true +} + // CursorUp moves the cursor up func (v *View) CursorUp(usePlugin bool) bool { if usePlugin && !PreActionCall("CursorUp") { diff --git a/cmd/micro/bindings.go b/cmd/micro/bindings.go index 93bd6f21..897933c0 100644 --- a/cmd/micro/bindings.go +++ b/cmd/micro/bindings.go @@ -44,6 +44,7 @@ var bindingActions = map[string]func(*View, bool) bool{ "Find": (*View).Find, "FindNext": (*View).FindNext, "FindPrevious": (*View).FindPrevious, + "Center": (*View).Center, "Undo": (*View).Undo, "Redo": (*View).Redo, "Copy": (*View).Copy, diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index 36b7b432..1c9e88c8 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -249,7 +249,7 @@ func main() { tabs = append(tabs, tab) for _, t := range tabs { for _, v := range t.views { - v.Relocate() + v.Center(false) if settings["syntax"].(bool) { v.matches = Match(v) }