12 // The options that the user can set
13 var globalSettings 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 globalSettings = 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(globalSettings, "", " ")
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 globalSettings[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 GetGlobalOption(name string) interface{} {
69 return globalSettings[name]
72 func GetLocalOption(name string, buf *Buffer) interface{} {
73 return buf.Settings[name]
76 func GetOption(name string) interface{} {
77 if GetLocalOption(name, CurView().Buf) != nil {
78 return GetLocalOption(name, CurView().Buf)
80 return GetGlobalOption(name)
83 // DefaultSettings returns the default settings for micro
84 func DefaultSettings() map[string]interface{} {
85 return map[string]interface{}{
87 "colorscheme": "monokai",
89 "filetype": "Unknown",
95 "scrollspeed": float64(2),
96 "scrollmargin": float64(3),
99 "tabsize": float64(4),
100 "tabstospaces": false,
104 // SetOption attempts to set the given option to the value
105 func SetGlobalOption(option, value string) error {
106 if _, ok := globalSettings[option]; !ok {
107 return errors.New("Invalid option")
110 kind := reflect.TypeOf(globalSettings[option]).Kind()
111 if kind == reflect.Bool {
112 b, err := ParseBool(value)
114 return errors.New("Invalid value")
116 globalSettings[option] = b
117 } else if kind == reflect.String {
118 globalSettings[option] = value
119 } else if kind == reflect.Float64 {
120 i, err := strconv.Atoi(value)
122 return errors.New("Invalid value")
124 globalSettings[option] = float64(i)
127 for _, tab := range tabs {
128 for _, view := range tab.views {
129 SetLocalOption(option, value, view)
136 func SetLocalOption(option, value string, view *View) error {
138 if _, ok := buf.Settings[option]; !ok {
139 return errors.New("Invalid option")
142 kind := reflect.TypeOf(buf.Settings[option]).Kind()
143 if kind == reflect.Bool {
144 b, err := ParseBool(value)
146 return errors.New("Invalid value")
148 buf.Settings[option] = b
149 } else if kind == reflect.String {
150 buf.Settings[option] = value
151 } else if kind == reflect.Float64 {
152 i, err := strconv.Atoi(value)
154 return errors.New("Invalid value")
156 buf.Settings[option] = float64(i)
159 if option == "colorscheme" {
162 if buf.Settings["syntax"].(bool) {
163 view.matches = Match(view)
167 if option == "statusline" {
168 view.ToggleStatusLine()
169 if buf.Settings["syntax"].(bool) {
170 view.matches = Match(view)
177 // SetOptionAndSettings sets the given option and saves the option setting to the settings config file
178 func SetOptionAndSettings(option, value string) {
179 filename := configDir + "/settings.json"
181 err := SetGlobalOption(option, value)
184 messenger.Message(err.Error())
188 err = WriteSettings(filename)
190 messenger.Error("Error writing to settings.json: " + err.Error())