12 // The options that the user can set
13 var settings map[string]interface{}
15 // InitSettings initializes the options map and sets all options to their default values
17 defaults := DefaultSettings()
18 var parsed map[string]interface{}
20 filename := configDir + "/settings.json"
21 if _, e := os.Stat(filename); e == nil {
22 input, err := ioutil.ReadFile(filename)
24 TermMessage("Error reading settings.json file: " + err.Error())
28 err = json.Unmarshal(input, &parsed)
30 TermMessage("Error reading settings.json:", err.Error())
34 settings = make(map[string]interface{})
35 for k, v := range defaults {
38 for k, v := range parsed {
42 err := WriteSettings(filename)
44 TermMessage("Error writing settings.json file: " + err.Error())
48 // WriteSettings writes the settings to the specified filename as JSON
49 func WriteSettings(filename string) error {
51 if _, e := os.Stat(configDir); e == nil {
52 txt, _ := json.MarshalIndent(settings, "", " ")
53 err = ioutil.WriteFile(filename, txt, 0644)
58 // AddOption creates a new option. This is meant to be called by plugins to add options.
59 func AddOption(name string, value interface{}) {
60 settings[name] = value
61 err := WriteSettings(configDir + "/settings.json")
63 TermMessage("Error writing settings.json file: " + err.Error())
67 // GetOption returns the specified option. This is meant to be called by plugins to add options.
68 func GetOption(name string) interface{} {
72 // DefaultSettings returns the default settings for micro
73 func DefaultSettings() map[string]interface{} {
74 return map[string]interface{}{
76 "colorscheme": "monokai",
83 "scrollspeed": float64(2),
84 "scrollmargin": float64(3),
87 "tabsize": float64(4),
88 "tabstospaces": false,
92 // SetOption attempts to set the given option to the value
93 func SetOption(option, value string) error {
94 if _, ok := settings[option]; !ok {
95 return errors.New("Invalid option")
98 kind := reflect.TypeOf(settings[option]).Kind()
99 if kind == reflect.Bool {
100 b, err := ParseBool(value)
102 return errors.New("Invalid value")
105 } else if kind == reflect.String {
106 settings[option] = value
107 } else if kind == reflect.Float64 {
108 i, err := strconv.Atoi(value)
110 return errors.New("Invalid value")
112 settings[option] = float64(i)
115 if option == "colorscheme" {
117 for _, tab := range tabs {
118 for _, view := range tab.views {
119 view.Buf.UpdateRules()
120 if settings["syntax"].(bool) {
121 view.matches = Match(view)
127 if option == "statusline" {
128 for _, tab := range tabs {
129 for _, view := range tab.views {
130 view.ToggleStatusLine()
131 if settings["syntax"].(bool) {
132 view.matches = Match(view)
141 // SetOptionAndSettings sets the given option and saves the option setting to the settings config file
142 func SetOptionAndSettings(option, value string) {
143 filename := configDir + "/settings.json"
145 err := SetOption(option, value)
148 messenger.Message(err.Error())
152 err = WriteSettings(filename)
154 messenger.Error("Error writing to settings.json: " + err.Error())