From 4a53419c6261f2d4b243f2e46ee4f7fe75f92a84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6ran=20Karl?= <3951388+JoeKar@users.noreply.github.com> Date: Fri, 15 Mar 2024 18:46:51 +0100 Subject: [PATCH] option: Don't apply rmtrailingws in case of timed autosave (#2850) --- cmd/micro/micro.go | 2 +- internal/buffer/save.go | 13 +++++++++---- runtime/help/options.md | 5 ++++- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index c45e12bb..80709bba 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -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: diff --git a/internal/buffer/save.go b/internal/buffer/save.go index 0a10a2dc..fbafa6a8 100644 --- a/internal/buffer/save.go +++ b/internal/buffer/save.go @@ -93,9 +93,14 @@ 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 { @@ -103,10 +108,10 @@ func (b *Buffer) SaveWithSudo() error { } 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") @@ -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)) diff --git a/runtime/help/options.md b/runtime/help/options.md index be515875..c0e1a753 100644 --- a/runtime/help/options.md +++ b/runtime/help/options.md @@ -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`