2006-10-27 20:31:44 -04:00
|
|
|
" surround.vim - Surroundings
|
2006-11-14 02:16:55 -05:00
|
|
|
" Author: Tim Pope <vimNOSPAM@tpope.info>
|
2006-11-02 12:21:39 -05:00
|
|
|
" GetLatestVimScripts: 1697 1 :AutoInstall: surround.vim
|
2006-10-27 20:31:44 -04:00
|
|
|
" $Id$
|
2006-11-07 18:03:52 -05:00
|
|
|
"
|
2006-11-14 02:16:55 -05:00
|
|
|
" See surround.txt for help. This can be accessed by doing
|
2006-10-28 23:44:12 -04:00
|
|
|
"
|
2006-11-14 02:16:55 -05:00
|
|
|
" :helptags ~/.vim/doc
|
|
|
|
" :help surround
|
2006-10-27 20:31:44 -04:00
|
|
|
"
|
2006-11-14 02:16:55 -05:00
|
|
|
" Licensed under the same terms as Vim itself.
|
2006-10-27 20:31:44 -04:00
|
|
|
|
|
|
|
" ============================================================================
|
|
|
|
|
|
|
|
" Exit quickly when:
|
2008-01-27 11:27:08 -05:00
|
|
|
" - this plugin was already loaded or disabled
|
2006-10-27 20:31:44 -04:00
|
|
|
" - when 'compatible' is set
|
|
|
|
if (exists("g:loaded_surround") && g:loaded_surround) || &cp
|
|
|
|
finish
|
|
|
|
endif
|
|
|
|
let g:loaded_surround = 1
|
|
|
|
|
|
|
|
let s:cpo_save = &cpo
|
|
|
|
set cpo&vim
|
|
|
|
|
2006-10-28 23:44:12 -04:00
|
|
|
" Input functions {{{1
|
|
|
|
|
|
|
|
function! s:getchar()
|
|
|
|
let c = getchar()
|
|
|
|
if c =~ '^\d\+$'
|
|
|
|
let c = nr2char(c)
|
|
|
|
endif
|
|
|
|
return c
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:inputtarget()
|
|
|
|
let c = s:getchar()
|
2006-10-29 23:57:40 -05:00
|
|
|
while c =~ '^\d\+$'
|
|
|
|
let c = c . s:getchar()
|
|
|
|
endwhile
|
2006-10-29 16:03:15 -05:00
|
|
|
if c == " "
|
|
|
|
let c = c . s:getchar()
|
|
|
|
endif
|
2006-10-28 23:44:12 -04:00
|
|
|
if c =~ "\<Esc>\|\<C-C>\|\0"
|
|
|
|
return ""
|
|
|
|
else
|
|
|
|
return c
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:inputreplacement()
|
|
|
|
"echo '-- SURROUND --'
|
|
|
|
let c = s:getchar()
|
|
|
|
if c == " "
|
|
|
|
let c = c . s:getchar()
|
|
|
|
endif
|
2006-10-31 15:59:02 -05:00
|
|
|
if c =~ "\<Esc>" || c =~ "\<C-C>"
|
2006-10-28 23:44:12 -04:00
|
|
|
return ""
|
|
|
|
else
|
|
|
|
return c
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:beep()
|
|
|
|
exe "norm! \<Esc>"
|
|
|
|
return ""
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:redraw()
|
|
|
|
redraw
|
|
|
|
return ""
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" }}}1
|
|
|
|
|
2006-10-30 14:19:57 -05:00
|
|
|
" Wrapping functions {{{1
|
|
|
|
|
|
|
|
function! s:extractbefore(str)
|
|
|
|
if a:str =~ '\r'
|
|
|
|
return matchstr(a:str,'.*\ze\r')
|
|
|
|
else
|
|
|
|
return matchstr(a:str,'.*\ze\n')
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:extractafter(str)
|
|
|
|
if a:str =~ '\r'
|
|
|
|
return matchstr(a:str,'\r\zs.*')
|
|
|
|
else
|
|
|
|
return matchstr(a:str,'\n\zs.*')
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:repeat(str,count)
|
|
|
|
let cnt = a:count
|
|
|
|
let str = ""
|
|
|
|
while cnt > 0
|
|
|
|
let str = str . a:str
|
|
|
|
let cnt = cnt - 1
|
|
|
|
endwhile
|
|
|
|
return str
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:fixindent(str,spc)
|
|
|
|
let str = substitute(a:str,'\t',s:repeat(' ',&sw),'g')
|
|
|
|
let spc = substitute(a:spc,'\t',s:repeat(' ',&sw),'g')
|
2006-10-30 21:02:17 -05:00
|
|
|
let str = substitute(str,'\(\n\|\%^\).\@=','\1'.spc,'g')
|
2006-10-30 14:19:57 -05:00
|
|
|
if ! &et
|
2006-11-05 01:15:24 -05:00
|
|
|
let str = substitute(str,'\s\{'.&ts.'\}',"\t",'g')
|
2006-10-30 14:19:57 -05:00
|
|
|
endif
|
|
|
|
return str
|
|
|
|
endfunction
|
|
|
|
|
2007-02-05 12:47:43 -05:00
|
|
|
function! s:process(string)
|
|
|
|
let i = 0
|
|
|
|
while i < 7
|
|
|
|
let i = i + 1
|
|
|
|
let repl_{i} = ''
|
|
|
|
let m = matchstr(a:string,nr2char(i).'.\{-\}\ze'.nr2char(i))
|
|
|
|
if m != ''
|
|
|
|
let m = substitute(strpart(m,1),'\r.*','','')
|
|
|
|
let repl_{i} = input(substitute(m,':\s*$','','').': ')
|
|
|
|
endif
|
|
|
|
endwhile
|
|
|
|
let s = ""
|
|
|
|
let i = 0
|
|
|
|
while i < strlen(a:string)
|
|
|
|
let char = strpart(a:string,i,1)
|
|
|
|
if char2nr(char) < 8
|
|
|
|
let next = stridx(a:string,char,i+1)
|
|
|
|
if next == -1
|
|
|
|
let s = s . char
|
|
|
|
else
|
|
|
|
let insertion = repl_{char2nr(char)}
|
|
|
|
let subs = strpart(a:string,i+1,next-i-1)
|
|
|
|
let subs = matchstr(subs,'\r.*')
|
|
|
|
while subs =~ '^\r.*\r'
|
|
|
|
let sub = matchstr(subs,"^\r\\zs[^\r]*\r[^\r]*")
|
|
|
|
let subs = strpart(subs,strlen(sub)+1)
|
|
|
|
let r = stridx(sub,"\r")
|
|
|
|
let insertion = substitute(insertion,strpart(sub,0,r),strpart(sub,r+1),'')
|
|
|
|
endwhile
|
|
|
|
let s = s . insertion
|
|
|
|
let i = next
|
|
|
|
endif
|
|
|
|
else
|
|
|
|
let s = s . char
|
|
|
|
endif
|
|
|
|
let i = i + 1
|
|
|
|
endwhile
|
|
|
|
return s
|
|
|
|
endfunction
|
|
|
|
|
2006-10-31 15:59:02 -05:00
|
|
|
function! s:wrap(string,char,type,...)
|
2006-10-27 20:31:44 -04:00
|
|
|
let keeper = a:string
|
|
|
|
let newchar = a:char
|
2006-10-31 15:59:02 -05:00
|
|
|
let type = a:type
|
2006-10-30 21:02:17 -05:00
|
|
|
let linemode = type ==# 'V' ? 1 : 0
|
2006-10-31 15:59:02 -05:00
|
|
|
let special = a:0 ? a:1 : 0
|
2006-10-28 23:44:12 -04:00
|
|
|
let before = ""
|
|
|
|
let after = ""
|
2006-10-31 15:59:02 -05:00
|
|
|
if type == "V"
|
2006-10-30 14:19:57 -05:00
|
|
|
let initspaces = matchstr(keeper,'\%^\s*')
|
|
|
|
else
|
|
|
|
let initspaces = matchstr(getline('.'),'\%^\s*')
|
|
|
|
endif
|
2006-10-29 16:03:15 -05:00
|
|
|
" Duplicate b's are just placeholders (removed)
|
2006-10-28 23:44:12 -04:00
|
|
|
let pairs = "b()B{}r[]a<>"
|
|
|
|
let extraspace = ""
|
|
|
|
if newchar =~ '^ '
|
|
|
|
let newchar = strpart(newchar,1)
|
|
|
|
let extraspace = ' '
|
|
|
|
endif
|
2006-10-27 20:31:44 -04:00
|
|
|
let idx = stridx(pairs,newchar)
|
2007-05-27 17:15:27 -04:00
|
|
|
if newchar == ' '
|
|
|
|
let before = ''
|
|
|
|
let after = ''
|
|
|
|
elseif exists("b:surround_".char2nr(newchar))
|
2007-02-05 12:47:43 -05:00
|
|
|
let all = s:process(b:surround_{char2nr(newchar)})
|
|
|
|
let before = s:extractbefore(all)
|
|
|
|
let after = s:extractafter(all)
|
2006-10-27 20:31:44 -04:00
|
|
|
elseif exists("g:surround_".char2nr(newchar))
|
2007-02-05 12:47:43 -05:00
|
|
|
let all = s:process(g:surround_{char2nr(newchar)})
|
|
|
|
let before = s:extractbefore(all)
|
|
|
|
let after = s:extractafter(all)
|
2006-10-29 16:03:15 -05:00
|
|
|
elseif newchar ==# "p"
|
2006-10-27 20:31:44 -04:00
|
|
|
let before = "\n"
|
|
|
|
let after = "\n\n"
|
2006-10-30 21:02:17 -05:00
|
|
|
elseif newchar =~# "[tT\<C-T><,]"
|
2007-05-10 16:49:49 -04:00
|
|
|
let dounmapp = 0
|
2006-10-27 20:31:44 -04:00
|
|
|
let dounmapb = 0
|
2007-05-10 16:49:49 -04:00
|
|
|
if !maparg(">","c")
|
2006-10-27 20:31:44 -04:00
|
|
|
let dounmapb= 1
|
2007-05-10 16:49:49 -04:00
|
|
|
" Hide from AsNeeded
|
|
|
|
exe "cn"."oremap > <CR>"
|
|
|
|
exe "cn"."oremap % %<C-V>"
|
|
|
|
"cm ap > <C-R>=getcmdline() =~ '^[^%?].*[%?]$' ? "\026\076" : "\026\076\015"<CR>
|
2006-10-27 20:31:44 -04:00
|
|
|
endif
|
2006-10-28 23:44:12 -04:00
|
|
|
let default = ""
|
2007-05-10 16:49:49 -04:00
|
|
|
if !maparg("%","c")
|
|
|
|
" This is to help when typing things like
|
|
|
|
" <a href="/images/<%= @image.filename %>">
|
|
|
|
" The downside is it breaks backspace, so lets disable it for now
|
|
|
|
"let dounmapp= 1
|
|
|
|
"exe "cn"."oremap % %<C-V>"
|
|
|
|
endif
|
2006-10-29 16:03:15 -05:00
|
|
|
if newchar ==# "T"
|
2006-10-30 21:02:17 -05:00
|
|
|
if !exists("s:lastdel")
|
|
|
|
let s:lastdel = ""
|
|
|
|
endif
|
|
|
|
let default = matchstr(s:lastdel,'<\zs.\{-\}\ze>')
|
2006-10-28 23:44:12 -04:00
|
|
|
endif
|
|
|
|
let tag = input("<",default)
|
|
|
|
echo "<".substitute(tag,'>*$','>','')
|
2006-10-30 14:19:57 -05:00
|
|
|
"if dounmapr
|
|
|
|
"silent! cunmap <CR>
|
|
|
|
"endif
|
2006-10-27 20:31:44 -04:00
|
|
|
if dounmapb
|
|
|
|
silent! cunmap >
|
|
|
|
endif
|
2007-05-10 16:49:49 -04:00
|
|
|
if dounmapp
|
|
|
|
silent! cunmap %
|
|
|
|
endif
|
2006-10-27 20:31:44 -04:00
|
|
|
if tag != ""
|
2006-10-28 23:44:12 -04:00
|
|
|
let tag = substitute(tag,'>*$','','')
|
2007-05-10 16:49:49 -04:00
|
|
|
let before = '<'.tag.'>'
|
|
|
|
if tag =~ '/$'
|
|
|
|
let after = ''
|
|
|
|
else
|
|
|
|
let after = '</'.substitute(tag,' .*','','').'>'
|
|
|
|
endif
|
2006-10-30 21:02:17 -05:00
|
|
|
if newchar == "\<C-T>" || newchar == ","
|
|
|
|
if type ==# "v" || type ==# "V"
|
|
|
|
let before = before . "\n\t"
|
|
|
|
endif
|
|
|
|
if type ==# "v"
|
2006-10-31 16:34:54 -05:00
|
|
|
let after = "\n". after
|
2006-10-30 21:02:17 -05:00
|
|
|
endif
|
|
|
|
endif
|
2006-10-28 23:44:12 -04:00
|
|
|
endif
|
2006-10-29 16:03:15 -05:00
|
|
|
elseif newchar ==# 'l' || newchar == '\'
|
2006-10-31 15:59:02 -05:00
|
|
|
" LaTeX
|
2006-10-30 14:19:57 -05:00
|
|
|
let env = input('\begin{')
|
|
|
|
let env = '{' . env
|
2006-10-29 16:03:15 -05:00
|
|
|
let env = env . s:closematch(env)
|
2006-10-28 23:44:12 -04:00
|
|
|
echo '\begin'.env
|
|
|
|
if env != ""
|
2006-10-30 21:02:17 -05:00
|
|
|
let before = '\begin'.env
|
|
|
|
let after = '\end'.matchstr(env,'[^}]*').'}'
|
|
|
|
endif
|
2006-10-31 15:59:02 -05:00
|
|
|
"if type ==# 'v' || type ==# 'V'
|
|
|
|
"let before = before ."\n\t"
|
|
|
|
"endif
|
|
|
|
"if type ==# 'v'
|
|
|
|
"let after = "\n".initspaces.after
|
|
|
|
"endif
|
2006-10-29 16:03:15 -05:00
|
|
|
elseif newchar ==# 'f' || newchar ==# 'F'
|
2006-10-30 21:02:17 -05:00
|
|
|
let fnc = input('function: ')
|
|
|
|
if fnc != ""
|
|
|
|
let before = substitute(fnc,'($','','').'('
|
2006-10-28 23:44:12 -04:00
|
|
|
let after = ')'
|
2006-10-29 16:03:15 -05:00
|
|
|
if newchar ==# 'F'
|
|
|
|
let before = before . ' '
|
|
|
|
let after = ' ' . after
|
|
|
|
endif
|
2006-10-27 20:31:44 -04:00
|
|
|
endif
|
|
|
|
elseif idx >= 0
|
|
|
|
let spc = (idx % 3) == 1 ? " " : ""
|
|
|
|
let idx = idx / 3 * 3
|
|
|
|
let before = strpart(pairs,idx+1,1) . spc
|
|
|
|
let after = spc . strpart(pairs,idx+2,1)
|
2007-05-10 16:49:49 -04:00
|
|
|
elseif newchar == "\<C-[>" || newchar == "\<C-]>"
|
|
|
|
let before = "{\n\t"
|
|
|
|
let after = "\n}"
|
2006-11-05 13:50:27 -05:00
|
|
|
elseif newchar !~ '\a'
|
2006-10-27 20:31:44 -04:00
|
|
|
let before = newchar
|
|
|
|
let after = newchar
|
2006-11-05 13:50:27 -05:00
|
|
|
else
|
|
|
|
let before = ''
|
|
|
|
let after = ''
|
2006-10-27 20:31:44 -04:00
|
|
|
endif
|
2006-10-31 16:34:54 -05:00
|
|
|
"let before = substitute(before,'\n','\n'.initspaces,'g')
|
|
|
|
let after = substitute(after ,'\n','\n'.initspaces,'g')
|
2006-11-05 01:15:24 -05:00
|
|
|
"let after = substitute(after,"\n\\s*\<C-U>\\s*",'\n','g')
|
2006-10-31 15:59:02 -05:00
|
|
|
if type ==# 'V' || (special && type ==# "v")
|
2006-10-30 21:02:17 -05:00
|
|
|
let before = substitute(before,' \+$','','')
|
|
|
|
let after = substitute(after ,'^ \+','','')
|
2006-10-31 15:59:02 -05:00
|
|
|
if after !~ '^\n'
|
|
|
|
let after = initspaces.after
|
|
|
|
endif
|
|
|
|
if keeper !~ '\n$' && after !~ '^\n'
|
|
|
|
let keeper = keeper . "\n"
|
2007-05-10 16:49:49 -04:00
|
|
|
elseif keeper =~ '\n$' && after =~ '^\n'
|
|
|
|
let after = strpart(after,1)
|
2006-10-31 15:59:02 -05:00
|
|
|
endif
|
2006-10-30 21:02:17 -05:00
|
|
|
if before !~ '\n\s*$'
|
2006-10-31 15:59:02 -05:00
|
|
|
let before = before . "\n"
|
|
|
|
if special
|
|
|
|
let before = before . "\t"
|
|
|
|
endif
|
2006-10-30 14:19:57 -05:00
|
|
|
endif
|
2006-10-30 21:02:17 -05:00
|
|
|
endif
|
2006-10-31 15:59:02 -05:00
|
|
|
if type ==# 'V'
|
|
|
|
let before = initspaces.before
|
|
|
|
endif
|
2006-10-30 21:02:17 -05:00
|
|
|
if before =~ '\n\s*\%$'
|
|
|
|
if type ==# 'v'
|
|
|
|
let keeper = initspaces.keeper
|
2006-10-30 14:19:57 -05:00
|
|
|
endif
|
2006-10-30 21:02:17 -05:00
|
|
|
let padding = matchstr(before,'\n\zs\s\+\%$')
|
|
|
|
let before = substitute(before,'\n\s\+\%$','\n','')
|
|
|
|
let keeper = s:fixindent(keeper,padding)
|
|
|
|
endif
|
|
|
|
if type ==# 'V'
|
|
|
|
let keeper = before.keeper.after
|
|
|
|
elseif type =~ "^\<C-V>"
|
2006-11-01 00:59:08 -05:00
|
|
|
" Really we should be iterating over the buffer
|
|
|
|
let repl = substitute(before,'[\\~]','\\&','g').'\1'.substitute(after,'[\\~]','\\&','g')
|
|
|
|
let repl = substitute(repl,'\n',' ','g')
|
2006-11-02 12:21:39 -05:00
|
|
|
let keeper = substitute(keeper."\n",'\(.\{-\}\)\('.(special ? '\s\{-\}' : '').'\n\)',repl.'\n','g')
|
|
|
|
let keeper = substitute(keeper,'\n\%$','','')
|
2006-10-27 20:31:44 -04:00
|
|
|
else
|
2006-10-28 23:44:12 -04:00
|
|
|
let keeper = before.extraspace.keeper.extraspace.after
|
2006-10-27 20:31:44 -04:00
|
|
|
endif
|
|
|
|
return keeper
|
2006-10-30 21:02:17 -05:00
|
|
|
endfunction
|
|
|
|
|
2006-10-31 15:59:02 -05:00
|
|
|
function! s:wrapreg(reg,char,...)
|
2006-10-30 21:02:17 -05:00
|
|
|
let orig = getreg(a:reg)
|
2006-11-02 12:21:39 -05:00
|
|
|
let type = substitute(getregtype(a:reg),'\d\+$','','')
|
2006-10-31 15:59:02 -05:00
|
|
|
let special = a:0 ? a:1 : 0
|
|
|
|
let new = s:wrap(orig,a:char,type,special)
|
2006-10-30 21:02:17 -05:00
|
|
|
call setreg(a:reg,new,type)
|
|
|
|
endfunction
|
|
|
|
" }}}1
|
2006-10-27 20:31:44 -04:00
|
|
|
|
2008-01-27 11:27:08 -05:00
|
|
|
function! s:dotset(seq,count)
|
|
|
|
" hedging bets on the name of a future plugin
|
|
|
|
silent! call repeat#set(a:seq,a:count)
|
|
|
|
if !exists("*repeat#set")
|
|
|
|
silent! call dot#set(a:seq,a:count)
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2006-10-28 23:44:12 -04:00
|
|
|
function! s:insert(...) " {{{1
|
|
|
|
" Optional argument causes the result to appear on 3 lines, not 1
|
2006-10-30 21:02:17 -05:00
|
|
|
"call inputsave()
|
2006-10-28 23:44:12 -04:00
|
|
|
let linemode = a:0 ? a:1 : 0
|
|
|
|
let char = s:inputreplacement()
|
2006-10-31 15:59:02 -05:00
|
|
|
while char == "\<CR>" || char == "\<C-S>"
|
2006-10-28 23:44:12 -04:00
|
|
|
" TODO: use total count for additional blank lines
|
|
|
|
let linemode = linemode + 1
|
|
|
|
let char = s:inputreplacement()
|
|
|
|
endwhile
|
2006-10-30 21:02:17 -05:00
|
|
|
"call inputrestore()
|
2006-10-28 23:44:12 -04:00
|
|
|
if char == ""
|
|
|
|
return ""
|
|
|
|
endif
|
2006-10-30 21:02:17 -05:00
|
|
|
"call inputsave()
|
2007-02-11 13:23:19 -05:00
|
|
|
let cb_save = &clipboard
|
2006-10-31 15:59:02 -05:00
|
|
|
let reg_save = @@
|
|
|
|
call setreg('"',"\r",'v')
|
|
|
|
call s:wrapreg('"',char,linemode)
|
2007-07-31 10:20:47 -04:00
|
|
|
" If line mode is used and the surrounding consists solely of a suffix,
|
|
|
|
" remove the initial newline. This fits a use case of mine but is a
|
|
|
|
" little inconsistent. Is there anyone that would prefer the simpler
|
|
|
|
" behavior of just inserting the newline?
|
2007-10-01 11:27:33 -04:00
|
|
|
if linemode && match(getreg('"'),'^\n\s*\zs.*') == 0
|
2007-07-31 10:20:47 -04:00
|
|
|
call setreg('"',matchstr(getreg('"'),'^\n\s*\zs.*'),getregtype('"'))
|
|
|
|
endif
|
2007-02-12 10:37:15 -05:00
|
|
|
" This can be used to append a placeholder to the end
|
|
|
|
if exists("g:surround_insert_tail")
|
|
|
|
call setreg('"',g:surround_insert_tail,"a".getregtype('"'))
|
|
|
|
endif
|
2006-10-31 15:59:02 -05:00
|
|
|
"if linemode
|
|
|
|
"call setreg('"',substitute(getreg('"'),'^\s\+','',''),'c')
|
|
|
|
"endif
|
2007-02-11 13:09:45 -05:00
|
|
|
if col('.') >= col('$')
|
2007-05-10 16:49:49 -04:00
|
|
|
norm! ""p
|
2006-10-31 15:59:02 -05:00
|
|
|
else
|
2007-05-10 16:49:49 -04:00
|
|
|
norm! ""P
|
|
|
|
endif
|
2007-07-31 10:20:47 -04:00
|
|
|
if linemode
|
2007-05-10 16:49:49 -04:00
|
|
|
call s:reindent()
|
2006-10-31 15:59:02 -05:00
|
|
|
endif
|
2007-05-10 16:49:49 -04:00
|
|
|
norm! `]
|
2006-10-31 15:59:02 -05:00
|
|
|
call search('\r','bW')
|
2006-11-05 01:15:24 -05:00
|
|
|
let @@ = reg_save
|
2007-02-11 13:23:19 -05:00
|
|
|
let &clipboard = cb_save
|
2006-10-31 15:59:02 -05:00
|
|
|
return "\<Del>"
|
2006-10-28 23:44:12 -04:00
|
|
|
endfunction " }}}1
|
|
|
|
|
|
|
|
function! s:reindent() " {{{1
|
2007-05-10 16:49:49 -04:00
|
|
|
if exists("b:surround_indent") ? b:surround_indent : (exists("g:surround_indent") && g:surround_indent)
|
2006-10-28 23:44:12 -04:00
|
|
|
silent norm! '[=']
|
|
|
|
endif
|
|
|
|
endfunction " }}}1
|
|
|
|
|
2006-10-27 20:31:44 -04:00
|
|
|
function! s:dosurround(...) " {{{1
|
2006-10-29 16:03:15 -05:00
|
|
|
let scount = v:count1
|
2006-10-28 23:44:12 -04:00
|
|
|
let char = (a:0 ? a:1 : s:inputtarget())
|
2006-10-29 00:18:16 -04:00
|
|
|
let spc = ""
|
2006-10-29 23:57:40 -05:00
|
|
|
if char =~ '^\d\+'
|
|
|
|
let scount = scount * matchstr(char,'^\d\+')
|
|
|
|
let char = substitute(char,'^\d\+','','')
|
|
|
|
endif
|
2006-10-29 00:18:16 -04:00
|
|
|
if char =~ '^ '
|
|
|
|
let char = strpart(char,1)
|
|
|
|
let spc = 1
|
|
|
|
endif
|
2006-10-30 14:19:57 -05:00
|
|
|
if char == 'a'
|
|
|
|
let char = '>'
|
|
|
|
endif
|
|
|
|
if char == 'r'
|
|
|
|
let char = ']'
|
|
|
|
endif
|
2006-10-28 23:44:12 -04:00
|
|
|
let newchar = ""
|
|
|
|
if a:0 > 1
|
|
|
|
let newchar = a:2
|
|
|
|
if newchar == "\<Esc>" || newchar == "\<C-C>" || newchar == ""
|
|
|
|
return s:beep()
|
|
|
|
endif
|
2006-10-27 20:31:44 -04:00
|
|
|
endif
|
2007-02-11 13:23:19 -05:00
|
|
|
let cb_save = &clipboard
|
|
|
|
set clipboard-=unnamed
|
2006-10-27 20:31:44 -04:00
|
|
|
let append = ""
|
2006-10-30 21:02:17 -05:00
|
|
|
let original = getreg('"')
|
|
|
|
let otype = getregtype('"')
|
|
|
|
call setreg('"',"")
|
2007-05-27 17:15:27 -04:00
|
|
|
let strcount = (scount == 1 ? "" : scount)
|
|
|
|
if char == '/'
|
|
|
|
exe 'norm '.strcount.'[/d'.strcount.']/'
|
|
|
|
else
|
|
|
|
exe 'norm d'.strcount.'i'.char
|
2008-01-25 13:31:07 -05:00
|
|
|
" One character backwards
|
|
|
|
call search('.','bW')
|
2007-05-27 17:15:27 -04:00
|
|
|
endif
|
2006-10-30 21:02:17 -05:00
|
|
|
let keeper = getreg('"')
|
2006-10-29 00:18:16 -04:00
|
|
|
let okeeper = keeper " for reindent below
|
2006-10-30 21:02:17 -05:00
|
|
|
if keeper == ""
|
|
|
|
call setreg('"',original,otype)
|
2007-02-11 13:23:19 -05:00
|
|
|
let &clipboard = cb_save
|
2006-10-27 20:31:44 -04:00
|
|
|
return ""
|
|
|
|
endif
|
|
|
|
let oldline = getline('.')
|
|
|
|
let oldlnum = line('.')
|
2006-10-29 16:03:15 -05:00
|
|
|
if char ==# "p"
|
2006-10-30 21:02:17 -05:00
|
|
|
call setreg('"','','V')
|
2006-10-29 16:03:15 -05:00
|
|
|
elseif char ==# "s" || char ==# "w" || char ==# "W"
|
2006-10-27 20:31:44 -04:00
|
|
|
" Do nothing
|
2006-10-30 21:02:17 -05:00
|
|
|
call setreg('"','')
|
2006-10-27 20:31:44 -04:00
|
|
|
elseif char =~ "[\"'`]"
|
|
|
|
exe "norm! i \<Esc>d2i".char
|
2006-10-30 21:02:17 -05:00
|
|
|
call setreg('"',substitute(getreg('"'),' ','',''))
|
2007-05-27 17:15:27 -04:00
|
|
|
elseif char == '/'
|
|
|
|
norm! "_x
|
|
|
|
call setreg('"','/**/',"c")
|
|
|
|
let keeper = substitute(substitute(keeper,'^/\*\s\=','',''),'\s\=\*$','','')
|
2006-10-27 20:31:44 -04:00
|
|
|
else
|
2008-01-25 13:31:07 -05:00
|
|
|
exe "norm da".char
|
2006-10-27 20:31:44 -04:00
|
|
|
endif
|
2006-10-30 21:02:17 -05:00
|
|
|
let removed = getreg('"')
|
2006-10-27 20:31:44 -04:00
|
|
|
let rem2 = substitute(removed,'\n.*','','')
|
|
|
|
let oldhead = strpart(oldline,0,strlen(oldline)-strlen(rem2))
|
|
|
|
let oldtail = strpart(oldline, strlen(oldline)-strlen(rem2))
|
|
|
|
let regtype = getregtype('"')
|
2006-10-29 16:03:15 -05:00
|
|
|
if char =~# '[\[({<T]' || spc
|
2006-10-29 00:18:16 -04:00
|
|
|
let keeper = substitute(keeper,'^\s\+','','')
|
|
|
|
let keeper = substitute(keeper,'\s\+$','','')
|
|
|
|
endif
|
2006-10-30 21:02:17 -05:00
|
|
|
if col("']") == col("$") && col('.') + 1 == col('$')
|
2006-10-27 20:31:44 -04:00
|
|
|
if oldhead =~# '^\s*$' && a:0 < 2
|
|
|
|
let keeper = substitute(keeper,'\%^\n'.oldhead.'\(\s*.\{-\}\)\n\s*\%$','\1','')
|
|
|
|
endif
|
|
|
|
let pcmd = "p"
|
|
|
|
else
|
|
|
|
let pcmd = "P"
|
|
|
|
endif
|
2006-10-29 16:03:15 -05:00
|
|
|
if line('.') < oldlnum && regtype ==# "V"
|
2006-10-27 20:31:44 -04:00
|
|
|
let pcmd = "p"
|
|
|
|
endif
|
2006-10-30 21:02:17 -05:00
|
|
|
call setreg('"',keeper,regtype)
|
2006-10-27 20:31:44 -04:00
|
|
|
if newchar != ""
|
2006-10-30 21:02:17 -05:00
|
|
|
call s:wrapreg('"',newchar)
|
2006-10-27 20:31:44 -04:00
|
|
|
endif
|
2007-02-11 13:09:45 -05:00
|
|
|
silent exe 'norm! ""'.pcmd.'`['
|
2006-10-30 21:02:17 -05:00
|
|
|
if removed =~ '\n' || okeeper =~ '\n' || getreg('"') =~ '\n'
|
2006-10-29 00:18:16 -04:00
|
|
|
call s:reindent()
|
|
|
|
endif
|
2006-10-27 20:31:44 -04:00
|
|
|
if getline('.') =~ '^\s\+$' && keeper =~ '^\s*\n'
|
|
|
|
silent norm! cc
|
|
|
|
endif
|
2006-10-30 21:02:17 -05:00
|
|
|
call setreg('"',removed,regtype)
|
|
|
|
let s:lastdel = removed
|
2007-02-11 13:23:19 -05:00
|
|
|
let &clipboard = cb_save
|
2008-01-23 22:15:36 -05:00
|
|
|
if newchar == ""
|
2008-01-27 11:27:08 -05:00
|
|
|
call s:dotset("\<Plug>Dsurround".char,scount)
|
2008-01-23 22:15:36 -05:00
|
|
|
else
|
2008-01-27 11:27:08 -05:00
|
|
|
call s:dotset("\<Plug>Csurround".char.newchar,scount)
|
2008-01-23 22:15:36 -05:00
|
|
|
endif
|
2006-10-27 20:31:44 -04:00
|
|
|
endfunction " }}}1
|
|
|
|
|
2006-10-28 23:44:12 -04:00
|
|
|
function! s:changesurround() " {{{1
|
|
|
|
let a = s:inputtarget()
|
|
|
|
if a == ""
|
|
|
|
return s:beep()
|
|
|
|
endif
|
|
|
|
let b = s:inputreplacement()
|
|
|
|
if b == ""
|
|
|
|
return s:beep()
|
|
|
|
endif
|
|
|
|
call s:dosurround(a,b)
|
|
|
|
endfunction " }}}1
|
2006-10-27 20:31:44 -04:00
|
|
|
|
2006-10-31 15:59:02 -05:00
|
|
|
function! s:opfunc(type,...) " {{{1
|
2006-10-28 23:44:12 -04:00
|
|
|
let char = s:inputreplacement()
|
|
|
|
if char == ""
|
2006-10-27 20:31:44 -04:00
|
|
|
return s:beep()
|
|
|
|
endif
|
2007-02-11 13:23:19 -05:00
|
|
|
let reg = '"'
|
2006-10-27 20:31:44 -04:00
|
|
|
let sel_save = &selection
|
|
|
|
let &selection = "inclusive"
|
2007-02-11 13:23:19 -05:00
|
|
|
let cb_save = &clipboard
|
|
|
|
set clipboard-=unnamed
|
2006-10-30 21:02:17 -05:00
|
|
|
let reg_save = getreg(reg)
|
|
|
|
let reg_type = getregtype(reg)
|
2007-02-05 12:47:43 -05:00
|
|
|
"call setreg(reg,"\n","c")
|
2006-10-30 21:02:17 -05:00
|
|
|
let type = a:type
|
2006-10-27 20:31:44 -04:00
|
|
|
if a:type == "char"
|
2007-02-11 13:09:45 -05:00
|
|
|
silent exe 'norm! v`[o`]"'.reg.'y'
|
2006-10-30 21:02:17 -05:00
|
|
|
let type = 'v'
|
2006-10-27 20:31:44 -04:00
|
|
|
elseif a:type == "line"
|
2007-02-11 13:09:45 -05:00
|
|
|
silent exe 'norm! `[V`]"'.reg.'y'
|
2006-10-30 21:02:17 -05:00
|
|
|
let type = 'V'
|
|
|
|
elseif a:type ==# "v" || a:type ==# "V" || a:type ==# "\<C-V>"
|
2007-02-11 13:09:45 -05:00
|
|
|
silent exe 'norm! gv"'.reg.'y'
|
2006-10-27 20:31:44 -04:00
|
|
|
elseif a:type =~ '^\d\+$'
|
2007-02-05 12:47:43 -05:00
|
|
|
let type = 'v'
|
2006-10-30 21:02:17 -05:00
|
|
|
silent exe 'norm! ^v'.a:type.'$h"'.reg.'y'
|
2007-02-05 12:47:43 -05:00
|
|
|
if mode() == 'v'
|
|
|
|
norm! v
|
|
|
|
return s:beep()
|
|
|
|
endif
|
2006-10-27 20:31:44 -04:00
|
|
|
else
|
|
|
|
let &selection = sel_save
|
2007-02-11 13:23:19 -05:00
|
|
|
let &clipboard = cb_save
|
2006-10-27 20:31:44 -04:00
|
|
|
return s:beep()
|
|
|
|
endif
|
2006-10-30 21:02:17 -05:00
|
|
|
let keeper = getreg(reg)
|
2006-11-07 18:03:52 -05:00
|
|
|
if type == "v" && a:type != "v"
|
|
|
|
let append = matchstr(keeper,'\_s\@<!\s*$')
|
|
|
|
let keeper = substitute(keeper,'\_s\@<!\s*$','','')
|
2006-10-27 20:31:44 -04:00
|
|
|
endif
|
2006-10-30 21:02:17 -05:00
|
|
|
call setreg(reg,keeper,type)
|
2006-10-31 15:59:02 -05:00
|
|
|
call s:wrapreg(reg,char,a:0)
|
2006-11-11 21:02:30 -05:00
|
|
|
if type == "v" && a:type != "v" && append != ""
|
2006-11-05 01:15:24 -05:00
|
|
|
call setreg(reg,append,"ac")
|
|
|
|
endif
|
2006-10-30 21:02:17 -05:00
|
|
|
silent exe 'norm! gv'.(reg == '"' ? '' : '"' . reg).'p`['
|
2006-11-02 12:21:39 -05:00
|
|
|
if type == 'V' || (getreg(reg) =~ '\n' && type == 'v')
|
2006-10-28 23:44:12 -04:00
|
|
|
call s:reindent()
|
|
|
|
endif
|
2006-10-30 21:02:17 -05:00
|
|
|
call setreg(reg,reg_save,reg_type)
|
2006-10-27 20:31:44 -04:00
|
|
|
let &selection = sel_save
|
2007-02-11 13:23:19 -05:00
|
|
|
let &clipboard = cb_save
|
2008-01-23 22:15:36 -05:00
|
|
|
if a:type =~ '^\d\+$'
|
2008-01-27 11:27:08 -05:00
|
|
|
call s:dotset("\<Plug>Y".(a:0 ? "S" : "s")."surround".char,a:type)
|
2008-01-23 22:15:36 -05:00
|
|
|
endif
|
2006-10-31 15:59:02 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:opfunc2(arg)
|
|
|
|
call s:opfunc(a:arg,1)
|
2006-10-27 20:31:44 -04:00
|
|
|
endfunction " }}}1
|
|
|
|
|
2006-10-29 19:09:40 -05:00
|
|
|
function! s:closematch(str) " {{{1
|
2006-10-28 23:44:12 -04:00
|
|
|
" Close an open (, {, [, or < on the command line.
|
2006-10-29 16:03:15 -05:00
|
|
|
let tail = matchstr(a:str,'.[^\[\](){}<>]*$')
|
2006-10-28 23:44:12 -04:00
|
|
|
if tail =~ '^\[.\+'
|
|
|
|
return "]"
|
|
|
|
elseif tail =~ '^(.\+'
|
|
|
|
return ")"
|
|
|
|
elseif tail =~ '^{.\+'
|
|
|
|
return "}"
|
|
|
|
elseif tail =~ '^<.+'
|
|
|
|
return ">"
|
|
|
|
else
|
|
|
|
return ""
|
|
|
|
endif
|
2006-10-29 19:09:40 -05:00
|
|
|
endfunction " }}}1
|
2006-10-28 23:44:12 -04:00
|
|
|
|
2006-10-29 19:09:40 -05:00
|
|
|
nnoremap <silent> <Plug>Dsurround :<C-U>call <SID>dosurround(<SID>inputtarget())<CR>
|
|
|
|
nnoremap <silent> <Plug>Csurround :<C-U>call <SID>changesurround()<CR>
|
|
|
|
nnoremap <silent> <Plug>Yssurround :<C-U>call <SID>opfunc(v:count1)<CR>
|
2006-10-31 15:59:02 -05:00
|
|
|
nnoremap <silent> <Plug>YSsurround :<C-U>call <SID>opfunc2(v:count1)<CR>
|
2006-11-06 00:53:09 -05:00
|
|
|
" <C-U> discards the numerical argument but there's not much we can do with it
|
|
|
|
nnoremap <silent> <Plug>Ysurround :<C-U>set opfunc=<SID>opfunc<CR>g@
|
|
|
|
nnoremap <silent> <Plug>YSurround :<C-U>set opfunc=<SID>opfunc2<CR>g@
|
2006-10-29 19:09:40 -05:00
|
|
|
vnoremap <silent> <Plug>Vsurround :<C-U>call <SID>opfunc(visualmode())<CR>
|
2006-10-31 15:59:02 -05:00
|
|
|
vnoremap <silent> <Plug>VSurround :<C-U>call <SID>opfunc2(visualmode())<CR>
|
2006-10-29 19:09:40 -05:00
|
|
|
inoremap <silent> <Plug>Isurround <C-R>=<SID>insert()<CR>
|
2006-10-31 15:59:02 -05:00
|
|
|
inoremap <silent> <Plug>ISurround <C-R>=<SID>insert(1)<CR>
|
2006-10-27 20:31:44 -04:00
|
|
|
|
2006-10-29 19:09:40 -05:00
|
|
|
if !exists("g:surround_no_mappings") || ! g:surround_no_mappings
|
|
|
|
nmap ds <Plug>Dsurround
|
|
|
|
nmap cs <Plug>Csurround
|
|
|
|
nmap ys <Plug>Ysurround
|
2006-10-31 15:59:02 -05:00
|
|
|
nmap yS <Plug>YSurround
|
2006-10-29 19:09:40 -05:00
|
|
|
nmap yss <Plug>Yssurround
|
2006-10-31 15:59:02 -05:00
|
|
|
nmap ySs <Plug>YSsurround
|
|
|
|
nmap ySS <Plug>YSsurround
|
2006-10-29 19:09:40 -05:00
|
|
|
if !hasmapto("<Plug>Vsurround","v")
|
2007-02-12 10:37:15 -05:00
|
|
|
if exists(":xmap")
|
2007-05-10 16:49:49 -04:00
|
|
|
xmap s <Plug>Vsurround
|
2007-02-12 10:37:15 -05:00
|
|
|
else
|
2007-05-10 16:49:49 -04:00
|
|
|
vmap s <Plug>Vsurround
|
2007-02-12 10:37:15 -05:00
|
|
|
endif
|
2006-10-29 19:09:40 -05:00
|
|
|
endif
|
2006-10-31 15:59:02 -05:00
|
|
|
if !hasmapto("<Plug>VSurround","v")
|
2007-02-12 10:37:15 -05:00
|
|
|
if exists(":xmap")
|
2007-05-10 16:49:49 -04:00
|
|
|
xmap S <Plug>VSurround
|
2007-02-12 10:37:15 -05:00
|
|
|
else
|
2007-05-10 16:49:49 -04:00
|
|
|
vmap S <Plug>VSurround
|
2007-02-12 10:37:15 -05:00
|
|
|
endif
|
2006-10-31 15:59:02 -05:00
|
|
|
endif
|
2007-05-10 16:49:49 -04:00
|
|
|
if !hasmapto("<Plug>Isurround","i") && "" == mapcheck("<C-S>","i")
|
2006-10-29 19:09:40 -05:00
|
|
|
imap <C-S> <Plug>Isurround
|
|
|
|
endif
|
2006-11-07 18:03:52 -05:00
|
|
|
imap <C-G>s <Plug>Isurround
|
|
|
|
imap <C-G>S <Plug>ISurround
|
2006-11-01 00:59:08 -05:00
|
|
|
"Implemented internally instead
|
2006-10-31 15:59:02 -05:00
|
|
|
"imap <C-S><C-S> <Plug>ISurround
|
2006-10-28 23:44:12 -04:00
|
|
|
endif
|
|
|
|
|
2006-10-27 20:31:44 -04:00
|
|
|
let &cpo = s:cpo_save
|
|
|
|
|
2006-10-29 19:09:40 -05:00
|
|
|
" vim:set ft=vim sw=4 sts=4 et:
|