]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/micro.go
Only lock event handling
[micro.git] / cmd / micro / micro.go
index 5eb62097192419c5b830c78483312ed2a7878f50..a4d22b70e5db8f95e194c532e0499c33957b49cd 100644 (file)
@@ -8,6 +8,7 @@ import (
        "regexp"
        "runtime"
        "sort"
+       "strconv"
        "time"
 
        "github.com/go-errors/errors"
@@ -16,6 +17,7 @@ import (
        "github.com/zyedidia/micro/v2/internal/action"
        "github.com/zyedidia/micro/v2/internal/buffer"
        "github.com/zyedidia/micro/v2/internal/config"
+       ulua "github.com/zyedidia/micro/v2/internal/lua"
        "github.com/zyedidia/micro/v2/internal/screen"
        "github.com/zyedidia/micro/v2/internal/shell"
        "github.com/zyedidia/micro/v2/internal/util"
@@ -44,7 +46,7 @@ func InitFlags() {
                fmt.Println("    \tCleans the configuration directory")
                fmt.Println("-config-dir dir")
                fmt.Println("    \tSpecify a custom location for the configuration directory")
-               fmt.Println("[FILE]:LINE:COL")
+               fmt.Println("[FILE]:LINE:COL (if the `parsecursor` option is enabled)")
                fmt.Println("+LINE:COL")
                fmt.Println("    \tSpecify a line and column to start the cursor at when opening a buffer")
                fmt.Println("-options")
@@ -131,7 +133,7 @@ func DoPluginFlags() {
 
 // LoadInput determines which files should be loaded into buffers
 // based on the input stored in flag.Args()
-func LoadInput() []*buffer.Buffer {
+func LoadInput(args []string) []*buffer.Buffer {
        // There are a number of ways micro should start given its input
 
        // 1. If it is given a files in flag.Args(), it should open those
@@ -146,7 +148,6 @@ func LoadInput() []*buffer.Buffer {
        var filename string
        var input []byte
        var err error
-       args := flag.Args()
        buffers := make([]*buffer.Buffer, 0, len(args))
 
        btype := buffer.BTDefault
@@ -155,18 +156,31 @@ func LoadInput() []*buffer.Buffer {
        }
 
        files := make([]string, 0, len(args))
-       flagStartPos := ""
-       flagr := regexp.MustCompile(`^\+\d+(:\d+)?$`)
+       flagStartPos := buffer.Loc{-1, -1}
+       flagr := regexp.MustCompile(`^\+(\d+)(?::(\d+))?$`)
        for _, a := range args {
-               if flagr.MatchString(a) {
-                       flagStartPos = a[1:]
-               } else {
-                       if flagStartPos != "" {
-                               files = append(files, a+":"+flagStartPos)
-                               flagStartPos = ""
-                       } else {
-                               files = append(files, a)
+               match := flagr.FindStringSubmatch(a)
+               if len(match) == 3 && match[2] != "" {
+                       line, err := strconv.Atoi(match[1])
+                       if err != nil {
+                               screen.TermMessage(err)
+                               continue
                        }
+                       col, err := strconv.Atoi(match[2])
+                       if err != nil {
+                               screen.TermMessage(err)
+                               continue
+                       }
+                       flagStartPos = buffer.Loc{col - 1, line - 1}
+               } else if len(match) == 3 && match[2] == "" {
+                       line, err := strconv.Atoi(match[1])
+                       if err != nil {
+                               screen.TermMessage(err)
+                               continue
+                       }
+                       flagStartPos = buffer.Loc{0, line - 1}
+               } else {
+                       files = append(files, a)
                }
        }
 
@@ -174,7 +188,7 @@ func LoadInput() []*buffer.Buffer {
                // Option 1
                // We go through each file and load it
                for i := 0; i < len(files); i++ {
-                       buf, err := buffer.NewBufferFromFile(files[i], btype)
+                       buf, err := buffer.NewBufferFromFileAtLoc(files[i], btype, flagStartPos)
                        if err != nil {
                                screen.TermMessage(err)
                                continue
@@ -191,10 +205,10 @@ func LoadInput() []*buffer.Buffer {
                        screen.TermMessage("Error reading from stdin: ", err)
                        input = []byte{}
                }
-               buffers = append(buffers, buffer.NewBufferFromString(string(input), filename, btype))
+               buffers = append(buffers, buffer.NewBufferFromStringAtLoc(string(input), filename, btype, flagStartPos))
        } else {
                // Option 3, just open an empty buffer
-               buffers = append(buffers, buffer.NewBufferFromString(string(input), filename, btype))
+               buffers = append(buffers, buffer.NewBufferFromStringAtLoc(string(input), filename, btype, flagStartPos))
        }
 
        return buffers
@@ -229,7 +243,10 @@ func main() {
        if err != nil {
                screen.TermMessage(err)
        }
-       config.InitGlobalSettings()
+       err = config.InitGlobalSettings()
+       if err != nil {
+               screen.TermMessage(err)
+       }
 
        // flag options
        for k, v := range optionFlags {
@@ -245,7 +262,12 @@ func main() {
 
        DoPluginFlags()
 
-       screen.Init()
+       err = screen.Init()
+       if err != nil {
+               fmt.Println(err)
+               fmt.Println("Fatal: Micro could not initialize a Screen.")
+               os.Exit(1)
+       }
 
        defer func() {
                if err := recover(); err != nil {
@@ -253,7 +275,7 @@ func main() {
                        fmt.Println("Micro encountered an error:", err)
                        // backup all open buffers
                        for _, b := range buffer.OpenBuffers {
-                               b.Backup(false)
+                               b.Backup()
                        }
                        // Print the stack trace too
                        fmt.Print(errors.Wrap(err, 2).ErrorStack())
@@ -274,7 +296,8 @@ func main() {
                screen.TermMessage(err)
        }
 
-       b := LoadInput()
+       args := flag.Args()
+       b := LoadInput(args)
 
        if len(b) == 0 {
                // No buffers to open
@@ -355,11 +378,15 @@ func DoEvent() {
        select {
        case f := <-shell.Jobs:
                // If a new job has finished while running in the background we should execute the callback
+               ulua.Lock.Lock()
                f.Function(f.Output, f.Args)
+               ulua.Lock.Unlock()
        case <-config.Autosave:
+               ulua.Lock.Lock()
                for _, b := range buffer.OpenBuffers {
                        b.Save()
                }
+               ulua.Lock.Unlock()
        case <-shell.CloseTerms:
        case event = <-events:
        case <-screen.DrawChan():
@@ -368,9 +395,11 @@ func DoEvent() {
                }
        }
 
+       ulua.Lock.Lock()
        if action.InfoBar.HasPrompt {
                action.InfoBar.HandleEvent(event)
        } else {
                action.Tabs.HandleEvent(event)
        }
+       ulua.Lock.Unlock()
 }