diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index 396c414a..52f69518 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -51,10 +51,9 @@ func InitFlags() { optionFlags = make(map[string]*string) - for k, v := range config.DefaultGlobalSettings() { + for k, v := range config.DefaultAllSettings() { optionFlags[k] = flag.String(k, "", fmt.Sprintf("The %s option. Default value: '%v'.", k, v)) } - optionFlags["filetype"] = flag.String("filetype", "", fmt.Sprintf("The filetype option. Autodetected by default.")) flag.Parse() @@ -69,7 +68,7 @@ func InitFlags() { if *flagOptions { // If -options was passed var keys []string - m := config.DefaultGlobalSettings() + m := config.DefaultAllSettings() for k, _ := range m { keys = append(keys, k) } @@ -154,15 +153,13 @@ func main() { // flag options for k, v := range optionFlags { - if *v != "" && k != "filetype" { - nativeValue, err := config.GetNativeValue(k, config.GlobalSettings[k], *v) + if *v != "" { + nativeValue, err := config.GetNativeValue(k, config.DefaultAllSettings()[k], *v) if err != nil { screen.TermMessage(err) continue } config.GlobalSettings[k] = nativeValue - } else if k == "filetype" && *v != "" { - config.GlobalSettings[k] = *v } } @@ -199,8 +196,10 @@ func main() { action.InitTabs(b) action.InitGlobals() - if _, ok := config.GlobalSettings["filetype"]; ok { - delete(config.GlobalSettings, "filetype") + for _, s := range config.LocalSettings { + if _, ok := config.GlobalSettings[s]; ok { + delete(config.GlobalSettings, s) + } } // Here is the event loop which runs in a separate thread diff --git a/internal/buffer/buffer.go b/internal/buffer/buffer.go index 5e74ac5f..b7583799 100644 --- a/internal/buffer/buffer.go +++ b/internal/buffer/buffer.go @@ -196,6 +196,10 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT b.EventHandler = NewEventHandler(b.SharedBuffer, b.cursors) } + if b.Settings["readonly"].(bool) { + b.Type.Readonly = true + } + b.Path = path b.AbsPath = absPath diff --git a/internal/buffer/settings.go b/internal/buffer/settings.go index 1d9801ad..6b43bbb7 100644 --- a/internal/buffer/settings.go +++ b/internal/buffer/settings.go @@ -35,6 +35,8 @@ func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error { } } else if option == "encoding" { b.isModified = true + } else if option == "readonly" { + b.Type.Readonly = nativeValue.(bool) } return nil diff --git a/internal/config/settings.go b/internal/config/settings.go index 45c39634..a775a1c0 100644 --- a/internal/config/settings.go +++ b/internal/config/settings.go @@ -194,14 +194,27 @@ func DefaultGlobalSettings() map[string]interface{} { return common } +// LocalSettings is a list of the local only settings +var LocalSettings = []string{"filetype", "readonly"} + // 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 } +func DefaultAllSettings() map[string]interface{} { + global := DefaultGlobalSettings() + local := DefaultLocalSettings() + for k, v := range global { + local[k] = v + } + return local +} + func GetNativeValue(option string, realValue interface{}, value string) (interface{}, error) { var native interface{} kind := reflect.TypeOf(realValue).Kind()