-
Notifications
You must be signed in to change notification settings - Fork 2
/
files.go
70 lines (64 loc) · 1.53 KB
/
files.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
package kg
import (
"io/ioutil"
"unicode"
)
// Refresh editor display(!)
func (e *Editor) Refresh() {}
// PosixFile foo
func (e *Editor) PosixFile(fname string) bool {
fn := []rune(fname)
if fn[0] == '_' {
return false
}
for f := range fn {
if (unicode.IsLetter(rune(f))) && f != '.' && f != '_' && f != '-' && f != '/' {
return false
}
}
return true
}
// Save foo
func (e *Editor) Save(fname string) bool {
if e.PosixFile(fname) != true {
e.msg("Not a portable POSIX file name.")
return false
}
d1 := []byte(e.CurrentBuffer.getText())
rch := d1[len(d1)-1]
if rch != '\n' {
prompt := "Last character is not newline. Add one?"
if e.yesno(true, prompt) {
d1 = append(d1, '\n')
}
}
err := ioutil.WriteFile(fname, d1, 0644)
if err != nil {
e.msg("Failed to save file \"%s\".", fname)
return false
}
e.CurrentBuffer.modified = false
e.msg("File \"%s\" %d bytes saved.", fname, len(d1))
return true
}
// LoadFile foo
// func (e *Editor) LoadFile(fname string) bool {
// return false
// }
// InsertFile reads file into buffer at point
func (e *Editor) InsertFile(fname string, modflag bool) bool {
bp := e.CurrentBuffer
dat, err := ioutil.ReadFile(fname)
if err != nil {
e.msg("Failed to read and insert file \"%s\".", fname)
}
if !modflag { // just do a load into buffer with no modification
bp.setText(string(dat))
bp.modified = false
} else { // insert into buffer and mark as modified.
bp.Insert(string(dat))
bp.modified = true
}
e.msg("File \"%s\" %d bytes read.", fname, len(dat))
return true
}