Add eofnewline option

Closes #429

Enable with '> set eofnewline on'
This commit is contained in:
Zachary Yedidia
2016-10-23 18:37:29 -04:00
parent ee553b7830
commit 63ccbc1ebd
4 changed files with 24 additions and 2 deletions

View File

@@ -269,7 +269,14 @@ func (b *Buffer) SaveAs(filename string) error {
b.UpdateRules()
b.Name = filename
b.Path = filename
data := []byte(b.String())
str := b.String()
if b.Settings["eofnewline"].(bool) {
end := b.End()
if b.RuneAt(Loc{end.X - 1, end.Y}) != '\n' {
b.Insert(end, "\n")
}
}
data := []byte(str)
err := ioutil.WriteFile(filename, data, 0644)
if err == nil {
b.IsModified = false
@@ -352,6 +359,15 @@ func (b *Buffer) End() Loc {
return Loc{utf8.RuneCount(b.lines[b.NumLines-1]), b.NumLines - 1}
}
// RuneAt returns the rune at a given location in the buffer
func (b *Buffer) RuneAt(loc Loc) rune {
line := []rune(b.Line(loc.Y))
if len(line) > 0 {
return line[loc.X]
}
return '\n'
}
// Line returns a single line
func (b *Buffer) Line(n int) string {
if n >= len(b.lines) {

File diff suppressed because one or more lines are too long

View File

@@ -180,6 +180,7 @@ func DefaultGlobalSettings() map[string]interface{} {
"colorcolumn": float64(0),
"colorscheme": "default",
"cursorline": true,
"eofnewline": false,
"ignorecase": false,
"indentchar": " ",
"infobar": true,
@@ -208,6 +209,7 @@ func DefaultLocalSettings() map[string]interface{} {
"autosave": false,
"colorcolumn": float64(0),
"cursorline": true,
"eofnewline": false,
"filetype": "Unknown",
"ignorecase": false,
"indentchar": " ",

View File

@@ -28,6 +28,10 @@ Here are the options that you can set:
default value: `0`
* `eofnewline`: micro will automatically add a newline to the file.
default value: `false`
* `tabsize`: sets the tab size to `option`
default value: `4`