]> git.lizzy.rs Git - micro.git/commitdiff
Cursor improvements
authorZachary Yedidia <zyedidia@gmail.com>
Mon, 27 Aug 2018 21:55:28 +0000 (17:55 -0400)
committerZachary Yedidia <zyedidia@gmail.com>
Wed, 25 Dec 2019 22:05:10 +0000 (17:05 -0500)
.gitignore
Makefile
cmd/micro/actions.go
cmd/micro/bufactionhandler.go
cmd/micro/buffer/cursor.go
cmd/micro/micro.go
cmd/micro/screen/screen.go
cmd/micro/statusline.go
cmd/micro/window.go

index acfa073027e48fd895125dead902e3d5e54a5284..910f4c58b9762214423b7d92e4152786840a7bec 100644 (file)
@@ -10,3 +10,4 @@ packages/
 todo.txt
 test.txt
 log.txt
+*.old
index 08a70768025b8e43b4d04eddbe6ea21cac8077f3..db9441a627dc4294d84faf8f38601c9704f3e1cb 100644 (file)
--- 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
index 1af5085cf89080b8e8c6cd52cbd6a3772646fdc9..2f39fe8c3cd42ca43bb1e7fbb48405151ae95c63 100644 (file)
@@ -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
 }
 
index 87cfd211912513f9747b02cd7347b5f19f27a88e..839c39b7612b54120562a855b59445f37ac5925e 100644 (file)
@@ -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
index 34741ad37c0f34a8094a1250da474c99413d67cb..78faf45b23803a078aad1565c5c20b24131b9e9e 100644 (file)
@@ -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()
+}
index c37467594b6317a9a27d7bf5a5d2cbb2c0f0a116..dca53a20aea822ade15f45c52c888437035dc953 100644 (file)
@@ -150,7 +150,7 @@ func main() {
 
        for {
                // Display everything
-               w.Clear()
+               screen.Screen.Fill(' ', config.DefStyle)
                w.DisplayBuffer()
                w.DisplayStatusLine()
                screen.Screen.Show()
index 0266a0f9343a5e49ec6f9468fac99403e84fed48..3653da321fe32de0ca4b1d77f95b9dc05325d230 100644 (file)
@@ -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)
 }
index 03df5601c1c49d9c3945a2c16b045a96f35ef5b8..5def796d62d507d94f7817a672e72a0089d284c2 100644 (file)
@@ -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() {
index 214895980c29a3f981b697a83f75b20603eb9932..b9cddaae9f5f5e9488a3978a33d7866adf3cb6f8 100644 (file)
@@ -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() {