]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/micro.go
Exit gracefully when SIGTERM is received
[micro.git] / cmd / micro / micro.go
index f6360255fa46d9f91c90895a20db5a343ee94363..cae723996e29f8b800acd347fc24c8ba7c70331b 100644 (file)
@@ -5,24 +5,30 @@ import (
        "fmt"
        "io/ioutil"
        "os"
+       "os/signal"
+       "regexp"
        "runtime"
        "sort"
+       "strconv"
+       "syscall"
        "time"
 
        "github.com/go-errors/errors"
        isatty "github.com/mattn/go-isatty"
-       "github.com/zyedidia/micro/internal/action"
-       "github.com/zyedidia/micro/internal/buffer"
-       "github.com/zyedidia/micro/internal/config"
-       "github.com/zyedidia/micro/internal/screen"
-       "github.com/zyedidia/micro/internal/shell"
-       "github.com/zyedidia/micro/internal/util"
+       lua "github.com/yuin/gopher-lua"
+       "github.com/zyedidia/micro/v2/internal/action"
+       "github.com/zyedidia/micro/v2/internal/buffer"
+       "github.com/zyedidia/micro/v2/internal/clipboard"
+       "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"
        "github.com/zyedidia/tcell"
 )
 
 var (
        // Event channel
-       events   chan tcell.Event
        autosave chan bool
 
        // Command line flags
@@ -42,7 +48,8 @@ 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")
                fmt.Println("    \tShow all option help")
@@ -128,7 +135,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
@@ -143,14 +150,47 @@ func LoadInput() []*buffer.Buffer {
        var filename string
        var input []byte
        var err error
-       args := flag.Args()
        buffers := make([]*buffer.Buffer, 0, len(args))
 
-       if len(args) > 0 {
+       btype := buffer.BTDefault
+       if !isatty.IsTerminal(os.Stdout.Fd()) {
+               btype = buffer.BTStdout
+       }
+
+       files := make([]string, 0, len(args))
+       flagStartPos := buffer.Loc{-1, -1}
+       flagr := regexp.MustCompile(`^\+(\d+)(?::(\d+))?$`)
+       for _, a := range args {
+               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)
+               }
+       }
+
+       if len(files) > 0 {
                // Option 1
                // We go through each file and load it
-               for i := 0; i < len(args); i++ {
-                       buf, err := buffer.NewBufferFromFile(args[i], buffer.BTDefault)
+               for i := 0; i < len(files); i++ {
+                       buf, err := buffer.NewBufferFromFileAtLoc(files[i], btype, flagStartPos)
                        if err != nil {
                                screen.TermMessage(err)
                                continue
@@ -167,17 +207,22 @@ func LoadInput() []*buffer.Buffer {
                        screen.TermMessage("Error reading from stdin: ", err)
                        input = []byte{}
                }
-               buffers = append(buffers, buffer.NewBufferFromString(string(input), filename, buffer.BTDefault))
+               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, buffer.BTDefault))
+               buffers = append(buffers, buffer.NewBufferFromStringAtLoc(string(input), filename, btype, flagStartPos))
        }
 
        return buffers
 }
 
 func main() {
-       defer os.Exit(0)
+       defer func() {
+               if util.Stdout.Len() > 0 {
+                       fmt.Fprint(os.Stdout, util.Stdout.String())
+               }
+               os.Exit(0)
+       }()
 
        // runtime.SetCPUProfileRate(400)
        // f, _ := os.Create("micro.prof")
@@ -200,7 +245,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 {
@@ -216,18 +264,36 @@ 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)
+       }
+
+       c := make(chan os.Signal, 1)
+       signal.Notify(c, os.Kill, syscall.SIGTERM)
+
+       go func() {
+               <-c
+               if screen.Screen != nil {
+                       screen.Screen.Fini()
+               }
+               os.Exit(0)
+       }()
+
+       m := clipboard.SetMethod(config.GetGlobalOption("clipboard").(string))
+       clipErr := clipboard.Initialize(m)
 
-       // If we have an error, we can exit cleanly and not completely
-       // mess up the terminal being worked in
-       // In other words we need to shut down tcell before the program crashes
        defer func() {
                if err := recover(); err != nil {
-                       screen.Screen.Fini()
+                       if screen.Screen != nil {
+                               screen.Screen.Fini()
+                       }
                        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())
@@ -248,7 +314,13 @@ func main() {
                screen.TermMessage(err)
        }
 
-       b := LoadInput()
+       err = config.RunPluginFn("preinit")
+       if err != nil {
+               screen.TermMessage(err)
+       }
+
+       args := flag.Args()
+       b := LoadInput(args)
 
        if len(b) == 0 {
                // No buffers to open
@@ -264,7 +336,16 @@ func main() {
                screen.TermMessage(err)
        }
 
-       events = make(chan tcell.Event)
+       err = config.RunPluginFn("postinit")
+       if err != nil {
+               screen.TermMessage(err)
+       }
+
+       if clipErr != nil {
+               action.InfoBar.Error(clipErr, " or change 'clipboard' option")
+       }
+
+       screen.Events = make(chan tcell.Event)
 
        // Here is the event loop which runs in a separate thread
        go func() {
@@ -273,59 +354,84 @@ func main() {
                        e := screen.Screen.PollEvent()
                        screen.Unlock()
                        if e != nil {
-                               events <- e
+                               screen.Events <- e
                        }
                }
        }()
 
        // clear the drawchan so we don't redraw excessively
        // if someone requested a redraw before we started displaying
-       for len(screen.DrawChan) > 0 {
-               <-screen.DrawChan
+       for len(screen.DrawChan()) > 0 {
+               <-screen.DrawChan()
        }
 
-       var event tcell.Event
-
        // wait for initial resize event
        select {
-       case event = <-events:
+       case event := <-screen.Events:
                action.Tabs.HandleEvent(event)
        case <-time.After(10 * time.Millisecond):
                // time out after 10ms
        }
 
+       // Since this loop is very slow (waits for user input every time) it's
+       // okay to be inefficient and run it via a function every time
+       // We do this so we can recover from panics without crashing the editor
        for {
-               // Display everything
-               screen.Screen.Fill(' ', config.DefStyle)
-               screen.Screen.HideCursor()
-               action.Tabs.Display()
-               for _, ep := range action.MainTab().Panes {
-                       ep.Display()
-               }
-               action.MainTab().Display()
-               action.InfoBar.Display()
-               screen.Screen.Show()
-
-               event = nil
-
-               // Check for new events
-               select {
-               case f := <-shell.Jobs:
-                       // If a new job has finished while running in the background we should execute the callback
-                       f.Function(f.Output, f.Args...)
-               case <-config.Autosave:
-                       for _, b := range buffer.OpenBuffers {
-                               b.Save()
+               DoEvent()
+       }
+}
+
+// DoEvent runs the main action loop of the editor
+func DoEvent() {
+       var event tcell.Event
+
+       // recover from errors without crashing the editor
+       defer func() {
+               if err := recover(); err != nil {
+                       if e, ok := err.(*lua.ApiError); ok {
+                               screen.TermMessage("Lua API error:", e)
+                       } else {
+                               screen.TermMessage("Micro encountered an error:", errors.Wrap(err, 2).ErrorStack(), "\nIf you can reproduce this error, please report it at https://github.com/zyedidia/micro/issues")
                        }
-               case <-shell.CloseTerms:
-               case event = <-events:
-               case <-screen.DrawChan:
                }
+       }()
+       // Display everything
+       screen.Screen.Fill(' ', config.DefStyle)
+       screen.Screen.HideCursor()
+       action.Tabs.Display()
+       for _, ep := range action.MainTab().Panes {
+               ep.Display()
+       }
+       action.MainTab().Display()
+       action.InfoBar.Display()
+       screen.Screen.Show()
 
-               if action.InfoBar.HasPrompt {
-                       action.InfoBar.HandleEvent(event)
-               } else {
-                       action.Tabs.HandleEvent(event)
+       // Check for new events
+       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 = <-screen.Events:
+       case <-screen.DrawChan():
+               for len(screen.DrawChan()) > 0 {
+                       <-screen.DrawChan()
                }
        }
+
+       ulua.Lock.Lock()
+       if action.InfoBar.HasPrompt {
+               action.InfoBar.HandleEvent(event)
+       } else {
+               action.Tabs.HandleEvent(event)
+       }
+       ulua.Lock.Unlock()
 }