1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 10:30:05 +08:00

feat(autosave): add autosave_location option

This commit is contained in:
wsdjeg 2022-02-06 13:12:15 +08:00
parent 225fc488c4
commit 577c3ceb9d
4 changed files with 36 additions and 5 deletions

View File

@ -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')

View File

@ -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)

View File

@ -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

View File

@ -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*