Skip to content

Commit 4579428

Browse files
Merge branch 'master' into dev
2 parents bba57f4 + fd3a002 commit 4579428

File tree

6 files changed

+62
-58
lines changed

6 files changed

+62
-58
lines changed

internal/action/actions.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,10 +1467,14 @@ func (h *BufPane) paste(clip string) {
14671467
func (h *BufPane) JumpToMatchingBrace() bool {
14681468
matchingBrace, left, found := h.Buf.FindMatchingBrace(h.Cursor.Loc)
14691469
if found {
1470-
if left {
1471-
h.Cursor.GotoLoc(matchingBrace)
1470+
if h.Buf.Settings["matchbraceleft"].(bool) {
1471+
if left {
1472+
h.Cursor.GotoLoc(matchingBrace)
1473+
} else {
1474+
h.Cursor.GotoLoc(matchingBrace.Move(1, h.Buf))
1475+
}
14721476
} else {
1473-
h.Cursor.GotoLoc(matchingBrace.Move(1, h.Buf))
1477+
h.Cursor.GotoLoc(matchingBrace)
14741478
}
14751479
h.Relocate()
14761480
return true

internal/buffer/buffer.go

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,6 @@ var (
6565
// BTStdout is a buffer that only writes to stdout
6666
// when closed
6767
BTStdout = BufType{6, false, true, true}
68-
69-
// ErrFileTooLarge is returned when the file is too large to hash
70-
// (fastdirty is automatically enabled)
71-
ErrFileTooLarge = errors.New("File is too large to hash")
7268
)
7369

7470
// SharedBuffer is a struct containing info that is shared among buffers
@@ -555,7 +551,11 @@ func (b *Buffer) ReOpen() error {
555551

556552
err = b.UpdateModTime()
557553
if !b.Settings["fastdirty"].(bool) {
558-
calcHash(b, &b.origHash)
554+
if len(data) > LargeFileThreshold {
555+
b.Settings["fastdirty"] = true
556+
} else {
557+
calcHash(b, &b.origHash)
558+
}
559559
}
560560
b.isModified = false
561561
b.RelocateCursors()
@@ -650,37 +650,23 @@ func (b *Buffer) Size() int {
650650
}
651651

652652
// calcHash calculates md5 hash of all lines in the buffer
653-
func calcHash(b *Buffer, out *[md5.Size]byte) error {
653+
func calcHash(b *Buffer, out *[md5.Size]byte) {
654654
h := md5.New()
655655

656-
size := 0
657656
if len(b.lines) > 0 {
658-
n, e := h.Write(b.lines[0].data)
659-
if e != nil {
660-
return e
661-
}
662-
size += n
657+
h.Write(b.lines[0].data)
663658

664659
for _, l := range b.lines[1:] {
665-
n, e = h.Write([]byte{'\n'})
666-
if e != nil {
667-
return e
668-
}
669-
size += n
670-
n, e = h.Write(l.data)
671-
if e != nil {
672-
return e
660+
if b.Endings == FFDos {
661+
h.Write([]byte{'\r', '\n'})
662+
} else {
663+
h.Write([]byte{'\n'})
673664
}
674-
size += n
665+
h.Write(l.data)
675666
}
676667
}
677668

678-
if size > LargeFileThreshold {
679-
return ErrFileTooLarge
680-
}
681-
682669
h.Sum((*out)[:0])
683-
return nil
684670
}
685671

686672
func parseDefFromFile(f config.RuntimeFile, header *highlight.Header) *highlight.Def {
@@ -1373,17 +1359,19 @@ func (b *Buffer) FindMatchingBrace(start Loc) (Loc, bool, bool) {
13731359
}
13741360
}
13751361

1376-
// failed to find matching brace for the given location, so try to find matching
1377-
// brace for the location one character left of it
1378-
if start.X-1 >= 0 && start.X-1 < len(curLine) {
1379-
leftChar := curLine[start.X-1]
1380-
left := Loc{start.X - 1, start.Y}
1362+
if b.Settings["matchbraceleft"].(bool) {
1363+
// failed to find matching brace for the given location, so try to find matching
1364+
// brace for the location one character left of it
1365+
if start.X-1 >= 0 && start.X-1 < len(curLine) {
1366+
leftChar := curLine[start.X-1]
1367+
left := Loc{start.X - 1, start.Y}
13811368

1382-
for _, bp := range BracePairs {
1383-
if leftChar == bp[0] || leftChar == bp[1] {
1384-
mb, found := b.findMatchingBrace(bp, left, leftChar)
1385-
if found {
1386-
return mb, true, true
1369+
for _, bp := range BracePairs {
1370+
if leftChar == bp[0] || leftChar == bp[1] {
1371+
mb, found := b.findMatchingBrace(bp, left, leftChar)
1372+
if found {
1373+
return mb, true, true
1374+
}
13871375
}
13881376
}
13891377
}

internal/buffer/eventhandler.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,9 @@ func (eh *EventHandler) UndoOneEvent() {
291291
eh.UndoTextEvent(t)
292292

293293
// Set the cursor in the right place
294-
teCursor := t.C
295-
if teCursor.Num >= 0 && teCursor.Num < len(eh.cursors) {
296-
t.C = *eh.cursors[teCursor.Num]
297-
eh.cursors[teCursor.Num].Goto(teCursor)
298-
eh.cursors[teCursor.Num].NewTrailingWsY = teCursor.NewTrailingWsY
299-
} else {
300-
teCursor.Num = -1
294+
if t.C.Num >= 0 && t.C.Num < len(eh.cursors) {
295+
eh.cursors[t.C.Num].Goto(t.C)
296+
eh.cursors[t.C.Num].NewTrailingWsY = t.C.NewTrailingWsY
301297
}
302298

303299
// Push it to the redo stack
@@ -336,13 +332,9 @@ func (eh *EventHandler) RedoOneEvent() {
336332
return
337333
}
338334

339-
teCursor := t.C
340-
if teCursor.Num >= 0 && teCursor.Num < len(eh.cursors) {
341-
t.C = *eh.cursors[teCursor.Num]
342-
eh.cursors[teCursor.Num].Goto(teCursor)
343-
eh.cursors[teCursor.Num].NewTrailingWsY = teCursor.NewTrailingWsY
344-
} else {
345-
teCursor.Num = -1
335+
if t.C.Num >= 0 && t.C.Num < len(eh.cursors) {
336+
eh.cursors[t.C.Num].Goto(t.C)
337+
eh.cursors[t.C.Num].NewTrailingWsY = t.C.NewTrailingWsY
346338
}
347339

348340
// Modifies the text event

internal/buffer/settings.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package buffer
22

33
import (
4+
"crypto/md5"
5+
46
"github.com/zyedidia/micro/v2/internal/config"
57
"github.com/zyedidia/micro/v2/internal/screen"
68
)
@@ -10,10 +12,14 @@ func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error {
1012

1113
if option == "fastdirty" {
1214
if !nativeValue.(bool) {
13-
if !b.Modified() {
14-
e := calcHash(b, &b.origHash)
15-
if e == ErrFileTooLarge {
16-
b.Settings["fastdirty"] = false
15+
if b.Size() > LargeFileThreshold {
16+
b.Settings["fastdirty"] = true
17+
} else {
18+
if !b.isModified {
19+
calcHash(b, &b.origHash)
20+
} else {
21+
// prevent using an old stale origHash value
22+
b.origHash = [md5.Size]byte{}
1723
}
1824
}
1925
}

internal/config/settings.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ var defaultCommonSettings = map[string]interface{}{
7171
"indentchar": " ",
7272
"keepautoindent": false,
7373
"matchbrace": true,
74+
"matchbraceleft": true,
7475
"matchbracestyle": "underline",
7576
"mkparents": false,
7677
"permbackup": false,

runtime/help/options.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,19 @@ Here are the available options:
231231
default value: `false`
232232

233233
* `matchbrace`: show matching braces for '()', '{}', '[]' when the cursor
234-
is on a brace character or next to it.
234+
is on a brace character or (if `matchbraceleft` is enabled) next to it.
235+
236+
default value: `true`
237+
238+
* `matchbraceleft`: simulate I-beam cursor behavior (cursor located not on a
239+
character but "between" characters): when showing matching braces, if there
240+
is no brace character directly under the cursor, match the brace character
241+
to the left of the cursor instead. Also when jumping to the matching brace,
242+
move the cursor either to the matching brace character or to the character
243+
next to it, depending on whether the initial cursor position was on the
244+
brace character or next to it (i.e. "inside" or "outside" the braces).
245+
With `matchbraceleft` disabled, micro will only match the brace directly
246+
under the cursor and will only jump to precisely to the matching brace.
235247

236248
default value: `true`
237249

@@ -533,6 +545,7 @@ so that you can see what the formatting should look like.
533545
"linter": true,
534546
"literate": true,
535547
"matchbrace": true,
548+
"matchbraceleft": true,
536549
"matchbracestyle": "underline",
537550
"mkparents": false,
538551
"mouse": true,

0 commit comments

Comments
 (0)