mirror of
https://github.com/zyedidia/micro.git
synced 2026-02-05 06:30:28 +09:00
Add readonly option
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user