Skip to content

Commit

Permalink
option: Don't apply rmtrailingws in case of timed autosave (#2850)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeKar authored Mar 15, 2024
1 parent 8af304c commit 4a53419
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/micro/micro.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func DoEvent() {
f.Function(f.Output, f.Args)
case <-config.Autosave:
for _, b := range buffer.OpenBuffers {
b.Save()
b.AutoSave()
}
case <-shell.CloseTerms:
case event = <-screen.Events:
Expand Down
13 changes: 9 additions & 4 deletions internal/buffer/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,25 @@ func (b *Buffer) Save() error {
return b.SaveAs(b.Path)
}

// AutoSave saves the buffer to its default path
func (b *Buffer) AutoSave() error {
return b.saveToFile(b.Path, false, true)
}

// SaveAs saves the buffer to a specified path (filename), creating the file if it does not exist
func (b *Buffer) SaveAs(filename string) error {
return b.saveToFile(filename, false)
return b.saveToFile(filename, false, false)
}

func (b *Buffer) SaveWithSudo() error {
return b.SaveAsWithSudo(b.Path)
}

func (b *Buffer) SaveAsWithSudo(filename string) error {
return b.saveToFile(filename, true)
return b.saveToFile(filename, true, false)
}

func (b *Buffer) saveToFile(filename string, withSudo bool) error {
func (b *Buffer) saveToFile(filename string, withSudo bool, autoSave bool) error {
var err error
if b.Type.Readonly {
return errors.New("Cannot save readonly buffer")
Expand All @@ -118,7 +123,7 @@ func (b *Buffer) saveToFile(filename string, withSudo bool) error {
return errors.New("Save with sudo not supported on Windows")
}

if b.Settings["rmtrailingws"].(bool) {
if !autoSave && b.Settings["rmtrailingws"].(bool) {
for i, l := range b.lines {
leftover := util.CharacterCount(bytes.TrimRightFunc(l.data, unicode.IsSpace))

Expand Down
5 changes: 4 additions & 1 deletion runtime/help/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,10 @@ Here are the available options:
default value: `prompt`

* `rmtrailingws`: micro will automatically trim trailing whitespaces at ends of
lines. Note: This setting overrides `keepautoindent`
lines.
Note: This setting overrides `keepautoindent` and isn't used at timed `autosave`
or forced `autosave` in case the buffer didn't change. A manual save will
involve the action regardless if the buffer has been changed or not.

default value: `false`

Expand Down

0 comments on commit 4a53419

Please sign in to comment.