11 // The options that the user can set
12 var settings map[string]interface{}
14 // InitSettings initializes the options map and sets all options to their default values
16 defaults := DefaultSettings()
17 var parsed map[string]interface{}
19 filename := configDir + "/settings.json"
20 if _, e := os.Stat(filename); e == nil {
21 input, err := ioutil.ReadFile(filename)
23 TermMessage("Error reading settings.json file: " + err.Error())
27 err = json.Unmarshal(input, &parsed)
29 TermMessage("Error reading settings.json:", err.Error())
33 settings = make(map[string]interface{})
34 for k, v := range defaults {
37 for k, v := range parsed {
41 err := WriteSettings(filename)
43 TermMessage("Error writing settings.json file: " + err.Error())
47 // WriteSettings writes the settings to the specified filename as JSON
48 func WriteSettings(filename string) error {
50 if _, e := os.Stat(configDir); e == nil {
51 txt, _ := json.MarshalIndent(settings, "", " ")
52 err = ioutil.WriteFile(filename, txt, 0644)
57 // AddOption creates a new option. This is meant to be called by plugins to add options.
58 func AddOption(name string, value interface{}) {
59 settings[name] = value
60 err := WriteSettings(configDir + "/settings.json")
62 TermMessage("Error writing settings.json file: " + err.Error())
66 // GetOption returns the specified option. This is meant to be called by plugins to add options.
67 func GetOption(name string) interface{} {
71 // DefaultSettings returns the default settings for micro
72 func DefaultSettings() map[string]interface{} {
73 return map[string]interface{}{
75 "colorscheme": "monokai",
82 "scrollspeed": float64(2),
83 "scrollmargin": float64(3),
86 "tabsize": float64(4),
87 "tabstospaces": false,
91 // SetOption prompts the user to set an option and checks that the response is valid
92 func SetOption(option, value string) {
93 filename := configDir + "/settings.json"
94 if _, ok := settings[option]; !ok {
95 messenger.Error(option + " is not a valid option")
99 kind := reflect.TypeOf(settings[option]).Kind()
100 if kind == reflect.Bool {
101 b, err := ParseBool(value)
103 messenger.Error("Invalid value for " + option)
107 } else if kind == reflect.String {
108 settings[option] = value
109 } else if kind == reflect.Float64 {
110 i, err := strconv.Atoi(value)
112 messenger.Error("Invalid value for " + option)
115 settings[option] = float64(i)
118 if option == "colorscheme" {
120 for _, tab := range tabs {
121 for _, view := range tab.views {
122 view.Buf.UpdateRules()
123 if settings["syntax"].(bool) {
124 view.matches = Match(view)
130 if option == "statusline" {
131 for _, tab := range tabs {
132 for _, view := range tab.views {
133 view.ToggleStatusLine()
134 if settings["syntax"].(bool) {
135 view.matches = Match(view)
141 err := WriteSettings(filename)
143 messenger.Error("Error writing to settings.json: " + err.Error())