X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=internal%2Fconfig%2Fsettings.go;h=b7cdaa691250fd15c8f8024d0738060b69469aff;hb=f39a916e5fc4d6ed6a1290723d5946f6fff84485;hp=2d0787a9fb10f72fe5a7cf1c5ff3b6d46fc8f814;hpb=7cd5024e34e99a5d53e7d6beaec7810d9948df6b;p=micro.git diff --git a/internal/config/settings.go b/internal/config/settings.go index 2d0787a9..b7cdaa69 100644 --- a/internal/config/settings.go +++ b/internal/config/settings.go @@ -30,6 +30,7 @@ var ( // Options with validators var optionValidators = map[string]optionValidator{ + "autosave": validateNonNegativeValue, "tabsize": validatePositiveValue, "scrollmargin": validateNonNegativeValue, "scrollspeed": validateNonNegativeValue, @@ -114,12 +115,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,41 +149,42 @@ 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", - "hidehelp": false, - "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), - "softwrap": false, - "smartpaste": true, - "splitbottom": true, - "splitright": true, - "statusline": true, - "syntax": true, - "tabmovement": false, - "tabsize": float64(4), - "tabstospaces": false, - "useprimary": true, - } +var defaultCommonSettings = map[string]interface{}{ + "autoindent": true, + "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 { @@ -177,28 +198,51 @@ func GetInfoBarOffset() int { return offset } +// DefaultCommonSettings returns the default global settings for micro +// Note that colorscheme is a global only option +func DefaultCommonSettings() map[string]interface{} { + commonsettings := make(map[string]interface{}) + for k, v := range defaultCommonSettings { + commonsettings[k] = v + } + return commonsettings +} + +var defaultGlobalSettings = map[string]interface{}{ + "autosave": float64(0), + "colorscheme": "default", + "infobar": true, + "keymenu": false, + "mouse": true, + "savehistory": true, + "sucmd": "sudo", + "termtitle": false, +} + // DefaultGlobalSettings 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 + globalsettings := make(map[string]interface{}) + for k, v := range defaultCommonSettings { + globalsettings[k] = v + } + for k, v := range defaultGlobalSettings { + globalsettings[k] = v + } + return globalsettings } -// DefaultLocalSettings returns the default local settings -// Note that filetype is a local only option -func DefaultLocalSettings() map[string]interface{} { - common := DefaultCommonSettings() - common["filetype"] = "unknown" - return common +// DefaultAllSettings returns a map of all settings and their +// default values (both common and global settings) +func DefaultAllSettings() map[string]interface{} { + allsettings := make(map[string]interface{}) + for k, v := range defaultCommonSettings { + allsettings[k] = v + } + for k, v := range defaultGlobalSettings { + allsettings[k] = v + } + return allsettings } func GetNativeValue(option string, realValue interface{}, value string) (interface{}, error) {