On 7/13/06, Hari Krishna Dara <[EMAIL PROTECTED]> wrote:

On Thu, 13 Jul 2006 at 9:21am, Yakov Lerner wrote:

> On 7/13/06, Peter Hodge <[EMAIL PROTECTED]> wrote:
> > However, if I type anything beginning with 'for', the letters disappear
> > until I
> > type something that is not part of the mapping.  I understand this is
> > the
> > standard behaviour, but is there any way to change it so I can see
> >  what I am
> > typing?
>
> Although there is no stardard option for imap to do this (make incompete
> mapping visible; yes I'd find it seful, too), I found  a weird trick that
> does what you want.
> Does the following do what you want:
> -----------------------------
> :imap h <c-r>=PrecedingChars(6) !=# 'foreac' ? 'h' : ExpandForeach() <cr>
>
> function! ExpandForeach() " expansion of the foreach mapping
>     " nb: 'foreac' is already there. So we don't need to repeat it
>     return "h() {\n}\<Left>"
> endfunction
>
> function! PrecedingChars(n)
>     " return n chars preceding cursor in rhs of :imap mapping
>     return getline('.')[col('.')-1-a:n : col('.')-2]
> endfunction
> -------------------------------

Very clever alternative to iab. You can take advantage of the new <expr>
maps to simplify and generalize this as below (note, I changed the
mapping to not remap):

:inoremap <expr> h CheckExpand('foreac', 'h', "h() {\n}\<Left>") " expansion of
the foreach mapping

function! CheckExpand(precChars, curChar, expansion)
    if PrecedingChars(strlen(a:precChars)) ==# a:precChars
        " nb: precChar is already there. So we don't need to repeat it
        return a:expansion
    else
        return a:curChar
    endif
endfunction

function! PrecedingChars(n)
    " return n chars preceding cursor in rhs of :imap mapping
    return getline('.')[col('.')-1-a:n : col('.')-2]
endfunction

Nice generalization, yes.

Yakov

Reply via email to