13 "github.com/zyedidia/glob"
14 "github.com/zyedidia/json5"
15 "github.com/zyedidia/micro/internal/util"
16 "golang.org/x/text/encoding/htmlindex"
19 type optionValidator func(string, interface{}) error
22 ErrInvalidOption = errors.New("Invalid option")
23 ErrInvalidValue = errors.New("Invalid value")
25 // The options that the user can set
26 GlobalSettings map[string]interface{}
28 // This is the raw parsed json
29 parsedSettings map[string]interface{}
33 parsedSettings = make(map[string]interface{})
36 // Options with validators
37 var optionValidators = map[string]optionValidator{
38 "autosave": validateNonNegativeValue,
39 "tabsize": validatePositiveValue,
40 "scrollmargin": validateNonNegativeValue,
41 "scrollspeed": validateNonNegativeValue,
42 "colorscheme": validateColorscheme,
43 "colorcolumn": validateNonNegativeValue,
44 "fileformat": validateLineEnding,
45 "encoding": validateEncoding,
48 func ReadSettings() error {
49 filename := filepath.Join(ConfigDir, "settings.json")
50 if _, e := os.Stat(filename); e == nil {
51 input, err := ioutil.ReadFile(filename)
53 return errors.New("Error reading settings.json file: " + err.Error())
55 if !strings.HasPrefix(string(input), "null") {
56 // Unmarshal the input into the parsed map
57 err = json5.Unmarshal(input, &parsedSettings)
59 return errors.New("Error reading settings.json: " + err.Error())
62 // check if autosave is a boolean and convert it to float if so
63 if v, ok := parsedSettings["autosave"]; ok {
67 parsedSettings["autosave"] = 8.0
69 parsedSettings["autosave"] = 0.0
78 // InitGlobalSettings initializes the options map and sets all options to their default values
79 // Must be called after ReadSettings
80 func InitGlobalSettings() {
81 GlobalSettings = DefaultGlobalSettings()
83 for k, v := range parsedSettings {
84 if !strings.HasPrefix(reflect.TypeOf(v).String(), "map") {
90 // InitLocalSettings scans the json in settings.json and sets the options locally based
91 // on whether the filetype or path matches ft or glob local settings
92 // Must be called after ReadSettings
93 func InitLocalSettings(settings map[string]interface{}, path string) error {
95 for k, v := range parsedSettings {
96 if strings.HasPrefix(reflect.TypeOf(v).String(), "map") {
97 if strings.HasPrefix(k, "ft:") {
98 if settings["filetype"].(string) == k[3:] {
99 for k1, v1 := range v.(map[string]interface{}) {
104 g, err := glob.Compile(k)
106 parseError = errors.New("Error with glob setting " + k + ": " + err.Error())
110 if g.MatchString(path) {
111 for k1, v1 := range v.(map[string]interface{}) {
121 // WriteSettings writes the settings to the specified filename as JSON
122 func WriteSettings(filename string) error {
124 if _, e := os.Stat(ConfigDir); e == nil {
125 for k, v := range GlobalSettings {
126 parsedSettings[k] = v
129 txt, _ := json.MarshalIndent(parsedSettings, "", " ")
130 err = ioutil.WriteFile(filename, append(txt, '\n'), 0644)
135 func OverwriteSettings(filename string) error {
137 if _, e := os.Stat(ConfigDir); e == nil {
138 txt, _ := json.MarshalIndent(GlobalSettings, "", " ")
139 err = ioutil.WriteFile(filename, append(txt, '\n'), 0644)
144 // RegisterCommonOptionPlug creates a new option (called pl.name). This is meant to be called by plugins to add options.
145 func RegisterCommonOptionPlug(pl string, name string, defaultvalue interface{}) error {
146 name = pl + "." + name
147 if v, ok := GlobalSettings[name]; !ok {
148 defaultCommonSettings[name] = defaultvalue
149 GlobalSettings[name] = defaultvalue
150 err := WriteSettings(filepath.Join(ConfigDir, "/settings.json"))
152 return errors.New("Error writing settings.json file: " + err.Error())
155 defaultCommonSettings[name] = v
160 // RegisterGlobalOptionPlug creates a new global-only option (named pl.name)
161 func RegisterGlobalOptionPlug(pl string, name string, defaultvalue interface{}) error {
162 return RegisterGlobalOption(pl+"."+name, defaultvalue)
165 // RegisterGlobalOption creates a new global-only option
166 func RegisterGlobalOption(name string, defaultvalue interface{}) error {
167 if v, ok := GlobalSettings[name]; !ok {
168 defaultGlobalSettings[name] = defaultvalue
169 GlobalSettings[name] = defaultvalue
170 err := WriteSettings(filepath.Join(ConfigDir, "settings.json"))
172 return errors.New("Error writing settings.json file: " + err.Error())
175 defaultGlobalSettings[name] = v
180 // GetGlobalOption returns the global value of the given option
181 func GetGlobalOption(name string) interface{} {
182 return GlobalSettings[name]
185 var defaultCommonSettings = map[string]interface{}{
189 "colorcolumn": float64(0),
194 "fileformat": "unix",
195 "filetype": "unknown",
198 "keepautoindent": false,
202 "rmtrailingws": false,
207 "scrollmargin": float64(3),
208 "scrollspeed": float64(2),
213 "statusformatl": "$(filename) $(modified)($(line),$(col)) $(status.paste)| ft:$(opt:filetype) | $(opt:fileformat) | $(opt:encoding)",
214 "statusformatr": "$(bind:ToggleKeyMenu): bindings, $(bind:ToggleHelp): help",
217 "tabmovement": false,
218 "tabsize": float64(4),
219 "tabstospaces": false,
223 func GetInfoBarOffset() int {
225 if GetGlobalOption("infobar").(bool) {
228 if GetGlobalOption("keymenu").(bool) {
234 // DefaultCommonSettings returns the default global settings for micro
235 // Note that colorscheme is a global only option
236 func DefaultCommonSettings() map[string]interface{} {
237 commonsettings := make(map[string]interface{})
238 for k, v := range defaultCommonSettings {
239 commonsettings[k] = v
241 return commonsettings
244 // a list of settings that should only be globally modified and their
246 var defaultGlobalSettings = map[string]interface{}{
247 "autosave": float64(0),
248 "colorscheme": "default",
255 "pluginchannels": []string{"https://raw.githubusercontent.com/micro-editor/plugin-channel/master/channel.json"},
256 "pluginrepos": []string{},
259 // a list of settings that should never be globally modified
260 var LocalSettings = []string{
265 // DefaultGlobalSettings returns the default global settings for micro
266 // Note that colorscheme is a global only option
267 func DefaultGlobalSettings() map[string]interface{} {
268 globalsettings := make(map[string]interface{})
269 for k, v := range defaultCommonSettings {
270 globalsettings[k] = v
272 for k, v := range defaultGlobalSettings {
273 globalsettings[k] = v
275 return globalsettings
278 // DefaultAllSettings returns a map of all settings and their
279 // default values (both common and global settings)
280 func DefaultAllSettings() map[string]interface{} {
281 allsettings := make(map[string]interface{})
282 for k, v := range defaultCommonSettings {
285 for k, v := range defaultGlobalSettings {
291 // GetNativeValue parses and validates a value for a given option
292 func GetNativeValue(option string, realValue interface{}, value string) (interface{}, error) {
293 var native interface{}
294 kind := reflect.TypeOf(realValue).Kind()
295 if kind == reflect.Bool {
296 b, err := util.ParseBool(value)
298 return nil, ErrInvalidValue
301 } else if kind == reflect.String {
303 } else if kind == reflect.Float64 {
304 i, err := strconv.Atoi(value)
306 return nil, ErrInvalidValue
310 return nil, ErrInvalidValue
313 if err := OptionIsValid(option, native); err != nil {
319 // OptionIsValid checks if a value is valid for a certain option
320 func OptionIsValid(option string, value interface{}) error {
321 if validator, ok := optionValidators[option]; ok {
322 return validator(option, value)
330 func validatePositiveValue(option string, value interface{}) error {
331 tabsize, ok := value.(float64)
334 return errors.New("Expected numeric type for " + option)
338 return errors.New(option + " must be greater than 0")
344 func validateNonNegativeValue(option string, value interface{}) error {
345 nativeValue, ok := value.(float64)
348 return errors.New("Expected numeric type for " + option)
352 return errors.New(option + " must be non-negative")
358 func validateColorscheme(option string, value interface{}) error {
359 colorscheme, ok := value.(string)
362 return errors.New("Expected string type for colorscheme")
365 if !ColorschemeExists(colorscheme) {
366 return errors.New(colorscheme + " is not a valid colorscheme")
372 func validateLineEnding(option string, value interface{}) error {
373 endingType, ok := value.(string)
376 return errors.New("Expected string type for file format")
379 if endingType != "unix" && endingType != "dos" {
380 return errors.New("File format must be either 'unix' or 'dos'")
386 func validateEncoding(option string, value interface{}) error {
387 _, err := htmlindex.Get(value.(string))