]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/buffer.go
Open at line syntax with filename:line:col
[micro.git] / cmd / micro / buffer.go
index f3f92d7629d5c36cb336eca8d99e83d8d2efe3cd..5a0eb99563c9aed5f191a29f8e8599a46039a58b 100644 (file)
@@ -74,6 +74,33 @@ type SerializedBuffer struct {
        ModTime      time.Time
 }
 
+// NewBufferFromFile opens a new buffer using the given filepath
+// It will also automatically handle `~`, and line/column with filename:l:c
+// It will return an empty buffer if the filepath does not exist
+// and an error if the file is a directory
+func NewBufferFromFile(path string) (*Buffer, error) {
+       filename := GetPath(path)
+       filename = ReplaceHome(filename)
+       file, err := os.Open(filename)
+       fileInfo, _ := os.Stat(filename)
+
+       if err == nil && fileInfo.IsDir() {
+               return nil, errors.New(filename + " is a directory")
+       }
+
+       defer file.Close()
+
+       var buf *Buffer
+       if err != nil {
+               // File does not exist -- create an empty buffer with that name
+               buf = NewBufferFromString("", path)
+       } else {
+               buf = NewBuffer(file, FSize(file), path)
+       }
+
+       return buf, nil
+}
+
 // NewBufferFromString creates a new buffer containing the given
 // string
 func NewBufferFromString(text, path string) *Buffer {
@@ -82,6 +109,26 @@ func NewBufferFromString(text, path string) *Buffer {
 
 // NewBuffer creates a new buffer from a given reader with a given path
 func NewBuffer(reader io.Reader, size int64, path string) *Buffer {
+       startpos := Loc{0, 0}
+       startposErr := true
+       if strings.Contains(path, ":") {
+               var err error
+               split := strings.Split(path, ":")
+               path = split[0]
+               startpos.Y, err = strconv.Atoi(split[1])
+               if err != nil {
+                       messenger.Error("Error opening file: ", err)
+               } else {
+                       startposErr = false
+                       if len(split) > 2 {
+                               startpos.X, err = strconv.Atoi(split[2])
+                               if err != nil {
+                                       messenger.Error("Error opening file: ", err)
+                               }
+                       }
+               }
+       }
+
        if path != "" {
                for _, tab := range tabs {
                        for _, view := range tab.views {
@@ -129,11 +176,18 @@ func NewBuffer(reader io.Reader, size int64, path string) *Buffer {
        cursorStartX := 0
        cursorStartY := 0
        // If -startpos LINE,COL was passed, use start position LINE,COL
-       if len(*flagStartPos) > 0 {
+       if len(*flagStartPos) > 0 || !startposErr {
                positions := strings.Split(*flagStartPos, ",")
-               if len(positions) == 2 {
-                       lineNum, errPos1 := strconv.Atoi(positions[0])
-                       colNum, errPos2 := strconv.Atoi(positions[1])
+               if len(positions) == 2 || !startposErr {
+                       var lineNum, colNum int
+                       var errPos1, errPos2 error
+                       if !startposErr {
+                               lineNum = startpos.Y
+                               colNum = startpos.X
+                       } else {
+                               lineNum, errPos1 = strconv.Atoi(positions[0])
+                               colNum, errPos2 = strconv.Atoi(positions[1])
+                       }
                        if errPos1 == nil && errPos2 == nil {
                                cursorStartX = colNum
                                cursorStartY = lineNum - 1
@@ -162,7 +216,7 @@ func NewBuffer(reader io.Reader, size int64, path string) *Buffer {
 
        InitLocalSettings(b)
 
-       if len(*flagStartPos) == 0 && (b.Settings["savecursor"].(bool) || b.Settings["saveundo"].(bool)) {
+       if startposErr && len(*flagStartPos) == 0 && (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))