| 1 |
package buffer |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/md5" |
| 5 |
"reflect" |
| 6 |
|
| 7 |
"github.com/zyedidia/micro/v2/internal/config" |
| 8 |
ulua "github.com/zyedidia/micro/v2/internal/lua" |
| 9 |
"github.com/zyedidia/micro/v2/internal/screen" |
| 10 |
"golang.org/x/text/encoding/htmlindex" |
| 11 |
"golang.org/x/text/encoding/unicode" |
| 12 |
luar "layeh.com/gopher-luar" |
| 13 |
) |
| 14 |
|
| 15 |
func (b *Buffer) ReloadSettings(reloadFiletype bool) { |
| 16 |
settings := config.ParsedSettings() |
| 17 |
config.UpdatePathGlobLocals(settings, b.AbsPath) |
| 18 |
|
| 19 |
oldFiletype := b.Settings["filetype"].(string) |
| 20 |
|
| 21 |
_, local := b.LocalSettings["filetype"] |
| 22 |
_, volatile := config.VolatileSettings["filetype"] |
| 23 |
if reloadFiletype && !local && !volatile { |
| 24 |
// need to update filetype before updating other settings based on it |
| 25 |
b.Settings["filetype"] = "unknown" |
| 26 |
if v, ok := settings["filetype"]; ok { |
| 27 |
b.Settings["filetype"] = v |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
// update syntax rules, which will also update filetype if needed |
| 32 |
b.UpdateRules() |
| 33 |
|
| 34 |
curFiletype := b.Settings["filetype"].(string) |
| 35 |
if oldFiletype != curFiletype { |
| 36 |
b.doCallbacks("filetype", oldFiletype, curFiletype) |
| 37 |
} |
| 38 |
|
| 39 |
config.UpdateFileTypeLocals(settings, curFiletype) |
| 40 |
|
| 41 |
for k, v := range config.DefaultCommonSettings() { |
| 42 |
if k == "filetype" { |
| 43 |
// prevent recursion |
| 44 |
continue |
| 45 |
} |
| 46 |
if _, ok := config.VolatileSettings[k]; ok { |
| 47 |
// reload should not override volatile settings |
| 48 |
continue |
| 49 |
} |
| 50 |
if _, ok := b.LocalSettings[k]; ok { |
| 51 |
// reload should not override local settings |
| 52 |
continue |
| 53 |
} |
| 54 |
if _, ok := settings[k]; ok { |
| 55 |
b.DoSetOptionNative(k, settings[k]) |
| 56 |
} else { |
| 57 |
b.DoSetOptionNative(k, v) |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func (b *Buffer) DoSetOptionNative(option string, nativeValue any) { |
| 63 |
oldValue := b.Settings[option] |
| 64 |
if reflect.DeepEqual(oldValue, nativeValue) { |
| 65 |
return |
| 66 |
} |
| 67 |
|
| 68 |
b.Settings[option] = nativeValue |
| 69 |
|
| 70 |
if option == "fastdirty" { |
| 71 |
if !nativeValue.(bool) { |
| 72 |
if b.Size() > LargeFileThreshold { |
| 73 |
b.Settings["fastdirty"] = true |
| 74 |
} else { |
| 75 |
if !b.isModified { |
| 76 |
b.calcHash(&b.origHash) |
| 77 |
} else { |
| 78 |
// prevent using an old stale origHash value |
| 79 |
b.origHash = [md5.Size]byte{} |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
} else if option == "statusline" { |
| 84 |
screen.Redraw() |
| 85 |
} else if option == "filetype" { |
| 86 |
b.ReloadSettings(false) |
| 87 |
} else if option == "fileformat" { |
| 88 |
switch b.Settings["fileformat"].(string) { |
| 89 |
case "unix": |
| 90 |
b.Endings = FFUnix |
| 91 |
case "dos": |
| 92 |
b.Endings = FFDos |
| 93 |
} |
| 94 |
b.setModified() |
| 95 |
} else if option == "syntax" { |
| 96 |
if !nativeValue.(bool) { |
| 97 |
b.ClearMatches() |
| 98 |
} else { |
| 99 |
b.UpdateRules() |
| 100 |
} |
| 101 |
} else if option == "encoding" { |
| 102 |
enc, err := htmlindex.Get(b.Settings["encoding"].(string)) |
| 103 |
if err != nil { |
| 104 |
enc = unicode.UTF8 |
| 105 |
b.Settings["encoding"] = "utf-8" |
| 106 |
} |
| 107 |
b.encoding = enc |
| 108 |
b.setModified() |
| 109 |
} else if option == "readonly" && b.Type.Kind == BTDefault.Kind { |
| 110 |
b.Type.Readonly = nativeValue.(bool) |
| 111 |
} else if option == "hlsearch" { |
| 112 |
for _, buf := range OpenBuffers { |
| 113 |
if b.SharedBuffer == buf.SharedBuffer { |
| 114 |
buf.HighlightSearch = nativeValue.(bool) |
| 115 |
} |
| 116 |
} |
| 117 |
} else { |
| 118 |
for _, pl := range config.Plugins { |
| 119 |
if option == pl.Name { |
| 120 |
if nativeValue.(bool) { |
| 121 |
if !pl.Loaded { |
| 122 |
pl.Load() |
| 123 |
} |
| 124 |
_, err := pl.Call("init") |
| 125 |
if err != nil && err != config.ErrNoSuchFunction { |
| 126 |
screen.TermMessage(err) |
| 127 |
} |
| 128 |
} else if !nativeValue.(bool) && pl.Loaded { |
| 129 |
_, err := pl.Call("deinit") |
| 130 |
if err != nil && err != config.ErrNoSuchFunction { |
| 131 |
screen.TermMessage(err) |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
b.doCallbacks(option, oldValue, nativeValue) |
| 139 |
} |
| 140 |
|
| 141 |
func (b *Buffer) SetOptionNative(option string, nativeValue any) error { |
| 142 |
if err := config.OptionIsValid(option, nativeValue); err != nil { |
| 143 |
return err |
| 144 |
} |
| 145 |
|
| 146 |
b.DoSetOptionNative(option, nativeValue) |
| 147 |
b.LocalSettings[option] = true |
| 148 |
|
| 149 |
return nil |
| 150 |
} |
| 151 |
|
| 152 |
// SetOption sets a given option to a value just for this buffer |
| 153 |
func (b *Buffer) SetOption(option, value string) error { |
| 154 |
if _, ok := b.Settings[option]; !ok { |
| 155 |
return config.ErrInvalidOption |
| 156 |
} |
| 157 |
|
| 158 |
nativeValue, err := config.GetNativeValue(option, value) |
| 159 |
if err != nil { |
| 160 |
return err |
| 161 |
} |
| 162 |
|
| 163 |
return b.SetOptionNative(option, nativeValue) |
| 164 |
} |
| 165 |
|
| 166 |
func (b *Buffer) doCallbacks(option string, oldValue any, newValue any) { |
| 167 |
if b.OptionCallback != nil { |
| 168 |
b.OptionCallback(option, newValue) |
| 169 |
} |
| 170 |
|
| 171 |
if err := config.RunPluginFn("onBufferOptionChanged", |
| 172 |
luar.New(ulua.L, b), luar.New(ulua.L, option), |
| 173 |
luar.New(ulua.L, oldValue), luar.New(ulua.L, newValue)); err != nil { |
| 174 |
screen.TermMessage(err) |
| 175 |
} |
| 176 |
} |