From d7057e8c68f4e0cc400fa193bf915fd4a73b1bc6 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 9 Nov 2011 15:49:25 -0600 Subject: use proper directory structure --- plugin/textobj.vim | 239 +++++++++++++++++++++++++++++++++++++++++++++++++++++ textobj.vim | 239 ----------------------------------------------------- 2 files changed, 239 insertions(+), 239 deletions(-) create mode 100644 plugin/textobj.vim delete mode 100644 textobj.vim diff --git a/plugin/textobj.vim b/plugin/textobj.vim new file mode 100644 index 0000000..01345bc --- /dev/null +++ b/plugin/textobj.vim @@ -0,0 +1,239 @@ +" Text object creation {{{ +" XXX: use virtualedit here, it should greatly simplify things +let s:text_object_number = 0 +function Textobj(char, callback, ...) + let s:text_object_number += 1 + function s:textobj_{s:text_object_number}(inner, operator, count, callback, ...) + try + let pos = getpos('.') + sandbox let [startline, startcol, endline, endcol] = call(a:callback, [a:inner, a:count] + a:000) + catch /no-match/ + return + finally + call setpos('.', pos) + endtry + if startline == endline + let objlength = endcol - startcol + 1 + else + let objlength = 0 + if endline - startline > 1 + exe 'let objlength += '.join(map(getline(startline + 1, endline - 1), 'strlen(v:val) + 1'), '+') + endif + let objlength += endcol + strlen(getline(startline)) - startcol + 2 + endif + let whichwrap = &whichwrap + set whichwrap+=s + let virtualedit = &virtualedit + if startcol > strlen(getline(startline)) + let startcol = 1 + let startline += 1 + let objlength -= 1 + endif + if endcol == 0 + set virtualedit=onemore + endif + if a:operator == 'v' + let objlength -= 1 + endif + call cursor(startline, startcol) + if a:operator == 'c' + let operator = 'd' + else + let operator = a:operator + end + exe 'normal! '.operator.objlength.' ' + + if a:operator == 'c' + startinsert + endif + let &whichwrap = whichwrap + let &virtualedit = virtualedit + endfunction + + exe 'onoremap a'.a:char.' :call call("textobj_'.s:text_object_number.'", [0, v:operator, v:prevcount, "'.a:callback.'"] + '.string(a:000).')' + exe 'onoremap i'.a:char.' :call call("textobj_'.s:text_object_number.'", [1, v:operator, v:prevcount, "'.a:callback.'"] + '.string(a:000).')' + exe 'xnoremap a'.a:char.' :call call("textobj_'.s:text_object_number.'", [0, "v", v:prevcount, "'.a:callback.'"] + '.string(a:000).')' + exe 'xnoremap i'.a:char.' :call call("textobj_'.s:text_object_number.'", [1, "v", v:prevcount, "'.a:callback.'"] + '.string(a:000).')' +endfunction +" }}} +" Text object definitions {{{ +" arbitrary paired symbols (/ for regex, etc) {{{ +function Textobj_paired(inner, count, char) + let pos = getpos('.') + + let line = strpart(getline(pos[1]), 0, pos[2]) + let lines = getline(1, pos[1] - 1) + [line] + let linenum = pos[1] + for line in reverse(lines) + let objstart = match(line, '.*\zs\\\@" + endif + return s:textobj_arg(a:inner, a:count) + elseif curchar =~ '\s' + normal! W + return s:textobj_arg(a:inner, a:count) + endif + + let line = strpart(getline(pos[1]), 0, pos[2]) + let lines = getline(1, pos[1] - 1) + [line] + let linenum = pos[1] + for line in reverse(lines) + let argbegin = matchend(line, '.*\%(,\s*\|(\)') + 1 + if argbegin != 0 + while argbegin > strlen(line) + let linenum += 1 + let line = getline(linenum) + let argbegin = matchend(line, '^\s*') + 1 + endwhile + break + endif + let linenum -= 1 + endfor + if argbegin == 0 + throw 'no-match' + endif + let argstartline = linenum + + let line = strpart(getline(pos[1]), pos[2] - 1) + let lines = [line] + getline(pos[1] + 1, line('$')) + let linenum = pos[1] + for line in lines + let argend = match(line, '\zs.\?\%(,\|)\)') + 1 + if argend != 0 + if linenum == pos[1] + let argend += pos[2] - 1 + endif + if argend == 1 && getline(linenum)[argend - 1] == ')' + let linenum -= 1 + let argend = strlen(getline(linenum)) + endif + break + endif + let linenum += 1 + endfor + if argend == 0 + throw 'no-match' + endif + let argendline = linenum + + if a:inner == 0 + let endline = getline(argendline) + let startline = getline(argstartline) + if argend >= strlen(endline) + let argend = 0 + let argendline += 1 + let endline = getline(argendline) + endif + if endline[argend] == ')' && startline[argbegin - 2] != '(' + let argbegin = match(strpart(startline, 0, argbegin - 1), '\s*$') + while argbegin == 0 + let argstartline -= 1 + let startline = getline(argstartline) + let argbegin = strlen(startline) + endwhile + elseif endline[argend] != ')' + let argend += matchend(strpart(endline, argend + 1), '^\s*') + 1 + if startline[argbegin - 2] == '(' + for line in [strpart(endline, argend)] + + \ getline(argendline + 1, line('$')) + let argincr = matchend(line, '\s*\ze\S') + if argincr != -1 + let argend += argincr + break + endif + let argendline += 1 + let argend = 0 + endfor + endif + endif + if argend >= strlen(endline) + if argendline == argstartline + let newbegin = matchend(strpart(endline, 0, argbegin), '.*,') + if newbegin != -1 + let argbegin = newbegin + 1 + endif + endif + let argend = 0 + let argendline += 1 + endif + endif + + return [argstartline, argbegin, argendline, argend] +endfunction +" }}} +" }}} +" Text object loading {{{ +for object in g:Textobj_defs + call call('Textobj', object) +endfor +unlet object +" }}} diff --git a/textobj.vim b/textobj.vim deleted file mode 100644 index 01345bc..0000000 --- a/textobj.vim +++ /dev/null @@ -1,239 +0,0 @@ -" Text object creation {{{ -" XXX: use virtualedit here, it should greatly simplify things -let s:text_object_number = 0 -function Textobj(char, callback, ...) - let s:text_object_number += 1 - function s:textobj_{s:text_object_number}(inner, operator, count, callback, ...) - try - let pos = getpos('.') - sandbox let [startline, startcol, endline, endcol] = call(a:callback, [a:inner, a:count] + a:000) - catch /no-match/ - return - finally - call setpos('.', pos) - endtry - if startline == endline - let objlength = endcol - startcol + 1 - else - let objlength = 0 - if endline - startline > 1 - exe 'let objlength += '.join(map(getline(startline + 1, endline - 1), 'strlen(v:val) + 1'), '+') - endif - let objlength += endcol + strlen(getline(startline)) - startcol + 2 - endif - let whichwrap = &whichwrap - set whichwrap+=s - let virtualedit = &virtualedit - if startcol > strlen(getline(startline)) - let startcol = 1 - let startline += 1 - let objlength -= 1 - endif - if endcol == 0 - set virtualedit=onemore - endif - if a:operator == 'v' - let objlength -= 1 - endif - call cursor(startline, startcol) - if a:operator == 'c' - let operator = 'd' - else - let operator = a:operator - end - exe 'normal! '.operator.objlength.' ' - - if a:operator == 'c' - startinsert - endif - let &whichwrap = whichwrap - let &virtualedit = virtualedit - endfunction - - exe 'onoremap a'.a:char.' :call call("textobj_'.s:text_object_number.'", [0, v:operator, v:prevcount, "'.a:callback.'"] + '.string(a:000).')' - exe 'onoremap i'.a:char.' :call call("textobj_'.s:text_object_number.'", [1, v:operator, v:prevcount, "'.a:callback.'"] + '.string(a:000).')' - exe 'xnoremap a'.a:char.' :call call("textobj_'.s:text_object_number.'", [0, "v", v:prevcount, "'.a:callback.'"] + '.string(a:000).')' - exe 'xnoremap i'.a:char.' :call call("textobj_'.s:text_object_number.'", [1, "v", v:prevcount, "'.a:callback.'"] + '.string(a:000).')' -endfunction -" }}} -" Text object definitions {{{ -" arbitrary paired symbols (/ for regex, etc) {{{ -function Textobj_paired(inner, count, char) - let pos = getpos('.') - - let line = strpart(getline(pos[1]), 0, pos[2]) - let lines = getline(1, pos[1] - 1) + [line] - let linenum = pos[1] - for line in reverse(lines) - let objstart = match(line, '.*\zs\\\@" - endif - return s:textobj_arg(a:inner, a:count) - elseif curchar =~ '\s' - normal! W - return s:textobj_arg(a:inner, a:count) - endif - - let line = strpart(getline(pos[1]), 0, pos[2]) - let lines = getline(1, pos[1] - 1) + [line] - let linenum = pos[1] - for line in reverse(lines) - let argbegin = matchend(line, '.*\%(,\s*\|(\)') + 1 - if argbegin != 0 - while argbegin > strlen(line) - let linenum += 1 - let line = getline(linenum) - let argbegin = matchend(line, '^\s*') + 1 - endwhile - break - endif - let linenum -= 1 - endfor - if argbegin == 0 - throw 'no-match' - endif - let argstartline = linenum - - let line = strpart(getline(pos[1]), pos[2] - 1) - let lines = [line] + getline(pos[1] + 1, line('$')) - let linenum = pos[1] - for line in lines - let argend = match(line, '\zs.\?\%(,\|)\)') + 1 - if argend != 0 - if linenum == pos[1] - let argend += pos[2] - 1 - endif - if argend == 1 && getline(linenum)[argend - 1] == ')' - let linenum -= 1 - let argend = strlen(getline(linenum)) - endif - break - endif - let linenum += 1 - endfor - if argend == 0 - throw 'no-match' - endif - let argendline = linenum - - if a:inner == 0 - let endline = getline(argendline) - let startline = getline(argstartline) - if argend >= strlen(endline) - let argend = 0 - let argendline += 1 - let endline = getline(argendline) - endif - if endline[argend] == ')' && startline[argbegin - 2] != '(' - let argbegin = match(strpart(startline, 0, argbegin - 1), '\s*$') - while argbegin == 0 - let argstartline -= 1 - let startline = getline(argstartline) - let argbegin = strlen(startline) - endwhile - elseif endline[argend] != ')' - let argend += matchend(strpart(endline, argend + 1), '^\s*') + 1 - if startline[argbegin - 2] == '(' - for line in [strpart(endline, argend)] + - \ getline(argendline + 1, line('$')) - let argincr = matchend(line, '\s*\ze\S') - if argincr != -1 - let argend += argincr - break - endif - let argendline += 1 - let argend = 0 - endfor - endif - endif - if argend >= strlen(endline) - if argendline == argstartline - let newbegin = matchend(strpart(endline, 0, argbegin), '.*,') - if newbegin != -1 - let argbegin = newbegin + 1 - endif - endif - let argend = 0 - let argendline += 1 - endif - endif - - return [argstartline, argbegin, argendline, argend] -endfunction -" }}} -" }}} -" Text object loading {{{ -for object in g:Textobj_defs - call call('Textobj', object) -endfor -unlet object -" }}} -- cgit v1.2.3