mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-25 18:07:07 +09:00
Fix multi cursor relocate
This commit is contained in:
@@ -2,6 +2,7 @@ package action
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
@@ -1186,10 +1187,11 @@ func (h *BufHandler) SpawnMultiCursor() bool {
|
|||||||
searchStart := spawner.CurSelection[1]
|
searchStart := spawner.CurSelection[1]
|
||||||
|
|
||||||
search := string(sel)
|
search := string(sel)
|
||||||
|
search = regexp.QuoteMeta(search)
|
||||||
if h.multiWord {
|
if h.multiWord {
|
||||||
search = "\\b" + search + "\\b"
|
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 {
|
if err != nil {
|
||||||
InfoBar.Error(err)
|
InfoBar.Error(err)
|
||||||
}
|
}
|
||||||
@@ -1202,6 +1204,7 @@ func (h *BufHandler) SpawnMultiCursor() bool {
|
|||||||
c.Loc = c.CurSelection[1]
|
c.Loc = c.CurSelection[1]
|
||||||
|
|
||||||
h.Buf.AddCursor(c)
|
h.Buf.AddCursor(c)
|
||||||
|
h.Buf.SetCurCursor(h.Buf.NumCursors() - 1)
|
||||||
h.Buf.MergeCursors()
|
h.Buf.MergeCursors()
|
||||||
} else {
|
} else {
|
||||||
InfoBar.Message("No matches found")
|
InfoBar.Message("No matches found")
|
||||||
@@ -1262,7 +1265,13 @@ func (h *BufHandler) SkipMultiCursor() bool {
|
|||||||
sel := lastC.GetSelection()
|
sel := lastC.GetSelection()
|
||||||
searchStart := lastC.CurSelection[1]
|
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 {
|
if err != nil {
|
||||||
InfoBar.Error(err)
|
InfoBar.Error(err)
|
||||||
}
|
}
|
||||||
@@ -1274,22 +1283,23 @@ func (h *BufHandler) SkipMultiCursor() bool {
|
|||||||
lastC.Loc = lastC.CurSelection[1]
|
lastC.Loc = lastC.CurSelection[1]
|
||||||
|
|
||||||
h.Buf.MergeCursors()
|
h.Buf.MergeCursors()
|
||||||
h.Relocate()
|
h.Buf.SetCurCursor(h.Buf.NumCursors() - 1)
|
||||||
} else {
|
} else {
|
||||||
InfoBar.Message("No matches found")
|
InfoBar.Message("No matches found")
|
||||||
}
|
}
|
||||||
return false
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveMultiCursor removes the latest multiple cursor
|
// RemoveMultiCursor removes the latest multiple cursor
|
||||||
func (h *BufHandler) RemoveMultiCursor() bool {
|
func (h *BufHandler) RemoveMultiCursor() bool {
|
||||||
if h.Buf.NumCursors() > 1 {
|
if h.Buf.NumCursors() > 1 {
|
||||||
h.Buf.RemoveCursor(h.Buf.NumCursors() - 1)
|
h.Buf.RemoveCursor(h.Buf.NumCursors() - 1)
|
||||||
|
h.Buf.SetCurCursor(h.Buf.NumCursors() - 1)
|
||||||
h.Buf.UpdateCursors()
|
h.Buf.UpdateCursors()
|
||||||
} else {
|
} else {
|
||||||
h.multiWord = false
|
h.multiWord = false
|
||||||
}
|
}
|
||||||
return false
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveAllMultiCursors removes all cursors except the base cursor
|
// RemoveAllMultiCursors removes all cursors except the base cursor
|
||||||
|
|||||||
@@ -253,6 +253,7 @@ func (h *BufHandler) DoRuneInsert(r rune) {
|
|||||||
cursors := h.Buf.GetCursors()
|
cursors := h.Buf.GetCursors()
|
||||||
for _, c := range cursors {
|
for _, c := range cursors {
|
||||||
// Insert a character
|
// Insert a character
|
||||||
|
h.Buf.SetCurCursor(c.Num)
|
||||||
if c.HasSelection() {
|
if c.HasSelection() {
|
||||||
c.DeleteSelection()
|
c.DeleteSelection()
|
||||||
c.ResetSelection()
|
c.ResetSelection()
|
||||||
|
|||||||
@@ -242,11 +242,13 @@ func (b *Buffer) SetName(s string) {
|
|||||||
|
|
||||||
func (b *Buffer) Insert(start Loc, text []byte) {
|
func (b *Buffer) Insert(start Loc, text []byte) {
|
||||||
b.EventHandler.cursors = b.cursors
|
b.EventHandler.cursors = b.cursors
|
||||||
|
b.EventHandler.active = b.curCursor
|
||||||
b.EventHandler.Insert(start, text)
|
b.EventHandler.Insert(start, text)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Buffer) Remove(start, end Loc) {
|
func (b *Buffer) Remove(start, end Loc) {
|
||||||
b.EventHandler.cursors = b.cursors
|
b.EventHandler.cursors = b.cursors
|
||||||
|
b.EventHandler.active = b.curCursor
|
||||||
b.EventHandler.Remove(start, end)
|
b.EventHandler.Remove(start, end)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -428,12 +430,14 @@ func (b *Buffer) IndentString(tabsize int) []byte {
|
|||||||
func (b *Buffer) SetCursors(c []*Cursor) {
|
func (b *Buffer) SetCursors(c []*Cursor) {
|
||||||
b.cursors = c
|
b.cursors = c
|
||||||
b.EventHandler.cursors = b.cursors
|
b.EventHandler.cursors = b.cursors
|
||||||
|
b.EventHandler.active = b.curCursor
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddCursor adds a new cursor to the list
|
// AddCursor adds a new cursor to the list
|
||||||
func (b *Buffer) AddCursor(c *Cursor) {
|
func (b *Buffer) AddCursor(c *Cursor) {
|
||||||
b.cursors = append(b.cursors, c)
|
b.cursors = append(b.cursors, c)
|
||||||
b.EventHandler.cursors = b.cursors
|
b.EventHandler.cursors = b.cursors
|
||||||
|
b.EventHandler.active = b.curCursor
|
||||||
b.UpdateCursors()
|
b.UpdateCursors()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -489,10 +493,13 @@ func (b *Buffer) MergeCursors() {
|
|||||||
b.curCursor = len(b.cursors) - 1
|
b.curCursor = len(b.cursors) - 1
|
||||||
}
|
}
|
||||||
b.EventHandler.cursors = b.cursors
|
b.EventHandler.cursors = b.cursors
|
||||||
|
b.EventHandler.active = b.curCursor
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateCursors updates all the cursors indicies
|
// UpdateCursors updates all the cursors indicies
|
||||||
func (b *Buffer) UpdateCursors() {
|
func (b *Buffer) UpdateCursors() {
|
||||||
|
b.EventHandler.cursors = b.cursors
|
||||||
|
b.EventHandler.active = b.curCursor
|
||||||
for i, c := range b.cursors {
|
for i, c := range b.cursors {
|
||||||
c.Num = i
|
c.Num = i
|
||||||
}
|
}
|
||||||
@@ -502,6 +509,8 @@ func (b *Buffer) RemoveCursor(i int) {
|
|||||||
copy(b.cursors[i:], b.cursors[i+1:])
|
copy(b.cursors[i:], b.cursors[i+1:])
|
||||||
b.cursors[len(b.cursors)-1] = nil
|
b.cursors[len(b.cursors)-1] = nil
|
||||||
b.cursors = b.cursors[:len(b.cursors)-1]
|
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
|
// ClearCursors removes all extra cursors
|
||||||
|
|||||||
@@ -69,6 +69,7 @@ func UndoTextEvent(t *TextEvent, buf *SharedBuffer) {
|
|||||||
type EventHandler struct {
|
type EventHandler struct {
|
||||||
buf *SharedBuffer
|
buf *SharedBuffer
|
||||||
cursors []*Cursor
|
cursors []*Cursor
|
||||||
|
active int
|
||||||
UndoStack *TEStack
|
UndoStack *TEStack
|
||||||
RedoStack *TEStack
|
RedoStack *TEStack
|
||||||
}
|
}
|
||||||
@@ -106,7 +107,7 @@ func (eh *EventHandler) ApplyDiff(str string) {
|
|||||||
// Insert creates an insert text event and executes it
|
// Insert creates an insert text event and executes it
|
||||||
func (eh *EventHandler) Insert(start Loc, text []byte) {
|
func (eh *EventHandler) Insert(start Loc, text []byte) {
|
||||||
e := &TextEvent{
|
e := &TextEvent{
|
||||||
C: *eh.cursors[0],
|
C: *eh.cursors[eh.active],
|
||||||
EventType: TextEventInsert,
|
EventType: TextEventInsert,
|
||||||
Deltas: []Delta{{text, start, Loc{0, 0}}},
|
Deltas: []Delta{{text, start, Loc{0, 0}}},
|
||||||
Time: time.Now(),
|
Time: time.Now(),
|
||||||
@@ -136,7 +137,7 @@ func (eh *EventHandler) Insert(start Loc, text []byte) {
|
|||||||
// Remove creates a remove text event and executes it
|
// Remove creates a remove text event and executes it
|
||||||
func (eh *EventHandler) Remove(start, end Loc) {
|
func (eh *EventHandler) Remove(start, end Loc) {
|
||||||
e := &TextEvent{
|
e := &TextEvent{
|
||||||
C: *eh.cursors[0],
|
C: *eh.cursors[eh.active],
|
||||||
EventType: TextEventRemove,
|
EventType: TextEventRemove,
|
||||||
Deltas: []Delta{{[]byte{}, start, end}},
|
Deltas: []Delta{{[]byte{}, start, end}},
|
||||||
Time: time.Now(),
|
Time: time.Now(),
|
||||||
@@ -164,7 +165,7 @@ func (eh *EventHandler) Remove(start, end Loc) {
|
|||||||
// MultipleReplace creates an multiple insertions executes them
|
// MultipleReplace creates an multiple insertions executes them
|
||||||
func (eh *EventHandler) MultipleReplace(deltas []Delta) {
|
func (eh *EventHandler) MultipleReplace(deltas []Delta) {
|
||||||
e := &TextEvent{
|
e := &TextEvent{
|
||||||
C: *eh.cursors[0],
|
C: *eh.cursors[eh.active],
|
||||||
EventType: TextEventReplace,
|
EventType: TextEventReplace,
|
||||||
Deltas: deltas,
|
Deltas: deltas,
|
||||||
Time: time.Now(),
|
Time: time.Now(),
|
||||||
|
|||||||
@@ -144,7 +144,6 @@ func (b *Buffer) ReplaceRegex(start, end Loc, search *regexp.Regexp, replace []b
|
|||||||
l := b.lines[i].data
|
l := b.lines[i].data
|
||||||
charpos := 0
|
charpos := 0
|
||||||
|
|
||||||
// TODO: replace within X coords of selection
|
|
||||||
if start.Y == end.Y && i == start.Y {
|
if start.Y == end.Y && i == start.Y {
|
||||||
l = util.SliceStart(l, end.X)
|
l = util.SliceStart(l, end.X)
|
||||||
l = util.SliceEnd(l, start.X)
|
l = util.SliceEnd(l, start.X)
|
||||||
|
|||||||
@@ -364,7 +364,6 @@ func (w *BufWindow) displayBuffer() {
|
|||||||
bufHeight--
|
bufHeight--
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Rehighlighting
|
|
||||||
start := w.StartLine
|
start := w.StartLine
|
||||||
if b.Settings["syntax"].(bool) && b.SyntaxDef != nil {
|
if b.Settings["syntax"].(bool) && b.SyntaxDef != nil {
|
||||||
if start > 0 && b.Rehighlight(start-1) {
|
if start > 0 && b.Rehighlight(start-1) {
|
||||||
|
|||||||
Reference in New Issue
Block a user