Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/micro/micro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/micro-editor/micro/v2/internal/buffer"
"github.com/micro-editor/micro/v2/internal/config"
"github.com/micro-editor/micro/v2/internal/screen"
"github.com/micro-editor/micro/v2/internal/util"
"github.com/micro-editor/tcell/v2"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -157,8 +158,9 @@ func openFile(file string) {

func findBuffer(file string) *buffer.Buffer {
var buf *buffer.Buffer
file = util.ResolvePath(file)
for _, b := range buffer.OpenBuffers {
if b.Path == file {
if b.AbsPath == file {
buf = b
}
}
Expand Down
17 changes: 10 additions & 7 deletions internal/buffer/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,9 @@ func NewBufferFromString(text, path string, btype BufType) *Buffer {
// Places the cursor at startcursor. If startcursor is -1, -1 places the
// cursor at an autodetected location (based on savecursor or :LINE:COL)
func NewBuffer(r io.Reader, size int64, path string, btype BufType, cmd Command) *Buffer {
absPath, err := filepath.Abs(path)
if err != nil {
absPath = path
absPath := path
if btype == BTDefault && path != "" {
absPath = util.ResolvePath(path)
}

b := new(Buffer)
Expand Down Expand Up @@ -391,6 +391,7 @@ func NewBuffer(r io.Reader, size int64, path string, btype BufType, cmd Command)
}
config.UpdatePathGlobLocals(b.Settings, absPath)

var err error
b.encoding, err = htmlindex.Get(b.Settings["encoding"].(string))
if err != nil {
b.encoding = unicode.UTF8
Expand Down Expand Up @@ -489,7 +490,7 @@ func NewBuffer(r io.Reader, size int64, path string, btype BufType, cmd Command)
}
}

err = config.RunPluginFn("onBufferOpen", luar.New(ulua.L, b))
err := config.RunPluginFn("onBufferOpen", luar.New(ulua.L, b))
if err != nil {
screen.TermMessage(err)
}
Expand Down Expand Up @@ -524,10 +525,12 @@ func (b *Buffer) Close() {
// Fini should be called when a buffer is closed and performs
// some cleanup
func (b *Buffer) Fini() {
if !b.Modified() {
b.Serialize()
if !b.Shared() {
if !b.Modified() {
b.Serialize()
}
b.CancelBackup()
}
b.CancelBackup()

if b.Type == BTStdout {
fmt.Fprint(util.Stdout, string(b.Bytes()))
Expand Down
5 changes: 1 addition & 4 deletions internal/buffer/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,7 @@ func (b *Buffer) saveToFile(filename string, withSudo bool, autoSave bool) error
return errors.New("Error: " + filename + " is not a regular file and cannot be saved")
}

absFilename, err := filepath.Abs(filename)
if err != nil {
return err
}
absFilename := util.ResolvePath(filename)

// Get the leading path to the file | "." is returned if there's no leading path provided
if dirname := filepath.Dir(absFilename); dirname != "." {
Expand Down
32 changes: 32 additions & 0 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,38 @@ func DetermineEscapePath(dir string, path string) (string, string) {
return url, ""
}

// ResolvePath provides the absolute file path for the given relative file path
// as well as resolves symlinks. If it fails to get the absolute path or to
// resolve symlinks, it returns unresolved path in place of resolved one.
func ResolvePath(path string) string {
absPath, err := filepath.Abs(path)
if err != nil {
absPath = path
}

var remainder []string
for {
resolvedPath, err := filepath.EvalSymlinks(absPath)
if err == nil {
absPath = resolvedPath
break
} else if errors.Is(err, fs.ErrNotExist) {
remainder = append([]string{filepath.Base(absPath)}, remainder...)
absPath = filepath.Dir(absPath)
continue
}
break
}

if len(remainder) > 0 {
remainder = append([]string{absPath}, remainder...)
absPath = filepath.Join(remainder...)
absPath = filepath.Clean(absPath)
}

return absPath
}

// GetLeadingWhitespace returns the leading whitespace of the given byte array
func GetLeadingWhitespace(b []byte) []byte {
ws := []byte{}
Expand Down
Loading