Loading ...
Sorry, an error occurred while loading the content.

InsertCharPre often lose handles of key input.

Expand Messages
  • mattn
    Hi. When set autocmd InsertCharPre like following, It should work with all key typings to insert f at all. autocmd InsertCharPre * let v:char = f But
    Message 1 of 8 , Oct 21, 2011
    • 0 Attachment
      Hi.

      When set autocmd InsertCharPre like following, It should work with all
      key typings to insert 'f' at all.

        autocmd InsertCharPre * let v:char = 'f'

      But often, it show other characters (not 'f') in the screen.
      This causes by two problems.

      1. Typing keys while popup menu is shown are not handled in normally.
        When popup menu is shown, key types are handled to decide of candidate.
        And it behave independently.

      2. Keys are grabbed at once for speed up.
        See the comment arround edit.c:5864.

          /*
           * If there's any pending input, grab up to INPUT_BUFLEN at once.
           * This speeds up normal text input considerably.
           * Don't do this when 'cindent' or 'indentexpr' is set, because we might
           * need to re-indent at a ':', or any other character (but not what
           * 'paste' is set)..
           * Don't do this when there is InsertCharPre autocommand defined,
           * because we need to fire the event for every character.
           */

        So, some key types don't fire InsertCharPre.
        It should be disabled while InsertCharPre is working.

      Below is a patch. Please check and include.

      https://gist.github.com/1303301

      diff -r 37ecb8ff4560 src/edit.c
      --- a/src/edit.c Thu Oct 20 22:22:38 2011 +0200
      +++ b/src/edit.c Fri Oct 21 16:19:52 2011 +0900
      @@ -784,7 +784,17 @@
        * completion: Add to "compl_leader". */
        if (ins_compl_accept_char(c))
        {
      -    ins_compl_addleader(c);
      +    set_vim_var_char(c);
      +    ++textlock;
      +    if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL,
      +   FALSE, curbuf))
      +    {
      + c = PTR2CHAR(get_vim_var_str(VV_CHAR));
      +    }
      +    if (c)
      + ins_compl_addleader(c);
      +    set_vim_var_string(VV_CHAR, NULL, -1);
      +    --textlock;
           continue;
        }
       
      @@ -1393,31 +1403,37 @@
       #ifdef FEAT_AUTOCMD
           if (!p_paste)
           {
      + int executed;
        /* Trigger the InsertCharPre event.  Lock the text to avoid
        * weird things from happening. */
        set_vim_var_char(c);
        ++textlock;
      - if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL,
      -       FALSE, curbuf))
      + executed = apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL,
      +    FALSE, curbuf);
      + --textlock;
      + if (executed)
        {
           /* Get the new value of v:char.  If it is more than one
            * character insert it literally. */
      -    char_u *s = get_vim_var_str(VV_CHAR);
      -    if (MB_CHARLEN(s) > 1)
      +    if (stop_arrow() != FAIL)
           {
      - if (stop_arrow() != FAIL)
      + char_u *str = get_vim_var_str(VV_CHAR);
      + char_u *ptr = str;
      + while (*ptr != NUL)
        {
      -    ins_str(s);
      -    AppendToRedobuffLit(s, -1);
      +    c = PTR2CHAR(ptr);
      +    if (c == CAR || c == K_KENTER || c == NL)
      + ins_eol(c);
      +    else
      + ins_char(c);
      +    ptr += (*mb_ptr2len)(ptr);
        }
      - c = NUL;
      + AppendToRedobuffLit(str, -1);
           }
      -    else
      - c = PTR2CHAR(s);
      +    c = NUL;
        }
       
        set_vim_var_string(VV_CHAR, NULL, -1);
      - --textlock;
       
        /* If the new value is an empty string then don't insert a
        * char. */
      @@ -5867,6 +5883,8 @@
            * Don't do this when 'cindent' or 'indentexpr' is set, because we might
            * need to re-indent at a ':', or any other character (but not what
            * 'paste' is set)..
      +     * Don't do this when there is InsertCharPre autocommand defined,
      +     * because we need to fire the event for every character.
            */
       #ifdef USE_ON_FLY_SCROLL
           dont_scroll = FALSE; /* allow scrolling here */
      @@ -5884,6 +5902,9 @@
       #ifdef FEAT_RIGHTLEFT
           && !p_ri
       #endif
      +#ifdef FEAT_AUTOCMD
      +    && !has_insertcharpre()
      +#endif
              )
           {
       #define INPUT_BUFLEN 100
      diff -r 37ecb8ff4560 src/fileio.c
      --- a/src/fileio.c Thu Oct 20 22:22:38 2011 +0200
      +++ b/src/fileio.c Fri Oct 21 16:19:52 2011 +0900
      @@ -6247,6 +6247,11 @@
       #ifdef VMS
        vms_remove_version(retval); /* we do not need versions here */
       #endif
      +#ifdef WIN3264
      + /* Avoids to make strange filename of NTFS stream like 'foo:bar.swp' */
      + if ((e = vim_strchr(retval, ':')) != NULL && !after_pathsep(retval, e))
      +    *e = NUL;
      +#endif
           }
       
           /*
      @@ -9092,6 +9097,15 @@
           return (first_autopat[(int)EVENT_CURSORMOVEDI] != NULL);
       }
       
      +/*
      + * Return TRUE when there is a InsertCharPre autocommand defined.
      + */
      +    int
      +has_insertcharpre()
      +{
      +    return (first_autopat[(int)EVENT_INSERTCHARPRE] != NULL);
      +}
      +
           static int
       apply_autocmds_group(event, fname, fname_io, force, group, buf, eap)
           event_T event;
      diff -r 37ecb8ff4560 src/proto/fileio.pro
      --- a/src/proto/fileio.pro Thu Oct 20 22:22:38 2011 +0200
      +++ b/src/proto/fileio.pro Fri Oct 21 16:19:52 2011 +0900
      @@ -43,6 +43,7 @@
       int trigger_cursorhold __ARGS((void));
       int has_cursormoved __ARGS((void));
       int has_cursormovedI __ARGS((void));
      +int has_insertcharpre __ARGS((void));
       void block_autocmds __ARGS((void));
       void unblock_autocmds __ARGS((void));
       int has_autocmd __ARGS((event_T event, char_u *sfname, buf_T *buf));


      --
      You received this message from the "vim_dev" 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
    • mattn
      Sorry, it was included another patch. Check below s patch. https://gist.github.com/1303356 diff -r 37ecb8ff4560 src/edit.c ... +++ b/src/edit.c Fri Oct 21
      Message 2 of 8 , Oct 21, 2011
      • 0 Attachment
        Sorry, it was included another patch.
        Check below's patch.


        diff -r 37ecb8ff4560 src/edit.c
        --- a/src/edit.c Thu Oct 20 22:22:38 2011 +0200
        +++ b/src/edit.c Fri Oct 21 16:19:52 2011 +0900
        @@ -784,7 +784,17 @@
          * completion: Add to "compl_leader". */
          if (ins_compl_accept_char(c))
          {
        -    ins_compl_addleader(c);
        +    set_vim_var_char(c);
        +    ++textlock;
        +    if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL,
        +   FALSE, curbuf))
        +    {
        + c = PTR2CHAR(get_vim_var_str(VV_CHAR));
        +    }
        +    if (c)
        + ins_compl_addleader(c);
        +    set_vim_var_string(VV_CHAR, NULL, -1);
        +    --textlock;
             continue;
          }
         
        @@ -1393,31 +1403,37 @@
         #ifdef FEAT_AUTOCMD
             if (!p_paste)
             {
        + int executed;
          /* Trigger the InsertCharPre event.  Lock the text to avoid
          * weird things from happening. */
          set_vim_var_char(c);
          ++textlock;
        - if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL,
        -       FALSE, curbuf))
        + executed = apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL,
        +    FALSE, curbuf);
        + --textlock;
        + if (executed)
          {
             /* Get the new value of v:char.  If it is more than one
              * character insert it literally. */
        -    char_u *s = get_vim_var_str(VV_CHAR);
        -    if (MB_CHARLEN(s) > 1)
        +    if (stop_arrow() != FAIL)
             {
        - if (stop_arrow() != FAIL)
        + char_u *str = get_vim_var_str(VV_CHAR);
        + char_u *ptr = str;
        + while (*ptr != NUL)
          {
        -    ins_str(s);
        -    AppendToRedobuffLit(s, -1);
        +    c = PTR2CHAR(ptr);
        +    if (c == CAR || c == K_KENTER || c == NL)
        + ins_eol(c);
        +    else
        + ins_char(c);
        +    ptr += (*mb_ptr2len)(ptr);
          }
        - c = NUL;
        + AppendToRedobuffLit(str, -1);
             }
        -    else
        - c = PTR2CHAR(s);
        +    c = NUL;
          }
         
          set_vim_var_string(VV_CHAR, NULL, -1);
        - --textlock;
         
          /* If the new value is an empty string then don't insert a
          * char. */
        @@ -5867,6 +5883,8 @@
              * Don't do this when 'cindent' or 'indentexpr' is set, because we might
              * need to re-indent at a ':', or any other character (but not what
              * 'paste' is set)..
        +     * Don't do this when there is InsertCharPre autocommand defined,
        +     * because we need to fire the event for every character.
              */
         #ifdef USE_ON_FLY_SCROLL
             dont_scroll = FALSE; /* allow scrolling here */
        @@ -5884,6 +5902,9 @@
         #ifdef FEAT_RIGHTLEFT
             && !p_ri
         #endif
        +#ifdef FEAT_AUTOCMD
        +    && !has_insertcharpre()
        +#endif
                )
             {
         #define INPUT_BUFLEN 100
        diff -r 37ecb8ff4560 src/fileio.c
        --- a/src/fileio.c Thu Oct 20 22:22:38 2011 +0200
        +++ b/src/fileio.c Fri Oct 21 16:19:52 2011 +0900
        @@ -9092,6 +9097,15 @@
             return (first_autopat[(int)EVENT_CURSORMOVEDI] != NULL);
         }
         
        +/*
        + * Return TRUE when there is a InsertCharPre autocommand defined.
        + */
        +    int
        +has_insertcharpre()
        +{
        +    return (first_autopat[(int)EVENT_INSERTCHARPRE] != NULL);
        +}
        +
             static int
         apply_autocmds_group(event, fname, fname_io, force, group, buf, eap)
             event_T event;
        diff -r 37ecb8ff4560 src/proto/fileio.pro
        --- a/src/proto/fileio.pro Thu Oct 20 22:22:38 2011 +0200
        +++ b/src/proto/fileio.pro Fri Oct 21 16:19:52 2011 +0900
        @@ -43,6 +43,7 @@
         int trigger_cursorhold __ARGS((void));
         int has_cursormoved __ARGS((void));
         int has_cursormovedI __ARGS((void));
        +int has_insertcharpre __ARGS((void));
         void block_autocmds __ARGS((void));
         void unblock_autocmds __ARGS((void));
         int has_autocmd __ARGS((event_T event, char_u *sfname, buf_T *buf));

        --
        You received this message from the "vim_dev" 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
      • Bram Moolenaar
        ... Thanks. I ll put it in the todo list. I m not sure if InsertCharPre should apply when the popup menu is visible, keys mean something else then. Perhaps
        Message 3 of 8 , Oct 22, 2011
        • 0 Attachment
          Yasuhiro Matsumoto wrote:

          > When set autocmd InsertCharPre like following, It should work with all
          > key typings to insert 'f' at all.
          >
          > autocmd InsertCharPre * let v:char = 'f'
          >
          > But often, it show other characters (not 'f') in the screen.
          > This causes by two problems.
          >
          > 1. Typing keys while popup menu is shown are not handled in normally.
          > When popup menu is shown, key types are handled to decide of candidate.
          > And it behave independently.
          >
          > 2. Keys are grabbed at once for speed up.
          > See the comment arround edit.c:5864.
          >
          > /*
          > * If there's any pending input, grab up to INPUT_BUFLEN at once.
          > * This speeds up normal text input considerably.
          > * Don't do this when 'cindent' or 'indentexpr' is set, because we might
          > * need to re-indent at a ':', or any other character (but not what
          > * 'paste' is set)..
          > * Don't do this when there is InsertCharPre autocommand defined,
          > * because we need to fire the event for every character.
          > */
          >
          > So, some key types don't fire InsertCharPre.
          > It should be disabled while InsertCharPre is working.
          >
          > Below is a patch. Please check and include.

          Thanks. I'll put it in the todo list.

          I'm not sure if InsertCharPre should apply when the popup menu is
          visible, keys mean something else then. Perhaps we would need another
          event for that sitiuation?

          --
          From "know your smileys":
          :-D Big smile

          /// Bram Moolenaar -- Bram@... -- http://www.Moolenaar.net \\\
          /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
          \\\ an exciting new programming language -- http://www.Zimbu.org ///
          \\\ help me help AIDS victims -- http://ICCF-Holland.org ///

          --
          You received this message from the "vim_dev" 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
        • mattn
          ... No. I think that it can avoid to insert f with checking pumvisible() while popup menu is shown. autocmd InsertCharPre * let v:char = pumvisible() ?
          Message 4 of 8 , Oct 22, 2011
          • 0 Attachment

            I'm not sure if InsertCharPre should apply when the popup menu is
            visible, keys mean something else then.  Perhaps we would need another
            event for that sitiuation?

            No. I think that it can avoid to insert 'f' with checking pumvisible() while popup menu is shown.

            autocmd InsertCharPre * let v:char = pumvisible() ? v:char : 'f'

            (sorry for rough code)

            --
            You received this message from the "vim_dev" 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
          • Bram Moolenaar
            ... The question is if plugin authors that use InsertCharPre are aware of the possibility that the event will be triggered in other circumstances than typing
            Message 5 of 8 , Oct 22, 2011
            • 0 Attachment
              Yasuhiro Matsumoto wrote:

              > > I'm not sure if InsertCharPre should apply when the popup menu is
              > > visible, keys mean something else then. Perhaps we would need another
              > > event for that sitiuation?
              > >
              > No. I think that it can avoid to insert 'f' with checking pumvisible() while
              > popup menu is shown.
              >
              > autocmd InsertCharPre * let v:char = pumvisible() ? v:char : 'f'

              The question is if plugin authors that use InsertCharPre are aware of
              the possibility that the event will be triggered in other circumstances
              than typing text to be inserted.

              --
              From "know your smileys":
              :'-D Laughing so much that they're crying

              /// Bram Moolenaar -- Bram@... -- http://www.Moolenaar.net \\\
              /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
              \\\ an exciting new programming language -- http://www.Zimbu.org ///
              \\\ help me help AIDS victims -- http://ICCF-Holland.org ///

              --
              You received this message from the "vim_dev" 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
            • mattn
              Ah, do you mean that several plugins using InsertCharPre may break own behavior? Hmm, I don t know what problem may happen. Can you adduce reasons or example
              Message 6 of 8 , Oct 22, 2011
              • 0 Attachment
                Ah, do you mean that several plugins using InsertCharPre may break own behavior?

                Hmm, I don't know what problem may happen.
                Can you adduce reasons or example case?

                --
                You received this message from the "vim_dev" 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
              • Ingo Karkat
                ... Isn t that mostly a matter of documenting it? I, for one, would appreciate the possibility to remap the keys in the special pumvisible mode (e.g. the
                Message 7 of 8 , Oct 22, 2011
                • 0 Attachment
                  On 22-Oct-2011 19:47, Bram Moolenaar wrote:

                  > Yasuhiro Matsumoto wrote:
                  >
                  >>> I'm not sure if InsertCharPre should apply when the popup menu is
                  >>> visible, keys mean something else then. Perhaps we would need another
                  >>> event for that sitiuation?
                  >>>
                  >> No. I think that it can avoid to insert 'f' with checking pumvisible() while
                  >> popup menu is shown.
                  >>
                  >> autocmd InsertCharPre * let v:char = pumvisible() ? v:char : 'f'
                  >
                  > The question is if plugin authors that use InsertCharPre are aware of
                  > the possibility that the event will be triggered in other circumstances
                  > than typing text to be inserted.

                  Isn't that mostly a matter of documenting it?

                  I, for one, would appreciate the possibility to "remap" the keys in the special
                  "pumvisible" mode (e.g. the CTRL-X_CTRL-N "copy the words following the previous
                  expansion"). Please forgive my ignorance, I haven't yet tried whether that is
                  now possible with InsertCharPre and changing v:char, but I remember that I once
                  wanted to remap those keys and found out that I couldn't via the usual :imap
                  commands.

                  (A (compatibility-breaking) alternative would be to extend the popup menu into a
                  proper sub-mode of insert mode, complete with :pummap, etc.; I have never found
                  the pumvisible() checks for insert-mode mappings particularly elegant.)

                  -- regards, ingo

                  --
                  You received this message from the "vim_dev" 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
                • mattn
                  I don t think InsertCharPre should support remap. It can use feedkeys() instead of that. I think problem is that vim spill the keys handling while popup is
                  Message 8 of 8 , Jan 30, 2012
                  • 0 Attachment
                    I don't think InsertCharPre should support remap. It can use feedkeys() instead of that.
                    I think problem is that vim spill the keys handling while popup is displaying.

                    Thanks.

                    --
                    You received this message from the "vim_dev" 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
                  Your message has been successfully submitted and would be delivered to recipients shortly.