]> git.lizzy.rs Git - micro.git/commitdiff
Allow aborting while opening a file with backup
authorZachary Yedidia <zyedidia@gmail.com>
Tue, 3 Aug 2021 01:05:22 +0000 (21:05 -0400)
committerZachary Yedidia <zyedidia@gmail.com>
Tue, 3 Aug 2021 01:05:22 +0000 (21:05 -0400)
Also fixes an issue where the abort prompt consumes interrupt signals.

Fixes #2151

cmd/micro/micro.go
internal/buffer/backup.go
internal/buffer/buffer.go

index 5427b7073288fd704da2eea71169349a237c7a68..e05902440ba2148c20459edb7d343c1eb1b3b122 100644 (file)
@@ -274,12 +274,6 @@ func main() {
                fmt.Println("Fatal: Micro could not initialize a Screen.")
                os.Exit(1)
        }
-
-       sigterm = make(chan os.Signal, 1)
-       sighup = make(chan os.Signal, 1)
-       signal.Notify(sigterm, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
-       signal.Notify(sighup, syscall.SIGHUP)
-
        m := clipboard.SetMethod(config.GetGlobalOption("clipboard").(string))
        clipErr := clipboard.Initialize(m)
 
@@ -353,6 +347,11 @@ func main() {
 
        screen.Events = make(chan tcell.Event)
 
+       sigterm = make(chan os.Signal, 1)
+       sighup = make(chan os.Signal, 1)
+       signal.Notify(sigterm, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
+       signal.Notify(sighup, syscall.SIGHUP)
+
        // Here is the event loop which runs in a separate thread
        go func() {
                for {
index 7ea9dbb2a39bc103ea3823c417c9b0d43bfe6432..a043651eba6cd847ca0c9cd22231d08665e2eb0b 100644 (file)
@@ -26,8 +26,9 @@ The backup was created on %s, and the file is
   When the buffer is closed, the backup will be removed.
 * 'ignore' will ignore the backup, discarding its changes. The backup file
   will be removed.
+* 'abort' will abort the open operation, and instead open an empty buffer.
 
-Options: [r]ecover, [i]gnore: `
+Options: [r]ecover, [i]gnore, [a]bort: `
 
 var backupRequestChan chan *Buffer
 
@@ -118,7 +119,7 @@ func (b *Buffer) RemoveBackup() {
 
 // ApplyBackup applies the corresponding backup file to this buffer (if one exists)
 // Returns true if a backup was applied
-func (b *Buffer) ApplyBackup(fsize int64) bool {
+func (b *Buffer) ApplyBackup(fsize int64) (bool, bool) {
        if b.Settings["backup"].(bool) && !b.Settings["permbackup"].(bool) && len(b.Path) > 0 && b.Type == BTDefault {
                backupfile := filepath.Join(config.ConfigDir, "backups", util.EscapePath(b.AbsPath))
                if info, err := os.Stat(backupfile); err == nil {
@@ -127,20 +128,22 @@ func (b *Buffer) ApplyBackup(fsize int64) bool {
                                defer backup.Close()
                                t := info.ModTime()
                                msg := fmt.Sprintf(backupMsg, t.Format("Mon Jan _2 at 15:04, 2006"), util.EscapePath(b.AbsPath))
-                               choice := screen.TermPrompt(msg, []string{"r", "i", "recover", "ignore"}, true)
+                               choice := screen.TermPrompt(msg, []string{"r", "i", "a", "recover", "ignore", "abort"}, true)
 
-                               if choice%2 == 0 {
+                               if choice%3 == 0 {
                                        // recover
                                        b.LineArray = NewLineArray(uint64(fsize), FFAuto, backup)
                                        b.isModified = true
-                                       return true
-                               } else if choice%2 == 1 {
+                                       return true, true
+                               } else if choice%3 == 1 {
                                        // delete
                                        os.Remove(backupfile)
+                               } else if choice%3 == 2 {
+                                       return false, false
                                }
                        }
                }
        }
 
-       return false
+       return false, true
 }
index 99921dfae254203b21f393e7a5462589a2ac3e81..188161f2f4aac34c3286fd08df4272319ae9e034 100644 (file)
@@ -250,6 +250,9 @@ func NewBufferFromFileAtLoc(path string, btype BufType, cursorLoc Loc) (*Buffer,
                return nil, err
        } else {
                buf = NewBuffer(file, util.FSize(file), filename, cursorLoc, btype)
+               if buf == nil {
+                       return nil, errors.New("could not open file")
+               }
        }
 
        if readonly && prompt != nil {
@@ -333,8 +336,12 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT
                        b.Settings["encoding"] = "utf-8"
                }
 
-               hasBackup = b.ApplyBackup(size)
+               var ok bool
+               hasBackup, ok = b.ApplyBackup(size)
 
+               if !ok {
+                       return NewBufferFromString("", "", btype)
+               }
                if !hasBackup {
                        reader := bufio.NewReader(transform.NewReader(r, enc.NewDecoder()))