-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathauto_save.vim
More file actions
88 lines (73 loc) · 1.74 KB
/
auto_save.vim
File metadata and controls
88 lines (73 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
if exists('g:loaded_auto_save') || v:version < 702
finish
endif
let g:loaded_auto_save = 1
let s:save_cpo = &cpoptions
set cpoptions&vim
let s:auto_save_enabled = 1
augroup auto_save
autocmd!
autocmd User AutoSavePre :
autocmd User AutoSavePost call <SID>record_time()
autocmd TextChanged,InsertLeave * ++nested call <SID>auto_save()
autocmd BufLeave,FocusLost * ++nested call <SID>auto_save()
autocmd BufWritePost * call <SID>record_time()
augroup END
command! AutoSaveToggle :call <SID>auto_save_toggle()
function! s:is_enabled() abort
if !s:auto_save_enabled
return v:false
end
if &readonly || !&modifiable
return v:false
endif
if !empty(&buftype)
return v:false
endif
if bufname() =~# '^__coc_'
return v:false
endif
if mode() !=# 'n'
return v:false
endif
return v:true
endfunction
function! s:auto_save() abort
if !s:is_enabled()
return
endif
if !&modified
return
endif
" Preserve marks that are used to remember start and
" end position of the last changed or yanked text (`:h '[`).
let l:first_char_pos = getpos("'[")
let l:last_char_pos = getpos("']")
doautocmd User AutoSavePre
silent! w
call setpos("'[", l:first_char_pos)
call setpos("']", l:last_char_pos)
if &modified
return
endif
let s:auto_save_enabled = 0
try
doautocmd User AutoSavePost
finally
let s:auto_save_enabled = 1
endtry
endfunction
function! s:auto_save_toggle() abort
if s:auto_save_enabled
let s:auto_save_enabled = 0
echo 'AutoSave is OFF'
else
let s:auto_save_enabled = 1
echo 'AutoSave is ON'
endif
endfunction
function! s:record_time() abort
let b:auto_save_last_saved_time = localtime()
endfunction
let &cpoptions = s:save_cpo
unlet s:save_cpo