]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/settings.go
Fix some issues with unicode handling
[micro.git] / cmd / micro / settings.go
index d260f26b8353fdaff1725d48a42d3cc8556a08ef..4df44237eaf1400a5664f696024f6a76482fd424 100644 (file)
@@ -6,40 +6,16 @@ import (
        "os"
        "reflect"
        "strconv"
-       "strings"
 )
 
 // The options that the user can set
-var settings Settings
-
-// All the possible settings
-// This map maps the name of the setting in the Settings struct
-// to the name that the user will actually use (the one in the json file)
-var possibleSettings = map[string]string{
-       "colorscheme":  "Colorscheme",
-       "tabsize":      "TabSize",
-       "autoindent":   "AutoIndent",
-       "syntax":       "Syntax",
-       "tabsToSpaces": "TabsToSpaces",
-       "ruler":        "Ruler",
-       "gofmt":        "GoFmt",
-       "goimports":    "GoImports",
-}
-
-// The Settings struct contains the settings for micro
-type Settings struct {
-       Colorscheme  string `json:"colorscheme"`
-       TabSize      int    `json:"tabsize"`
-       AutoIndent   bool   `json:"autoindent"`
-       Syntax       bool   `json:"syntax"`
-       TabsToSpaces bool   `json:"tabsToSpaces"`
-       Ruler        bool   `json:"ruler"`
-       GoFmt        bool   `json:"gofmt"`
-       GoImports    bool   `json:"goimports"`
-}
+var settings map[string]interface{}
 
 // InitSettings initializes the options map and sets all options to their default values
 func InitSettings() {
+       defaults := DefaultSettings()
+       var parsed map[string]interface{}
+
        filename := configDir + "/settings.json"
        if _, e := os.Stat(filename); e == nil {
                input, err := ioutil.ReadFile(filename)
@@ -48,16 +24,23 @@ func InitSettings() {
                        return
                }
 
-               err = json.Unmarshal(input, &settings)
+               err = json.Unmarshal(input, &parsed)
                if err != nil {
                        TermMessage("Error reading settings.json:", err.Error())
                }
-       } else {
-               settings = DefaultSettings()
-               err := WriteSettings(filename)
-               if err != nil {
-                       TermMessage("Error writing settings.json file: " + err.Error())
-               }
+       }
+
+       settings = make(map[string]interface{})
+       for k, v := range defaults {
+               settings[k] = v
+       }
+       for k, v := range parsed {
+               settings[k] = v
+       }
+
+       err := WriteSettings(filename)
+       if err != nil {
+               TermMessage("Error writing settings.json file: " + err.Error())
        }
 }
 
@@ -71,63 +54,93 @@ func WriteSettings(filename string) error {
        return err
 }
 
+// AddOption creates a new option. This is meant to be called by plugins to add options.
+func AddOption(name string, value interface{}) {
+       settings[name] = value
+       err := WriteSettings(configDir + "/settings.json")
+       if err != nil {
+               TermMessage("Error writing settings.json file: " + err.Error())
+       }
+}
+
+// GetOption returns the specified option. This is meant to be called by plugins to add options.
+func GetOption(name string) interface{} {
+       return settings[name]
+}
+
 // DefaultSettings returns the default settings for micro
-func DefaultSettings() Settings {
-       return Settings{
-               Colorscheme:  "default",
-               TabSize:      4,
-               AutoIndent:   true,
-               Syntax:       true,
-               TabsToSpaces: false,
-               Ruler:        true,
-               GoFmt:        false,
-               GoImports:    false,
+func DefaultSettings() map[string]interface{} {
+       return map[string]interface{}{
+               "autoindent":   true,
+               "colorscheme":  "monokai",
+               "cursorline":   false,
+               "ignorecase":   false,
+               "indentchar":   " ",
+               "ruler":        true,
+               "savecursor":   false,
+               "saveundo":     false,
+               "scrollspeed":  float64(2),
+               "scrollmargin": float64(3),
+               "statusline":   true,
+               "syntax":       true,
+               "tabsize":      float64(4),
+               "tabstospaces": false,
        }
 }
 
 // SetOption prompts the user to set an option and checks that the response is valid
-func SetOption(view *View, args []string) {
+func SetOption(option, value string) {
        filename := configDir + "/settings.json"
-       if len(args) == 2 {
-               option := strings.TrimSpace(args[0])
-               value := strings.TrimSpace(args[1])
-
-               mutable := reflect.ValueOf(&settings).Elem()
-               field := mutable.FieldByName(possibleSettings[option])
-               if !field.IsValid() {
-                       messenger.Error(option + " is not a valid option")
+       if _, ok := settings[option]; !ok {
+               messenger.Error(option + " is not a valid option")
+               return
+       }
+
+       kind := reflect.TypeOf(settings[option]).Kind()
+       if kind == reflect.Bool {
+               b, err := ParseBool(value)
+               if err != nil {
+                       messenger.Error("Invalid value for " + option)
                        return
                }
-               kind := field.Type().Kind()
-               if kind == reflect.Bool {
-                       b, err := ParseBool(value)
-                       if err != nil {
-                               messenger.Error("Invalid value for " + option)
-                               return
-                       }
-                       field.SetBool(b)
-               } else if kind == reflect.String {
-                       field.SetString(value)
-               } else if kind == reflect.Int {
-                       i, err := strconv.Atoi(value)
-                       if err != nil {
-                               messenger.Error("Invalid value for " + option)
-                               return
-                       }
-                       field.SetInt(int64(i))
+               settings[option] = b
+       } else if kind == reflect.String {
+               settings[option] = value
+       } else if kind == reflect.Float64 {
+               i, err := strconv.Atoi(value)
+               if err != nil {
+                       messenger.Error("Invalid value for " + option)
+                       return
                }
+               settings[option] = float64(i)
+       }
 
-               if option == "colorscheme" {
-                       LoadSyntaxFiles()
-                       view.buf.UpdateRules()
+       if option == "colorscheme" {
+               LoadSyntaxFiles()
+               for _, tab := range tabs {
+                       for _, view := range tab.views {
+                               view.Buf.UpdateRules()
+                               if settings["syntax"].(bool) {
+                                       view.matches = Match(view)
+                               }
+                       }
                }
+       }
 
-               err := WriteSettings(filename)
-               if err != nil {
-                       messenger.Error("Error writing to settings.json: " + err.Error())
-                       return
+       if option == "statusline" {
+               for _, tab := range tabs {
+                       for _, view := range tab.views {
+                               view.ToggleStatusLine()
+                               if settings["syntax"].(bool) {
+                                       view.matches = Match(view)
+                               }
+                       }
                }
-       } else {
-               messenger.Error("No value given")
+       }
+
+       err := WriteSettings(filename)
+       if err != nil {
+               messenger.Error("Error writing to settings.json: " + err.Error())
+               return
        }
 }