Add OpenBuffer() function to View

Fixes #43
This commit is contained in:
Zachary Yedidia 2016-04-19 18:58:52 -04:00
parent aaa0f29540
commit c767b3dc0c
2 changed files with 26 additions and 3 deletions

View file

@ -211,7 +211,7 @@ func main() {
case tcell.KeyCtrlQ:
// Make sure not to quit if there are unsaved changes
if helpOpen {
view.buf = buf
view.OpenBuffer(buf)
helpOpen = false
} else {
if view.CanClose("Quit anyway? (yes, no, save) ") {
@ -234,9 +234,9 @@ func main() {
helpBuffer := NewBuffer(helpTxt, "")
helpBuffer.name = "Help"
helpOpen = true
view.buf = helpBuffer
view.OpenBuffer(helpBuffer)
} else {
view.buf = buf
view.OpenBuffer(buf)
helpOpen = false
}
}

View file

@ -276,6 +276,29 @@ func (v *View) SelectAll() {
v.cursor.y = 0
}
// OpenBuffer opens a new buffer in this view.
// This resets the topline, event handler and cursor.
func (v *View) OpenBuffer(buf *Buffer) {
v.buf = buf
v.topline = 0
// Put the cursor at the first spot
v.cursor = Cursor{
x: 0,
y: 0,
v: v,
}
v.cursor.ResetSelection()
v.eh = NewEventHandler(v)
v.matches = Match(v)
// Set mouseReleased to true because we assume the mouse is not being pressed when
// the editor is opened
v.mouseReleased = true
v.lastClickTime = time.Time{}
}
// OpenFile opens a new file in the current view
// It makes sure that the current buffer can be closed first (unsaved changes)
func (v *View) OpenFile() {