Created
August 6, 2016 03:14
-
-
Save romgrk/dc871a06975b787d70bc40e2cd7feeae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" cs'' => will switch surrounding simple/double quotes | |
" cs'[x] => will delegate to vim-surround | |
nmap <expr>cs' CSurroundQuotes() | |
fu! CSurroundQuotes() | |
let qch = s:findQuote() | |
let char = s:getChar('Question', 'Exchange quotes with?') | |
if char == "'" | |
let char = (qch ==# "\"" ? "'" : "\"") | end | |
return "\<Plug>Csurround" . qch . char | |
endfu | |
fu! s:findQuote(...) " ( forward=1, action='m' ) | |
" action: m -> move cursor | |
" p -> returns the character position [line, col] | |
" c -> returns the matched character (' or ") | |
if (a:0 > 0) && (a:1==1 || a:1==0) | |
let dir = a:1 | |
let which = (a:0 > 1 ? a:2 : 'm') | |
let fb = (dir) ? '' : 'b' | |
if which =~ 'm' | |
call searchpos('\v["'']', fb) | |
return | end | |
if which =~ 'p' | |
let pos = searchpos('\v["'']', fb . 'n') | |
return pos | end | |
if which =~ 'c' | |
let pos = searchpos('\v["'']', fb . 'nc') | |
let ch = s:charAt( [pos[0], pos[1]-1] ) | |
return ch | end | |
return | |
end | |
let flags = a:0 ? a:1 : 'nc' | |
let cw = getcurpos()[1:2] | |
let fw = searchpos('\v["'']', flags) | |
let fw[1] -= 1 | |
let bw = searchpos('\v["'']', 'b' . flags) | |
let bw[1] -= 1 | |
if fw == cw | |
let pos = fw | |
else | |
let df = (fw[0] - cw[0]) * 80 + (fw[1] - cw[1]) | |
let db = (cw[0] - bw[0]) * 80 + (cw[1] - bw[1]) | |
let pos = df < db ? fw : bw | |
end | |
return s:charAt(pos) | |
endfu | |
fu! s:charAt(...) | |
if a:0 == 1 | |
let pos = a:1 | |
else | |
let pos = [a:1, a:2] | |
end | |
return getline(pos[0])[pos[1]] | |
endfu | |
fu! s:getChar(...) | |
echo '' | |
if (a:0 == 1) | |
echon a:1 | |
elseif (a:0 == 2) | |
exe 'echohl ' . a:1 | |
echon a:2 | |
echohl None | |
end | |
let c = getchar() | |
if (c =~ '^\d\+$') | |
let c = nr2char(c) | |
end | |
return c | |
endfu | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment