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

Re: setting command-line from a function

Expand Messages
  • Benji Fisher
    ... So the idea is to use the standard completion mechanism and then modify it, rather than do it all from scratch? ... Isn t e a Command-line-mode
    Message 1 of 7 , Jul 2, 2004
      On Thu, Jul 01, 2004 at 02:14:07PM -0700, Hari Krishna Dara wrote:
      >
      > I am wondering if there is a trick to change the command-line from with
      > in a function that gets executed from the command-line, more precisely
      > from the custom completion function. What I want to do is be able to
      > expand special characters such as the filename meta-characters and other
      > custom sequences using the custom completion mechanism. I tried two
      > approaches and both didn't work.
      >
      > - Execute ^\e command using :normal!. This is totally ignored (I think I
      > see a beep).
      > - Return control characters from the completion function for a match.
      > The control characters get inserved literally.

      So the idea is to use the standard completion mechanism and then
      modify it, rather than do it all from scratch?

      > Here is a sample for the first case:
      >
      > command! -nargs=* -complete=custom,CmdComplete Cmd :echo '<args>'
      >
      > function! CmdComplete(ArgLead, CmdLine, CursorPos)
      > let argLead = expand(a:ArgLead)
      > if argLead !=# a:ArgLead
      > let cmdLine = strpart(a:CmdLine, 0, a:CursorPos-strlen(a:ArgLead)) .
      > \ argLead . strpart(a:CmdLine, a:CursorPos)
      > exec "normal! \<C-\>e'".cmdLine."'\<CR>"
      > call setcmdpos(a:CursorPos+(strlen(argLead) - strlen(a:ArgLead)))
      > return ''
      > endif
      > return ''
      > endfunction
      >
      > What I am expecting the above sample to do is to expand % to the current
      > filename etc.
      >
      > Is it hard for the ^\e command to work from with in a function? Why
      > isn't there a setcmdline() function like the existing setcmdpos()? I
      > appreciate any help.

      Isn't <C-\>e a Command-line-mode command rather than a Normal-mode
      command? Also, I think you have to double the second \ character. So
      try

      exec "normal! :\<C-\\>e'".cmdLine."'\<CR>"
      ^ ^

      HTH --Benji Fisher
    • Hari Krishna Dara
      ... I am not sure what you mean by do it from scratch , but using the custom completion mechanism is the only thing that comes in my mind that to do anything
      Message 2 of 7 , Jul 2, 2004
        On Fri, 2 Jul 2004 at 11:13am, Benji Fisher wrote:

        > On Thu, Jul 01, 2004 at 02:14:07PM -0700, Hari Krishna Dara wrote:
        > >
        > > I am wondering if there is a trick to change the command-line from with
        > > in a function that gets executed from the command-line, more precisely
        > > from the custom completion function. What I want to do is be able to
        > > expand special characters such as the filename meta-characters and other
        > > custom sequences using the custom completion mechanism. I tried two
        > > approaches and both didn't work.
        > >
        > > - Execute ^\e command using :normal!. This is totally ignored (I think I
        > > see a beep).
        > > - Return control characters from the completion function for a match.
        > > The control characters get inserved literally.
        >
        > So the idea is to use the standard completion mechanism and then
        > modify it, rather than do it all from scratch?

        I am not sure what you mean by 'do it from scratch', but using the
        custom completion mechanism is the only thing that comes in my mind that
        to do anything like this. Of course, you can map <Tab>, but making <Tab>
        work only for one command can be impossible, besides this approach may
        not be clean. Besides, I already implemented custom completion to
        support context-sensitive completion, so I just want to enhance that.

        >
        > Isn't <C-\>e a Command-line-mode command rather than a Normal-mode
        > command?

        I think you are right, but how do I execute such a command? Seems like
        :normal! when done when mode() is 'c' would execute it in
        command-line-mode, but couldn't verify it or confirm from documentation.

        > Also, I think you have to double the second \ character. So
        > try
        >
        > exec "normal! :\<C-\\>e'".cmdLine."'\<CR>"
        > ^ ^

        Actually I that is what I first tried, but I think Vim considers <C-\>
        as special, so it doesn't eat away the \ in the keysequence (or the key
        sequence is expanded even before the general back-slash expansion, so
        only a single backslash seems to work. I verified it by stepping through
        the debug mode. The command when stepped-in, appears as "normal! ^\e".

        But prefixing the command with : is interesting. I just tried it and
        oops... it made Vim crash. If this is the expected way to execute a
        command-line-mode normal command, then the bug needs to be fixed. Here
        is how you can reproduce the crash:

        command! -nargs=* -complete=custom,CmdComplete Cmd :echo '<args>'

        function! CmdComplete(ArgLead, CmdLine, CursorPos)
        let argLead = expand(a:ArgLead)
        if argLead !=# a:ArgLead
        let cmdLine = strpart(a:CmdLine, 0, a:CursorPos-strlen(a:ArgLead)) .
        \ argLead . strpart(a:CmdLine, a:CursorPos)
        exec "normal! :\<C-\>e'".cmdLine."'\<CR>"
        call setcmdpos(a:CursorPos+(strlen(argLead) - strlen(a:ArgLead)))
        return ''
        endif
        return ''
        endfunction

        Type:

        :Cmd %<Tab>

        --
        Thanks,
        Hari




        __________________________________
        Do you Yahoo!?
        New and Improved Yahoo! Mail - 100MB free storage!
        http://promotions.yahoo.com/new_mail
      • Bram Moolenaar
        ... The user-command completion function was not prepared for invoking getcmdline() recursively. I ll make a patch. -- TIM: Too late. ARTHUR: What? TIM:
        Message 3 of 7 , Jul 2, 2004
          Hari Krishna Dara wrote:

          > But prefixing the command with : is interesting. I just tried it and
          > oops... it made Vim crash. If this is the expected way to execute a
          > command-line-mode normal command, then the bug needs to be fixed. Here
          > is how you can reproduce the crash:

          The user-command completion function was not prepared for invoking
          getcmdline() recursively. I'll make a patch.

          --
          TIM: Too late.
          ARTHUR: What?
          TIM: There he is!
          [They all turn,, and see a large white RABBIT lollop a few yards out of the
          cave. Accompanied by terrifying chord and jarring metallic monster noise.]
          "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

          /// Bram Moolenaar -- Bram@... -- http://www.Moolenaar.net \\\
          /// Sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
          \\\ Project leader for A-A-P -- http://www.A-A-P.org ///
          \\\ Buy at Amazon and help AIDS victims -- http://ICCF.nl/click1.html ///
        • Hari Krishna Dara
          ... Thanks Bram. Here is a much simpler way to reproduce the crash (I ... =ExecNormal() function! ExecNormal() normal! :ls return endfunction --
          Message 4 of 7 , Jul 9, 2004
            On Fri, 2 Jul 2004 at 10:05pm, Bram Moolenaar wrote:

            >
            > Hari Krishna Dara wrote:
            >
            > > But prefixing the command with : is interesting. I just tried it and
            > > oops... it made Vim crash. If this is the expected way to execute a
            > > command-line-mode normal command, then the bug needs to be fixed. Here
            > > is how you can reproduce the crash:
            >
            > The user-command completion function was not prepared for invoking
            > getcmdline() recursively. I'll make a patch.
            >
            >

            Thanks Bram. Here is a much simpler way to reproduce the crash (I
            presume it is the same crash):


            :<C-R>=
            =ExecNormal()<CR>

            function! ExecNormal()
            normal! :ls<CR>
            return ''
            endfunction


            --
            Thanks,
            Hari



            __________________________________
            Do you Yahoo!?
            Yahoo! Mail is new and improved - Check it out!
            http://promotions.yahoo.com/new_mail
          • Bram Moolenaar
            ... This is actually another problem. Thanks for finding it. I ll make another patch. -- CART DRIVER: Bring out your dead! We follow the cart through a
            Message 5 of 7 , Jul 9, 2004
              Hari Krishna Dara wrote:

              > > > But prefixing the command with : is interesting. I just tried it and
              > > > oops... it made Vim crash. If this is the expected way to execute a
              > > > command-line-mode normal command, then the bug needs to be fixed. Here
              > > > is how you can reproduce the crash:
              > >
              > > The user-command completion function was not prepared for invoking
              > > getcmdline() recursively. I'll make a patch.
              >
              > Thanks Bram. Here is a much simpler way to reproduce the crash (I
              > presume it is the same crash):
              >
              >
              > :<C-R>=
              > =ExecNormal()<CR>
              >
              > function! ExecNormal()
              > normal! :ls<CR>
              > return ''
              > endfunction

              This is actually another problem. Thanks for finding it. I'll make
              another patch.

              --
              CART DRIVER: Bring out your dead!
              We follow the cart through a wretched, impoverished plague-ridden village.
              A few starved mongrels run about in the mud scavenging. In the open
              doorway of one house perhaps we jug glimpse a pair of legs dangling from
              the ceiling. In another doorway an OLD WOMAN is beating a cat against a
              wall rather like one does with a mat. The cart passes round a dead donkey
              or cow in the mud. And a MAN tied to a cart is being hammered to death by
              four NUNS with huge mallets.
              "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

              /// Bram Moolenaar -- Bram@... -- http://www.Moolenaar.net \\\
              /// Sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
              \\\ Project leader for A-A-P -- http://www.A-A-P.org ///
              \\\ Buy at Amazon and help AIDS victims -- http://ICCF.nl/click1.html ///
            Your message has been successfully submitted and would be delivered to recipients shortly.