Add support for local or global only settings

This commit is contained in:
Zachary Yedidia 2016-08-25 15:03:37 -04:00
parent 348922e1f2
commit 1a9123630b
5 changed files with 68 additions and 18 deletions

View file

@ -91,11 +91,17 @@ func HelpComplete(input string) (string, []string) {
// OptionComplete autocompletes options
func OptionComplete(input string) (string, []string) {
var suggestions []string
localSettings := DefaultLocalSettings()
for option := range globalSettings {
if strings.HasPrefix(option, input) {
suggestions = append(suggestions, option)
}
}
for option := range localSettings {
if strings.HasPrefix(option, input) {
suggestions = append(suggestions, option)
}
}
var chosen string
if len(suggestions) == 1 {

View file

@ -57,9 +57,11 @@ func NewBuffer(txt []byte, path string) *Buffer {
b := new(Buffer)
b.LineArray = NewLineArray(txt)
b.Settings = make(map[string]interface{})
b.Settings = DefaultLocalSettings()
for k, v := range globalSettings {
b.Settings[k] = v
if _, ok := b.Settings[k]; ok {
b.Settings[k] = v
}
}
b.Path = path

View file

@ -185,7 +185,10 @@ func SetLocal(args []string) {
option := strings.TrimSpace(args[0])
value := strings.TrimSpace(args[1])
SetLocalOption(option, value, CurView())
err := SetLocalOption(option, value, CurView())
if err != nil {
messenger.Error(err.Error())
}
}
// Bind creates a new keybinding

View file

@ -266,7 +266,7 @@ func main() {
L.SetGlobal("messenger", luar.New(L, messenger))
L.SetGlobal("GetOption", luar.New(L, GetOption))
L.SetGlobal("AddOption", luar.New(L, AddOption))
L.SetGlobal("SetOption", luar.New(L, SetGlobalOption))
L.SetGlobal("SetOption", luar.New(L, SetOption))
L.SetGlobal("BindKey", luar.New(L, BindKey))
L.SetGlobal("MakeCommand", luar.New(L, MakeCommand))
L.SetGlobal("CurView", luar.New(L, CurView))

View file

@ -14,7 +14,7 @@ var globalSettings map[string]interface{}
// InitSettings initializes the options map and sets all options to their default values
func InitSettings() {
defaults := DefaultSettings()
defaults := DefaultGlobalSettings()
var parsed map[string]interface{}
filename := configDir + "/settings.json"
@ -81,11 +81,29 @@ func GetOption(name string) interface{} {
}
// DefaultSettings returns the default settings for micro
func DefaultSettings() map[string]interface{} {
func DefaultGlobalSettings() map[string]interface{} {
return map[string]interface{}{
"autoindent": true,
"colorscheme": "monokai",
"cursorline": false,
"ignorecase": false,
"indentchar": " ",
"ruler": true,
"savecursor": false,
"saveundo": false,
"scrollspeed": float64(2),
"scrollmargin": float64(3),
"statusline": true,
"syntax": true,
"tabsize": float64(4),
"tabstospaces": false,
}
}
func DefaultLocalSettings() map[string]interface{} {
return map[string]interface{}{
"autoindent": true,
"cursorline": false,
"filetype": "Unknown",
"ignorecase": false,
"indentchar": " ",
@ -102,9 +120,16 @@ func DefaultSettings() map[string]interface{} {
}
// SetOption attempts to set the given option to the value
func SetGlobalOption(option, value string) error {
// By default it will set the option as global, but if the option
// is local only it will set the local version
// Use setlocal to force an option to be set locally
func SetOption(option, value string) error {
if _, ok := globalSettings[option]; !ok {
return errors.New("Invalid option")
if _, ok := CurView().Buf.Settings[option]; !ok {
return errors.New("Invalid option")
}
SetLocalOption(option, value, CurView())
return nil
}
kind := reflect.TypeOf(globalSettings[option]).Kind()
@ -124,9 +149,23 @@ func SetGlobalOption(option, value string) error {
globalSettings[option] = float64(i)
}
for _, tab := range tabs {
for _, view := range tab.views {
SetLocalOption(option, value, view)
if option == "colorscheme" {
LoadSyntaxFiles()
for _, tab := range tabs {
for _, view := range tab.views {
view.Buf.UpdateRules()
if view.Buf.Settings["syntax"].(bool) {
view.matches = Match(view)
}
}
}
}
if _, ok := CurView().Buf.Settings[option]; ok {
for _, tab := range tabs {
for _, view := range tab.views {
SetLocalOption(option, value, view)
}
}
}
@ -156,16 +195,16 @@ func SetLocalOption(option, value string, view *View) error {
buf.Settings[option] = float64(i)
}
if option == "colorscheme" {
LoadSyntaxFiles()
buf.UpdateRules()
if option == "statusline" {
view.ToggleStatusLine()
if buf.Settings["syntax"].(bool) {
view.matches = Match(view)
}
}
if option == "statusline" {
view.ToggleStatusLine()
if option == "filetype" {
LoadSyntaxFiles()
buf.UpdateRules()
if buf.Settings["syntax"].(bool) {
view.matches = Match(view)
}
@ -178,10 +217,10 @@ func SetLocalOption(option, value string, view *View) error {
func SetOptionAndSettings(option, value string) {
filename := configDir + "/settings.json"
err := SetGlobalOption(option, value)
err := SetOption(option, value)
if err != nil {
messenger.Message(err.Error())
messenger.Error(err.Error())
return
}