From b0e404351311b7c62810f3f7fb2cc865d3012f33 Mon Sep 17 00:00:00 2001 From: Nitish Sakhawalkar Date: Fri, 15 Dec 2017 14:43:37 -0800 Subject: [PATCH] Changes to add support for Insert Key Press --- cmd/micro/view.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/cmd/micro/view.go b/cmd/micro/view.go index b8fa7093..852c2838 100644 --- a/cmd/micro/view.go +++ b/cmd/micro/view.go @@ -73,6 +73,8 @@ type View struct { // mouse release events mouseReleased bool + // We need to keep track of insert key press toggle + isInsertMode bool // This stores when the last click was // This is useful for detecting double and triple clicks lastClickTime time.Time @@ -245,6 +247,9 @@ func (v *View) OpenBuffer(buf *Buffer) { // Set mouseReleased to true because we assume the mouse is not being pressed when // the editor is opened v.mouseReleased = true + // Set isInsertMode to false, because we assume we are in the default mode when editor + // is opened + v.isInsertMode = false v.lastClickTime = time.Time{} } @@ -564,6 +569,9 @@ func (v *View) HandleEvent(event tcell.Event) { } } } + if e.Key() == tcell.KeyInsert { + v.isInsertMode = !v.isInsertMode + } if !isBinding && e.Key() == tcell.KeyRune { // Check viewtype if readonly don't insert a rune (readonly help and log view etc.) if v.Type.Readonly == false { @@ -575,7 +583,14 @@ func (v *View) HandleEvent(event tcell.Event) { v.Cursor.DeleteSelection() v.Cursor.ResetSelection() } - v.Buf.Insert(v.Cursor.Loc, string(e.Rune())) + + if v.isInsertMode { + next := v.Cursor.Loc + next.X++ + v.Buf.Replace(v.Cursor.Loc, next, string(e.Rune())) + } else { + v.Buf.Insert(v.Cursor.Loc, string(e.Rune())) + } for pl := range loadedPlugins { _, err := Call(pl+".onRune", string(e.Rune()), v)