mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-22 00:37:12 +09:00
Allows opening files using full path on Windows (#1126)
* Now can open Windows full-path from command line arg Example that now works: micro.exe D:\myfile.txt * Now correctly retrieves the path from the input path string. Except for single-letter filenames * Fixed line/cols, need to make the code prettier * Fixed path matching with regex by @Pariador * Fixed not stripping the line/col args from file path * Added tests for ParseCursorLocation
This commit is contained in:
committed by
Zachary Yedidia
parent
d7f7d845b9
commit
efe343b37c
@@ -77,12 +77,12 @@ type SerializedBuffer struct {
|
||||
ModTime time.Time
|
||||
}
|
||||
|
||||
// NewBufferFromFile opens a new buffer using the given filepath
|
||||
// NewBufferFromFile opens a new buffer using the given path
|
||||
// It will also automatically handle `~`, and line/column with filename:l:c
|
||||
// It will return an empty buffer if the filepath does not exist
|
||||
// It will return an empty buffer if the path does not exist
|
||||
// and an error if the file is a directory
|
||||
func NewBufferFromFile(path string) (*Buffer, error) {
|
||||
filename := GetPath(path)
|
||||
filename, cursorPosition := GetPathAndCursorPosition(path)
|
||||
filename = ReplaceHome(filename)
|
||||
file, err := os.Open(filename)
|
||||
fileInfo, _ := os.Stat(filename)
|
||||
@@ -96,41 +96,22 @@ func NewBufferFromFile(path string) (*Buffer, error) {
|
||||
var buf *Buffer
|
||||
if err != nil {
|
||||
// File does not exist -- create an empty buffer with that name
|
||||
buf = NewBufferFromString("", path)
|
||||
buf = NewBufferFromString("", filename)
|
||||
} else {
|
||||
buf = NewBuffer(file, FSize(file), path)
|
||||
buf = NewBuffer(file, FSize(file), filename, cursorPosition)
|
||||
}
|
||||
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
// NewBufferFromString creates a new buffer containing the given
|
||||
// string
|
||||
// NewBufferFromString creates a new buffer containing the given string
|
||||
func NewBufferFromString(text, path string) *Buffer {
|
||||
return NewBuffer(strings.NewReader(text), int64(len(text)), path)
|
||||
return NewBuffer(strings.NewReader(text), int64(len(text)), path, []string{"0", "0"})
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
func NewBuffer(reader io.Reader, size int64, path string, cursorPosition []string) *Buffer {
|
||||
cursorLocation, cursorLocationError := ParseCursorLocation(cursorPosition)
|
||||
|
||||
if path != "" {
|
||||
for _, tab := range tabs {
|
||||
@@ -178,15 +159,15 @@ func NewBuffer(reader io.Reader, size int64, path string) *Buffer {
|
||||
// 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 || !startposErr {
|
||||
// If -cursorLocation LINE,COL was passed, use start position LINE,COL
|
||||
if len(*flagStartPos) > 0 || cursorLocationError == nil {
|
||||
positions := strings.Split(*flagStartPos, ",")
|
||||
if len(positions) == 2 || !startposErr {
|
||||
if len(positions) == 2 || cursorLocationError == nil {
|
||||
var lineNum, colNum int
|
||||
var errPos1, errPos2 error
|
||||
if !startposErr {
|
||||
lineNum = startpos.Y
|
||||
colNum = startpos.X
|
||||
if cursorLocationError == nil {
|
||||
lineNum = cursorLocation.Y
|
||||
colNum = cursorLocation.X
|
||||
} else {
|
||||
lineNum, errPos1 = strconv.Atoi(positions[0])
|
||||
colNum, errPos2 = strconv.Atoi(positions[1])
|
||||
@@ -219,7 +200,7 @@ func NewBuffer(reader io.Reader, size int64, path string) *Buffer {
|
||||
|
||||
InitLocalSettings(b)
|
||||
|
||||
if startposErr && len(*flagStartPos) == 0 && (b.Settings["savecursor"].(bool) || b.Settings["saveundo"].(bool)) {
|
||||
if cursorLocationError != nil && 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))
|
||||
|
||||
Reference in New Issue
Block a user