diff --git a/autoload/SpaceVim/api/file.vim b/autoload/SpaceVim/api/file.vim index c17fc9d83..58f0fab33 100644 --- a/autoload/SpaceVim/api/file.vim +++ b/autoload/SpaceVim/api/file.vim @@ -249,8 +249,9 @@ endfunction let s:file['unify_path'] = function('s:unify_path') -function! s:path_to_fname(path) abort - return substitute(s:unify_path(a:path), '[\\/:;.]', '_', 'g') +function! s:path_to_fname(path, ...) abort + let sep = get(a:000, 0, '_') + return substitute(s:unify_path(a:path), '[\\/:;.]', sep, 'g') endfunction let s:file['path_to_fname'] = function('s:path_to_fname') diff --git a/autoload/SpaceVim/layers/edit.vim b/autoload/SpaceVim/layers/edit.vim index e6be418e4..d549d6907 100644 --- a/autoload/SpaceVim/layers/edit.vim +++ b/autoload/SpaceVim/layers/edit.vim @@ -41,6 +41,13 @@ " name = 'edit' " autosave_all_buffers = true " < +" 4. `autosave_location`: set the directory where to save changed files. By +" default it is empty string, that means saving to the original file. If this +" option is not an empty string. files will me saved to that directory +" automatically. and the format is: +" > +" autosave_location/path+=to+=filename.ext.backup +" < scriptencoding utf-8 if exists('s:autosave_timeout') @@ -57,6 +64,7 @@ let s:BUFFER = SpaceVim#api#import('vim#buffer') let s:autosave_timeout = 0 let s:autosave_events = [] let s:autosave_all_buffers = 0 +let s:autosave_location = '' function! SpaceVim#layers#edit#health() abort call SpaceVim#layers#edit#plugins() @@ -101,6 +109,7 @@ function! SpaceVim#layers#edit#set_variable(var) abort let s:autosave_timeout = get(a:var, 'autosave_timeout', s:autosave_timeout) let s:autosave_events = get(a:var, 'autosave_events', s:autosave_events) let s:autosave_all_buffers = get(a:var, 'autosave_all_buffers', s:autosave_all_buffers) + let s:autosave_location = get(a:var, 'autosave_location', s:autosave_location) endfunction function! SpaceVim#layers#edit#get_options() abort @@ -111,6 +120,7 @@ function! SpaceVim#layers#edit#config() abort let autosave_opt = { \ 'timeoutlen' : s:autosave_timeout, \ 'save_all_buffers' : s:autosave_all_buffers, + \ 'backupdir' : s:autosave_location, \ 'event' : s:autosave_events, \ } call SpaceVim#plugins#autosave#config(autosave_opt) diff --git a/autoload/SpaceVim/plugins/autosave.vim b/autoload/SpaceVim/plugins/autosave.vim index e0deebbcd..41dc326ff 100644 --- a/autoload/SpaceVim/plugins/autosave.vim +++ b/autoload/SpaceVim/plugins/autosave.vim @@ -28,6 +28,7 @@ let s:default_opt = { \ } let s:LOGGER =SpaceVim#logger#derive('autosave') +let s:FILE = SpaceVim#api#import('file') let s:autosave_timer = -1 @@ -47,6 +48,16 @@ function! SpaceVim#plugins#autosave#config(opt) abort endfor endfunction +function! s:location_path(bufname) abort + if empty(s:default_opt.backupdir) + return a:bufname + else + return s:default_opt.backupdir . '/' + \ . s:FILE.path_to_fname(a:bufname) + \ . '.backup' + endif +endfunction + function! s:save_buffer(bufnr) abort if getbufvar(a:bufnr, '&modified') && @@ -54,9 +65,11 @@ function! s:save_buffer(bufnr) abort \ filewritable(bufname(a:bufnr)) && \ !empty(bufname(a:bufnr)) let lines = getbufline(a:bufnr, 1, "$") - call writefile(lines, bufname(a:bufnr)) - call setbufvar(a:bufnr, "&modified", 0) - exe 'silent checktime ' . a:bufnr + call writefile(lines, s:location_path(bufname(a:bufnr), '+=')) + if empty(s:default_opt.backupdir) + call setbufvar(a:bufnr, "&modified", 0) + exe 'silent checktime ' . a:bufnr + endif endif endfunction diff --git a/doc/SpaceVim.txt b/doc/SpaceVim.txt index a9cf27b84..9b9c2f5a9 100644 --- a/doc/SpaceVim.txt +++ b/doc/SpaceVim.txt @@ -1789,6 +1789,13 @@ If you want to save all buffers automatically. Set this option to `true`. name = 'edit' autosave_all_buffers = true < + 4. `autosave_location`: set the directory where to save changed files. By +default it is empty string, that means saving to the original file. If this +option is not an empty string. files will me saved to that directory +automatically. and the format is: +> + autosave_location/path+=to+=filename.ext.backup +< ============================================================================== EXPRFOLD *SpaceVim-layers-exprfold*