]> git.lizzy.rs Git - micro.git/commitdiff
Enable autosave option
authorZachary Yedidia <zyedidia@gmail.com>
Sat, 8 Feb 2020 21:53:08 +0000 (16:53 -0500)
committerZachary Yedidia <zyedidia@gmail.com>
Sat, 8 Feb 2020 21:53:08 +0000 (16:53 -0500)
The autosave option is now specified as an integer, which denotes
the number of seconds to wait between saving the file. If the option
is 0, then autosaving is disabled. If the option is given by the user
as a boolean, it will be converted to 8 if true, and 0 if false.

Fixes #1479

internal/action/actions.go
internal/action/command.go
internal/buffer/buffer.go
internal/config/settings.go

index 3db9af31dec6130e7a7e1e4ba3e1dffbc9993ce4..3f0d6ee1a58a00bbf4e88d36bbe35d5d7faa52da 100644 (file)
@@ -1278,20 +1278,20 @@ 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()
-               // } 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()
-                       }
-               })
-               // }
+               if config.GlobalSettings["autosave"].(float64) > 0 {
+                       // autosave on means we automatically save when quitting
+                       h.Save()
+                       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()
+                               }
+                       })
+               }
        } else {
                quit()
        }
index 94383bf74f8c586c766b6de4a1e345079c44d0e7..724a8d69edd77045e1121fe1e0c2c1a12e889166 100644 (file)
@@ -442,14 +442,13 @@ func SetGlobalOptionNative(option string, nativeValue interface{}) error {
                        } else {
                                screen.Screen.EnableMouse()
                        }
-                       // autosave option has been removed
-                       // } else if option == "autosave" {
-                       //      if nativeValue.(float64) > 0 {
-                       //              config.SetAutoTime(int(nativeValue.(float64)))
-                       //              config.StartAutoSave()
-                       //      } else {
-                       //              config.SetAutoTime(0)
-                       //      }
+               } else if option == "autosave" {
+                       if nativeValue.(float64) > 0 {
+                               config.SetAutoTime(int(nativeValue.(float64)))
+                               config.StartAutoSave()
+                       } else {
+                               config.SetAutoTime(0)
+                       }
                } else if option == "paste" {
                        screen.Screen.SetPaste(nativeValue.(bool))
                } else {
index 48b30e04a55aa2fd1213c532517f8c83bb45f915..903aa78e6369433b0ff1d534cb4c4b2981281621 100644 (file)
@@ -6,6 +6,7 @@ import (
        "errors"
        "io"
        "io/ioutil"
+       "log"
        "os"
        "path/filepath"
        "strconv"
@@ -527,6 +528,7 @@ func (b *Buffer) UpdateRules() {
        if syntaxFile == "" {
                // search for the syntax file in the user's custom syntax files
                for _, f := range config.ListRealRuntimeFiles(config.RTSyntax) {
+                       log.Println("real runtime file", f.Name())
                        data, err := f.Data()
                        if err != nil {
                                screen.TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
@@ -540,7 +542,7 @@ func (b *Buffer) UpdateRules() {
                                continue
                        }
 
-                       if (ft == "unknown" || ft == "" && highlight.MatchFiletype(header.FtDetect, b.Path, b.lines[0].data)) || header.FileType == ft {
+                       if ((ft == "unknown" || ft == "") && highlight.MatchFiletype(header.FtDetect, b.Path, b.lines[0].data)) || header.FileType == ft {
                                syndef, err := highlight.ParseDef(file, header)
                                if err != nil {
                                        screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
index f11443fe29eb5fa1bda9293bd30abe5a89161aa3..e8f37e012cb55ef6aa06464e34aa425eebd02aa0 100644 (file)
@@ -35,7 +35,7 @@ func init() {
 
 // Options with validators
 var optionValidators = map[string]optionValidator{
-       // "autosave":     validateNonNegativeValue,
+       "autosave":     validateNonNegativeValue,
        "tabsize":      validatePositiveValue,
        "scrollmargin": validateNonNegativeValue,
        "scrollspeed":  validateNonNegativeValue,
@@ -58,6 +58,18 @@ func ReadSettings() error {
                        if err != nil {
                                return errors.New("Error reading settings.json: " + err.Error())
                        }
+
+                       // check if autosave is a boolean and convert it to float if so
+                       if v, ok := parsedSettings["autosave"]; ok {
+                               s, ok := v.(bool)
+                               if ok {
+                                       if s {
+                                               parsedSettings["autosave"] = 8.0
+                                       } else {
+                                               parsedSettings["autosave"] = 0.0
+                                       }
+                               }
+                       }
                }
        }
        return nil
@@ -232,7 +244,7 @@ func DefaultCommonSettings() map[string]interface{} {
 // a list of settings that should only be globally modified and their
 // default values
 var defaultGlobalSettings = map[string]interface{}{
-       // "autosave":    float64(0),
+       "autosave":       float64(0),
        "colorscheme":    "default",
        "infobar":        true,
        "keymenu":        false,