From c4d5d7c195e2947785d875913b3019cbea3a3353 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sat, 21 Dec 2019 23:26:53 -0500 Subject: [PATCH] Better backup behavior --- cmd/micro/micro.go | 4 ++++ internal/buffer/backup.go | 12 +++++++----- internal/buffer/buffer.go | 5 +++-- runtime/help/options.md | 11 +++++++++++ 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index 53c88bc5..c01a063f 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -190,6 +190,10 @@ func main() { if err := recover(); err != nil { screen.Screen.Fini() fmt.Println("Micro encountered an error:", err) + // backup all open buffers + for _, b := range buffer.OpenBuffers { + b.Backup(false) + } // Print the stack trace too fmt.Print(errors.Wrap(err, 2).ErrorStack()) os.Exit(1) diff --git a/internal/buffer/backup.go b/internal/buffer/backup.go index 6f6978bd..32e1b63c 100644 --- a/internal/buffer/backup.go +++ b/internal/buffer/backup.go @@ -25,15 +25,17 @@ The backup was created at %s. Options: [r]ecover, [i]gnore: ` // Backup saves the current buffer to ConfigDir/backups -func (b *Buffer) Backup() error { +func (b *Buffer) Backup(checkTime bool) error { if !b.Settings["backup"].(bool) { return nil } - sub := time.Now().Sub(b.lastbackup) - if sub < time.Duration(backup_time)*time.Millisecond { - log.Println("Backup event but not enough time has passed", sub) - return nil + if checkTime { + sub := time.Now().Sub(b.lastbackup) + if sub < time.Duration(backup_time)*time.Millisecond { + log.Println("Backup event but not enough time has passed", sub) + return nil + } } b.lastbackup = time.Now() diff --git a/internal/buffer/buffer.go b/internal/buffer/buffer.go index b85677b8..e518c7d2 100644 --- a/internal/buffer/buffer.go +++ b/internal/buffer/buffer.go @@ -217,6 +217,7 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT if choice%2 == 0 { // recover b.LineArray = NewLineArray(uint64(size), FFAuto, backup) + b.isModified = true } else if choice%2 == 1 { // delete os.Remove(backupfile) @@ -334,7 +335,7 @@ func (b *Buffer) Insert(start Loc, text string) { b.EventHandler.active = b.curCursor b.EventHandler.Insert(start, text) - go b.Backup() + go b.Backup(true) } } @@ -344,7 +345,7 @@ func (b *Buffer) Remove(start, end Loc) { b.EventHandler.active = b.curCursor b.EventHandler.Remove(start, end) - go b.Backup() + go b.Backup(true) } } diff --git a/runtime/help/options.md b/runtime/help/options.md index 6afe0081..9670f940 100644 --- a/runtime/help/options.md +++ b/runtime/help/options.md @@ -22,6 +22,17 @@ Here are the options that you can set: default value: `0` +* `backup`: micro will automatically keep backups of all open buffers. Backups + are stored in `~/.config/micro/backups` and are removed when the buffer is + closed cleanly. In the case of a system crash or a micro crash, the contents + of the buffer can be recovered automatically by opening the file that + was being edited before the crash, or manually by searching for the backup + in the backup directory. Backups are made in the background when a buffer is + modified and the latest backup is more than 8 seconds old, or when micro + detects a crash. It is highly recommended that you leave this feature enabled. + + default value: `true` + * `basename`: in the infobar, show only the basename of the file being edited rather than the full path.