Add Open command for view

This commit is contained in:
Zachary Yedidia
2016-09-06 19:58:34 -04:00
parent 0373589ab8
commit e643860e3d
4 changed files with 46 additions and 17 deletions

View File

@@ -1,13 +1,11 @@
package main
import (
"io/ioutil"
"os"
"strconv"
"strings"
"time"
"github.com/mitchellh/go-homedir"
"github.com/yuin/gopher-lua"
"github.com/zyedidia/clipboard"
)
@@ -1018,18 +1016,7 @@ func (v *View) OpenFile(usePlugin bool) bool {
// the filename might or might not be quoted, so unquote first then join the strings.
filename = strings.Join(SplitCommandArgs(filename), " ")
home, _ := homedir.Dir()
filename = strings.Replace(filename, "~", home, 1)
file, err := ioutil.ReadFile(filename)
var buf *Buffer
if err != nil {
// File does not exist -- create an empty buffer with that name
buf = NewBuffer([]byte{}, filename)
} else {
buf = NewBuffer(file, filename)
}
v.OpenBuffer(buf)
v.Open(filename)
if usePlugin {
return PostActionCall("OpenFile", v)

View File

@@ -307,6 +307,7 @@ func main() {
L.SetGlobal("HandleShellCommand", luar.New(L, HandleShellCommand))
L.SetGlobal("GetLeadingWhitespace", luar.New(L, GetLeadingWhitespace))
L.SetGlobal("MakeCompletion", luar.New(L, MakeCompletion))
L.SetGlobal("NewBuffer", luar.New(L, NewBuffer))
// Used for asynchronous jobs
L.SetGlobal("JobStart", luar.New(L, JobStart))

File diff suppressed because one or more lines are too long

View File

@@ -1,11 +1,13 @@
package main
import (
"io/ioutil"
"strconv"
"strings"
"time"
"github.com/mattn/go-runewidth"
"github.com/mitchellh/go-homedir"
"github.com/zyedidia/tcell"
)
@@ -221,6 +223,22 @@ func (v *View) OpenBuffer(buf *Buffer) {
v.lastClickTime = time.Time{}
}
func (v *View) Open(filename string) {
home, _ := homedir.Dir()
filename = strings.Replace(filename, "~", home, 1)
file, err := ioutil.ReadFile(filename)
var buf *Buffer
if err != nil {
messenger.Message(err.Error())
// File does not exist -- create an empty buffer with that name
buf = NewBuffer([]byte{}, filename)
} else {
buf = NewBuffer(file, filename)
}
v.OpenBuffer(buf)
}
// CloseBuffer performs any closing functions on the buffer
func (v *View) CloseBuffer() {
if v.Buf != nil {