]> git.lizzy.rs Git - micro.git/commitdiff
Fix save callbacks
authorZachary Yedidia <zyedidia@gmail.com>
Sun, 9 Feb 2020 21:36:15 +0000 (16:36 -0500)
committerZachary Yedidia <zyedidia@gmail.com>
Sun, 9 Feb 2020 21:36:15 +0000 (16:36 -0500)
Fixes #1491

internal/action/actions.go

index 783bffad94464d9e22c1cda8f02ab92b87673d61..44f0595f66d487190f58a4f5883946ffb6646908 100644 (file)
@@ -659,23 +659,27 @@ func (h *BufPane) SaveAll() bool {
        return true
 }
 
-// Save the buffer to disk
-func (h *BufPane) Save() bool {
+// SaveCB performs a save and does a callback at the very end (after all prompts have been resolved)
+func (h *BufPane) SaveCB(action string, callback func()) bool {
        // If this is an empty buffer, ask for a filename
        if h.Buf.Path == "" {
-               h.SaveAs()
+               h.SaveAsCB(action, callback)
        } else {
-               noPrompt := h.saveBufToFile(h.Buf.Path, "Save")
+               noPrompt := h.saveBufToFile(h.Buf.Path, action, callback)
                if noPrompt {
                        return true
                }
        }
-
        return false
 }
 
-// SaveAs saves the buffer to disk with the given name
-func (h *BufPane) SaveAs() bool {
+// Save the buffer to disk
+func (h *BufPane) Save() bool {
+       return h.SaveCB("Save", nil)
+}
+
+// SaveAsCB performs a save as and does a callback at the very end (after all prompts have been resolved)
+func (h *BufPane) SaveAsCB(action string, callback func()) bool {
        InfoBar.Prompt("Filename: ", "", "Save", nil, func(resp string, canceled bool) {
                if !canceled {
                        // the filename might or might not be quoted, so unquote first then join the strings.
@@ -689,18 +693,23 @@ func (h *BufPane) SaveAs() bool {
                                return
                        }
                        filename := strings.Join(args, " ")
-                       noPrompt := h.saveBufToFile(filename, "SaveAs")
+                       noPrompt := h.saveBufToFile(filename, action, callback)
                        if noPrompt {
-                               h.completeAction("SaveAs")
+                               h.completeAction(action)
                        }
                }
        })
        return false
 }
 
+// SaveAs saves the buffer to disk with the given name
+func (h *BufPane) SaveAs() bool {
+       return h.SaveAsCB("SaveAs", nil)
+}
+
 // This function saves the buffer to `filename` and changes the buffer's path and name
 // to `filename` if the save is successful
-func (h *BufPane) saveBufToFile(filename string, action string) bool {
+func (h *BufPane) saveBufToFile(filename string, action string, callback func()) bool {
        err := h.Buf.SaveAs(filename)
        if err != nil {
                if strings.HasSuffix(err.Error(), "permission denied") {
@@ -716,6 +725,9 @@ func (h *BufPane) saveBufToFile(filename string, action string) bool {
                                        }
                                        h.completeAction(action)
                                }
+                               if callback != nil {
+                                       callback()
+                               }
                        })
                        return false
                } else {
@@ -726,6 +738,9 @@ func (h *BufPane) saveBufToFile(filename string, action string) bool {
                h.Buf.SetName(filename)
                InfoBar.Message("Saved " + filename)
        }
+       if callback != nil {
+               callback()
+       }
        return true
 }
 
@@ -1280,15 +1295,17 @@ func (h *BufPane) Quit() bool {
        if h.Buf.Modified() {
                if config.GlobalSettings["autosave"].(float64) > 0 {
                        // autosave on means we automatically save when quitting
-                       h.Save()
-                       quit()
+                       h.SaveCB("Quit", func() {
+                               quit()
+                       })
                } else {
                        InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) {
                                if !canceled && !yes {
                                        quit()
                                } else if !canceled && yes {
-                                       h.Save()
-                                       quit()
+                                       h.SaveCB("Quit", func() {
+                                               quit()
+                                       })
                                }
                        })
                }