summaryrefslogtreecommitdiffstats
path: root/.vim/plugin/autoswap.vim
blob: 714ad14ce6256e3119907c438e452d3c23196850 (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
" Vim global plugin for automating response to swapfiles
" Maintainer: Florian Pritz
" Contributor:	Damian Conway
" License:	This file is placed in the public domain.

" Based on the script by Damian Conway, changed to remove the window handling

" If already loaded, we're done...
if exists("loaded_autoswap")
    finish
endif
let loaded_autoswap = 1

" Preserve external compatibility options, then enable full vim compatibility...
let s:save_cpo = &cpo
set cpo&vim

" Invoke the behaviour whenever a swapfile is detected...
"
augroup AutoSwap
    autocmd!
    autocmd SwapExists *  call AS_M_HandleSwapfile(expand('<afile>:p'))
augroup END


" The automatic behaviour...
"
function! AS_M_HandleSwapfile (filename)
    " If swapfile is older than file itself, just get rid of it...
    if getftime(v:swapname) < getftime(a:filename)
        call AS_M_DelayedMsg("Old swapfile detected...and deleted")
        call delete(v:swapname)
        let v:swapchoice = 'e'

    " Otherwise, recover
    else
        call AS_M_DelayedMsg("Swapfile detected...recovering")
        let v:swapchoice = 'r'
    endif
endfunction


" Print a message after the autocommand completes
" (so you can see it, but don't have to hit <ENTER> to continue)...
"
function! AS_M_DelayedMsg (msg)
    " A sneaky way of injecting a message when swapping into the new buffer...
    augroup AutoSwap_Msg
        autocmd!
        " Print the message on finally entering the buffer...
        autocmd BufWinEnter *  echohl WarningMsg
  exec 'autocmd BufWinEnter *  echon "\r'.printf("%-60s", a:msg).'"'
        autocmd BufWinEnter *  echohl NONE

        " And then remove these autocmds, so it's a "one-shot" deal...
        autocmd BufWinEnter *  augroup AutoSwap_Msg
        autocmd BufWinEnter *  autocmd!
        autocmd BufWinEnter *  augroup END
    augroup END
endfunction


" Restore previous external compatibility options
let &cpo = s:save_cpo