14 "github.com/zyedidia/micro/internal/config"
15 "github.com/zyedidia/micro/internal/screen"
16 "github.com/zyedidia/micro/internal/util"
17 "golang.org/x/text/encoding"
18 "golang.org/x/text/encoding/htmlindex"
19 "golang.org/x/text/transform"
22 // LargeFileThreshold is the number of bytes when fastdirty is forced
23 // because hashing is too slow
24 const LargeFileThreshold = 50000
26 // overwriteFile opens the given file for writing, truncating if one exists, and then calls
27 // the supplied function with the file as io.Writer object, also making sure the file is
29 func overwriteFile(name string, enc encoding.Encoding, fn func(io.Writer) error) (err error) {
32 if file, err = os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
37 if e := file.Close(); e != nil && err == nil {
42 w := transform.NewWriter(file, enc.NewEncoder())
43 // w := bufio.NewWriter(file)
45 if err = fn(w); err != nil {
53 // overwriteFileAsRoot executes dd as root and then calls the supplied function
54 // with dd's standard input as an io.Writer object. Dd opens the given file for writing,
55 // truncating it if it exists, and writes what it receives on its standard input to the file.
56 func overwriteFileAsRoot(name string, enc encoding.Encoding, fn func(io.Writer) error) (err error) {
57 cmd := exec.Command(config.GlobalSettings["sucmd"].(string), "dd", "status=none", "bs=4K", "of="+name)
58 var stdin io.WriteCloser
60 screenb := screen.TempFini()
62 // This is a trap for Ctrl-C so that it doesn't kill micro
63 // Instead we trap Ctrl-C to kill the program we're running
64 c := make(chan os.Signal, 1)
65 signal.Notify(c, os.Interrupt)
72 if stdin, err = cmd.StdinPipe(); err != nil {
76 if err = cmd.Start(); err != nil {
82 if err = stdin.Close(); err != nil {
86 if err = cmd.Wait(); err != nil {
90 screen.TempStart(screenb)
95 // Save saves the buffer to its default path
96 func (b *Buffer) Save() error {
97 return b.SaveAs(b.Path)
100 // SaveAs saves the buffer to a specified path (filename), creating the file if it does not exist
101 func (b *Buffer) SaveAs(filename string) error {
102 return b.saveToFile(filename, false)
105 func (b *Buffer) SaveWithSudo() error {
106 return b.SaveAsWithSudo(b.Path)
109 func (b *Buffer) SaveAsWithSudo(filename string) error {
110 return b.saveToFile(filename, true)
113 func (b *Buffer) saveToFile(filename string, withSudo bool) error {
116 return errors.New("Cannot save readonly buffer")
119 return errors.New("Cannot save scratch buffer")
123 if b.Settings["rmtrailingws"].(bool) {
124 for i, l := range b.lines {
125 leftover := utf8.RuneCount(bytes.TrimRightFunc(l.data, unicode.IsSpace))
127 linelen := utf8.RuneCount(l.data)
128 b.Remove(Loc{leftover, i}, Loc{linelen, i})
134 if b.Settings["eofnewline"].(bool) {
136 if b.RuneAt(Loc{end.X - 1, end.Y}) != '\n' {
141 // Update the last time this file was updated after saving
143 b.ModTime, _ = util.GetModTime(filename)
147 // Removes any tilde and replaces with the absolute path to home
148 absFilename, _ := util.ReplaceHome(filename)
150 // Get the leading path to the file | "." is returned if there's no leading path provided
151 if dirname := filepath.Dir(absFilename); dirname != "." {
152 // Check if the parent dirs don't exist
153 if _, statErr := os.Stat(dirname); os.IsNotExist(statErr) {
154 // Prompt to make sure they want to create the dirs that are missing
155 if b.Settings["mkparents"].(bool) {
156 // Create all leading dir(s) since they don't exist
157 if mkdirallErr := os.MkdirAll(dirname, os.ModePerm); mkdirallErr != nil {
158 // If there was an error creating the dirs
162 return errors.New("Parent dirs don't exist, enable 'mkparents' for auto creation")
169 enc, err := htmlindex.Get(b.Settings["encoding"].(string))
174 fwriter := func(file io.Writer) (e error) {
175 if len(b.lines) == 0 {
181 if b.Endings == FFDos {
182 eol = []byte{'\r', '\n'}
188 if fileSize, e = file.Write(b.lines[0].data); e != nil {
192 for _, l := range b.lines[1:] {
193 if _, e = file.Write(eol); e != nil {
196 if _, e = file.Write(l.data); e != nil {
199 fileSize += len(eol) + len(l.data)
205 err = overwriteFileAsRoot(absFilename, enc, fwriter)
207 err = overwriteFile(absFilename, enc, fwriter)
214 if !b.Settings["fastdirty"].(bool) {
215 if fileSize > LargeFileThreshold {
216 // For large files 'fastdirty' needs to be on
217 b.Settings["fastdirty"] = true
219 calcHash(b, &b.origHash)
224 absPath, _ := filepath.Abs(filename)