Hi everybody!

Thanks for helpful links on creating text objects. I have the
following issue now. As I described in my previous message, I am after
a text object ae (for "an environment") that would allow to select,
yank, delete, change etc. LaTeX environments. It turned fairly easy
out to code it if you don't want it to work with a count prefix,
namely, I use the following code:

nnoremap [e :<C-U>call
searchpair('\\begin{\a\+\*\=}\|\\(\|\\\[','','\\end{\a\+\*\=\zs}\|\\\zs)\|\\\zs]','bW')<CR>
nnoremap ]e :<C-U>call
searchpair('\\begin{\a\+\*\=}\|\\(\|\\\[','','\\end{\a\+\*\=\zs}\|\\\zs)\|\\\zs]','W')<CR>

vmap [e <Esc>[em'gv``
vmap ]e <Esc>]em'gv``
vmap ae <Esc>[ev]e

omap [e :normal v[e<CR>
omap ]e :normal v]e<CR>
omap ae :normal vae<CR>

It is transparent and seems to work fine: [e brings you to the
beginning of the environment you are currently in, ]e brings you to
its end (to the last character!), ae selects the environment, and the
operator-pending maps work fine, too. You can use [e and ]e repeatedly
to go up within nested environments, and then I decided I want my maps
work with a counter prefix as well, and to make that happen was more
complicated. The code now looks more cumbersome:

fun ToBeginEnv(count)
    let level = a:count
    let l = line(".")
    let c = col(".")
    while level
        if searchpair('\\begin{\a\+\*\=}\|\\(\|\\\[','',
                    \ '\\end{\a\+\*\=\zs}\|\\\zs)\|\\\zs]','bW') < 1
            call cursor(l,c)
            return
        endif
        let level -= 1
    endwhile
endfun

fun ToEndEnv(count)
    let level = a:count
    let l = line(".")
    let c = col(".")
    while level
        if searchpair('\\begin{\a\+\*\=}\|\\(\|\\\[','',
                    \ '\\end{\a\+\*\=\zs}\|\\\zs)\|\\\zs]','W') < 1
            call cursor(l,c)
            return
        endif
        let level -= 1
    endwhile
endfun

nnoremap <silent> [e :<C-U>call ToBeginEnv(v:count1)<CR>
nnoremap <silent> ]e :<C-U>call ToEndEnv(v:count1)<CR>

" using the above definitions doesn't work, I suppose because hitting
<Esc> discards the count
" prefix if any
vmap <silent> [e :<C-U>call ToBeginEnv(v:count1)<CR><Esc>m'gv``
vmap <silent> ]e :<C-U>call ToEndEnv(v:count1)<CR><Esc>m'gv``
vmap <silent> ae :<C-U>call ToBeginEnv(v:count1)<CR>v]e

" These seem to work fine...
omap <silent> [e :normal v[e<CR>
omap <silent> ]e :normal v]e<CR>

" ...but this doesn't!
omap <silent> ae :normal vae<CR>

I cannot figure out what is the problem with the last map. It behaves
as follows: if I do, e.g., y2ae, it goes 2 levels up to the beginning
of the environment, but then instead of yanking the piece of text from
that point to the matching end of environment it actually does 2]e,
that is, it goes to the matching end of the environment, and then goes
one more level up. In other words, the count gets substituted in both
(hidden) instances of [e and ]e.

Furthermore, although hitting v2ae in normal mode gives the correct
result, typing :normal v2ae selects only the most inner environment
(instead of going one level up). What is the difference between v2ae
and :normal v2ae?

Any suggestions as to how to fix this? Many thanks in advance to the
whole community! I am relatively new to Vim, but I'd like to say: Vim
rocks!

Regards,
Oleksandr

On Mon, Feb 9, 2009 at 10:12 AM, Ben Fritz <[email protected]> wrote:
>
>
>
> On Feb 7, 5:30 am, Luc Hermitte <[email protected]> wrote:
>
>> On vim.wikia, there is a tip that shows how to define such a
>> mapping.
>>  http://vim.wikia.com/wiki/Indent_text_object
>>
>
> There's also:
>
> http://vim.wikia.com/wiki/Creating_new_text_objects
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply via email to