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

Motion for C functions

Expand Messages
  • Marco
    Hi, I often need to uncomment or copy a C function and I wonder if there s a motion for that. What I do at the moment is [[{v][y which seems rather verbose and
    Message 1 of 6 , Mar 22, 2013
    • 0 Attachment
      Hi,

      I often need to uncomment or copy a C function and I wonder if
      there's a motion for that. What I do at the moment is

      [[{v][y

      which seems rather verbose and a lot to type for just “copy this
      function”. The following does not work for some reason:

      [[{y][

      The closing brace is missing. I tried

      ya[

      which seems logical, but it also does not work. If the functions do
      not contain empty lines

      yap

      does the job, but only in case there are no empty lines. Here an
      example of such a function:

      template <typename Stream>
      void do_read( Stream& stream, std::vector<char>& buf )
      {
      boost::asio::async_read(
      stream, boost::asio::buffer( buf ), boost::bind( read_handler ));

      stream.get_io_service().reset();
      stream.get_io_service().run();

      std::cout << "done" << std::endl;
      }

      Marco
    • Paul Isambert
      ... You can use an omap to do the same (with linewise visual mode to include the closing brace): onoremap af : normal! [[{V][ Then yaf yanks the
      Message 2 of 6 , Mar 22, 2013
      • 0 Attachment
        Marco <netuse@...> a écrit:
        > Hi,
        >
        > I often need to uncomment or copy a C function and I wonder if
        > there's a motion for that. What I do at the moment is
        >
        > [[{v][y
        >
        > which seems rather verbose and a lot to type for just “copy this
        > function”.

        You can use an omap to do the same (with linewise visual mode to include the
        closing brace):

        onoremap af :<C-U>normal! [[{V][<CR>

        Then yaf yanks the current function.

        Best,
        Paul

        --
        --
        You received this message from the "vim_use" 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

        ---
        You received this message because you are subscribed to the Google Groups "vim_use" group.
        To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
        For more options, visit https://groups.google.com/groups/opt_out.
      • Marco
        ... This works, thanks. However, this raises more questions: Why does the following implementation of “if” copy one line more than necessary? If I run yif
        Message 3 of 6 , Mar 22, 2013
        • 0 Attachment
          On 2013–03–22 Paul Isambert wrote:

          > You can use an omap to do the same (with linewise visual mode to include the
          > closing brace):
          >
          > onoremap af :<C-U>normal! [[{V][<CR>
          >
          > Then yaf yanks the current function.

          This works, thanks. However, this raises more questions:

          Why does the following implementation of “if” copy one line more
          than necessary? If I run yif it says “8 lines yanked”. If I run yi{
          it says “7 lines yanked”.

          onoremap if :<C-U>normal! [[vi{<cr>

          What is conceptually wrong with yi[[ or yi[m when it works with
          other motions like yis or yi{? Since vim has an understanding of
          functions (determined by the outermost brace pair probably) why is
          there no built in way to use this information for a motion?

          Marco
        • Paul Isambert
          ... To me this is simply wrong (and shouldn’t yank a single line) since you’re outside the braced block once [[ is performed, and i{ selects the interior
          Message 4 of 6 , Mar 22, 2013
          • 0 Attachment
            Marco <netuse@...> a écrit:
            > On 2013–03–22 Paul Isambert wrote:
            >
            > > You can use an omap to do the same (with linewise visual mode to include the
            > > closing brace):
            > >
            > > onoremap af :<C-U>normal! [[{V][<CR>
            > >
            > > Then yaf yanks the current function.
            >
            > This works, thanks. However, this raises more questions:
            >
            > Why does the following implementation of “if” copy one line more
            > than necessary? If I run yif it says “8 lines yanked”. If I run yi{
            > it says “7 lines yanked”.
            >
            > onoremap if :<C-U>normal! [[vi{<cr>

            To me this is simply wrong (and shouldn’t yank a single line) since
            you’re outside the braced block once [[ is performed, and i{ selects
            the interior of a braced block.

            > What is conceptually wrong with yi[[ or yi[m when it works with
            > other motions like yis or yi{?

            iX and aX selects text object X; there is no selectable text object [[
            and [m. The list of existing text objects is in “help text-objects”.

            > Since vim has an understanding of
            > functions (determined by the outermost brace pair probably) why is
            > there no built in way to use this information for a motion?

            That I don’t know. You can script it, though!

            Best,
            Paul

            --
            --
            You received this message from the "vim_use" 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

            ---
            You received this message because you are subscribed to the Google Groups "vim_use" group.
            To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
            For more options, visit https://groups.google.com/groups/opt_out.
          • Ben Fritz
            ... I don t think Vim does have an understanding of functions. It knows about { or } in the first column (the [[ and ][ motions which you re using in your
            Message 5 of 6 , Mar 22, 2013
            • 0 Attachment
              On Friday, March 22, 2013 7:42:41 AM UTC-5, Marco wrote:
              > Since vim has an understanding of
              > functions (determined by the outermost brace pair probably) why is
              > there no built in way to use this information for a motion?
              >

              I don't think Vim does have an understanding of functions. It knows about { or } in the first column (the [[ and ][ motions which you're using in your mapping).

              Also, your example mapping:

              onoremap if :<C-U>normal! [[vi{<cr>

              On your example text:

              template <typename Stream>
              void do_read( Stream& stream, std::vector<char>& buf )
              {
              boost::asio::async_read(
              stream, boost::asio::buffer( buf ), boost::bind( read_handler ));

              stream.get_io_service().reset();
              stream.get_io_service().run();

              std::cout << "done" << std::endl;
              }

              Only selects 7 lines, unless there is a space character after the opening {.

              ...I see why.

              I have 'selection' set to "exclusive". If I change it to "inclusive" then I get 8 lines.

              Looking at WHAT gets copied with yif leads me to the following solution:

              onoremap if :<C-U>normal! [[vi{h<cr>

              This will work if 'selection' is set to "inclusive". If "exclusive" then you need to leave off the h as you did before. If you want to make your mapping generic you'll probably need to use an <expr> mapping which checks the value of the 'selection' option.

              --
              --
              You received this message from the "vim_use" 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

              ---
              You received this message because you are subscribed to the Google Groups "vim_use" group.
              To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
              For more options, visit https://groups.google.com/groups/opt_out.
            • Marco
              ... You re totally right. But vim has a mechanism to navigate functions, given they are formatted in a particular way. And that was what I was referring to.
              Message 6 of 6 , Mar 22, 2013
              • 0 Attachment
                On 2013–03–22 Ben Fritz wrote:

                > I don't think Vim does have an understanding of functions.

                You're totally right. But vim has a mechanism to navigate functions,
                given they are formatted in a particular way. And that was what I
                was referring to.

                > It knows about { or } in the first column (the [[ and ][ motions
                > which you're using in your mapping).

                Indeed, the column matters.

                > I have 'selection' set to "exclusive". If I change it to
                > "inclusive" then I get 8 lines.

                I didn't know about these options before.

                > Looking at WHAT gets copied with yif leads me to the following
                > solution:
                >
                > onoremap if :<C-U>normal! [[vi{h<cr>
                >
                > This will work if 'selection' is set to "inclusive". If
                > "exclusive" then you need to leave off the h as you did before. If
                > you want to make your mapping generic you'll probably need to use
                > an <expr> mapping which checks the value of the 'selection'
                > option.

                That's not necessary. I wasn't even aware of the existence of this
                setting. After having read the help page, it seems unlikely that I
                will change it. Thanks for your help.

                Marco
              Your message has been successfully submitted and would be delivered to recipients shortly.