Created
June 24, 2012 13:06
-
-
Save kien/2983198 to your computer and use it in GitHub Desktop.
bufsync.vim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| " Sharing the buffer list between Vim instances | |
| " Usage: | |
| " Run :BufSyncServer in the main Vim instance | |
| " Run :BufSyncClient in the client instance(s) | |
| " Option: | |
| let s:buflist = 'path/to/cachefile' | |
| " Commands: | |
| com! BufSyncServer cal s:server() | |
| com! BufSyncClient cal s:client() | |
| " Functions: | |
| fu! s:record(bufnr, ...) | |
| let bufname = bufname(a:bufnr + 0) | |
| if empty(bufname) | retu | en | |
| let fn = fnamemodify(bufname, ':p') | |
| if !empty(&bt) || !filereadable(fn) | retu | en | |
| cal filter(s:files, 'v:val !=? fn') | |
| if a:0 | |
| cal insert(s:files, fn) | |
| en | |
| if !( a:0 && a:1 == 2 ) | |
| cal writefile(s:files, s:buflist) | |
| en | |
| endf | |
| fu! s:server() | |
| if exists('#BufSyncClient') | |
| au! BufSyncClient | |
| en | |
| aug BufSyncServer | |
| au! | |
| au BufAdd * cal s:record(expand('<abuf>', 1), 1) | |
| au BufDelete * cal s:record(expand('<abuf>', 1)) | |
| au VimLeavePre * cal delete(s:buflist) | |
| aug END | |
| if filereadable(s:buflist) | |
| cal delete(s:buflist) | |
| en | |
| let s:files = [] | |
| for each in range(1, bufnr('$')) | |
| cal s:record(each, 2) | |
| endfo | |
| cal writefile(s:files, s:buflist) | |
| endf | |
| fu! s:client() | |
| if exists('#BufSyncServer') | |
| au! BufSyncServer | |
| en | |
| aug BufSyncClient | |
| au! | |
| au FocusGained,CursorHold * cal s:update() | |
| aug END | |
| for each in range(1, bufnr('$')) | |
| let fn = fnamemodify(bufname(each), ':p') | |
| if getbufvar(each, '&bl') && filereadable(fn) | |
| exe each.'bw' | |
| en | |
| endfo | |
| cal s:update() | |
| endf | |
| fu! s:update() | |
| let s:files = filereadable(s:buflist) ? readfile(s:buflist) : [] | |
| for bufnr in range(1, bufnr('$')) | |
| let fn = fnamemodify(bufname(bufnr), ':p') | |
| if index(s:files, fn) < 0 && getbufvar(bufnr, '&bl') && filereadable(fn) | |
| exe bufnr.'bw' | |
| en | |
| endfo | |
| for each in s:files | |
| if bufnr('^'.each.'$') < 1 | |
| exe 'bad' s:fnesc(each) | |
| en | |
| endfo | |
| endf | |
| fu! s:fnesc(path) | |
| retu exists('*fnameescape') ? fnameescape(a:path) : escape(a:path, " %#*?|<\"\n") | |
| endf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment