From 254b892a3bf4a39c1cbc6c4d76bd6d2d9082cd8d Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Wed, 16 Jan 2019 17:52:30 -0500 Subject: [PATCH] Fix multi cursor relocate --- cmd/micro/action/actions.go | 20 +++++++++++++++----- cmd/micro/action/bufhandler.go | 1 + cmd/micro/buffer/buffer.go | 9 +++++++++ cmd/micro/buffer/eventhandler.go | 7 ++++--- cmd/micro/buffer/search.go | 1 - cmd/micro/display/bufwindow.go | 1 - 6 files changed, 29 insertions(+), 10 deletions(-) diff --git a/cmd/micro/action/actions.go b/cmd/micro/action/actions.go index 588437fb..1c68d17e 100644 --- a/cmd/micro/action/actions.go +++ b/cmd/micro/action/actions.go @@ -2,6 +2,7 @@ package action import ( "os" + "regexp" "strings" "time" "unicode/utf8" @@ -1186,10 +1187,11 @@ func (h *BufHandler) SpawnMultiCursor() bool { searchStart := spawner.CurSelection[1] search := string(sel) + search = regexp.QuoteMeta(search) if h.multiWord { search = "\\b" + search + "\\b" } - match, found, err := h.Buf.FindNext(search, h.Buf.Start(), h.Buf.End(), searchStart, true, false) + match, found, err := h.Buf.FindNext(search, h.Buf.Start(), h.Buf.End(), searchStart, true, true) if err != nil { InfoBar.Error(err) } @@ -1202,6 +1204,7 @@ func (h *BufHandler) SpawnMultiCursor() bool { c.Loc = c.CurSelection[1] h.Buf.AddCursor(c) + h.Buf.SetCurCursor(h.Buf.NumCursors() - 1) h.Buf.MergeCursors() } else { InfoBar.Message("No matches found") @@ -1262,7 +1265,13 @@ func (h *BufHandler) SkipMultiCursor() bool { sel := lastC.GetSelection() searchStart := lastC.CurSelection[1] - match, found, err := h.Buf.FindNext(string(sel), h.Buf.Start(), h.Buf.End(), searchStart, true, false) + search := string(sel) + search = regexp.QuoteMeta(search) + if h.multiWord { + search = "\\b" + search + "\\b" + } + + match, found, err := h.Buf.FindNext(search, h.Buf.Start(), h.Buf.End(), searchStart, true, true) if err != nil { InfoBar.Error(err) } @@ -1274,22 +1283,23 @@ func (h *BufHandler) SkipMultiCursor() bool { lastC.Loc = lastC.CurSelection[1] h.Buf.MergeCursors() - h.Relocate() + h.Buf.SetCurCursor(h.Buf.NumCursors() - 1) } else { InfoBar.Message("No matches found") } - return false + return true } // RemoveMultiCursor removes the latest multiple cursor func (h *BufHandler) RemoveMultiCursor() bool { if h.Buf.NumCursors() > 1 { h.Buf.RemoveCursor(h.Buf.NumCursors() - 1) + h.Buf.SetCurCursor(h.Buf.NumCursors() - 1) h.Buf.UpdateCursors() } else { h.multiWord = false } - return false + return true } // RemoveAllMultiCursors removes all cursors except the base cursor diff --git a/cmd/micro/action/bufhandler.go b/cmd/micro/action/bufhandler.go index e763b460..ede74b5f 100644 --- a/cmd/micro/action/bufhandler.go +++ b/cmd/micro/action/bufhandler.go @@ -253,6 +253,7 @@ func (h *BufHandler) DoRuneInsert(r rune) { cursors := h.Buf.GetCursors() for _, c := range cursors { // Insert a character + h.Buf.SetCurCursor(c.Num) if c.HasSelection() { c.DeleteSelection() c.ResetSelection() diff --git a/cmd/micro/buffer/buffer.go b/cmd/micro/buffer/buffer.go index e87e5e49..f6d91a89 100644 --- a/cmd/micro/buffer/buffer.go +++ b/cmd/micro/buffer/buffer.go @@ -242,11 +242,13 @@ func (b *Buffer) SetName(s string) { func (b *Buffer) Insert(start Loc, text []byte) { b.EventHandler.cursors = b.cursors + b.EventHandler.active = b.curCursor b.EventHandler.Insert(start, text) } func (b *Buffer) Remove(start, end Loc) { b.EventHandler.cursors = b.cursors + b.EventHandler.active = b.curCursor b.EventHandler.Remove(start, end) } @@ -428,12 +430,14 @@ func (b *Buffer) IndentString(tabsize int) []byte { func (b *Buffer) SetCursors(c []*Cursor) { b.cursors = c b.EventHandler.cursors = b.cursors + b.EventHandler.active = b.curCursor } // AddCursor adds a new cursor to the list func (b *Buffer) AddCursor(c *Cursor) { b.cursors = append(b.cursors, c) b.EventHandler.cursors = b.cursors + b.EventHandler.active = b.curCursor b.UpdateCursors() } @@ -489,10 +493,13 @@ func (b *Buffer) MergeCursors() { b.curCursor = len(b.cursors) - 1 } b.EventHandler.cursors = b.cursors + b.EventHandler.active = b.curCursor } // UpdateCursors updates all the cursors indicies func (b *Buffer) UpdateCursors() { + b.EventHandler.cursors = b.cursors + b.EventHandler.active = b.curCursor for i, c := range b.cursors { c.Num = i } @@ -502,6 +509,8 @@ func (b *Buffer) RemoveCursor(i int) { copy(b.cursors[i:], b.cursors[i+1:]) b.cursors[len(b.cursors)-1] = nil b.cursors = b.cursors[:len(b.cursors)-1] + b.curCursor = Clamp(b.curCursor, 0, len(b.cursors)-1) + b.UpdateCursors() } // ClearCursors removes all extra cursors diff --git a/cmd/micro/buffer/eventhandler.go b/cmd/micro/buffer/eventhandler.go index 81f1b67d..22a4806c 100644 --- a/cmd/micro/buffer/eventhandler.go +++ b/cmd/micro/buffer/eventhandler.go @@ -69,6 +69,7 @@ func UndoTextEvent(t *TextEvent, buf *SharedBuffer) { type EventHandler struct { buf *SharedBuffer cursors []*Cursor + active int UndoStack *TEStack RedoStack *TEStack } @@ -106,7 +107,7 @@ func (eh *EventHandler) ApplyDiff(str string) { // Insert creates an insert text event and executes it func (eh *EventHandler) Insert(start Loc, text []byte) { e := &TextEvent{ - C: *eh.cursors[0], + C: *eh.cursors[eh.active], EventType: TextEventInsert, Deltas: []Delta{{text, start, Loc{0, 0}}}, Time: time.Now(), @@ -136,7 +137,7 @@ func (eh *EventHandler) Insert(start Loc, text []byte) { // Remove creates a remove text event and executes it func (eh *EventHandler) Remove(start, end Loc) { e := &TextEvent{ - C: *eh.cursors[0], + C: *eh.cursors[eh.active], EventType: TextEventRemove, Deltas: []Delta{{[]byte{}, start, end}}, Time: time.Now(), @@ -164,7 +165,7 @@ func (eh *EventHandler) Remove(start, end Loc) { // MultipleReplace creates an multiple insertions executes them func (eh *EventHandler) MultipleReplace(deltas []Delta) { e := &TextEvent{ - C: *eh.cursors[0], + C: *eh.cursors[eh.active], EventType: TextEventReplace, Deltas: deltas, Time: time.Now(), diff --git a/cmd/micro/buffer/search.go b/cmd/micro/buffer/search.go index 03932fc3..5157a0bc 100644 --- a/cmd/micro/buffer/search.go +++ b/cmd/micro/buffer/search.go @@ -144,7 +144,6 @@ func (b *Buffer) ReplaceRegex(start, end Loc, search *regexp.Regexp, replace []b l := b.lines[i].data charpos := 0 - // TODO: replace within X coords of selection if start.Y == end.Y && i == start.Y { l = util.SliceStart(l, end.X) l = util.SliceEnd(l, start.X) diff --git a/cmd/micro/display/bufwindow.go b/cmd/micro/display/bufwindow.go index ef532c56..8d11ed5f 100644 --- a/cmd/micro/display/bufwindow.go +++ b/cmd/micro/display/bufwindow.go @@ -364,7 +364,6 @@ func (w *BufWindow) displayBuffer() { bufHeight-- } - // TODO: Rehighlighting start := w.StartLine if b.Settings["syntax"].(bool) && b.SyntaxDef != nil { if start > 0 && b.Rehighlight(start-1) {