11 // The options that the user can set
14 // All the possible settings
15 var possibleSettings = []string{"colorscheme", "tabsize", "autoindent", "syntax", "tabsToSpaces", "ruler", "gofmt", "goimports"}
17 // The Settings struct contains the settings for micro
18 type Settings struct {
19 Colorscheme string `json:"colorscheme"`
20 TabSize int `json:"tabsize"`
21 AutoIndent bool `json:"autoindent"`
22 Syntax bool `json:"syntax"`
23 TabsToSpaces bool `json:"tabsToSpaces"`
24 Ruler bool `json:"ruler"`
25 GoFmt bool `json:"gofmt"`
26 GoImports bool `json:"goimports"`
29 // InitSettings initializes the options map and sets all options to their default values
31 filename := configDir + "/settings.json"
32 if _, e := os.Stat(filename); e == nil {
33 input, err := ioutil.ReadFile(filename)
35 TermMessage("Error reading settings.json file: " + err.Error())
39 err = json.Unmarshal(input, &settings)
41 TermMessage("Error reading settings.json:", err.Error())
44 settings = DefaultSettings()
45 err := WriteSettings(filename)
47 TermMessage("Error writing settings.json file: " + err.Error())
52 // WriteSettings writes the settings to the specified filename as JSON
53 func WriteSettings(filename string) error {
55 if _, e := os.Stat(configDir); e == nil {
56 txt, _ := json.MarshalIndent(settings, "", " ")
57 err = ioutil.WriteFile(filename, txt, 0644)
62 // DefaultSettings returns the default settings for micro
63 func DefaultSettings() Settings {
65 Colorscheme: "default",
76 // SetOption prompts the user to set an option and checks that the response is valid
77 func SetOption(view *View, args []string) {
78 filename := configDir + "/settings.json"
80 option := strings.TrimSpace(args[0])
81 value := strings.TrimSpace(args[1])
83 if Contains(possibleSettings, option) {
84 if option == "tabsize" {
85 tsize, err := strconv.Atoi(value)
87 messenger.Error("Invalid value for " + option)
90 settings.TabSize = tsize
91 } else if option == "colorscheme" {
92 settings.Colorscheme = value
94 view.buf.UpdateRules()
95 } else if option == "syntax" {
97 settings.Syntax = true
98 } else if value == "off" {
99 settings.Syntax = false
101 messenger.Error("Invalid value for " + option)
105 view.buf.UpdateRules()
106 } else if option == "tabsToSpaces" {
108 settings.TabsToSpaces = true
109 } else if value == "off" {
110 settings.TabsToSpaces = false
112 messenger.Error("Invalid value for " + option)
115 } else if option == "autoindent" {
117 settings.AutoIndent = true
118 } else if value == "off" {
119 settings.AutoIndent = false
121 messenger.Error("Invalid value for " + option)
124 } else if option == "ruler" {
126 settings.Ruler = true
127 } else if value == "off" {
128 settings.Ruler = false
130 messenger.Error("Invalid value for " + option)
133 } else if option == "gofmt" {
135 settings.GoFmt = true
136 } else if value == "off" {
137 settings.GoFmt = false
139 messenger.Error("Invalid value for " + option)
142 } else if option == "goimports" {
144 settings.GoFmt = false // goimports does gofmt
145 settings.GoImports = true
146 } else if value == "off" {
147 settings.GoFmt = false
149 messenger.Error("Invalid value for " + option)
154 err := WriteSettings(filename)
156 messenger.Error("Error writing to settings.json: " + err.Error())
160 messenger.Error("Option " + option + " does not exist")
163 messenger.Error("Invalid option, please use option value")