forked from limetext/commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
63 lines (55 loc) · 1.25 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2014 The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package commands
import (
"os"
"os/user"
"path"
"github.com/limetext/backend"
)
type (
// NewFile command creates a new file.
NewFile struct {
backend.DefaultCommand
}
// PromptOpenFile command prompts opening
// an existing file from the filesystem.
PromptOpenFile struct {
backend.DefaultCommand
}
)
// Run executes the NewFile command.
func (c *NewFile) Run(w *backend.Window) error {
ed := backend.GetEditor()
ed.ActiveWindow().NewFile()
return nil
}
// Run executes the PromptOpenFile command.
func (o *PromptOpenFile) Run(w *backend.Window) error {
dir := viewDirectory(w.ActiveView())
fe := backend.GetEditor().Frontend()
files := fe.Prompt("Open file", dir, backend.PROMPT_SELECT_MULTIPLE)
for _, file := range files {
w.OpenFile(file, 0)
}
return nil
}
func viewDirectory(v *backend.View) string {
if v != nil && v.FileName() != "" {
p := path.Dir(v.FileName())
if _, err := os.Stat(p); err == nil {
return p
}
}
if usr, err := user.Current(); err == nil {
return usr.HomeDir
}
return "/"
}
func init() {
register([]backend.Command{
&NewFile{},
&PromptOpenFile{},
})
}