+ if _, err := os.Stat(configDir + "/buffers/"); os.IsNotExist(err) {
+ os.Mkdir(configDir+"/buffers/", os.ModePerm)
+ }
+
+ // Put the cursor at the first spot
+ cursorStartX := 0
+ cursorStartY := 0
+ // If -startpos LINE,COL was passed, use start position LINE,COL
+ if len(*flagStartPos) > 0 {
+ positions := strings.Split(*flagStartPos, ",")
+ if len(positions) == 2 {
+ lineNum, errPos1 := strconv.Atoi(positions[0])
+ colNum, errPos2 := strconv.Atoi(positions[1])
+ if errPos1 == nil && errPos2 == nil {
+ cursorStartX = colNum
+ cursorStartY = lineNum - 1
+ // Check to avoid line overflow
+ if cursorStartY > b.NumLines {
+ cursorStartY = b.NumLines - 1
+ } else if cursorStartY < 0 {
+ cursorStartY = 0
+ }
+ // Check to avoid column overflow
+ if cursorStartX > len(b.Line(cursorStartY)) {
+ cursorStartX = len(b.Line(cursorStartY))
+ } else if cursorStartX < 0 {
+ cursorStartX = 0
+ }
+ }
+ }
+ }
+ b.Cursor = Cursor{
+ Loc: Loc{
+ X: cursorStartX,
+ Y: cursorStartY,
+ },
+ buf: b,
+ }
+
+ InitLocalSettings(b)
+
+ if b.Settings["savecursor"].(bool) || b.Settings["saveundo"].(bool) {
+ // If either savecursor or saveundo is turned on, we need to load the serialized information
+ // from ~/.config/micro/buffers
+ file, err := os.Open(configDir + "/buffers/" + EscapePath(b.AbsPath))
+ if err == nil {
+ var buffer SerializedBuffer
+ decoder := gob.NewDecoder(file)
+ gob.Register(TextEvent{})
+ err = decoder.Decode(&buffer)
+ if err != nil {
+ TermMessage(err.Error(), "\n", "You may want to remove the files in ~/.config/micro/buffers (these files store the information for the 'saveundo' and 'savecursor' options) if this problem persists.")
+ }
+ if b.Settings["savecursor"].(bool) {
+ b.Cursor = buffer.Cursor
+ b.Cursor.buf = b
+ b.Cursor.Relocate()
+ }
+
+ if b.Settings["saveundo"].(bool) {
+ // We should only use last time's eventhandler if the file wasn't by someone else in the meantime
+ if b.ModTime == buffer.ModTime {
+ b.EventHandler = buffer.EventHandler
+ b.EventHandler.buf = b
+ }
+ }
+ }
+ file.Close()
+ }
+