9 "github.com/go-errors/errors"
10 "github.com/mattn/go-isatty"
11 "github.com/mitchellh/go-homedir"
12 "github.com/zyedidia/tcell"
13 "github.com/zyedidia/tcell/encoding"
17 synLinesUp = 75 // How many lines up to look to do syntax highlighting
18 synLinesDown = 75 // How many lines down to look to do syntax highlighting
19 doubleClickThreshold = 400 // How many milliseconds to wait before a second click is not a double click
20 undoThreshold = 500 // If two events are less than n milliseconds apart, undo both of them
27 // Object to send messages and prompts to the user
33 // Where the user's configuration is
34 // This should be $XDG_CONFIG_HOME/micro
35 // If $XDG_CONFIG_HOME is not set, it is ~/.config/micro
38 // Version is the version number.
39 // This should be set by the linker
42 // Is the help screen open
46 // LoadInput loads the file input for the editor
47 func LoadInput() (string, []byte, error) {
48 // There are a number of ways micro should start given its input
49 // 1. If it is given a file in os.Args, it should open that
51 // 2. If there is no input file and the input is not a terminal, that means
52 // something is being piped in and the stdin should be opened in an
55 // 3. If there is no input file and the input is a terminal, an empty buffer
58 // These are empty by default so if we get to option 3, we can just returns the
67 // Check that the file exists
68 if _, e := os.Stat(filename); e == nil {
69 input, err = ioutil.ReadFile(filename)
71 } else if !isatty.IsTerminal(os.Stdin.Fd()) {
73 // The input is not a terminal, so something is being piped in
74 // and we should read from stdin
75 input, err = ioutil.ReadAll(os.Stdin)
78 // Option 3, or just return whatever we got
79 return filename, input, err
82 // InitConfigDir finds the configuration directory for micro according to the
84 // If no directory is found, it creates one.
85 func InitConfigDir() {
86 xdgHome := os.Getenv("XDG_CONFIG_HOME")
88 home, err := homedir.Dir()
90 TermMessage("Error finding your home directory\nCan't load syntax files")
93 xdgHome = home + "/.config"
95 configDir = xdgHome + "/micro"
97 if _, err := os.Stat(xdgHome); os.IsNotExist(err) {
98 err = os.Mkdir(xdgHome, os.ModePerm)
100 TermMessage("Error creating XDG_CONFIG_HOME directory: " + err.Error())
104 if _, err := os.Stat(configDir); os.IsNotExist(err) {
105 err = os.Mkdir(configDir, os.ModePerm)
107 TermMessage("Error creating configuration directory: " + err.Error())
112 // InitScreen creates and initializes the tcell screen
114 // Should we enable true color?
115 truecolor := os.Getenv("MICRO_TRUECOLOR") == "1"
117 // In order to enable true color, we have to set the TERM to `xterm-truecolor` when
118 // initializing tcell, but after that, we can set the TERM back to whatever it was
119 oldTerm := os.Getenv("TERM")
121 os.Setenv("TERM", "xterm-truecolor")
126 screen, err = tcell.NewScreen()
131 if err = screen.Init(); err != nil {
136 // Now we can put the TERM back to what it was before
138 os.Setenv("TERM", oldTerm)
142 defStyle = tcell.StyleDefault.
143 Foreground(tcell.ColorDefault).
144 Background(tcell.ColorDefault)
146 // There may be another default style defined in the colorscheme
147 if style, ok := colorscheme["default"]; ok {
151 screen.SetStyle(defStyle)
155 // Redraw redraws the screen and the given view
156 func Redraw(view *View) {
163 var flagVersion = flag.Bool("version", false, "Show version number")
168 fmt.Println("Micro version:", Version)
172 filename, input, err := LoadInput()
179 tcell.SetEncodingFallback(tcell.EncodingFallbackASCII)
181 // Find the user's configuration directory (probably $XDG_CONFIG_HOME/micro)
183 // Load the user's settings
186 // Load the syntax files, including the colorscheme
188 // Load the help files
191 buf := NewBuffer(string(input), filename)
195 // This is just so if we have an error, we can exit cleanly and not completely
196 // mess up the terminal being worked in
198 if err := recover(); err != nil {
200 fmt.Println("Micro encountered an error:", err)
201 // Print the stack trace too
202 fmt.Print(errors.Wrap(err, 2).ErrorStack())
207 messenger = new(Messenger)
211 // Display everything
214 // Wait for the user's action
215 event := screen.PollEvent()
218 HandleSearchEvent(event, view)
220 // Check if we should quit
221 switch e := event.(type) {
222 case *tcell.EventKey:
225 // Make sure not to quit if there are unsaved changes
230 if view.CanClose("Quit anyway? (yes, no, save) ") {
236 input, canceled := messenger.Prompt("> ")
238 HandleCommand(input, view)
241 input, canceled := messenger.Prompt("$ ")
243 HandleShellCommand(input, view, true)
247 helpBuffer := NewBuffer(helpTxt, "help.md")
248 helpBuffer.name = "Help"
250 view.OpenBuffer(helpBuffer)
258 // Send it to the view
259 view.HandleEvent(event)