]> git.lizzy.rs Git - micro.git/commitdiff
Changes to add support for Insert Key Press
authorNitish Sakhawalkar <nitsakh@icloud.com>
Fri, 15 Dec 2017 22:43:37 +0000 (14:43 -0800)
committerNitish Sakhawalkar <nitsakh@icloud.com>
Mon, 18 Dec 2017 21:28:21 +0000 (13:28 -0800)
cmd/micro/view.go

index b8fa7093aa064c73d62be36bed4f1a62b9da2a9c..852c283800ca6942c0e5c3a67cbd997872029214 100644 (file)
@@ -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)