Skip to content

Instantly share code, notes, and snippets.

@thinca
Created December 14, 2010 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinca/740572 to your computer and use it in GitHub Desktop.
Save thinca/740572 to your computer and use it in GitHub Desktop.
" Use the Vim with singleton.
" Version: 0.1.0
" Author : thinca <thinca+vim@gmail.com>
" License: Creative Commons Attribution 2.1 Japan License
" <http://creativecommons.org/licenses/by/2.1/jp/deed.en>
let s:save_cpo = &cpo
set cpo&vim
if !exists('g:singleton#ignore_pattern')
let g:singleton#ignore_pattern = join([
\ '/svn-\%(commit\|prop\)\%(\.\d\+\)\?\.tmp$',
\ '.git/COMMIT_EDITMSG$',
\ '/hg-editor-.\{6}.txt$',
\ '/bzr_log\..\{6}$',
\ ], '\|')
if $TMP != ''
let g:singleton#ignore_pattern .=
\ '\|^' . substitute(expand($TMP), '\', '/', 'g')
elseif has('unix')
let g:singleton#ignore_pattern .= '\|^/tmp/\|^/var/tmp/'
endif
endif
let s:openbuf = openbuf#new('singleton', {
\ 'nomanage': 1,
\ })
let s:data = openbuf#new('singleton/data', {
\ 'reuse': 'never',
\ 'nomanage': 1,
\ })
function! singleton#enable() " {{{2
if !has('vim_starting')
return
endif
let c = argc()
if c == 0
augroup plugin-singleton
autocmd!
autocmd StdinReadPost * call s:stdin()
augroup END
return
endif
let files = map(argv(), 'fnamemodify(v:val, ":p")')
if &diff && (c == 2 || c == 3)
call singleton#delegate('diff', files)
return
endif
if c == 1 &&
\ substitute(fnamemodify(argv(0), ':p'), '\\', '/', 'g')
\ !~# g:singleton#ignore_pattern
call singleton#delegate('open', files)
endif
endfunction
function! singleton#entrust(files) " {{{2
let server = s:target_server()
if server == ''
return
endif
endfunction
function! singleton#recieve(from, file) " {{{2
" TODO: to be customizable.
new `=a:file`
let b:singleton_from = a:from
augroup plugin-singleton
autocmd! BufUnload <buffer> singleton#reply(b:singleton_from)
augroup END
endfunction
function! singleton#reply(to) " {{{2
endfunction
function! singleton#delegate(func, ...) " {{{2
let server = s:target_server()
if server == ''
return
endif
let args = map(copy(a:000), 'string(v:val)')
call remote_expr(server, printf('singleton#%s(%s)', a:func, join(args, ',')))
try
call remote_foreground(server)
catch
endtry
call remote_expr(server, 'foreground()')
set viminfo= " Don't save viminfo.
if !has('gui_running')
echo 'Edit in externals...'
endif
quitall!
endfunction
function! singleton#open(files) " {{{2
for f in type(a:files) == type([]) ? a:files : [a:files]
call s:open(f)
endfor
redraw
endfunction
function! singleton#diff(files) " {{{2
if type(a:files) != ([]) || len(a:files) < 2 || 3 < len(a:files)
throw 'singleton: Invalid argument for diff(): ' . string(a:files)
endif
let files = map(copy(a:files), 'fnamemodify(v:val, ":p")')
tabnew `=files[0]`
for f in files[1:]
rightbelow vsplit `=f`
endfor
windo diffthis
1 wincmd w
endfunction
function! singleton#data(name, data) " {{{2
" TODO: to be customizable.
call s:data.open(a:name)
silent put =a:data
silent 1 delete _
setlocal readonly nomodified buftype=nofile
filetype detect
redraw
endfunction
function! s:target_server() " {{{2
if !has('clientserver')
return ''
endif
let server = ''
for srv in split(serverlist(), "\n")
if v:servername !=? srv && (server == '' || srv !~ '\d')
let server = srv
endif
endfor
return server
endfunction
function! s:open(file) " {{{2
let openfile = fnamemodify(a:file, ':p')
if s:bufopened(openfile)
try
let switchbuf = &switchbuf
" TODO: to be customizable.
let &switchbuf = 'useopen,usetab'
execute 'sbuffer' openfile
finally
let &switchbuf = switchbuf
endtry
else
" TODO: to be customizable.
tabnew `=openfile`
endif
endfunction
function! s:bufopened(file) " {{{2
let f = fnamemodify(a:file, ':p')
for tabnr in range(1, tabpagenr('$'))
for nbuf in tabpagebuflist(tabnr)
if f ==# fnamemodify(bufname(nbuf), ':p')
return 1
endif
endfor
endfor
return 0
endfunction
function! s:stdin() " {{{2
call singleton#delegate('data', '[stdin]', getline(1, '$'))
endfunction
let &cpo = s:save_cpo
unlet s:save_cpo
@tyru
Copy link

tyru commented Dec 14, 2010

singleton.vim先行配信だとっ...!(ゴゴゴ...
typo見つけました! s/recieve/receive/g

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment