Add autosave option

This commit is contained in:
Zachary Yedidia 2019-08-04 14:22:24 -07:00
parent bc6dd990e5
commit c0293b5d0e
9 changed files with 73 additions and 29 deletions

View File

@ -233,6 +233,10 @@ func main() {
case f := <-shell.Jobs:
// If a new job has finished while running in the background we should execute the callback
f.Function(f.Output, f.Args...)
case <-config.Autosave:
for _, b := range buffer.OpenBuffers {
b.Save()
}
case event = <-events:
case <-screen.DrawChan:
}

View File

@ -1162,14 +1162,20 @@ func (h *BufPane) Quit() bool {
}
}
if h.Buf.Modified() {
InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) {
if !canceled && !yes {
quit()
} else if !canceled && yes {
h.Save()
quit()
}
})
if config.GlobalSettings["autosave"].(float64) > 0 {
// autosave on means we automatically save when quitting
h.Save()
quit()
} else {
InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) {
if !canceled && !yes {
quit()
} else if !canceled && yes {
h.Save()
quit()
}
})
}
} else {
quit()
}

View File

@ -404,6 +404,12 @@ func SetGlobalOptionNative(option string, nativeValue interface{}) error {
} else {
screen.Screen.EnableMouse()
}
} else if option == "autosave" {
if nativeValue.(float64) > 0 {
config.StartAutoSave()
} else {
config.StopAutoSave()
}
} else {
for _, pl := range config.Plugins {
if option == pl.Name {

View File

@ -57,6 +57,9 @@ func (b *Buffer) Save() error {
// SaveAs saves the buffer to a specified path (filename), creating the file if it does not exist
func (b *Buffer) SaveAs(filename string) error {
var err error
if b.Type.Readonly {
return errors.New("Cannot save readonly buffer")
}
if b.Type.Scratch {
return errors.New("Cannot save scratch buffer")
}

View File

@ -0,0 +1,30 @@
package config
import (
"log"
"time"
)
var Autosave chan bool
func init() {
Autosave = make(chan bool)
}
func StartAutoSave() {
go func() {
for {
autotime := time.Duration(GlobalSettings["autosave"].(float64))
if autotime < 1 {
break
}
time.Sleep(autotime * time.Second)
log.Println("Autosave")
Autosave <- true
}
}()
}
func StopAutoSave() {
GlobalSettings["autosave"] = float64(0)
}

View File

@ -2,7 +2,6 @@ package config
const (
DoubleClickThreshold = 400 // How many milliseconds to wait before a second click is not a double click
AutosaveTime = 8 // Number of seconds to wait before autosaving
)
var Bindings map[string]string

File diff suppressed because one or more lines are too long

View File

@ -30,6 +30,7 @@ var (
// Options with validators
var optionValidators = map[string]optionValidator{
"autosave": validateNonNegativeValue,
"tabsize": validatePositiveValue,
"scrollmargin": validateNonNegativeValue,
"scrollspeed": validateNonNegativeValue,
@ -150,7 +151,6 @@ func GetGlobalOption(name string) interface{} {
var defaultCommonSettings = map[string]interface{}{
"autoindent": true,
"autosave": false,
"basename": false,
"colorcolumn": float64(0),
"cursorline": true,
@ -209,6 +209,7 @@ func DefaultCommonSettings() map[string]interface{} {
}
var defaultGlobalSettings = map[string]interface{}{
"autosave": float64(0),
"colorscheme": "default",
"infobar": true,
"keymenu": false,

View File

@ -13,12 +13,14 @@ Here are the options that you can set:
default value: `true`
* `autosave`: micro will save the buffer every 8 seconds automatically. Micro
also will automatically save and quit when you exit without asking. Be
careful when using this feature, because you might accidentally save a file,
overwriting what was there before.
* `autosave`: micro will save the buffer every `n` seconds automatically
(where `n` is the value of the option). Micro also will automatically
save and quit when you exit without asking. Be careful when using this
feature, because you might accidentally save a file, overwriting what
was there before. If the value of the option is `0` then micro will
not autosave.
default value: `false`
default value: `0`
* `basename`: in the infobar, show only the basename of the file being edited
rather than the full path.
@ -123,6 +125,12 @@ Here are the options that you can set:
default value: `false`
* `mkparents`: if a file is opened on a path that does not exist, the file cannot
be saved because the parent directories don't exist. This option lets micro
automatically create the parent directories in such a situation.
default value: `false`
* `mouse`: whether to enable mouse support. When mouse support is disabled,
usually the terminal will be able to access mouse events which can be useful
if you want to copy from the terminal instead of from micro (if over ssh for
@ -131,19 +139,6 @@ Here are the options that you can set:
default value: `true`
* `pluginchannels`: contains all the channels micro's plugin manager will search
for plugins in. A channel is simply a list of 'repository' json files which
contain metadata about the given plugin. See the `Plugin Manager` section of
the `plugins` help topic for more information.
default value: `https://github.com/micro-editor/plugin-channel`
* `pluginrepos`: contains all the 'repositories' micro's plugin manager will
search for plugins in. A repository consists of a `repo.json` file which
contains metadata for a single plugin.
default value: ` `
* `rmtrailingws`: micro will automatically trim trailing whitespaces at eol.
default value: `false`