]> git.lizzy.rs Git - micro.git/commitdiff
Add macro and QuitAll support
authorZachary Yedidia <zyedidia@gmail.com>
Mon, 5 Aug 2019 03:23:32 +0000 (20:23 -0700)
committerZachary Yedidia <zyedidia@gmail.com>
Wed, 25 Dec 2019 22:05:11 +0000 (17:05 -0500)
cmd/micro/micro.go
internal/action/actions.go
internal/action/bufpane.go

index bd1918b73d1e3843f4dc6f674fe50b93ea4e647a..734539de38aecd1086e3cf70ed5934f36d47f898 100644 (file)
@@ -4,7 +4,6 @@ import (
        "flag"
        "fmt"
        "io/ioutil"
-       "log"
        "os"
        "sort"
 
@@ -246,6 +245,5 @@ func main() {
                } else {
                        action.Tabs.HandleEvent(event)
                }
-               log.Println("Done event cycle")
        }
 }
index f8b02b66f7f34575587183ba2da3825a55896d55..da802c70dad4089a09141508c584bae7f99c2309 100644 (file)
@@ -46,6 +46,8 @@ func (h *BufPane) MousePress(e *tcell.EventMouse) bool {
                if b.NumCursors() > 1 {
                        b.ClearCursors()
                        h.Relocate()
+                       h.Cursor = h.Buf.GetActiveCursor()
+                       h.Cursor.Loc = mouseLoc
                }
                if time.Since(h.lastClickTime)/time.Millisecond < config.DoubleClickThreshold && (mouseLoc.X == h.lastLoc.X && mouseLoc.Y == h.lastLoc.Y) {
                        if h.doubleClick {
@@ -88,6 +90,7 @@ func (h *BufPane) MousePress(e *tcell.EventMouse) bool {
                }
        }
 
+       h.Cursor.StoreVisualX()
        h.lastLoc = mouseLoc
        return false
 }
@@ -269,11 +272,12 @@ func (h *BufPane) SelectWordLeft() bool {
 // StartOfLine moves the cursor to the start of the line
 func (h *BufPane) StartOfLine() bool {
        h.Cursor.Deselect(true)
-       if h.Cursor.X != 0 {
-               h.Cursor.Start()
-       } else {
-               h.Cursor.StartOfText()
-       }
+       h.Cursor.StartOfText()
+       // if h.Cursor.X != 0 {
+       //      h.Cursor.Start()
+       // } else {
+       //      h.Cursor.StartOfText()
+       // }
        return true
 }
 
@@ -1189,6 +1193,33 @@ func (h *BufPane) Quit() bool {
 
 // QuitAll quits the whole editor; all splits and tabs
 func (h *BufPane) QuitAll() bool {
+       anyModified := false
+       for _, b := range buffer.OpenBuffers {
+               if b.Modified() {
+                       anyModified = true
+                       break
+               }
+       }
+
+       quit := func() {
+               for _, b := range buffer.OpenBuffers {
+                       b.Close()
+               }
+               screen.Screen.Fini()
+               InfoBar.Close()
+               os.Exit(0)
+       }
+
+       if anyModified {
+               InfoBar.YNPrompt("Quit micro? (all open buffers will be closed without saving)", func(yes, canceled bool) {
+                       if !canceled && yes {
+                               quit()
+                       }
+               })
+       } else {
+               quit()
+       }
+
        return false
 }
 
@@ -1271,16 +1302,34 @@ func (h *BufPane) PreviousSplit() bool {
        return false
 }
 
-var curMacro []interface{}
-var recordingMacro bool
+var curmacro []interface{}
+var recording_macro bool
 
 // ToggleMacro toggles recording of a macro
 func (h *BufPane) ToggleMacro() bool {
+       recording_macro = !recording_macro
+       if recording_macro {
+               curmacro = []interface{}{}
+               InfoBar.Message("Recording")
+       } else {
+               InfoBar.Message("Stopped recording")
+       }
        return true
 }
 
 // PlayMacro plays back the most recently recorded macro
 func (h *BufPane) PlayMacro() bool {
+       if recording_macro {
+               return false
+       }
+       for _, action := range curmacro {
+               switch t := action.(type) {
+               case rune:
+                       h.DoRuneInsert(t)
+               case Event:
+                       h.DoKeyEvent(t)
+               }
+       }
        return true
 }
 
index 969ed9565c1804d1725e135ecb60c580208e0617..9709fa52c6a1216e87f07422164c5460c455b738 100644 (file)
@@ -289,6 +289,12 @@ func (h *BufPane) DoKeyEvent(e Event) bool {
                                        if h.PluginCB("on"+estr) && rel {
                                                h.Relocate()
                                        }
+
+                                       if recording_macro {
+                                               if estr != "ToggleMacro" && estr != "PlayMacro" {
+                                                       curmacro = append(curmacro, e)
+                                               }
+                                       }
                                }
                                return true
                        }
@@ -331,6 +337,7 @@ func (h *BufPane) DoRuneInsert(r rune) {
        for _, c := range cursors {
                // Insert a character
                h.Buf.SetCurCursor(c.Num)
+               h.Cursor = c
                if !h.PluginCBRune("preRune", r) {
                        continue
                }
@@ -346,6 +353,9 @@ func (h *BufPane) DoRuneInsert(r rune) {
                } else {
                        h.Buf.Insert(c.Loc, string(r))
                }
+               if recording_macro {
+                       curmacro = append(curmacro, r)
+               }
                h.PluginCBRune("onRune", r)
        }
 }