How to wake up getchar() ?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How to wake up getchar() ?

Eric Arnold

Here is the test example:


map <expr> ,tt Tst()

function! Tst()
        "exe "normal! :   <CR>"

        while getchar(1) < 1
                sleep 100ms
        endwhile

        echomsg "Done!"
endfunction



It never sees input without the

        exe "normal! :   <CR>"

As you might guess, it took me forever to find a workaround.  Anybody know
about this?

Running MeScrewed XP, gvim70c.




Reply | Threaded
Open this post in threaded view
|

Re: How to wake up getchar() ?

Bram Moolenaar

Eric Arnold wrote:

> Here is the test example:
>
>
> map <expr> ,tt Tst()
>
> function! Tst()
> "exe "normal! :   <CR>"
>
> while getchar(1) < 1
> sleep 100ms
> endwhile
>
> echomsg "Done!"
> endfunction
>
>
>
> It never sees input without the
>
> exe "normal! :   <CR>"
>
> As you might guess, it took me forever to find a workaround.  Anybody know
> about this?

The evaluation of the map expression is done inside the function that
gets a character from the user and takes care of applying mappings.
That function is protected from being called recursively, thus getchar()
doesn't work.

That the use of a normal command makes it work is a bug.  Instead of
incrementing/decrementing a counter for recursive use of the function a
true/false flag is used.  I'll fix that.  Your function with the
workaround will stop working then.

I'll see if I can make it work.

--
hundred-and-one symptoms of being an internet addict:
17. You turn on your intercom when leaving the room so you can hear if new
    e-mail arrives.

 /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\        download, build and distribute -- http://www.A-A-P.org        ///
 \\\            help me help AIDS victims -- http://www.ICCF.nl         ///
Reply | Threaded
Open this post in threaded view
|

Vim70d map-to-func input prob? was Re: How to wake up getchar() ?

Eric Arnold

I'm not sure what the intended fix for 70d was, but I can no longer send a char
to a function indirectly through an <expr> mapping, meaning that in the example
below,  \b  does not send a <space>  to  Tst().  Tst() waits for input directly
from the user.  

Note:  using      
    map \a : call Tst()<CR>
works as expected, so the problem seems to be with <expr>




map <expr> \a Tst()
map \b \a<space>

function! Tst()
        "exe "normal! :   <CR>"

        while getchar(1) < 1
                sleep 100ms
        endwhile

        echomsg "Done!"
endfunction




--- Bram Moolenaar <[hidden email]> wrote:

>
> Eric Arnold wrote:
>
> > Here is the test example:
> >
> >
> > map <expr> ,tt Tst()
> >
> > function! Tst()
> > "exe "normal! :   <CR>"
> >
> > while getchar(1) < 1
> > sleep 100ms
> > endwhile
> >
> > echomsg "Done!"
> > endfunction
> >
> >
> >
> > It never sees input without the
> >
> > exe "normal! :   <CR>"
> >
> > As you might guess, it took me forever to find a workaround.  Anybody know
> > about this?
>
> The evaluation of the map expression is done inside the function that
> gets a character from the user and takes care of applying mappings.
> That function is protected from being called recursively, thus getchar()
> doesn't work.
>
> That the use of a normal command makes it work is a bug.  Instead of
> incrementing/decrementing a counter for recursive use of the function a
> true/false flag is used.  I'll fix that.  Your function with the
> workaround will stop working then.
>
> I'll see if I can make it work.
>
> --
> hundred-and-one symptoms of being an internet addict:
> 17. You turn on your intercom when leaving the room so you can hear if new
>     e-mail arrives.
>
>  /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
> ///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
> \\\        download, build and distribute -- http://www.A-A-P.org        ///
>  \\\            help me help AIDS victims -- http://www.ICCF.nl         ///
>

Reply | Threaded
Open this post in threaded view
|

Vim70d map-to-func input prob? was Re: How to wake up getchar() ?

Eric Arnold
In reply to this post by Bram Moolenaar

I'm not sure what the intended fix for 70d was, but I can no longer send a char
to a function indirectly through an <expr> mapping, meaning that in the example
below,  \b  does not send a <space>  to  Tst().  Tst() waits for input directly
from the user.  

Note:  using      
    map \a : call Tst()<CR>
works as expected, so the problem seems to be with <expr>




map <expr> \a Tst()
map \b \a<space>

function! Tst()
        "exe "normal! :   <CR>"

        while getchar(1) < 1
                sleep 100ms
        endwhile

        echomsg "Done!"
endfunction




--- Bram Moolenaar <[hidden email]> wrote:

>
> Eric Arnold wrote:
>
> > Here is the test example:
> >
> >
> > map <expr> ,tt Tst()
> >
> > function! Tst()
> > "exe "normal! :   <CR>"
> >
> > while getchar(1) < 1
> > sleep 100ms
> > endwhile
> >
> > echomsg "Done!"
> > endfunction
> >
> >
> >
> > It never sees input without the
> >
> > exe "normal! :   <CR>"
> >
> > As you might guess, it took me forever to find a workaround.  Anybody know
> > about this?
>
> The evaluation of the map expression is done inside the function that
> gets a character from the user and takes care of applying mappings.
> That function is protected from being called recursively, thus getchar()
> doesn't work.
>
> That the use of a normal command makes it work is a bug.  Instead of
> incrementing/decrementing a counter for recursive use of the function a
> true/false flag is used.  I'll fix that.  Your function with the
> workaround will stop working then.
>
> I'll see if I can make it work.
>
> --
> hundred-and-one symptoms of being an internet addict:
> 17. You turn on your intercom when leaving the room so you can hear if new
>     e-mail arrives.
>
>  /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
> ///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
> \\\        download, build and distribute -- http://www.A-A-P.org        ///
>  \\\            help me help AIDS victims -- http://www.ICCF.nl         ///
>

Reply | Threaded
Open this post in threaded view
|

Vim70d map-to-func input prob? was Re: How to wake up getchar() ?

Bram Moolenaar
In reply to this post by Eric Arnold

Eric Arnold wrote:

> I'm not sure what the intended fix for 70d was, but I can no longer
> send a char to a function indirectly through an <expr> mapping,
> meaning that in the example below,  \b  does not send a <space>  to
> Tst().  Tst() waits for input directly from the user.  
>
> Note:  using      
>     map \a : call Tst()<CR>
> works as expected, so the problem seems to be with <expr>
>
>
> map <expr> \a Tst()
> map \b \a<space>
>
> function! Tst()
> "exe "normal! :   <CR>"
>
> while getchar(1) < 1
> sleep 100ms
> endwhile
>
> echomsg "Done!"
> endfunction

To make getchar() work inside the function that's evaluated for an
<expr> mapping the typeahead needs to be saved, reset and restored
later.  Thus the function can't get any typeahead.

This can't be changed, because recursive use of the function that gets
characters is not possible.

I think this is actually what most people want to happen.  If you want
to pass something tot he function you can give it an argument.

--
From "know your smileys":
 ...---...   SOS

 /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\        download, build and distribute -- http://www.A-A-P.org        ///
 \\\            help me help AIDS victims -- http://www.ICCF.nl         ///
Reply | Threaded
Open this post in threaded view
|

Re: Vim70d map-to-func input prob? was Re: How to wake up getchar() ?

Eric Arnold-3
When I first saw the  <expr>  feature, I thought it was a just a
cleaner way of avoiding the :ex interaction in    :call .... <CR> in

     map \a : call Tst()<CR>

Since I'm still not really clear on why there is a difference with
respect to recursion handling, I thought I should ask whether using
the above is still a supported way to implement

1) calling  Tst()  via  \a  to allow  Tst()  to read chars directly
from the user,

while at the same time,

2) calling  Tst()  via
    map \b  \a<key><key><key>
to simulate multiple user key input.


On 4/15/06, Bram Moolenaar <[hidden email]> wrote:

>
> Eric Arnold wrote:
>
> > I'm not sure what the intended fix for 70d was, but I can no longer
> > send a char to a function indirectly through an <expr> mapping,
> > meaning that in the example below,  \b  does not send a <space>  to
> > Tst().  Tst() waits for input directly from the user.
> >
> > Note:  using
> >     map \a : call Tst()<CR>
> > works as expected, so the problem seems to be with <expr>
> >
> >
> > map <expr> \a Tst()
> > map \b \a<space>
> >
> > function! Tst()
> >       "exe "normal! :   <CR>"
> >
> >       while getchar(1) < 1
> >               sleep 100ms
> >       endwhile
> >
> >       echomsg "Done!"
> > endfunction
>
> To make getchar() work inside the function that's evaluated for an
> <expr> mapping the typeahead needs to be saved, reset and restored
> later.  Thus the function can't get any typeahead.
>
> This can't be changed, because recursive use of the function that gets
> characters is not possible.
>
> I think this is actually what most people want to happen.  If you want
> to pass something tot he function you can give it an argument.
>
> --
> From "know your smileys":
>  ...---...   SOS
>
>  /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
> ///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
> \\\        download, build and distribute -- http://www.A-A-P.org        ///
>  \\\            help me help AIDS victims -- http://www.ICCF.nl         ///
>