8 "github.com/go-errors/errors"
9 "github.com/zyedidia/micro/cmd/micro/buffer"
10 "github.com/zyedidia/micro/cmd/micro/config"
11 "github.com/zyedidia/micro/cmd/micro/screen"
12 "github.com/zyedidia/micro/cmd/micro/util"
13 "github.com/zyedidia/tcell"
17 doubleClickThreshold = 400 // How many milliseconds to wait before a second click is not a double click
18 autosaveTime = 8 // Number of seconds to wait before autosaving
22 // Version is the version number or commit hash
23 // These variables should be set by the linker when compiling
24 Version = "0.0.0-unknown"
25 // CommitHash is the commit this version was built on
26 CommitHash = "Unknown"
27 // CompileDate is the date this binary was compiled on
28 CompileDate = "Unknown"
33 events chan tcell.Event
36 // How many redraws have happened
40 flagVersion = flag.Bool("version", false, "Show the version number and information")
41 flagStartPos = flag.String("startpos", "", "LINE,COL to start the cursor at when opening a buffer.")
42 flagConfigDir = flag.String("config-dir", "", "Specify a custom location for the configuration directory")
43 flagOptions = flag.Bool("options", false, "Show all option help")
48 fmt.Println("Usage: micro [OPTIONS] [FILE]...")
49 fmt.Println("-config-dir dir")
50 fmt.Println(" \tSpecify a custom location for the configuration directory")
51 fmt.Println("-startpos LINE,COL")
52 fmt.Println("+LINE:COL")
53 fmt.Println(" \tSpecify a line and column to start the cursor at when opening a buffer")
54 fmt.Println(" \tThis can also be done by opening file:LINE:COL")
55 fmt.Println("-options")
56 fmt.Println(" \tShow all option help")
57 fmt.Println("-version")
58 fmt.Println(" \tShow the version number and information")
60 fmt.Print("\nMicro's options can also be set via command line arguments for quick\nadjustments. For real configuration, please use the settings.json\nfile (see 'help options').\n\n")
61 fmt.Println("-option value")
62 fmt.Println(" \tSet `option` to `value` for this session")
63 fmt.Println(" \tFor example: `micro -syntax off file.c`")
64 fmt.Println("\nUse `micro -options` to see the full list of configuration options")
67 optionFlags := make(map[string]*string)
69 for k, v := range config.DefaultGlobalSettings() {
70 optionFlags[k] = flag.String(k, "", fmt.Sprintf("The %s option. Default value: '%v'", k, v))
76 // If -version was passed
77 fmt.Println("Version:", Version)
78 fmt.Println("Commit hash:", CommitHash)
79 fmt.Println("Compiled on", CompileDate)
84 // If -options was passed
85 for k, v := range config.DefaultGlobalSettings() {
86 fmt.Printf("-%s value\n", k)
87 fmt.Printf(" \tDefault value: '%v'\n", v)
98 err = config.InitConfigDir(*flagConfigDir)
100 util.TermMessage(err)
102 config.InitRuntimeFiles()
103 err = config.ReadSettings()
105 util.TermMessage(err)
107 config.InitGlobalSettings()
109 err = config.InitColorscheme()
111 util.TermMessage(err)
116 // If we have an error, we can exit cleanly and not completely
117 // mess up the terminal being worked in
118 // In other words we need to shut down tcell before the program crashes
120 if err := recover(); err != nil {
122 fmt.Println("Micro encountered an error:", err)
123 // Print the stack trace too
124 fmt.Print(errors.Wrap(err, 2).ErrorStack())
129 TryBindKey("Ctrl-z", "Undo", true)
131 b, err := buffer.NewBufferFromFile(os.Args[1])
134 util.TermMessage(err)
137 width, height := screen.Screen.Size()
138 w := NewWindow(0, 0, width, height-1, b)
140 a := NewBufActionHandler(b, w)
142 // Here is the event loop which runs in a separate thread
144 events = make(chan tcell.Event)
146 // TODO: fix race condition with screen.Screen = nil
147 events <- screen.Screen.PollEvent()
152 // Display everything
155 w.DisplayStatusLine()
158 var event tcell.Event
160 // Check for new events
162 case event = <-events: