From c3e2085e3cc6d50e67427374a0ad51dc4a5c59dc Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Mon, 27 Aug 2018 17:55:28 -0400 Subject: [PATCH] Cursor improvements --- .gitignore | 1 + Makefile | 6 +++--- cmd/micro/actions.go | 8 ++++++++ cmd/micro/bufactionhandler.go | 1 + cmd/micro/buffer/cursor.go | 31 ++++++++++++++++++++++++++----- cmd/micro/micro.go | 2 +- cmd/micro/screen/screen.go | 6 ++++-- cmd/micro/statusline.go | 8 ++++---- cmd/micro/window.go | 16 ++++++++++++++++ 9 files changed, 64 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index acfa0730..910f4c58 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ packages/ todo.txt test.txt log.txt +*.old diff --git a/Makefile b/Makefile index 08a70768..db9441a6 100644 --- a/Makefile +++ b/Makefile @@ -40,9 +40,9 @@ update: # Builds the runtime runtime: go get -u github.com/jteeuwen/go-bindata/... - $(GOBIN)/go-bindata -nometadata -o runtime.go runtime/... - mv runtime.go cmd/micro - gofmt -w cmd/micro/runtime.go + $(GOBIN)/go-bindata -pkg config -nomemcopy -nometadata -o runtime.go runtime/... + mv runtime.go cmd/micro/config + gofmt -w cmd/micro/config/runtime.go test: go test ./cmd/micro diff --git a/cmd/micro/actions.go b/cmd/micro/actions.go index 1af5085c..2f39fe8c 100644 --- a/cmd/micro/actions.go +++ b/cmd/micro/actions.go @@ -30,21 +30,29 @@ func (a *BufActionHandler) Center() bool { // CursorUp moves the cursor up func (a *BufActionHandler) CursorUp() bool { + a.Cursor.Deselect(true) + a.Cursor.Up() return true } // CursorDown moves the cursor down func (a *BufActionHandler) CursorDown() bool { + a.Cursor.Deselect(true) + a.Cursor.Down() return true } // CursorLeft moves the cursor left func (a *BufActionHandler) CursorLeft() bool { + a.Cursor.Deselect(true) + a.Cursor.Left() return true } // CursorRight moves the cursor right func (a *BufActionHandler) CursorRight() bool { + a.Cursor.Deselect(true) + a.Cursor.Right() return true } diff --git a/cmd/micro/bufactionhandler.go b/cmd/micro/bufactionhandler.go index 87cfd211..839c39b7 100644 --- a/cmd/micro/bufactionhandler.go +++ b/cmd/micro/bufactionhandler.go @@ -74,6 +74,7 @@ func NewBufActionHandler(buf *buffer.Buffer, win *Window) *BufActionHandler { Buf: buf, Loc: buf.StartCursor, }} + a.Cursor = a.cursors[0] buf.SetCursors(a.cursors) return a diff --git a/cmd/micro/buffer/cursor.go b/cmd/micro/buffer/cursor.go index 34741ad3..78faf45b 100644 --- a/cmd/micro/buffer/cursor.go +++ b/cmd/micro/buffer/cursor.go @@ -48,7 +48,7 @@ func (c *Cursor) Goto(b Cursor) { // the current cursor its selection too func (c *Cursor) GotoLoc(l Loc) { c.X, c.Y = l.X, l.Y - c.LastVisualX = c.GetVisualX() + c.StoreVisualX() } // GetVisualX returns the x value of the cursor in visual spaces @@ -89,11 +89,13 @@ func (c *Cursor) GetCharPosInLine(b []byte, visualPos int) int { width += runewidth.RuneWidth(r) } - i++ - if width >= visualPos { + if width == visualPos { + i++ + } break } + i++ } return i @@ -155,6 +157,21 @@ func (c *Cursor) DeleteSelection() { } } +// Deselect closes the cursor's current selection +// Start indicates whether the cursor should be placed +// at the start or end of the selection +func (c *Cursor) Deselect(start bool) { + if c.HasSelection() { + if start { + c.Loc = c.CurSelection[0] + } else { + c.Loc = c.CurSelection[1] + } + c.ResetSelection() + c.StoreVisualX() + } +} + // GetSelection returns the cursor's selection func (c *Cursor) GetSelection() []byte { if InBounds(c.CurSelection[0], c.Buf) && InBounds(c.CurSelection[1], c.Buf) { @@ -244,7 +261,7 @@ func (c *Cursor) Left() { c.Up() c.End() } - c.LastVisualX = c.GetVisualX() + c.StoreVisualX() } // Right moves the cursor right one cell (if possible) or @@ -259,7 +276,7 @@ func (c *Cursor) Right() { c.Down() c.Start() } - c.LastVisualX = c.GetVisualX() + c.StoreVisualX() } // Relocate makes sure that the cursor is inside the bounds @@ -278,3 +295,7 @@ func (c *Cursor) Relocate() { c.X = utf8.RuneCount(c.Buf.LineBytes(c.Y)) } } + +func (c *Cursor) StoreVisualX() { + c.LastVisualX = c.GetVisualX() +} diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index c3746759..dca53a20 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -150,7 +150,7 @@ func main() { for { // Display everything - w.Clear() + screen.Screen.Fill(' ', config.DefStyle) w.DisplayBuffer() w.DisplayStatusLine() screen.Screen.Show() diff --git a/cmd/micro/screen/screen.go b/cmd/micro/screen/screen.go index 0266a0f9..3653da32 100644 --- a/cmd/micro/screen/screen.go +++ b/cmd/micro/screen/screen.go @@ -40,12 +40,12 @@ func Init() { Screen, err = tcell.NewScreen() if err != nil { fmt.Println(err) - fmt.Println("Fatal: Micro could not initialize a screen.") + fmt.Println("Fatal: Micro could not initialize a Screen.") os.Exit(1) } } else { fmt.Println(err) - fmt.Println("Fatal: Micro could not initialize a screen.") + fmt.Println("Fatal: Micro could not initialize a Screen.") os.Exit(1) } } @@ -64,4 +64,6 @@ func Init() { } os.Setenv("TCELLDB", tcelldb) + + // Screen.SetStyle(defStyle) } diff --git a/cmd/micro/statusline.go b/cmd/micro/statusline.go index 03df5601..5def796d 100644 --- a/cmd/micro/statusline.go +++ b/cmd/micro/statusline.go @@ -30,8 +30,8 @@ type StatusLine struct { // NewStatusLine returns a statusline bound to a window func NewStatusLine(win *Window) *StatusLine { s := new(StatusLine) - // s.FormatLeft = "$(filename) $(modified)($(line),$(col)) $(opt:filetype) $(opt:fileformat)" - s.FormatLeft = "$(filename) $(modified)(line,col) $(opt:filetype) $(opt:fileformat)" + s.FormatLeft = "$(filename) $(modified)($(line),$(col)) $(opt:filetype) $(opt:fileformat)" + // s.FormatLeft = "$(filename) $(modified)(line,col) $(opt:filetype) $(opt:fileformat)" s.FormatRight = "$(bind:ToggleKeyMenu): show bindings, $(bind:ToggleHelp): open help" s.Info = map[string]func(*buffer.Buffer) string{ "filename": func(b *buffer.Buffer) string { @@ -41,10 +41,10 @@ func NewStatusLine(win *Window) *StatusLine { return b.GetName() }, "line": func(b *buffer.Buffer) string { - return strconv.Itoa(b.GetActiveCursor().Y) + return strconv.Itoa(b.GetActiveCursor().Y + 1) }, "col": func(b *buffer.Buffer) string { - return strconv.Itoa(b.GetActiveCursor().X) + return strconv.Itoa(b.GetActiveCursor().X + 1) }, "modified": func(b *buffer.Buffer) string { if b.Modified() { diff --git a/cmd/micro/window.go b/cmd/micro/window.go index 21489598..b9cddaae 100644 --- a/cmd/micro/window.go +++ b/cmd/micro/window.go @@ -82,6 +82,15 @@ func (w *Window) GetStyle(style tcell.Style, bloc buffer.Loc, r rune) tcell.Styl return style } +func (w *Window) ShowCursor(x, y int, main bool) { + if main { + screen.Screen.ShowCursor(x, y) + } else { + r, _, _, _ := screen.Screen.GetContent(x, y) + screen.Screen.SetContent(x, y, r, nil, config.DefStyle.Reverse(true)) + } +} + // DisplayBuffer draws the buffer being shown in this window on the screen.Screen func (w *Window) DisplayBuffer() { b := w.Buf @@ -134,6 +143,10 @@ func (w *Window) DisplayBuffer() { line, nColsBeforeStart := util.SliceVisualEnd(line, bloc.X, tabsize) totalwidth := bloc.X - nColsBeforeStart for len(line) > 0 { + if w.Buf.GetActiveCursor().X == bloc.X && w.Buf.GetActiveCursor().Y == bloc.Y { + w.ShowCursor(vloc.X, vloc.Y, true) + } + r, size := utf8.DecodeRune(line) curStyle = w.GetStyle(curStyle, bloc, r) @@ -186,6 +199,9 @@ func (w *Window) DisplayBuffer() { } } } + if w.Buf.GetActiveCursor().X == bloc.X && w.Buf.GetActiveCursor().Y == bloc.Y { + w.ShowCursor(vloc.X, vloc.Y, true) + } bloc.X = w.StartCol bloc.Y++ if bloc.Y >= b.LinesNum() { -- 2.44.0