From 20ab7a44ab63112b77dfb05d9a0d5b1f7849c519 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sun, 24 Apr 2016 17:26:42 -0400 Subject: [PATCH] Relocate cursor on reOpen() --- cmd/micro/cursor.go | 16 ++++++++++++++++ cmd/micro/view.go | 6 +----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/cmd/micro/cursor.go b/cmd/micro/cursor.go index 43f2cc22..4e2ce558 100644 --- a/cmd/micro/cursor.go +++ b/cmd/micro/cursor.go @@ -304,6 +304,22 @@ func (c *Cursor) GetVisualX() int { return c.x + NumOccurences(string(runes[:c.x]), '\t')*(tabSize-1) } +// Relocate makes sure that the cursor is inside the bounds of the buffer +// If it isn't, it moves it to be within the buffer's lines +func (c *Cursor) Relocate() { + if c.y < 0 { + c.y = 0 + } else if c.y >= len(c.v.buf.lines) { + c.y = len(c.v.buf.lines) - 1 + } + + if c.x < 0 { + c.x = 0 + } else if c.x > Count(c.v.buf.lines[c.y]) { + c.x = Count(c.v.buf.lines[c.y]) + } +} + // Display draws the cursor to the screen at the correct position func (c *Cursor) Display() { // Don't draw the cursor if it is out of the viewport or if it has a selection diff --git a/cmd/micro/view.go b/cmd/micro/view.go index 63519f83..6ac769ad 100644 --- a/cmd/micro/view.go +++ b/cmd/micro/view.go @@ -183,15 +183,11 @@ func (v *View) reOpen() { buf := NewBuffer(string(file), filename) v.buf = buf v.matches = Match(v) + v.cursor.Relocate() v.Relocate() } } -// OpenFile opens a new file in the current view -// It makes sure that the current buffer can be closed first (unsaved changes) -func (v *View) OpenFile() { -} - // Relocate moves the view window so that the cursor is in view // This is useful if the user has scrolled far away, and then starts typing func (v *View) Relocate() bool {