+// 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 {
+ return NewBuffer(strings.NewReader(text), int64(len(text)), path)
+}
+
+// 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)
+ }
+ }
+ }
+ }
+