summaryrefslogtreecommitdiffstats
path: root/.vim/plugin/autoswap.vim
diff options
context:
space:
mode:
Diffstat (limited to '.vim/plugin/autoswap.vim')
-rw-r--r--.vim/plugin/autoswap.vim64
1 files changed, 64 insertions, 0 deletions
diff --git a/.vim/plugin/autoswap.vim b/.vim/plugin/autoswap.vim
new file mode 100644
index 0000000..714ad14
--- /dev/null
+++ b/.vim/plugin/autoswap.vim
@@ -0,0 +1,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