From 0aca84b834d453b8c36eb79ce10be9e1838f14b5 Mon Sep 17 00:00:00 2001 From: jluehrs2 Date: Mon, 28 Apr 2008 01:22:47 -0500 Subject: add a function to generate text objects --- vimrc | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/vimrc b/vimrc index 03ddc2d..59070b5 100644 --- a/vimrc +++ b/vimrc @@ -584,6 +584,43 @@ endif " }}} " }}} " Text objects {{{ +" Text object creation {{{ +let g:text_object_number = 0 +function Textobj(char, callback) + let g:text_object_number += 1 + function Textobj_{g:text_object_number}(inner, operator, count, callback) + let [startline, startcol, endline, endcol] = function(a:callback)(a:inner, a:count) + if startline == endline + let objlength = endcol - startcol + 1 + else + let lines = getline(startline + 1, endline - 1) + let lines = [strpart(getline(startline), startcol - 1)] + + \ lines + + \ [strpart(getline(endline), 0, endcol)] + let objlength = 0 + for line in lines + let objlength += strlen(line) + 1 + endfor + let objlength -= 1 + endif + call cursor(startline, startcol) + exe 'normal! '.a:operator.objlength.' ' + + if a:operator == 'c' + normal! l + startinsert + elseif a:operator == 'v' + normal! h + endif + endfunction + + let cbname = '"' . substitute(a:callback, '^s:', '', '') . '"' + exe 'onoremap a'.a:char.' :call Textobj_'.g:text_object_number.'(0, v:operator, v:prevcount, '.cbname.')' + exe 'onoremap i'.a:char.' :call Textobj_'.g:text_object_number.'(1, v:operator, v:prevcount, '.cbname.')' + exe 'xnoremap a'.a:char.' :call Textobj_'.g:text_object_number.'(0, "v", v:prevcount, '.cbname.')' + exe 'xnoremap i'.a:char.' :call Textobj_'.g:text_object_number.'(1, "v", v:prevcount, '.cbname.')' +endfunction +" }}} " / for regex {{{ function Textobj_regex(inner, operator) let pos = getpos('.') -- cgit v1.2.3