aboutsummaryrefslogtreecommitdiffhomepage
path: root/.config/vim/my/autoload/autosave.vim
blob: 0c34d0e142f5751ce89c6df563e0bd5bb2b462c1 (plain)
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
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