mirror of
https://github.com/tpope/vim-surround.git
synced 2025-11-12 13:23:50 -05:00
Insert mode mapping, space adding, LaTeX/function replacements
This commit is contained in:
@@ -38,13 +38,20 @@
|
|||||||
" Old Keystroke New
|
" Old Keystroke New
|
||||||
" Hello w*orld! yssB {Hello world!}
|
" Hello w*orld! yssB {Hello world!}
|
||||||
"
|
"
|
||||||
" Finally, in visual mode, a simple "s" with an argument wraps the selection.
|
" In visual mode, a simple "s" with an argument wraps the selection.
|
||||||
" Note that "s" already has a valid meaning in visual mode, but it is
|
" Note that "s" already has a valid meaning in visual mode, but it is
|
||||||
" identical to "c". If you have muscle memory for "s" and would like to use a
|
" identical to "c". If you have muscle memory for "s" and would like to use a
|
||||||
" different key, add your own mapping and the existing one will be disabled.
|
" different key, add your own mapping and the existing one will be disabled.
|
||||||
"
|
"
|
||||||
" vmap S <Plug>VSurround
|
" vmap S <Plug>VSurround
|
||||||
"
|
"
|
||||||
|
" Finally, there is an experimental insert mode mapping on <C-S>. Beware that
|
||||||
|
" this won't work on terminals with flow control (if you accidentally freeze
|
||||||
|
" your terminal, use <C-Q> to unfreeze it). This inserts the surrounding
|
||||||
|
" characters and puts the cursor between them. If, immediately after <C-S>
|
||||||
|
" and before the replacement carriage return is pressed, the prefix, cursor,
|
||||||
|
" and suffix will be placed on three separate lines.
|
||||||
|
"
|
||||||
" Replacements:
|
" Replacements:
|
||||||
"
|
"
|
||||||
" A replacement argument is a single character. The default behavior is to
|
" A replacement argument is a single character. The default behavior is to
|
||||||
@@ -80,7 +87,7 @@
|
|||||||
" Issues:
|
" Issues:
|
||||||
"
|
"
|
||||||
" Vim could potentially get confused when deleting/changing occurs at the very
|
" Vim could potentially get confused when deleting/changing occurs at the very
|
||||||
" end of the line. Report any repeatable instances of this.
|
" end of the line. Pleae report any repeatable instances of this.
|
||||||
"
|
"
|
||||||
" Do we need to use inputsave() / inputrestore() with the tag replacement?
|
" Do we need to use inputsave() / inputrestore() with the tag replacement?
|
||||||
"
|
"
|
||||||
@@ -88,7 +95,11 @@
|
|||||||
" similar to with tags.
|
" similar to with tags.
|
||||||
"
|
"
|
||||||
" Reindenting is handled haphazardly. Need to decide the most appropriate
|
" Reindenting is handled haphazardly. Need to decide the most appropriate
|
||||||
" behavior and implement it.
|
" behavior and implement it. Right now one can do :let b:surround_indent = 1
|
||||||
|
" (or the global equivalent) to enable automatic reindenting by Vim; should
|
||||||
|
" this be the default?
|
||||||
|
"
|
||||||
|
" It would be nice if . would work to repeat an operation.
|
||||||
|
|
||||||
" ============================================================================
|
" ============================================================================
|
||||||
|
|
||||||
@@ -103,12 +114,63 @@ let g:loaded_surround = 1
|
|||||||
let s:cpo_save = &cpo
|
let s:cpo_save = &cpo
|
||||||
set cpo&vim
|
set cpo&vim
|
||||||
|
|
||||||
|
" 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()
|
||||||
|
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
|
||||||
|
if c =~ "\<Esc>\|\<C-C>\|\0"
|
||||||
|
return ""
|
||||||
|
else
|
||||||
|
return c
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:beep()
|
||||||
|
exe "norm! \<Esc>"
|
||||||
|
return ""
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:redraw()
|
||||||
|
redraw
|
||||||
|
return ""
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
" }}}1
|
||||||
|
|
||||||
function! s:wrap(string,char,...) " {{{1
|
function! s:wrap(string,char,...) " {{{1
|
||||||
let keeper = a:string
|
let keeper = a:string
|
||||||
let newchar = a:char
|
let newchar = a:char
|
||||||
let linemode = a:0 ? a:1 : 0
|
let linemode = a:0 ? a:1 : 0
|
||||||
|
let before = ""
|
||||||
|
let after = ""
|
||||||
" Duplicate b's are just placeholders
|
" Duplicate b's are just placeholders
|
||||||
let pairs = "b()B{}b[]b<>"
|
let pairs = "b()B{}r[]a<>"
|
||||||
|
let extraspace = ""
|
||||||
|
if newchar =~ '^ '
|
||||||
|
let newchar = strpart(newchar,1)
|
||||||
|
let extraspace = ' '
|
||||||
|
endif
|
||||||
let idx = stridx(pairs,newchar)
|
let idx = stridx(pairs,newchar)
|
||||||
if exists("b:surround_".char2nr(newchar))
|
if exists("b:surround_".char2nr(newchar))
|
||||||
let before = matchstr(b:surround_{char2nr(newchar)},'.*\ze\n')
|
let before = matchstr(b:surround_{char2nr(newchar)},'.*\ze\n')
|
||||||
@@ -119,7 +181,7 @@ function! s:wrap(string,char,...) " {{{1
|
|||||||
elseif newchar == "p"
|
elseif newchar == "p"
|
||||||
let before = "\n"
|
let before = "\n"
|
||||||
let after = "\n\n"
|
let after = "\n\n"
|
||||||
elseif newchar == "t" || newchar == "<"
|
elseif newchar == "t" || newchar == "T" || newchar == "<"
|
||||||
let dounmapr = 0
|
let dounmapr = 0
|
||||||
let dounmapb = 0
|
let dounmapb = 0
|
||||||
if !mapcheck("<CR>","c")
|
if !mapcheck("<CR>","c")
|
||||||
@@ -130,7 +192,12 @@ function! s:wrap(string,char,...) " {{{1
|
|||||||
let dounmapb= 1
|
let dounmapb= 1
|
||||||
cnoremap > ><CR>
|
cnoremap > ><CR>
|
||||||
endif
|
endif
|
||||||
let tag = input("<")
|
let default = ""
|
||||||
|
if newchar == "T"
|
||||||
|
let default = matchstr(@@,'<\zs.\{-\}\ze>')
|
||||||
|
endif
|
||||||
|
let tag = input("<",default)
|
||||||
|
echo "<".substitute(tag,'>*$','>','')
|
||||||
if dounmapr
|
if dounmapr
|
||||||
silent! cunmap <CR>
|
silent! cunmap <CR>
|
||||||
endif
|
endif
|
||||||
@@ -138,12 +205,30 @@ function! s:wrap(string,char,...) " {{{1
|
|||||||
silent! cunmap >
|
silent! cunmap >
|
||||||
endif
|
endif
|
||||||
if tag != ""
|
if tag != ""
|
||||||
let tag = substitute(tag,'>$','','')
|
let tag = substitute(tag,'>*$','','')
|
||||||
let before = "<".tag.">"
|
let before = "<".tag.">"
|
||||||
let after = "</".substitute(tag," .*",'','').">"
|
let after = "</".substitute(tag," .*",'','').">"
|
||||||
else
|
endif
|
||||||
let before = ""
|
elseif newchar == 'l' || newchar == '\'
|
||||||
let after = ""
|
let dounmapr = 0
|
||||||
|
if !mapcheck("<CR>","c")
|
||||||
|
let dounmapr = 1
|
||||||
|
cnoremap <silent> <CR> <C-R>=<SID>closematch()<CR><CR>
|
||||||
|
endif
|
||||||
|
let env = input('\begin','{')
|
||||||
|
echo '\begin'.env
|
||||||
|
if dounmapr
|
||||||
|
silent! cunmap <CR>
|
||||||
|
endif
|
||||||
|
if env != ""
|
||||||
|
let before = '\begin'.env
|
||||||
|
let after = '\end'.matchstr(env,'[^}]*').'}'
|
||||||
|
endif
|
||||||
|
elseif newchar == 'f'
|
||||||
|
let func = input('function: ')
|
||||||
|
if func != ""
|
||||||
|
let before = substitute(func,'($','','').'('
|
||||||
|
let after = ')'
|
||||||
endif
|
endif
|
||||||
elseif idx >= 0
|
elseif idx >= 0
|
||||||
let spc = (idx % 3) == 1 ? " " : ""
|
let spc = (idx % 3) == 1 ? " " : ""
|
||||||
@@ -164,17 +249,53 @@ function! s:wrap(string,char,...) " {{{1
|
|||||||
let initspaces = matchstr(keeper,'\%^\s*')
|
let initspaces = matchstr(keeper,'\%^\s*')
|
||||||
let keeper = initspaces.before."\n".keeper."\n".initspaces.after
|
let keeper = initspaces.before."\n".keeper."\n".initspaces.after
|
||||||
else
|
else
|
||||||
let keeper = before.keeper.after
|
let keeper = before.extraspace.keeper.extraspace.after
|
||||||
endif
|
endif
|
||||||
return keeper
|
return keeper
|
||||||
endfunction " }}}1
|
endfunction " }}}1
|
||||||
|
|
||||||
|
function! s:insert(...) " {{{1
|
||||||
|
" Optional argument causes the result to appear on 3 lines, not 1
|
||||||
|
let linemode = a:0 ? a:1 : 0
|
||||||
|
let char = s:inputreplacement()
|
||||||
|
while char == "\<CR>"
|
||||||
|
" TODO: use total count for additional blank lines
|
||||||
|
let linemode = linemode + 1
|
||||||
|
let char = s:inputreplacement()
|
||||||
|
endwhile
|
||||||
|
if char == ""
|
||||||
|
return ""
|
||||||
|
endif
|
||||||
|
" We could just use null, but nooooo, that won't work
|
||||||
|
let text = s:wrap("\1",char,0)
|
||||||
|
if linemode
|
||||||
|
return substitute(text,'\s*\%x01\s*',"\<CR>",'')."\<C-O>O"
|
||||||
|
else
|
||||||
|
let len = strlen(substitute(substitute(text,'.*\%x01','',''),'.','.','g'))
|
||||||
|
let left = ""
|
||||||
|
while len > 0
|
||||||
|
let len = len - 1
|
||||||
|
let left = left . "\<Left>"
|
||||||
|
endwhile
|
||||||
|
return substitute(text,'\%x01','','') . left
|
||||||
|
endif
|
||||||
|
endfunction " }}}1
|
||||||
|
|
||||||
|
function! s:reindent() " {{{1
|
||||||
|
if @@ =~ '\n' && (exists("b:surround_indent") || exists("g:surround_indent"))
|
||||||
|
silent norm! '[=']
|
||||||
|
endif
|
||||||
|
endfunction " }}}1
|
||||||
|
|
||||||
function! s:dosurround(...) " {{{1
|
function! s:dosurround(...) " {{{1
|
||||||
let char = nr2char(a:0 ? a:1 : getchar())
|
let char = (a:0 ? a:1 : s:inputtarget())
|
||||||
let newchar = a:0 > 1 ? nr2char(a:2) : ""
|
let newchar = ""
|
||||||
if newchar == "\<Esc>" || newchar == "\<C-C>"
|
if a:0 > 1
|
||||||
|
let newchar = a:2
|
||||||
|
if newchar == "\<Esc>" || newchar == "\<C-C>" || newchar == ""
|
||||||
return s:beep()
|
return s:beep()
|
||||||
endif
|
endif
|
||||||
|
endif
|
||||||
let append = ""
|
let append = ""
|
||||||
let original = @@
|
let original = @@
|
||||||
let @@ = ""
|
let @@ = ""
|
||||||
@@ -191,7 +312,7 @@ function! s:dosurround(...) " {{{1
|
|||||||
let append = matchstr(keeper,'\n*\%$')
|
let append = matchstr(keeper,'\n*\%$')
|
||||||
let keeper = substitute(keeper,'\n*\%$','','')
|
let keeper = substitute(keeper,'\n*\%$','','')
|
||||||
let @@ = ""
|
let @@ = ""
|
||||||
elseif char == "s"
|
elseif char == "s" || char == "w" || char == "W"
|
||||||
" Do nothing
|
" Do nothing
|
||||||
let @@ = ""
|
let @@ = ""
|
||||||
elseif char =~ "[\"'`]"
|
elseif char =~ "[\"'`]"
|
||||||
@@ -241,24 +362,28 @@ function! s:dosurround(...) " {{{1
|
|||||||
let @@ = substitute(keeper,'\n\s+\n','\n\n','g')
|
let @@ = substitute(keeper,'\n\s+\n','\n\n','g')
|
||||||
call setreg('"','','a'.regtype)
|
call setreg('"','','a'.regtype)
|
||||||
silent exe "norm! ".(a:0 < 2 ? "" : "").pcmd.'`['
|
silent exe "norm! ".(a:0 < 2 ? "" : "").pcmd.'`['
|
||||||
"if @@ =~ '\n'
|
call s:reindent()
|
||||||
"silent norm! '[=']
|
|
||||||
"endif
|
|
||||||
if getline('.') =~ '^\s\+$' && keeper =~ '^\s*\n'
|
if getline('.') =~ '^\s\+$' && keeper =~ '^\s*\n'
|
||||||
silent norm! cc
|
silent norm! cc
|
||||||
endif
|
endif
|
||||||
let @@ = removed
|
let @@ = removed
|
||||||
endfunction " }}}1
|
endfunction " }}}1
|
||||||
|
|
||||||
function! s:beep()
|
function! s:changesurround() " {{{1
|
||||||
exe "norm! \<Esc>"
|
let a = s:inputtarget()
|
||||||
return ""
|
if a == ""
|
||||||
endfunction
|
return s:beep()
|
||||||
|
endif
|
||||||
|
let b = s:inputreplacement()
|
||||||
|
if b == ""
|
||||||
|
return s:beep()
|
||||||
|
endif
|
||||||
|
call s:dosurround(a,b)
|
||||||
|
endfunction " }}}1
|
||||||
|
|
||||||
function! s:opfunc(type) " {{{1
|
function! s:opfunc(type) " {{{1
|
||||||
let char = nr2char(getchar())
|
let char = s:inputreplacement()
|
||||||
let g:count = v:count1
|
if char == ""
|
||||||
if char == "\<Esc>" || char == "\<C-C>"
|
|
||||||
return s:beep()
|
return s:beep()
|
||||||
endif
|
endif
|
||||||
let sel_save = &selection
|
let sel_save = &selection
|
||||||
@@ -289,32 +414,53 @@ function! s:opfunc(type) " {{{1
|
|||||||
let keeper = s:wrap(keeper,char,linemode) . append
|
let keeper = s:wrap(keeper,char,linemode) . append
|
||||||
let @@ = keeper
|
let @@ = keeper
|
||||||
silent norm! gvp`[
|
silent norm! gvp`[
|
||||||
"if linemode
|
if linemode
|
||||||
"silent norm! '[=']
|
call s:reindent()
|
||||||
"endif
|
endif
|
||||||
let @@ = reg_save
|
let @@ = reg_save
|
||||||
let &selection = sel_save
|
let &selection = sel_save
|
||||||
endfunction " }}}1
|
endfunction " }}}1
|
||||||
|
|
||||||
function! s:yss() range
|
function! s:closematch()
|
||||||
echo a:firstline." ".a:lastline
|
" Close an open (, {, [, or < on the command line.
|
||||||
|
let tail = matchstr(getcmdline(),'.[^\[\](){}<>]*$')
|
||||||
|
if tail =~ '^\[.\+'
|
||||||
|
return "]"
|
||||||
|
elseif tail =~ '^(.\+'
|
||||||
|
return ")"
|
||||||
|
elseif tail =~ '^{.\+'
|
||||||
|
return "}"
|
||||||
|
elseif tail =~ '^<.+'
|
||||||
|
return ">"
|
||||||
|
else
|
||||||
|
return ""
|
||||||
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
nnoremap <silent> <Plug>DSurround :call <SID>dosurround(getchar())<CR>
|
|
||||||
nnoremap <silent> <Plug>CSurround :call <SID>dosurround(getchar(),getchar())<CR>
|
function! s:closedcmd()
|
||||||
nnoremap <silent> <SID>YSurround :set opfunc=<SID>opfunc<CR>g@
|
let cm = s:closematch()
|
||||||
nnoremap <silent> <Plug>YSurroundS :<C-U> call <SID>opfunc(v:count1)<CR>
|
return getcmdline() . cm
|
||||||
nnoremap <script> <Plug>YSurround <SID>YSurround
|
endfunction
|
||||||
|
|
||||||
|
nnoremap <silent> <Plug>DSurround :call <SID>dosurround(<SID>inputtarget())<CR>
|
||||||
|
nnoremap <silent> <Plug>CSurround :call <SID>changesurround()<CR>
|
||||||
|
nnoremap <silent> <Plug>YSurround :set opfunc=<SID>opfunc<CR>g@
|
||||||
|
nnoremap <silent> <Plug>YSSurround :<C-U>call <SID>opfunc(v:count1)<CR>
|
||||||
vnoremap <silent> <Plug>VSurround :<C-U>call <SID>opfunc(visualmode())<CR>
|
vnoremap <silent> <Plug>VSurround :<C-U>call <SID>opfunc(visualmode())<CR>
|
||||||
|
inoremap <silent> <Plug>ISurround <C-R>=<SID>insert()<CR>
|
||||||
|
|
||||||
|
|
||||||
nmap ds <Plug>DSurround
|
nmap ds <Plug>DSurround
|
||||||
nmap cs <Plug>CSurround
|
nmap cs <Plug>CSurround
|
||||||
nmap ys <Plug>YSurround
|
nmap ys <Plug>YSurround
|
||||||
"nmap <script> yss :<C-U>exe 'norm ^ys'.v:count1.'$'.nr2char(getchar())<CR>
|
nmap yss <Plug>YSSurround
|
||||||
nmap <script> yss <SID>YSurroundS
|
|
||||||
if !hasmapto("<Plug>VSurround","v")
|
if !hasmapto("<Plug>VSurround","v")
|
||||||
vmap s <Plug>VSurround
|
vmap s <Plug>VSurround
|
||||||
endif
|
endif
|
||||||
|
if !hasmapto("<Plug>ISurround","i")
|
||||||
|
imap <C-S> <Plug>ISurround
|
||||||
|
endif
|
||||||
|
|
||||||
|
|
||||||
let &cpo = s:cpo_save
|
let &cpo = s:cpo_save
|
||||||
|
|
||||||
" vim:set ft=vim ff=unix ts=8 sw=4 sts=4:
|
|
||||||
|
|||||||
Reference in New Issue
Block a user