mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-30 14:47:16 +09:00
Add Open command for view
This commit is contained in:
@@ -1,13 +1,11 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mitchellh/go-homedir"
|
|
||||||
"github.com/yuin/gopher-lua"
|
"github.com/yuin/gopher-lua"
|
||||||
"github.com/zyedidia/clipboard"
|
"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.
|
// the filename might or might not be quoted, so unquote first then join the strings.
|
||||||
filename = strings.Join(SplitCommandArgs(filename), " ")
|
filename = strings.Join(SplitCommandArgs(filename), " ")
|
||||||
|
|
||||||
home, _ := homedir.Dir()
|
v.Open(filename)
|
||||||
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)
|
|
||||||
|
|
||||||
if usePlugin {
|
if usePlugin {
|
||||||
return PostActionCall("OpenFile", v)
|
return PostActionCall("OpenFile", v)
|
||||||
|
|||||||
@@ -307,6 +307,7 @@ func main() {
|
|||||||
L.SetGlobal("HandleShellCommand", luar.New(L, HandleShellCommand))
|
L.SetGlobal("HandleShellCommand", luar.New(L, HandleShellCommand))
|
||||||
L.SetGlobal("GetLeadingWhitespace", luar.New(L, GetLeadingWhitespace))
|
L.SetGlobal("GetLeadingWhitespace", luar.New(L, GetLeadingWhitespace))
|
||||||
L.SetGlobal("MakeCompletion", luar.New(L, MakeCompletion))
|
L.SetGlobal("MakeCompletion", luar.New(L, MakeCompletion))
|
||||||
|
L.SetGlobal("NewBuffer", luar.New(L, NewBuffer))
|
||||||
|
|
||||||
// Used for asynchronous jobs
|
// Used for asynchronous jobs
|
||||||
L.SetGlobal("JobStart", luar.New(L, JobStart))
|
L.SetGlobal("JobStart", luar.New(L, JobStart))
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@@ -1,11 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mattn/go-runewidth"
|
"github.com/mattn/go-runewidth"
|
||||||
|
"github.com/mitchellh/go-homedir"
|
||||||
"github.com/zyedidia/tcell"
|
"github.com/zyedidia/tcell"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -221,6 +223,22 @@ func (v *View) OpenBuffer(buf *Buffer) {
|
|||||||
v.lastClickTime = time.Time{}
|
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
|
// CloseBuffer performs any closing functions on the buffer
|
||||||
func (v *View) CloseBuffer() {
|
func (v *View) CloseBuffer() {
|
||||||
if v.Buf != nil {
|
if v.Buf != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user