Add 'autosave' option

Closes #278
This commit is contained in:
Zachary Yedidia
2016-09-28 13:07:05 -04:00
parent 191fd5e495
commit fb69ecdc9b
5 changed files with 33 additions and 3 deletions

View File

@@ -8,6 +8,7 @@ import (
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
"time"
"github.com/go-errors/errors" "github.com/go-errors/errors"
"github.com/layeh/gopher-luar" "github.com/layeh/gopher-luar"
@@ -24,6 +25,7 @@ const (
synLinesDown = 75 // How many lines down to look to do syntax highlighting synLinesDown = 75 // How many lines down to look to do syntax highlighting
doubleClickThreshold = 400 // How many milliseconds to wait before a second click is not a double click doubleClickThreshold = 400 // How many milliseconds to wait before a second click is not a double click
undoThreshold = 500 // If two events are less than n milliseconds apart, undo both of them undoThreshold = 500 // If two events are less than n milliseconds apart, undo both of them
autosaveTime = 8 // Number of seconds to wait before autosaving
) )
var ( var (
@@ -61,7 +63,8 @@ var (
// Channel of jobs running in the background // Channel of jobs running in the background
jobs chan JobFunction jobs chan JobFunction
// Event channel // Event channel
events chan tcell.Event events chan tcell.Event
autosave chan bool
) )
// LoadInput determines which files should be loaded into buffers // LoadInput determines which files should be loaded into buffers
@@ -330,6 +333,7 @@ func main() {
jobs = make(chan JobFunction, 100) jobs = make(chan JobFunction, 100)
events = make(chan tcell.Event, 100) events = make(chan tcell.Event, 100)
autosave = make(chan bool)
LoadPlugins() LoadPlugins()
@@ -360,6 +364,15 @@ func main() {
} }
}() }()
go func() {
for {
time.Sleep(autosaveTime * time.Second)
if globalSettings["autosave"].(bool) {
autosave <- true
}
}
}()
for { for {
// Display everything // Display everything
RedrawAll() RedrawAll()
@@ -372,6 +385,8 @@ func main() {
// If a new job has finished while running in the background we should execute the callback // If a new job has finished while running in the background we should execute the callback
f.function(f.output, f.args...) f.function(f.output, f.args...)
continue continue
case <-autosave:
CurView().Save(true)
case event = <-events: case event = <-events:
} }

File diff suppressed because one or more lines are too long

View File

@@ -176,6 +176,7 @@ func GetOption(name string) interface{} {
func DefaultGlobalSettings() map[string]interface{} { func DefaultGlobalSettings() map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"autoindent": true, "autoindent": true,
"autosave": false,
"colorcolumn": float64(0), "colorcolumn": float64(0),
"colorscheme": "zenburn", "colorscheme": "zenburn",
"cursorline": true, "cursorline": true,
@@ -199,6 +200,7 @@ func DefaultGlobalSettings() map[string]interface{} {
func DefaultLocalSettings() map[string]interface{} { func DefaultLocalSettings() map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"autoindent": true, "autoindent": true,
"autosave": false,
"colorcolumn": float64(0), "colorcolumn": float64(0),
"cursorline": true, "cursorline": true,
"filetype": "Unknown", "filetype": "Unknown",

View File

@@ -194,7 +194,13 @@ func (v *View) ScrollDown(n int) {
// causing them to lose the unsaved changes // causing them to lose the unsaved changes
func (v *View) CanClose() bool { func (v *View) CanClose() bool {
if v.Type == vtDefault && v.Buf.IsModified { if v.Type == vtDefault && v.Buf.IsModified {
char, canceled := messenger.LetterPrompt("Save changes to "+v.Buf.Name+" before closing? (y,n,esc) ", 'y', 'n') var char rune
var canceled bool
if v.Buf.Settings["autosave"].(bool) {
char = 'y'
} else {
char, canceled = messenger.LetterPrompt("Save changes to "+v.Buf.Name+" before closing? (y,n,esc) ", 'y', 'n')
}
if !canceled { if !canceled {
if char == 'y' { if char == 'y' {
v.Save(true) v.Save(true)

View File

@@ -93,6 +93,13 @@ Here are the options that you can set:
default value: `2` default value: `2`
* `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.
default value: `off`
--- ---
Default plugin options: Default plugin options: