mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-30 14:47:16 +09:00
Start terminal emulator
This commit is contained in:
@@ -17,19 +17,19 @@ import (
|
||||
|
||||
// ScrollUp is not an action
|
||||
func (h *BufHandler) ScrollUp(n int) {
|
||||
v := h.Win.GetView()
|
||||
v := h.GetView()
|
||||
if v.StartLine >= n {
|
||||
v.StartLine -= n
|
||||
h.Win.SetView(v)
|
||||
h.SetView(v)
|
||||
}
|
||||
}
|
||||
|
||||
// ScrollDown is not an action
|
||||
func (h *BufHandler) ScrollDown(n int) {
|
||||
v := h.Win.GetView()
|
||||
v := h.GetView()
|
||||
if v.StartLine <= h.Buf.LinesNum()-1-n {
|
||||
v.StartLine += n
|
||||
h.Win.SetView(v)
|
||||
h.SetView(v)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,12 +38,12 @@ func (h *BufHandler) ScrollDown(n int) {
|
||||
func (h *BufHandler) MousePress(e *tcell.EventMouse) bool {
|
||||
b := h.Buf
|
||||
mx, my := e.Position()
|
||||
mouseLoc := h.Win.GetMouseLoc(buffer.Loc{mx, my})
|
||||
mouseLoc := h.GetMouseLoc(buffer.Loc{mx, my})
|
||||
h.Cursor.Loc = mouseLoc
|
||||
if h.mouseReleased {
|
||||
if b.NumCursors() > 1 {
|
||||
b.ClearCursors()
|
||||
h.Win.Relocate()
|
||||
h.Relocate()
|
||||
}
|
||||
if time.Since(h.lastClickTime)/time.Millisecond < config.DoubleClickThreshold && (mouseLoc.X == h.lastLoc.X && mouseLoc.Y == h.lastLoc.Y) {
|
||||
if h.doubleClick {
|
||||
@@ -104,7 +104,7 @@ func (h *BufHandler) ScrollDownAction() bool {
|
||||
|
||||
// Center centers the view on the cursor
|
||||
func (h *BufHandler) Center() bool {
|
||||
v := h.Win.GetView()
|
||||
v := h.GetView()
|
||||
v.StartLine = h.Cursor.Y - v.Height/2
|
||||
if v.StartLine+v.Height > h.Buf.LinesNum() {
|
||||
v.StartLine = h.Buf.LinesNum() - v.Height
|
||||
@@ -112,7 +112,7 @@ func (h *BufHandler) Center() bool {
|
||||
if v.StartLine < 0 {
|
||||
v.StartLine = 0
|
||||
}
|
||||
h.Win.SetView(v)
|
||||
h.SetView(v)
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -826,19 +826,19 @@ func (h *BufHandler) OpenFile() bool {
|
||||
|
||||
// Start moves the viewport to the start of the buffer
|
||||
func (h *BufHandler) Start() bool {
|
||||
v := h.Win.GetView()
|
||||
v := h.GetView()
|
||||
v.StartLine = 0
|
||||
h.Win.SetView(v)
|
||||
h.SetView(v)
|
||||
return false
|
||||
}
|
||||
|
||||
// End moves the viewport to the end of the buffer
|
||||
func (h *BufHandler) End() bool {
|
||||
// TODO: softwrap problems?
|
||||
v := h.Win.GetView()
|
||||
v := h.GetView()
|
||||
if v.Height > h.Buf.LinesNum() {
|
||||
v.StartLine = 0
|
||||
h.Win.SetView(v)
|
||||
h.SetView(v)
|
||||
} else {
|
||||
h.StartLine = h.Buf.LinesNum() - v.Height
|
||||
}
|
||||
@@ -847,19 +847,19 @@ func (h *BufHandler) End() bool {
|
||||
|
||||
// PageUp scrolls the view up a page
|
||||
func (h *BufHandler) PageUp() bool {
|
||||
v := h.Win.GetView()
|
||||
v := h.GetView()
|
||||
if v.StartLine > v.Height {
|
||||
h.ScrollUp(v.Height)
|
||||
} else {
|
||||
v.StartLine = 0
|
||||
}
|
||||
h.Win.SetView(v)
|
||||
h.SetView(v)
|
||||
return false
|
||||
}
|
||||
|
||||
// PageDown scrolls the view down a page
|
||||
func (h *BufHandler) PageDown() bool {
|
||||
v := h.Win.GetView()
|
||||
v := h.GetView()
|
||||
if h.Buf.LinesNum()-(v.StartLine+v.Height) > v.Height {
|
||||
h.ScrollDown(v.Height)
|
||||
} else if h.Buf.LinesNum() >= v.Height {
|
||||
@@ -873,7 +873,7 @@ func (h *BufHandler) SelectPageUp() bool {
|
||||
if !h.Cursor.HasSelection() {
|
||||
h.Cursor.OrigSelection[0] = h.Cursor.Loc
|
||||
}
|
||||
h.Cursor.UpN(h.Win.GetView().Height)
|
||||
h.Cursor.UpN(h.GetView().Height)
|
||||
h.Cursor.SelectTo(h.Cursor.Loc)
|
||||
return true
|
||||
}
|
||||
@@ -883,7 +883,7 @@ func (h *BufHandler) SelectPageDown() bool {
|
||||
if !h.Cursor.HasSelection() {
|
||||
h.Cursor.OrigSelection[0] = h.Cursor.Loc
|
||||
}
|
||||
h.Cursor.DownN(h.Win.GetView().Height)
|
||||
h.Cursor.DownN(h.GetView().Height)
|
||||
h.Cursor.SelectTo(h.Cursor.Loc)
|
||||
return true
|
||||
}
|
||||
@@ -897,7 +897,7 @@ func (h *BufHandler) CursorPageUp() bool {
|
||||
h.Cursor.ResetSelection()
|
||||
h.Cursor.StoreVisualX()
|
||||
}
|
||||
h.Cursor.UpN(h.Win.GetView().Height)
|
||||
h.Cursor.UpN(h.GetView().Height)
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -910,25 +910,25 @@ func (h *BufHandler) CursorPageDown() bool {
|
||||
h.Cursor.ResetSelection()
|
||||
h.Cursor.StoreVisualX()
|
||||
}
|
||||
h.Cursor.DownN(h.Win.GetView().Height)
|
||||
h.Cursor.DownN(h.GetView().Height)
|
||||
return true
|
||||
}
|
||||
|
||||
// HalfPageUp scrolls the view up half a page
|
||||
func (h *BufHandler) HalfPageUp() bool {
|
||||
v := h.Win.GetView()
|
||||
v := h.GetView()
|
||||
if v.StartLine > v.Height/2 {
|
||||
h.ScrollUp(v.Height / 2)
|
||||
} else {
|
||||
v.StartLine = 0
|
||||
}
|
||||
h.Win.SetView(v)
|
||||
h.SetView(v)
|
||||
return false
|
||||
}
|
||||
|
||||
// HalfPageDown scrolls the view down half a page
|
||||
func (h *BufHandler) HalfPageDown() bool {
|
||||
v := h.Win.GetView()
|
||||
v := h.GetView()
|
||||
if h.Buf.LinesNum()-(v.StartLine+v.Height) > v.Height/2 {
|
||||
h.ScrollDown(v.Height / 2)
|
||||
} else {
|
||||
@@ -936,7 +936,7 @@ func (h *BufHandler) HalfPageDown() bool {
|
||||
v.StartLine = h.Buf.LinesNum() - v.Height
|
||||
}
|
||||
}
|
||||
h.Win.SetView(v)
|
||||
h.SetView(v)
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -1193,7 +1193,7 @@ func (h *BufHandler) SpawnMultiCursorSelect() bool {
|
||||
func (h *BufHandler) MouseMultiCursor(e *tcell.EventMouse) bool {
|
||||
b := h.Buf
|
||||
mx, my := e.Position()
|
||||
mouseLoc := h.Win.GetMouseLoc(buffer.Loc{X: mx, Y: my})
|
||||
mouseLoc := h.GetMouseLoc(buffer.Loc{X: mx, Y: my})
|
||||
c := buffer.NewCursor(b, mouseLoc)
|
||||
b.AddCursor(c)
|
||||
b.MergeCursors()
|
||||
@@ -1219,7 +1219,7 @@ func (h *BufHandler) SkipMultiCursor() bool {
|
||||
lastC.Loc = lastC.CurSelection[1]
|
||||
|
||||
h.Buf.MergeCursors()
|
||||
h.Win.Relocate()
|
||||
h.Relocate()
|
||||
} else {
|
||||
InfoBar.Message("No matches found")
|
||||
}
|
||||
|
||||
@@ -52,8 +52,9 @@ func BufMapMouse(k MouseEvent, action string) {
|
||||
// The ActionHandler can access the window for necessary info about
|
||||
// visual positions for mouse clicks and scrolling
|
||||
type BufHandler struct {
|
||||
display.Window
|
||||
|
||||
Buf *buffer.Buffer
|
||||
Win display.Window
|
||||
|
||||
cursors []*buffer.Cursor
|
||||
Cursor *buffer.Cursor // the active cursor
|
||||
@@ -100,7 +101,7 @@ type BufHandler struct {
|
||||
func NewBufHandler(buf *buffer.Buffer, win display.Window) *BufHandler {
|
||||
h := new(BufHandler)
|
||||
h.Buf = buf
|
||||
h.Win = win
|
||||
h.Window = win
|
||||
|
||||
h.cursors = []*buffer.Cursor{buffer.NewCursor(buf, buf.StartCursor)}
|
||||
h.Cursor = h.cursors[0]
|
||||
@@ -133,7 +134,7 @@ func (h *BufHandler) HandleEvent(event tcell.Event) {
|
||||
// Mouse was just released
|
||||
|
||||
mx, my := e.Position()
|
||||
mouseLoc := h.Win.GetMouseLoc(buffer.Loc{X: mx, Y: my})
|
||||
mouseLoc := h.GetMouseLoc(buffer.Loc{X: mx, Y: my})
|
||||
|
||||
// Relocating here isn't really necessary because the cursor will
|
||||
// be in the right place from the last mouse event
|
||||
@@ -171,14 +172,14 @@ func (h *BufHandler) DoKeyEvent(e KeyEvent) bool {
|
||||
h.Buf.SetCurCursor(c.Num)
|
||||
h.Cursor = c
|
||||
if action(h) {
|
||||
h.Win.Relocate()
|
||||
h.Relocate()
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
if action(h) {
|
||||
h.Win.Relocate()
|
||||
h.Relocate()
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -190,7 +191,7 @@ func (h *BufHandler) DoKeyEvent(e KeyEvent) bool {
|
||||
func (h *BufHandler) DoMouseEvent(e MouseEvent, te *tcell.EventMouse) bool {
|
||||
if action, ok := BufMouseBindings[e]; ok {
|
||||
if action(h, te) {
|
||||
h.Win.Relocate()
|
||||
h.Relocate()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -7,6 +7,12 @@ import (
|
||||
"github.com/zyedidia/micro/cmd/micro/views"
|
||||
)
|
||||
|
||||
type Pane interface {
|
||||
Handler
|
||||
display.Window
|
||||
ID() uint64
|
||||
}
|
||||
|
||||
type EditPane struct {
|
||||
display.Window
|
||||
*BufHandler
|
||||
|
||||
@@ -37,7 +37,5 @@ type MouseAction func(Handler, tcell.EventMouse) bool
|
||||
// A Handler will take a tcell event and execute it
|
||||
// appropriately
|
||||
type Handler interface {
|
||||
// DoKeyEvent(KeyEvent) bool
|
||||
// DoMouseEvent(MouseEvent, *tcell.EventMouse) (MouseAction, bool)
|
||||
HandleEvent(tcell.Event)
|
||||
}
|
||||
|
||||
66
cmd/micro/action/termhandler.go
Normal file
66
cmd/micro/action/termhandler.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package action
|
||||
|
||||
import (
|
||||
"github.com/zyedidia/clipboard"
|
||||
"github.com/zyedidia/micro/cmd/micro/display"
|
||||
"github.com/zyedidia/micro/cmd/micro/shell"
|
||||
"github.com/zyedidia/tcell"
|
||||
"github.com/zyedidia/terminal"
|
||||
)
|
||||
|
||||
type TermHandler struct {
|
||||
*shell.Terminal
|
||||
display.Window
|
||||
|
||||
mouseReleased bool
|
||||
}
|
||||
|
||||
// HandleEvent handles a tcell event by forwarding it to the terminal emulator
|
||||
// If the event is a mouse event and the program running in the emulator
|
||||
// does not have mouse support, the emulator will support selections and
|
||||
// copy-paste
|
||||
func (t *TermHandler) HandleEvent(event tcell.Event) {
|
||||
if e, ok := event.(*tcell.EventKey); ok {
|
||||
if t.Status == shell.TTDone {
|
||||
switch e.Key() {
|
||||
case tcell.KeyEscape, tcell.KeyCtrlQ, tcell.KeyEnter:
|
||||
t.Close()
|
||||
default:
|
||||
}
|
||||
}
|
||||
if e.Key() == tcell.KeyCtrlC && t.HasSelection() {
|
||||
clipboard.WriteAll(t.GetSelection(t.GetView().Width), "clipboard")
|
||||
InfoBar.Message("Copied selection to clipboard")
|
||||
} else if t.Status != shell.TTDone {
|
||||
t.WriteString(event.EscSeq())
|
||||
}
|
||||
} else if e, ok := event.(*tcell.EventMouse); !ok || t.State.Mode(terminal.ModeMouseMask) {
|
||||
t.WriteString(event.EscSeq())
|
||||
} else {
|
||||
x, y := e.Position()
|
||||
v := t.GetView()
|
||||
x -= v.X
|
||||
y += v.Y
|
||||
|
||||
if e.Buttons() == tcell.Button1 {
|
||||
if !t.mouseReleased {
|
||||
// drag
|
||||
t.Selection[1].X = x
|
||||
t.Selection[1].Y = y
|
||||
} else {
|
||||
t.Selection[0].X = x
|
||||
t.Selection[0].Y = y
|
||||
t.Selection[1].X = x
|
||||
t.Selection[1].Y = y
|
||||
}
|
||||
|
||||
t.mouseReleased = false
|
||||
} else if e.Buttons() == tcell.ButtonNone {
|
||||
if !t.mouseReleased {
|
||||
t.Selection[1].X = x
|
||||
t.Selection[1].Y = y
|
||||
}
|
||||
t.mouseReleased = true
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user