X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=internal%2Fconfig%2Fsettings.go;h=8702bff17e5419be2f7501fe3ecb437fbda943fe;hb=bc6dd990e5632b0ebb8359979eb1445a3cc54ec2;hp=a775a1c0fd519db83feba6d162882e5d4d781d7e;hpb=55e33badd04e51f6f642c7ae0ae262213a6d0454;p=micro.git diff --git a/internal/config/settings.go b/internal/config/settings.go index a775a1c0..8702bff1 100644 --- a/internal/config/settings.go +++ b/internal/config/settings.go @@ -114,12 +114,31 @@ func WriteSettings(filename string) error { return err } -// AddOption creates a new option. This is meant to be called by plugins to add options. -func AddOption(name string, value interface{}) error { - GlobalSettings[name] = value - err := WriteSettings(ConfigDir + "/settings.json") - if err != nil { - return errors.New("Error writing settings.json file: " + err.Error()) +// RegisterCommonOption creates a new option. This is meant to be called by plugins to add options. +func RegisterCommonOption(name string, defaultvalue interface{}) error { + if v, ok := GlobalSettings[name]; !ok { + defaultCommonSettings[name] = defaultvalue + GlobalSettings[name] = defaultvalue + err := WriteSettings(ConfigDir + "/settings.json") + if err != nil { + return errors.New("Error writing settings.json file: " + err.Error()) + } + } else { + defaultCommonSettings[name] = v + } + return nil +} + +func RegisterGlobalOption(name string, defaultvalue interface{}) error { + if v, ok := GlobalSettings[name]; !ok { + defaultGlobalSettings[name] = defaultvalue + GlobalSettings[name] = defaultvalue + err := WriteSettings(ConfigDir + "/settings.json") + if err != nil { + return errors.New("Error writing settings.json file: " + err.Error()) + } + } else { + defaultGlobalSettings[name] = v } return nil } @@ -129,42 +148,43 @@ func GetGlobalOption(name string) interface{} { return GlobalSettings[name] } -func DefaultCommonSettings() map[string]interface{} { - return map[string]interface{}{ - "autoindent": true, - "autosave": false, - "basename": false, - "colorcolumn": float64(0), - "cursorline": true, - "encoding": "utf-8", - "eofnewline": false, - "fastdirty": true, - "fileformat": "unix", - "ignorecase": false, - "indentchar": " ", - "keepautoindent": false, - "matchbrace": false, - "matchbraceleft": false, - "rmtrailingws": false, - "ruler": true, - "savecursor": false, - "saveundo": false, - "scrollbar": false, - "scrollmargin": float64(3), - "scrollspeed": float64(2), - "smartpaste": true, - "softwrap": false, - "splitbottom": true, - "splitright": true, - "statusformatl": "$(filename) $(modified)($(line),$(col)) $(opt:filetype) $(opt:fileformat) $(opt:encoding)", - "statusformatr": "$(bind:ToggleKeyMenu): show bindings, $(bind:ToggleHelp): toggle help", - "statusline": true, - "syntax": true, - "tabmovement": false, - "tabsize": float64(4), - "tabstospaces": false, - "useprimary": true, - } +var defaultCommonSettings = map[string]interface{}{ + "autoindent": true, + "autosave": false, + "basename": false, + "colorcolumn": float64(0), + "cursorline": true, + "encoding": "utf-8", + "eofnewline": false, + "fastdirty": true, + "fileformat": "unix", + "filetype": "unknown", + "ignorecase": false, + "indentchar": " ", + "keepautoindent": false, + "matchbrace": false, + "matchbraceleft": false, + "mkparents": false, + "readonly": false, + "rmtrailingws": false, + "ruler": true, + "savecursor": false, + "saveundo": false, + "scrollbar": false, + "scrollmargin": float64(3), + "scrollspeed": float64(2), + "smartpaste": true, + "softwrap": false, + "splitbottom": true, + "splitright": true, + "statusformatl": "$(filename) $(modified)($(line),$(col)) $(opt:filetype) $(opt:fileformat) $(opt:encoding)", + "statusformatr": "$(bind:ToggleKeyMenu): show bindings, $(bind:ToggleHelp): toggle help", + "statusline": true, + "syntax": true, + "tabmovement": false, + "tabsize": float64(4), + "tabstospaces": false, + "useprimary": true, } func GetInfoBarOffset() int { @@ -178,41 +198,50 @@ func GetInfoBarOffset() int { return offset } -// DefaultGlobalSettings returns the default global settings for micro +// DefaultCommonSettings returns the default global settings for micro // Note that colorscheme is a global only option -func DefaultGlobalSettings() map[string]interface{} { - common := DefaultCommonSettings() - common["colorscheme"] = "default" - common["infobar"] = true - common["keymenu"] = false - common["mouse"] = true - common["pluginchannels"] = []string{"https://raw.githubusercontent.com/micro-editor/plugin-channel/master/channel.json"} - common["pluginrepos"] = []string{} - common["savehistory"] = true - common["sucmd"] = "sudo" - common["termtitle"] = false - return common +func DefaultCommonSettings() map[string]interface{} { + commonsettings := make(map[string]interface{}) + for k, v := range defaultCommonSettings { + commonsettings[k] = v + } + return commonsettings } -// LocalSettings is a list of the local only settings -var LocalSettings = []string{"filetype", "readonly"} +var defaultGlobalSettings = map[string]interface{}{ + "colorscheme": "default", + "infobar": true, + "keymenu": false, + "mouse": true, + "savehistory": true, + "sucmd": "sudo", + "termtitle": false, +} -// DefaultLocalSettings returns the default local settings -// Note that filetype is a local only option -func DefaultLocalSettings() map[string]interface{} { - common := DefaultCommonSettings() - common["filetype"] = "unknown" - common["readonly"] = false - return common +// DefaultGlobalSettings returns the default global settings for micro +// Note that colorscheme is a global only option +func DefaultGlobalSettings() map[string]interface{} { + globalsettings := make(map[string]interface{}) + for k, v := range defaultCommonSettings { + globalsettings[k] = v + } + for k, v := range defaultGlobalSettings { + globalsettings[k] = v + } + return globalsettings } +// DefaultAllSettings returns a map of all settings and their +// default values (both common and global settings) func DefaultAllSettings() map[string]interface{} { - global := DefaultGlobalSettings() - local := DefaultLocalSettings() - for k, v := range global { - local[k] = v + allsettings := make(map[string]interface{}) + for k, v := range defaultCommonSettings { + allsettings[k] = v + } + for k, v := range defaultGlobalSettings { + allsettings[k] = v } - return local + return allsettings } func GetNativeValue(option string, realValue interface{}, value string) (interface{}, error) {