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

This method also allows you to do very detailed conditional checks to
decide if the expansion should be made or not (to check if you are
inside a comment using the current syntax group, e.g.).

-- 
HTH,
Hari

>
> Explanation: this remaps 'h' character to check 6 preceding chars.
> If 6 preceding chars are 'foreac', then it is expanded into your you
> foreach-template. Otherwise, literas 'h' is inserted. Since this is
> 1-char-length mapping, there's no delay and no invisibilities.
>
> If you wonder where is expansion (rhs) of your template, it's
> inside function ExpandForeach(). The function PrecedingChars()
> is generic and doesn't change when you change rhs of your template.
>
> 2.
> If you find this too complex, and you don't want to
> press space or tab after 'foreach' (supposedly because it
> makes 8 chars instead of 7?) , then can I suggest either this simple
weirdity:
>
>      :iab foreac foreach ...
>
> (In which case, you get to type 'foreac' then space, which makes
> total 7 characters not 8.), or this mapping
>
>     :imap <ctrl-Z>f foreach ....
>
> which is economy of 5 chars on the lhs side. (You type ctrl-z then f)
>
> Yakov
>
>

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Reply via email to