Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add surrounding function name delete/change #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/surround.txt
Expand Up @@ -114,6 +114,10 @@ slight shortcut for ysi (cswb == ysiwb, more or less).
A p represents a |paragraph|. This behaves similarly to w, W, and s above;
however, newlines are sometimes added and/or removed.

An f represents a function call. A function call consists of an identifier
followed by a parenthesized expression. See |isident| for details on
identifiers.

REPLACEMENTS *surround-replacements*

A replacement argument is a single character, and is required by |cs|, |ys|,
Expand All @@ -136,6 +140,10 @@ by themselves.
If s is used, a leading but not trailing space is added. This is useful for
removing parentheses from a function call with csbs.

If f is used, Vim prompts for a function name to insert. If F is used, the
expression is surrounded by additional whitespace. If <C-F> is used, the
function name will be place in the parenthesis, Lisp-style.

CUSTOMIZING *surround-customizing*

The following adds a potential replacement on "-" (ASCII 45) in PHP files.
Expand Down
57 changes: 56 additions & 1 deletion plugin/surround.vim
Expand Up @@ -385,6 +385,16 @@ function! s:dosurround(...) " {{{1
exe 'norm! l'
endif
exe 'norm! dt'.char
elseif char =~# '[fF]'
let [sfline,sfcol,mfline,mfcol,efline,efcol] = [0,0,0,0,0,0]
for i in range(scount)
let [sfline,sfcol,mfline,mfcol,efline,efcol] = s:searchfuncpos()
call cursor(sfline, sfcol)
endfor
if sfline != 0
call cursor(mfline, mfcol)
norm! di(
endif
else
exe 'norm! d'.strcount.'i'.char
endif
Expand Down Expand Up @@ -412,6 +422,9 @@ function! s:dosurround(...) " {{{1
elseif char =~# '[[:punct:]]' && char !~# '[][(){}<>]'
exe 'norm! F'.char
exe 'norm! df'.char
elseif char =~# '[fF]'
call cursor(sfline, sfcol)
norm! df)
else
" One character backwards
call search('.','bW')
Expand All @@ -422,7 +435,7 @@ function! s:dosurround(...) " {{{1
let oldhead = strpart(oldline,0,strlen(oldline)-strlen(rem2))
let oldtail = strpart(oldline, strlen(oldline)-strlen(rem2))
let regtype = getregtype('"')
if char =~# '[\[({<T]' || spc
if char =~# '[\[({<TF]' || spc
let keeper = substitute(keeper,'^\s\+','','')
let keeper = substitute(keeper,'\s\+$','','')
endif
Expand Down Expand Up @@ -553,6 +566,48 @@ function! s:closematch(str) " {{{1
endif
endfunction " }}}1

function! s:searchfuncpos() " {{{1
" Save cursor position and last visual mode
let ppos = getpos(".")
let svpos = getpos("'<")
let evpos = getpos("'>")
let pvmode = visualmode()

let [sfline,sfcol,mfline,mfcol,efline,efcol] = [0,0,0,0,0,0]
while 1
" Get surounding paren text object
norm! va(
let [ebuf,eline,ecol,eoff] = getpos(".")
let [sbuf,sline,scol,soff] = getpos("v")
execute "norm! \<esc>"

if eline == sline && ecol == scol
break
endif

call cursor(sline,scol,soff)
let [fline,fcol] = searchpos('\i\+\s*(','bnec')
if fline == sline && fcol == scol
let [sfline,sfcol] = searchpos('\i\+\s*(','bnc')
let [mfline,mfcol] = [sline,scol]
let [efline,efcol] = [eline,ecol]
break
elseif fline == 0 && fcol == 0
break
else
norm! h
endif
endwhile

" Reset cursor position and last visual mode
call setpos(".",ppos)
call setpos("'<",svpos)
call setpos("'>",evpos)
call visualmode(pvmode)

return [sfline,sfcol,mfline,mfcol,efline,efcol]
endfunction " }}}1

nnoremap <silent> <Plug>SurroundRepeat .
nnoremap <silent> <Plug>Dsurround :<C-U>call <SID>dosurround(<SID>inputtarget())<CR>
nnoremap <silent> <Plug>Csurround :<C-U>call <SID>changesurround()<CR>
Expand Down