]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/view.go
Code optimisation (#1117)
[micro.git] / cmd / micro / view.go
index 627634286f1d18dd2fe4b0448a7edc1ceb1777ad..d6d9470f2dac66d2f4b91cd230125857cdec6155 100644 (file)
@@ -2,15 +2,12 @@ package main
 
 import (
        "fmt"
-       "os"
        "reflect"
        "strconv"
        "strings"
        "time"
 
-       "github.com/zyedidia/clipboard"
        "github.com/zyedidia/tcell"
-       "github.com/zyedidia/terminal"
 )
 
 // The ViewType defines what kind of view this is
@@ -167,8 +164,10 @@ func (v *View) ToggleStatusLine() {
 }
 
 // StartTerminal execs a command in this view
-func (v *View) StartTerminal(execCmd []string) error {
-       err := v.term.Start(execCmd, v)
+func (v *View) StartTerminal(execCmd []string, wait bool, getOutput bool, luaCallback string) error {
+       err := v.term.Start(execCmd, v, getOutput)
+       v.term.wait = wait
+       v.term.callback = luaCallback
        if err == nil {
                v.term.Resize(v.Width, v.Height)
                v.Type = vtTerm
@@ -199,7 +198,10 @@ func (v *View) ToggleTabbar() {
 }
 
 func (v *View) paste(clip string) {
-       leadingWS := GetLeadingWhitespace(v.Buf.Line(v.Cursor.Y))
+       leadingWS := ""
+       if v.Cursor.X > 0 {
+               leadingWS = GetLeadingWhitespace(v.Buf.Line(v.Cursor.Y))
+       }
 
        if v.Cursor.HasSelection() {
                v.Cursor.DeleteSelection()
@@ -277,28 +279,17 @@ func (v *View) OpenBuffer(buf *Buffer) {
        // is opened
        v.isOverwriteMode = false
        v.lastClickTime = time.Time{}
+
+       GlobalPluginCall("onBufferOpen", v.Buf)
+       GlobalPluginCall("onViewOpen", v)
 }
 
 // Open opens the given file in the view
-func (v *View) Open(filename string) {
-       filename = ReplaceHome(filename)
-       file, err := os.Open(filename)
-       fileInfo, _ := os.Stat(filename)
-
-       if err == nil && fileInfo.IsDir() {
-               messenger.Error(filename, " is a directory")
-               return
-       }
-
-       defer file.Close()
-
-       var buf *Buffer
+func (v *View) Open(path string) {
+       buf, err := NewBufferFromFile(path)
        if err != nil {
-               messenger.Message(err.Error())
-               // File does not exist -- create an empty buffer with that name
-               buf = NewBufferFromString("", filename)
-       } else {
-               buf = NewBuffer(file, FSize(file), filename)
+               messenger.Error(err)
+               return
        }
        v.OpenBuffer(buf)
 }
@@ -448,7 +439,7 @@ func (v *View) Relocate() bool {
        if cy > v.Topline+height-1-scrollmargin && cy < v.Buf.NumLines-scrollmargin {
                v.Topline = cy - height + 1 + scrollmargin
                ret = true
-       } else if cy >= v.Buf.NumLines-scrollmargin && cy > height {
+       } else if cy >= v.Buf.NumLines-scrollmargin && cy >= height {
                v.Topline = v.Buf.NumLines - height
                ret = true
        }
@@ -467,6 +458,31 @@ func (v *View) Relocate() bool {
        return ret
 }
 
+// GetMouseClickLocation gets the location in the buffer from a mouse click
+// on the screen
+func (v *View) GetMouseClickLocation(x, y int) (int, int) {
+       x -= v.lineNumOffset - v.leftCol + v.x
+       y += v.Topline - v.y
+
+       if y-v.Topline > v.Height-1 {
+               v.ScrollDown(1)
+               y = v.Height + v.Topline - 1
+       }
+       if y < 0 {
+               y = 0
+       }
+       if x < 0 {
+               x = 0
+       }
+
+       newX, newY := v.GetSoftWrapLocation(x, y)
+       if newX > Count(v.Buf.Line(newY)) {
+               newX = Count(v.Buf.Line(newY))
+       }
+
+       return newX, newY
+}
+
 // MoveToMouseClick moves the cursor to location x, y assuming x, y were given
 // by a mouse click
 func (v *View) MoveToMouseClick(x, y int) {
@@ -482,7 +498,6 @@ func (v *View) MoveToMouseClick(x, y int) {
        }
 
        x, y = v.GetSoftWrapLocation(x, y)
-       // x = v.Cursor.GetCharPosInLine(y, x)
        if x > Count(v.Buf.Line(y)) {
                x = Count(v.Buf.Line(y))
        }
@@ -498,7 +513,8 @@ func (v *View) ExecuteActions(actions []func(*View, bool) bool) bool {
        for _, action := range actions {
                readonlyBindingsResult := false
                funcName := ShortFuncName(action)
-               if v.Type.Readonly == true {
+               curv := CurView()
+               if curv.Type.Readonly == true {
                        // check for readonly and if true only let key bindings get called if they do not change the contents.
                        for _, readonlyBindings := range readonlyBindingsList {
                                if strings.Contains(funcName, readonlyBindings) {
@@ -508,7 +524,7 @@ func (v *View) ExecuteActions(actions []func(*View, bool) bool) bool {
                }
                if !readonlyBindingsResult {
                        // call the key binding
-                       relocate = action(v, true) || relocate
+                       relocate = action(curv, true) || relocate
                        // Macro
                        if funcName != "ToggleMacro" && funcName != "PlayMacro" {
                                if recordingMacro {
@@ -535,49 +551,7 @@ func (v *View) SetCursor(c *Cursor) bool {
 // HandleEvent handles an event passed by the main loop
 func (v *View) HandleEvent(event tcell.Event) {
        if v.Type == vtTerm {
-               if e, ok := event.(*tcell.EventKey); ok {
-                       if v.term.status == VTDone {
-                               switch e.Key() {
-                               case tcell.KeyEscape, tcell.KeyCtrlQ, tcell.KeyEnter:
-                                       v.term.Close()
-                                       v.Type = vtDefault
-                               default:
-                               }
-                       }
-                       if e.Key() == tcell.KeyCtrlC && v.term.HasSelection() {
-                               clipboard.WriteAll(v.term.GetSelection(v.Width), "clipboard")
-                               messenger.Message("Copied selection to clipboard")
-                       } else if v.term.status != VTDone {
-                               v.term.WriteString(event.EscSeq())
-                       }
-               } else if e, ok := event.(*tcell.EventMouse); !ok || v.term.state.Mode(terminal.ModeMouseMask) {
-                       v.term.WriteString(event.EscSeq())
-               } else {
-                       x, y := e.Position()
-                       x -= v.x
-                       y += v.y
-
-                       if e.Buttons() == tcell.Button1 {
-                               if !v.mouseReleased {
-                                       // drag
-                                       v.term.selection[1].X = x
-                                       v.term.selection[1].Y = y
-                               } else {
-                                       v.term.selection[0].X = x
-                                       v.term.selection[0].Y = y
-                                       v.term.selection[1].X = x
-                                       v.term.selection[1].Y = y
-                               }
-
-                               v.mouseReleased = false
-                       } else if e.Buttons() == tcell.ButtonNone {
-                               if !v.mouseReleased {
-                                       v.term.selection[1].X = x
-                                       v.term.selection[1].Y = y
-                               }
-                               v.mouseReleased = true
-                       }
-               }
+               v.term.HandleEvent(event)
                return
        }
 
@@ -816,7 +790,7 @@ func (v *View) openHelp(helpPage string) {
 // DisplayView draws the view to the screen
 func (v *View) DisplayView() {
        if v.Type == vtTerm {
-               v.term.Display(v)
+               v.term.Display()
                return
        }