-
Notifications
You must be signed in to change notification settings - Fork 8
/
save.go
79 lines (67 loc) · 1.64 KB
/
save.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2013 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 (
"fmt"
"github.com/limetext/backend"
)
type (
// Save command writes the currently
// opened file to the disk.
Save struct {
backend.DefaultCommand
}
// PromptSaveAs command lets us save
// the currently active
// file with a different name.
PromptSaveAs struct {
backend.DefaultCommand
}
// SaveAll command saves all the open files to the disk.
SaveAll struct {
backend.DefaultCommand
}
)
// Run executes the Save command.
func (c *Save) Run(v *backend.View, e *backend.Edit) error {
err := v.Save()
if err != nil {
backend.GetEditor().Frontend().ErrorMessage(fmt.Sprintf("Failed to save %s:n%s", v.FileName(), err))
return err
}
return nil
}
// Run executes the PromptSaveAs command.
func (c *PromptSaveAs) Run(v *backend.View, e *backend.Edit) error {
dir := viewDirectory(v)
fe := backend.GetEditor().Frontend()
files := fe.Prompt("Save file", dir, backend.PROMPT_SAVE_AS)
if len(files) == 0 {
return nil
}
name := files[0]
if err := v.SaveAs(name); err != nil {
fe.ErrorMessage(fmt.Sprintf("Failed to save as %s:%s", name, err))
return err
}
return nil
}
// Run executes the SaveAll command.
func (c *SaveAll) Run(w *backend.Window) error {
fe := backend.GetEditor().Frontend()
for _, v := range w.Views() {
if err := v.Save(); err != nil {
fe.ErrorMessage(fmt.Sprintf("Failed to save %s:n%s", v.FileName(), err))
return err
}
}
return nil
}
func init() {
register([]backend.Command{
&Save{},
&PromptSaveAs{},
&SaveAll{},
})
}