Skip to content

Commit

Permalink
Support backslash escaped characters (Fixes #164)
Browse files Browse the repository at this point in the history
Support for surrounding, changing and deleting backslash escapes.

Example: ysiw\"
  • Loading branch information
shanesmith committed Feb 12, 2017
1 parent e49d6c2 commit f15d450
Showing 1 changed file with 44 additions and 9 deletions.
53 changes: 44 additions & 9 deletions plugin/surround.vim
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function! s:inputtarget()
while c =~ '^\d\+$'
let c .= s:getchar()
endwhile
if c == " "
if c == " " || c == "\\"
let c .= s:getchar()
endif
if c =~ "\<Esc>\|\<C-C>\|\0"
Expand All @@ -35,7 +35,7 @@ endfunction

function! s:inputreplacement()
let c = s:getchar()
if c == " "
if c == " " || c == "\\"
let c .= s:getchar()
endif
if c =~ "\<Esc>" || c =~ "\<C-C>"
Expand Down Expand Up @@ -139,9 +139,13 @@ function! s:wrap(string,char,type,removed,special)
endif
let pairs = "b()B{}r[]a<>"
let extraspace = ""
let prepend = ""
if newchar =~ '^ '
let newchar = strpart(newchar,1)
let extraspace = ' '
elseif newchar =~ '^\\'
let newchar = strpart(newchar,1)
let prepend = '\'
endif
let idx = stridx(pairs,newchar)
if newchar == ' '
Expand Down Expand Up @@ -209,7 +213,7 @@ function! s:wrap(string,char,type,removed,special)
endif
endif
endif
elseif newchar ==# 'l' || newchar == '\'
elseif newchar ==# 'l'
" LaTeX
let env = input('\begin{')
if env != ""
Expand Down Expand Up @@ -239,11 +243,13 @@ function! s:wrap(string,char,type,removed,special)
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)
let before = prepend . strpart(pairs,idx+1,1) . spc
let after = spc . prepend . strpart(pairs,idx+2,1)
let prepend = ""
elseif newchar == "\<C-[>" || newchar == "\<C-]>"
let before = "{\n\t"
let after = "\n}"
let before = prepend . "{\n\t"
let after = "\n" . prepend . "}"
let prepend = ""
elseif newchar !~ '\a'
let before = newchar
let after = newchar
Expand All @@ -252,6 +258,10 @@ function! s:wrap(string,char,type,removed,special)
let after = ''
endif
let after = substitute(after ,'\n','\n'.initspaces,'g')
if prepend != ""
let before = prepend.before
let after = prepend.after
endif
if type ==# 'V' || (a:special && type ==# "v")
let before = substitute(before,' \+$','','')
let after = substitute(after ,'^ \+','','')
Expand Down Expand Up @@ -355,11 +365,25 @@ endfunction " }}}1
function! s:dosurround(...) " {{{1
let scount = v:count1
let char = (a:0 ? a:1 : s:inputtarget())
let leftchar = char
let rightchar = char
let spc = ""
let escape = ""
if char =~ '^\d\+'
let scount = scount * matchstr(char,'^\d\+')
let char = substitute(char,'^\d\+','','')
endif
if char =~ '^\\'
let char = strpart(char,1)
let escape = 1
let pairs = "b()B{}r[]a<>"
let idx = stridx(pairs, char)
if idx >= 0
let idx = idx / 3 * 3
let leftchar = strpart(pairs, idx+1,1)
let rightchar = strpart(pairs, idx+2,1)
endif
endif
if char =~ '^ '
let char = strpart(char,1)
let spc = 1
Expand All @@ -384,7 +408,14 @@ function! s:dosurround(...) " {{{1
let otype = getregtype('"')
call setreg('"',"")
let strcount = (scount == 1 ? "" : scount)
if char == '/'
if escape
let savecursor = getcurpos()
if search('\\'.rightchar, "W") && search('\\'.leftchar, "besW")
norm! ld`'
else
call setpos('.', savecursor)
endif
elseif char == '/'
exe 'norm! '.strcount.'[/d'.strcount.']/'
elseif char =~# '[[:punct:][:space:]]' && char !~# '[][(){}<>"''`]'
exe 'norm! T'.char
Expand All @@ -404,7 +435,11 @@ function! s:dosurround(...) " {{{1
endif
let oldline = getline('.')
let oldlnum = line('.')
if char ==# "p"
if escape
call search('\\'.leftchar, "bW")
call search('\%#\@!\\'.rightchar, "seW")
norm! v`'d
elseif char ==# "p"
call setreg('"','','V')
elseif char ==# "s" || char ==# "w" || char ==# "W"
" Do nothing
Expand Down

0 comments on commit f15d450

Please sign in to comment.