Add support for switching between crlf and lf

Dos and Unix line endings are now both supported (previously on unix
line endings were supported) and can be accessed via the `fileformat`
option. The file format will be automatically detected and displayed in
the statusline but can be overriden.

Possible values for the `fileformat` option are `dos` and `unix`.

Closes #443
Closes #755
This commit is contained in:
Zachary Yedidia
2017-08-24 13:13:14 -04:00
parent 69c6d8a099
commit 9628b73525
5 changed files with 69 additions and 3 deletions

View File

@@ -19,6 +19,13 @@ import (
"github.com/zyedidia/micro/cmd/micro/highlight"
)
var (
// 0 - no line type detected
// 1 - lf detected
// 2 - crlf detected
fileformat = 0
)
// Buffer stores the text for files that are loaded into the text editor
// It uses a rope to efficiently store the string and contains some
// simple functions for saving and wrapper functions for modifying the rope
@@ -88,6 +95,12 @@ func NewBuffer(reader io.Reader, size int64, path string) *Buffer {
}
}
if fileformat == 1 {
b.Settings["fileformat"] = "unix"
} else if fileformat == 2 {
b.Settings["fileformat"] = "dos"
}
absPath, _ := filepath.Abs(path)
b.Path = path
@@ -355,7 +368,7 @@ func (b *Buffer) Serialize() error {
b.ModTime,
})
}
file.Close()
err = file.Close()
return err
}
return nil
@@ -383,7 +396,7 @@ func (b *Buffer) SaveAs(filename string) error {
b.Insert(end, "\n")
}
}
str := b.String()
str := b.SaveString(b.Settings["fileformat"] == "dos")
data := []byte(str)
err := ioutil.WriteFile(filename, data, 0644)
if err == nil {
@@ -408,7 +421,7 @@ func (b *Buffer) SaveAsWithSudo(filename string) error {
// Set up everything for the command
cmd := exec.Command("sudo", "tee", filename)
cmd.Stdin = bytes.NewBufferString(b.String())
cmd.Stdin = bytes.NewBufferString(b.SaveString(b.Settings["fileformat"] == "dos"))
// This is a trap for Ctrl-C so that it doesn't kill micro
// Instead we trap Ctrl-C to kill the program we're running