mirror of
https://github.com/zyedidia/micro.git
synced 2025-06-19 15:25:42 -04:00
Merge pull request #3662 from JoeKar/fix/reload-settings
Some checks failed
Build and Test / test (1.19.x, macos-latest) (push) Has been cancelled
Build and Test / test (1.19.x, ubuntu-latest) (push) Has been cancelled
Build and Test / test (1.19.x, windows-latest) (push) Has been cancelled
Build and Test / test (1.23.x, macos-latest) (push) Has been cancelled
Build and Test / test (1.23.x, ubuntu-latest) (push) Has been cancelled
Build and Test / test (1.23.x, windows-latest) (push) Has been cancelled
Some checks failed
Build and Test / test (1.19.x, macos-latest) (push) Has been cancelled
Build and Test / test (1.19.x, ubuntu-latest) (push) Has been cancelled
Build and Test / test (1.19.x, windows-latest) (push) Has been cancelled
Build and Test / test (1.23.x, macos-latest) (push) Has been cancelled
Build and Test / test (1.23.x, ubuntu-latest) (push) Has been cancelled
Build and Test / test (1.23.x, windows-latest) (push) Has been cancelled
buffer: Fix `ReloadSettings(true)` for volatile `filetype`
This commit is contained in:
commit
c93747926d
@ -1794,12 +1794,12 @@ func (h *BufPane) HalfPageDown() bool {
|
|||||||
|
|
||||||
// ToggleDiffGutter turns the diff gutter off and on
|
// ToggleDiffGutter turns the diff gutter off and on
|
||||||
func (h *BufPane) ToggleDiffGutter() bool {
|
func (h *BufPane) ToggleDiffGutter() bool {
|
||||||
if !h.Buf.Settings["diffgutter"].(bool) {
|
diffgutter := !h.Buf.Settings["diffgutter"].(bool)
|
||||||
h.Buf.Settings["diffgutter"] = true
|
h.Buf.SetOptionNative("diffgutter", diffgutter)
|
||||||
|
if diffgutter {
|
||||||
h.Buf.UpdateDiff()
|
h.Buf.UpdateDiff()
|
||||||
InfoBar.Message("Enabled diff gutter")
|
InfoBar.Message("Enabled diff gutter")
|
||||||
} else {
|
} else {
|
||||||
h.Buf.Settings["diffgutter"] = false
|
|
||||||
InfoBar.Message("Disabled diff gutter")
|
InfoBar.Message("Disabled diff gutter")
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
@ -1807,11 +1807,11 @@ func (h *BufPane) ToggleDiffGutter() bool {
|
|||||||
|
|
||||||
// ToggleRuler turns line numbers off and on
|
// ToggleRuler turns line numbers off and on
|
||||||
func (h *BufPane) ToggleRuler() bool {
|
func (h *BufPane) ToggleRuler() bool {
|
||||||
if !h.Buf.Settings["ruler"].(bool) {
|
ruler := !h.Buf.Settings["ruler"].(bool)
|
||||||
h.Buf.Settings["ruler"] = true
|
h.Buf.SetOptionNative("ruler", ruler)
|
||||||
|
if ruler {
|
||||||
InfoBar.Message("Enabled ruler")
|
InfoBar.Message("Enabled ruler")
|
||||||
} else {
|
} else {
|
||||||
h.Buf.Settings["ruler"] = false
|
|
||||||
InfoBar.Message("Disabled ruler")
|
InfoBar.Message("Disabled ruler")
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
@ -325,28 +325,17 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT
|
|||||||
b.AbsPath = absPath
|
b.AbsPath = absPath
|
||||||
b.Path = path
|
b.Path = path
|
||||||
|
|
||||||
// this is a little messy since we need to know some settings to read
|
|
||||||
// the file properly, but some settings depend on the filetype, which
|
|
||||||
// we don't know until reading the file. We first read the settings
|
|
||||||
// into a local variable and then use that to determine the encoding,
|
|
||||||
// readonly, and fileformat necessary for reading the file and
|
|
||||||
// assigning the filetype.
|
|
||||||
settings := config.DefaultCommonSettings()
|
|
||||||
b.Settings = config.DefaultCommonSettings()
|
b.Settings = config.DefaultCommonSettings()
|
||||||
b.LocalSettings = make(map[string]bool)
|
b.LocalSettings = make(map[string]bool)
|
||||||
for k, v := range config.GlobalSettings {
|
for k, v := range config.GlobalSettings {
|
||||||
if _, ok := config.DefaultGlobalOnlySettings[k]; !ok {
|
if _, ok := config.DefaultGlobalOnlySettings[k]; !ok {
|
||||||
// make sure setting is not global-only
|
// make sure setting is not global-only
|
||||||
settings[k] = v
|
|
||||||
b.Settings[k] = v
|
b.Settings[k] = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
config.InitLocalSettings(settings, absPath)
|
config.UpdatePathGlobLocals(b.Settings, absPath)
|
||||||
b.Settings["readonly"] = settings["readonly"]
|
|
||||||
b.Settings["filetype"] = settings["filetype"]
|
|
||||||
b.Settings["syntax"] = settings["syntax"]
|
|
||||||
|
|
||||||
enc, err := htmlindex.Get(settings["encoding"].(string))
|
enc, err := htmlindex.Get(b.Settings["encoding"].(string))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
enc = unicode.UTF8
|
enc = unicode.UTF8
|
||||||
b.Settings["encoding"] = "utf-8"
|
b.Settings["encoding"] = "utf-8"
|
||||||
@ -366,7 +355,7 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT
|
|||||||
if size == 0 {
|
if size == 0 {
|
||||||
// for empty files, use the fileformat setting instead of
|
// for empty files, use the fileformat setting instead of
|
||||||
// autodetection
|
// autodetection
|
||||||
switch settings["fileformat"] {
|
switch b.Settings["fileformat"] {
|
||||||
case "unix":
|
case "unix":
|
||||||
ff = FFUnix
|
ff = FFUnix
|
||||||
case "dos":
|
case "dos":
|
||||||
@ -397,8 +386,8 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT
|
|||||||
}
|
}
|
||||||
|
|
||||||
b.UpdateRules()
|
b.UpdateRules()
|
||||||
// init local settings again now that we know the filetype
|
// we know the filetype now, so update per-filetype settings
|
||||||
config.InitLocalSettings(b.Settings, b.Path)
|
config.UpdateFileTypeLocals(b.Settings, b.Settings["filetype"].(string))
|
||||||
|
|
||||||
if _, err := os.Stat(filepath.Join(config.ConfigDir, "buffers")); os.IsNotExist(err) {
|
if _, err := os.Stat(filepath.Join(config.ConfigDir, "buffers")); os.IsNotExist(err) {
|
||||||
os.Mkdir(filepath.Join(config.ConfigDir, "buffers"), os.ModePerm)
|
os.Mkdir(filepath.Join(config.ConfigDir, "buffers"), os.ModePerm)
|
||||||
|
@ -12,8 +12,13 @@ import (
|
|||||||
|
|
||||||
func (b *Buffer) ReloadSettings(reloadFiletype bool) {
|
func (b *Buffer) ReloadSettings(reloadFiletype bool) {
|
||||||
settings := config.ParsedSettings()
|
settings := config.ParsedSettings()
|
||||||
|
config.UpdatePathGlobLocals(settings, b.AbsPath)
|
||||||
|
|
||||||
if _, ok := b.LocalSettings["filetype"]; !ok && reloadFiletype {
|
oldFiletype := b.Settings["filetype"].(string)
|
||||||
|
|
||||||
|
_, local := b.LocalSettings["filetype"]
|
||||||
|
_, volatile := config.VolatileSettings["filetype"]
|
||||||
|
if reloadFiletype && !local && !volatile {
|
||||||
// need to update filetype before updating other settings based on it
|
// need to update filetype before updating other settings based on it
|
||||||
b.Settings["filetype"] = "unknown"
|
b.Settings["filetype"] = "unknown"
|
||||||
if v, ok := settings["filetype"]; ok {
|
if v, ok := settings["filetype"]; ok {
|
||||||
@ -23,9 +28,14 @@ func (b *Buffer) ReloadSettings(reloadFiletype bool) {
|
|||||||
|
|
||||||
// update syntax rules, which will also update filetype if needed
|
// update syntax rules, which will also update filetype if needed
|
||||||
b.UpdateRules()
|
b.UpdateRules()
|
||||||
settings["filetype"] = b.Settings["filetype"]
|
|
||||||
|
|
||||||
config.InitLocalSettings(settings, b.Path)
|
curFiletype := b.Settings["filetype"].(string)
|
||||||
|
if oldFiletype != curFiletype {
|
||||||
|
b.doCallbacks("filetype", oldFiletype, curFiletype)
|
||||||
|
}
|
||||||
|
|
||||||
|
config.UpdateFileTypeLocals(settings, curFiletype)
|
||||||
|
|
||||||
for k, v := range config.DefaultCommonSettings() {
|
for k, v := range config.DefaultCommonSettings() {
|
||||||
if k == "filetype" {
|
if k == "filetype" {
|
||||||
// prevent recursion
|
// prevent recursion
|
||||||
@ -117,15 +127,7 @@ func (b *Buffer) DoSetOptionNative(option string, nativeValue interface{}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if b.OptionCallback != nil {
|
b.doCallbacks(option, oldValue, nativeValue)
|
||||||
b.OptionCallback(option, nativeValue)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := config.RunPluginFn("onBufferOptionChanged",
|
|
||||||
luar.New(ulua.L, b), luar.New(ulua.L, option),
|
|
||||||
luar.New(ulua.L, oldValue), luar.New(ulua.L, nativeValue)); err != nil {
|
|
||||||
screen.TermMessage(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error {
|
func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error {
|
||||||
@ -152,3 +154,15 @@ func (b *Buffer) SetOption(option, value string) error {
|
|||||||
|
|
||||||
return b.SetOptionNative(option, nativeValue)
|
return b.SetOptionNative(option, nativeValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Buffer) doCallbacks(option string, oldValue interface{}, newValue interface{}) {
|
||||||
|
if b.OptionCallback != nil {
|
||||||
|
b.OptionCallback(option, newValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := config.RunPluginFn("onBufferOptionChanged",
|
||||||
|
luar.New(ulua.L, b), luar.New(ulua.L, option),
|
||||||
|
luar.New(ulua.L, oldValue), luar.New(ulua.L, newValue)); err != nil {
|
||||||
|
screen.TermMessage(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -288,22 +288,31 @@ func InitGlobalSettings() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitLocalSettings scans the json in settings.json and sets the options locally based
|
// UpdatePathGlobLocals scans the already parsed settings and sets the options locally
|
||||||
// on whether the filetype or path matches ft or glob local settings
|
// based on whether the path matches a glob
|
||||||
// Must be called after ReadSettings
|
// Must be called after ReadSettings
|
||||||
func InitLocalSettings(settings map[string]interface{}, path string) {
|
func UpdatePathGlobLocals(settings map[string]interface{}, path string) {
|
||||||
for k, v := range parsedSettings {
|
for k, v := range parsedSettings {
|
||||||
if strings.HasPrefix(reflect.TypeOf(v).String(), "map") {
|
if strings.HasPrefix(reflect.TypeOf(v).String(), "map") && !strings.HasPrefix(k, "ft:") {
|
||||||
if strings.HasPrefix(k, "ft:") {
|
g, _ := glob.Compile(k)
|
||||||
if settings["filetype"].(string) == k[3:] {
|
if g.MatchString(path) {
|
||||||
for k1, v1 := range v.(map[string]interface{}) {
|
for k1, v1 := range v.(map[string]interface{}) {
|
||||||
settings[k1] = v1
|
settings[k1] = v1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
g, _ := glob.Compile(k)
|
}
|
||||||
if g.MatchString(path) {
|
}
|
||||||
|
|
||||||
|
// UpdateFileTypeLocals scans the already parsed settings and sets the options locally
|
||||||
|
// based on whether the filetype matches to "ft:"
|
||||||
|
// Must be called after ReadSettings
|
||||||
|
func UpdateFileTypeLocals(settings map[string]interface{}, filetype string) {
|
||||||
|
for k, v := range parsedSettings {
|
||||||
|
if strings.HasPrefix(reflect.TypeOf(v).String(), "map") && strings.HasPrefix(k, "ft:") {
|
||||||
|
if filetype == k[3:] {
|
||||||
for k1, v1 := range v.(map[string]interface{}) {
|
for k1, v1 := range v.(map[string]interface{}) {
|
||||||
|
if k1 != "filetype" {
|
||||||
settings[k1] = v1
|
settings[k1] = v1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user