diff --git a/cmd/micro/buffer/buffer.go b/cmd/micro/buffer/buffer.go index b585e688..e9faf019 100644 --- a/cmd/micro/buffer/buffer.go +++ b/cmd/micro/buffer/buffer.go @@ -163,6 +163,13 @@ func NewBuffer(reader io.Reader, size int64, path string, cursorPosition []strin b.EventHandler = NewEventHandler(b.SharedBuffer, b.cursors) } + switch b.Endings { + case FFUnix: + b.Settings["fileformat"] = "unix" + case FFDos: + b.Settings["fileformat"] = "dos" + } + b.Path = path b.AbsPath = absPath diff --git a/cmd/micro/buffer/line_array.go b/cmd/micro/buffer/line_array.go index 43d86100..64352c11 100644 --- a/cmd/micro/buffer/line_array.go +++ b/cmd/micro/buffer/line_array.go @@ -53,7 +53,7 @@ type FileFormat byte // and delete in it type LineArray struct { lines []Line - endings FileFormat + Endings FileFormat initsize uint64 } @@ -95,11 +95,12 @@ func NewLineArray(size uint64, endings FileFormat, reader io.Reader) *LineArray if dlen > 1 && data[dlen-2] == '\r' { data = append(data[:dlen-2], '\n') if endings == FFAuto { - la.endings = FFDos + la.Endings = FFDos } + dlen = len(data) } else if dlen > 0 { if endings == FFAuto { - la.endings = FFUnix + la.Endings = FFUnix } } @@ -143,7 +144,7 @@ func (la *LineArray) Bytes() []byte { for i, l := range la.lines { str = append(str, l.data...) if i != len(la.lines)-1 { - if la.endings == FFDos { + if la.Endings == FFDos { str = append(str, '\r') } str = append(str, '\n') diff --git a/cmd/micro/buffer/save.go b/cmd/micro/buffer/save.go index b8672dd7..d8318499 100644 --- a/cmd/micro/buffer/save.go +++ b/cmd/micro/buffer/save.go @@ -107,7 +107,7 @@ func (b *Buffer) SaveAs(filename string) error { // end of line var eol []byte - if b.Settings["fileformat"] == "dos" { + if b.Endings == FFDos { eol = []byte{'\r', '\n'} } else { eol = []byte{'\n'} diff --git a/cmd/micro/buffer/settings.go b/cmd/micro/buffer/settings.go index fb3b5486..d7144ca9 100644 --- a/cmd/micro/buffer/settings.go +++ b/cmd/micro/buffer/settings.go @@ -30,6 +30,12 @@ func (b *Buffer) SetOption(option, value string) error { } else if option == "filetype" { b.UpdateRules() } else if option == "fileformat" { + switch b.Settings["fileformat"].(string) { + case "unix": + b.Endings = FFUnix + case "dos": + b.Endings = FFDos + } b.isModified = true } else if option == "syntax" { if !nativeValue.(bool) {