mirror of
https://github.com/zyedidia/micro.git
synced 2026-02-04 22:20:20 +09:00
Cursor improvements
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -10,3 +10,4 @@ packages/
|
|||||||
todo.txt
|
todo.txt
|
||||||
test.txt
|
test.txt
|
||||||
log.txt
|
log.txt
|
||||||
|
*.old
|
||||||
|
|||||||
6
Makefile
6
Makefile
@@ -40,9 +40,9 @@ update:
|
|||||||
# Builds the runtime
|
# Builds the runtime
|
||||||
runtime:
|
runtime:
|
||||||
go get -u github.com/jteeuwen/go-bindata/...
|
go get -u github.com/jteeuwen/go-bindata/...
|
||||||
$(GOBIN)/go-bindata -nometadata -o runtime.go runtime/...
|
$(GOBIN)/go-bindata -pkg config -nomemcopy -nometadata -o runtime.go runtime/...
|
||||||
mv runtime.go cmd/micro
|
mv runtime.go cmd/micro/config
|
||||||
gofmt -w cmd/micro/runtime.go
|
gofmt -w cmd/micro/config/runtime.go
|
||||||
|
|
||||||
test:
|
test:
|
||||||
go test ./cmd/micro
|
go test ./cmd/micro
|
||||||
|
|||||||
@@ -30,21 +30,29 @@ func (a *BufActionHandler) Center() bool {
|
|||||||
|
|
||||||
// CursorUp moves the cursor up
|
// CursorUp moves the cursor up
|
||||||
func (a *BufActionHandler) CursorUp() bool {
|
func (a *BufActionHandler) CursorUp() bool {
|
||||||
|
a.Cursor.Deselect(true)
|
||||||
|
a.Cursor.Up()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// CursorDown moves the cursor down
|
// CursorDown moves the cursor down
|
||||||
func (a *BufActionHandler) CursorDown() bool {
|
func (a *BufActionHandler) CursorDown() bool {
|
||||||
|
a.Cursor.Deselect(true)
|
||||||
|
a.Cursor.Down()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// CursorLeft moves the cursor left
|
// CursorLeft moves the cursor left
|
||||||
func (a *BufActionHandler) CursorLeft() bool {
|
func (a *BufActionHandler) CursorLeft() bool {
|
||||||
|
a.Cursor.Deselect(true)
|
||||||
|
a.Cursor.Left()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// CursorRight moves the cursor right
|
// CursorRight moves the cursor right
|
||||||
func (a *BufActionHandler) CursorRight() bool {
|
func (a *BufActionHandler) CursorRight() bool {
|
||||||
|
a.Cursor.Deselect(true)
|
||||||
|
a.Cursor.Right()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -74,6 +74,7 @@ func NewBufActionHandler(buf *buffer.Buffer, win *Window) *BufActionHandler {
|
|||||||
Buf: buf,
|
Buf: buf,
|
||||||
Loc: buf.StartCursor,
|
Loc: buf.StartCursor,
|
||||||
}}
|
}}
|
||||||
|
a.Cursor = a.cursors[0]
|
||||||
|
|
||||||
buf.SetCursors(a.cursors)
|
buf.SetCursors(a.cursors)
|
||||||
return a
|
return a
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ func (c *Cursor) Goto(b Cursor) {
|
|||||||
// the current cursor its selection too
|
// the current cursor its selection too
|
||||||
func (c *Cursor) GotoLoc(l Loc) {
|
func (c *Cursor) GotoLoc(l Loc) {
|
||||||
c.X, c.Y = l.X, l.Y
|
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
|
// 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)
|
width += runewidth.RuneWidth(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
i++
|
|
||||||
|
|
||||||
if width >= visualPos {
|
if width >= visualPos {
|
||||||
|
if width == visualPos {
|
||||||
|
i++
|
||||||
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
return 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
|
// GetSelection returns the cursor's selection
|
||||||
func (c *Cursor) GetSelection() []byte {
|
func (c *Cursor) GetSelection() []byte {
|
||||||
if InBounds(c.CurSelection[0], c.Buf) && InBounds(c.CurSelection[1], c.Buf) {
|
if InBounds(c.CurSelection[0], c.Buf) && InBounds(c.CurSelection[1], c.Buf) {
|
||||||
@@ -244,7 +261,7 @@ func (c *Cursor) Left() {
|
|||||||
c.Up()
|
c.Up()
|
||||||
c.End()
|
c.End()
|
||||||
}
|
}
|
||||||
c.LastVisualX = c.GetVisualX()
|
c.StoreVisualX()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Right moves the cursor right one cell (if possible) or
|
// Right moves the cursor right one cell (if possible) or
|
||||||
@@ -259,7 +276,7 @@ func (c *Cursor) Right() {
|
|||||||
c.Down()
|
c.Down()
|
||||||
c.Start()
|
c.Start()
|
||||||
}
|
}
|
||||||
c.LastVisualX = c.GetVisualX()
|
c.StoreVisualX()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Relocate makes sure that the cursor is inside the bounds
|
// 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))
|
c.X = utf8.RuneCount(c.Buf.LineBytes(c.Y))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Cursor) StoreVisualX() {
|
||||||
|
c.LastVisualX = c.GetVisualX()
|
||||||
|
}
|
||||||
|
|||||||
@@ -150,7 +150,7 @@ func main() {
|
|||||||
|
|
||||||
for {
|
for {
|
||||||
// Display everything
|
// Display everything
|
||||||
w.Clear()
|
screen.Screen.Fill(' ', config.DefStyle)
|
||||||
w.DisplayBuffer()
|
w.DisplayBuffer()
|
||||||
w.DisplayStatusLine()
|
w.DisplayStatusLine()
|
||||||
screen.Screen.Show()
|
screen.Screen.Show()
|
||||||
|
|||||||
@@ -40,12 +40,12 @@ func Init() {
|
|||||||
Screen, err = tcell.NewScreen()
|
Screen, err = tcell.NewScreen()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
fmt.Println("Fatal: Micro could not initialize a screen.")
|
fmt.Println("Fatal: Micro could not initialize a Screen.")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
fmt.Println("Fatal: Micro could not initialize a screen.")
|
fmt.Println("Fatal: Micro could not initialize a Screen.")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -64,4 +64,6 @@ func Init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
os.Setenv("TCELLDB", tcelldb)
|
os.Setenv("TCELLDB", tcelldb)
|
||||||
|
|
||||||
|
// Screen.SetStyle(defStyle)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,8 +30,8 @@ type StatusLine struct {
|
|||||||
// NewStatusLine returns a statusline bound to a window
|
// NewStatusLine returns a statusline bound to a window
|
||||||
func NewStatusLine(win *Window) *StatusLine {
|
func NewStatusLine(win *Window) *StatusLine {
|
||||||
s := new(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.FormatRight = "$(bind:ToggleKeyMenu): show bindings, $(bind:ToggleHelp): open help"
|
||||||
s.Info = map[string]func(*buffer.Buffer) string{
|
s.Info = map[string]func(*buffer.Buffer) string{
|
||||||
"filename": func(b *buffer.Buffer) string {
|
"filename": func(b *buffer.Buffer) string {
|
||||||
@@ -41,10 +41,10 @@ func NewStatusLine(win *Window) *StatusLine {
|
|||||||
return b.GetName()
|
return b.GetName()
|
||||||
},
|
},
|
||||||
"line": func(b *buffer.Buffer) string {
|
"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 {
|
"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 {
|
"modified": func(b *buffer.Buffer) string {
|
||||||
if b.Modified() {
|
if b.Modified() {
|
||||||
|
|||||||
@@ -82,6 +82,15 @@ func (w *Window) GetStyle(style tcell.Style, bloc buffer.Loc, r rune) tcell.Styl
|
|||||||
return style
|
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
|
// DisplayBuffer draws the buffer being shown in this window on the screen.Screen
|
||||||
func (w *Window) DisplayBuffer() {
|
func (w *Window) DisplayBuffer() {
|
||||||
b := w.Buf
|
b := w.Buf
|
||||||
@@ -134,6 +143,10 @@ func (w *Window) DisplayBuffer() {
|
|||||||
line, nColsBeforeStart := util.SliceVisualEnd(line, bloc.X, tabsize)
|
line, nColsBeforeStart := util.SliceVisualEnd(line, bloc.X, tabsize)
|
||||||
totalwidth := bloc.X - nColsBeforeStart
|
totalwidth := bloc.X - nColsBeforeStart
|
||||||
for len(line) > 0 {
|
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)
|
r, size := utf8.DecodeRune(line)
|
||||||
|
|
||||||
curStyle = w.GetStyle(curStyle, bloc, r)
|
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.X = w.StartCol
|
||||||
bloc.Y++
|
bloc.Y++
|
||||||
if bloc.Y >= b.LinesNum() {
|
if bloc.Y >= b.LinesNum() {
|
||||||
|
|||||||
Reference in New Issue
Block a user