Am 07.03.2010 12:39, schrieb James Cole:
Hi,

I was trying to write a vmap for a custom text-object, and I could
move the cursor to select the text by using a search specified
directly within the vmap, but if I tried to instead put those searches
in separte functions and call those functions from within the vmap, it
didn't work.

When you execute the sequence
    V:<C-U>call NextSection()<Enter>
by hand, you will notice that returning from Cmdline mode clears the
visual area.  Executing
    normal! gv
early within function restores the visual area.

     function! PrevSection()
         execute "?^:"
     endfunction

     function! NextSection()
         execute "/^:"
     endfunction

     vmap ac<ESC>:call PrevSection()<CR>V:call NextSection()<CR>k

(BTW, this is for a text file format I'm using that contains sections
delimited by headings - a heading is just a line that starts with a
colon.
e.g.

     :section 1 heading

     text of section one

     ::section 1.1 heading

     some more text

     :section 2 heading

     the text of section 2
)

suggestion:


function! SelectASection()
    call search("^:", "bcW")
    normal! V
    let line2 = search("^:", "W")
    if line2 == 0
        $
    else
        -
    endif
endfunction

noremap <silent> ac :<C-U>call SelectASection()<CR>
nunmap ac| sunmap ac


This will work for Visual mode and Operator pending mode.

TODO (maybe)
- support a count
- if there is already a Visual area, extend it (don't replace it)

--
Andy

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

Reply via email to