Skip to content

Commit 8b21724

Browse files
committed
buffer: Store the encoding inside the buffer
1 parent fe134b9 commit 8b21724

3 files changed

Lines changed: 16 additions & 11 deletions

File tree

internal/buffer/buffer.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/zyedidia/micro/v2/internal/screen"
2626
"github.com/zyedidia/micro/v2/internal/util"
2727
"github.com/zyedidia/micro/v2/pkg/highlight"
28+
"golang.org/x/text/encoding"
2829
"golang.org/x/text/encoding/htmlindex"
2930
"golang.org/x/text/encoding/unicode"
3031
"golang.org/x/text/transform"
@@ -87,6 +88,8 @@ type SharedBuffer struct {
8788
// LocalSettings customized by the user for this buffer only
8889
LocalSettings map[string]bool
8990

91+
encoding encoding.Encoding
92+
9093
Suggestions []string
9194
Completions []string
9295
CurSuggestion int
@@ -337,9 +340,9 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT
337340
}
338341
config.UpdatePathGlobLocals(b.Settings, absPath)
339342

340-
enc, err := htmlindex.Get(b.Settings["encoding"].(string))
343+
b.encoding, err = htmlindex.Get(b.Settings["encoding"].(string))
341344
if err != nil {
342-
enc = unicode.UTF8
345+
b.encoding = unicode.UTF8
343346
b.Settings["encoding"] = "utf-8"
344347
}
345348

@@ -350,7 +353,7 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT
350353
return NewBufferFromString("", "", btype)
351354
}
352355
if !hasBackup {
353-
reader := bufio.NewReader(transform.NewReader(r, enc.NewDecoder()))
356+
reader := bufio.NewReader(transform.NewReader(r, b.encoding.NewDecoder()))
354357

355358
var ff FileFormat = FFAuto
356359

internal/buffer/save.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"github.com/zyedidia/micro/v2/internal/config"
1919
"github.com/zyedidia/micro/v2/internal/screen"
2020
"github.com/zyedidia/micro/v2/internal/util"
21-
"golang.org/x/text/encoding/htmlindex"
2221
"golang.org/x/text/transform"
2322
)
2423

@@ -118,12 +117,7 @@ func openFile(name string, withSudo bool) (wrappedFile, error) {
118117
}
119118

120119
func (wf wrappedFile) Write(b *Buffer) (int, error) {
121-
enc, err := htmlindex.Get(b.Settings["encoding"].(string))
122-
if err != nil {
123-
return 0, err
124-
}
125-
126-
file := bufio.NewWriter(transform.NewWriter(wf.writeCloser, enc.NewEncoder()))
120+
file := bufio.NewWriter(transform.NewWriter(wf.writeCloser, b.encoding.NewEncoder()))
127121

128122
b.Lock()
129123
defer b.Unlock()
@@ -142,7 +136,7 @@ func (wf wrappedFile) Write(b *Buffer) (int, error) {
142136

143137
if !wf.withSudo {
144138
f := wf.writeCloser.(*os.File)
145-
err = f.Truncate(0)
139+
err := f.Truncate(0)
146140
if err != nil {
147141
return 0, err
148142
}

internal/buffer/settings.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"github.com/zyedidia/micro/v2/internal/config"
88
ulua "github.com/zyedidia/micro/v2/internal/lua"
99
"github.com/zyedidia/micro/v2/internal/screen"
10+
"golang.org/x/text/encoding/htmlindex"
11+
"golang.org/x/text/encoding/unicode"
1012
luar "layeh.com/gopher-luar"
1113
)
1214

@@ -97,6 +99,12 @@ func (b *Buffer) DoSetOptionNative(option string, nativeValue interface{}) {
9799
b.UpdateRules()
98100
}
99101
} 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
100108
b.isModified = true
101109
} else if option == "readonly" && b.Type.Kind == BTDefault.Kind {
102110
b.Type.Readonly = nativeValue.(bool)

0 commit comments

Comments
 (0)