]> git.lizzy.rs Git - micro.git/blob - cmd/micro/settings.go
Fix some issues with unicode handling
[micro.git] / cmd / micro / settings.go
1 package main
2
3 import (
4         "encoding/json"
5         "io/ioutil"
6         "os"
7         "reflect"
8         "strconv"
9 )
10
11 // The options that the user can set
12 var settings map[string]interface{}
13
14 // InitSettings initializes the options map and sets all options to their default values
15 func InitSettings() {
16         defaults := DefaultSettings()
17         var parsed map[string]interface{}
18
19         filename := configDir + "/settings.json"
20         if _, e := os.Stat(filename); e == nil {
21                 input, err := ioutil.ReadFile(filename)
22                 if err != nil {
23                         TermMessage("Error reading settings.json file: " + err.Error())
24                         return
25                 }
26
27                 err = json.Unmarshal(input, &parsed)
28                 if err != nil {
29                         TermMessage("Error reading settings.json:", err.Error())
30                 }
31         }
32
33         settings = make(map[string]interface{})
34         for k, v := range defaults {
35                 settings[k] = v
36         }
37         for k, v := range parsed {
38                 settings[k] = v
39         }
40
41         err := WriteSettings(filename)
42         if err != nil {
43                 TermMessage("Error writing settings.json file: " + err.Error())
44         }
45 }
46
47 // WriteSettings writes the settings to the specified filename as JSON
48 func WriteSettings(filename string) error {
49         var err error
50         if _, e := os.Stat(configDir); e == nil {
51                 txt, _ := json.MarshalIndent(settings, "", "    ")
52                 err = ioutil.WriteFile(filename, txt, 0644)
53         }
54         return err
55 }
56
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")
61         if err != nil {
62                 TermMessage("Error writing settings.json file: " + err.Error())
63         }
64 }
65
66 // GetOption returns the specified option. This is meant to be called by plugins to add options.
67 func GetOption(name string) interface{} {
68         return settings[name]
69 }
70
71 // DefaultSettings returns the default settings for micro
72 func DefaultSettings() map[string]interface{} {
73         return map[string]interface{}{
74                 "autoindent":   true,
75                 "colorscheme":  "monokai",
76                 "cursorline":   false,
77                 "ignorecase":   false,
78                 "indentchar":   " ",
79                 "ruler":        true,
80                 "savecursor":   false,
81                 "saveundo":     false,
82                 "scrollspeed":  float64(2),
83                 "scrollmargin": float64(3),
84                 "statusline":   true,
85                 "syntax":       true,
86                 "tabsize":      float64(4),
87                 "tabstospaces": false,
88         }
89 }
90
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")
96                 return
97         }
98
99         kind := reflect.TypeOf(settings[option]).Kind()
100         if kind == reflect.Bool {
101                 b, err := ParseBool(value)
102                 if err != nil {
103                         messenger.Error("Invalid value for " + option)
104                         return
105                 }
106                 settings[option] = b
107         } else if kind == reflect.String {
108                 settings[option] = value
109         } else if kind == reflect.Float64 {
110                 i, err := strconv.Atoi(value)
111                 if err != nil {
112                         messenger.Error("Invalid value for " + option)
113                         return
114                 }
115                 settings[option] = float64(i)
116         }
117
118         if option == "colorscheme" {
119                 LoadSyntaxFiles()
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)
125                                 }
126                         }
127                 }
128         }
129
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)
136                                 }
137                         }
138                 }
139         }
140
141         err := WriteSettings(filename)
142         if err != nil {
143                 messenger.Error("Error writing to settings.json: " + err.Error())
144                 return
145         }
146 }