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
}
// 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
}
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()
// 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)
}
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
}
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) {
}
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))
}
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) {
}
if !readonlyBindingsResult {
// call the key binding
- relocate = action(v, true) || relocate
+ relocate = action(curv, true) || relocate
// Macro
if funcName != "ToggleMacro" && funcName != "PlayMacro" {
if recordingMacro {
// 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
}
// DisplayView draws the view to the screen
func (v *View) DisplayView() {
if v.Type == vtTerm {
- v.term.Display(v)
+ v.term.Display()
return
}