diff options
| author | nsfisis <nsfisis@gmail.com> | 2021-11-19 19:41:39 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2021-11-19 19:41:39 +0900 |
| commit | 0d4a7fe77275a40996a710b673e58b4d2d78a8f4 (patch) | |
| tree | 79d84dda8ddca08df05ec89593df6ef9162a0860 | |
| parent | c749f8e4654e22f0d9f87e5fa30710ba0c25edca (diff) | |
| download | dotfiles-0d4a7fe77275a40996a710b673e58b4d2d78a8f4.tar.gz dotfiles-0d4a7fe77275a40996a710b673e58b4d2d78a8f4.tar.zst dotfiles-0d4a7fe77275a40996a710b673e58b4d2d78a8f4.zip | |
vim: autosave
| -rw-r--r-- | .vim/my/autoload/autosave.vim | 70 | ||||
| -rw-r--r-- | .vim/my/plugin/autosave.vim | 29 |
2 files changed, 99 insertions, 0 deletions
diff --git a/.vim/my/autoload/autosave.vim b/.vim/my/autoload/autosave.vim new file mode 100644 index 0000000..0c34d0e --- /dev/null +++ b/.vim/my/autoload/autosave.vim @@ -0,0 +1,70 @@ +scriptencoding utf-8 + + + +if !exists('g:autosave_interval_ms') + let g:autosave_interval_ms = 10 * 1000 +endif + +if !exists('g:autosave_silent') + let g:autosave_silent = v:true +endif + + + +function! autosave#handler(timer_id) abort + if !&modified + return + endif + if &readonly + return + endif + if &buftype !=# '' + return + endif + if expand('%') ==# '' + return + endif + + if !g:autosave_silent + echohl Comment + echo 'Auto-saving...' + echohl None + endif + + silent! write + + if !g:autosave_silent + echohl Comment + echo 'Saved.' + echohl None + endif +endfunction + + +function! autosave#enable() abort + if has('+timers') + echohl Error + echo "Feature '+timers' not available." + echohl None + return + endif + if exists('b:autosave_timer') + return + endif + + let b:autosave_timer = timer_start( + \ g:autosave_interval_ms, + \ 'autosave#handler', + \ { 'repeat': -1 }) +endfunction + + +function! autosave#disable() abort + if !exists('b:autosave_timer') + return + endif + + call timer_stop('b:autosave_timer') + unlet b:autosave_timer +endfunction diff --git a/.vim/my/plugin/autosave.vim b/.vim/my/plugin/autosave.vim new file mode 100644 index 0000000..2548609 --- /dev/null +++ b/.vim/my/plugin/autosave.vim @@ -0,0 +1,29 @@ +scriptencoding utf-8 + +if exists('g:loaded_autosave') + finish +endif + + + +command! -bar + \ AutosaveEnable + \ call autosave#enable() + + +command! -bar + \ AutosaveDisable + \ call autosave#disable() + + +command! -bar + \ AutosaveToggle + \ if exists('b:autosave_timer') | + \ call autosave#enable() | + \ else | + \ call autosave#disable() | + \ endif + + + +let g:loaded_autosave = 1 |
