From 4aa706cbc5480121dc003f8f675f6db996a0400a Mon Sep 17 00:00:00 2001 From: Dmytro Maluka Date: Tue, 10 Feb 2026 20:32:16 +0100 Subject: [PATCH] serialize: Don't save undo stack if saveundo=off (#4003) Micro always saves the undo stack information into the serialized buffer file as long as either `saveundo` or `savecursor` is enabled. Whereas in the case when only `savecursor` is enabled, while `saveundo` is disabled, this information is not used afterwards, so it only wastes the disk space in `~/.config/micro/buffers`. (And given that currently micro never automatically removes any serialized buffer files, it may significantly contribute to the overall ever growing size of the `~/.config/micro/buffers` directory.) So avoid saving the undo info if `saveundo` is disabled. This makes the size of each serialized buffer file with savecursor=on saveundo=off small and predictable, e.g. around 600 bytes in my observations (whereas without this fix, it may grow indefinitely big, depending on the number of modifications the user made before saving the file). --- internal/buffer/serialize.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/internal/buffer/serialize.go b/internal/buffer/serialize.go index e358eb3d..008f0e45 100644 --- a/internal/buffer/serialize.go +++ b/internal/buffer/serialize.go @@ -29,12 +29,16 @@ func (b *Buffer) Serialize() error { return nil } + buffer := SerializedBuffer{ + Cursor: b.GetActiveCursor().Loc, + ModTime: b.ModTime, + } + if b.Settings["saveundo"].(bool) { + buffer.EventHandler = b.EventHandler + } + var buf bytes.Buffer - err := gob.NewEncoder(&buf).Encode(SerializedBuffer{ - b.EventHandler, - b.GetActiveCursor().Loc, - b.ModTime, - }) + err := gob.NewEncoder(&buf).Encode(buffer) if err != nil { return err } @@ -76,7 +80,7 @@ func (b *Buffer) Unserialize() error { b.StartCursor = buffer.Cursor } - if b.Settings["saveundo"].(bool) { + if b.Settings["saveundo"].(bool) && buffer.EventHandler != nil { // We should only use last time's eventhandler if the file wasn't modified by someone else in the meantime if b.ModTime == buffer.ModTime { b.EventHandler = buffer.EventHandler