buffer: Store the encoding inside the buffer

This commit is contained in:
Jöran Karl 2025-02-06 19:54:47 +01:00
parent fe134b92d5
commit 8b21724c6e
3 changed files with 16 additions and 11 deletions

View File

@ -25,6 +25,7 @@ import (
"github.com/zyedidia/micro/v2/internal/screen"
"github.com/zyedidia/micro/v2/internal/util"
"github.com/zyedidia/micro/v2/pkg/highlight"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/htmlindex"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
@ -87,6 +88,8 @@ type SharedBuffer struct {
// LocalSettings customized by the user for this buffer only
LocalSettings map[string]bool
encoding encoding.Encoding
Suggestions []string
Completions []string
CurSuggestion int
@ -337,9 +340,9 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT
}
config.UpdatePathGlobLocals(b.Settings, absPath)
enc, err := htmlindex.Get(b.Settings["encoding"].(string))
b.encoding, err = htmlindex.Get(b.Settings["encoding"].(string))
if err != nil {
enc = unicode.UTF8
b.encoding = unicode.UTF8
b.Settings["encoding"] = "utf-8"
}
@ -350,7 +353,7 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT
return NewBufferFromString("", "", btype)
}
if !hasBackup {
reader := bufio.NewReader(transform.NewReader(r, enc.NewDecoder()))
reader := bufio.NewReader(transform.NewReader(r, b.encoding.NewDecoder()))
var ff FileFormat = FFAuto

View File

@ -18,7 +18,6 @@ import (
"github.com/zyedidia/micro/v2/internal/config"
"github.com/zyedidia/micro/v2/internal/screen"
"github.com/zyedidia/micro/v2/internal/util"
"golang.org/x/text/encoding/htmlindex"
"golang.org/x/text/transform"
)
@ -118,12 +117,7 @@ func openFile(name string, withSudo bool) (wrappedFile, error) {
}
func (wf wrappedFile) Write(b *Buffer) (int, error) {
enc, err := htmlindex.Get(b.Settings["encoding"].(string))
if err != nil {
return 0, err
}
file := bufio.NewWriter(transform.NewWriter(wf.writeCloser, enc.NewEncoder()))
file := bufio.NewWriter(transform.NewWriter(wf.writeCloser, b.encoding.NewEncoder()))
b.Lock()
defer b.Unlock()
@ -142,7 +136,7 @@ func (wf wrappedFile) Write(b *Buffer) (int, error) {
if !wf.withSudo {
f := wf.writeCloser.(*os.File)
err = f.Truncate(0)
err := f.Truncate(0)
if err != nil {
return 0, err
}

View File

@ -7,6 +7,8 @@ import (
"github.com/zyedidia/micro/v2/internal/config"
ulua "github.com/zyedidia/micro/v2/internal/lua"
"github.com/zyedidia/micro/v2/internal/screen"
"golang.org/x/text/encoding/htmlindex"
"golang.org/x/text/encoding/unicode"
luar "layeh.com/gopher-luar"
)
@ -97,6 +99,12 @@ func (b *Buffer) DoSetOptionNative(option string, nativeValue interface{}) {
b.UpdateRules()
}
} else if option == "encoding" {
enc, err := htmlindex.Get(b.Settings["encoding"].(string))
if err != nil {
enc = unicode.UTF8
b.Settings["encoding"] = "utf-8"
}
b.encoding = enc
b.isModified = true
} else if option == "readonly" && b.Type.Kind == BTDefault.Kind {
b.Type.Readonly = nativeValue.(bool)