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

How does a func determine if $ was used in a -- VISUAL BLOCK -- ?

Expand Messages
  • Jason Smigiel
    How do I write a function that can determine if $ was used while selecting a -- VISUAL BLOCK -- ? (The $ selects all the text from the current column until
    Message 1 of 5 , Nov 29, 2001
    • 0 Attachment
      How do I write a function that can determine if '$' was used while
      selecting a -- VISUAL BLOCK -- ? (The $ selects all the text
      from the current column until end of line)

      Here's a detailed example that further illustrates my question.
      With the sample text:

      abc firstzhort
      really long line efgh
      sorta short line

      The command :execute "normal! /zhort\<CR>\<C-V>jj$x"
      produces this output:

      abc first
      really lon
      sorta shor

      But if I try to achieve the same result using a function that
      accepts a "-- VISUAL BLOCK --", I don't know how to distinguish
      between a normal rectangular block, or a block where a "$" was used
      to select to the end of every line.

      function! TestVisBlock() range abort
      " get the top left selection mark
      let x1=virtcol("'<")
      let y1=line("'<") - 1

      " get the bottom right selection mark
      let x2=virtcol("'>")
      let y2=line("'>")

      " select the area, and cut it out
      execute "normal! ".y1."G0".x1."l\<C-V>".y2."G0".x2."lx"

      "but depending on whether or not $ was used in the visual block, the
      "function might need to do this instead:
      " execute "normal! ".y1."G0".x1."l\<C-V>".y2."G$lx"
      endfunction


      Now execute this command, that utilizes the TestVisBlock function:

      :execute "normal! /zhort\<CR>\<C-V>jj$:call TestVisBlock()\<CR>"

      and the result is:

      abc first
      really lonefgh <-- note the trailing "efgh"
      sorta shor


      Thanks for any help,
      Jason
    • Charles E. Campbell
      ... Your function could do something like norm ` let rght= virtcol( . ) ..scan through lines line(
      Message 2 of 5 , Nov 30, 2001
      • 0 Attachment
        Thus saith Jason Smigiel:
        > How do I write a function that can determine if '$' was used while
        > selecting a -- VISUAL BLOCK -- ?

        ---------------------------------------------------------------------

        Your function could do something like

        norm `>
        let rght= virtcol(".")

        ..scan through lines line("'<") through line("'>"):
        if virtcol("$") != rght
        echo "Throwing hissy fit :<"
        endif

        Regards,
        Chip Campbell

        --
        Charles E Campbell, Jr, PhD _ __ __
        Goddard Space Flight Center / /_/\_\_/ /
        cec@... /_/ \/_//_/
        PGP public key: http://www.erols.com/astronaut/pgp.html
      • Benji Fisher
        ... I do not see why that will work. I do have a couple of suggestions. 1. Yank the Visual selection into a register. Then count the number of characters in
        Message 3 of 5 , Nov 30, 2001
        • 0 Attachment
          On Friday, November 30, 2001, at 11:56 AM, Charles E. Campbell wrote:

          > Thus saith Jason Smigiel:
          >> How do I write a function that can determine if '$' was used while
          >> selecting a -- VISUAL BLOCK -- ?
          >
          > ---------------------------------------------------------------------
          >
          > Your function could do something like
          >
          > norm `>
          > let rght= virtcol(".")
          >
          > ..scan through lines line("'<") through line("'>"):
          > if virtcol("$") != rght
          > echo "Throwing hissy fit :<"
          > endif

          I do not see why that will work. I do have a couple of suggestions.

          1. Yank the Visual selection into a register. Then count the number of
          characters in each line of the register. This will get confused by
          tabs. :-(

          2. Add a long line after the current line. Then restore the Visual
          selection, and move to the line you added. Check whether the cursor is
          on the last character of your long line. Something like

          :<C-U>put='abcdefghijklmnopqrstuvwxyz'
          :execute "normal! gvj\<Esc>"
          :if col(".") == col("$") ...

          HTH --Benji Fisher
        • Charles E. Campbell
          ... let l= line(
          Message 4 of 5 , Nov 30, 2001
          • 0 Attachment
            Thus saith Jason Smigiel:
            > How do I write a function that can determine if '$' was used while
            > selecting a -- VISUAL BLOCK -- ?

            On Friday, November 30, 2001, at 11:56 AM, Charles E. Campbell wrote:
            > Your function could do something like
            >
            > norm `>
            > let rght= virtcol(".")
            >
            let l= line("'<")
            while l <= line("'>")
            > if virtcol("$") != rght
            > echo "Throwing hissy fit :<"
            > endif
            norm! j
            endwhile

            Thus saith Benji Fisher:
            > I do not see why that will work. I do have a couple of suggestions.

            Ah, but it *does* work, except for the case where the right-hand-side
            of all the lines are all justfied (ie. of the same length). Admittedly
            its a brute force check, however. I've even used a variant of it in
            my latest <visincr.vim> (which handles generating columns of increasing
            or decreasing integers).

            > 1. Yank the Visual selection into a register. Then count the number of
            > characters in each line of the register. This will get confused by
            > tabs. :-(

            That's why I used virtcol() above.

            > 2. Add a long line after the current line. Then restore the Visual
            > selection, and move to the line you added. Check whether the cursor is
            > on the last character of your long line. Something like

            That would take care of the justified right-hand-side problem I
            mentioned.

            Regards,
            Chip Campbell

            --
            Charles E Campbell, Jr, PhD _ __ __
            Goddard Space Flight Center / /_/\_\_/ /
            cec@... /_/ \/_//_/
            PGP public key: http://www.erols.com/astronaut/pgp.html
          • Benji Fisher
            ... I see now: you do a :normal j and a :let l=l+1 inside the loop. (Without the latter, you have a slight problem.) --Benji Fisher
            Message 5 of 5 , Dec 1, 2001
            • 0 Attachment
              "Charles E. Campbell" wrote:
              >
              > Thus saith Jason Smigiel:
              > > How do I write a function that can determine if '$' was used while
              > > selecting a -- VISUAL BLOCK -- ?
              >
              > On Friday, November 30, 2001, at 11:56 AM, Charles E. Campbell wrote:
              > > Your function could do something like
              > >
              > > norm `>
              > > let rght= virtcol(".")
              > >
              > let l= line("'<")
              > while l <= line("'>")
              > > if virtcol("$") != rght
              > > echo "Throwing hissy fit :<"
              > > endif
              > norm! j
              > endwhile
              >
              > Thus saith Benji Fisher:
              > > I do not see why that will work. I do have a couple of suggestions.
              >
              > Ah, but it *does* work, except for the case where the right-hand-side
              > of all the lines are all justfied (ie. of the same length). Admittedly
              > its a brute force check, however. I've even used a variant of it in
              > my latest <visincr.vim> (which handles generating columns of increasing
              > or decreasing integers).

              I see now: you do a ":normal j" and a ":let l=l+1" inside the loop.
              (Without the latter, you have a slight problem.)

              --Benji Fisher
            Your message has been successfully submitted and would be delivered to recipients shortly.