From b0e404351311b7c62810f3f7fb2cc865d3012f33 Mon Sep 17 00:00:00 2001 From: Nitish Sakhawalkar Date: Fri, 15 Dec 2017 14:43:37 -0800 Subject: [PATCH 1/2] 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) From f58c5412a81135b39a36a02d46b1c17ec2b0554c Mon Sep 17 00:00:00 2001 From: Nitish Sakhawalkar Date: Mon, 18 Dec 2017 17:11:00 -0800 Subject: [PATCH 2/2] Updating to make overwrite mode as an action --- cmd/micro/actions.go | 16 ++++++++++++++++ cmd/micro/bindings.go | 2 ++ cmd/micro/view.go | 12 +++++------- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/cmd/micro/actions.go b/cmd/micro/actions.go index 35c10738..00ecb35d 100644 --- a/cmd/micro/actions.go +++ b/cmd/micro/actions.go @@ -1697,6 +1697,22 @@ func (v *View) CommandMode(usePlugin bool) bool { return false } +// ToggleOverwriteMode lets the user toggle the text overwrite mode +func (v *View) ToggleOverwriteMode(usePlugin bool) bool { + if v.mainCursor() { + if usePlugin && !PreActionCall("ToggleOverwriteMode", v) { + return false + } + + v.isOverwriteMode = !v.isOverwriteMode + + if usePlugin { + return PostActionCall("ToggleOverwriteMode", v) + } + } + return false +} + // Escape leaves current mode func (v *View) Escape(usePlugin bool) bool { if v.mainCursor() { diff --git a/cmd/micro/bindings.go b/cmd/micro/bindings.go index c08ef5da..4307f831 100644 --- a/cmd/micro/bindings.go +++ b/cmd/micro/bindings.go @@ -90,6 +90,7 @@ var bindingActions = map[string]func(*View, bool) bool{ "ClearStatus": (*View).ClearStatus, "ShellMode": (*View).ShellMode, "CommandMode": (*View).CommandMode, + "ToggleOverwriteMode": (*View).ToggleOverwriteMode, "Escape": (*View).Escape, "Quit": (*View).Quit, "QuitAll": (*View).QuitAll, @@ -563,6 +564,7 @@ func DefaultBindings() map[string]string { "CtrlW": "NextSplit", "CtrlU": "ToggleMacro", "CtrlJ": "PlayMacro", + "Insert": "ToggleOverwriteMode", // Emacs-style keybindings "Alt-f": "WordRight", diff --git a/cmd/micro/view.go b/cmd/micro/view.go index 852c2838..e53c6618 100644 --- a/cmd/micro/view.go +++ b/cmd/micro/view.go @@ -74,7 +74,7 @@ type View struct { mouseReleased bool // We need to keep track of insert key press toggle - isInsertMode bool + isOverwriteMode bool // This stores when the last click was // This is useful for detecting double and triple clicks lastClickTime time.Time @@ -247,9 +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 + // Set isOverwriteMode to false, because we assume we are in the default mode when editor // is opened - v.isInsertMode = false + v.isOverwriteMode = false v.lastClickTime = time.Time{} } @@ -569,9 +569,7 @@ 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 { @@ -584,7 +582,7 @@ func (v *View) HandleEvent(event tcell.Event) { v.Cursor.ResetSelection() } - if v.isInsertMode { + if v.isOverwriteMode { next := v.Cursor.Loc next.X++ v.Buf.Replace(v.Cursor.Loc, next, string(e.Rune()))