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"
"runtime"
"strings"
"time"
"github.com/go-errors/errors"
"github.com/layeh/gopher-luar"
@ -24,6 +25,7 @@ const (
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
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 (
@ -61,7 +63,8 @@ var (
// Channel of jobs running in the background
jobs chan JobFunction
// Event channel
events chan tcell.Event
events chan tcell.Event
autosave chan bool
)
// LoadInput determines which files should be loaded into buffers
@ -330,6 +333,7 @@ func main() {
jobs = make(chan JobFunction, 100)
events = make(chan tcell.Event, 100)
autosave = make(chan bool)
LoadPlugins()
@ -360,6 +364,15 @@ func main() {
}
}()
go func() {
for {
time.Sleep(autosaveTime * time.Second)
if globalSettings["autosave"].(bool) {
autosave <- true
}
}
}()
for {
// Display everything
RedrawAll()
@ -372,6 +385,8 @@ func main() {
// If a new job has finished while running in the background we should execute the callback
f.function(f.output, f.args...)
continue
case <-autosave:
CurView().Save(true)
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{} {
return map[string]interface{}{
"autoindent": true,
"autosave": false,
"colorcolumn": float64(0),
"colorscheme": "zenburn",
"cursorline": true,
@ -199,6 +200,7 @@ func DefaultGlobalSettings() map[string]interface{} {
func DefaultLocalSettings() map[string]interface{} {
return map[string]interface{}{
"autoindent": true,
"autosave": false,
"colorcolumn": float64(0),
"cursorline": true,
"filetype": "Unknown",

View file

@ -194,7 +194,13 @@ func (v *View) ScrollDown(n int) {
// causing them to lose the unsaved changes
func (v *View) CanClose() bool {
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 char == 'y' {
v.Save(true)

View file

@ -93,6 +93,13 @@ Here are the options that you can set:
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: